mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 13:23:56 -07:00
Added code to requestLowLevelPath that modifies the final waypoint to use exactly the coordinates where the player clicked, instead of the rounded-to-nearest-tile coordinates from the A* pathfinder. A more complete fix required eventually might be to make the A* engine itself aware of fractional locations within a tile but for now this seemed like a simple thing to do. Also fixed a bug that was causing the final step of the path not to be added: the code was adding an ORDER_PATH_END_MARKER but no ORDER_GOTO_NOPATHING for the final point. The ORDER_PATH_END_MARKER doesn't seem to do anything useful here so the unit never went that last step. Also fixed a bug with "pass-through-allies" that was causing units to avoid allies anyway: although the pathfinder assumed you could pass through allies, when you actually bumped into one while executing a goto instruction, the collision test used did not check for pass-through-allies so you ended up avoiding allies anyway. This might not be too horrible if it works in practice (since the avoidance was done using simple local work, not a full repath) but I've removed it for now since it might cause problems with formations, when avoidance will not be trivial. This was SVN commit r4226.
111 lines
3.5 KiB
C++
111 lines
3.5 KiB
C++
#include "precompiled.h"
|
|
|
|
#include "ps/Profile.h"
|
|
|
|
#include "EntityOrders.h"
|
|
#include "Entity.h"
|
|
#include "PathfindEngine.h"
|
|
|
|
|
|
CPathfindEngine::CPathfindEngine()
|
|
{
|
|
}
|
|
|
|
void CPathfindEngine::requestPath( HEntity entity, const CVector2D& destination )
|
|
{
|
|
/* TODO: Add code to generate high level path
|
|
For now, just the one high level waypoint to the final
|
|
destination is added
|
|
*/
|
|
CEntityOrder waypoint;
|
|
waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT;
|
|
waypoint.m_data[0].location = destination;
|
|
*((float*)&waypoint.m_data[0].data) = 0.0f;
|
|
entity->m_orderQueue.push_front( waypoint );
|
|
}
|
|
|
|
void CPathfindEngine::requestLowLevelPath( HEntity entity, const CVector2D& destination, bool contact, float radius )
|
|
{
|
|
PROFILE_START("Pathfinding");
|
|
|
|
CVector2D source( entity->m_position.X, entity->m_position.Z );
|
|
|
|
if ( mLowPathfinder.findPath(source, destination, entity->m_player, radius) )
|
|
{
|
|
std::vector<CVector2D> path = mLowPathfinder.getLastPath();
|
|
if( path.size() > 0 )
|
|
{
|
|
std::vector<CVector2D>::iterator it;
|
|
CEntityOrder node;
|
|
for( it = path.begin(); (it+1) != path.end(); it++ )
|
|
{
|
|
if ( !contact )
|
|
{
|
|
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING;
|
|
}
|
|
else
|
|
{
|
|
// TODO: Is this right?
|
|
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING;
|
|
}
|
|
node.m_data[0].location = *it;
|
|
entity->m_orderQueue.push_back(node);
|
|
}
|
|
// Hack to make pathfinding slightly more precise:
|
|
// If the radius was 0, make the final node be exactly at the destination
|
|
// (otherwise, go to wherever the pathfinder tells us since we just want to be in range)
|
|
CVector2D finalDest = (radius==0 ? destination : (*it));
|
|
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING;
|
|
node.m_data[0].location = finalDest;
|
|
entity->m_orderQueue.push_back(node);
|
|
node.m_type = CEntityOrder::ORDER_PATH_END_MARKER;
|
|
node.m_data[0].location = finalDest;
|
|
entity->m_orderQueue.push_back(node);
|
|
}
|
|
else {
|
|
// Hack to make pathfinding slightly more precise:
|
|
// If radius = 0, we are still moving within the same tile, so add a GOTO order anyway
|
|
if(radius == 0)
|
|
{
|
|
CEntityOrder node;
|
|
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING;
|
|
node.m_data[0].location = destination;
|
|
entity->m_orderQueue.push_back(node);
|
|
node.m_type = CEntityOrder::ORDER_PATH_END_MARKER;
|
|
node.m_data[0].location = destination;
|
|
entity->m_orderQueue.push_back(node);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// If no path was found, then unsolvable
|
|
// TODO: Figure out what to do in this case
|
|
}
|
|
|
|
PROFILE_END("Pathfinding");
|
|
}
|
|
|
|
void CPathfindEngine::requestContactPath( HEntity entity, CEntityOrder* current, float range )
|
|
{
|
|
/* TODO: Same as non-contact: need high-level planner */
|
|
CEntityOrder waypoint;
|
|
waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT_CONTACT;
|
|
waypoint.m_data[0].location = current->m_data[0].entity->m_position;
|
|
*((float*)&waypoint.m_data[0].data) = std::max( current->m_data[0].entity->m_bounds->m_radius, range );
|
|
entity->m_orderQueue.push_front( waypoint );
|
|
|
|
//pathSparse( entity, current->m_data[0].entity->m_position );
|
|
//// For attack orders, do some additional postprocessing (replace goto/nopathing
|
|
//// with attack/nopathing, up until the attack order marker)
|
|
//std::deque<CEntityOrder>::iterator it;
|
|
//for( it = entity->m_orderQueue.begin(); it != entity->m_orderQueue.end(); it++ )
|
|
//{
|
|
// if( it->m_type == CEntityOrder::ORDER_PATH_END_MARKER )
|
|
// break;
|
|
// if( it->m_type == CEntityOrder::ORDER_GOTO_NOPATHING )
|
|
// {
|
|
// *it = *current;
|
|
// }
|
|
//}
|
|
}
|