23 #include "precompiled.h"
49 m_ObstructionGrid = NULL;
50 m_TerrainDirty =
true;
51 m_NextAsyncTicket = 1;
53 m_DebugOverlay = NULL;
57 m_SameTurnMovesCount = 0;
81 m_MaxSameTurnMoves = (
u16)pathingSettings.
GetChild(
"MaxSameTurnMoves").
ToInt();
85 for (CParamNode::ChildrenMap::const_iterator it = passClasses.begin(); it != passClasses.end(); ++it)
87 std::string name = it->first;
89 pass_class_t mask = (pass_class_t)(1u << (m_PassClasses.size() + 2));
91 m_PassClassMasks[name] = mask;
99 std::set<std::string> unitClassNames;
100 unitClassNames.insert(
"default");
104 for (CParamNode::ChildrenMap::const_iterator it = moveClasses.begin(); it != moveClasses.end(); ++it)
106 std::string terrainClassName = it->first;
107 m_TerrainCostClassTags[terrainClassName] = (cost_class_t)i;
111 for (CParamNode::ChildrenMap::const_iterator uit = unitClasses.begin(); uit != unitClasses.end(); ++uit)
112 unitClassNames.insert(uit->first);
120 for (std::set<std::string>::const_iterator nit = unitClassNames.begin(); nit != unitClassNames.end(); ++nit)
122 m_UnitCostClassTags[*nit] = (cost_class_t)i;
125 std::vector<u32> costs;
126 std::vector<fixed> speeds;
128 for (CParamNode::ChildrenMap::const_iterator it = moveClasses.begin(); it != moveClasses.end(); ++it)
131 fixed cost = it->second.GetChild(
"@Cost").ToFixed();
132 fixed speed = it->second.GetChild(
"@Speed").ToFixed();
135 if (unitClass.
IsOk())
141 speeds.push_back(speed);
144 m_MoveCosts.push_back(costs);
145 m_MoveSpeeds.push_back(speeds);
164 serialize.NumberU32_Unbounded(
"ticket", value.
ticket);
165 serialize.NumberFixed_Unbounded(
"x0", value.
x0);
166 serialize.NumberFixed_Unbounded(
"z0", value.
z0);
168 serialize.NumberU16_Unbounded(
"pass class", value.
passClass);
169 serialize.NumberU8_Unbounded(
"cost class", value.
costClass);
170 serialize.NumberU32_Unbounded(
"notify", value.
notify);
179 serialize.NumberU32_Unbounded(
"ticket", value.
ticket);
180 serialize.NumberFixed_Unbounded(
"x0", value.
x0);
181 serialize.NumberFixed_Unbounded(
"z0", value.
z0);
182 serialize.NumberFixed_Unbounded(
"r", value.
r);
183 serialize.NumberFixed_Unbounded(
"range", value.
range);
185 serialize.NumberU16_Unbounded(
"pass class", value.
passClass);
187 serialize.NumberU32_Unbounded(
"group", value.
group);
188 serialize.NumberU32_Unbounded(
"notify", value.
notify);
255 LOGERROR(L
"Invalid passability class name '%hs'", name.c_str());
271 LOGERROR(L
"Invalid unit cost class name '%hs'", name.c_str());
335 PROFILE(
"UpdateGrid obstructions");
381 const u16 shoreMax = 32767;
393 waterGrid.
set(i, j, underWater);
397 Grid<u16> shoreGrid(m_MapSize, m_MapSize);
403 if (!waterGrid.
get(i, j))
405 if ((i > 0 && waterGrid.
get(i-1, j)) || (i > 0 && j < m_MapSize-1 && waterGrid.
get(i-1, j+1)) || (i > 0 && j > 0 && waterGrid.
get(i-1, j-1))
406 || (i < m_MapSize-1 && waterGrid.
get(i+1, j)) || (i < m_MapSize-1 && j < m_MapSize-1 && waterGrid.
get(i+1, j+1)) || (i < m_MapSize-1 && j > 0 && waterGrid.
get(i+1, j-1))
407 || (j > 0 && waterGrid.
get(i, j-1)) || (j < m_MapSize-1 && waterGrid.
get(i, j+1))
410 shoreGrid.
set(i, j, 0);
414 shoreGrid.
set(i, j, shoreMax);
426 if (!waterGrid.
get(x, y))
428 u16 g = shoreGrid.
get(x, y);
430 shoreGrid.
set(x, y, min);
437 for (
u16 x = m_MapSize; x > 0; --x)
439 if (!waterGrid.
get(x-1, y))
441 u16 g = shoreGrid.
get(x-1, y);
443 shoreGrid.
set(x-1, y, min);
456 if (!waterGrid.
get(x, y))
458 u16 g = shoreGrid.
get(x, y);
460 shoreGrid.
set(x, y, min);
467 for (
u16 y = m_MapSize; y > 0; --y)
469 if (!waterGrid.
get(x, y-1))
471 u16 g = shoreGrid.
get(x, y-1);
473 shoreGrid.
set(x, y-1, min);
500 fixed depth = water - height;
562 std::vector<AsyncLongPathRequest> longRequests;
565 std::vector<AsyncShortPathRequest> shortRequests;
579 for (
size_t i = 0; i < longRequests.size(); ++i)
591 for (
size_t i = 0; i < shortRequests.size(); ++i)
613 std::vector<AsyncLongPathRequest> longRequests;
617 moveCount = (
i32)longRequests.size();
621 longRequests.resize(moveCount);
628 m_SameTurnMovesCount = (
u16)(m_SameTurnMovesCount + moveCount);
640 std::vector<AsyncShortPathRequest> shortRequests;
644 moveCount = (
i32)shortRequests.size();
648 shortRequests.resize(moveCount);
655 m_SameTurnMovesCount = (
u16)(m_SameTurnMovesCount + moveCount);
670 if (!cmpObstructionManager)
673 if (cmpObstructionManager->
TestUnitShape(filter, x, z, r, NULL))
694 for (
u16 j = j0; j <= j1; ++j)
696 for (
u16 i = i0; i <= i1; ++i)
721 if (!cmpObstructionManager)
724 if (cmpObstructionManager->
TestStaticShape(filter, x, z, a, w, h, NULL))
755 for (
u16 j = j0; j <= j1; ++j)
757 for (
u16 i = i0; i <= i1; ++i)
CStr8 GetMovementClass(ssize_t i, ssize_t j) const
An entity initialisation parameter node.
std::vector< SOverlayLine > m_DebugOverlayShortPathLines
virtual bool TestStaticShape(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h, std::vector< entity_id_t > *out)=0
Collision test a static square shape against the current set of shapes.
A simple fixed-point number class.
ICmpPathfinder::pass_class_t passClass
Interface for ICmpObstructionManager Test functions to filter out unwanted shapes.
Declares CCmpPathfinder, whose implementation is split into multiple source files, and provides common code needed for more than one of those files.
virtual entity_pos_t GetWaterLevel(entity_pos_t x, entity_pos_t z)=0
Get the current water level at the given point.
#define REGISTER_COMPONENT_TYPE(cname)
Implementation of ICmpPathfinder.
void UpdateGrid()
Regenerates the grid based on the current obstruction list, if necessary.
Helper templates for serializing/deserializing common objects.
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
virtual void HandleMessage(const CMessage &msg, bool global)
static fixed DistanceToGoal(CFixedVector2D pos, const CCmpPathfinder::Goal &goal)
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
virtual void ResetDebugPath()
virtual bool TestUnitShape(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, std::vector< entity_id_t > *out)=0
Collision test a unit shape against the current set of registered shapes, and optionally writes a lis...
std::vector< PathfinderPassability > m_PassClasses
std::map< std::string, cost_class_t > m_TerrainCostClassTags
bool IsOk() const
Returns true if this is a valid CParamNode, false if it represents a non-existent node...
virtual fixed GetMovementSpeed(entity_pos_t x0, entity_pos_t z0, cost_class_t costClass)
Find the speed factor (typically around 1.0) for a unit of the given cost class at the given position...
Serialization interface; see serialization overview.
virtual void ProcessSameTurnMoves()
Process moves during the same turn they were created in to improve responsiveness.
Obstruction test filter that reject shapes in a given control group, and rejects shapes that don't bl...
Add renderable objects to the scene collector.
virtual std::map< std::string, pass_class_t > GetPassabilityClasses()
Get the list of all known passability classes.
virtual ICmpObstruction::EFoundationCheck CheckBuildingPlacement(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h, entity_id_t id, pass_class_t passClass)
Check whether a building placed here is valid and doesn't hit any obstructions or impassable terrain...
void NearestTile(entity_pos_t x, entity_pos_t z, u16 &i, u16 &j)
Returns the tile containing the given position.
virtual bool GetObstructionSquare(ICmpObstructionManager::ObstructionSquare &out)=0
Gets the square corresponding to this obstruction shape.
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
int ToInt() const
Parses the content of this node as an integer.
Grid< u8 > * m_ObstructionGrid
const ChildrenMap & GetChildren() const
Returns the names/nodes of the children of this node, ordered by name.
void operator()(S &serialize, const char *name, AsyncShortPathRequest &value)
Grid< TerrainTile > * m_Grid
#define COST_CLASS_MASK(id)
void RenderSubmit(SceneCollector &collector)
ICmpPathfinder::pass_class_t passClass
std::vector< AsyncShortPathRequest > m_AsyncShortPathRequests
This interface accepts renderable objects.
fixed ToFixed() const
Parses the content of this node as a fixed-point number.
static void LoadXML(CParamNode &ret, const XMBFile &file, const wchar_t *sourceIdentifier=NULL)
Loads the XML data specified by file into the node ret.
#define IS_TERRAIN_PASSABLE(item, classmask)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
virtual int GetType() const =0
const CParamNode & GetChild(const char *name) const
Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none...
fixed GetSlopeFixed(ssize_t i, ssize_t j) const
virtual void Serialize(ISerializer &serialize)
virtual u32 ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass, entity_id_t notify)
Asynchronous version of ComputePath.
std::vector< std::vector< fixed > > m_MoveSpeeds
void ProcessLongRequests(const std::vector< AsyncLongPathRequest > &longRequests)
CFixed Multiply(CFixed n) const
Multiply by a CFixed.
std::map< std::string, cost_class_t > m_UnitCostClassTags
#define SAFE_DELETE(p)
delete memory ensuing from new and set the pointer to zero (thus making double-frees safe / a no-op) ...
std::vector< AsyncLongPathRequest > m_AsyncLongPathRequests
virtual void ComputePath(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass, Path &ret)
ICmpPathfinder::Goal goal
const int DEFAULT_MOVE_COST
virtual void SetDebugOverlay(bool enabled)
Toggle the storage and rendering of debug info.
const CSimContext & GetSimContext() const
virtual pass_class_t GetPassabilityClass(const std::string &name)
Get the tag for a given passability class name.
std::map< std::string, pass_class_t > m_PassClassMasks
void NumberU32_Unbounded(const char *name, uint32_t value)
Serialize a number.
A simplified syntax for accessing entity components.
fixed GetExactGroundLevelFixed(fixed x, fixed z) const
virtual void Init(const CParamNode ¶mNode)
static CFixed FromInt(int n)
CEntityHandle GetSystemEntity() const
virtual u32 ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t r, entity_pos_t range, const Goal &goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t controller, entity_id_t notify)
Asynchronous version of ComputeShortPath (using ControlGroupObstructionFilter).
void PostMessage(entity_id_t ent, const CMessage &msg) const
Send a message, targeted at a particular entity.
ICmpPathfinder::Goal goal
enum ICmpPathfinder::Goal::Type type
ICmpPathfinder::cost_class_t costClass
virtual ICmpObstruction::EFoundationCheck CheckUnitPlacement(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, pass_class_t passClass)
Check whether a unit placed here is valid and doesn't hit any obstructions or impassable terrain...
Sent by CCmpPathfinder after async path requests.
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.
SceneCollector & collector
void ProcessShortRequests(const std::vector< AsyncShortPathRequest > &shortRequests)
T & get(int i, int j) const
virtual bool Rasterise(Grid< u8 > &grid)=0
Convert the current set of shapes onto a grid.
#define GET_COST_CLASS(item)
void operator()(S &serialize, const char *name, AsyncLongPathRequest &value)
#define debug_warn(expr)
display the error dialog with the given text.
fixed DistanceToSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize)
virtual u16 GetTilesPerSide()=0
Returns number of tiles per side on the terrain.
bool PointIsInSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize)
Checks if a point is inside the given rotated square or rectangle.
virtual cost_class_t GetCostClass(const std::string &name)
Get the tag for a given movement cost class name.
void NumberU16_Unbounded(const char *name, uint16_t value)
Serialize a number.
u32 entity_id_t
Entity ID type.
virtual void Submit(CPatch *patch)=0
Submit a terrain patch that is part of the scene.
virtual const Grid< u16 > & GetPassabilityGrid()
const int PASS_CLASS_BITS
std::map< std::string, CParamNode > ChildrenMap
virtual void ComputeShortPath(const IObstructionTestFilter &filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t r, entity_pos_t range, const Goal &goal, pass_class_t passClass, Path &ret)
Standard representation for all types of shapes, for use with geometry processing code...
virtual void NumberU16_Unbounded(const char *name, uint16_t &out)
void set(int i, int j, const T &value)
CComponentManager & GetComponentManager() const
CTerrain & GetTerrain() const
CFixedVector2D GetHalfBoundingBox(CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize)
virtual void FinishAsyncRequests()
Finish computing asynchronous path requests and send the CMessagePathResult messages.
Deserialization interface; see serialization overview.
virtual void Deserialize(const CParamNode ¶mNode, IDeserializer &deserialize)