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 @@ -1685,12 +1685,22 @@ "FLEEING": { "enter": function() { - // We use the distance between the entities to account for ranged attacks - let distance = DistanceBetweenEntities(this.entity, this.order.data.target) + (+this.template.FleeDistance); - let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); - // Use unit motion directly to ignore the visibility check. TODO: change this if we add LOS to fauna. - if (this.CheckTargetRangeExplicit(this.order.data.target, distance, distance) || - !cmpUnitMotion || !cmpUnitMotion.MoveToTargetRange(this.order.data.target, distance, -1)) + let cmpPosition = Engine.QueryInterface(this.order.data.target, IID_Position); + if (!cmpPosition) + { + this.FinishOrder(); + return true; + } + + this.order.data = { + "x": cmpPosition.GetPosition().x, + "z": cmpPosition.GetPosition().z, + // We use the distance between the entities to account for ranged attacks + "min": DistanceBetweenEntities(this.entity, this.order.data.target) + (+this.template.FleeDistance) + }; + + // Move away from the attacker's position and not the attacker to avoid fleeing infinitely. + if (this.CheckRange(this.order.data) || !this.MoveTo(this.order.data)) { this.FinishOrder(); return true;