Files
ForeverCore/src/common/Utilities/EventProcessor.cpp
T

134 lines
3.5 KiB
C++
Raw Normal View History

/*
2020-01-02 06:44:10 +01:00
* 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/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "EventProcessor.h"
#include "Errors.h"
2009-10-17 15:51:44 -07:00
void BasicEvent::ScheduleAbort()
2008-10-02 16:23:55 -05:00
{
ASSERT(IsRunning()
&& "Tried to scheduled the abortion of an event twice!");
m_abortState = AbortState::STATE_ABORT_SCHEDULED;
}
void BasicEvent::SetAborted()
{
ASSERT(!IsAborted()
&& "Tried to abort an already aborted event!");
m_abortState = AbortState::STATE_ABORTED;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
EventProcessor::~EventProcessor()
{
KillAllEvents(true);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void EventProcessor::Update(uint32 p_time)
{
// update time
m_time += p_time;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// main event loop
2017-05-11 22:29:51 +02:00
std::multimap<uint64, BasicEvent*>::iterator i;
2008-10-02 16:23:55 -05:00
while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time)
{
// get and remove event from queue
BasicEvent* event = i->second;
2008-10-02 16:23:55 -05:00
m_events.erase(i);
2009-10-17 15:51:44 -07:00
if (event->IsRunning())
2008-10-02 16:23:55 -05:00
{
if (event->Execute(m_time, p_time))
2008-10-02 16:23:55 -05:00
{
// completely destroy event if it is not re-added
delete event;
2008-10-02 16:23:55 -05:00
}
continue;
}
if (event->IsAbortScheduled())
{
event->Abort(m_time);
// Mark the event as aborted
event->SetAborted();
2008-10-02 16:23:55 -05:00
}
if (event->IsDeletable())
2008-10-02 16:23:55 -05:00
{
delete event;
continue;
2008-10-02 16:23:55 -05:00
}
// Reschedule non deletable events to be checked at
// the next update tick
AddEvent(event, CalculateTime(1), false);
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
void EventProcessor::KillAllEvents(bool force)
2008-10-02 16:23:55 -05:00
{
for (auto itr = m_events.begin(); itr != m_events.end();)
2008-10-02 16:23:55 -05:00
{
// Abort events which weren't aborted already
if (!itr->second->IsAborted())
{
itr->second->SetAborted();
itr->second->Abort(m_time);
}
2009-10-17 15:51:44 -07:00
// Skip non-deletable events when we are
// not forcing the event cancellation.
if (!force && !itr->second->IsDeletable())
{
++itr;
continue;
}
delete itr->second;
if (force)
++itr; // Clear the whole container when forcing
else
itr = m_events.erase(itr);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-09-29 12:43:05 +02:00
if (force)
m_events.clear();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-11-30 20:45:44 +01:00
void EventProcessor::AddEvent(BasicEvent* event, uint64 e_time, bool set_addtime)
2008-10-02 16:23:55 -05:00
{
if (set_addtime)
2017-11-30 20:45:44 +01:00
event->m_addTime = m_time;
event->m_execTime = e_time;
m_events.insert(std::pair<uint64, BasicEvent*>(e_time, event));
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-11-30 20:45:44 +01:00
void EventProcessor::ModifyEventTime(BasicEvent* event, uint64 newTime)
{
for (auto itr = m_events.begin(); itr != m_events.end(); ++itr)
{
2017-11-30 20:45:44 +01:00
if (itr->second != event)
continue;
2017-11-30 20:45:44 +01:00
event->m_execTime = newTime;
m_events.erase(itr);
2017-11-30 20:45:44 +01:00
m_events.insert(std::pair<uint64, BasicEvent*>(newTime, event));
break;
}
}