Index: binaries/data/mods/public/simulation/components/Mirage.js =================================================================== --- binaries/data/mods/public/simulation/components/Mirage.js +++ binaries/data/mods/public/simulation/components/Mirage.js @@ -10,36 +10,10 @@ Mirage.prototype.Init = function() { - this.player = null; this.parent = INVALID_ENTITY; + this.player = null; - this.miragedIids = new Set(); - - 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; + this.miragedIids = new Map(); }; Mirage.prototype.SetParent = function(ent) @@ -67,102 +41,126 @@ return this.miragedIids.has(iid); }; +Mirage.prototype.Get = function(iid) +{ + return this.miragedIids.get(iid); +}; + // ============================ // Parent entity data 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 // 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, // 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 Mirage.prototype.CopyFoundation = function(cmpFoundation) { - this.miragedIids.add(IID_Foundation); - this.numBuilders = cmpFoundation.GetNumBuilders(); - this.buildTime = cmpFoundation.GetBuildTime(); -}; + let numBuilders = cmpFoundation.GetNumBuilders(); + let buildTime = cmpFoundation.GetBuildTime(); -Mirage.prototype.GetNumBuilders = function() { return this.numBuilders; }; -Mirage.prototype.GetBuildTime = function() { return this.buildTime; }; + let data = { + "GetNumBuilders": () => numBuilders, + "GetBuildTime": () => buildTime + }; + + this.miragedIids.set(IID_Foundation, data); +}; // Repairable data (numBuilders and buildTime shared with foundation as entities can't have both) Mirage.prototype.CopyRepairable = function(cmpRepairable) { - this.miragedIids.add(IID_Repairable); - this.numBuilders = cmpRepairable.GetNumBuilders(); - this.buildTime = cmpRepairable.GetBuildTime(); + let numBuilders = cmpRepairable.GetNumBuilders(); + let buildTime = cmpRepairable.GetBuildTime(); + + let data = { + "GetNumBuilders": () => numBuilders, + "GetBuildTime": () => buildTime + }; + + this.miragedIids.set(IID_Repairable, data); }; // Health data Mirage.prototype.CopyHealth = function(cmpHealth) { - this.miragedIids.add(IID_Health); - this.maxHitpoints = cmpHealth.GetMaxHitpoints(); - this.hitpoints = cmpHealth.GetHitpoints(); - this.repairable = cmpHealth.IsRepairable(); - this.injured = cmpHealth.IsInjured(); - this.unhealable = cmpHealth.IsUnhealable(); -}; + let maxHitpoints = cmpHealth.GetMaxHitpoints(); + let hitpoints = cmpHealth.GetHitpoints(); + let repairable = cmpHealth.IsRepairable(); + let injured = cmpHealth.IsInjured(); + let unhealable = cmpHealth.IsUnhealable(); + + let data = { + "GetMaxHitpoints": () => maxHitpoints, + "GetHitpoints": () => hitpoints, + "IsRepairable": () => repairable, + "IsInjured": () => injured, + "IsUnhealable": () => unhealable + }; -Mirage.prototype.GetMaxHitpoints = function() { return this.maxHitpoints; }; -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; }; + this.miragedIids.set(IID_Health, data); +}; // Capture data Mirage.prototype.CopyCapturable = function(cmpCapturable) { - this.miragedIids.add(IID_Capturable); - this.capturePoints = clone(cmpCapturable.GetCapturePoints()); - this.maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); -}; + let capturePoints = clone(cmpCapturable.GetCapturePoints()); + let maxCapturePoints = cmpCapturable.GetMaxCapturePoints(); -Mirage.prototype.GetMaxCapturePoints = function() { return this.maxCapturePoints; }; -Mirage.prototype.GetCapturePoints = function() { return this.capturePoints; }; + let data = { + "CanCapture": (player) => cmpCapturable.CanCapture(player), + "GetCapturePoints": () => capturePoints, + "GetMaxCapturePoints": () => maxCapturePoints + }; -Mirage.prototype.CanCapture = Capturable.prototype.CanCapture; + this.miragedIids.set(IID_Capturable, data); +}; // ResourceSupply data Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply) { - this.miragedIids.add(IID_ResourceSupply); - this.maxAmount = cmpResourceSupply.GetMaxAmount(); - this.amount = cmpResourceSupply.GetCurrentAmount(); - this.type = cmpResourceSupply.GetType(); - this.isInfinite = cmpResourceSupply.IsInfinite(); - this.killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); - this.maxGatherers = cmpResourceSupply.GetMaxGatherers(); - this.numGatherers = cmpResourceSupply.GetNumGatherers(); -}; - -Mirage.prototype.GetMaxAmount = function() { return this.maxAmount; }; -Mirage.prototype.GetCurrentAmount = function() { return this.amount; }; -Mirage.prototype.GetType = function() { return this.type; }; -Mirage.prototype.IsInfinite = function() { return this.isInfinite; }; -Mirage.prototype.GetKillBeforeGather = function() { return this.killBeforeGather; }; -Mirage.prototype.GetMaxGatherers = function() { return this.maxGatherers; }; -Mirage.prototype.GetNumGatherers = function() { return this.numGatherers; }; + let maxAmount = cmpResourceSupply.GetMaxAmount(); + let amount = cmpResourceSupply.GetCurrentAmount(); + let type = cmpResourceSupply.GetType(); + let isInfinite = cmpResourceSupply.IsInfinite(); + let killBeforeGather = cmpResourceSupply.GetKillBeforeGather(); + let maxGatherers = cmpResourceSupply.GetMaxGatherers(); + let numGatherers = cmpResourceSupply.GetNumGatherers(); + + let data = { + "GetMaxAmount": () => maxAmount, + "GetCurrentAmount": () => amount, + "GetType": () => type, + "IsInfinite": () => isInfinite, + "GetKillBeforeGather": () => killBeforeGather, + "GetMaxGatherers": () => maxGatherers, + "GetNumGatherers": () => numGatherers + }; + + this.miragedIids.set(IID_ResourceSupply, data); +}; // Market data Mirage.prototype.CopyMarket = function(cmpMarket) { - this.miragedIids.add(IID_Market); - this.traders = new Set(); + let traders = new Set(); for (let trader of cmpMarket.GetTraders()) { let cmpTrader = Engine.QueryInterface(trader, IID_Trader); @@ -176,41 +174,41 @@ continue; cmpTrader.SwitchMarket(cmpMarket.entity, this.entity); cmpMarket.RemoveTrader(trader); - this.AddTrader(trader); + traders.add(trader); } - this.marketType = cmpMarket.GetType(); - this.internationalBonus = cmpMarket.GetInternationalBonus(); -}; + let marketType = cmpMarket.GetType(); + let internationalBonus = cmpMarket.GetInternationalBonus(); -Mirage.prototype.HasType = function(type) { return this.marketType.has(type); }; -Mirage.prototype.GetInternationalBonus = function() { return this.internationalBonus; }; -Mirage.prototype.AddTrader = function(trader) { this.traders.add(trader); }; -Mirage.prototype.RemoveTrader = function(trader) { this.traders.delete(trader); }; - -Mirage.prototype.UpdateTraders = function(msg) -{ - let cmpMarket = Engine.QueryInterface(this.parent, IID_Market); - if (!cmpMarket) // The parent market does not exist anymore - { - for (let trader of this.traders) - { - let cmpTrader = Engine.QueryInterface(trader, IID_Trader); - if (cmpTrader) - cmpTrader.RemoveMarket(this.entity); + let data = { + "HasType": (type) => marketType.has(type), + "GetInternationalBonus": () => internationalBonus, + "AddTrader": (trader) => traders.add(trader), + "RemoveTrader": (trader) => traders.delete(trader), + "UpdateTraders": (msg) => { + let cmpMarket = Engine.QueryInterface(this.parent, IID_Market); + if (!cmpMarket) + // The parent market does not exist anymore. + for (let trader of traders) + { + let cmpTrader = Engine.QueryInterface(trader, IID_Trader); + if (cmpTrader) + cmpTrader.RemoveMarket(this.entity); + } + else + // The market becomes visible, switch all traders from the mirage to the market + for (let trader of traders) + { + let cmpTrader = Engine.QueryInterface(trader, IID_Trader); + if (!cmpTrader) + continue; + cmpTrader.SwitchMarket(this.entity, cmpMarket.entity); + traders.delete(trader); + cmpMarket.AddTrader(trader); + } } - return; - } + }; - // The market becomes visible, switch all traders from the mirage to the market - for (let trader of this.traders) - { - let cmpTrader = Engine.QueryInterface(trader, IID_Trader); - if (!cmpTrader) - continue; - cmpTrader.SwitchMarket(this.entity, cmpMarket.entity); - this.RemoveTrader(trader); - cmpMarket.AddTrader(trader); - } + this.miragedIids.set(IID_Market, data); }; // ============================ @@ -222,7 +220,7 @@ return; if (this.miragedIids.has(IID_Market)) - this.UpdateTraders(msg); + this.miragedIids.get(IID_Market).UpdateTraders(msg); if (this.parent == INVALID_ENTITY) Engine.DestroyEntity(this.entity); Index: binaries/data/mods/public/simulation/helpers/Player.js =================================================================== --- binaries/data/mods/public/simulation/helpers/Player.js +++ binaries/data/mods/public/simulation/helpers/Player.js @@ -290,13 +290,13 @@ */ function QueryMiragedInterface(ent, iid) { - var cmp = Engine.QueryInterface(ent, IID_Mirage); - if (cmp && !cmp.Mirages(iid)) + let cmpMirage = Engine.QueryInterface(ent, IID_Mirage); + if (cmpMirage && !cmpMirage.Mirages(iid)) return null; - else if (!cmp) - cmp = Engine.QueryInterface(ent, iid); + else if (!cmpMirage) + return Engine.QueryInterface(ent, iid); - return cmp; + return cmpMirage.Get(iid); } /**