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 @@ -815,10 +815,10 @@ "template": data.template }); } - return; + return undefined; } this.PushOrderFront("Attack", { "target": msg.data.target, "force": !!msg.data.force, "hunting": true, "allowCapture": false, "min": 0, "max": 10 }); - return; + return undefined; } // TODO: on what should we base this range? @@ -828,15 +828,22 @@ this.FinishOrder(); // TODO: Should we issue a gather-near-position order // if the target isn't gatherable/doesn't exist anymore? - else + else if (!msg.data.secondTry) + { + msg.data.secondTry = true; // Out of range; move there in formation this.PushOrderFront("WalkToTargetRange", { "target": msg.data.target, "min": 0, "max": 10 }); - return; + } + else + return { "discardOrder": true }; + return undefined; } this.CallMemberFunction("Gather", [msg.data.target, false]); this.SetNextState("MEMBER"); + + return undefined; }, "Order.GatherNearPosition": function(msg) { @@ -859,15 +866,22 @@ { if (!this.TargetIsAlive(msg.data.target) || !this.CheckTargetVisible(msg.data.target)) this.FinishOrder(); - else + else if (!msg.data.secondTry) + { + msg.data.secondTry = true; // Out of range; move there in formation this.PushOrderFront("WalkToTargetRange", { "target": msg.data.target, "min": 0, "max": 10 }); - return; + } + else + return { "discardOrder": true }; + return undefined; } this.CallMemberFunction("Heal", [msg.data.target, false]); this.SetNextState("MEMBER"); + + return undefined; }, "Order.Repair": function(msg) { @@ -876,15 +890,22 @@ { if (!this.TargetIsAlive(msg.data.target) || !this.CheckTargetVisible(msg.data.target)) this.FinishOrder(); - else - // Out of range move there in formation + else if (!msg.data.secondTry) + { + msg.data.secondTry = true; + // Out of range; move there in formation this.PushOrderFront("WalkToTargetRange", { "target": msg.data.target, "min": 0, "max": 10 }); - return; + } + else + return { "discardOrder": true }; + return undefined; } this.CallMemberFunction("Repair", [msg.data.target, msg.data.autocontinue, false]); this.SetNextState("MEMBER"); + + return undefined; }, "Order.ReturnResource": function(msg) { @@ -893,15 +914,22 @@ { if (!this.CheckTargetVisible(msg.data.target)) this.FinishOrder(); - else + else if (!msg.data.secondTry) + { + msg.data.secondTry = true; // Out of range; move there in formation this.PushOrderFront("WalkToTargetRange", { "target": msg.data.target, "min": 0, "max": 10 }); - return; + } + else + return { "discardOrder": true }; + return undefined; } this.CallMemberFunction("ReturnResource", [msg.data.target, false]); this.SetNextState("MEMBER"); + + return undefined; }, "Order.Pack": function(msg) { @@ -948,14 +976,14 @@ "WALKING": { "enter": function() { + let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); + cmpFormation.SetRearrange(true); + cmpFormation.MoveMembersIntoFormation(true, true); if (!this.MoveTo(this.order.data)) { this.FinishOrder(); return true; } - let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); - cmpFormation.SetRearrange(true); - cmpFormation.MoveMembersIntoFormation(true, true); return false; }, @@ -988,15 +1016,15 @@ "WALKINGANDFIGHTING": { "enter": function(msg) { - if (!this.MoveTo(this.order.data)) - { - this.FinishOrder(); - return true; - } - this.StartTimer(0, 1000); let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); cmpFormation.SetRearrange(true); cmpFormation.MoveMembersIntoFormation(true, true, "combat"); + if (!this.MoveTo(this.order.data)) + { + this.FinishOrder(); + return true; + } + this.StartTimer(0, 1000); return false; }, @@ -1043,18 +1071,18 @@ "PATROLLING": { "enter": function() { - let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); - if (!cmpPosition || !cmpPosition.IsInWorld() || - !this.MoveTo(this.order.data)) - { - this.FinishOrder(); - return true; - } - let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); cmpFormation.SetRearrange(true); cmpFormation.MoveMembersIntoFormation(true, true, "combat"); + let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); + if (!cmpPosition || !cmpPosition.IsInWorld() || + !this.MoveTo(this.order.data)) + { + this.FinishOrder(); + return true; + } + this.StartTimer(0, 1000); return false; }, @@ -1108,15 +1136,16 @@ "GARRISON": { "APPROACHING": { "enter": function() { - if (!this.MoveToGarrisonRange(this.order.data.target)) - { - this.FinishOrder(); - return true; - } let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); cmpFormation.SetRearrange(true); cmpFormation.MoveMembersIntoFormation(true, true); + if (!this.MoveToGarrisonRange(this.order.data.target)) + { + this.FinishOrder(); + return true; + } + // If the garrisonholder should pickup, warn it so it can take needed action. let cmpGarrisonHolder = Engine.QueryInterface(this.order.data.target, IID_GarrisonHolder); if (cmpGarrisonHolder && cmpGarrisonHolder.CanPickup(this.entity)) @@ -1153,14 +1182,15 @@ "FORMING": { "enter": function() { + let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); + cmpFormation.SetRearrange(true); + cmpFormation.MoveMembersIntoFormation(true, true); + if (!this.MoveTo(this.order.data)) { this.FinishOrder(); return true; } - let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); - cmpFormation.SetRearrange(true); - cmpFormation.MoveMembersIntoFormation(true, true); return false; }, @@ -1179,14 +1209,15 @@ "COMBAT": { "APPROACHING": { "enter": function() { + let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); + cmpFormation.SetRearrange(true); + cmpFormation.MoveMembersIntoFormation(true, true, "combat"); + if (!this.MoveFormationToTargetAttackRange(this.order.data.target)) { this.FinishOrder(); return true; } - let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); - cmpFormation.SetRearrange(true); - cmpFormation.MoveMembersIntoFormation(true, true, "combat"); return false; },