TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EventMap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 #ifndef _EVENT_MAP_H_
19 #define _EVENT_MAP_H_
20 
21 #include "Common.h"
22 #include "Duration.h"
23 #include "Util.h"
24 
26 {
38  typedef std::multimap<uint32, uint32> EventStore;
39 
40 public:
41  EventMap() : _time(0), _phase(0), _lastEvent(0) { }
42 
47  void Reset();
48 
54  void Update(uint32 time)
55  {
56  _time += time;
57  }
58 
63  uint32 GetTimer() const
64  {
65  return _time;
66  }
67 
73  {
74  return _phase;
75  }
76 
81  bool Empty() const
82  {
83  return _eventMap.empty();
84  }
85 
91  void SetPhase(uint8 phase);
92 
98  void AddPhase(uint8 phase)
99  {
100  if (phase && phase <= 8)
101  _phase |= uint8(1 << (phase - 1));
102  }
103 
109  void RemovePhase(uint8 phase)
110  {
111  if (phase && phase <= 8)
112  _phase &= uint8(~(1 << (phase - 1)));
113  }
114 
123  void ScheduleEvent(uint32 eventId, Milliseconds const& time, uint32 group = 0, uint8 phase = 0)
124  {
125  ScheduleEvent(eventId, uint32(time.count()), group, phase);
126  }
127 
136  void ScheduleEvent(uint32 eventId, uint32 time, uint32 group = 0, uint8 phase = 0);
137 
146  void RescheduleEvent(uint32 eventId, Milliseconds const& time, uint32 group = 0, uint8 phase = 0)
147  {
148  RescheduleEvent(eventId, uint32(time.count()), group, phase);
149  }
150 
159  void RescheduleEvent(uint32 eventId, uint32 time, uint32 group = 0, uint8 phase = 0)
160  {
161  CancelEvent(eventId);
162  ScheduleEvent(eventId, time, group, phase);
163  }
164 
170  void Repeat(Milliseconds const& time)
171  {
172  Repeat(uint32(time.count()));
173  }
174 
180  void Repeat(uint32 time)
181  {
182  _eventMap.insert(EventStore::value_type(_time + time, _lastEvent));
183  }
184 
191  void Repeat(Milliseconds const& minTime, Milliseconds const& maxTime)
192  {
193  Repeat(uint32(minTime.count()), uint32(maxTime.count()));
194  }
195 
202  void Repeat(uint32 minTime, uint32 maxTime)
203  {
204  Repeat(urand(minTime, maxTime));
205  }
206 
212  uint32 ExecuteEvent();
213 
219  void DelayEvents(Milliseconds const& delay)
220  {
221  DelayEvents(uint32(delay.count()));
222  }
223 
229  void DelayEvents(uint32 delay)
230  {
231  _time = delay < _time ? _time - delay : 0;
232  }
233 
240  void DelayEvents(Milliseconds const& delay, uint32 group)
241  {
242  DelayEvents(uint32(delay.count()), group);
243  }
244 
251  void DelayEvents(uint32 delay, uint32 group);
252 
258  void CancelEvent(uint32 eventId);
259 
265  void CancelEventGroup(uint32 group);
266 
273  uint32 GetNextEventTime(uint32 eventId) const;
274 
280  {
281  return Empty() ? 0 : _eventMap.begin()->first;
282  }
283 
290  bool IsInPhase(uint8 phase) const
291  {
292  return phase <= 8 && (!phase || _phase & (1 << (phase - 1)));
293  }
294 
301  uint32 GetTimeUntilEvent(uint32 eventId) const;
302 
303 private:
315 
325 
333  EventStore _eventMap;
334 
340 };
341 
342 #endif // _EVENT_MAP_H_
void RescheduleEvent(uint32 eventId, uint32 time, uint32 group=0, uint8 phase=0)
Definition: EventMap.h:159
void Repeat(uint32 minTime, uint32 maxTime)
Definition: EventMap.h:202
void DelayEvents(uint32 delay)
Definition: EventMap.h:229
std::multimap< uint32, uint32 > EventStore
Definition: EventMap.h:38
void DelayEvents(Milliseconds const &delay, uint32 group)
Definition: EventMap.h:240
uint32 GetTimer() const
Definition: EventMap.h:63
void Repeat(uint32 time)
Definition: EventMap.h:180
uint8 _phase
Definition: EventMap.h:324
bool IsInPhase(uint8 phase) const
Definition: EventMap.h:290
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:24
bool Empty() const
Definition: EventMap.h:81
uint8 GetPhaseMask() const
Definition: EventMap.h:72
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:45
Definition: EventMap.h:25
uint32 _time
Definition: EventMap.h:314
uint32_t uint32
Definition: Define.h:150
void Repeat(Milliseconds const &time)
Definition: EventMap.h:170
void ScheduleEvent(uint32 eventId, Milliseconds const &time, uint32 group=0, uint8 phase=0)
Definition: EventMap.h:123
#define TC_COMMON_API
Definition: Define.h:116
void Update(uint32 time)
Definition: EventMap.h:54
void RescheduleEvent(uint32 eventId, Milliseconds const &time, uint32 group=0, uint8 phase=0)
Definition: EventMap.h:146
void RemovePhase(uint8 phase)
Definition: EventMap.h:109
uint8_t uint8
Definition: g3dmath.h:164
void AddPhase(uint8 phase)
Definition: EventMap.h:98
uint32 GetNextEventTime() const
Definition: EventMap.h:279
void Repeat(Milliseconds const &minTime, Milliseconds const &maxTime)
Definition: EventMap.h:191
uint8_t uint8
Definition: Define.h:152
uint32 _lastEvent
Definition: EventMap.h:339
EventMap()
Definition: EventMap.h:41
uint32_t uint32
Definition: g3dmath.h:168
void DelayEvents(Milliseconds const &delay)
Definition: EventMap.h:219
EventStore _eventMap
Definition: EventMap.h:333