From b8ec515234ff6913eb4ff9389526dfb10483684e Mon Sep 17 00:00:00 2001 From: historic_bruno Date: Fri, 20 Jul 2012 02:50:43 +0000 Subject: [PATCH] Construction cost should be checked after everything else to avoid messy refunds. This was SVN commit r12166. --- .../public/simulation/helpers/Commands.js | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/binaries/data/mods/public/simulation/helpers/Commands.js b/binaries/data/mods/public/simulation/helpers/Commands.js index fff768d656..b4f7437396 100644 --- a/binaries/data/mods/public/simulation/helpers/Commands.js +++ b/binaries/data/mods/public/simulation/helpers/Commands.js @@ -477,26 +477,6 @@ function TryConstructBuilding(player, cmpPlayer, controlAllUnits, cmd) var foundationTemplate = "foundation|" + cmd.template; - // We need the cost after tech modifications - // To calculate this with an entity requires ownership, so use the template instead - var cmpTechnologyManager = QueryPlayerIDInterface(player, IID_TechnologyManager); - var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); - var template = cmpTemplateManager.GetTemplate(foundationTemplate); - var costs = {}; - for (var r in template.Cost.Resources) - { - costs[r] = +template.Cost.Resources[r]; - if (cmpTechnologyManager) - costs[r] = cmpTechnologyManager.ApplyModificationsTemplate("Cost/Resources/"+r, costs[r], template); - } - - if (!cmpPlayer.TrySubtractResources(costs)) - { - if (g_DebugCommands) - warn("Invalid command: building cost check failed for player "+player+": "+uneval(cmd)); - return false; - } - // Tentatively create the foundation (we might find later that it's a invalid build command) var ent = Engine.AddEntity(foundationTemplate); if (ent == INVALID_ENTITY) @@ -562,6 +542,8 @@ function TryConstructBuilding(player, cmpPlayer, controlAllUnits, cmd) return false; } + var cmpTechnologyManager = QueryPlayerIDInterface(player, IID_TechnologyManager); + // TODO: Enable this check once the AI gets technology support if (!cmpTechnologyManager.CanProduce(cmd.template) && false) { @@ -600,6 +582,27 @@ function TryConstructBuilding(player, cmpPlayer, controlAllUnits, cmd) } } + // We need the cost after tech modifications + // To calculate this with an entity requires ownership, so use the template instead + var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); + var template = cmpTemplateManager.GetTemplate(foundationTemplate); + var costs = {}; + for (var r in template.Cost.Resources) + { + costs[r] = +template.Cost.Resources[r]; + if (cmpTechnologyManager) + costs[r] = cmpTechnologyManager.ApplyModificationsTemplate("Cost/Resources/"+r, costs[r], template); + } + + if (!cmpPlayer.TrySubtractResources(costs)) + { + if (g_DebugCommands) + warn("Invalid command: building cost check failed for player "+player+": "+uneval(cmd)); + + Engine.DestroyEntity(ent); + return false; + } + // Make it owned by the current player var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership); cmpOwnership.SetOwner(player);