Skip to content

Commit 6bccd78

Browse files
mdX7ccrs
authored andcommitted
Core/Utilities: Added timer series queuing to EventMap (TrinityCore#29420)
(cherry picked from commit c5735ed) # Conflicts: # src/common/Utilities/EventMap.cpp # src/common/Utilities/EventMap.h
1 parent 79c6eb4 commit 6bccd78

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/common/Utilities/EventMap.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ EventMap::EventId EventMap::ExecuteEvent()
8585
auto eventId = itr->second._id;
8686
_lastEvent = itr->second;
8787
_eventMap.erase(itr);
88+
ScheduleNextFromSeries(_lastEvent);
8889
return eventId;
8990
}
9091
}
@@ -160,6 +161,14 @@ void EventMap::CancelEvent(EventId eventId)
160161
else
161162
++itr;
162163
}
164+
165+
for (EventSeriesStore::iterator itr = _timerSeries.begin(); itr != _timerSeries.end();)
166+
{
167+
if (eventId == (itr->first & 0x0000FFFF))
168+
_timerSeries.erase(itr++);
169+
else
170+
++itr;
171+
}
163172
}
164173

165174
void EventMap::CancelEventGroup(GroupIndex group)
@@ -174,6 +183,14 @@ void EventMap::CancelEventGroup(GroupIndex group)
174183
else
175184
++itr;
176185
}
186+
187+
for (EventSeriesStore::iterator itr = _timerSeries.begin(); itr != _timerSeries.end();)
188+
{
189+
if (itr->first & (1 << (group + 15)))
190+
_timerSeries.erase(itr++);
191+
else
192+
++itr;
193+
}
177194
}
178195

179196
Milliseconds EventMap::GetTimeUntilEvent(EventId eventId) const
@@ -189,3 +206,40 @@ bool EventMap::HasEventScheduled(EventId eventId) const
189206
{
190207
return GetTimeUntilEvent(eventId) != Milliseconds::max();
191208
}
209+
210+
void EventMap::ScheduleNextFromSeries(uint32 eventData)
211+
{
212+
EventSeriesStore::iterator itr = _timerSeries.find(eventData);
213+
if (itr == _timerSeries.end())
214+
return;
215+
216+
if (itr->first != eventData)
217+
return;
218+
219+
if (itr->second.size() == 0)
220+
return;
221+
222+
Milliseconds time = itr->second.front();
223+
itr->second.pop();
224+
225+
ScheduleEvent(eventData, time);
226+
}
227+
228+
void EventMap::ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list<Milliseconds> const& timeSeries)
229+
{
230+
if (group && group <= 8)
231+
eventId |= (1 << (group + 15));
232+
233+
if (phase && phase <= 8)
234+
eventId |= (1 << (phase + 23));
235+
236+
for (Milliseconds const& time : timeSeries)
237+
_timerSeries[eventId].push(time);
238+
239+
ScheduleNextFromSeries(eventId);
240+
}
241+
242+
void EventMap::ScheduleEventSeries(uint32 eventId, std::initializer_list<Milliseconds> const& timeSeries)
243+
{
244+
ScheduleEventSeries(eventId, 0, 0, timeSeries);
245+
}

src/common/Utilities/EventMap.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Define.h"
2222
#include "Duration.h"
2323
#include <map>
24+
#include <queue>
2425

2526
class TC_COMMON_API EventMap
2627
{
@@ -49,6 +50,7 @@ class TC_COMMON_API EventMap
4950
* Key: Time as TimePoint when the event should occur.
5051
*/
5152
using EventStore = std::multimap<TimePoint, Event>;
53+
typedef std::map<uint32 /*event data*/, std::queue<Milliseconds>> EventSeriesStore;
5254

5355
public:
5456
EventMap() : _time(TimePoint::min()), _phaseMask(0) { }
@@ -255,6 +257,31 @@ class TC_COMMON_API EventMap
255257
*/
256258
bool HasEventScheduled(EventId eventId) const;
257259

260+
/**
261+
* @name ScheduleNextFromSeries
262+
* @brief Schedules specified event with next timer from series
263+
* @param full event data, including group and phase
264+
*/
265+
void ScheduleNextFromSeries(uint32 eventData);
266+
267+
/**
268+
* @name ScheduleEventSeries
269+
* @brief Schedules specified event with first value of the series and then requeues with the next
270+
* @param eventId of the event.
271+
* @param group of the event.
272+
* @param phase of the event.
273+
* @param timeSeries specifying the times the event should be automatically scheduled after each trigger (first value is initial schedule)
274+
*/
275+
void ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list<Milliseconds> const& timeSeries);
276+
277+
/**
278+
* @name ScheduleEventSeries
279+
* @brief Schedules specified event with first value of the series and then requeues with the next
280+
* @param eventId of the event.
281+
* @param timeSeries specifying the times the event should be automatically scheduled after each trigger (first value is initial schedule)
282+
*/
283+
void ScheduleEventSeries(uint32 eventId, std::initializer_list<Milliseconds> const& series);
284+
258285
private:
259286
/**
260287
* @name _time
@@ -292,6 +319,12 @@ class TC_COMMON_API EventMap
292319
* @brief Stores information on the most recently executed event
293320
*/
294321
Event _lastEvent;
322+
323+
/**
324+
* @name _timerSeries
325+
* @brief Stores information about time series which requeue itself until series is empty
326+
*/
327+
EventSeriesStore _timerSeries;
295328
};
296329

297330
#endif // TRINITYCORE_EVENT_MAP_H

0 commit comments

Comments
 (0)