Ticket #3606: IdleRegenRate2.patch

File IdleRegenRate2.patch, 6.2 KB (added by Matthew Guttag, 8 years ago)

The logic for checking the idle state is now moved to UnitAI and will correctly handle turreted units. IdleRegenRate is now an optional element.

  • 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    "<optional>" +
     28        "<element name='IdleRegenRate' a:help='Hitpoint regeneration rate per second when idle or garrisoned.'>" +
     29            "<data type='decimal'/>" +
     30        "</element>" +
     31    "</optional>" +
    2632    "<element name='DeathType' a:help='Behaviour when the unit dies'>" +
    2733        "<choice>" +
    2834            "<value a:help='Disappear instantly'>vanish</value>" +
     
    4551    // (Allowing 0 initial HP would break our death detection code)
    4652    this.hitpoints = +(this.template.Initial || this.GetMaxHitpoints());
    4753    this.regenRate = ApplyValueModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity);
     54    if(this.template.IdleRegenRate == undefined)
     55    {
     56        this.idleRegenRate = 0;
     57    }
     58    else
     59    {
     60        this.idleRegenRate = ApplyValueModificationsToEntity("Health/IdleRegenRate", +this.template.IdleRegenRate, this.entity);
     61    }
    4862    this.CheckRegenTimer();
    4963};
    5064
     
    107121    return this.template.Undeletable == "true";
    108122};
    109123
     124Health.prototype.GetIdleRegenRate = function()
     125{
     126    return this.idleRegenRate;
     127};
     128
    110129Health.prototype.GetRegenRate = function()
    111130{
    112131    return this.regenRate;
     
    114133
    115134Health.prototype.ExecuteRegeneration = function()
    116135{
    117     var regen = this.GetRegenRate();
     136    let regen = this.GetRegenRate();
    118137    if (regen > 0)
    119138        this.Increase(regen);
    120139    else
    121140        this.Reduce(-regen);
     141    if(this.GetIdleRegenRate() != 0)
     142    {
     143        let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
     144        if(cmpUnitAI && cmpUnitAI.IsIdleOrIdlyGarrisoned())
     145        {
     146            let idleRegen = this.GetIdleRegenRate();
     147            if (idleRegen > 0)
     148                this.Increase(idleRegen);
     149            else
     150                this.Reduce(-idleRegen);
     151        }
     152    }
    122153};
    123154
    124155/*
     
    127158Health.prototype.CheckRegenTimer = function()
    128159{
    129160    // check if we need a timer
    130     if (this.GetRegenRate() == 0 ||
    131         this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() > 0 ||
    132         this.GetHitpoints() == 0)
     161    if ((this.GetRegenRate() == 0 && this.GetIdleRegenRate() == 0) ||
     162        (this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() > 0 && this.GetIdleRegenRate() > 0) ||
     163         this.GetHitpoints() == 0)
    133164    {
    134165        // we don't need a timer, disable if one exists
    135166        if (this.regenTimer)
     
    325356    if (msg.component != "Health")
    326357        return;
    327358
    328     var oldMaxHitpoints = this.GetMaxHitpoints();
    329     var newMaxHitpoints = Math.round(ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity));
     359    let oldMaxHitpoints = this.GetMaxHitpoints();
     360    let newMaxHitpoints = Math.round(ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity));
    330361    if (oldMaxHitpoints != newMaxHitpoints)
    331362    {
    332         var newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);
     363        let newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);
    333364        this.maxHitpoints = newMaxHitpoints;
    334365        this.SetHitpoints(newHitpoints);
    335366    }
    336367
    337     var oldRegenRate = this.regenRate;
     368    let oldRegenRate = this.regenRate;
    338369    this.regenRate = ApplyValueModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity);
    339370
    340371    if (this.regenRate != oldRegenRate)
    341372        this.CheckRegenTimer();
     373   
     374    if(this.template.IdleRegenRate != undefined)
     375    {
     376        let oldIdleRegenRate = this.idleRegenRate;
     377        this.idleRegenRate = ApplyValueModificationsToEntity("Health/IdleRegenRate", +this.template.IdleRegenRate, this.entity);
     378   
     379        if (this.idleRegenRate != oldIdleRegenRate)
     380            this.CheckRegenTimer();
     381    }
    342382};
    343383
    344384Health.prototype.OnHealthChanged = function()
  • simulation/components/UnitAI.js

     
    32983298    return this.isGarrisoned;
    32993299};
    33003300
     3301/**
     3302* Returns true if the unit is idle a turret and idle
     3303* or a non-turret garrisoned unit
     3304*/
     3305UnitAI.prototype.IsIdleOrIdlyGarrisoned = function()
     3306{
     3307    if(this.IsIdle())
     3308        return true;
     3309    if(this.IsGarrisoned() && !this.IsTurret())
     3310        return true;
     3311    return false;
     3312};
     3313
    33013314UnitAI.prototype.SetGarrisoned = function()
    33023315{
    33033316    this.isGarrisoned = true;
  • 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/IdleRegenRate", "add": 0.5}],
    1111    "affects": ["Unit Organic"],
    1212    "soundComplete": "interface/alarm/alarm_upgradearmory.xml"
    1313}
  • 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>