25 #include "precompiled.h"
35 #define PATHFIND_STATS 0
37 #define USE_DIAGONAL_MOVEMENT 1
68 dpi = (
i8)((
int)pi_ - (int)i);
69 dpj = (
i8)((
int)pj_ - (int)j);
72 ENSURE(pi_-i == -1 || pi_-i == 0 || pi_-i == 1);
73 ENSURE(pj_-j == -1 || pj_-j == 0 || pj_-j == 1);
139 for (
size_t n = 0; n < wp.size(); ++n)
211 size_t numImproveOpen;
212 size_t numImproveClosed;
227 return (dist < tolerance);
234 #if USE_DIAGONAL_MOVEMENT
248 const int premul = 32;
250 return (rdist * premul).ToInt_RoundToZero() * (
g_CostPerTile / premul);
253 return (abs((
int)i - (
int)iGoal) + abs((
int)j - (
int)jGoal)) *
g_CostPerTile;
262 #if USE_DIAGONAL_MOVEMENT
275 if (ppi != i && ppj != j)
276 dg = (dg << 16) / 92682;
282 if ((di == 1 && dj == 2) || (di == 2 && dj == 1))
283 dg = (dg << 16) / 79742;
294 state.numProcessed++;
338 state.numImproveOpen++;
348 state.numImproveClosed++;
359 PriorityQueue::Item t = { std::make_pair(i, j), g + n.
h };
362 state.numAddToOpen++;
383 path.m_Waypoints.push_back(w);
407 PriorityQueue::Item start = { std::make_pair(i0, j0), 0 };
409 state.
tiles->
get(i0, j0).SetStatusOpen();
410 state.
tiles->
get(i0, j0).SetPred(i0, j0, i0, j0);
423 if (state.
steps > 40000)
431 state.sumOpenSize += state.
open.
size();
435 PriorityQueue::Item curr = state.
open.
pop();
436 u16 i = curr.id.first;
437 u16 j = curr.id.second;
438 state.
tiles->
get(i, j).SetStatusClosed();
476 while (ip != i0 || jp != j0)
482 path.m_Waypoints.push_back(w);
500 printf(
"PATHFINDER: steps=%d avgo=%d proc=%d impc=%d impo=%d addo=%d\n", state.
steps, state.sumOpenSize/state.
steps, state.numProcessed, state.numImproveClosed, state.numImproveOpen, state.numAddToOpen);
A simple fixed-point number class.
std::vector< u32 > moveCosts
virtual void StartRender()
Override to perform processing at the start of the overlay rendering, before the ProcessTile calls...
Declares CCmpPathfinder, whose implementation is split into multiple source files, and provides common code needed for more than one of those files.
virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass)
If the debug overlay is enabled, render the path that will computed by ComputePath.
Implementation of ICmpPathfinder.
void UpdateGrid()
Regenerates the grid based on the current obstruction list, if necessary.
Similar to Grid, except optimised for sparse usage (the grid is subdivided into buckets whose content...
void SetPred(u16 pi_, u16 pj_, u16 i, u16 j)
static fixed DistanceToGoal(CFixedVector2D pos, const CCmpPathfinder::Goal &goal)
PathfinderOverlay(CCmpPathfinder &pathfinder)
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
virtual void ResetDebugPath()
virtual void EndRender()
Override to perform processing at the end of the overlay rendering, after the ProcessTile calls...
void NearestTile(entity_pos_t x, entity_pos_t z, u16 &i, u16 &j)
Returns the tile containing the given position.
Priority queue implemented as a binary heap.
std::vector< std::vector< u32 > > m_MoveCosts
Base class for (relatively) simple drawing of data onto terrain tiles, intended for debugging purpose...
Grid< TerrainTile > * m_Grid
static void ProcessNeighbour(u16 pi, u16 pj, u16 i, u16 j, u32 pg, PathfinderState &state)
void RenderTile(const CColor &colour, bool draw_hidden)
Draw a filled quad on top of the current tile.
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Terrain overlay for pathfinder debugging.
static u32 CalculateHeuristic(u16 i, u16 j, u16 iGoal, u16 jGoal, u16 rGoal)
virtual void ProcessTile(ssize_t i, ssize_t j)
Override to perform processing of each tile.
PathfindTileGrid * m_DebugGrid
Tile data for A* computation.
void promote(ID id, R newrank)
void RenderTileOutline(const CColor &colour, int line_width, bool draw_hidden)
Draw an outlined quad on top of the current tile.
virtual void ComputePath(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass, Path &ret)
Grid< TerrainTile > * terrain
virtual void SetDebugOverlay(bool enabled)
Toggle the storage and rendering of debug info.
CCmpPathfinder & m_Pathfinder
SparseGrid< PathfindTile > PathfindTileGrid
static CFixed FromInt(int n)
#define IS_PASSABLE(item, classmask)
#define PROFILE2_ATTR
Associates a string (with printf-style formatting) with the current region or event.
PathfinderOverlay * m_DebugOverlay
enum ICmpPathfinder::Goal::Type type
static u32 CalculateCostDelta(u16 pi, u16 pj, u16 i, u16 j, PathfindTileGrid *tempGrid, u32 tileCost)
static void TileCenter(u16 i, u16 j, entity_pos_t &x, entity_pos_t &z)
Returns the position of the center of the given tile.
static bool AtGoal(u16 i, u16 j, const ICmpPathfinder::Goal &goal)
T & get(int i, int j) const
#define GET_COST_CLASS(item)
#define cassert(expr)
Compile-time assertion.
static float Length(const SVec3 v)
ICmpPathfinder::pass_class_t passClass
PriorityQueueHeap< std::pair< u16, u16 >, u32 > PriorityQueue
void push(const Item &item)
NONCOPYABLE(PathfinderOverlay)
T clamp(T value, T min, T max)
pass_class_t m_DebugPassClass