Ticket #633: repair.diff

File repair.diff, 5.1 KB (added by sanderd17, 10 years ago)
  • binaries/data/mods/public/simulation/components/Builder.js

     
    33Builder.prototype.Schema =
    44    "<a:help>Allows the unit to construct and repair buildings.</a:help>" +
    55    "<a:example>" +
    6         "<Rate>1.0</Rate>" +
     6        "<BuildRate>1.0</BuildRate>" +
    77        "<Entities datatype='tokens'>" +
    88            "\n    structures/{civ}_barracks\n    structures/{civ}_civil_centre\n    structures/celt_sb1\n  " +
    99        "</Entities>" +
     
    1111    "<element name='Rate' a:help='Construction speed multiplier (1.0 is normal speed, higher values are faster)'>" +
    1212        "<ref name='positiveDecimal'/>" +
    1313    "</element>" +
     14    "<optional>" +
     15        "<element name='RepairRate' a:help='Repairing speed multiplier (1.0 is normal speed, higher values are faster), by default the same as the normal rate.'>" +
     16            "<ref name='positiveDecimal'/>" +
     17        "</element>" +
     18    "</optional>" +
     19
    1420    "<element name='Entities' a:help='Space-separated list of entity template names that this unit can build. The special string \"{civ}\" will be automatically replaced by the unit&apos;s four-character civ code. This element can also be empty, in which case no new foundations may be placed by the unit, but they can still repair existing buildings'>" +
    1521        "<attribute name='datatype'>" +
    1622            "<value>tokens</value>" +
     
    5662 */
    5763Builder.prototype.PerformBuilding = function(target)
    5864{
    59     var rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
    6065
    6166    // If it's a foundation, then build it
    6267    var cmpFoundation = Engine.QueryInterface(target, IID_Foundation);
    6368    if (cmpFoundation)
    6469    {
     70        var rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
    6571        cmpFoundation.Build(this.entity, rate);
    6672        return;
    6773    }
     
    6874
    6975    // Otherwise try to repair it
    7076    var cmpHealth = Engine.QueryInterface(target, IID_Health);
    71     if (cmpHealth)
    72     {
    73         cmpHealth.Repair(this.entity, rate);
     77    if (!cmpHealth)
    7478        return;
    75     }
     79
     80    if (this.template.RepairRate)
     81        var rate = ApplyValueModificationsToEntity("Builder/RepairRate", +this.template.RepairRate, this.entity);
     82    else
     83        var rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
     84
     85    cmpHealth.Repair(this.entity, rate);
    7686};
    7787
    7888Engine.RegisterComponentType(IID_Builder, "Builder", Builder);
  • binaries/data/mods/public/simulation/components/Cost.js

     
    2222    "<element name='BuildTime' a:help='Time taken to construct/train this unit (in seconds)'>" +
    2323        "<ref name='nonNegativeDecimal'/>" +
    2424    "</element>" +
     25    "<optional>" +
     26        "<element name='RepairTime' a:help='Time taken to repair this unit (in seconds). By default, the max health of an entity (so it repairs at 1 HP/s)'>" +
     27            "<ref name='nonNegativeDecimal'/>" +
     28        "</element>" +
     29    "</optional>" +
    2530    "<element name='Resources' a:help='Resource costs to construct/train this unit'>" +
    2631        "<interleave>" +
    2732            "<element name='food'><data type='nonNegativeInteger'/></element>" +
     
    4853
    4954Cost.prototype.GetBuildTime = function()
    5055{
     56    if (!this.template.RepairTime)
     57        return null;
    5158    var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
    5259    var buildTime = (+this.template.BuildTime) * cmpPlayer.cheatTimeMultiplier;
    5360    return ApplyValueModificationsToEntity("Cost/BuildTime", buildTime, this.entity);
    5461}
    5562
     63Cost.prototype.GetRepairTime = function()
     64{
     65    if (!this.template.RepairTime)
     66        return null;
     67    var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
     68    var repairTime = (+this.template.RepairTime) * cmpPlayer.cheatTimeMultiplier;
     69    return ApplyValueModificationsToEntity("Cost/RepairTime", repairTime, this.entity);
     70}
     71
    5672Cost.prototype.GetResourceCosts = function()
    5773{
    5874    var costs = {};
  • binaries/data/mods/public/simulation/components/Health.js

     
    300300    return spawnedEntity;
    301301};
    302302
    303 Health.prototype.Repair = function(builderEnt, work)
     303Health.prototype.Repair = function(builderEnt, rate)
    304304{
    305305    var damage = this.GetMaxHitpoints() - this.GetHitpoints();
    306306
     
    309309        return;
    310310
    311311    // Calculate the amount of hitpoints that will be added
     312    var repairTime = this.GetMaxHitpoints();
     313    var cmpCost = Engine.QueryInterface(this.entity, IID_Cost);
     314    if (cmpCost && cmpCost.GetRepairTime())
     315        repairTime = cmpCost.GetRepairTime();
     316
     317    var work = rate / repairTime * this.GetMaxHitpoints();
     318
    312319    // TODO: what computation should we use?
    313320    // TODO: should we do some diminishing returns thing? (see Foundation.Build)
    314321    var amount = Math.min(damage, work);