Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpPathfinder_Common.h
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
#define PATHFIND_DEBUG 1 | #define PATHFIND_DEBUG 1 | ||||
#endif | #endif | ||||
/** | /** | ||||
* Implementation of ICmpPathfinder | * Implementation of ICmpPathfinder | ||||
*/ | */ | ||||
class CCmpPathfinder final : public ICmpPathfinder | class CCmpPathfinder final : public ICmpPathfinder | ||||
{ | { | ||||
protected: | |||||
class PathfinderWorker | |||||
{ | |||||
friend CCmpPathfinder; | |||||
public: | |||||
PathfinderWorker(); | |||||
// Process path requests, checking if we should stop before each new one. | |||||
void Work(const CCmpPathfinder& pathfinder); | |||||
private: | |||||
// Insert requests in m_[Long/Short]Requests depending on from. | |||||
// This could be removed when we may use if-constexpr in CCmpPathfinder::PushRequestsToWorkers | |||||
template<typename T> | |||||
void PushRequests(std::vector<T>& from, ssize_t amount); | |||||
// Stores our results, the main thread will fetch this. | |||||
std::vector<PathResult> m_Results; | |||||
std::vector<LongPathRequest> m_LongRequests; | |||||
std::vector<ShortPathRequest> m_ShortRequests; | |||||
}; | |||||
// Allow the workers to access our private variables | |||||
friend class PathfinderWorker; | |||||
public: | public: | ||||
static void ClassInit(CComponentManager& componentManager) | static void ClassInit(CComponentManager& componentManager) | ||||
{ | { | ||||
componentManager.SubscribeToMessageType(MT_Update); | componentManager.SubscribeToMessageType(MT_Update); | ||||
componentManager.SubscribeToMessageType(MT_RenderSubmit); // for debug overlays | componentManager.SubscribeToMessageType(MT_RenderSubmit); // for debug overlays | ||||
componentManager.SubscribeToMessageType(MT_TerrainChanged); | componentManager.SubscribeToMessageType(MT_TerrainChanged); | ||||
componentManager.SubscribeToMessageType(MT_WaterChanged); | componentManager.SubscribeToMessageType(MT_WaterChanged); | ||||
componentManager.SubscribeToMessageType(MT_ObstructionMapShapeChanged); | componentManager.SubscribeToMessageType(MT_ObstructionMapShapeChanged); | ||||
componentManager.SubscribeToMessageType(MT_TurnStart); | componentManager.SubscribeToMessageType(MT_TurnStart); | ||||
} | } | ||||
~CCmpPathfinder(); | ~CCmpPathfinder(); | ||||
DEFAULT_COMPONENT_ALLOCATOR(Pathfinder) | DEFAULT_COMPONENT_ALLOCATOR(Pathfinder) | ||||
// Template state: | // Template state: | ||||
std::map<std::string, pass_class_t> m_PassClassMasks; | std::map<std::string, pass_class_t> m_PassClassMasks; | ||||
std::vector<PathfinderPassability> m_PassClasses; | std::vector<PathfinderPassability> m_PassClasses; | ||||
// Dynamic state: | // Dynamic state: | ||||
std::vector<LongPathRequest> m_LongPathRequests; | std::vector<LongPathRequest> m_LongPathRequests; | ||||
std::vector<ShortPathRequest> m_ShortPathRequests; | std::vector<ShortPathRequest> m_ShortPathRequests; | ||||
u32 m_NextAsyncTicket; // unique IDs for asynchronous path requests | u32 m_NextAsyncTicket; // Unique IDs for asynchronous path requests. | ||||
u16 m_SameTurnMovesCount; // current number of same turn moves we have processed this turn | u16 m_MaxSameTurnMoves; // Compute only this many paths when useMax is true in StartProcessingMoves. | ||||
// Lazily-constructed dynamic state (not serialized): | // Lazily-constructed dynamic state (not serialized): | ||||
u16 m_MapSize; // tiles per side | u16 m_MapSize; // tiles per side | ||||
Grid<NavcellData>* m_Grid; // terrain/passability information | Grid<NavcellData>* m_Grid; // terrain/passability information | ||||
Grid<NavcellData>* m_TerrainOnlyGrid; // same as m_Grid, but only with terrain, to avoid some recomputations | Grid<NavcellData>* m_TerrainOnlyGrid; // same as m_Grid, but only with terrain, to avoid some recomputations | ||||
// Keep clever updates in memory to avoid memory fragmentation from the grid. | // Keep clever updates in memory to avoid memory fragmentation from the grid. | ||||
// This should be used only in UpdateGrid(), there is no guarantee the data is properly initialized anywhere else. | // This should be used only in UpdateGrid(), there is no guarantee the data is properly initialized anywhere else. | ||||
GridUpdateInformation m_DirtinessInformation; | GridUpdateInformation m_DirtinessInformation; | ||||
// The data from clever updates is stored for the AI manager | // The data from clever updates is stored for the AI manager | ||||
GridUpdateInformation m_AIPathfinderDirtinessInformation; | GridUpdateInformation m_AIPathfinderDirtinessInformation; | ||||
bool m_TerrainDirty; | bool m_TerrainDirty; | ||||
std::unique_ptr<VertexPathfinder> m_VertexPathfinder; | std::unique_ptr<VertexPathfinder> m_VertexPathfinder; | ||||
std::unique_ptr<HierarchicalPathfinder> m_PathfinderHier; | std::unique_ptr<HierarchicalPathfinder> m_PathfinderHier; | ||||
std::unique_ptr<LongPathfinder> m_LongPathfinder; | std::unique_ptr<LongPathfinder> m_LongPathfinder; | ||||
// For responsiveness we will process some moves in the same turn they were generated in | // Workers process pathing requests. | ||||
std::vector<PathfinderWorker> m_Workers; | |||||
u16 m_MaxSameTurnMoves; // max number of moves that can be created and processed in the same turn | |||||
AtlasOverlay* m_AtlasOverlay; | AtlasOverlay* m_AtlasOverlay; | ||||
static std::string GetSchema() | static std::string GetSchema() | ||||
{ | { | ||||
return "<a:component type='system'/><empty/>"; | return "<a:component type='system'/><empty/>"; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | public: | ||||
virtual void FlushAIPathfinderDirtinessInformation() | virtual void FlushAIPathfinderDirtinessInformation() | ||||
{ | { | ||||
m_AIPathfinderDirtinessInformation.Clean(); | m_AIPathfinderDirtinessInformation.Clean(); | ||||
} | } | ||||
virtual Grid<u16> ComputeShoreGrid(bool expandOnWater = false); | virtual Grid<u16> ComputeShoreGrid(bool expandOnWater = false); | ||||
virtual void ComputePath(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass, WaypointPath& ret) const; | virtual void ComputePathImmediate(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass, WaypointPath& ret) const; | ||||
virtual u32 ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass, entity_id_t notify); | virtual u32 ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass, entity_id_t notify); | ||||
virtual WaypointPath ComputeShortPath(const ShortPathRequest& request) const; | virtual WaypointPath ComputeShortPathImmediate(const ShortPathRequest& request) const; | ||||
virtual u32 ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t clearance, entity_pos_t range, const PathGoal& goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t controller, entity_id_t notify); | virtual u32 ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t clearance, entity_pos_t range, const PathGoal& goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t controller, entity_id_t notify); | ||||
virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass); | virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass); | ||||
virtual void SetDebugOverlay(bool enabled); | virtual void SetDebugOverlay(bool enabled); | ||||
virtual void SetHierDebugOverlay(bool enabled); | virtual void SetHierDebugOverlay(bool enabled); | ||||
virtual void GetDebugData(u32& steps, double& time, Grid<u8>& grid) const; | virtual void GetDebugData(u32& steps, double& time, Grid<u8>& grid) const; | ||||
virtual void SetAtlasOverlay(bool enable, pass_class_t passClass = 0); | virtual void SetAtlasOverlay(bool enable, pass_class_t passClass = 0); | ||||
virtual bool CheckMovement(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, pass_class_t passClass) const; | virtual bool CheckMovement(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, pass_class_t passClass) const; | ||||
virtual ICmpObstruction::EFoundationCheck CheckUnitPlacement(const IObstructionTestFilter& filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, pass_class_t passClass, bool onlyCenterPoint) const; | virtual ICmpObstruction::EFoundationCheck CheckUnitPlacement(const IObstructionTestFilter& filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, pass_class_t passClass, bool onlyCenterPoint) const; | ||||
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) const; | 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) const; | ||||
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, bool onlyCenterPoint) const; | 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, bool onlyCenterPoint) const; | ||||
virtual void FinishAsyncRequests(); | virtual void FetchAsyncResultsAndSendMessages(); | ||||
void ProcessLongRequests(const std::vector<LongPathRequest>& longRequests); | virtual void StartProcessingMoves(bool useMax); | ||||
void ProcessShortRequests(const std::vector<ShortPathRequest>& shortRequests); | template <typename T> | ||||
std::vector<T> PopMovesToProcess(std::vector<T>& requests, bool useMax = false, size_t maxMoves = 0); | |||||
virtual void ProcessSameTurnMoves(); | template <typename T> | ||||
void PushRequestsToWorkers(std::vector<T>& from); | |||||
/** | /** | ||||
* Regenerates the grid based on the current obstruction list, if necessary | * Regenerates the grid based on the current obstruction list, if necessary | ||||
*/ | */ | ||||
virtual void UpdateGrid(); | virtual void UpdateGrid(); | ||||
/** | /** | ||||
* Updates the terrain-only grid without updating the dirtiness informations. | * Updates the terrain-only grid without updating the dirtiness informations. | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator