refactor(Core/EventMap): Refactor EventMap and related scripts (#23121)

Co-authored-by: Kelno <3866946+kelno@users.noreply.github.com>
Co-authored-by: Peter Keresztes Schmidt <carbenium@outlook.com>
This commit is contained in:
天鹭
2025-10-13 09:19:24 +08:00
committed by GitHub
parent 8e1426c06a
commit 9c49349e1e
168 changed files with 1468 additions and 1593 deletions

View File

@@ -21,113 +21,81 @@
void EventMap::Reset()
{
_eventMap.clear();
_time = 0;
_phase = 0;
_time = TimePoint::min();
_phaseMask = 0;
}
void EventMap::SetPhase(uint8 phase)
void EventMap::SetPhase(PhaseIndex phase)
{
if (!phase)
{
_phase = 0;
}
else if (phase <= 8)
{
_phase = (1 << (phase - 1));
}
_phaseMask = 0;
else if (phase <= sizeof(PhaseMask) * 8)
_phaseMask = PhaseMask(1u << (phase - 1u));
}
void EventMap::AddPhase(uint8 phase)
void EventMap::AddPhase(PhaseIndex phase)
{
if (phase && phase <= 8)
{
_phase |= (1 << (phase - 1));
}
if (phase && phase <= sizeof(PhaseMask) * 8)
_phaseMask |= PhaseMask(1u << (phase - 1u));
}
void EventMap::RemovePhase(uint8 phase)
void EventMap::RemovePhase(PhaseIndex phase)
{
if (phase && phase <= 8)
{
_phase &= ~(1 << (phase - 1));
}
if (phase && phase <= sizeof(PhaseMask) * 8)
_phaseMask &= PhaseMask(~(1u << (phase - 1u)));
}
void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/, uint32 phase /*= 0*/)
void EventMap::ScheduleEvent(EventId eventId, Milliseconds time, GroupIndex group /*= 0u*/, PhaseIndex phase /*= 0u*/)
{
if (group && group <= 8)
{
eventId |= (1 << (group + 15));
}
if (group > sizeof(GroupMask) * 8)
return;
if (phase && phase <= 8)
{
eventId |= (1 << (phase + 23));
}
if (phase > sizeof(PhaseMask) * 8)
return;
_eventMap.emplace(_time + time, eventId);
_eventMap.emplace(_time + time, Event(eventId, group, phase));
}
void EventMap::ScheduleEvent(uint32 eventId, Milliseconds time, uint32 group /*= 0*/, uint8 phase /* = 0*/)
void EventMap::ScheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group /*= 0u*/, PhaseIndex phase /*= 0u*/)
{
ScheduleEvent(eventId, time.count(), group, phase);
ScheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
}
void EventMap::ScheduleEvent(uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/)
{
ScheduleEvent(eventId, randtime(minTime, maxTime).count(), group, phase);
}
void EventMap::RescheduleEvent(uint32 eventId, uint32 time, uint32 groupId /*= 0*/, uint32 phase/* = 0*/)
void EventMap::RescheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group /*= 0u*/, PhaseIndex phase /*= 0u*/)
{
CancelEvent(eventId);
ScheduleEvent(eventId, time, groupId, phase);
ScheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
}
void EventMap::RescheduleEvent(uint32 eventId, Milliseconds time, uint32 group /*= 0*/, uint8 phase /* = 0*/)
void EventMap::RescheduleEvent(EventId eventId, Milliseconds time, GroupIndex group /*= 0u*/, PhaseIndex phase /*= 0u*/)
{
CancelEvent(eventId);
ScheduleEvent(eventId, time.count(), group, phase);
}
void EventMap::RescheduleEvent(uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/)
{
CancelEvent(eventId);
ScheduleEvent(eventId, randtime(minTime, maxTime).count(), group, phase);
}
void EventMap::RepeatEvent(uint32 time)
{
_eventMap.emplace(_time + time, _lastEvent);
ScheduleEvent(eventId, time, group, phase);
}
void EventMap::Repeat(Milliseconds time)
{
RepeatEvent(time.count());
_eventMap.emplace(_time + time, _lastEvent);
}
void EventMap::Repeat(Milliseconds minTime, Milliseconds maxTime)
{
RepeatEvent(randtime(minTime, maxTime).count());
Repeat(randtime(minTime, maxTime));
}
uint32 EventMap::ExecuteEvent()
EventMap::EventId EventMap::ExecuteEvent()
{
while (!Empty())
{
auto const& itr = _eventMap.begin();
if (itr->first > _time)
{
return 0;
}
else if (_phase && (itr->second & 0xFF000000) && !((itr->second >> 24) & _phase))
{
else if (_phaseMask && itr->second._phaseMask && !(itr->second._phaseMask & _phaseMask))
_eventMap.erase(itr);
}
else
{
uint32 eventId = (itr->second & 0x0000FFFF);
auto eventId = itr->second._id;
_lastEvent = itr->second;
_eventMap.erase(itr);
return eventId;
@@ -137,30 +105,32 @@ uint32 EventMap::ExecuteEvent()
return 0;
}
void EventMap::DelayEvents(uint32 delay)
{
_time = delay < _time ? _time - delay : 0;
}
void EventMap::DelayEvents(Milliseconds delay)
{
DelayEvents(delay.count());
if (Empty())
return;
EventStore delayed = std::move(_eventMap);
for (auto itr = delayed.begin(); itr != delayed.end();)
{
auto node = delayed.extract(itr++);
node.key() = node.key() + delay;
_eventMap.insert(_eventMap.end(), std::move(node));
}
}
void EventMap::DelayEvents(uint32 delay, uint32 group)
void EventMap::DelayEvents(Milliseconds delay, GroupIndex group)
{
if (group > 8 || Empty())
{
if (group > sizeof(GroupMask) * 8 || Empty())
return;
}
EventStore delayed;
for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
for (auto itr = _eventMap.begin(); itr != _eventMap.end();)
{
if (!group || (itr->second & (1 << (group + 15))))
if (!group || (itr->second._groupMask & GroupMask(1u << (group - 1u))))
{
delayed.insert(EventStore::value_type(itr->first + delay, itr->second));
delayed.emplace(itr->first + delay, itr->second);
itr = _eventMap.erase(itr);
continue;
}
@@ -171,13 +141,13 @@ void EventMap::DelayEvents(uint32 delay, uint32 group)
_eventMap.insert(delayed.begin(), delayed.end());
}
void EventMap::DelayEventsToMax(uint32 delay, uint32 group)
void EventMap::DelayEventsToMax(Milliseconds delay, GroupIndex group)
{
for (auto itr = _eventMap.begin(); itr != _eventMap.end();)
{
if (itr->first < _time + delay && (group == 0 || ((1 << (group + 15)) & itr->second)))
if (itr->first < _time + delay && (!group || (itr->second._groupMask & GroupMask(1u << (group - 1u)))))
{
ScheduleEvent(itr->second, delay);
ScheduleEvent(itr->second._id, delay, group);
_eventMap.erase(itr);
itr = _eventMap.begin();
continue;
@@ -187,16 +157,14 @@ void EventMap::DelayEventsToMax(uint32 delay, uint32 group)
}
}
void EventMap::CancelEvent(uint32 eventId)
void EventMap::CancelEvent(EventId eventId)
{
if (Empty())
{
return;
}
for (auto itr = _eventMap.begin(); itr != _eventMap.end();)
{
if (eventId == (itr->second & 0x0000FFFF))
if (eventId == itr->second._id)
{
itr = _eventMap.erase(itr);
continue;
@@ -206,17 +174,14 @@ void EventMap::CancelEvent(uint32 eventId)
}
}
void EventMap::CancelEventGroup(uint32 group)
void EventMap::CancelEventGroup(GroupIndex group)
{
if (!group || group > 8 || Empty())
{
if (!group || group > sizeof(GroupMask) * 8 || Empty())
return;
}
uint32 groupMask = (1 << (group + 15));
for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
for (auto itr = _eventMap.begin(); itr != _eventMap.end();)
{
if (itr->second & groupMask)
if (itr->second._groupMask & GroupMask(1u << (group - 1u)))
{
_eventMap.erase(itr);
itr = _eventMap.begin();
@@ -227,39 +192,21 @@ void EventMap::CancelEventGroup(uint32 group)
}
}
uint32 EventMap::GetNextEventTime(uint32 eventId) const
bool EventMap::IsInPhase(PhaseIndex phase) const
{
if (Empty())
{
return 0;
}
for (auto const& itr : _eventMap)
{
if (eventId == (itr.second & 0x0000FFFF))
{
return itr.first;
}
}
return 0;
return phase <= sizeof(PhaseIndex) * 8 && (!phase || _phaseMask & PhaseMask(1u << (phase - 1u)));
}
uint32 EventMap::GetNextEventTime() const
Milliseconds EventMap::GetTimeUntilEvent(EventId eventId) const
{
return Empty() ? 0 : _eventMap.begin()->first;
}
bool EventMap::IsInPhase(uint8 phase)
{
return phase <= 8 && (!phase || _phase & (1 << (phase - 1)));
}
Milliseconds EventMap::GetTimeUntilEvent(uint32 eventId) const
{
for (std::pair<uint32 const, uint32> const& itr : _eventMap)
if (eventId == (itr.second & 0x0000FFFF))
return std::chrono::duration_cast<Milliseconds>(Milliseconds(itr.first) - Milliseconds(_time));
for (auto const& [time, event] : _eventMap)
if (eventId == event._id)
return std::chrono::duration_cast<Milliseconds>(time - _time);
return Milliseconds::max();
}
bool EventMap::HasTimeUntilEvent(EventId eventId) const
{
return GetTimeUntilEvent(eventId) != Milliseconds::max();
}

View File

@@ -24,18 +24,31 @@
class 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<uint32, uint32> EventStore;
* Internal storage type.
* Key: Time as TimePoint when the event should occur.
*/
using EventStore = std::multimap<TimePoint, Event>;
public:
EventMap() { }
@@ -47,13 +60,13 @@ public:
void Reset();
/**
* @name Update
* @brief Updates the timer of the event map.
* @param time Value to be added to time.
*/
* @name Update
* @brief Updates the timer of the event map.
* @param time Value to be added to time.
*/
void Update(uint32 time)
{
_time += time;
Update(Milliseconds(time));
}
/**
@@ -63,37 +76,23 @@ public:
*/
void Update(Milliseconds time)
{
_time += static_cast<uint32>(time.count());
}
/**
* @name GetTimer
* @return Current timer value.
*/
[[nodiscard]] uint32 GetTimer() const
{
return _time;
}
void SetTimer(uint32 time)
{
_time = time;
_time += time;
}
/**
* @name GetPhaseMask
* @return Active phases as mask.
*/
[[nodiscard]] uint8 GetPhaseMask() const
PhaseMask GetPhaseMask() const
{
return _phase;
return _phaseMask;
}
/**
* @name Empty
* @return True, if there are no events scheduled.
*/
[[nodiscard]] bool Empty() const
bool Empty() const
{
return _eventMap.empty();
}
@@ -103,31 +102,21 @@ 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);
/**
* @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);
/**
* @name ScheduleEvent
* @brief Creates new event entry in map.
* @param eventId The id of the new event.
* @param time The time in milliseconds until the event occurs.
* @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, uint32 time, uint32 group = 0, uint32 phase = 0);
void RemovePhase(PhaseIndex phase);
/**
* @name ScheduleEvent
@@ -137,7 +126,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 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
@@ -148,17 +137,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 ScheduleEvent(uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group = 0, uint32 phase = 0);
/**
* @name RescheduleEvent
* @brief Cancels the given event and reschedules it.
* @param eventId The id of the event.
* @param time The time in milliseconds until the event occurs.
* @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, uint32 time, uint32 groupId = 0, uint32 phase = 0);
void ScheduleEvent(EventId eventId, Milliseconds minTime, Milliseconds maxTime, GroupIndex group = 0u, PhaseIndex phase = 0u);
/**
* @name RescheduleEvent
@@ -168,7 +147,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
@@ -179,25 +158,17 @@ 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
* @brief Repeats the most recently executed event.
* @param time Time until the event occurs as std::chrono type.
*/
void RepeatEvent(uint32 time);
/**
* @name RepeatEvent
* @name Repeat
* @brief Repeats the most recently executed event.
* @param time Time until the event occurs as std::chrono type.
*/
void Repeat(Milliseconds time);
/**
* @name RepeatEvent
* @name Repeat
* @brief Repeats the most recently executed 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.
@@ -209,14 +180,7 @@ public:
* @brief Returns the next event to execute and removes it from map.
* @return Id of the event to execute.
*/
uint32 ExecuteEvent();
/**
* @name DelayEvents
* @brief Delays all events in the map. If delay is greater than or equal internal timer, delay will be 0.
* @param delay Amount of delay.
*/
void DelayEvents(uint32 delay);
EventId ExecuteEvent();
/**
* @name DelayEvents
@@ -228,62 +192,57 @@ public:
/**
* @name DelayEvents
* @brief Delay all events of the same group.
* @param delay Amount of delay.
* @param delay Amount of delay as std::chrono type.
* @param group Group of the events.
*/
void DelayEvents(uint32 delay, uint32 group);
void DelayEvents(Milliseconds delay, GroupIndex group);
/**
* @name EventsEvents
* @brief Delay all events of the same group.
* @param delay Amount of delay.
* @param delay Amount of delay as std::chrono type.
* @param group Group of the events.
*/
void DelayEventsToMax(uint32 delay, uint32 group);
void DelayEventsToMax(Milliseconds delay, GroupIndex group);
/**
* @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);
/**
* @name GetNextEventTime
* @brief Returns closest occurence of specified event.
* @param eventId Wanted event id.
* @return Time of found event.
*/
[[nodiscard]] uint32 GetNextEventTime(uint32 eventId) const;
/**
* @name GetNextEventTime
* @return Time of next event.
*/
[[nodiscard]] uint32 GetNextEventTime() const;
void CancelEventGroup(GroupIndex group);
/**
* @name IsInPhase
* @brief Returns wether event map is in specified phase or not.
* @brief Returns whether event map is in specified phase or not.
* @param phase Wanted phase.
* @return True, if phase of event map contains specified phase.
*/
bool IsInPhase(uint8 phase);
bool IsInPhase(PhaseIndex phase) const;
/**
* @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 HasTimeUntilEvent
* @brief Returns whether an event is scheduled
* @param eventId The id of the event.
* @return True if event is scheduled
*/
bool HasTimeUntilEvent(EventId eventId) const;
private:
/**
@@ -296,23 +255,23 @@ private:
* has reached their time value. Its value is changed in the
* Update method.
*/
uint32 _time{ 0 };
TimePoint _time{ TimePoint::min() };
/**
* @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.
*/
uint32 _phase{0};
PhaseMask _phaseMask{ 0 };
/**
* @name _lastEvent
* @brief Stores information on the most recently executed event
*/
uint32 _lastEvent{0};
Event _lastEvent;
/**
* @name _eventMap