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 @@ -549,25 +549,6 @@ }, "Order.ReturnResource": function(msg) { - // Check if the dropsite is already in range - if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer) && this.CanReturnResource(this.order.data.target, true)) - { - var cmpResourceDropsite = Engine.QueryInterface(this.order.data.target, IID_ResourceDropsite); - if (cmpResourceDropsite) - { - // Dump any resources we can - var dropsiteTypes = cmpResourceDropsite.GetTypes(); - - Engine.QueryInterface(this.entity, IID_ResourceGatherer).CommitResources(dropsiteTypes); - // Stop showing the carried resource animation. - this.SetDefaultAnimationVariant(); - - // Our next order should always be a Gather, - // so just switch back to that order - this.FinishOrder(); - return; - } - } this.SetNextState("INDIVIDUAL.RETURNRESOURCE.APPROACHING"); }, @@ -619,7 +600,6 @@ "Order.Ungarrison": function() { this.FinishOrder(); - this.isGarrisoned = false; }, "Order.Cheering": function(msg) { @@ -637,16 +617,10 @@ }, "Order.CancelPack": function(msg) { - var cmpPack = Engine.QueryInterface(this.entity, IID_Pack); - if (cmpPack && cmpPack.IsPacking() && !cmpPack.IsPacked()) - cmpPack.CancelPack(); this.FinishOrder(); }, "Order.CancelUnpack": function(msg) { - var cmpPack = Engine.QueryInterface(this.entity, IID_Pack); - if (cmpPack && cmpPack.IsPacking() && cmpPack.IsPacked()) - cmpPack.CancelPack(); this.FinishOrder(); }, @@ -2593,55 +2567,73 @@ "RETURNRESOURCE": { "APPROACHING": { "enter": function() { - if (!this.MoveTo(this.order.data)) + if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer)) { - this.FinishOrder(); + this.SetNextState("DROPPING"); return true; } + + if (!this.MoveTo(this.order.data, IID_ResourceGatherer)) + { + this.SetNextState("LOOKINGFORDROPSITE"); + return; + } + this.SelectAnimation("move"); }, "leave": function() { - // Switch back to idle animation to guarantee we won't - // get stuck with the carry animation after stopping moving + this.StopMoving(); this.SelectAnimation("idle"); }, "MoveCompleted": function() { - // Check the dropsite is in range and we can return our resource there - // (we didn't get stopped before reaching it) - if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer) && this.CanReturnResource(this.order.data.target, true)) + this.SetNextState("DROPPING"); + }, + }, + + "DROPPING": { + "enter": function() { + if (!this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer)) + { + this.SetNextState("APPROACHING"); + return true; + } + + let cmpResourceDropsite = Engine.QueryInterface(this.order.data.target, IID_ResourceDropsite); + + if (! cmpResourceDropsite || !this.CanReturnResource(this.order.data.target, true)) { - var cmpResourceDropsite = Engine.QueryInterface(this.order.data.target, IID_ResourceDropsite); - if (cmpResourceDropsite) - { - // Dump any resources we can - var dropsiteTypes = cmpResourceDropsite.GetTypes(); - - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); - cmpResourceGatherer.CommitResources(dropsiteTypes); - - // Stop showing the carried resource animation. - this.SetDefaultAnimationVariant(); - - // Our next order should always be a Gather, - // so just switch back to that order - this.FinishOrder(); - return; - } + this.SetNextState("LOOKINGFORDROPSITE"); + return true; } - // The dropsite was destroyed, or we couldn't reach it, or ownership changed - // Look for a new one. + // Dump any resources we can + let dropsiteTypes = cmpResourceDropsite.GetTypes(); + + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + cmpResourceGatherer.CommitResources(dropsiteTypes); + + // Stop showing the carried resource animation. + this.SetDefaultAnimationVariant(); + + // Our next order should always be a Gather, + // so just switch back to that order + this.FinishOrder(); + return true; + }, + }, - var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); - var genericType = cmpResourceGatherer.GetMainCarryingType(); - var nearby = this.FindNearestDropsite(genericType); + "LOOKINGFORDROPSITE": { + "enter": function() { + let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); + let genericType = cmpResourceGatherer.GetMainCarryingType(); + let nearby = this.FindNearestDropsite(genericType); if (nearby) { this.FinishOrder(); this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); - return; + return true; } // Oh no, couldn't find any drop sites. Give up on returning. @@ -3019,6 +3011,7 @@ }, "leave": function() { + this.isGarrisoned = false; } }, }, @@ -3057,6 +3050,9 @@ }, "leave": function() { + let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); + if (cmpPack && cmpPack.IsPacking() && !cmpPack.IsPacked()) + cmpPack.CancelPack(); }, "Attacked": function(msg) { @@ -3077,6 +3073,9 @@ }, "leave": function() { + let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); + if (cmpPack && cmpPack.IsPacking() && !cmpPack.IsPacked()) + cmpPack.CancelPack(); }, "Attacked": function(msg) {