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 <compare>
24#include <map>
25#include <vector>
26
28{
29 using EventId = uint16;
34 struct Event
35 {
36 Event() = default;
37 Event(EventId id, GroupIndex groupIndex, PhaseIndex phaseIndex) :
38 _id(id),
39 _groupMask(groupIndex ? GroupMask(1u << (groupIndex - 1u)) : 0u),
40 _phaseMask(phaseIndex ? PhaseMask(1u << (phaseIndex - 1u)) : 0u)
41 {
42 }
43
44 EventId _id = 0u;
45 GroupMask _groupMask = 0u;
46 PhaseMask _phaseMask = 0u;
47
48 std::strong_ordering operator<=>(Event const& right) const = default;
49 };
50
55 using EventStore = std::multimap<TimePoint, Event>;
56 using EventSeriesStore = std::map<Event, std::vector<Milliseconds>>;
57
58public:
59 EventMap() : _time(TimePoint::min()), _phaseMask(0) { }
60 EventMap(EventMap const& other);
61 EventMap(EventMap&& other) noexcept = default;
62 EventMap& operator=(EventMap const& other);
63 EventMap& operator=(EventMap&& other) noexcept = default;
65
70 void Reset();
71
77 void Update(uint32 time)
78 {
79 Update(Milliseconds(time));
80 }
81
88 {
89 _time += time;
90 }
91
97 {
98 return _phaseMask;
99 }
100
105 bool Empty() const
106 {
107 return _eventMap.empty();
108 }
109
115 void SetPhase(PhaseIndex phase);
116
123 {
124 if (phase && phase <= sizeof(PhaseMask) * 8)
125 _phaseMask |= PhaseMask(1u << (phase - 1u));
126 }
127
134 {
135 if (phase && phase <= sizeof(PhaseMask) * 8)
136 _phaseMask &= PhaseMask(~(1u << (phase - 1u)));
137 }
138
147 void ScheduleEvent(EventId eventId, Milliseconds time, GroupIndex group = 0u, PhaseIndex phase = 0u);
148
158 void ScheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
159
168 void RescheduleEvent(EventId eventId, Milliseconds time, GroupIndex group = 0u, PhaseIndex phase = 0u);
169
179 void RescheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
180
186 void Repeat(Milliseconds time);
187
194 void Repeat(Milliseconds minTime, Milliseconds maxTime);
195
201 EventId ExecuteEvent();
202
208 void DelayEvents(Milliseconds delay);
209
216 void DelayEvents(Milliseconds delay, GroupIndex group);
217
224 void SetMinimalDelay(EventId eventId, Milliseconds delay);
225
231 void CancelEvent(EventId eventId);
232
238 void CancelEventGroup(GroupIndex group);
239
246 bool IsInPhase(PhaseIndex phase) const
247 {
248 return phase <= sizeof(PhaseIndex) * 8 && (!phase || _phaseMask & PhaseMask(1u << (phase - 1u)));
249 }
250
257 Milliseconds GetTimeUntilEvent(EventId eventId) const;
258
265 bool HasEventScheduled(EventId eventId) const;
266
272 void ScheduleNextFromSeries(Event eventData);
273
282 void ScheduleEventSeries(EventId eventId, GroupIndex group, PhaseIndex phase, std::initializer_list<Milliseconds> timeSeries);
283
290 void ScheduleEventSeries(EventId eventId, std::initializer_list<Milliseconds> timeSeries);
291
292private:
304
314
323
329
335};
336
337#endif // TRINITYCORE_EVENT_MAP_H
uint8_t uint8
Definition Define.h:156
#define TC_COMMON_API
Definition Define.h:99
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
std::chrono::steady_clock::time_point TimePoint
time_point shorthand typedefs
Definition Duration.h:40
EventId
void AddPhase(PhaseIndex phase)
Definition EventMap.h:122
uint8 PhaseIndex
Definition EventMap.h:32
EventMap & operator=(EventMap &&other) noexcept=default
EventMap()
Definition EventMap.h:59
uint8 GroupIndex
Definition EventMap.h:30
std::map< Event, std::vector< Milliseconds > > EventSeriesStore
Definition EventMap.h:56
TimePoint _time
Definition EventMap.h:303
void Update(uint32 time)
Definition EventMap.h:77
bool Empty() const
Definition EventMap.h:105
EventMap & operator=(EventMap const &other)
std::multimap< TimePoint, Event > EventStore
Definition EventMap.h:55
EventStore _eventMap
Definition EventMap.h:322
PhaseMask GetPhaseMask() const
Definition EventMap.h:96
Event _lastEvent
Definition EventMap.h:328
bool IsInPhase(PhaseIndex phase) const
Definition EventMap.h:246
EventSeriesStore _timerSeries
Definition EventMap.h:334
void RemovePhase(PhaseIndex phase)
Definition EventMap.h:133
uint8 PhaseMask
Definition EventMap.h:33
uint8 GroupMask
Definition EventMap.h:31
PhaseMask _phaseMask
Definition EventMap.h:313
void Update(Milliseconds time)
Definition EventMap.h:87
uint16 EventId
Definition EventMap.h:29
EventMap(EventMap &&other) noexcept=default
EventMap(EventMap const &other)
Event(EventId id, GroupIndex groupIndex, PhaseIndex phaseIndex)
Definition EventMap.h:37
std::strong_ordering operator<=>(Event const &right) const =default
Event()=default