mirror of
https://github.com/araxiaonline/TrinityCore.git
synced 2026-06-20 15:01:38 -04:00
cd86a015c4
Closes #25012
(cherry picked from commit ce1e2c0f9b)
169 lines
4.4 KiB
C++
169 lines
4.4 KiB
C++
/*
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
* option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "EventMap.h"
|
|
#include "Random.h"
|
|
|
|
void EventMap::Reset()
|
|
{
|
|
_eventMap.clear();
|
|
_time = 0;
|
|
_phase = 0;
|
|
}
|
|
|
|
void EventMap::SetPhase(uint8 phase)
|
|
{
|
|
if (!phase)
|
|
_phase = 0;
|
|
else if (phase <= 8)
|
|
_phase = uint8(1 << (phase - 1));
|
|
}
|
|
|
|
void EventMap::ScheduleEvent(uint32 eventId, Milliseconds time, uint32 group /*= 0*/, uint8 phase /*= 0*/)
|
|
{
|
|
if (group && group <= 8)
|
|
eventId |= (1 << (group + 15));
|
|
|
|
if (phase && phase <= 8)
|
|
eventId |= (1 << (phase + 23));
|
|
|
|
_eventMap.insert(EventStore::value_type(_time + time.count(), eventId));
|
|
}
|
|
|
|
void EventMap::ScheduleEvent(uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group /*= 0*/, uint8 phase /*= 0*/)
|
|
{
|
|
ScheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
|
|
}
|
|
|
|
void EventMap::RescheduleEvent(uint32 eventId, Milliseconds time, uint32 group /*= 0*/, uint8 phase /*= 0*/)
|
|
{
|
|
CancelEvent(eventId);
|
|
ScheduleEvent(eventId, time, group, phase);
|
|
}
|
|
|
|
void EventMap::RescheduleEvent(uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group /*= 0*/, uint8 phase /*= 0*/)
|
|
{
|
|
RescheduleEvent(eventId, randtime(minTime, maxTime), group, phase);
|
|
}
|
|
|
|
void EventMap::Repeat(Milliseconds time)
|
|
{
|
|
_eventMap.insert(EventStore::value_type(_time + time.count(), _lastEvent));
|
|
}
|
|
|
|
void EventMap::Repeat(Milliseconds minTime, Milliseconds maxTime)
|
|
{
|
|
Repeat(randtime(minTime, maxTime));
|
|
}
|
|
|
|
uint32 EventMap::ExecuteEvent()
|
|
{
|
|
while (!Empty())
|
|
{
|
|
EventStore::iterator itr = _eventMap.begin();
|
|
|
|
if (itr->first > _time)
|
|
return 0;
|
|
else if (_phase && (itr->second & 0xFF000000) && !((itr->second >> 24) & _phase))
|
|
_eventMap.erase(itr);
|
|
else
|
|
{
|
|
uint32 eventId = (itr->second & 0x0000FFFF);
|
|
_lastEvent = itr->second; // include phase/group
|
|
_eventMap.erase(itr);
|
|
return eventId;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void EventMap::DelayEvents(Milliseconds delay)
|
|
{
|
|
_time = delay.count() < _time ? _time - delay.count() : 0;
|
|
}
|
|
|
|
void EventMap::DelayEvents(Milliseconds delay, uint32 group)
|
|
{
|
|
if (!group || group > 8 || Empty())
|
|
return;
|
|
|
|
EventStore delayed;
|
|
|
|
for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
|
|
{
|
|
if (itr->second & (1 << (group + 15)))
|
|
{
|
|
delayed.insert(EventStore::value_type(itr->first + delay.count(), itr->second));
|
|
_eventMap.erase(itr++);
|
|
}
|
|
else
|
|
++itr;
|
|
}
|
|
|
|
_eventMap.insert(delayed.begin(), delayed.end());
|
|
}
|
|
|
|
void EventMap::CancelEvent(uint32 eventId)
|
|
{
|
|
if (Empty())
|
|
return;
|
|
|
|
for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
|
|
{
|
|
if (eventId == (itr->second & 0x0000FFFF))
|
|
_eventMap.erase(itr++);
|
|
else
|
|
++itr;
|
|
}
|
|
}
|
|
|
|
void EventMap::CancelEventGroup(uint32 group)
|
|
{
|
|
if (!group || group > 8 || Empty())
|
|
return;
|
|
|
|
for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();)
|
|
{
|
|
if (itr->second & (1 << (group + 15)))
|
|
_eventMap.erase(itr++);
|
|
else
|
|
++itr;
|
|
}
|
|
}
|
|
|
|
uint32 EventMap::GetNextEventTime(uint32 eventId) const
|
|
{
|
|
if (Empty())
|
|
return 0;
|
|
|
|
for (std::pair<uint32 const, uint32> const& itr : _eventMap)
|
|
if (eventId == (itr.second & 0x0000FFFF))
|
|
return itr.first;
|
|
|
|
return 0;
|
|
}
|
|
|
|
uint32 EventMap::GetTimeUntilEvent(uint32 eventId) const
|
|
{
|
|
for (std::pair<uint32 const, uint32> const& itr : _eventMap)
|
|
if (eventId == (itr.second & 0x0000FFFF))
|
|
return itr.first - _time;
|
|
|
|
return std::numeric_limits<uint32>::max();
|
|
}
|