TrinityCore
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
 

Private Types

typedef std::multimap< TimePoint, uint32EventStore
 

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
 

Member Typedef Documentation

◆ 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

Constructor & Destructor Documentation

◆ EventMap()

EventMap::EventMap ( )
inline
41 : _time(TimePoint::min()), _phase(0), _lastEvent(0) { }
TimePoint _time
Definition: EventMap.h:239
uint8 _phase
Definition: EventMap.h:249
uint32 _lastEvent
Definition: EventMap.h:264

Member Function Documentation

◆ AddPhase()

void EventMap::AddPhase ( uint8  phase)
inline
100  {
101  if (phase && phase <= 8)
102  _phase |= uint8(1 << (phase - 1));
103  }
uint8 _phase
Definition: EventMap.h:249
uint8_t uint8
Definition: Define.h:145
+ Here is the caller graph for this function:

◆ CancelEvent()

void EventMap::CancelEvent ( uint32  eventId)
131 {
132  if (Empty())
133  return;
134 
135  for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
136  {
137  if (eventId == (itr->second & 0x0000FFFF))
138  _eventMap.erase(itr++);
139  else
140  ++itr;
141  }
142 }
bool Empty() const
Definition: EventMap.h:82
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CancelEventGroup()

void EventMap::CancelEventGroup ( uint32  group)
145 {
146  if (!group || group > 8 || Empty())
147  return;
148 
149  for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
150  {
151  if (itr->second & (1 << (group + 15)))
152  _eventMap.erase(itr++);
153  else
154  ++itr;
155  }
156 }
bool Empty() const
Definition: EventMap.h:82
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DelayEvents() [1/2]

void EventMap::DelayEvents ( Milliseconds  delay)
96 {
97  if (Empty())
98  return;
99 
100  EventStore delayed = std::move(_eventMap);
101  for (EventStore::iterator itr = delayed.begin(); itr != delayed.end();)
102  {
103  EventStore::node_type node = delayed.extract(itr++);
104  node.key() = node.key() + delay;
105  _eventMap.insert(_eventMap.end(), std::move(node));
106  }
107 }
bool Empty() const
Definition: EventMap.h:82
std::multimap< TimePoint, uint32 > EventStore
Definition: EventMap.h:38
EventStore _eventMap
Definition: EventMap.h:258
+ 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 
)
110 {
111  if (!group || group > 8 || Empty())
112  return;
113 
114  EventStore delayed;
115 
116  for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
117  {
118  if (itr->second & (1 << (group + 15)))
119  {
120  delayed.insert(EventStore::value_type(itr->first + delay, itr->second));
121  _eventMap.erase(itr++);
122  }
123  else
124  ++itr;
125  }
126 
127  _eventMap.insert(delayed.begin(), delayed.end());
128 }
bool Empty() const
Definition: EventMap.h:82
std::multimap< TimePoint, uint32 > EventStore
Definition: EventMap.h:38
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the call graph for this function:

◆ Empty()

bool EventMap::Empty ( ) const
inline
83  {
84  return _eventMap.empty();
85  }
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the caller graph for this function:

◆ ExecuteEvent()

uint32 EventMap::ExecuteEvent ( )
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);
88  return eventId;
89  }
90  }
91 
92  return 0;
93 }
TimePoint _time
Definition: EventMap.h:239
uint8 _phase
Definition: EventMap.h:249
uint32_t uint32
Definition: Define.h:143
bool Empty() const
Definition: EventMap.h:82
uint32 _lastEvent
Definition: EventMap.h:264
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the call graph for this function:

◆ GetPhaseMask()

uint8 EventMap::GetPhaseMask ( ) const
inline
74  {
75  return _phase;
76  }
uint8 _phase
Definition: EventMap.h:249
+ Here is the caller graph for this function:

◆ GetTimeUntilEvent()

Milliseconds EventMap::GetTimeUntilEvent ( uint32  eventId) const
159 {
160  for (std::pair<TimePoint const, uint32> const& itr : _eventMap)
161  if (eventId == (itr.second & 0x0000FFFF))
162  return std::chrono::duration_cast<Milliseconds>(itr.first - _time);
163 
164  return Milliseconds::max();
165 }
TimePoint _time
Definition: EventMap.h:239
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:24
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the caller graph for this function:

◆ IsInPhase()

bool EventMap::IsInPhase ( uint8  phase) const
inline
216  {
217  return phase <= 8 && (!phase || _phase & (1 << (phase - 1)));
218  }
uint8 _phase
Definition: EventMap.h:249
+ Here is the caller graph for this function:

◆ RemovePhase()

void EventMap::RemovePhase ( uint8  phase)
inline
111  {
112  if (phase && phase <= 8)
113  _phase &= uint8(~(1 << (phase - 1)));
114  }
uint8 _phase
Definition: EventMap.h:249
uint8_t uint8
Definition: Define.h:145
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Repeat() [1/2]

void EventMap::Repeat ( Milliseconds  time)
64 {
65  _eventMap.insert(EventStore::value_type(_time + time, _lastEvent));
66 }
TimePoint _time
Definition: EventMap.h:239
uint32 _lastEvent
Definition: EventMap.h:264
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the caller graph for this function:

◆ Repeat() [2/2]

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

◆ RescheduleEvent() [1/2]

void EventMap::RescheduleEvent ( uint32  eventId,
Milliseconds  time,
uint32  group = 0,
uint8  phase = 0 
)
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:130
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RescheduleEvent() [2/2]

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

◆ Reset()

void EventMap::Reset ( )
22 {
23  _eventMap.clear();
24  _time = TimePoint::min();
25  _phase = 0;
26 }
TimePoint _time
Definition: EventMap.h:239
uint8 _phase
Definition: EventMap.h:249
EventStore _eventMap
Definition: EventMap.h:258
+ Here is the caller graph for this function:

◆ ScheduleEvent() [1/2]

void EventMap::ScheduleEvent ( uint32  eventId,
Milliseconds  time,
uint32  group = 0,
uint8  phase = 0 
)
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 }
TimePoint _time
Definition: EventMap.h:239
EventStore _eventMap
Definition: EventMap.h:258

◆ ScheduleEvent() [2/2]

void EventMap::ScheduleEvent ( uint32  eventId,
Milliseconds  minTime,
Milliseconds  maxTime,
uint32  group = 0,
uint8  phase = 0 
)
48 {
49  ScheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
50 }
Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition: Random.cpp:62
void ScheduleEvent(uint32 eventId, Milliseconds time, uint32 group=0, uint8 phase=0)
Definition: EventMap.cpp:36
+ Here is the call graph for this function:

◆ SetPhase()

void EventMap::SetPhase ( uint8  phase)
29 {
30  if (!phase)
31  _phase = 0;
32  else if (phase <= 8)
33  _phase = uint8(1 << (phase - 1));
34 }
uint8 _phase
Definition: EventMap.h:249
uint8_t uint8
Definition: Define.h:145
+ Here is the caller graph for this function:

◆ Update() [1/2]

void EventMap::Update ( uint32  time)
inline
55  {
56  Update(Milliseconds(time));
57  }
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:24
void Update(uint32 time)
Definition: EventMap.h:54
+ Here is the call graph for this function:

◆ Update() [2/2]

void EventMap::Update ( Milliseconds  time)
inline
65  {
66  _time += time;
67  }
TimePoint _time
Definition: EventMap.h:239

Member Data Documentation

◆ _eventMap

EventStore EventMap::_eventMap
private

◆ _lastEvent

uint32 EventMap::_lastEvent
private

◆ _phase

uint8 EventMap::_phase
private

◆ _time

TimePoint EventMap::_time
private

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