Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Mirage.js
const VIS_HIDDEN = 0; | const VIS_HIDDEN = 0; | ||||
const VIS_FOGGED = 1; | const VIS_FOGGED = 1; | ||||
const VIS_VISIBLE = 2; | const VIS_VISIBLE = 2; | ||||
function Mirage() {} | function Mirage() {} | ||||
Mirage.prototype.Schema = | Mirage.prototype.Schema = | ||||
"<a:help>Mirage entities replace real entities in the fog-of-war.</a:help>" + | "<a:help>Mirage entities replace real entities in the fog-of-war.</a:help>" + | ||||
"<empty/>"; | "<empty/>"; | ||||
Mirage.prototype.Init = function() | Mirage.prototype.Init = function() | ||||
{ | { | ||||
this.player = null; | |||||
this.parent = INVALID_ENTITY; | this.parent = INVALID_ENTITY; | ||||
this.player = null; | |||||
this.miragedIids = new Set(); | this.miragedIids = new Map(); | ||||
this.classesList = []; | |||||
this.numBuilders = 0; | |||||
this.buildTime = {}; | |||||
this.maxHitpoints = null; | |||||
this.hitpoints = null; | |||||
this.repairable = null; | |||||
this.unhealable = null; | |||||
this.injured = null; | |||||
this.capturePoints = []; | |||||
this.maxCapturePoints = 0; | |||||
this.maxAmount = null; | |||||
this.amount = null; | |||||
this.type = null; | |||||
this.isInfinite = null; | |||||
this.killBeforeGather = null; | |||||
this.maxGatherers = null; | |||||
this.numGatherers = null; | |||||
this.traders = null; | |||||
this.marketType = null; | |||||
this.internationalBonus = null; | |||||
}; | }; | ||||
Mirage.prototype.SetParent = function(ent) | Mirage.prototype.SetParent = function(ent) | ||||
{ | { | ||||
this.parent = ent; | this.parent = ent; | ||||
}; | }; | ||||
Mirage.prototype.GetParent = function() | Mirage.prototype.GetParent = function() | ||||
Show All 11 Lines | Mirage.prototype.GetPlayer = function() | ||||
return this.player; | return this.player; | ||||
}; | }; | ||||
Mirage.prototype.Mirages = function(iid) | Mirage.prototype.Mirages = function(iid) | ||||
{ | { | ||||
return this.miragedIids.has(iid); | return this.miragedIids.has(iid); | ||||
}; | }; | ||||
Mirage.prototype.Get = function(iid) | |||||
{ | |||||
return this.miragedIids.get(iid); | |||||
}; | |||||
// ============================ | // ============================ | ||||
// Parent entity data | // Parent entity data | ||||
Mirage.prototype.CopyIdentity = function(cmpIdentity) | Mirage.prototype.CopyIdentity = function(cmpIdentity) | ||||
{ | { | ||||
this.miragedIids.add(IID_Identity); | |||||
// Mirages don't get identity classes via the template-filter, so that code can query | // Mirages don't get identity classes via the template-filter, so that code can query | ||||
// identity components via Engine.QueryInterface without having to explicitly check for mirages. | // identity components via Engine.QueryInterface without having to explicitly check for mirages. | ||||
// This is cloned as otherwise we get a reference to Identity's property, | // This is cloned as otherwise we get a reference to Identity's property, | ||||
// and that array is deleted when serializing (as it's not seralized), which ends in OOS. | // and that array is deleted when serializing (as it's not seralized), which ends in OOS. | ||||
this.classesList = clone(cmpIdentity.GetClassesList()); | let classes = clone(cmpIdentity.GetClassesList()); | ||||
}; | |||||
Mirage.prototype.GetClassesList = function() { return this.classesList; }; | let data = { | ||||
"GetClassesList": () => classes | |||||
} | |||||
this.miragedIids.set(IID_Identity, data); | |||||
}; | |||||
// Foundation data | // Foundation data | ||||
Mirage.prototype.CopyFoundation = function(cmpFoundation) | Mirage.prototype.CopyFoundation = function(cmpFoundation) | ||||
{ | { | ||||
this.miragedIids.add(IID_Foundation); | let numBuilders = cmpFoundation.GetNumBuilders(); | ||||
this.numBuilders = cmpFoundation.GetNumBuilders(); | let buildTime = cmpFoundation.GetBuildTime(); | ||||
this.buildTime = cmpFoundation.GetBuildTime(); | |||||
let data = { | |||||
"GetNumBuilders": () => numBuilders, | |||||
"GetBuildTime": () => buildTime | |||||
}; | }; | ||||
Mirage.prototype.GetNumBuilders = function() { return this.numBuilders; }; | this.miragedIids.set(IID_Foundation, data); | ||||
Mirage.prototype.GetBuildTime = function() { return this.buildTime; }; | }; | ||||
// Repairable data (numBuilders and buildTime shared with foundation as entities can't have both) | // Repairable data (numBuilders and buildTime shared with foundation as entities can't have both) | ||||
Mirage.prototype.CopyRepairable = function(cmpRepairable) | Mirage.prototype.CopyRepairable = function(cmpRepairable) | ||||
{ | { | ||||
this.miragedIids.add(IID_Repairable); | let numBuilders = cmpRepairable.GetNumBuilders(); | ||||
this.numBuilders = cmpRepairable.GetNumBuilders(); | let buildTime = cmpRepairable.GetBuildTime(); | ||||
this.buildTime = cmpRepairable.GetBuildTime(); | |||||
let data = { | |||||
"GetNumBuilders": () => numBuilders, | |||||
"GetBuildTime": () => buildTime | |||||
}; | |||||
this.miragedIids.set(IID_Repairable, data); | |||||
}; | }; | ||||
// Health data | // Health data | ||||
Mirage.prototype.CopyHealth = function(cmpHealth) | Mirage.prototype.CopyHealth = function(cmpHealth) | ||||
{ | { | ||||
this.miragedIids.add(IID_Health); | let maxHitpoints = cmpHealth.GetMaxHitpoints(); | ||||
this.maxHitpoints = cmpHealth.GetMaxHitpoints(); | let hitpoints = cmpHealth.GetHitpoints(); | ||||
this.hitpoints = cmpHealth.GetHitpoints(); | let repairable = cmpHealth.IsRepairable(); | ||||
this.repairable = cmpHealth.IsRepairable(); | let injured = cmpHealth.IsInjured(); | ||||
this.injured = cmpHealth.IsInjured(); | let unhealable = cmpHealth.IsUnhealable(); | ||||
this.unhealable = cmpHealth.IsUnhealable(); | |||||
let data = { | |||||
"GetMaxHitpoints": () => maxHitpoints, | |||||
"GetHitpoints": () => hitpoints, | |||||
"IsRepairable": () => repairable, | |||||
"IsInjured": () => injured, | |||||
"IsUnhealable": () => unhealable | |||||
}; | }; | ||||
Mirage.prototype.GetMaxHitpoints = function() { return this.maxHitpoints; }; | this.miragedIids.set(IID_Health, data); | ||||
Mirage.prototype.GetHitpoints = function() { return this.hitpoints; }; | }; | ||||
Mirage.prototype.IsRepairable = function() { return this.repairable; }; | |||||
Mirage.prototype.IsInjured = function() { return this.injured; }; | |||||
Mirage.prototype.IsUnhealable = function() { return this.unhealable; }; | |||||
// Capture data | // Capture data | ||||
Mirage.prototype.CopyCapturable = function(cmpCapturable) | Mirage.prototype.CopyCapturable = function(cmpCapturable) | ||||
{ | { | ||||
this.miragedIids.add(IID_Capturable); | let capturePoints = clone(cmpCapturable.GetCapturePoints()); | ||||
this.capturePoints = clone(cmpCapturable.GetCapturePoints()); | let maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); | ||||
this.maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); | |||||
}; | |||||
Mirage.prototype.GetMaxCapturePoints = function() { return this.maxCapturePoints; }; | let data = { | ||||
Mirage.prototype.GetCapturePoints = function() { return this.capturePoints; }; | "CanCapture": (player) => cmpCapturable.CanCapture(player), | ||||
"GetCapturePoints": () => capturePoints, | |||||
"GetMaxCapturePoints": () => maxCapturePoints | |||||
}; | |||||
Mirage.prototype.CanCapture = Capturable.prototype.CanCapture; | this.miragedIids.set(IID_Capturable, data); | ||||
}; | |||||
// ResourceSupply data | // ResourceSupply data | ||||
Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply) | Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply) | ||||
{ | { | ||||
this.miragedIids.add(IID_ResourceSupply); | let maxAmount = cmpResourceSupply.GetMaxAmount(); | ||||
this.maxAmount = cmpResourceSupply.GetMaxAmount(); | let amount = cmpResourceSupply.GetCurrentAmount(); | ||||
this.amount = cmpResourceSupply.GetCurrentAmount(); | let type = cmpResourceSupply.GetType(); | ||||
this.type = cmpResourceSupply.GetType(); | let isInfinite = cmpResourceSupply.IsInfinite(); | ||||
this.isInfinite = cmpResourceSupply.IsInfinite(); | let killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); | ||||
this.killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); | let maxGatherers = cmpResourceSupply.GetMaxGatherers(); | ||||
this.maxGatherers = cmpResourceSupply.GetMaxGatherers(); | let numGatherers = cmpResourceSupply.GetNumGatherers(); | ||||
this.numGatherers = cmpResourceSupply.GetNumGatherers(); | |||||
}; | let data = { | ||||
"GetMaxAmount": () => maxAmount, | |||||
Mirage.prototype.GetMaxAmount = function() { return this.maxAmount; }; | "GetCurrentAmount": () => amount, | ||||
Mirage.prototype.GetCurrentAmount = function() { return this.amount; }; | "GetType": () => type, | ||||
Mirage.prototype.GetType = function() { return this.type; }; | "IsInfinite": () => isInfinite, | ||||
Mirage.prototype.IsInfinite = function() { return this.isInfinite; }; | "GetKillBeforeGather": () => killBeforeGather, | ||||
Mirage.prototype.GetKillBeforeGather = function() { return this.killBeforeGather; }; | "GetMaxGatherers": () => maxGatherers, | ||||
Mirage.prototype.GetMaxGatherers = function() { return this.maxGatherers; }; | "GetNumGatherers": () => numGatherers | ||||
Mirage.prototype.GetNumGatherers = function() { return this.numGatherers; }; | }; | ||||
this.miragedIids.set(IID_ResourceSupply, data); | |||||
}; | |||||
// Market data | // Market data | ||||
Mirage.prototype.CopyMarket = function(cmpMarket) | Mirage.prototype.CopyMarket = function(cmpMarket) | ||||
{ | { | ||||
this.miragedIids.add(IID_Market); | let traders = new Set(); | ||||
this.traders = new Set(); | |||||
for (let trader of cmpMarket.GetTraders()) | for (let trader of cmpMarket.GetTraders()) | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | ||||
let cmpOwnership = Engine.QueryInterface(trader, IID_Ownership); | let cmpOwnership = Engine.QueryInterface(trader, IID_Ownership); | ||||
if (!cmpTrader || !cmpOwnership) | if (!cmpTrader || !cmpOwnership) | ||||
{ | { | ||||
cmpMarket.RemoveTrader(trader); | cmpMarket.RemoveTrader(trader); | ||||
continue; | continue; | ||||
} | } | ||||
if (this.player != cmpOwnership.GetOwner()) | if (this.player != cmpOwnership.GetOwner()) | ||||
continue; | continue; | ||||
cmpTrader.SwitchMarket(cmpMarket.entity, this.entity); | cmpTrader.SwitchMarket(cmpMarket.entity, this.entity); | ||||
cmpMarket.RemoveTrader(trader); | cmpMarket.RemoveTrader(trader); | ||||
this.AddTrader(trader); | traders.add(trader); | ||||
} | } | ||||
this.marketType = cmpMarket.GetType(); | let marketType = cmpMarket.GetType(); | ||||
this.internationalBonus = cmpMarket.GetInternationalBonus(); | let internationalBonus = cmpMarket.GetInternationalBonus(); | ||||
}; | |||||
Mirage.prototype.HasType = function(type) { return this.marketType.has(type); }; | let data = { | ||||
Mirage.prototype.GetInternationalBonus = function() { return this.internationalBonus; }; | "HasType": (type) => marketType.has(type), | ||||
Mirage.prototype.AddTrader = function(trader) { this.traders.add(trader); }; | "GetInternationalBonus": () => internationalBonus, | ||||
Mirage.prototype.RemoveTrader = function(trader) { this.traders.delete(trader); }; | "AddTrader": (trader) => traders.add(trader), | ||||
"RemoveTrader": (trader) => traders.delete(trader), | |||||
Mirage.prototype.UpdateTraders = function(msg) | "UpdateTraders": (msg) => { | ||||
{ | |||||
let cmpMarket = Engine.QueryInterface(this.parent, IID_Market); | let cmpMarket = Engine.QueryInterface(this.parent, IID_Market); | ||||
if (!cmpMarket) // The parent market does not exist anymore | if (!cmpMarket) | ||||
{ | // The parent market does not exist anymore. | ||||
for (let trader of this.traders) | for (let trader of traders) | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | ||||
if (cmpTrader) | if (cmpTrader) | ||||
cmpTrader.RemoveMarket(this.entity); | cmpTrader.RemoveMarket(this.entity); | ||||
} | } | ||||
return; | else | ||||
} | |||||
// The market becomes visible, switch all traders from the mirage to the market | // The market becomes visible, switch all traders from the mirage to the market | ||||
for (let trader of this.traders) | for (let trader of traders) | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | let cmpTrader = Engine.QueryInterface(trader, IID_Trader); | ||||
if (!cmpTrader) | if (!cmpTrader) | ||||
continue; | continue; | ||||
cmpTrader.SwitchMarket(this.entity, cmpMarket.entity); | cmpTrader.SwitchMarket(this.entity, cmpMarket.entity); | ||||
this.RemoveTrader(trader); | traders.delete(trader); | ||||
cmpMarket.AddTrader(trader); | cmpMarket.AddTrader(trader); | ||||
} | } | ||||
} | |||||
}; | |||||
this.miragedIids.set(IID_Market, data); | |||||
}; | }; | ||||
// ============================ | // ============================ | ||||
Mirage.prototype.OnVisibilityChanged = function(msg) | Mirage.prototype.OnVisibilityChanged = function(msg) | ||||
{ | { | ||||
// Mirages get VIS_HIDDEN when the original entity becomes VIS_VISIBLE. | // Mirages get VIS_HIDDEN when the original entity becomes VIS_VISIBLE. | ||||
if (msg.player != this.player || msg.newVisibility != VIS_HIDDEN) | if (msg.player != this.player || msg.newVisibility != VIS_HIDDEN) | ||||
return; | return; | ||||
if (this.miragedIids.has(IID_Market)) | if (this.miragedIids.has(IID_Market)) | ||||
this.UpdateTraders(msg); | this.miragedIids.get(IID_Market).UpdateTraders(msg); | ||||
if (this.parent == INVALID_ENTITY) | if (this.parent == INVALID_ENTITY) | ||||
Engine.DestroyEntity(this.entity); | Engine.DestroyEntity(this.entity); | ||||
else | else | ||||
Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": this.parent }); | Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": this.parent }); | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_Mirage, "Mirage", Mirage); | Engine.RegisterComponentType(IID_Mirage, "Mirage", Mirage); |
Wildfire Games · Phabricator