From 99a341f37903f4b25609ad94b884b62d3f696c82 Mon Sep 17 00:00:00 2001 From: wraitii Date: Wed, 3 Jul 2019 18:09:31 +0000 Subject: [PATCH] UnitMotion - account for target's movement in ComputeTargetPosition (improve chasing behaviour). After the recent UM changes, units sometimes chase/flee forever as they can never actually get in range. This is because moving to the current target's position is not enough when the target is moving. By accounting for the target movement's in ComputeTargetPosition, the behaviour is much improved. Differential Revision: https://code.wildfiregames.com/D1987 This was SVN commit r22431. --- source/simulation2/components/CCmpUnitMotion.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/source/simulation2/components/CCmpUnitMotion.cpp b/source/simulation2/components/CCmpUnitMotion.cpp index 9bc4023443..8e9c1ad0db 100644 --- a/source/simulation2/components/CCmpUnitMotion.cpp +++ b/source/simulation2/components/CCmpUnitMotion.cpp @@ -1065,7 +1065,17 @@ bool CCmpUnitMotion::ComputeTargetPosition(CFixedVector2D& out) const out = cmpPosition->GetPosition2D() + offset; } else + { out = cmpPosition->GetPosition2D(); + // If the target is moving, we might never get in range if we just try to reach its current position, + // so we have to try and move to a position where we will be in-range, including their movement. + // Since we request paths asynchronously a the end of our turn, we need to account for twice the movement speed. + // TODO: be cleverer about this. It fixes fleeing nicely currently, but orthogonal movement should be considered, + // and the overall logic could be improved upon. + CmpPtr cmpUnitMotion(GetSimContext(), m_MoveRequest.m_Entity); + if (cmpUnitMotion && cmpUnitMotion->IsMoving()) + out += (out - cmpPosition->GetPreviousPosition2D()) * 2; + } return true; }