TrinityCore
Loading...
Searching...
No Matches
EventMap Class Reference

#include <EventMap.h>

Public Member Functions

 EventMap ()
 
Reset

Removes all scheduled events and resets time and phase.

void Reset ()
 
Update

Updates the timer of the event map.

Parameters
timeValue in ms to be added to time.
void Update (uint32 time)
 
void Update (Milliseconds time)
 
GetPhaseMask
Returns
Active phases as mask.
uint8 GetPhaseMask () const
 
Empty
Returns
True, if there are no events scheduled.
bool Empty () const
 
SetPhase

Sets the phase of the map (absolute).

Parameters
phasePhase which should be set. Values: 1 - 8. 0 resets phase.
void SetPhase (uint8 phase)
 
AddPhase

Activates the given phase (bitwise).

Parameters
phasePhase which should be activated. Values: 1 - 8
void AddPhase (uint8 phase)
 
RemovePhase

Deactivates the given phase (bitwise).

Parameters
phasePhase which should be deactivated. Values: 1 - 8.
void RemovePhase (uint8 phase)
 
ScheduleEvent

Schedules a new event. An existing event is not canceled.

Parameters
eventIdThe id of the new event.
minTimeThe minimum time until the event occurs as std::chrono type.
maxTimeThe maximum time until the event occurs as std::chrono type.
groupThe group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
phaseThe phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
void ScheduleEvent (uint32 eventId, Milliseconds time, uint32 group=0, uint8 phase=0)
 
void ScheduleEvent (uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group=0, uint8 phase=0)
 
RescheduleEvent

Cancels the given event and reschedules it.

Parameters
eventIdThe id of the event.
minTimeThe minimum time until the event occurs as std::chrono type.
maxTimeThe maximum time until the event occurs as std::chrono type.
groupThe group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
phaseThe phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
void RescheduleEvent (uint32 eventId, Milliseconds time, uint32 group=0, uint8 phase=0)
 
void RescheduleEvent (uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group=0, uint8 phase=0)
 
RepeatEvent

Repeats the most recently executed event.

Parameters
minTimeThe minimum time until the event occurs as std::chrono type.
maxTimeThe maximum time until the event occurs as std::chrono type.
void Repeat (Milliseconds time)
 
void Repeat (Milliseconds minTime, Milliseconds maxTime)
 
ExecuteEvent

Returns the next event to be executed and removes it from map.

Returns
Id of the event to execute.
uint32 ExecuteEvent ()
 
DelayEvents

Delay all events of the same group.

Parameters
delayAmount of delay as std::chrono type.
groupGroup of the events.
void DelayEvents (Milliseconds delay)
 
void DelayEvents (Milliseconds delay, uint32 group)
 
CancelEvent

Cancels all events of the specified id.

Parameters
eventIdEvent id to cancel.
void CancelEvent (uint32 eventId)
 
CancelEventGroup

Cancel events belonging to specified group.

Parameters
groupGroup to cancel.
void CancelEventGroup (uint32 group)
 
IsInPhase

Returns whether event map is in specified phase or not.

Parameters
phaseWanted phase.
Returns
True, if phase of event map contains specified phase.
bool IsInPhase (uint8 phase) const
 
GetTimeUntilEvent

Returns time as std::chrono type until next event.

Parameters
eventIdof the event.
Returns
Time of next event. If event is not scheduled returns Milliseconds::max()
Milliseconds GetTimeUntilEvent (uint32 eventId) const
 
ScheduleNextFromSeries

Schedules specified event with next timer from series

Parameters
fullevent data, including group and phase
void ScheduleNextFromSeries (uint32 eventData)
 
ScheduleEventSeries

Schedules specified event with first value of the series and then requeues with the next

Parameters
eventIdof the event.
timeSeriesspecifying the times the event should be automatically scheduled after each trigger (first value is initial schedule)
void ScheduleEventSeries (uint32 eventId, uint8 group, uint8 phase, std::initializer_list< Milliseconds > const &timeSeries)
 
void ScheduleEventSeries (uint32 eventId, std::initializer_list< Milliseconds > const &series)
 

Private Types

typedef std::multimap< TimePoint, uint32EventStore
 
typedef std::map< uint32, std::queue< Milliseconds > > EventSeriesStore
 

Private Attributes

_time

Internal timer.

This does not represent the real date/time value. It's more like a stopwatch: It can run, it can be stopped, it can be resetted and so on. Events occur when this timer has reached their time value. Its value is changed in the Update method.

TimePoint _time
 
_phase

Phase mask of the event map.

Contains the phases the event map is in. Multiple phases from 1 to 8 can be set with SetPhase or AddPhase. RemovePhase deactives a phase.

uint8 _phase
 
_eventMap

Internal event storage map. Contains the scheduled events.

See typedef at the beginning of the class for more details.

EventStore _eventMap
 
_lastEvent

Stores information on the most recently executed event

uint32 _lastEvent
 
_timerSeries

Stores information about time series which requeue itself until series is empty

EventSeriesStore _timerSeries
 

Detailed Description

Definition at line 26 of file EventMap.h.

Member Typedef Documentation

◆ EventSeriesStore

typedef std::map<uint32 , std::queue<Milliseconds> > EventMap::EventSeriesStore
private

Definition at line 40 of file EventMap.h.

◆ EventStore

typedef std::multimap<TimePoint, uint32> EventMap::EventStore
private

Internal storage type. Key: Time as TimePoint when the event should occur. Value: The event data as uint32.

Structure of event data:

  • Bit 0 - 15: Event Id.
  • Bit 16 - 23: Group
  • Bit 24 - 31: Phase
  • Pattern: 0xPPGGEEEE

Definition at line 39 of file EventMap.h.

Constructor & Destructor Documentation

◆ EventMap()

EventMap::EventMap ( )
inline

Definition at line 43 of file EventMap.h.

43: _time(TimePoint::min()), _phase(0), _lastEvent(0) { }
uint8 _phase
Definition: EventMap.h:276
TimePoint _time
Definition: EventMap.h:266
uint32 _lastEvent
Definition: EventMap.h:291

Member Function Documentation

◆ AddPhase()

void EventMap::AddPhase ( uint8  phase)
inline

Definition at line 101 of file EventMap.h.

102 {
103 if (phase && phase <= 8)
104 _phase |= uint8(1 << (phase - 1));
105 }
uint8_t uint8
Definition: Define.h:145
+ Here is the caller graph for this function:

◆ CancelEvent()

void EventMap::CancelEvent ( uint32  eventId)

Definition at line 131 of file EventMap.cpp.

132{
133 if (Empty())
134 return;
135
136 for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
137 {
138 if (eventId == (itr->second & 0x0000FFFF))
139 _eventMap.erase(itr++);
140 else
141 ++itr;
142 }
143
144 for (EventSeriesStore::iterator itr = _timerSeries.begin(); itr != _timerSeries.end();)
145 {
146 if (eventId == (itr->first & 0x0000FFFF))
147 _timerSeries.erase(itr++);
148 else
149 ++itr;
150 }
151}
bool Empty() const
Definition: EventMap.h:84
EventStore _eventMap
Definition: EventMap.h:285
EventSeriesStore _timerSeries
Definition: EventMap.h:297
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CancelEventGroup()

void EventMap::CancelEventGroup ( uint32  group)

Definition at line 153 of file EventMap.cpp.

154{
155 if (!group || group > 8 || Empty())
156 return;
157
158 for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
159 {
160 if (itr->second & (1 << (group + 15)))
161 _eventMap.erase(itr++);
162 else
163 ++itr;
164 }
165
166 for (EventSeriesStore::iterator itr = _timerSeries.begin(); itr != _timerSeries.end();)
167 {
168 if (itr->first & (1 << (group + 15)))
169 _timerSeries.erase(itr++);
170 else
171 ++itr;
172 }
173}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DelayEvents() [1/2]

void EventMap::DelayEvents ( Milliseconds  delay)

Definition at line 96 of file EventMap.cpp.

97{
98 if (Empty())
99 return;
100
101 EventStore delayed = std::move(_eventMap);
102 for (EventStore::iterator itr = delayed.begin(); itr != delayed.end();)
103 {
104 EventStore::node_type node = delayed.extract(itr++);
105 node.key() = node.key() + delay;
106 _eventMap.insert(_eventMap.end(), std::move(node));
107 }
108}
std::multimap< TimePoint, uint32 > EventStore
Definition: EventMap.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DelayEvents() [2/2]

void EventMap::DelayEvents ( Milliseconds  delay,
uint32  group 
)

Definition at line 110 of file EventMap.cpp.

111{
112 if (!group || group > 8 || Empty())
113 return;
114
115 EventStore delayed;
116
117 for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
118 {
119 if (itr->second & (1 << (group + 15)))
120 {
121 delayed.insert(EventStore::value_type(itr->first + delay, itr->second));
122 _eventMap.erase(itr++);
123 }
124 else
125 ++itr;
126 }
127
128 _eventMap.insert(delayed.begin(), delayed.end());
129}
+ Here is the call graph for this function:

◆ Empty()

bool EventMap::Empty ( ) const
inline

Definition at line 84 of file EventMap.h.

85 {
86 return _eventMap.empty();
87 }
+ Here is the caller graph for this function:

◆ ExecuteEvent()

uint32 EventMap::ExecuteEvent ( )

Definition at line 73 of file EventMap.cpp.

74{
75 while (!Empty())
76 {
77 EventStore::iterator itr = _eventMap.begin();
78
79 if (itr->first > _time)
80 return 0;
81 else if (_phase && (itr->second & 0xFF000000) && !((itr->second >> 24) & _phase))
82 _eventMap.erase(itr);
83 else
84 {
85 uint32 eventId = (itr->second & 0x0000FFFF);
86 _lastEvent = itr->second; // include phase/group
87 _eventMap.erase(itr);
89 return eventId;
90 }
91 }
92
93 return 0;
94}
uint32_t uint32
Definition: Define.h:143
void ScheduleNextFromSeries(uint32 eventData)
Definition: EventMap.cpp:184
+ Here is the call graph for this function:

◆ GetPhaseMask()

uint8 EventMap::GetPhaseMask ( ) const
inline

Definition at line 75 of file EventMap.h.

76 {
77 return _phase;
78 }
+ Here is the caller graph for this function:

◆ GetTimeUntilEvent()

Milliseconds EventMap::GetTimeUntilEvent ( uint32  eventId) const

Definition at line 175 of file EventMap.cpp.

176{
177 for (std::pair<TimePoint const, uint32> const& itr : _eventMap)
178 if (eventId == (itr.second & 0x0000FFFF))
179 return std::chrono::duration_cast<Milliseconds>(itr.first - _time);
180
181 return Milliseconds::max();
182}
+ Here is the caller graph for this function:

◆ IsInPhase()

bool EventMap::IsInPhase ( uint8  phase) const
inline

Definition at line 217 of file EventMap.h.

218 {
219 return phase <= 8 && (!phase || _phase & (1 << (phase - 1)));
220 }
+ Here is the caller graph for this function:

◆ RemovePhase()

void EventMap::RemovePhase ( uint8  phase)
inline

Definition at line 112 of file EventMap.h.

113 {
114 if (phase && phase <= 8)
115 _phase &= uint8(~(1 << (phase - 1)));
116 }
+ Here is the caller graph for this function:

◆ Repeat() [1/2]

void EventMap::Repeat ( Milliseconds  minTime,
Milliseconds  maxTime 
)

Definition at line 68 of file EventMap.cpp.

69{
70 Repeat(randtime(minTime, maxTime));
71}
Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition: Random.cpp:62
void Repeat(Milliseconds time)
Definition: EventMap.cpp:63
+ Here is the call graph for this function:

◆ Repeat() [2/2]

void EventMap::Repeat ( Milliseconds  time)

Definition at line 63 of file EventMap.cpp.

64{
65 _eventMap.insert(EventStore::value_type(_time + time, _lastEvent));
66}

◆ RescheduleEvent() [1/2]

void EventMap::RescheduleEvent ( uint32  eventId,
Milliseconds  minTime,
Milliseconds  maxTime,
uint32  group = 0,
uint8  phase = 0 
)

Definition at line 58 of file EventMap.cpp.

59{
60 RescheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
61}
void RescheduleEvent(uint32 eventId, Milliseconds time, uint32 group=0, uint8 phase=0)
Definition: EventMap.cpp:52
+ Here is the call graph for this function:

◆ RescheduleEvent() [2/2]

void EventMap::RescheduleEvent ( uint32  eventId,
Milliseconds  time,
uint32  group = 0,
uint8  phase = 0 
)

Definition at line 52 of file EventMap.cpp.

53{
54 CancelEvent(eventId);
55 ScheduleEvent(eventId, time, group, phase);
56}
void ScheduleEvent(uint32 eventId, Milliseconds time, uint32 group=0, uint8 phase=0)
Definition: EventMap.cpp:36
void CancelEvent(uint32 eventId)
Definition: EventMap.cpp:131
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Reset()

void EventMap::Reset ( )

Definition at line 21 of file EventMap.cpp.

22{
23 _eventMap.clear();
24 _time = TimePoint::min();
25 _phase = 0;
26}

◆ ScheduleEvent() [1/2]

void EventMap::ScheduleEvent ( uint32  eventId,
Milliseconds  minTime,
Milliseconds  maxTime,
uint32  group = 0,
uint8  phase = 0 
)

Definition at line 47 of file EventMap.cpp.

48{
49 ScheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
50}
+ Here is the call graph for this function:

◆ ScheduleEvent() [2/2]

void EventMap::ScheduleEvent ( uint32  eventId,
Milliseconds  time,
uint32  group = 0,
uint8  phase = 0 
)

Definition at line 36 of file EventMap.cpp.

37{
38 if (group && group <= 8)
39 eventId |= (1 << (group + 15));
40
41 if (phase && phase <= 8)
42 eventId |= (1 << (phase + 23));
43
44 _eventMap.insert(EventStore::value_type(_time + time, eventId));
45}

◆ ScheduleEventSeries() [1/2]

void EventMap::ScheduleEventSeries ( uint32  eventId,
std::initializer_list< Milliseconds > const &  series 
)

Definition at line 216 of file EventMap.cpp.

217{
218 ScheduleEventSeries(eventId, 0, 0, timeSeries);
219}
void ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list< Milliseconds > const &timeSeries)
Definition: EventMap.cpp:202
+ Here is the call graph for this function:

◆ ScheduleEventSeries() [2/2]

void EventMap::ScheduleEventSeries ( uint32  eventId,
uint8  group,
uint8  phase,
std::initializer_list< Milliseconds > const &  timeSeries 
)

Definition at line 202 of file EventMap.cpp.

203{
204 if (group && group <= 8)
205 eventId |= (1 << (group + 15));
206
207 if (phase && phase <= 8)
208 eventId |= (1 << (phase + 23));
209
210 for (Milliseconds const& time : timeSeries)
211 _timerSeries[eventId].push(time);
212
213 ScheduleNextFromSeries(eventId);
214}
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ScheduleNextFromSeries()

void EventMap::ScheduleNextFromSeries ( uint32  eventData)

Definition at line 184 of file EventMap.cpp.

185{
186 EventSeriesStore::iterator itr = _timerSeries.find(eventData);
187 if (itr == _timerSeries.end())
188 return;
189
190 if (itr->first != eventData)
191 return;
192
193 if (itr->second.size() == 0)
194 return;
195
196 Milliseconds time = itr->second.front();
197 itr->second.pop();
198
199 ScheduleEvent(eventData, time);
200}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetPhase()

void EventMap::SetPhase ( uint8  phase)

Definition at line 28 of file EventMap.cpp.

29{
30 if (!phase)
31 _phase = 0;
32 else if (phase <= 8)
33 _phase = uint8(1 << (phase - 1));
34}

◆ Update() [1/2]

void EventMap::Update ( Milliseconds  time)
inline

Definition at line 66 of file EventMap.h.

67 {
68 _time += time;
69 }

◆ Update() [2/2]

void EventMap::Update ( uint32  time)
inline

Definition at line 56 of file EventMap.h.

57 {
58 Update(Milliseconds(time));
59 }
void Update(uint32 time)
Definition: EventMap.h:56
+ Here is the call graph for this function:

Member Data Documentation

◆ _eventMap

EventStore EventMap::_eventMap
private

Definition at line 285 of file EventMap.h.

◆ _lastEvent

uint32 EventMap::_lastEvent
private

Definition at line 291 of file EventMap.h.

◆ _phase

uint8 EventMap::_phase
private

Definition at line 276 of file EventMap.h.

◆ _time

TimePoint EventMap::_time
private

Definition at line 266 of file EventMap.h.

◆ _timerSeries

EventSeriesStore EventMap::_timerSeries
private

Definition at line 297 of file EventMap.h.


The documentation for this class was generated from the following files: