Ticket #3707: t3707_repair_cost.diff
File t3707_repair_cost.diff, 2.2 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Repairable.js
13 13 { 14 14 this.builders = []; // builder entities 15 15 this.buildMultiplier = 1; // Multiplier for the amount of work builders do. 16 this.costsToPay = { "food": 0, "wood": 0, "metal": 0, "stone": 0 }; 16 17 this.repairTimeRatio = +this.template.RepairTimeRatio; 17 18 }; 18 19 … … 50 51 this.buildMultiplier = Math.pow(numBuilders, 0.7) / numBuilders; 51 52 }; 52 53 53 // TODO: should we have resource costs?54 54 Repairable.prototype.Repair = function(builderEnt, rate) 55 55 { 56 56 let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 57 57 let cmpCost = Engine.QueryInterface(this.entity, IID_Cost); 58 if (!cmpHealth || !cmpCost) 58 let cmpPlayer = QueryOwnerInterface(this.entity); 59 if (!cmpHealth || !cmpCost || !cmpPlayer) 59 60 return; 60 61 let damage = cmpHealth.GetMaxHitpoints() - cmpHealth.GetHitpoints(); 61 62 if (damage <= 0) … … 67 68 if (repairTime) 68 69 work *= (cmpHealth.GetMaxHitpoints() / repairTime); 69 70 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 70 94 cmpHealth.Increase(amount); 71 95 72 96 // If we repaired all the damage, send a message to entities to stop repairing this building