Ticket #3606: IdleRegenRate.patch

File IdleRegenRate.patch, 5.9 KB (added by Matthew Guttag, 8 years ago)

This patch adds the IdleRegenRate element to health and modifies Battlefield Medicine to use it

  • simulation/ai/petra/researchManager.js

     
    156156                return tech[0];
    157157            else if (template.modifications[i].value === "Health/RegenRate")
    158158                return tech[0];
     159            else if (template.modifications[i].value === "Health/IdleRegenRate")
     160                return tech[0];
    159161        }
    160162    }
    161163    return false;
  • simulation/components/Health.js

     
    55    "<a:example>" +
    66        "<Max>100</Max>" +
    77        "<RegenRate>1.0</RegenRate>" +
     8        "<IdleRegenRate>0</IdleRegenRate>" +
    89        "<DeathType>corpse</DeathType>" +
    910    "</a:example>" +
    1011    "<element name='Max' a:help='Maximum hitpoints'>" +
     
    2324    "<element name='RegenRate' a:help='Hitpoint regeneration rate per second.'>" +
    2425        "<data type='decimal'/>" +
    2526    "</element>" +
     27    "<element name='IdleRegenRate' a:help='Hitpoint regeneration rate per second when idle or garrisoned.'>" +
     28        "<data type='decimal'/>" +
     29    "</element>" +
    2630    "<element name='DeathType' a:help='Behaviour when the unit dies'>" +
    2731        "<choice>" +
    2832            "<value a:help='Disappear instantly'>vanish</value>" +
     
    4549    // (Allowing 0 initial HP would break our death detection code)
    4650    this.hitpoints = +(this.template.Initial || this.GetMaxHitpoints());
    4751    this.regenRate = ApplyValueModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity);
     52    this.idleRegenRate = ApplyValueModificationsToEntity("Health/IdleRegenRate", +this.template.IdleRegenRate, this.entity);
    4853    this.CheckRegenTimer();
    4954};
    5055
     
    107112    return this.template.Undeletable == "true";
    108113};
    109114
     115Health.prototype.GetIdleRegenRate = function()
     116{
     117    return this.idleRegenRate;
     118};
     119
    110120Health.prototype.GetRegenRate = function()
    111121{
    112122    return this.regenRate;
    113123};
    114124
     125Health.prototype.ExecuteIdleRegeneration = function()
     126{
     127    let regen = this.GetIdleRegenRate();
     128    let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
     129    if (!cmpUnitAI || (!cmpUnitAI.IsIdle() && !cmpUnitAI.IsGarrisoned()))
     130        return;
     131    if (regen > 0)
     132        this.Increase(regen);
     133    else
     134        this.Reduce(-regen);
     135};
     136
    115137Health.prototype.ExecuteRegeneration = function()
    116138{
    117     var regen = this.GetRegenRate();
     139    let regen = this.GetRegenRate();
    118140    if (regen > 0)
    119141        this.Increase(regen);
    120142    else
    121143        this.Reduce(-regen);
     144    if(this.GetIdleRegenRate() != 0)
     145        this.ExecuteIdleRegeneration();
    122146};
    123147
    124148/*
     
    127151Health.prototype.CheckRegenTimer = function()
    128152{
    129153    // check if we need a timer
    130     if (this.GetRegenRate() == 0 ||
    131         this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() > 0 ||
    132         this.GetHitpoints() == 0)
     154    if ((this.GetRegenRate() == 0 && this.GetIdleRegenRate() == 0) ||
     155        (this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() > 0 && this.GetIdleRegenRate() > 0) ||
     156         this.GetHitpoints() == 0)
    133157    {
    134158        // we don't need a timer, disable if one exists
    135159        if (this.regenTimer)
     
    325349    if (msg.component != "Health")
    326350        return;
    327351
    328     var oldMaxHitpoints = this.GetMaxHitpoints();
    329     var newMaxHitpoints = Math.round(ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity));
     352    let oldMaxHitpoints = this.GetMaxHitpoints();
     353    let newMaxHitpoints = Math.round(ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity));
    330354    if (oldMaxHitpoints != newMaxHitpoints)
    331355    {
    332         var newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);
     356        let newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);
    333357        this.maxHitpoints = newMaxHitpoints;
    334358        this.SetHitpoints(newHitpoints);
    335359    }
    336360
    337     var oldRegenRate = this.regenRate;
     361    let oldRegenRate = this.regenRate;
    338362    this.regenRate = ApplyValueModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity);
    339363
    340364    if (this.regenRate != oldRegenRate)
    341365        this.CheckRegenTimer();
     366   
     367    let oldIdleRegenRate = this.idleRegenRate;
     368    this.idleRegenRate = ApplyValueModificationsToEntity("Health/IdleRegenRate", +this.template.IdleRegenRate, this.entity);
     369   
     370    if (this.idleRegenRate != oldIdleRegenRate)
     371        this.CheckRegenTimer();
    342372};
    343373
    344374Health.prototype.OnHealthChanged = function()
  • simulation/data/technologies/health_regen_units.json

     
    77    "icon": "bandage.png",
    88    "researchTime": 40,
    99    "tooltip": "Organic units will slowly regenerate health over time when idle.",
    10     "modifications": [{"value": "Health/RegenRate", "add": 0.5}],
     10    "modifications": [{"value": "Health/Idle", "add": 0.5}],
    1111    "affects": ["Unit Organic"],
    1212    "soundComplete": "interface/alarm/alarm_upgradearmory.xml"
    1313}
  • simulation/templates/template_structure.xml

     
    4747  <Health>
    4848    <DeathType>corpse</DeathType>
    4949    <RegenRate>0</RegenRate>
     50    <IdleRegenRate>0</IdleRegenRate>
    5051    <Undeletable>false</Undeletable>
    5152    <Unhealable>true</Unhealable>
    5253  </Health>
  • simulation/templates/template_unit.xml

     
    3131    <DeathType>corpse</DeathType>
    3232    <Max>100</Max>
    3333    <RegenRate>0</RegenRate>
     34    <IdleRegenRate>0</IdleRegenRate>
    3435    <Undeletable>false</Undeletable>
    3536    <Unhealable>false</Unhealable>
    3637  </Health>