Ticket #4333: guard-4333.patch

File guard-4333.patch, 8.7 KB (added by mimo, 7 years ago)
  • binaries/data/mods/public/simulation/components/Guard.js

     
    1010
    1111Guard.prototype.GetRange = function(entity)
    1212{
    13     var range = 8;
    14     var cmpFootprint = Engine.QueryInterface(entity, IID_Footprint);
     13    let range = 8;
     14    let cmpFootprint = Engine.QueryInterface(entity, IID_Footprint);
    1515    if (cmpFootprint)
    1616    {
    17         var shape = cmpFootprint.GetShape();
     17        let shape = cmpFootprint.GetShape();
    1818        if (shape.type == "square")
    1919            range += Math.sqrt(shape.depth*shape.depth + shape.width*shape.width)*2/3;
    2020        else if (shape.type == "circle")
     
    4242
    4343Guard.prototype.RemoveGuard = function(ent)
    4444{
    45     var index = this.entities.indexOf(ent);
     45    let index = this.entities.indexOf(ent);
    4646    if (index != -1)
    4747        this.entities.splice(index, 1);
    4848};
    4949
     50Guard.prototype.RenameGuard = function(oldent, newent)
     51{
     52    let index = this.entities.indexOf(oldent);
     53    if (index != -1)
     54        this.entities[index] = newent;
     55};
     56
    5057Guard.prototype.OnAttacked = function(msg)
    5158{
    52     for each (var ent in this.entities)
     59    for (let ent of this.entities)
    5360        Engine.PostMessage(ent, MT_GuardedAttacked, { "guarded": this.entity, "data": msg });
    5461};
    5562
    5663/**
    57  * Update list of guarding/escorting entities if one gets renamed (e.g. by promotion)
     64 * If an entity is captured, or about to be killed (so its owner
     65 * changes to '-1') or if diplomacy changed, update the guards list
    5866 */
    59 Guard.prototype.OnGlobalEntityRenamed = function(msg)
     67Guard.prototype.OnOwnershipChanged = function(msg)
    6068{
    61     var entityIndex = this.entities.indexOf(msg.entity);
    62     if (entityIndex != -1)
    63         this.entities[entityIndex] = msg.newentity;
     69    if (!this.entities.length)
     70        return;
     71    this.CheckGuards(msg.to == -1);
    6472};
    6573
    66 /**
    67  * If an entity is captured, or about to be killed (so its owner
    68  * changes to '-1'), update the guards list
    69  */
    70 Guard.prototype.OnGlobalOwnershipChanged = function(msg)
     74Guard.prototype.OnDiplomacyChanged = function(msg)
    7175{
    72     // the ownership change may be on the guarded
    73     if (this.entity == msg.entity)
    74     {
    75         var entities = this.GetEntities();
    76         for each (var entity in entities)
    77         {
    78             if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity))
    79             {
    80                 var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
    81                 if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity)
    82                     cmpUnitAI.RemoveGuard();
    83                 else
    84                     this.RemoveGuard(entity);
    85             }
    86         }
    87         this.entities = entities;
     76    if (!this.entities.length)
    8877        return;
    89     }
     78    this.CheckGuards();
     79};
    9080
    91     // or on some of its guards
    92     if (this.entities.indexOf(msg.entity) != -1)
     81Guard.prototype.CheckGuards = function(force = false)
     82{
     83    let entities = this.GetEntities();
     84    for (let ent of entities)
    9385    {
    94         if (msg.to == -1)
    95             this.RemoveGuard(msg.entity);
    96         else if(!IsOwnedByMutualAllyOfEntity(this.entity, msg.entity))
     86        if (force || !IsOwnedByMutualAllyOfEntity(this.entity, ent))
    9787        {
    98             var cmpUnitAI = Engine.QueryInterface(msg.entity, IID_UnitAI);
    99             if (cmpUnitAI)
     88            let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     89            if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity)
    10090                cmpUnitAI.RemoveGuard();
    10191            else
    102                 this.RemoveGuard(msg.entity);
     92                this.RemoveGuard(ent);
    10393        }
    10494    }
    10595};
  • binaries/data/mods/public/simulation/components/Promotion.js

     
    9191
    9292    var workOrders = cmpCurrentUnitAI.GetWorkOrders();
    9393    cmpPromotedUnitAI.SetWorkOrders(workOrders);
    94     cmpPromotedUnitAI.SetGuardOf(cmpCurrentUnitAI.IsGuardOf());
    9594
    96     var cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard);
    97     var cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard);
     95    if (cmpCurrentUnitAI.IsGuardOf())
     96    {
     97        let guarded = cmpCurrentUnitAI.IsGuardOf();
     98        let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI)
     99        if (cmpGuard)
     100        {
     101            cmpGuard.RenameGuard(this.entity, promotedUnitEntity);
     102            cmpPromotedUnitAI.SetGuardOf(guarded);
     103        }
     104    }
     105
     106    let cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard);
     107    let cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard);
    98108    if (cmpCurrentUnitGuard && cmpPromotedUnitGuard)
    99         cmpPromotedUnitGuard.SetEntities(cmpCurrentUnitGuard.GetEntities());
     109    {
     110        let entities = cmpCurrentUnitGuard.GetEntities();
     111        if (entities.length)
     112        {
     113            cmpPromotedUnitGuard.SetEntities(entities);
     114            for (let ent of entities)
     115            {
     116                let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     117                if (cmpUnitAI)
     118                    cmpUnitAI.SetGuardOf(promotedUnitEntity);
     119            }
     120        }
     121    }
    100122
    101123    Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity });
    102124
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    34683468
    34693469UnitAI.prototype.OnDiplomacyChanged = function(msg)
    34703470{
    3471     var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     3471    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    34723472    if (cmpOwnership && cmpOwnership.GetOwner() == msg.player)
    34733473        this.SetupRangeQueries();
     3474
     3475    if (this.isGuardOf && !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf))
     3476        this.RemoveGuard();
    34743477};
    34753478
    34763479UnitAI.prototype.OnOwnershipChanged = function(msg)
     
    34773480{
    34783481    this.SetupRangeQueries();
    34793482
     3483    if (this.isGuardOf && (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf)))
     3484        this.RemoveGuard();
     3485
    34803486    // If the unit isn't being created or dying, reset stance and clear orders
    34813487    if (msg.to != -1 && msg.from != -1)
    34823488    {
     
    40384044    }
    40394045    if (changed)
    40404046        Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() });
    4041 
    4042     if (this.isGuardOf && this.isGuardOf == msg.entity)
    4043         this.isGuardOf = msg.newentity;
    40444047};
    40454048
    40464049UnitAI.prototype.OnAttacked = function(msg)
     
    49954998
    49964999UnitAI.prototype.RemoveGuard = function()
    49975000{
    4998     if (this.isGuardOf)
    4999     {
    5000         var cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard);
    5001         if (cmpGuard)
    5002             cmpGuard.RemoveGuard(this.entity);
    5003         this.guardRange = undefined;
    5004         this.isGuardOf = undefined;
    5005     }
     5001    if (!this.isGuardOf)
     5002        return;
    50065003
     5004    let cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard);
     5005    if (cmpGuard)
     5006        cmpGuard.RemoveGuard(this.entity);
     5007    this.guardRange = undefined;
     5008    this.isGuardOf = undefined;
     5009
    50075010    if (!this.order)
    50085011        return;
    50095012
     
    50105013    if (this.order.type == "Guard")
    50115014        this.UnitFsm.ProcessMessage(this, {"type": "RemoveGuard"});
    50125015    else
    5013         for (var i = 1; i < this.orderQueue.length; ++i)
     5016        for (let i = 1; i < this.orderQueue.length; ++i)
    50145017            if (this.orderQueue[i].type == "Guard")
    50155018                this.orderQueue.splice(i, 1);
    5016 
    50175019    Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() });
    50185020};
    50195021
  • binaries/data/mods/public/simulation/helpers/Transform.js

     
    6363        if (cmpUnitAI.GetStanceName())
    6464            cmpNewUnitAI.SwitchToStance(cmpUnitAI.GetStanceName());
    6565        cmpNewUnitAI.AddOrders(cmpUnitAI.GetOrders());
    66         cmpNewUnitAI.SetGuardOf(cmpUnitAI.IsGuardOf());
     66        if (cmpUnitAI.IsGuardOf())
     67        {
     68            let guarded = cmpUnitAI.IsGuardOf();
     69            let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI)
     70            if (cmpGuard)
     71            {
     72                cmpGuard.RenameGuard(oldEnt, newEnt);
     73                cmpNewUnitAI.SetGuardOf(guarded);
     74            }
     75        }
    6776    }
    6877
    6978    // Maintain the list of guards
    70     var cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard);
    71     var cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard);
     79    let cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard);
     80    let cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard);
    7281    if (cmpGuard && cmpNewGuard)
    73         cmpNewGuard.SetEntities(cmpGuard.GetEntities());
     82    {
     83        let entities = cmpGuard.GetEntities();
     84        if (entities.length)
     85        {
     86            cmpNewGuard.SetEntities(entities);
     87            for (let ent of entities)
     88            {
     89                let cmpEntUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     90                if (cmpEntUnitAI)
     91                    cmpEntUnitAI.SetGuardOf(NewEnt);
     92            }
     93        }
     94    }
    7495
    7596    TransferGarrisonedUnits(oldEnt, newEnt);
    7697