TrinityCore
EventProcessor.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "EventProcessor.h"
19#include "Errors.h"
20
22{
24 && "Tried to scheduled the abortion of an event twice!");
26}
27
29{
31 && "Tried to abort an already aborted event!");
33}
34
36{
37 KillAllEvents(true);
38}
39
41{
42 // update time
43 m_time += p_time;
44
45 // main event loop
46 std::multimap<uint64, BasicEvent*>::iterator i;
47 while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time)
48 {
49 // get and remove event from queue
50 BasicEvent* event = i->second;
51 m_events.erase(i);
52
53 if (event->IsRunning())
54 {
55 if (event->Execute(m_time, p_time))
56 {
57 // completely destroy event if it is not re-added
58 delete event;
59 }
60 continue;
61 }
62
63 if (event->IsAbortScheduled())
64 {
65 event->Abort(m_time);
66 // Mark the event as aborted
67 event->SetAborted();
68 }
69
70 if (event->IsDeletable())
71 {
72 delete event;
73 continue;
74 }
75
76 // Reschedule non deletable events to be checked at
77 // the next update tick
78 AddEvent(event, CalculateTime(1ms), false);
79 }
80}
81
83{
84 for (auto itr = m_events.begin(); itr != m_events.end();)
85 {
86 // Abort events which weren't aborted already
87 if (!itr->second->IsAborted())
88 {
89 itr->second->SetAborted();
90 itr->second->Abort(m_time);
91 }
92
93 // Skip non-deletable events when we are
94 // not forcing the event cancellation.
95 if (!force && !itr->second->IsDeletable())
96 {
97 ++itr;
98 continue;
99 }
100
101 delete itr->second;
102
103 if (force)
104 ++itr; // Clear the whole container when forcing
105 else
106 itr = m_events.erase(itr);
107 }
108
109 if (force)
110 m_events.clear();
111}
112
113void EventProcessor::AddEvent(BasicEvent* event, Milliseconds e_time, bool set_addtime)
114{
115 if (set_addtime)
116 event->m_addTime = m_time;
117 event->m_execTime = e_time.count();
118 m_events.insert(std::pair<uint64, BasicEvent*>(e_time.count(), event));
119}
120
122{
123 for (auto itr = m_events.begin(); itr != m_events.end(); ++itr)
124 {
125 if (itr->second != event)
126 continue;
127
128 event->m_execTime = newTime.count();
129 m_events.erase(itr);
130 m_events.insert(std::pair<uint64, BasicEvent*>(newTime.count(), event));
131 break;
132 }
133}
uint32_t uint32
Definition: Define.h:142
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
#define ASSERT
Definition: Errors.h:68
bool IsAborted() const
void SetAborted()
bool IsRunning() const
void ScheduleAbort()
AbortState m_abortState
std::multimap< uint64, BasicEvent * > m_events
void KillAllEvents(bool force)
void Update(uint32 p_time)
void AddEvent(BasicEvent *event, Milliseconds e_time, bool set_addtime=true)
void ModifyEventTime(BasicEvent *event, Milliseconds newTime)
Milliseconds CalculateTime(Milliseconds t_offset) const