Ticket #3707: t3707_repair_cost.diff

File t3707_repair_cost.diff, 2.2 KB (added by bb, 8 years ago)

patch for repair costs

  • binaries/data/mods/public/simulation/components/Repairable.js

     
    1313{
    1414    this.builders = []; // builder entities
    1515    this.buildMultiplier = 1; // Multiplier for the amount of work builders do.
     16    this.costsToPay = { "food": 0, "wood": 0, "metal": 0, "stone": 0 };
    1617    this.repairTimeRatio = +this.template.RepairTimeRatio;
    1718};
    1819
     
    5051        this.buildMultiplier = Math.pow(numBuilders, 0.7) / numBuilders;
    5152};
    5253
    53 // TODO: should we have resource costs?
    5454Repairable.prototype.Repair = function(builderEnt, rate)
    5555{
    5656    let cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    5757    let cmpCost = Engine.QueryInterface(this.entity, IID_Cost);
    58     if (!cmpHealth || !cmpCost)
     58    let cmpPlayer = QueryOwnerInterface(this.entity);
     59    if (!cmpHealth || !cmpCost || !cmpPlayer)
    5960        return;
    6061    let damage = cmpHealth.GetMaxHitpoints() - cmpHealth.GetHitpoints();
    6162    if (damage <= 0)
     
    6768    if (repairTime)
    6869        work *= (cmpHealth.GetMaxHitpoints() / repairTime);
    6970    let amount = Math.min(damage, work);
     71
     72    // Calculate resource costs for the added hitpoints
     73    let newPrice = cmpCost.GetResourceCosts();
     74    let repairCost = [];
     75    let resourcesToSubtract = [];
     76    for (let type in newPrice)
     77    {
     78        repairCost[type] = 0.5 * newPrice[type] * amount / cmpHealth.GetMaxHitpoints();
     79        this.costsToPay[type] += repairCost[type];
     80        resourcesToSubtract[type] = Math.floor(this.costsToPay[type]);
     81        this.costsToPay[type] -= resourcesToSubtract[type];
     82    };
     83
     84    // Substract resource
     85    // TrySubtractResources should report error to player (if they ran out of resources)
     86    // If ran out of resource, send a message to entities to stop repairing this building
     87    if (!cmpPlayer.TrySubtractResources(resourcesToSubtract))
     88    {
     89
     90        Engine.PostMessage(this.entity, MT_ConstructionFinished, { "entity": this.entity, "newentity": this.entity });
     91        return;
     92    };
     93   
    7094    cmpHealth.Increase(amount);
    7195
    7296    // If we repaired all the damage, send a message to entities to stop repairing this building