TrinityCore
Loading...
Searching...
No Matches
EventMap.h
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#ifndef TRINITYCORE_EVENT_MAP_H
19#define TRINITYCORE_EVENT_MAP_H
20
21#include "Define.h"
22#include "Duration.h"
23#include <map>
24
26{
27 using EventId = uint16;
32 struct Event
33 {
34 Event() = default;
35 Event(EventId id, GroupIndex groupIndex, PhaseIndex phaseIndex) :
36 _id(id),
37 _groupMask(groupIndex ? GroupMask(1u << (groupIndex - 1u)) : 0u),
38 _phaseMask(phaseIndex ? PhaseMask(1u << (phaseIndex - 1u)) : 0u)
39 {
40 }
41
42 EventId _id = 0u;
43 GroupMask _groupMask = 0u;
44 PhaseMask _phaseMask = 0u;
45 };
46
51 using EventStore = std::multimap<TimePoint, Event>;
52
53public:
54 EventMap() : _time(TimePoint::min()), _phaseMask(0) { }
55
60 void Reset();
61
67 void Update(uint32 time)
68 {
69 Update(Milliseconds(time));
70 }
71
78 {
79 _time += time;
80 }
81
87 {
88 return _phaseMask;
89 }
90
95 bool Empty() const
96 {
97 return _eventMap.empty();
98 }
99
105 void SetPhase(PhaseIndex phase);
106
113 {
114 if (phase && phase <= sizeof(PhaseMask) * 8)
115 _phaseMask |= PhaseMask(1u << (phase - 1u));
116 }
117
124 {
125 if (phase && phase <= sizeof(PhaseMask) * 8)
126 _phaseMask &= PhaseMask(~(1u << (phase - 1u)));
127 }
128
137 void ScheduleEvent(EventId eventId, Milliseconds time, GroupIndex group = 0u, PhaseIndex phase = 0u);
138
148 void ScheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
149
158 void RescheduleEvent(EventId eventId, Milliseconds time, GroupIndex group = 0u, PhaseIndex phase = 0u);
159
169 void RescheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
170
176 void Repeat(Milliseconds time);
177
184 void Repeat(Milliseconds minTime, Milliseconds maxTime);
185
191 EventId ExecuteEvent();
192
198 void DelayEvents(Milliseconds delay);
199
206 void DelayEvents(Milliseconds delay, GroupIndex group);
207
214 void SetMinimalDelay(EventId eventId, Milliseconds delay);
215
221 void CancelEvent(EventId eventId);
222
228 void CancelEventGroup(GroupIndex group);
229
236 bool IsInPhase(PhaseIndex phase) const
237 {
238 return phase <= sizeof(PhaseIndex) * 8 && (!phase || _phaseMask & PhaseMask(1u << (phase - 1u)));
239 }
240
248 Milliseconds GetTimeUntilEvent(EventId eventId) const;
249
256 bool HasEventScheduled(EventId eventId) const;
257
258private:
270
280
289
295};
296
297#endif // TRINITYCORE_EVENT_MAP_H
uint8_t uint8
Definition Define.h:135
#define TC_COMMON_API
Definition Define.h:96
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
std::chrono::steady_clock::time_point TimePoint
time_point shorthand typedefs
Definition Duration.h:36
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
EventId
void AddPhase(PhaseIndex phase)
Definition EventMap.h:112
uint8 PhaseIndex
Definition EventMap.h:30
EventMap()
Definition EventMap.h:54
uint8 GroupIndex
Definition EventMap.h:28
TimePoint _time
Definition EventMap.h:269
void Update(uint32 time)
Definition EventMap.h:67
bool Empty() const
Definition EventMap.h:95
std::multimap< TimePoint, Event > EventStore
Definition EventMap.h:51
EventStore _eventMap
Definition EventMap.h:288
PhaseMask GetPhaseMask() const
Definition EventMap.h:86
Event _lastEvent
Definition EventMap.h:294
bool IsInPhase(PhaseIndex phase) const
Definition EventMap.h:236
void RemovePhase(PhaseIndex phase)
Definition EventMap.h:123
uint8 PhaseMask
Definition EventMap.h:31
uint8 GroupMask
Definition EventMap.h:29
PhaseMask _phaseMask
Definition EventMap.h:279
void Update(Milliseconds time)
Definition EventMap.h:77
uint16 EventId
Definition EventMap.h:27
Event(EventId id, GroupIndex groupIndex, PhaseIndex phaseIndex)
Definition EventMap.h:35
Event()=default