1
1
forked from 0ad/0ad

Fix walk-to-target and other movements with max-range "0" for target entities

Entities may never get to a distance of "0" when trying to reach other
entities, as that would mean they are right next to each other, or even
overlapping, which can fail.
Always give some leeway to distance when trying to move to a target with
no specified max-range.

This fixes WalkToTarget orders which resulted in units being stuck on
mines or on dropsites occasionally.

Reported by: elexis
Fixes #5510

Differential Revision: https://code.wildfiregames.com/D2087
This was SVN commit r22496.
This commit is contained in:
wraitii 2019-07-17 18:11:15 +00:00
parent dbebe0a39a
commit 81cd2f1cdf

View File

@ -320,7 +320,7 @@ UnitAI.prototype.UnitFsmSpec = {
}
if (this.CheckTargetRangeExplicit(this.order.data.target, 0, 0))
if (this.CheckRange(this.order.data))
{
// We are already at the target, or can't move at all
this.FinishOrder();
@ -334,29 +334,28 @@ UnitAI.prototype.UnitFsmSpec = {
},
"Order.PickupUnit": function(msg) {
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
if (!cmpGarrisonHolder || cmpGarrisonHolder.IsFull())
{
this.FinishOrder();
return;
}
if (this.CheckTargetRangeExplicit(this.order.data.target, 0, 0))
if (this.CheckRange(this.order.data))
{
this.FinishOrder();
return;
}
// Check if we need to move TODO implement a better way to know if we are on the shoreline
var needToMove = true;
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x)
&& (this.lastShorelinePosition.z == cmpPosition.GetPosition().z))
{
// Check if we need to move
// TODO implement a better way to know if we are on the shoreline
let needToMove = true;
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x) &&
(this.lastShorelinePosition.z == cmpPosition.GetPosition().z))
// we were already on the shoreline, and have not moved since
if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50)
needToMove = false;
}
if (needToMove)
this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING");
@ -665,21 +664,21 @@ UnitAI.prototype.UnitFsmSpec = {
// Only used by other orders to walk there in formation
"Order.WalkToTargetRange": function(msg) {
if (!this.CheckTargetRangeExplicit(this.order.data.target, this.order.data.min, this.order.data.max))
if (!this.CheckRange(this.order.data))
this.SetNextState("WALKING");
else
this.FinishOrder();
},
"Order.WalkToTarget": function(msg) {
if (!this.CheckTargetRangeExplicit(this.order.data.target, 0, 0))
if (!this.CheckRange(this.order.data))
this.SetNextState("WALKING");
else
this.FinishOrder();
},
"Order.WalkToPointRange": function(msg) {
if (!this.CheckPointRangeExplicit(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max))
if (!this.CheckRange(this.order.data))
this.SetNextState("WALKING");
else
this.FinishOrder();
@ -1291,7 +1290,7 @@ UnitAI.prototype.UnitFsmSpec = {
if (!atDestination && cmpPosition)
{
let pos = cmpPosition.GetPosition2D();
atDestination = this.CheckPointRangeExplicit(pos.X + this.order.data.x, pos.Y + this.order.data.z, 0, 0);
atDestination = this.CheckPointRangeExplicit(pos.X + this.order.data.x, pos.Y + this.order.data.z, 0, 1);
}
if (!atDestination && !msg.error)
return;
@ -4147,7 +4146,7 @@ UnitAI.prototype.MoveTo = function(data, iid, type)
UnitAI.prototype.MoveToPoint = function(x, z)
{
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
return cmpUnitMotion.MoveToPointRange(x, z, 0, 0);
return cmpUnitMotion.MoveToPointRange(x, z, 0, 0); // For point goals, allow a max range of 0.
};
UnitAI.prototype.MoveToPointRange = function(x, z, rangeMin, rangeMax)
@ -4162,7 +4161,7 @@ UnitAI.prototype.MoveToTarget = function(target)
return false;
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
return cmpUnitMotion.MoveToTargetRange(target, 0, 0);
return cmpUnitMotion.MoveToTargetRange(target, 0, 1);
};
UnitAI.prototype.MoveToTargetRange = function(target, iid, type)
@ -4268,7 +4267,7 @@ UnitAI.prototype.CheckRange = function(data, iid, type)
if (data.min || data.max)
return this.CheckTargetRangeExplicit(data.target, data.min || -1, data.max || -1);
else if (!iid)
return this.CheckTargetRangeExplicit(data.target, 0, 0);
return this.CheckTargetRangeExplicit(data.target, 0, 1);
return this.CheckTargetRange(data.target, iid, type);
}