Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
PathfindingManager.h
Go to the documentation of this file.
1/**
2 * @file PathfindingManager.h
3 * @brief Async pathfinding request manager for the Atomic Task System.
4 * @author Olympe Engine
5 * @date 2026-02-24
6 *
7 * @details
8 * PathfindingManager provides a simple async API for submitting pathfinding
9 * requests and polling for completion. Each request is processed on a
10 * dedicated detached std::thread that writes a straight-line path string into
11 * the result entry once the optional delay has elapsed.
12 *
13 * Usage:
14 * @code
15 * auto id = PathfindingManager::Get().Request(start, target, 0.0f);
16 * // ... later ticks ...
17 * if (PathfindingManager::Get().IsComplete(id)) {
18 * std::string path = PathfindingManager::Get().GetPathString(id);
19 * PathfindingManager::Get().Cancel(id); // release entry
20 * }
21 * @endcode
22 *
23 * Thread-safety: all public methods are guarded by m_mutex, except m_nextID
24 * which is std::atomic.
25 *
26 * C++14 compliant - no C++17/20 features.
27 */
28
29#pragma once
30
31#include <atomic>
32#include <cstdint>
33#include <mutex>
34#include <string>
35#include <unordered_map>
36
37#include "../../vector.h"
38
39namespace Olympe {
40
41/**
42 * @class PathfindingManager
43 * @brief Singleton async pathfinding request manager.
44 *
45 * @details
46 * Requests are identified by a uint64 RequestID. Each request spawns a
47 * detached thread that computes a straight-line path string and marks the
48 * entry as completed. Callers poll IsComplete() and retrieve the result via
49 * GetPathString(), then call Cancel() to release the entry.
50 */
52public:
53
54 /// Unique identifier for a pathfinding request.
56
57 /// Sentinel value for an invalid / unsubmitted request.
58 static constexpr RequestID INVALID_REQUEST_ID = 0u;
59
60 // -----------------------------------------------------------------------
61 // Singleton access
62 // -----------------------------------------------------------------------
63
64 /**
65 * @brief Returns the singleton PathfindingManager instance.
66 */
67 static PathfindingManager& Get();
68
69 // -----------------------------------------------------------------------
70 // Public API
71 // -----------------------------------------------------------------------
72
73 /**
74 * @brief Submits an async pathfinding request.
75 *
76 * Launches a detached thread that sleeps for @p delaySeconds (simulating
77 * pathfinding work) and then writes a straight-line path string into the
78 * result entry unless the request has been cancelled.
79 *
80 * @param start Start position.
81 * @param target Target position.
82 * @param delaySeconds Simulated computation delay in seconds (0.0 = instant).
83 * @return A unique RequestID identifying this request.
84 */
85 RequestID Request(const ::Vector& start,
86 const ::Vector& target,
87 float delaySeconds = 0.0f);
88
89 /**
90 * @brief Returns true if the request identified by @p id has completed.
91 *
92 * Returns false for unknown or cancelled requests.
93 *
94 * @param id RequestID returned by Request().
95 */
96 bool IsComplete(RequestID id);
97
98 /**
99 * @brief Returns the path string computed for @p id.
100 *
101 * Returns an empty string if the request is not complete or unknown.
102 *
103 * @param id RequestID returned by Request().
104 */
105 std::string GetPathString(RequestID id);
106
107 /**
108 * @brief Cancels a pending request and removes its entry.
109 *
110 * If the request is still in-flight the worker thread will see the
111 * cancelled flag and skip writing the result. The entry is removed from
112 * the map immediately so subsequent calls to IsComplete()/GetPathString()
113 * return the default empty/false values.
114 *
115 * Safe to call on an already-completed request (releases the entry).
116 *
117 * @param id RequestID returned by Request().
118 */
119 void Cancel(RequestID id);
120
121private:
122
123 // -----------------------------------------------------------------------
124 // Internal types
125 // -----------------------------------------------------------------------
126
130 std::string result;
131 bool completed = false;
132 bool cancelled = false;
133 };
134
135 // -----------------------------------------------------------------------
136 // State
137 // -----------------------------------------------------------------------
138
139 std::mutex m_mutex;
140 std::unordered_map<RequestID, RequestEntry> m_requests;
141 std::atomic<uint64_t> m_nextID;
142
143 // -----------------------------------------------------------------------
144 // Construction (private - singleton only)
145 // -----------------------------------------------------------------------
146
149
150 // Non-copyable, non-movable
153};
154
155} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Singleton async pathfinding request manager.
static constexpr RequestID INVALID_REQUEST_ID
Sentinel value for an invalid / unsubmitted request.
static PathfindingManager & Get()
Returns the singleton PathfindingManager instance.
uint64_t RequestID
Unique identifier for a pathfinding request.
void Cancel(RequestID id)
Cancels a pending request and removes its entry.
std::atomic< uint64_t > m_nextID
RequestID Request(const ::Vector &start, const ::Vector &target, float delaySeconds=0.0f)
Submits an async pathfinding request.
std::unordered_map< RequestID, RequestEntry > m_requests
PathfindingManager(const PathfindingManager &)=delete
bool IsComplete(RequestID id)
Returns true if the request identified by id has completed.
PathfindingManager & operator=(const PathfindingManager &)=delete
std::string GetPathString(RequestID id)
Returns the path string computed for id.
< Provides AssetID and INVALID_ASSET_ID