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 @@ -225,11 +225,12 @@ this.FinishOrder(); return; } - // Move a tile outside the building + + // Move a tile outside the building if necessary. let range = 4; - if (this.MoveToTargetRangeExplicit(msg.data.target, range, range)) + if (!this.CheckTargetRangeExplicit(msg.data.target, range, range) && + this.MoveToTargetRangeExplicit(msg.data.target, range, range)) { - // We've started walking to the given point this.SetNextState("INDIVIDUAL.WALKING"); } else @@ -333,7 +334,7 @@ return; } - var ok = this.MoveToTarget(this.order.data.target); + let ok = !this.CheckTargetRange(this.order.data.target) && this.MoveToTarget(this.order.data.target); if (ok) { // We've started walking to the given point @@ -371,7 +372,7 @@ // TODO: what if the units are on a cliff ? the ship will go below the cliff // and the units won't be able to garrison. Should go to the nearest (accessible) shore - if (needToMove && this.MoveToTarget(this.order.data.target)) + if (needToMove && !this.CheckTargetRange(this.order.data.target) && this.MoveToTarget(this.order.data.target)) { this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING"); } @@ -390,7 +391,8 @@ return; } - if (this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) + if (!this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange) && + this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) this.SetNextState("INDIVIDUAL.GUARD.ESCORTING"); else this.SetNextState("INDIVIDUAL.GUARD.GUARDING"); @@ -398,9 +400,9 @@ "Order.Flee": function(msg) { // We use the distance between the entities to account for ranged attacks - var distance = DistanceBetweenEntities(this.entity, this.order.data.target) + (+this.template.FleeDistance); - var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); - if (cmpUnitMotion.MoveToTargetRange(this.order.data.target, distance, -1)) + let distance = DistanceBetweenEntities(this.entity, this.order.data.target) + (+this.template.FleeDistance); + if (!this.CheckTargetRangeExplicit(this.order.data.target, distance, distance) && + this.MoveToTargetRangeExplicit(this.order.data.target, distance, distance)) { // We've started fleeing from the given target if (this.IsAnimal()) @@ -482,7 +484,8 @@ } // Try to move within attack range - if (this.MoveToTargetAttackRange(this.order.data.target, this.order.data.attackType)) + if (!this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType) && + this.MoveToTargetAttackRange(this.order.data.target, this.order.data.attackType)) { // We've started walking to the given point if (this.IsAnimal()) @@ -546,7 +549,8 @@ } // Try to move within heal range - if (this.MoveToTargetRange(this.order.data.target, IID_Heal)) + if (!this.CheckTargetRange(this.order.data.target, IID_Heal) && + this.MoveToTargetRange(this.order.data.target, IID_Heal)) { // We've started walking to the given point this.SetNextState("INDIVIDUAL.HEAL.APPROACHING"); @@ -592,7 +596,8 @@ } // Try to move within range - if (this.MoveToTargetRange(this.order.data.target, IID_ResourceGatherer)) + if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer) && + !this.MoveToTargetRange(this.order.data.target, IID_ResourceGatherer)) { // We've started walking to the given point this.SetNextState("INDIVIDUAL.GATHER.APPROACHING"); @@ -634,7 +639,8 @@ } } // Try to move to the dropsite - if (this.MoveToTargetRange(this.order.data.target, IID_ResourceGatherer)) + if (!this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer) && + this.MoveToTargetRange(this.order.data.target, IID_ResourceGatherer)) { // We've started walking to the target this.SetNextState("INDIVIDUAL.RETURNRESOURCE.APPROACHING"); @@ -669,7 +675,8 @@ "Order.Repair": function(msg) { // Try to move within range - if (this.MoveToTargetRange(this.order.data.target, IID_Builder)) + if (!this.CheckTargetRange(this.order.data.target, IID_Builder) && + this.MoveToTargetRange(this.order.data.target, IID_Builder)) { // We've started walking to the given point this.SetNextState("INDIVIDUAL.REPAIR.APPROACHING"); @@ -782,21 +789,24 @@ // Only used by other orders to walk there in formation "Order.WalkToTargetRange": function(msg) { - if (this.MoveToTargetRangeExplicit(this.order.data.target, this.order.data.min, this.order.data.max)) + if (!this.CheckTargetRangeExplicit(this.order.data.target, this.order.data.min, this.order.data.max) && + this.MoveToTargetRangeExplicit(this.order.data.target, this.order.data.min, this.order.data.max)) this.SetNextState("WALKING"); else this.FinishOrder(); }, "Order.WalkToTarget": function(msg) { - if (this.MoveToTarget(this.order.data.target)) + if (!this.CheckTargetRange(this.order.data.target) && + this.MoveToTarget(this.order.data.target)) this.SetNextState("WALKING"); else this.FinishOrder(); }, "Order.WalkToPointRange": function(msg) { - if (this.MoveToPointRange(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max)) + if (!this.CheckPointRangeExplicit(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max) && + this.MoveToPointRange(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max)) this.SetNextState("WALKING"); else this.FinishOrder(); @@ -1324,7 +1334,8 @@ } // Move a tile outside the building let range = 4; - if (this.MoveToTargetRangeExplicit(msg.data.target, range, range)) + if (!this.CheckTargetRangeExplicit(msg.data.target, range, range) && + this.MoveToTargetRangeExplicit(msg.data.target, range, range)) { // We've started walking to the given point this.SetNextState("WALKINGTOPOINT"); @@ -1682,7 +1693,7 @@ "MoveCompleted": function() { this.ResetMoveSpeed(); - if (!this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) + if (this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) this.SetNextState("GUARDING"); }, }, @@ -1710,7 +1721,8 @@ return; } // then check is the target has moved - if (this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) + if (!this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange) && + this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) this.SetNextState("ESCORTING"); else { @@ -2376,8 +2388,8 @@ var maxRange = 8; // get close but not too close if (this.order.data.lastPos && - this.MoveToPointRange(this.order.data.lastPos.x, this.order.data.lastPos.z, - 0, maxRange)) + !this.CheckPointRangeExplicit(this.order.data.lastPos.x, this.order.data.lastPos.z, 0, maxRange) && + this.MoveToPointRange(this.order.data.lastPos.x, this.order.data.lastPos.z, 0, maxRange)) { this.SetNextState("APPROACHING"); return; @@ -2749,9 +2761,10 @@ // in that case, the repairTarget is deleted, and we can just return if (!this.repairTarget) return; - if (this.MoveToTargetRange(this.repairTarget, IID_Builder)) + let inRange = this.CheckTargetRange(this.repairTarget, IID_Builder); + if (!inRange && this.MoveToTargetRange(this.repairTarget, IID_Builder)) this.SetNextState("APPROACHING"); - else if (!this.CheckTargetRange(this.repairTarget, IID_Builder)) + else if (!inRange) this.FinishOrder(); //can't approach and isn't in reach }, }, @@ -2962,7 +2975,7 @@ } } - if (this.MoveToTarget(target)) + if (!this.CheckTargetRange(target) && this.MoveToTarget(target)) { this.SetNextState("APPROACHING"); return false; @@ -3093,7 +3106,8 @@ "Order.LeaveFoundation": function(msg) { // Move a tile outside the building var range = 4; - if (this.MoveToTargetRangeExplicit(msg.data.target, range, range)) + if (!this.CheckTargetRangeExplicit(msg.data.target, range, range) && + this.MoveToTargetRangeExplicit(msg.data.target, range, range)) { // We've started walking to the given point this.SetNextState("WALKING");