mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/EventProcessor: Wait for non deletable events to get deletable.
* Immediate deletion caused issues with the SpellEvent, so we delete it at one of the next update ticks now. * Only affects the unforced cancellation of events. * Ref #16675
This commit is contained in:
@@ -17,11 +17,20 @@
|
||||
*/
|
||||
|
||||
#include "EventProcessor.h"
|
||||
#include "Errors.h"
|
||||
|
||||
EventProcessor::EventProcessor()
|
||||
void BasicEvent::ScheduleAbort()
|
||||
{
|
||||
m_time = 0;
|
||||
m_aborting = false;
|
||||
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;
|
||||
}
|
||||
|
||||
EventProcessor::~EventProcessor()
|
||||
@@ -39,37 +48,47 @@ void EventProcessor::Update(uint32 p_time)
|
||||
while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time)
|
||||
{
|
||||
// get and remove event from queue
|
||||
BasicEvent* Event = i->second;
|
||||
BasicEvent* event = i->second;
|
||||
m_events.erase(i);
|
||||
|
||||
if (!Event->to_Abort)
|
||||
if (event->IsRunning())
|
||||
{
|
||||
if (Event->Execute(m_time, p_time))
|
||||
if (event->Execute(m_time, p_time))
|
||||
{
|
||||
// completely destroy event if it is not re-added
|
||||
delete Event;
|
||||
delete event;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
||||
if (event->IsAbortScheduled())
|
||||
{
|
||||
Event->Abort(m_time);
|
||||
delete Event;
|
||||
event->Abort(m_time);
|
||||
// Mark the event as aborted
|
||||
event->SetAborted();
|
||||
}
|
||||
|
||||
if (event->IsDeletable())
|
||||
{
|
||||
delete event;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Reschedule non deletable events to be checked at
|
||||
// the next update tick
|
||||
AddEvent(event, CalculateTime(1), false);
|
||||
}
|
||||
}
|
||||
|
||||
void EventProcessor::KillAllEvents(bool force)
|
||||
{
|
||||
// prevent event insertions
|
||||
m_aborting = true;
|
||||
|
||||
// first, abort all existing events
|
||||
for (EventList::iterator i = m_events.begin(); i != m_events.end();)
|
||||
{
|
||||
EventList::iterator i_old = i;
|
||||
++i;
|
||||
|
||||
i_old->second->to_Abort = true;
|
||||
i_old->second->SetAborted();
|
||||
i_old->second->Abort(m_time);
|
||||
if (force || i_old->second->IsDeletable())
|
||||
{
|
||||
@@ -87,7 +106,8 @@ void EventProcessor::KillAllEvents(bool force)
|
||||
|
||||
void EventProcessor::AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime)
|
||||
{
|
||||
if (set_addtime) Event->m_addTime = m_time;
|
||||
if (set_addtime)
|
||||
Event->m_addTime = m_time;
|
||||
Event->m_execTime = e_time;
|
||||
m_events.insert(std::pair<uint64, BasicEvent*>(e_time, Event));
|
||||
}
|
||||
@@ -96,4 +116,3 @@ uint64 EventProcessor::CalculateTime(uint64 t_offset) const
|
||||
{
|
||||
return(m_time + t_offset);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,20 +20,27 @@
|
||||
#define __EVENTPROCESSOR_H
|
||||
|
||||
#include "Define.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
class EventProcessor;
|
||||
|
||||
// Note. All times are in milliseconds here.
|
||||
|
||||
class TC_COMMON_API BasicEvent
|
||||
{
|
||||
friend class EventProcessor;
|
||||
|
||||
enum class AbortState : uint8
|
||||
{
|
||||
STATE_RUNNING,
|
||||
STATE_ABORT_SCHEDULED,
|
||||
STATE_ABORTED
|
||||
};
|
||||
|
||||
public:
|
||||
BasicEvent()
|
||||
{
|
||||
to_Abort = false;
|
||||
m_addTime = 0;
|
||||
m_execTime = 0;
|
||||
}
|
||||
: m_abortState(AbortState::STATE_RUNNING), m_addTime(0), m_execTime(0) { }
|
||||
|
||||
virtual ~BasicEvent() { } // override destructor to perform some actions on event removal
|
||||
|
||||
// this method executes when the event is triggered
|
||||
@@ -45,8 +52,16 @@ class TC_COMMON_API BasicEvent
|
||||
|
||||
virtual void Abort(uint64 /*e_time*/) { } // this method executes when the event is aborted
|
||||
|
||||
bool to_Abort; // set by externals when the event is aborted, aborted events don't execute
|
||||
// and get Abort call when deleted
|
||||
// Aborts the event at the next update tick
|
||||
void ScheduleAbort();
|
||||
|
||||
private:
|
||||
void SetAborted();
|
||||
bool IsRunning() const { return (m_abortState == AbortState::STATE_RUNNING); }
|
||||
bool IsAbortScheduled() const { return (m_abortState == AbortState::STATE_ABORT_SCHEDULED); }
|
||||
bool IsAborted() const { return (m_abortState == AbortState::STATE_ABORTED); }
|
||||
|
||||
AbortState m_abortState; // set by externals when the event is aborted, aborted events don't execute
|
||||
|
||||
// these can be used for time offset control
|
||||
uint64 m_addTime; // time when the event was added to queue, filled by event handler
|
||||
@@ -58,16 +73,17 @@ typedef std::multimap<uint64, BasicEvent*> EventList;
|
||||
class TC_COMMON_API EventProcessor
|
||||
{
|
||||
public:
|
||||
EventProcessor();
|
||||
EventProcessor() : m_time(0) { }
|
||||
~EventProcessor();
|
||||
|
||||
void Update(uint32 p_time);
|
||||
void KillAllEvents(bool force);
|
||||
void AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime = true);
|
||||
uint64 CalculateTime(uint64 t_offset) const;
|
||||
|
||||
protected:
|
||||
uint64 m_time;
|
||||
EventList m_events;
|
||||
bool m_aborting;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -238,7 +238,7 @@ void Vehicle::RemoveAllPassengers()
|
||||
while (!_pendingJoinEvents.empty())
|
||||
{
|
||||
VehicleJoinEvent* e = _pendingJoinEvents.front();
|
||||
e->to_Abort = true;
|
||||
e->ScheduleAbort();
|
||||
e->Target = eventVehicle;
|
||||
_pendingJoinEvents.pop_front();
|
||||
}
|
||||
@@ -429,7 +429,7 @@ bool Vehicle::AddPassenger(Unit* unit, int8 seatId)
|
||||
|
||||
if (seat == Seats.end()) // no available seat
|
||||
{
|
||||
e->to_Abort = true;
|
||||
e->ScheduleAbort();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -441,7 +441,7 @@ bool Vehicle::AddPassenger(Unit* unit, int8 seatId)
|
||||
seat = Seats.find(seatId);
|
||||
if (seat == Seats.end())
|
||||
{
|
||||
e->to_Abort = true;
|
||||
e->ScheduleAbort();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -701,7 +701,7 @@ void Vehicle::RemovePendingEventsForSeat(int8 seatId)
|
||||
{
|
||||
if ((*itr)->Seat->first == seatId)
|
||||
{
|
||||
(*itr)->to_Abort = true;
|
||||
(*itr)->ScheduleAbort();
|
||||
_pendingJoinEvents.erase(itr++);
|
||||
}
|
||||
else
|
||||
@@ -726,7 +726,7 @@ void Vehicle::RemovePendingEventsForPassenger(Unit* passenger)
|
||||
{
|
||||
if ((*itr)->Passenger == passenger)
|
||||
{
|
||||
(*itr)->to_Abort = true;
|
||||
(*itr)->ScheduleAbort();
|
||||
_pendingJoinEvents.erase(itr++);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -470,7 +470,7 @@ void Aura::_Remove(AuraRemoveMode removeMode)
|
||||
|
||||
if (m_dropEvent)
|
||||
{
|
||||
m_dropEvent->to_Abort = true;
|
||||
m_dropEvent->ScheduleAbort();
|
||||
m_dropEvent = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user