Skip to content

Commit eeef627

Browse files
committed
Integrate Coro-Roro with TaskManager
1 parent d79faa9 commit eeef627

36 files changed

+392
-288
lines changed

ext/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ CPMAddPackage(
236236
GIT_TAG 1a1824df7ac798177a521eed952720681b0bf482
237237
) # defines: magic_enum
238238

239+
CPMAddPackage(
240+
NAME Coro-Roro
241+
GITHUB_REPOSITORY zach2good/Coro-Roro
242+
GIT_TAG 310c34eb7dfb35a064181bb5119f9039be048d32
243+
OPTIONS
244+
"ENABLE_TESTS OFF"
245+
) # defines: Coro-Roro
246+
239247
set(SHARED_EXTERNAL_LIBS
240248
fmt::fmt
241249
spdlog
@@ -251,6 +259,7 @@ set(SHARED_EXTERNAL_LIBS
251259
asio
252260
alpaca
253261
magic_enum
262+
Coro-Roro
254263
)
255264

256265
if(WIN32)

src/common/task_manager.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include <queue>
3131
#include <string>
3232

33+
#include <corororo/corororo.h>
34+
using namespace CoroRoro;
35+
3336
class CTaskManager : public Singleton<CTaskManager>
3437
{
3538
public:
@@ -93,11 +96,36 @@ class CTaskManager : public Singleton<CTaskManager>
9396
return AddTask(new CTask(InitName, InitTick, InitData, InitType, InitInterval, std::forward<F>(InitFunc)));
9497
}
9598

99+
template <typename MakeTaskFn, typename Ret = decltype(std::declval<MakeTaskFn&>()())>
100+
auto AddCoroutineTask(
101+
std::string const& name,
102+
timer::time_point tick,
103+
TASKTYPE type,
104+
timer::duration interval,
105+
MakeTaskFn makeTask) -> CTask*
106+
{
107+
return AddTask(
108+
name,
109+
tick,
110+
nullptr,
111+
type,
112+
interval,
113+
[makeTask = std::move(makeTask)](timer::time_point tick, CTask* /*PTask*/) mutable -> int32
114+
{
115+
// TODO: Hook up an actual scheduler
116+
auto task = makeTask();
117+
runCoroutine(task);
118+
return 0;
119+
});
120+
}
121+
96122
auto doExpiredTasks(timer::time_point tick) -> timer::duration;
97123
void RemoveTask(std::string const& TaskName);
98124

99125
protected:
100-
CTaskManager() = default;
126+
CTaskManager()
127+
{
128+
}
101129

102130
private:
103131
TaskList_t m_TaskList;

src/map/ai/ai_container.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void CAIContainer::Reset()
412412
}
413413
}
414414

415-
void CAIContainer::Tick(timer::time_point _tick)
415+
auto CAIContainer::Tick(timer::time_point _tick) -> Task<void>
416416
{
417417
TracyZoneScoped;
418418
m_PrevTick = m_Tick;
@@ -429,7 +429,7 @@ void CAIContainer::Tick(timer::time_point _tick)
429429
bool isPathingPaused = PEntity->GetLocalVar("pauseNPCPathing");
430430
if (!Controller && CanFollowPath() && !isPathingPaused)
431431
{
432-
PathFind->FollowPath(_tick);
432+
co_await PathFind->FollowPath(_tick);
433433
if (PathFind->OnPoint())
434434
{
435435
EventHandler.triggerListener("PATH", PEntity);
@@ -439,7 +439,7 @@ void CAIContainer::Tick(timer::time_point _tick)
439439

440440
if (Controller && Controller->canUpdate)
441441
{
442-
Controller->Tick(_tick);
442+
co_await Controller->Tick(_tick);
443443
}
444444
CState* top = nullptr;
445445
while (!m_stateStack.empty() && (top = m_stateStack.top().get())->DoUpdate(_tick))
@@ -453,6 +453,8 @@ void CAIContainer::Tick(timer::time_point _tick)
453453
}
454454

455455
PEntity->PostTick();
456+
457+
co_return;
456458
}
457459

458460
bool CAIContainer::IsStateStackEmpty()

src/map/ai/ai_container.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ class CAIContainer
8181
bool Internal_Respawn(timer::duration _duration);
8282
bool Internal_Synth(SKILLTYPE synthSkill);
8383

84-
void Reset();
85-
void Tick(timer::time_point _tick);
84+
void Reset();
85+
86+
auto Tick(timer::time_point _tick) -> Task<void>;
87+
8688
CState* GetCurrentState();
8789
bool IsStateStackEmpty();
8890
void ClearStateStack();

src/map/ai/controllers/automaton_controller.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,20 @@ CurrentManeuvers CAutomatonController::GetCurrentManeuvers() const
175175
statuses->GetEffectsCount(EFFECT_LIGHT_MANEUVER), statuses->GetEffectsCount(EFFECT_DARK_MANEUVER) };
176176
}
177177

178-
void CAutomatonController::DoCombatTick(timer::time_point tick)
178+
auto CAutomatonController::DoCombatTick(timer::time_point tick) -> Task<void>
179179
{
180180
if ((PAutomaton->PMaster == nullptr || PAutomaton->PMaster->isDead()) && PAutomaton->isAlive())
181181
{
182182
PAutomaton->Die();
183-
return;
183+
co_return;
184184
}
185185

186186
PTarget = static_cast<CBattleEntity*>(PAutomaton->GetEntity(PAutomaton->GetBattleTargetID()));
187187

188188
if (TryDeaggro())
189189
{
190190
Disengage();
191-
return;
191+
co_return;
192192
}
193193

194194
// Automatons only attempt actions in 3 second intervals (Reduced by the Tactical Processor)
@@ -199,39 +199,39 @@ void CAutomatonController::DoCombatTick(timer::time_point tick)
199199
if (TryShieldBash())
200200
{
201201
m_LastShieldBashTime = m_Tick;
202-
return;
202+
co_return;
203203
}
204204
else if (TrySpellcast(maneuvers))
205205
{
206206
m_LastMagicTime = m_Tick;
207-
return;
207+
co_return;
208208
}
209209
else if (TryTPMove())
210210
{
211-
return;
211+
co_return;
212212
}
213213
else if (TryRangedAttack())
214214
{
215215
m_LastRangedTime = m_Tick;
216-
return;
216+
co_return;
217217
}
218218
else if (TryAttachment())
219219
{
220-
return;
220+
co_return;
221221
}
222222
}
223-
Move();
223+
co_await Move();
224224
}
225225

226-
void CAutomatonController::Move()
226+
auto CAutomatonController::Move() -> Task<void>
227227
{
228228
if ((shouldStandBack() && !isWithinDistance(PAutomaton->loc.p, PTarget->loc.p, 15.0f)) ||
229229
(PAutomaton->health.mp < 8 && PAutomaton->health.maxmp > 8))
230230
{
231231
PAutomaton->m_Behavior &= ~BEHAVIOR_STANDBACK;
232232
}
233233

234-
CPetController::Move();
234+
co_await CPetController::Move();
235235
}
236236

237237
bool CAutomatonController::TryAction()

src/map/ai/controllers/automaton_controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class CAutomatonController : public CPetController
6565
virtual bool Disengage() override;
6666

6767
protected:
68-
virtual void DoCombatTick(timer::time_point tick) override;
69-
virtual void Move() override;
68+
auto DoCombatTick(timer::time_point tick) -> Task<void> override;
69+
auto Move() -> Task<void> override;
7070

7171
void setCooldowns();
7272
void setMagicCooldowns();

src/map/ai/controllers/controller.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
#include "common/cbasetypes.h"
2626
#include "common/mmo.h"
27+
#include "common/task_manager.h"
2728
#include "common/timer.h"
29+
2830
#include "spell.h"
2931

3032
class CBattleEntity;
@@ -36,7 +38,9 @@ class CController
3638
virtual ~CController()
3739
{
3840
}
39-
virtual void Tick(timer::time_point tick) = 0;
41+
42+
virtual auto Tick(timer::time_point tick) -> Task<void> = 0;
43+
4044
virtual void Despawn();
4145
virtual void Reset();
4246
virtual bool Cast(uint16 targid, SpellID spellid);

0 commit comments

Comments
 (0)