diff --git a/binaries/data/mods/public/simulation/components/Guard.js b/binaries/data/mods/public/simulation/components/Guard.js index e90e9747f6..dca201f8c7 100644 --- a/binaries/data/mods/public/simulation/components/Guard.js +++ b/binaries/data/mods/public/simulation/components/Guard.js @@ -10,11 +10,11 @@ Guard.prototype.Init = function() Guard.prototype.GetRange = function(entity) { - var range = 8; - var cmpFootprint = Engine.QueryInterface(entity, IID_Footprint); + let range = 8; + let cmpFootprint = Engine.QueryInterface(entity, IID_Footprint); if (cmpFootprint) { - var shape = cmpFootprint.GetShape(); + let shape = cmpFootprint.GetShape(); if (shape.type == "square") range += Math.sqrt(shape.depth*shape.depth + shape.width*shape.width)*2/3; else if (shape.type == "circle") @@ -42,64 +42,54 @@ Guard.prototype.AddGuard = function(ent) Guard.prototype.RemoveGuard = function(ent) { - var index = this.entities.indexOf(ent); + let index = this.entities.indexOf(ent); if (index != -1) this.entities.splice(index, 1); }; +Guard.prototype.RenameGuard = function(oldent, newent) +{ + let index = this.entities.indexOf(oldent); + if (index != -1) + this.entities[index] = newent; +}; + Guard.prototype.OnAttacked = function(msg) { - for each (var ent in this.entities) + for (let ent of this.entities) Engine.PostMessage(ent, MT_GuardedAttacked, { "guarded": this.entity, "data": msg }); }; /** - * Update list of guarding/escorting entities if one gets renamed (e.g. by promotion) + * If an entity is captured, or about to be killed (so its owner + * changes to '-1') or if diplomacy changed, update the guards list */ -Guard.prototype.OnGlobalEntityRenamed = function(msg) +Guard.prototype.OnOwnershipChanged = function(msg) { - var entityIndex = this.entities.indexOf(msg.entity); - if (entityIndex != -1) - this.entities[entityIndex] = msg.newentity; + if (!this.entities.length) + return; + this.CheckGuards(msg.to == -1); }; -/** - * If an entity is captured, or about to be killed (so its owner - * changes to '-1'), update the guards list - */ -Guard.prototype.OnGlobalOwnershipChanged = function(msg) +Guard.prototype.OnDiplomacyChanged = function(msg) { - // the ownership change may be on the guarded - if (this.entity == msg.entity) - { - var entities = this.GetEntities(); - for each (var entity in entities) - { - if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity)) - { - var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); - if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity) - cmpUnitAI.RemoveGuard(); - else - this.RemoveGuard(entity); - } - } - this.entities = entities; + if (!this.entities.length) return; - } + this.CheckGuards(); +}; - // or on some of its guards - if (this.entities.indexOf(msg.entity) != -1) +Guard.prototype.CheckGuards = function(force = false) +{ + let entities = this.GetEntities(); + for (let ent of entities) { - if (msg.to == -1) - this.RemoveGuard(msg.entity); - else if(!IsOwnedByMutualAllyOfEntity(this.entity, msg.entity)) + if (force || !IsOwnedByMutualAllyOfEntity(this.entity, ent)) { - var cmpUnitAI = Engine.QueryInterface(msg.entity, IID_UnitAI); - if (cmpUnitAI) + let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); + if (cmpUnitAI && cmpUnitAI.IsGuardOf() && cmpUnitAI.IsGuardOf() == this.entity) cmpUnitAI.RemoveGuard(); else - this.RemoveGuard(msg.entity); + this.RemoveGuard(ent); } } }; diff --git a/binaries/data/mods/public/simulation/components/Promotion.js b/binaries/data/mods/public/simulation/components/Promotion.js index de183fa727..b4837f5487 100644 --- a/binaries/data/mods/public/simulation/components/Promotion.js +++ b/binaries/data/mods/public/simulation/components/Promotion.js @@ -91,12 +91,34 @@ Promotion.prototype.Promote = function(promotedTemplateName) var workOrders = cmpCurrentUnitAI.GetWorkOrders(); cmpPromotedUnitAI.SetWorkOrders(workOrders); - cmpPromotedUnitAI.SetGuardOf(cmpCurrentUnitAI.IsGuardOf()); - var cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard); - var cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard); + if (cmpCurrentUnitAI.IsGuardOf()) + { + let guarded = cmpCurrentUnitAI.IsGuardOf(); + let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI) + if (cmpGuard) + { + cmpGuard.RenameGuard(this.entity, promotedUnitEntity); + cmpPromotedUnitAI.SetGuardOf(guarded); + } + } + + let cmpCurrentUnitGuard = Engine.QueryInterface(this.entity, IID_Guard); + let cmpPromotedUnitGuard = Engine.QueryInterface(promotedUnitEntity, IID_Guard); if (cmpCurrentUnitGuard && cmpPromotedUnitGuard) - cmpPromotedUnitGuard.SetEntities(cmpCurrentUnitGuard.GetEntities()); + { + let entities = cmpCurrentUnitGuard.GetEntities(); + if (entities.length) + { + cmpPromotedUnitGuard.SetEntities(entities); + for (let ent of entities) + { + let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); + if (cmpUnitAI) + cmpUnitAI.SetGuardOf(promotedUnitEntity); + } + } + } Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity }); diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index d3a81321da..cbb0428ead 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -3468,15 +3468,21 @@ UnitAI.prototype.OnCreate = function() UnitAI.prototype.OnDiplomacyChanged = function(msg) { - var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); + let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (cmpOwnership && cmpOwnership.GetOwner() == msg.player) this.SetupRangeQueries(); + + if (this.isGuardOf && !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf)) + this.RemoveGuard(); }; UnitAI.prototype.OnOwnershipChanged = function(msg) { this.SetupRangeQueries(); + if (this.isGuardOf && (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, this.isGuardOf))) + this.RemoveGuard(); + // If the unit isn't being created or dying, reset stance and clear orders if (msg.to != -1 && msg.from != -1) { @@ -4038,9 +4044,6 @@ UnitAI.prototype.OnGlobalEntityRenamed = function(msg) } if (changed) Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); - - if (this.isGuardOf && this.isGuardOf == msg.entity) - this.isGuardOf = msg.newentity; }; UnitAI.prototype.OnAttacked = function(msg) @@ -4995,14 +4998,14 @@ UnitAI.prototype.AddGuard = function(target) UnitAI.prototype.RemoveGuard = function() { - if (this.isGuardOf) - { - var cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard); - if (cmpGuard) - cmpGuard.RemoveGuard(this.entity); - this.guardRange = undefined; - this.isGuardOf = undefined; - } + if (!this.isGuardOf) + return; + + let cmpGuard = Engine.QueryInterface(this.isGuardOf, IID_Guard); + if (cmpGuard) + cmpGuard.RemoveGuard(this.entity); + this.guardRange = undefined; + this.isGuardOf = undefined; if (!this.order) return; @@ -5010,10 +5013,9 @@ UnitAI.prototype.RemoveGuard = function() if (this.order.type == "Guard") this.UnitFsm.ProcessMessage(this, {"type": "RemoveGuard"}); else - for (var i = 1; i < this.orderQueue.length; ++i) + for (let i = 1; i < this.orderQueue.length; ++i) if (this.orderQueue[i].type == "Guard") this.orderQueue.splice(i, 1); - Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); }; diff --git a/binaries/data/mods/public/simulation/helpers/Transform.js b/binaries/data/mods/public/simulation/helpers/Transform.js index dbdedddf64..3d4a44439f 100644 --- a/binaries/data/mods/public/simulation/helpers/Transform.js +++ b/binaries/data/mods/public/simulation/helpers/Transform.js @@ -63,14 +63,35 @@ function ChangeEntityTemplate(oldEnt, newTemplate) if (cmpUnitAI.GetStanceName()) cmpNewUnitAI.SwitchToStance(cmpUnitAI.GetStanceName()); cmpNewUnitAI.AddOrders(cmpUnitAI.GetOrders()); - cmpNewUnitAI.SetGuardOf(cmpUnitAI.IsGuardOf()); + if (cmpUnitAI.IsGuardOf()) + { + let guarded = cmpUnitAI.IsGuardOf(); + let cmpGuard = Engine.QueryInterface(guarded, IID_UnitAI) + if (cmpGuard) + { + cmpGuard.RenameGuard(oldEnt, newEnt); + cmpNewUnitAI.SetGuardOf(guarded); + } + } } // Maintain the list of guards - var cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard); - var cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard); + let cmpGuard = Engine.QueryInterface(oldEnt, IID_Guard); + let cmpNewGuard = Engine.QueryInterface(newEnt, IID_Guard); if (cmpGuard && cmpNewGuard) - cmpNewGuard.SetEntities(cmpGuard.GetEntities()); + { + let entities = cmpGuard.GetEntities(); + if (entities.length) + { + cmpNewGuard.SetEntities(entities); + for (let ent of entities) + { + let cmpEntUnitAI = Engine.QueryInterface(ent, IID_UnitAI); + if (cmpEntUnitAI) + cmpEntUnitAI.SetGuardOf(NewEnt); + } + } + } TransferGarrisonedUnits(oldEnt, newEnt);