@@ -18,22 +18,19 @@ extern "C"
1818#include " lauxlib.h"
1919};
2020
21- ElunaEventProcessor::ElunaEventProcessor (Eluna* _E, WorldObject* _obj) : m_time(0 ), obj(_obj), E(_E)
22- {
23- }
24-
2521ElunaEventProcessor::~ElunaEventProcessor ()
2622{
27- RemoveEvents_internal ();
23+ ClearAllEvents ();
2824}
2925
3026void ElunaEventProcessor::Update (uint32 diff)
3127{
3228 isUpdating = true ;
3329
3430 m_time += diff;
35- for (EventList::iterator it = eventList.begin (); it != eventList.end () && it ->first <= m_time; it = eventList. begin () )
31+ while (! eventList.empty () && eventList.begin () ->first <= m_time)
3632 {
33+ auto it = eventList.begin ();
3734 LuaEvent* luaEvent = it->second ;
3835 eventList.erase (it);
3936
@@ -47,13 +44,15 @@ void ElunaEventProcessor::Update(uint32 diff)
4744 if (!remove)
4845 AddEvent (luaEvent); // may be deferred if we recurse into Update
4946
47+ // Call the timed event
5048 if (!obj || (obj && obj->IsInWorld ()))
51- E->OnTimedEvent (luaEvent->funcRef , delay, luaEvent->repeats ? luaEvent->repeats -- : luaEvent->repeats , obj);
49+ mgr-> E ->OnTimedEvent (luaEvent->funcRef , delay, luaEvent->repeats ? luaEvent->repeats -- : luaEvent->repeats , obj);
5250
5351 if (!remove)
5452 continue ;
5553 }
5654
55+ // Event should be deleted (executed last time or set to be aborted)
5756 RemoveEvent (luaEvent);
5857 }
5958
@@ -76,7 +75,7 @@ void ElunaEventProcessor::SetStates(LuaEventState state)
7675 eventMap.clear ();
7776}
7877
79- void ElunaEventProcessor::RemoveEvents_internal ()
78+ void ElunaEventProcessor::ClearAllEvents ()
8079{
8180 if (isUpdating)
8281 {
@@ -117,7 +116,7 @@ void ElunaEventProcessor::AddEvent(LuaEvent* luaEvent)
117116 }
118117
119118 luaEvent->GenerateDelay ();
120- eventList.insert ( std::make_pair ( m_time + luaEvent->delay , luaEvent) );
119+ eventList.emplace ( m_time + luaEvent->delay , luaEvent);
121120 eventMap[luaEvent->funcRef ] = luaEvent;
122121}
123122
@@ -129,10 +128,10 @@ void ElunaEventProcessor::AddEvent(int funcRef, uint32 min, uint32 max, uint32 r
129128void ElunaEventProcessor::RemoveEvent (LuaEvent* luaEvent)
130129{
131130 // Unreference if should and if Eluna was not yet uninitialized and if the lua state still exists
132- if (luaEvent->state != LUAEVENT_STATE_ERASE && E->HasLuaState ())
131+ if (luaEvent->state != LUAEVENT_STATE_ERASE && mgr-> E ->HasLuaState ())
133132 {
134133 // Free lua function ref
135- luaL_unref (E->L , LUA_REGISTRYINDEX, luaEvent->funcRef );
134+ luaL_unref (mgr-> E ->L , LUA_REGISTRYINDEX, luaEvent->funcRef );
136135 }
137136 delete luaEvent;
138137}
@@ -172,7 +171,7 @@ void ElunaEventProcessor::ProcessDeferredOps()
172171 break ;
173172
174173 case DeferredOpType::ClearAll:
175- RemoveEvents_internal ();
174+ ClearAllEvents ();
176175 break ;
177176 }
178177 }
@@ -186,7 +185,7 @@ ElunaProcessorInfo::~ElunaProcessorInfo()
186185
187186EventMgr::EventMgr (Eluna* _E) : E(_E)
188187{
189- auto gp = std::make_unique<ElunaEventProcessor>(E , nullptr );
188+ auto gp = std::make_unique<ElunaEventProcessor>(this , nullptr );
190189 processors.insert (gp.get ());
191190 globalProcessors.emplace (GLOBAL_EVENTS, std::move (gp));
192191}
@@ -235,7 +234,7 @@ ElunaEventProcessor* EventMgr::GetGlobalProcessor(GlobalEventSpace space)
235234uint64 EventMgr::CreateObjectProcessor (WorldObject* obj)
236235{
237236 uint64 id = obj->GetGUID ().GetRawValue ();
238- auto proc = std::make_unique<ElunaEventProcessor>(E , obj);
237+ auto proc = std::make_unique<ElunaEventProcessor>(this , obj);
239238 ElunaEventProcessor* raw = proc.get ();
240239
241240 processors.insert (raw);
0 commit comments