Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Task_RequestPathfinding.cpp
Go to the documentation of this file.
1/**
2 * @file Task_RequestPathfinding.cpp
3 * @brief Atomic task: async path request via PathfindingManager.
4 * @author Olympe Engine
5 * @date 2026-02-24
6 *
7 * @details
8 * On the first ExecuteWithContext() call the task reads "Position" from the
9 * LocalBlackboard and the "Target" parameter, then submits an async request
10 * to PathfindingManager and returns Running. On subsequent ticks it polls
11 * IsComplete(); when the path is ready the string is written to the "Path"
12 * LocalBlackboard key and the task returns Success.
13 *
14 * C++14 compliant - no C++17/20 features.
15 */
16
18#include "../../AtomicTaskRegistry.h"
19#include "../../LocalBlackboard.h"
20#include "../../../system/system_utils.h"
21
22namespace Olympe {
23
24static const char* BB_KEY_POSITION = "Position";
25static const char* BB_KEY_PATH = "Path";
26
27// ---------------------------------------------------------------------------
28// Construction / Abort
29// ---------------------------------------------------------------------------
30
32 : m_requestID(PathfindingManager::INVALID_REQUEST_ID)
33 , m_hasRequest(false)
34{
35}
36
38{
40 {
41 SYSTEM_LOG << "[Task_RequestPathfinding] Abort() - cancelling request "
42 << m_requestID << "\n";
44 }
46 m_hasRequest = false;
47}
48
49// ---------------------------------------------------------------------------
50// Execute (legacy fallback)
51// ---------------------------------------------------------------------------
52
54{
55 return TaskStatus::Failure; // requires context; use ExecuteWithContext
56}
57
58// ---------------------------------------------------------------------------
59// ExecuteWithContext
60// ---------------------------------------------------------------------------
61
63 const ParameterMap& params)
64{
65 if (!m_hasRequest)
66 {
67 // --- First tick: resolve parameters and submit async request ---
68
69 // Read current position from LocalBlackboard.
70 if (!ctx.LocalBB || !ctx.LocalBB->HasVariable(BB_KEY_POSITION))
71 {
72 SYSTEM_LOG << "[Task_RequestPathfinding] 'Position' key not found in LocalBlackboard\n";
74 }
75
76 ::Vector start(0.0f, 0.0f, 0.0f);
77 try
78 {
79 start = ctx.LocalBB->GetValue(BB_KEY_POSITION).AsVector();
80 }
81 catch (const std::exception& e)
82 {
83 SYSTEM_LOG << "[Task_RequestPathfinding] Failed to read 'Position': " << e.what() << "\n";
85 }
86
87 // Resolve Target parameter.
88 ::Vector target(0.0f, 0.0f, 0.0f);
89 {
90 auto it = params.find("Target");
91 if (it == params.end() || it->second.GetType() != VariableType::Vector)
92 {
93 SYSTEM_LOG << "[Task_RequestPathfinding] Missing or invalid 'Target' parameter\n";
95 }
96 target = it->second.AsVector();
97 }
98
99 // Resolve optional AsyncDelay parameter.
100 float asyncDelay = 0.0f;
101 {
102 auto it = params.find("AsyncDelay");
103 if (it != params.end() && it->second.GetType() == VariableType::Float)
104 {
105 asyncDelay = it->second.AsFloat();
106 if (asyncDelay < 0.0f) asyncDelay = 0.0f;
107 }
108 }
109
110 // Ensure "Path" key exists in LocalBlackboard before writing later.
111 if (!ctx.LocalBB->HasVariable(BB_KEY_PATH))
112 {
113 SYSTEM_LOG << "[Task_RequestPathfinding] 'Path' key not found in LocalBlackboard\n";
114 return TaskStatus::Failure;
115 }
116
117 // Submit async request.
119 m_hasRequest = true;
120
121 SYSTEM_LOG << "[Task_RequestPathfinding] Entity " << ctx.Entity
122 << " submitted request " << m_requestID
123 << " from (" << start.x << "," << start.y << ")"
124 << " to (" << target.x << "," << target.y << ")\n";
125
126 return TaskStatus::Running;
127 }
128
129 // --- Subsequent ticks: poll for completion ---
130
131 if (!PathfindingManager::Get().IsComplete(m_requestID))
132 {
133 SYSTEM_LOG << "[Task_RequestPathfinding] Entity " << ctx.Entity
134 << " waiting for request " << m_requestID << "\n";
135 return TaskStatus::Running;
136 }
137
138 // Request completed - retrieve path string.
140 PathfindingManager::Get().Cancel(m_requestID); // release entry
141
143 m_hasRequest = false;
144
145 // Write path to LocalBlackboard.
146 try
147 {
148 ctx.LocalBB->SetValue(BB_KEY_PATH, TaskValue(path));
149 }
150 catch (const std::exception& e)
151 {
152 SYSTEM_LOG << "[Task_RequestPathfinding] Failed to write 'Path': " << e.what() << "\n";
153 return TaskStatus::Failure;
154 }
155
156 SYSTEM_LOG << "[Task_RequestPathfinding] Entity " << ctx.Entity
157 << " path ready: " << path << " - Success\n";
158
159 return TaskStatus::Success;
160}
161
162REGISTER_ATOMIC_TASK(Task_RequestPathfinding, "Task_RequestPathfinding")
163
164} // namespace Olympe
#define REGISTER_ATOMIC_TASK(ClassName, Id)
Registers a factory for ClassName under Id at static init time.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Atomic task that asynchronously requests a path via PathfindingManager.
std::unordered_map< std::string, TaskValue > ParameterMap
Convenience alias for the parameter map passed to Execute().
Definition IAtomicTask.h:67
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.
void Cancel(RequestID id)
Cancels a pending request and removes its entry.
RequestID Request(const ::Vector &start, const ::Vector &target, float delaySeconds=0.0f)
Submits an async pathfinding request.
std::string GetPathString(RequestID id)
Returns the path string computed for id.
C++14-compliant type-safe value container for task parameters.
Async pathfinding task that polls PathfindingManager for completion.
void Abort() override
Aborts the task, releasing any in-progress state.
TaskStatus Execute(const ParameterMap &params) override
Executes the atomic task for one frame.
TaskStatus ExecuteWithContext(const AtomicTaskContext &ctx, const ParameterMap &params) override
Executes the atomic task for one frame with full runtime context.
PathfindingManager::RequestID m_requestID
Active request ID (0 = none)
bool m_hasRequest
True after first-tick submission.
float y
Definition vector.h:25
float x
Definition vector.h:25
< Provides AssetID and INVALID_ASSET_ID
@ Float
Single-precision float.
@ Vector
3-component vector (Vector from vector.h)
TaskStatus
Result code returned by IAtomicTask::Execute().
Definition IAtomicTask.h:38
@ Success
Task completed successfully.
@ Running
Task is still in progress (multi-frame tasks)
@ Failure
Task failed.
static const char * BB_KEY_POSITION
static const char * BB_KEY_PATH
Lightweight context bundle passed to IAtomicTask::ExecuteWithContext().
#define SYSTEM_LOG