Skip to content

Commit 49937a5

Browse files
committed
Move state-owned timed events to generic processor list
1 parent 121958f commit 49937a5

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

ElunaEventMgr.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ extern "C"
2020

2121
ElunaEventProcessor::ElunaEventProcessor(Eluna* _E, WorldObject* _obj) : m_time(0), obj(_obj), E(_E)
2222
{
23-
if (obj)
23+
if (E->eventMgr)
2424
E->eventMgr->processors.insert(this);
2525
}
2626

2727
ElunaEventProcessor::~ElunaEventProcessor()
2828
{
29-
{
30-
RemoveEvents_internal();
31-
}
29+
RemoveEvents_internal();
3230

33-
if (obj)
31+
if (E->eventMgr)
3432
E->eventMgr->processors.erase(this);
3533
}
3634

@@ -187,43 +185,45 @@ void ElunaEventProcessor::ProcessDeferredOps()
187185

188186
EventMgr::EventMgr(Eluna* _E) : E(_E)
189187
{
190-
globalProcessor = std::make_unique<ElunaEventProcessor>(E, nullptr);
188+
auto gp = std::make_unique<ElunaEventProcessor>(E, nullptr);
189+
processors.insert(gp.get());
190+
globalProcessors.emplace(GLOBAL_EVENTS, std::move(gp));
191191
}
192192

193193
EventMgr::~EventMgr()
194194
{
195195
for (auto* processor : processors)
196196
processor->RemoveEvents_internal();
197197

198-
globalProcessor->RemoveEvents_internal();
198+
globalProcessors.clear();
199199
}
200200

201201
void EventMgr::UpdateProcessors(uint32 diff)
202202
{
203-
if (!processors.empty())
204-
{
205-
// iterate a copy because processors may be destroyed during update (creature removed by a script, etc)
206-
ProcessorSet copy = processors;
203+
// iterate a copy because processors may be destroyed during update (creature removed by a script, etc)
204+
ProcessorSet copy = processors;
207205

208-
for (auto* processor : copy)
206+
for (auto* processor : copy)
207+
{
208+
if (processors.find(processor) != processors.end())
209209
processor->Update(diff);
210210
}
211-
212-
globalProcessor->Update(diff);
213211
}
214212

215213
void EventMgr::SetStates(LuaEventState state)
216214
{
217215
for (auto* processor : processors)
218216
processor->SetStates(state);
219-
220-
globalProcessor->SetStates(state);
221217
}
222218

223219
void EventMgr::SetState(int eventId, LuaEventState state)
224220
{
225221
for (auto* processor : processors)
226222
processor->SetState(eventId, state);
223+
}
227224

228-
globalProcessor->SetState(eventId, state);
225+
ElunaEventProcessor* EventMgr::GetGlobalProcessor(GlobalEventSpace space)
226+
{
227+
auto it = globalProcessors.find(space);
228+
return (it != globalProcessors.end()) ? it->second.get() : nullptr;
229229
}

ElunaEventMgr.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ enum LuaEventState
3636
LUAEVENT_STATE_ERASE, // On next call just erases the data
3737
};
3838

39+
enum GlobalEventSpace
40+
{
41+
GLOBAL_EVENTS
42+
};
43+
3944
struct LuaEvent
4045
{
4146
LuaEvent(int _funcRef, uint32 _min, uint32 _max, uint32 _repeats) :
@@ -116,23 +121,24 @@ class ElunaEventProcessor
116121
class EventMgr
117122
{
118123
public:
119-
typedef std::unordered_set<ElunaEventProcessor*> ProcessorSet;
120-
ProcessorSet processors;
121-
std::unique_ptr<ElunaEventProcessor> globalProcessor;
122-
Eluna* E;
123-
124124
EventMgr(Eluna* _E);
125125
~EventMgr();
126126

127-
// Set the state of all timed events
128-
// Execute only in safe env
127+
void UpdateProcessors(uint32 diff);
129128
void SetStates(LuaEventState state);
130-
131-
// Sets the eventId's state in all processors
132-
// Execute only in safe env
133129
void SetState(int eventId, LuaEventState state);
134130

135-
void UpdateProcessors(uint32 diff);
131+
ElunaEventProcessor* GetGlobalProcessor(GlobalEventSpace space);
132+
133+
private:
134+
typedef std::unordered_set<ElunaEventProcessor*> ProcessorSet;
135+
136+
ProcessorSet processors; // tracks ALL processors (object + global)
137+
std::unordered_map<GlobalEventSpace, std::unique_ptr<ElunaEventProcessor>> globalProcessors;
138+
139+
Eluna* E;
140+
141+
friend class ElunaEventProcessor;
136142
};
137143

138144
#endif

methods/TrinityCore/GlobalMethods.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ namespace LuaGlobalFunctions
15941594
int functionRef = luaL_ref(E->L, LUA_REGISTRYINDEX);
15951595
if (functionRef != LUA_REFNIL && functionRef != LUA_NOREF)
15961596
{
1597-
E->eventMgr->globalProcessor->AddEvent(functionRef, min, max, repeats);
1597+
E->eventMgr->GetGlobalProcessor(GLOBAL_EVENTS)->AddEvent(functionRef, min, max, repeats);
15981598
E->Push(functionRef);
15991599
}
16001600
return 1;
@@ -1615,7 +1615,7 @@ namespace LuaGlobalFunctions
16151615
if (all_Events)
16161616
E->eventMgr->SetState(eventId, LUAEVENT_STATE_ABORT);
16171617
else
1618-
E->eventMgr->globalProcessor->SetState(eventId, LUAEVENT_STATE_ABORT);
1618+
E->eventMgr->GetGlobalProcessor(GLOBAL_EVENTS)->SetState(eventId, LUAEVENT_STATE_ABORT);
16191619
return 0;
16201620
}
16211621

@@ -1632,7 +1632,7 @@ namespace LuaGlobalFunctions
16321632
if (all_Events)
16331633
E->eventMgr->SetStates(LUAEVENT_STATE_ABORT);
16341634
else
1635-
E->eventMgr->globalProcessor->SetStates(LUAEVENT_STATE_ABORT);
1635+
E->eventMgr->GetGlobalProcessor(GLOBAL_EVENTS)->SetStates(LUAEVENT_STATE_ABORT);
16361636
return 0;
16371637
}
16381638

0 commit comments

Comments
 (0)