Core/Scripts: Commented EventMap and added GetNextEventTime to know when will next event with given id run

This commit is contained in:
Shauren
2011-06-30 22:56:32 +02:00
parent 049ed754ba
commit f5102b6e6b
2 changed files with 63 additions and 34 deletions
+62 -33
View File
@@ -313,75 +313,85 @@ const T& RAND(const T& v1, const T& v2, const T& v3, const T& v4, const T& v5, c
class EventMap : private std::map<uint32, uint32> class EventMap : private std::map<uint32, uint32>
{ {
private:
uint32 m_time, m_phase;
public: public:
explicit EventMap(): m_time(0), m_phase(0) {} EventMap() : _time(0), _phase(0) {}
uint32 GetTimer() const { return m_time; } // Returns current timer value, does not represent real dates/times
uint32 GetTimer() const { return _time; }
void Reset() { clear(); m_time = 0; m_phase = 0; } // Removes all events and clears phase
void Reset() { clear(); _time = 0; _phase = 0; }
void Update(uint32 time) { m_time += time; } void Update(uint32 time) { _time += time; }
uint32 GetPhaseMask() const { return (m_phase >> 24) & 0xFF; } uint32 GetPhaseMask() const { return (_phase >> 24) & 0xFF; }
// Sets event phase, must be in range 1 - 8
void SetPhase(uint32 phase) void SetPhase(uint32 phase)
{ {
if (phase && phase < 9) if (phase && phase < 9)
m_phase = (1 << (phase + 24)); _phase = (1 << (phase + 24));
} }
void ScheduleEvent(uint32 eventId, uint32 time, uint32 gcd = 0, uint32 phase = 0) // Creates new event entry in map with given id, time, group if given (1 - 8) and phase if given (1 - 8)
// 0 for group/phase means it belongs to no group or runs in all phases
void ScheduleEvent(uint32 eventId, uint32 time, uint32 groupId = 0, uint32 phase = 0)
{ {
time += m_time; time += _time;
if (gcd && gcd < 9) if (groupId && groupId < 9)
eventId |= (1 << (gcd + 16)); eventId |= (1 << (groupId + 16));
if (phase && phase < 9) if (phase && phase < 9)
eventId |= (1 << (phase + 24)); eventId |= (1 << (phase + 24));
iterator itr = find(time); const_iterator itr = find(time);
while (itr != end()) while (itr != end())
{ {
++time; ++time;
itr = find(time); itr = find(time);
} }
insert(std::make_pair(time, eventId)); insert(std::make_pair(time, eventId));
} }
void RescheduleEvent(uint32 eventId, uint32 time, uint32 gcd = 0, uint32 phase = 0) // Removes event with specified id and creates new entry for it
void RescheduleEvent(uint32 eventId, uint32 time, uint32 groupId = 0, uint32 phase = 0)
{ {
CancelEvent(eventId); CancelEvent(eventId);
ScheduleEvent(eventId, time, gcd, phase); ScheduleEvent(eventId, time, groupId, phase);
} }
// Reschedules closest event
void RepeatEvent(uint32 time) void RepeatEvent(uint32 time)
{ {
if (empty()) if (empty())
return; return;
uint32 eventId = begin()->second; uint32 eventId = begin()->second;
erase(begin()); erase(begin());
time += m_time; time += _time;
iterator itr = find(time); const_iterator itr = find(time);
while (itr != end()) while (itr != end())
{ {
++time; ++time;
itr = find(time); itr = find(time);
} }
insert(std::make_pair(time, eventId)); insert(std::make_pair(time, eventId));
} }
// Removes first event
void PopEvent() void PopEvent()
{ {
erase(begin()); erase(begin());
} }
// Gets next event id to execute and removes it from map
uint32 ExecuteEvent() uint32 ExecuteEvent()
{ {
while (!empty()) while (!empty())
{ {
if (begin()->first > m_time) if (begin()->first > _time)
return 0; return 0;
else if (m_phase && (begin()->second & 0xFF000000) && !(begin()->second & m_phase)) else if (_phase && (begin()->second & 0xFF000000) && !(begin()->second & _phase))
erase(begin()); erase(begin());
else else
{ {
@@ -393,39 +403,41 @@ class EventMap : private std::map<uint32, uint32>
return 0; return 0;
} }
// Gets next event id to execute
uint32 GetEvent() uint32 GetEvent()
{ {
while (!empty()) while (!empty())
{ {
if (begin()->first > m_time) if (begin()->first > _time)
return 0; return 0;
else if (m_phase && (begin()->second & 0xFF000000) && !(begin()->second & m_phase)) else if (_phase && (begin()->second & 0xFF000000) && !(begin()->second & _phase))
erase(begin()); erase(begin());
else else
return (begin()->second & 0x0000FFFF); return (begin()->second & 0x0000FFFF);
} }
return 0; return 0;
} }
// Delay all events // Delay all events
void DelayEvents(uint32 delay) void DelayEvents(uint32 delay)
{ {
if (delay < m_time) if (delay < _time)
m_time -= delay; _time -= delay;
else else
m_time = 0; _time = 0;
} }
// Delay all events having the specified Global Cooldown. // Delay all events having the specified Group
void DelayEvents(uint32 delay, uint32 gcd) void DelayEvents(uint32 delay, uint32 groupId)
{ {
uint32 nextTime = m_time + delay; uint32 nextTime = _time + delay;
gcd = (1 << (gcd + 16)); uint32 groupMask = (1 << (groupId + 16));
for (iterator itr = begin(); itr != end() && itr->first < nextTime;) for (iterator itr = begin(); itr != end() && itr->first < nextTime;)
{ {
if (itr->second & gcd) if (itr->second & groupMask)
{ {
ScheduleEvent(itr->second, itr->first-m_time+delay); ScheduleEvent(itr->second, itr->first - _time + delay);
erase(itr); erase(itr);
itr = begin(); itr = begin();
} }
@@ -434,6 +446,7 @@ class EventMap : private std::map<uint32, uint32>
} }
} }
// Cancel events with specified id
void CancelEvent(uint32 eventId) void CancelEvent(uint32 eventId)
{ {
for (iterator itr = begin(); itr != end();) for (iterator itr = begin(); itr != end();)
@@ -448,13 +461,14 @@ class EventMap : private std::map<uint32, uint32>
} }
} }
void CancelEventsByGCD(uint32 gcd) // Cancel events belonging to specified group
void CancelEventGroup(uint32 groupId)
{ {
gcd = (1 << (gcd + 16)); uint32 groupMask = (1 << (groupId + 16));
for (iterator itr = begin(); itr != end();) for (iterator itr = begin(); itr != end();)
{ {
if (itr->second & gcd) if (itr->second & groupMask)
{ {
erase(itr); erase(itr);
itr = begin(); itr = begin();
@@ -463,6 +477,21 @@ class EventMap : private std::map<uint32, uint32>
++itr; ++itr;
} }
} }
// Returns time of next event to execute
// To get how much time remains substract _time
uint32 GetNextEventTime(uint32 eventId) const
{
for (const_iterator itr = begin(); itr != end(); ++itr)
if (eventId == (itr->second & 0x0000FFFF))
return itr->first;
return 0;
}
private:
uint32 _time;
uint32 _phase;
}; };
enum AITarget enum AITarget
@@ -402,7 +402,7 @@ class boss_blood_queen_lana_thel : public CreatureScript
DoStopAttack(); DoStopAttack();
me->SetReactState(REACT_PASSIVE); me->SetReactState(REACT_PASSIVE);
events.DelayEvents(7000, EVENT_GROUP_NORMAL); events.DelayEvents(7000, EVENT_GROUP_NORMAL);
events.CancelEventsByGCD(EVENT_GROUP_CANCELLABLE); events.CancelEventGroup(EVENT_GROUP_CANCELLABLE);
me->GetMotionMaster()->MovePoint(POINT_CENTER, centerPos); me->GetMotionMaster()->MovePoint(POINT_CENTER, centerPos);
break; break;
case EVENT_AIR_START_FLYING: case EVENT_AIR_START_FLYING: