Ticket #2238: garrison.diff

File garrison.diff, 2.2 KB (added by mimo, 11 years ago)
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    390390 */
    391391GarrisonHolder.prototype.OnHealthChanged = function(msg)
    392392{
    393     if (!this.HasEnoughHealth())
    394         this.EjectOrKill(this.entities);
     393    if (!this.HasEnoughHealth() && this.entities.length)
     394    {
     395        var entities = this.entities.slice(0, this.entities.length);
     396        this.EjectOrKill(entities);
     397    }
    395398};
    396399
    397400/**
     
    532535GarrisonHolder.prototype.EjectOrKill = function(entities)
    533536{
    534537    var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
    535     // Destroy the garrisoned units if the holder kill his entities on destroy or
    536     // is not in the world (generally means this holder is inside
    537     // a holder which kills its entities which has sunk).
    538     if (!this.EjectEntitiesOnDestroy() || !cmpPosition.IsInWorld())
     538    // Eject the units which can be ejected (if not in world, it generally means this holder
     539    // is inside a holder which kills its entities, so do not eject)
     540    if (cmpPosition.IsInWorld() && this.EjectEntitiesOnDestroy())
     541        this.PerformEject(entities, true);
     542
     543    // And destroy all remaining entities
     544    for each (var entity in entities)
    539545    {
    540         for each (var entity in entities)
    541         {
    542             var cmpHealth = Engine.QueryInterface(entity, IID_Health);
    543             if (cmpHealth)
    544                 cmpHealth.Kill();
    545             var entityIndex = this.entities.indexOf(entity);
    546             this.entities.splice(entityIndex, 1);
    547             Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, {});
    548         }
    549         this.UpdateGarrisonFlag();
     546        var entityIndex = this.entities.indexOf(entity);
     547        if (entityIndex == -1)
     548            continue;
     549        var cmpHealth = Engine.QueryInterface(entity, IID_Health);
     550        if (cmpHealth)
     551            cmpHealth.Kill();
     552        this.entities.splice(entityIndex, 1);
     553        Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, {});
    550554    }
    551     else
    552     {   // Building - force ejection
    553         this.PerformEject(entities, true);
    554     }
     555
     556    this.UpdateGarrisonFlag();
    555557};
    556558
    557559Engine.RegisterComponentType(IID_GarrisonHolder, "GarrisonHolder", GarrisonHolder);