aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/EventMap.h
diff options
context:
space:
mode:
authorKelno <kelno@users.noreply.github.com>2023-08-19 23:57:57 +0200
committerGitHub <noreply@github.com>2023-08-19 23:57:57 +0200
commit1bea4aab456a01d89b87344d47cf345cd68f2d85 (patch)
tree7ced85020d8a3043f21e52c4f2c6e29b5956fb89 /src/common/Utilities/EventMap.h
parent24359b0618470eb580532306b6bd04783c2d3f7c (diff)
Core/EventMap: Refactor and modernize EventMap (#29183)
Diffstat (limited to 'src/common/Utilities/EventMap.h')
-rw-r--r--src/common/Utilities/EventMap.h118
1 files changed, 74 insertions, 44 deletions
diff --git a/src/common/Utilities/EventMap.h b/src/common/Utilities/EventMap.h
index 986dd7a2989..0fb16793d65 100644
--- a/src/common/Utilities/EventMap.h
+++ b/src/common/Utilities/EventMap.h
@@ -15,8 +15,8 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef _EVENT_MAP_H_
-#define _EVENT_MAP_H_
+#ifndef TRINITYCORE_EVENT_MAP_H
+#define TRINITYCORE_EVENT_MAP_H
#include "Define.h"
#include "Duration.h"
@@ -24,21 +24,34 @@
class TC_COMMON_API EventMap
{
+ using EventId = uint16;
+ using GroupIndex = uint8;
+ using GroupMask = uint8;
+ using PhaseIndex = uint8;
+ using PhaseMask = uint8;
+ struct Event
+ {
+ Event() = default;
+ Event(EventId id, GroupIndex groupIndex, PhaseIndex phaseIndex) :
+ _id(id),
+ _groupMask(groupIndex ? GroupMask(1u << (groupIndex - 1u)) : 0u),
+ _phaseMask(phaseIndex ? PhaseMask(1u << (phaseIndex - 1u)) : 0u)
+ {
+ }
+
+ EventId _id = 0u;
+ GroupMask _groupMask = 0u;
+ PhaseMask _phaseMask = 0u;
+ };
+
/**
- * 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
- */
- typedef std::multimap<TimePoint, uint32> EventStore;
+ * Internal storage type.
+ * Key: Time as TimePoint when the event should occur.
+ */
+ using EventStore = std::multimap<TimePoint, Event>;
public:
- EventMap() : _time(TimePoint::min()), _phase(0), _lastEvent(0) { }
+ EventMap() : _time(TimePoint::min()), _phaseMask(0) { }
/**
* @name Reset
@@ -70,9 +83,9 @@ public:
* @name GetPhaseMask
* @return Active phases as mask.
*/
- uint8 GetPhaseMask() const
+ PhaseMask GetPhaseMask() const
{
- return _phase;
+ return _phaseMask;
}
/**
@@ -89,50 +102,50 @@ public:
* @brief Sets the phase of the map (absolute).
* @param phase Phase which should be set. Values: 1 - 8. 0 resets phase.
*/
- void SetPhase(uint8 phase);
+ void SetPhase(PhaseIndex phase);
/**
* @name AddPhase
- * @brief Activates the given phase (bitwise).
+ * @brief Activates the given phase (absolute).
* @param phase Phase which should be activated. Values: 1 - 8
*/
- void AddPhase(uint8 phase)
+ void AddPhase(PhaseIndex phase)
{
- if (phase && phase <= 8)
- _phase |= uint8(1 << (phase - 1));
+ if (phase && phase <= sizeof(PhaseMask) * 8)
+ _phaseMask |= PhaseMask(1u << (phase - 1u));
}
/**
* @name RemovePhase
- * @brief Deactivates the given phase (bitwise).
+ * @brief Deactivates the given phase (absolute).
* @param phase Phase which should be deactivated. Values: 1 - 8.
*/
- void RemovePhase(uint8 phase)
+ void RemovePhase(PhaseIndex phase)
{
- if (phase && phase <= 8)
- _phase &= uint8(~(1 << (phase - 1)));
+ if (phase && phase <= sizeof(PhaseMask) * 8)
+ _phaseMask &= PhaseMask(~(1u << (phase - 1u)));
}
/**
* @name ScheduleEvent
- * @brief Schedules a new event. An existing event is not canceled.
+ * @brief Creates new event entry in map.
* @param eventId The id of the new event.
* @param time The time until the event occurs as std::chrono type.
* @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
* @param phase The 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(EventId eventId, Milliseconds time, GroupIndex group = 0u, PhaseIndex phase = 0u);
/**
* @name ScheduleEvent
- * @brief Schedules a new event. An existing event is not canceled.
+ * @brief Creates new event entry in map.
* @param eventId The id of the new event.
* @param minTime The minimum time until the event occurs as std::chrono type.
* @param maxTime The maximum time until the event occurs as std::chrono type.
* @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
* @param phase The 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 minTime, Milliseconds maxTime, uint32 group = 0, uint32 phase = 0);
+ void ScheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
/**
* @name RescheduleEvent
@@ -142,7 +155,7 @@ public:
* @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
* @param phase The 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(EventId eventId, Milliseconds time, GroupIndex group = 0u, PhaseIndex phase = 0u);
/**
* @name RescheduleEvent
@@ -153,7 +166,7 @@ public:
* @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
* @param phase The 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 minTime, Milliseconds maxTime, uint32 group = 0, uint32 phase = 0);
+ void RescheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
/**
* @name RepeatEvent
@@ -172,10 +185,10 @@ public:
/**
* @name ExecuteEvent
- * @brief Returns the next event to be executed and removes it from map.
+ * @brief Returns the next event to execute and removes it from map.
* @return Id of the event to execute.
*/
- uint32 ExecuteEvent();
+ EventId ExecuteEvent();
/**
* @name DelayEvents
@@ -190,21 +203,29 @@ public:
* @param delay Amount of delay as std::chrono type.
* @param group Group of the events.
*/
- void DelayEvents(Milliseconds delay, uint32 group);
+ void DelayEvents(Milliseconds delay, GroupIndex group);
+
+ /**
+ * @name SetMinimalDelay
+ * @brief Increase event delay if smaller than given delay.
+ * @param eventId The id of the event.
+ * @param delay Minimum delay for given event.
+ */
+ void SetMinimalDelay(EventId eventId, Milliseconds delay);
/**
* @name CancelEvent
* @brief Cancels all events of the specified id.
* @param eventId Event id to cancel.
*/
- void CancelEvent(uint32 eventId);
+ void CancelEvent(EventId eventId);
/**
* @name CancelEventGroup
* @brief Cancel events belonging to specified group.
* @param group Group to cancel.
*/
- void CancelEventGroup(uint32 group);
+ void CancelEventGroup(GroupIndex group);
/**
* @name IsInPhase
@@ -212,18 +233,27 @@ public:
* @param phase Wanted phase.
* @return True, if phase of event map contains specified phase.
*/
- bool IsInPhase(uint8 phase) const
+ bool IsInPhase(PhaseIndex phase) const
{
- return phase <= 8 && (!phase || _phase & (1 << (phase - 1)));
+ return phase <= sizeof(PhaseIndex) * 8 && (!phase || _phaseMask & PhaseMask(1u << (phase - 1u)));
}
/**
* @name GetTimeUntilEvent
* @brief Returns time as std::chrono type until next event.
- * @param eventId of the event.
+ * @param eventId The id of the event.
* @return Time of next event. If event is not scheduled returns Milliseconds::max()
+ * @return Time of next event.
*/
- Milliseconds GetTimeUntilEvent(uint32 eventId) const;
+ Milliseconds GetTimeUntilEvent(EventId eventId) const;
+
+ /**
+ * @name HasEventScheduled
+ * @brief Returns whether an event is scheduled
+ * @param eventId The id of the event.
+ * @return True if event is scheduled
+ */
+ bool HasEventScheduled(EventId eventId) const;
private:
/**
@@ -239,14 +269,14 @@ private:
TimePoint _time;
/**
- * @name _phase
+ * @name _phaseMask
* @brief 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;
+ PhaseMask _phaseMask;
/**
* @name _eventMap
@@ -261,7 +291,7 @@ private:
* @name _lastEvent
* @brief Stores information on the most recently executed event
*/
- uint32 _lastEvent;
+ Event _lastEvent;
};
-#endif // _EVENT_MAP_H_
+#endif // TRINITYCORE_EVENT_MAP_H