Index: ps/trunk/binaries/data/mods/public/simulation/components/Upgrade.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Upgrade.js +++ ps/trunk/binaries/data/mods/public/simulation/components/Upgrade.js @@ -56,24 +56,8 @@ Upgrade.prototype.Init = function() { - this.upgrading = false; - this.completed = false; this.elapsedTime = 0; - this.timer = undefined; this.expendedResources = {}; - - this.upgradeTemplates = {}; - - for (let choice in this.template) - { - let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); - let name = this.template[choice].Entity; - if (cmpIdentity) - name = name.replace(/\{civ\}/g, cmpIdentity.GetCiv()); - if (this.upgradeTemplates.name) - warn("Upgrade Component: entity " + this.entity + " has two upgrades to the same entity, only the last will be used."); - this.upgradeTemplates[name] = choice; - } }; // This will also deal with the "OnDestroy" case. @@ -83,7 +67,32 @@ this.CancelUpgrade(msg.from); if (msg.to != INVALID_PLAYER) + { this.owner = msg.to; + this.DetermineUpgrades(); + } +}; + +Upgrade.prototype.DetermineUpgrades = function() +{ + this.upgradeTemplates = {}; + + for (const choice in this.template) + { + const nativeCiv = Engine.QueryInterface(this.entity, IID_Identity).GetCiv(); + const playerCiv = QueryPlayerIDInterface(this.owner, IID_Identity).GetCiv(); + const name = this.template[choice].Entity. + replace(/\{native\}/g, nativeCiv). + replace(/\{civ\}/g, playerCiv); + + if (!Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).TemplateExists(name)) + continue; + + if (this.upgradeTemplates[name]) + warn("Upgrade Component: entity " + this.entity + " has two upgrades to the same entity, only the last will be used."); + + this.upgradeTemplates[name] = choice; + } }; Upgrade.prototype.ChangeUpgradedEntityCount = function(amount) @@ -128,22 +137,19 @@ { let ret = []; - let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); - - for (let option in this.template) + for (const option in this.upgradeTemplates) { - let choice = this.template[option]; - let templateName = cmpIdentity ? choice.Entity.replace(/\{civ\}/g, cmpIdentity.GetCiv()) : choice.Entity; + const choice = this.template[this.upgradeTemplates[option]]; let cost = {}; if (choice.Cost) - cost = this.GetResourceCosts(templateName); + cost = this.GetResourceCosts(option); if (choice.Time) - cost.time = this.GetUpgradeTime(templateName); + cost.time = this.GetUpgradeTime(option); let hasCost = choice.Cost || choice.Time; ret.push({ - "entity": templateName, + "entity": option, "icon": choice.Icon || undefined, "cost": hasCost ? cost : undefined, "tooltip": choice.Tooltip || undefined, @@ -161,7 +167,7 @@ let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); cmpTimer.CancelTimer(this.timer); - this.timer = undefined; + delete this.timer; }; Upgrade.prototype.IsUpgrading = function() @@ -289,7 +295,7 @@ cmpVisual.SelectAnimation("idle", false, 1.0); } - this.upgrading = false; + delete this.upgrading; this.CancelTimer(); this.SetElapsedTime(0); }; Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js @@ -82,7 +82,8 @@ "GetPlayerByID": pID => 10 // Called in helpers/player.js::QueryPlayerIDInterface(), as part of Tests T2 and T5. }); AddMock(SYSTEM_ENTITY, IID_TemplateManager, { - "GetTemplate": () => template // Called in components/Upgrade.js::ChangeUpgradedEntityCount(). + "GetTemplate": () => template, // Called in components/Upgrade.js::ChangeUpgradedEntityCount(). + "TemplateExists": (templ) => true }); AddMock(SYSTEM_ENTITY, IID_Timer, { "SetInterval": () => 1, // Called in components/Upgrade.js::Upgrade(). @@ -113,6 +114,9 @@ "GetPlayerID": () => playerID, // Called in helpers/Player.js::QueryOwnerInterface() (and several times below). "TrySubtractResources": () => true // Called in components/Upgrade.js::Upgrade(). }); +AddMock(10, IID_Identity, { + "GetCiv": () => civCode +}); // Create an entity with an Upgrade component: AddMock(20, IID_Ownership, { @@ -126,6 +130,7 @@ }); let cmpUpgrade = ConstructComponent(20, "Upgrade", template.Upgrade); cmpUpgrade.owner = playerID; +cmpUpgrade.OnOwnershipChanged({ "to": playerID }); /** * Now to start the test proper Index: ps/trunk/binaries/data/mods/public/simulation/templates/structures/han/civil_centre.xml =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/templates/structures/han/civil_centre.xml +++ ps/trunk/binaries/data/mods/public/simulation/templates/structures/han/civil_centre.xml @@ -29,7 +29,7 @@ - structures/han/civil_centre_court + structures/{civ}/civil_centre_court This greatly increases the health, capture resistance, and garrison capacity of this specific Civic Center. Unlock training of Heroes here and reduce its research and batch training times by half. phase_city