Ticket #2405: Cost_js_fix_population_cost_and_bonus_v2.patch

File Cost_js_fix_population_cost_and_bonus_v2.patch, 5.0 KB (added by Vladislav Belov, 9 years ago)
  • binaries/data/mods/public/simulation/components/Cost.js

     
    3333
    3434Cost.prototype.Init = function()
    3535{
     36    this.populationCost = +this.template.Population;
    3637    this.populationBonus = +this.template.PopulationBonus;
    3738};
    3839
    3940Cost.prototype.GetPopCost = function()
    4041{
    41     return +this.template.Population;
     42    return this.populationCost;
    4243};
    4344
    4445Cost.prototype.GetPopBonus = function()
     
    8788    if (msg.component != "Cost")
    8889        return;
    8990
    90     // foundations shouldn't give a pop bonus
     91    // foundations shouldn't give a pop bonus and a pop cost
    9192    var cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation)
    9293    if (cmpFoundation)
    9394        return;
    9495
     96    // update the population costs
     97    var newPopCost = ApplyValueModificationsToEntity("Cost/Population",  +this.template.Population, this.entity);
     98    var popCostDifference = newPopCost - this.populationCost;
     99    if (popCostDifference)
     100        this.populationCost = newPopCost;
     101
    95102    // update the population bonuses
    96103    var newPopBonus = ApplyValueModificationsToEntity("Cost/PopulationBonus",  +this.template.PopulationBonus, this.entity);
    97104    var popDifference = newPopBonus - this.populationBonus;
    98     if (!popDifference)
    99         return;
     105    if (popDifference)
     106        this.populationBonus = newPopBonus;
    100107
    101108    var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
    102109    if (cmpPlayer)
    103         cmpPlayer.AddPopulationBonuses(popDifference);
     110    {
     111        if (popCostDifference)
     112            cmpPlayer.AddPopulation(popCostDifference);
     113        if (popDifference)
     114            cmpPlayer.AddPopulationBonuses(popDifference);
     115    }
    104116};
    105117
    106118Engine.RegisterComponentType(IID_Cost, "Cost", Cost);
  • binaries/data/mods/public/simulation/components/Player.js

     
    112112    return this.popUsed;
    113113};
    114114
     115Player.prototype.AddPopulation = function(num)
     116{
     117    this.popUsed += num;
     118};
     119
    115120Player.prototype.SetPopulationBonuses = function(num)
    116121{
    117122    this.popBonuses = num;
  • binaries/data/mods/public/simulation/components/ProductionQueue.js

     
    244244        if (type == "unit")
    245245        {
    246246            // Find the template data so we can determine the build costs
    247             var cmpTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
    248             var template = cmpTempMan.GetTemplate(templateName);
     247            var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     248            var template = cmpTemplateManager.GetTemplate(templateName);
    249249            if (!template)
    250250                return;
    251251            if (template.Promotion)
     
    275275                totalCosts[r] = Math.floor(count * costs[r]);
    276276            }
    277277
    278             var population = +template.Cost.Population;
     278            var population = ApplyValueModificationsToTemplate("Cost/Population",  +template.Cost.Population, cmpPlayer.GetPlayerID(), template);
    279279
    280280            // TrySubtractResources should report error to player (they ran out of resources)
    281281            if (!cmpPlayer.TrySubtractResources(totalCosts))
     
    309309        else if (type == "technology")
    310310        {
    311311            // Load the technology template
    312             var cmpTechTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_TechnologyTemplateManager);
    313             var template = cmpTechTempMan.GetTemplate(templateName);
     312            var cmpTechnologyTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TechnologyTemplateManager);
     313            var template = cmpTechnologyTemplateManager.GetTemplate(templateName);
    314314            if (!template)
    315315                return;
    316316            var cmpPlayer = QueryOwnerInterface(this.entity);
     
    395395        // Update entity count in the EntityLimits component
    396396        if (item.unitTemplate)
    397397        {
    398             var cmpTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
    399             var template = cmpTempMan.GetTemplate(item.unitTemplate);
     398            var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     399            var template = cmpTemplateManager.GetTemplate(item.unitTemplate);
    400400            if (template.TrainingRestrictions)
    401401            {
    402402                var unitCategory = template.TrainingRestrictions.Category;
     
    665665            // If the item is a unit then do population checks
    666666            if (item.unitTemplate)
    667667            {
     668                // If something change population cost
     669                var template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate(item.unitTemplate);
     670                item.population = ApplyValueModificationsToTemplate("Cost/Population",  +template.Cost.Population, item.player, template);
     671               
    668672                // Batch's training hasn't started yet.
    669673                // Try to reserve the necessary population slots
    670674                item.neededSlots = cmpPlayer.TryReservePopulationSlots(item.population * item.count);