Index: binaries/data/mods/public/simulation/components/UnitAI.js =================================================================== --- binaries/data/mods/public/simulation/components/UnitAI.js +++ binaries/data/mods/public/simulation/components/UnitAI.js @@ -2998,16 +2998,6 @@ }, "GARRISON": { - "leave": function() { - // If a pickup has been requested and not yet canceled, cancel it. - if (this.pickup) - { - Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); - delete this.pickup; - } - return false; - }, - "APPROACHING": { "enter": function() { if (!this.MoveToGarrisonRange(this.order.data.target)) @@ -3031,103 +3021,90 @@ }, "leave": function() { + if (this.pickup) + { + Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); + delete this.pickup; + } this.StopMoving(); }, "MovementUpdate": function(msg) { - if (msg.likelyFailure || msg.likelySuccess) - this.SetNextState("GARRISONED"); + if (!msg.likelyFailure && !msg.likelySuccess) + return; + + if (this.CheckGarrisonRange(this.order.data.target)) + this.SetNextState("GARRISONING"); + else + { + // Unable to reach the target, try again (or follow if it is a moving target) + // except if the target does not exist anymore or its orders have changed. + if (this.pickup) + { + let cmpUnitAI = Engine.QueryInterface(this.pickup, IID_UnitAI); + if (!cmpUnitAI || (!cmpUnitAI.HasPickupOrder(this.entity) && !cmpUnitAI.IsIdle())) + this.FinishOrder(); + + } + } }, }, - "GARRISONED": { + "GARRISONING": { "enter": function() { let target = this.order.data.target; - if (!target) + if (!this.CanGarrison(target)) { this.FinishOrder(); return true; } - if (this.IsGarrisoned()) - return false; + let cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); + if (!cmpGarrisonHolder || !cmpGarrisonHolder.Garrison(this.entity)) + { + this.FinishOrder(); + return true; + } - // Check that we can garrison here - if (this.CanGarrison(target)) - // Check that we're in range of the garrison target - if (this.CheckGarrisonRange(target)) + if (this.formationController) + { + let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); + if (cmpFormation) { - var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); - // Check that garrisoning succeeds - if (cmpGarrisonHolder.Garrison(this.entity)) - { - this.isGarrisoned = true; - this.SetImmobile(true); - - if (this.formationController) - { - var cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); - if (cmpFormation) - { - // disable rearrange for this removal, - // but enable it again for the next - // move command - var rearrange = cmpFormation.rearrange; - cmpFormation.SetRearrange(false); - cmpFormation.RemoveMembers([this.entity]); - cmpFormation.SetRearrange(rearrange); - } - } - - // Check if we are garrisoned in a dropsite. - let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); - if (this.CanReturnResource(target, true, cmpResourceGatherer)) - { - cmpResourceGatherer.CommitResources(target); - this.SetDefaultAnimationVariant(); - } - - // If a pickup has been requested, remove it - if (this.pickup) - { - Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); - delete this.pickup; - } - - if (this.IsTurret()) - { - this.SetNextState("IDLE"); - return true; - } - - return false; - } + // Disable rearrange for this removal, but + // enable it again for the next move command. + let rearrange = cmpFormation.rearrange; + cmpFormation.SetRearrange(false); + cmpFormation.RemoveMembers([this.entity]); + cmpFormation.SetRearrange(rearrange); } - else - { - // Unable to reach the target, try again (or follow if it is a moving target) - // except if the does not exits anymore or its orders have changed - if (this.pickup) - { - var cmpUnitAI = Engine.QueryInterface(this.pickup, IID_UnitAI); - if (!cmpUnitAI || (!cmpUnitAI.HasPickupOrder(this.entity) && - !cmpUnitAI.IsIdle())) - { - this.FinishOrder(); - return true; - } + } - } - this.SetNextState("APPROACHING"); - return true; - } - // Garrisoning failed for some reason, so finish the order - this.FinishOrder(); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + if (this.CanReturnResource(target, true, cmpResourceGatherer)) + { + cmpResourceGatherer.CommitResources(target); + this.SetDefaultAnimationVariant(); + } + + this.SetNextState("GARRISONED"); return true; }, + }, - "leave": function() { - } + "GARRISONED": { + "enter": function() { + this.isGarrisoned = true; + this.SetImmobile(true); + + if (this.IsTurret()) + { + this.SetNextState("IDLE"); + return true; + } + + return false; + }, }, },