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 @@ -2880,12 +2880,66 @@ }, "leave": function() { + // If a pickup has been requested, remove it. + 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) + if (!msg.likelyFailure && !msg.likelySuccess) + return; + + let target = this.order.data.target; + if (this.CanGarrison(target) && this.CheckGarrisonRange(target)) + { + let cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); + // In range but garrison failed somehow, abort the order. + if (!cmpGarrisonHolder.Garrison(this.entity)) + { + this.FinishOrder(); + return; + } + + if (this.formationController) + { + let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); + if (cmpFormation) + { + // 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); + } + } + if (this.pickup) + { + let cmpHolderPosition = Engine.QueryInterface(target, IID_Position); + let cmpHolderUnitAI = Engine.QueryInterface(target, IID_UnitAI); + if (cmpHolderUnitAI && cmpHolderPosition) + cmpHolderUnitAI.lastShorelinePosition = cmpHolderPosition.GetPosition(); + } + this.SetNextState("GARRISONED"); + } + 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) + { + let cmpUnitAI = Engine.QueryInterface(this.pickup, IID_UnitAI); + if (!cmpUnitAI || !cmpUnitAI.HasPickupOrder(this.entity)) + this.FinishOrder(); + + } + } }, }, @@ -2898,93 +2952,33 @@ return true; } - if (this.IsGarrisoned()) - return false; + this.isGarrisoned = 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)) + // Check if we are garrisoned in a dropsite. + let cmpResourceDropsite = Engine.QueryInterface(target, IID_ResourceDropsite); + if (cmpResourceDropsite && this.CanReturnResource(target, true)) + { + // Dump any resources we can. + let dropsiteTypes = cmpResourceDropsite.GetTypes(); + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + if (cmpResourceGatherer) { - var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); - // Check that garrisoning succeeds - if (cmpGarrisonHolder.Garrison(this.entity)) - { - this.isGarrisoned = 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 - var cmpResourceDropsite = Engine.QueryInterface(target, IID_ResourceDropsite); - if (cmpResourceDropsite && this.CanReturnResource(target, true)) - { - // Dump any resources we can - var dropsiteTypes = cmpResourceDropsite.GetTypes(); - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); - if (cmpResourceGatherer) - { - cmpResourceGatherer.CommitResources(dropsiteTypes); - this.SetDefaultAnimationVariant(); - } - } - - // If a pickup has been requested, remove it - if (this.pickup) - { - var cmpHolderPosition = Engine.QueryInterface(target, IID_Position); - var cmpHolderUnitAI = Engine.QueryInterface(target, IID_UnitAI); - if (cmpHolderUnitAI && cmpHolderPosition) - cmpHolderUnitAI.lastShorelinePosition = cmpHolderPosition.GetPosition(); - Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); - delete this.pickup; - } - - if (this.IsTurret()) - { - this.SetNextState("IDLE"); - return true; - } - - return false; - } + cmpResourceGatherer.CommitResources(dropsiteTypes); + this.SetDefaultAnimationVariant(); } - 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)) - { - this.FinishOrder(); - return true; - } + } - } - this.SetNextState("APPROACHING"); - return true; - } - // Garrisoning failed for some reason, so finish the order - this.FinishOrder(); - return true; + if (this.IsTurret()) + { + this.SetNextState("IDLE"); + return true; + } + + return false; }, "leave": function() { - } + }, }, },