Index: ps/trunk/binaries/data/mods/public/simulation/components/EntityLimits.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/EntityLimits.js +++ ps/trunk/binaries/data/mods/public/simulation/components/EntityLimits.js @@ -145,7 +145,6 @@ } }; - EntityLimits.prototype.AllowedToCreate = function(limitType, category, count) { // Allow unspecified categories and those with no limit @@ -192,6 +191,32 @@ return this.AllowedToCreate(TRAINING, category, count); }; +/** + * @param {number} ent - id of the entity which would be replaced. + * @param {string} template - name of the new template. + * @return {boolean} - whether we can replace ent. + */ +EntityLimits.prototype.AllowedToReplace = function(ent, template) +{ + let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); + let templateFrom = cmpTemplateManager.GetTemplate(cmpTemplateManager.GetCurrentTemplateName(ent)); + let templateTo = cmpTemplateManager.GetTemplate(template); + + if (templateTo.TrainingRestrictions) + { + let category = templateTo.TrainingRestrictions.Category; + return this.AllowedToCreate(TRAINING, category, templateFrom.TrainingRestrictions && templateFrom.TrainingRestrictions.Category == category ? 0 : 1); + } + + if (templateTo.BuildRestrictions) + { + let category = templateTo.BuildRestrictions.Category; + return this.AllowedToCreate(BUILD, category, templateFrom.BuildRestrictions && templateFrom.BuildRestrictions.Category == category ? 0 : 1); + } + + return true; +}; + EntityLimits.prototype.OnGlobalOwnershipChanged = function(msg) { // check if we are adding or removing an entity from this player Index: ps/trunk/binaries/data/mods/public/simulation/helpers/Commands.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/helpers/Commands.js +++ ps/trunk/binaries/data/mods/public/simulation/helpers/Commands.js @@ -722,11 +722,8 @@ } // Check entity limits - var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); - var template = cmpTemplateManager.GetTemplate(cmd.template); var cmpEntityLimits = QueryPlayerIDInterface(player, IID_EntityLimits); - if (template.TrainingRestrictions && !cmpEntityLimits.AllowedToTrain(template.TrainingRestrictions.Category, 1) || - template.BuildRestrictions && !cmpEntityLimits.AllowedToBuild(template.BuildRestrictions.Category)) + if (cmpEntityLimits && !cmpEntityLimits.AllowedToReplace(ent, cmd.template)) { if (g_DebugCommands) warn("Invalid command: build limits check failed for player " + player + ": " + uneval(cmd));