From 38af159598532dd505ced0e138ff812e7c13ce63 Mon Sep 17 00:00:00 2001 From: mimo Date: Thu, 8 Oct 2015 21:59:52 +0000 Subject: [PATCH] fix UnitAI state of turrets, fix #3481 This was SVN commit r17111. --- .../simulation/components/GarrisonHolder.js | 41 +++++++++++++------ .../public/simulation/components/Promotion.js | 19 ++------- .../public/simulation/components/UnitAI.js | 39 ++++++++++++++---- 3 files changed, 62 insertions(+), 37 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/GarrisonHolder.js b/binaries/data/mods/public/simulation/components/GarrisonHolder.js index 5d2fd26fb8..60138b689a 100644 --- a/binaries/data/mods/public/simulation/components/GarrisonHolder.js +++ b/binaries/data/mods/public/simulation/components/GarrisonHolder.js @@ -224,8 +224,9 @@ GarrisonHolder.prototype.AllowedToGarrison = function(entity) * Garrison a unit inside. * Returns true if successful, false if not * The timer for AutoHeal is started here + * if vgpEntity is given, this visualGarrisonPoint will be used for the entity */ -GarrisonHolder.prototype.Garrison = function(entity) +GarrisonHolder.prototype.Garrison = function(entity, vgpEntity) { var cmpPosition = Engine.QueryInterface(entity, IID_Position); if (!cmpPosition) @@ -234,21 +235,29 @@ GarrisonHolder.prototype.Garrison = function(entity) if (!this.PerformGarrison(entity)) return false; - var visiblyGarrisoned = false; - for (var vgp of this.visibleGarrisonPoints) + let visibleGarrisonPoint = vgpEntity; + if (!visibleGarrisonPoint) { - if (vgp.entity) - continue; - vgp.entity = entity; - cmpPosition.SetTurretParent(this.entity, vgp.offset); - visiblyGarrisoned = true; - var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); + for (let vgp of this.visibleGarrisonPoints) + { + if (vgp.entity) + continue; + visibleGarrisonPoint = vgp; + break; + } + } + + if (visibleGarrisonPoint) + { + visibleGarrisonPoint.entity = entity; + cmpPosition.SetTurretParent(this.entity, visibleGarrisonPoint.offset); + let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI); if (cmpUnitAI) cmpUnitAI.SetTurretStance(); - break; } - if (!visiblyGarrisoned) + else cmpPosition.MoveOutOfWorld(); + return true; }; @@ -674,8 +683,16 @@ GarrisonHolder.prototype.OnGlobalEntityRenamed = function(msg) var entityIndex = this.entities.indexOf(msg.entity); if (entityIndex != -1) { + let vgpRenamed; + for (let vgp of this.visibleGarrisonPoints) + { + if (vgp.entity != msg.entity) + continue; + vgpRenamed = vgp; + break; + } this.Eject(msg.entity); - this.Garrison(msg.newentity); + this.Garrison(msg.newentity, vgpRenamed); } if (!this.initGarrison) diff --git a/binaries/data/mods/public/simulation/components/Promotion.js b/binaries/data/mods/public/simulation/components/Promotion.js index d103d1a606..4226d5e9e8 100644 --- a/binaries/data/mods/public/simulation/components/Promotion.js +++ b/binaries/data/mods/public/simulation/components/Promotion.js @@ -80,22 +80,9 @@ Promotion.prototype.Promote = function(promotedTemplateName) var orders = cmpCurrentUnitAI.GetOrders(); if (cmpCurrentUnitAI.IsGarrisoned()) - { - if (orders.length > 0 && (orders[0].type == "Garrison" || orders[0].type == "Autogarrison")) - { - // Replace the garrison order by an autogarrison order, - // as we are already garrisoned and do not need to do - // any further checks (or else we should do them here). - var garrisonHolder = orders[0].data.target; - orders.shift(); - cmpPromotedUnitAI.Autogarrison(garrisonHolder); - } - else - warn("Promoted garrisoned entity with empty order queue."); - } - else - cmpPromotedUnitAI.Cheer(); - + cmpPromotedUnitAI.isGarrisoned = true; + if (cmpCurrentUnitPosition.IsInWorld()) // do not cheer if not visibly garrisoned + cmpPromotedUnitAI.Cheer(); cmpPromotedUnitAI.AddOrders(orders); var workOrders = cmpCurrentUnitAI.GetWorkOrders(); diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index 5afe69e2c0..4b2eca6bfd 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -687,9 +687,15 @@ UnitAI.prototype.UnitFsmSpec = { "Order.Garrison": function(msg) { if (this.IsTurret()) { - this.FinishOrder(); + this.SetNextState("IDLE"); return; } + else if (this.IsGarrisoned()) + { + this.SetNextState("INDIVIDUAL.AUTOGARRISON"); + return; + } + // For packable units: // 1. If packed, we can move to the garrison target. // 2. If unpacked, we first need to pack, then follow case 1. @@ -713,6 +719,12 @@ UnitAI.prototype.UnitFsmSpec = { }, "Order.Autogarrison": function(msg) { + if (this.IsTurret()) + { + this.SetNextState("IDLE"); + return; + } + this.SetNextState("INDIVIDUAL.AUTOGARRISON"); }, @@ -1438,7 +1450,7 @@ UnitAI.prototype.UnitFsmSpec = { // from FinishOrder (SetNextState("IDLE") is only executed when we get // a ProcessMessage), and thus we should not start an attack which could // put us in a weird state - if (this.orderQueue.length > 0) + if (this.orderQueue.length > 0 && !this.IsGarrisoned()) return false; // If a unit can heal and attack we first want to heal wounded units, @@ -1501,6 +1513,11 @@ UnitAI.prototype.UnitFsmSpec = { Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle }); } }, + + "Order.Ungarrison": function() { // Needed for turrets + this.FinishOrder(); + this.isGarrisoned = false; + }, }, "WALKING": { @@ -2918,12 +2935,11 @@ UnitAI.prototype.UnitFsmSpec = { }, "Order.Ungarrison": function() { - if (this.FinishOrder()) - return; + this.FinishOrder(); + this.isGarrisoned = false; }, "leave": function() { - this.isGarrisoned = false; } }, }, @@ -2935,12 +2951,11 @@ UnitAI.prototype.UnitFsmSpec = { }, "Order.Ungarrison": function() { - if (this.FinishOrder()) - return; + this.FinishOrder(); + this.isGarrisoned = false; }, "leave": function() { - this.isGarrisoned = false; } }, @@ -3318,7 +3333,7 @@ UnitAI.prototype.OnDiplomacyChanged = function(msg) { var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (cmpOwnership && cmpOwnership.GetOwner() == msg.player) - this.SetupRangeQuery(); + this.SetupRangeQueries(); }; UnitAI.prototype.OnOwnershipChanged = function(msg) @@ -4976,7 +4991,13 @@ UnitAI.prototype.Garrison = function(target, queued) UnitAI.prototype.Ungarrison = function() { if (this.IsGarrisoned()) + { + // Turret may be attacking, so we must finish all orders except the last one + // which must be Garrison or Autogarrison + while (this.orderQueue.length > 1) + this.FinishOrder(); this.AddOrder("Ungarrison", null, false); + } }; /**