Index: binaries/data/mods/public/maps/scripts/TriggerHelper.js =================================================================== --- binaries/data/mods/public/maps/scripts/TriggerHelper.js +++ binaries/data/mods/public/maps/scripts/TriggerHelper.js @@ -121,7 +121,11 @@ for (let i = 0; i < count; ++i) { - let ent = Engine.AddEntity(template); + const upgradedTemplate = GetUpgradedTemplate(owner, template); + if (upgradedTemplate !== template) + warn(`tried to spawn template '${template}' but upgraded template ${upgradedTemplate} will be spawned instead.`); + + let ent = Engine.AddEntity(upgradedTemplate); let cmpEntPosition = Engine.QueryInterface(ent, IID_Position); if (!cmpEntPosition) { Index: binaries/data/mods/public/simulation/components/Trainer.js =================================================================== --- binaries/data/mods/public/simulation/components/Trainer.js +++ binaries/data/mods/public/simulation/components/Trainer.js @@ -554,7 +554,7 @@ return entMap; } - token = this.GetUpgradedTemplate(token); + token = this.GetUpgradedTemplate(cmpPlayer.GetPlayerID(), token); entMap.set(rawToken, token); updateAllQueuedTemplate(rawToken, token); return entMap; @@ -563,32 +563,6 @@ this.CalculateTrainCostMultiplier(); }; -/* - * Returns the upgraded template name if necessary. - */ -Trainer.prototype.GetUpgradedTemplate = function(templateName) -{ - const cmpPlayer = QueryOwnerInterface(this.entity); - if (!cmpPlayer) - return templateName; - - const cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); - let template = cmpTemplateManager.GetTemplate(templateName); - while (template && template.Promotion !== undefined) - { - const requiredXp = ApplyValueModificationsToTemplate( - "Promotion/RequiredXp", - +template.Promotion.RequiredXp, - cmpPlayer.GetPlayerID(), - template); - if (requiredXp > 0) - break; - templateName = template.Promotion.Entity; - template = cmpTemplateManager.GetTemplate(templateName); - } - return templateName; -}; - Trainer.prototype.CalculateTrainCostMultiplier = function() { for (const res of Resources.GetCodes().concat(["time"])) Index: binaries/data/mods/public/simulation/helpers/Transform.js =================================================================== --- binaries/data/mods/public/simulation/helpers/Transform.js +++ binaries/data/mods/public/simulation/helpers/Transform.js @@ -294,5 +294,30 @@ } } +/** + * @param {number} playerId - the player id to check technologies for. + * @param {string} templateName - the template name. + * @returns the upgraded template name if it exists. + */ +function GetUpgradedTemplate(playerId, templateName) +{ + const cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); + let template = cmpTemplateManager.GetTemplate(templateName); + while (template && template.Promotion !== undefined) + { + const requiredXp = ApplyValueModificationsToTemplate( + "Promotion/RequiredXp", + +template.Promotion.RequiredXp, + playerId, + template); + if (requiredXp > 0) + break; + templateName = template.Promotion.Entity; + template = cmpTemplateManager.GetTemplate(templateName); + } + return templateName; +}; + +Engine.RegisterGlobal("GetUpgradedTemplate", GetUpgradedTemplate); Engine.RegisterGlobal("ChangeEntityTemplate", ChangeEntityTemplate); Engine.RegisterGlobal("ObstructionsBlockingTemplateChange", ObstructionsBlockingTemplateChange);