Ticket #2342: patch_alert_autogarrison_alter.patch

File patch_alert_autogarrison_alter.patch, 4.5 KB (added by Itms, 10 years ago)
  • binaries/data/mods/public/simulation/components/AlertRaiser.js

     
    1111    // Remember the units ordered to garrison
    1212    this.garrisonedUnits = [];
    1313    this.walkingUnits = [];
     14   
     15    // Remember production buildings under alert
     16    this.prodBuildings = [];
    1417};
    1518
    1619AlertRaiser.prototype.GetLevel = function()
     
    3437    PlaySound(alertString, this.entity);
    3538};
    3639
    37 AlertRaiser.prototype.IncreaseAlertLevel = function()
     40AlertRaiser.prototype.UpdateUnits = function()
    3841{
    39     if(!this.CanIncreaseLevel())
    40         return false;
    41    
    4242    // Find units owned by this unit's player
    4343    var players = [];
    4444    var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    4545    if (cmpOwnership)
    4646        players = [cmpOwnership.GetOwner()];
    47        
    48     this.level++;
    49     this.SoundAlert();
    5047   
    5148    // Select units to put under alert, ignoring domestic animals (NB : war dogs are not domestic)
    5249    var rangeMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     
    6259        cmpUnitAI.ReplaceOrder("Alert", {"raiser": this.entity, "force": true});
    6360        this.walkingUnits.push(unit);
    6461    }
     62}
     63
     64AlertRaiser.prototype.IncreaseAlertLevel = function()
     65{
     66    if(!this.CanIncreaseLevel())
     67        return false;
     68       
     69    this.level++;
     70    this.SoundAlert();
    6571   
     72    // Find buildings owned by this unit's player
     73    var players = [];
     74    var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     75    if (cmpOwnership)
     76        players = [cmpOwnership.GetOwner()];
     77   
     78    // Select production buildings to put "under alert", including the raiser itself if possible
     79    var rangeMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     80    var level = this.GetLevel();
     81    var buildings = rangeMan.ExecuteQuery(this.entity, 0, this.template.Range, players, IID_ProductionQueue);
     82    var ownCmpProductionQueue = Engine.QueryInterface(this.entity, IID_ProductionQueue);
     83    if (ownCmpProductionQueue)
     84        buildings.push(this.entity);
     85   
     86    for each(var building in buildings)
     87    {
     88        var cmpProductionQueue = Engine.QueryInterface(building, IID_ProductionQueue);
     89        cmpProductionQueue.PutUnderAlert(this.entity);
     90        this.prodBuildings.push(building);
     91    }
     92   
     93    // Put units under alert
     94    this.UpdateUnits();
     95   
    6696    return true;
    6797};
    6898
     
    72102   
    73103    var index = this.walkingUnits.indexOf(msg.unit);
    74104    if (index != -1)
    75         this.walkingUnits.splice(index, 1);
    76    
    77 }
     105        this.walkingUnits.splice(index, 1);
     106};
    78107
    79108AlertRaiser.prototype.EndOfAlert = function()
    80109{
     
    85114    for each(var unit in this.walkingUnits)
    86115    {
    87116        var cmpUnitAI = Engine.QueryInterface(unit, IID_UnitAI);
     117        if(!cmpUnitAI)
     118            continue;
    88119       
    89120        cmpUnitAI.ResetAlert();
    90121           
     
    100131    {
    101132        var cmpGarrisonHolder = Engine.QueryInterface(slot.holder, IID_GarrisonHolder);
    102133        var cmpUnitAI = Engine.QueryInterface(slot.unit, IID_UnitAI);
     134        if(!cmpUnitAI)
     135            continue;
    103136       
    104137        if(cmpGarrisonHolder && cmpGarrisonHolder.PerformEject([slot.unit], true))
    105138        {
     
    110143    }
    111144    this.garrisonedUnits = [];
    112145   
     146    // Finally, reset production buildings state
     147    for each(var building in this.prodBuildings)
     148    {
     149        var cmpProductionQueue = Engine.QueryInterface(building, IID_ProductionQueue);
     150        if(!cmpProductionQueue)
     151            continue;
     152       
     153        cmpProductionQueue.ResetAlert();
     154    }
     155    this.prodBuildings = [];
     156   
    113157    return true;
    114158};
    115159
  • binaries/data/mods/public/simulation/components/ProductionQueue.js

     
    6565
    6666    this.entityCache = [];
    6767    this.spawnNotified = false;
     68   
     69    this.alertRaiser = undefined;
    6870};
    6971
     72ProductionQueue.prototype.PutUnderAlert = function(raiser)
     73{
     74    this.alertRaiser = raiser;
     75};
     76
     77ProductionQueue.prototype.ResetAlert = function()
     78{
     79    this.alertRaiser = undefined;
     80};
     81
    7082/*
    7183 * Returns list of entities that can be trained by this building.
    7284 */
     
    592604            "owner": cmpOwnership.GetOwner(),
    593605            "metadata": metadata,
    594606        });
     607       
     608        if(this.alertRaiser)
     609        {
     610            var cmpAlertRaiser = Engine.QueryInterface(this.alertRaiser, IID_AlertRaiser);
     611            if(cmpAlertRaiser)
     612                cmpAlertRaiser.UpdateUnits();
     613        }
    595614    }
    596615   
    597616    return createdEnts.length;