1
1
forked from 0ad/0ad

Fix some animals "roaming" state not working correctly (units seem stuck in place)

Due to an issue in 4a15cc3b9f, animals incorrectly tried going towards
the roaming point instead of away from it.

With that fixed, MovementUpdate still did not trigger as the min and max
range were the same.
Use -1 as max range (= infinite) instead since we want to move
arbitrarily away.

Having an infinite max range was broken in c219ee54b2, this
re-implements that.

Further, other calls with equal min and max range have been changed
likewise.

This does not entirely fix whales, which run in other problems because
of their large roaming range.

Differential Revision: https://code.wildfiregames.com/D1980
This was SVN commit r22413.
This commit is contained in:
wraitii 2019-06-30 18:53:25 +00:00
parent a7573ae911
commit f42d97c6f3
4 changed files with 22 additions and 13 deletions

View File

@ -1232,7 +1232,7 @@ UnitAI.prototype.UnitFsmSpec = {
}
// Move a tile outside the building
let range = 4;
if (this.CheckTargetRangeExplicit(msg.data.target, range, range))
if (this.CheckTargetRangeExplicit(msg.data.target, range, -1))
{
// We are already at the target, or can't move at all
this.FinishOrder();
@ -2963,17 +2963,14 @@ UnitAI.prototype.UnitFsmSpec = {
"Order.LeaveFoundation": function(msg) {
// Move a tile outside the building
var range = 4;
if (this.CheckTargetRangeExplicit(msg.data.target, range, range))
let range = 4;
if (this.CheckTargetRangeExplicit(msg.data.target, range, -1))
{
this.FinishOrder();
return;
}
else
{
this.order.data.min = range;
this.SetNextState("WALKING");
}
this.order.data.min = range;
this.SetNextState("WALKING");
},
"IDLE": {
@ -5785,7 +5782,7 @@ UnitAI.prototype.MoveRandomly = function(distance)
// Then second half of the rotation
ang += halfDelta;
let dist = randFloat(0.5, 1.5) * distance;
cmpUnitMotion.MoveToPointRange(pos.x - 0.5 * Math.sin(ang), pos.z - 0.5 * Math.cos(ang), dist, dist);
cmpUnitMotion.MoveToPointRange(pos.x - 0.5 * Math.sin(ang), pos.z - 0.5 * Math.cos(ang), dist, -1);
};
UnitAI.prototype.SetFacePointAfterMove = function(val)

View File

@ -821,18 +821,26 @@ fixed CCmpObstructionManager::MaxDistanceBetweenShapes(const ObstructionSquare&
bool CCmpObstructionManager::IsInPointRange(entity_id_t ent, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const
{
fixed dist = DistanceToPoint(ent, px, pz);
return dist != fixed::FromInt(-1) && dist <= maxRange + fixed::FromFloat(0.0001) && (opposite ? MaxDistanceToPoint(ent, px, pz) : dist) >= minRange - fixed::FromFloat(0.0001);
// Treat -1 max range as infinite
return dist != fixed::FromInt(-1) &&
(dist <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) &&
(opposite ? MaxDistanceToPoint(ent, px, pz) : dist) >= minRange - fixed::FromFloat(0.0001);
}
bool CCmpObstructionManager::IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const
{
fixed dist = DistanceToTarget(ent, target);
return dist != fixed::FromInt(-1) && dist <= maxRange + fixed::FromFloat(0.0001) && (opposite ? MaxDistanceToTarget(ent, target) : dist) >= minRange- fixed::FromFloat(0.0001);
// Treat -1 max range as infinite
return dist != fixed::FromInt(-1) &&
(dist <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) &&
(opposite ? MaxDistanceToTarget(ent, target) : dist) >= minRange - fixed::FromFloat(0.0001);
}
bool CCmpObstructionManager::IsPointInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange) const
{
entity_pos_t distance = (CFixedVector2D(x, z) - CFixedVector2D(px, pz)).Length();
return distance <= maxRange + fixed::FromFloat(0.0001) && distance >= minRange - fixed::FromFloat(0.0001);
// Treat -1 max range as infinite
return (distance <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) &&
distance >= minRange - fixed::FromFloat(0.0001);
}
bool CCmpObstructionManager::TestLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits) const

View File

@ -1081,7 +1081,8 @@ bool CCmpUnitMotion::ComputeTargetPosition(CFixedVector2D& out) const
bool CCmpUnitMotion::TryGoingStraightToGoalPoint(const CFixedVector2D& from)
{
// Make sure the goal is a point (and not a point-like target like a formation controller)
if (m_MoveRequest.m_Type != MoveRequest::POINT)
if (m_MoveRequest.m_Type != MoveRequest::POINT ||
m_MoveRequest.m_MinRange > fixed::Zero())
return false;
// Fail if the goal is too far away

View File

@ -201,16 +201,19 @@ public:
/**
* Check if the given entity is in range of the other point given those parameters.
* @param maxRange - if -1, treated as infinite.
*/
virtual bool IsInPointRange(entity_id_t ent, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const = 0;
/**
* Check if the given entity is in range of the target given those parameters.
* @param maxRange - if -1, treated as infinite.
*/
virtual bool IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const = 0;
/**
* Check if the given point is in range of the other point given those parameters.
* @param maxRange - if -1, treated as infinite.
*/
virtual bool IsPointInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange) const = 0;