1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include "EventProcessor.h" |
---|
22 | |
---|
23 | EventProcessor::EventProcessor() |
---|
24 | { |
---|
25 | m_time = 0; |
---|
26 | m_aborting = false; |
---|
27 | } |
---|
28 | |
---|
29 | EventProcessor::~EventProcessor() |
---|
30 | { |
---|
31 | KillAllEvents(true); |
---|
32 | } |
---|
33 | |
---|
34 | void EventProcessor::Update(uint32 p_time) |
---|
35 | { |
---|
36 | // update time |
---|
37 | m_time += p_time; |
---|
38 | |
---|
39 | // main event loop |
---|
40 | EventList::iterator i; |
---|
41 | while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time) |
---|
42 | { |
---|
43 | // get and remove event from queue |
---|
44 | BasicEvent* Event = i->second; |
---|
45 | m_events.erase(i); |
---|
46 | |
---|
47 | if (!Event->to_Abort) |
---|
48 | { |
---|
49 | if (Event->Execute(m_time, p_time)) |
---|
50 | { |
---|
51 | // completely destroy event if it is not re-added |
---|
52 | delete Event; |
---|
53 | } |
---|
54 | } |
---|
55 | else |
---|
56 | { |
---|
57 | Event->Abort(m_time); |
---|
58 | delete Event; |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | void EventProcessor::KillAllEvents(bool force) |
---|
64 | { |
---|
65 | // prevent event insertions |
---|
66 | m_aborting = true; |
---|
67 | |
---|
68 | // first, abort all existing events |
---|
69 | for (EventList::iterator i = m_events.begin(); i != m_events.end();) |
---|
70 | { |
---|
71 | EventList::iterator i_old = i; |
---|
72 | ++i; |
---|
73 | |
---|
74 | i_old->second->to_Abort = true; |
---|
75 | i_old->second->Abort(m_time); |
---|
76 | if(force || i_old->second->IsDeletable()) |
---|
77 | { |
---|
78 | delete i_old->second; |
---|
79 | |
---|
80 | if(!force) // need per-element cleanup |
---|
81 | m_events.erase (i_old); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // fast clear event list (in force case) |
---|
86 | if(force) |
---|
87 | m_events.clear(); |
---|
88 | } |
---|
89 | |
---|
90 | void EventProcessor::AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime) |
---|
91 | { |
---|
92 | if (set_addtime) Event->m_addTime = m_time; |
---|
93 | Event->m_execTime = e_time; |
---|
94 | m_events.insert(std::pair<uint64, BasicEvent*>(e_time, Event)); |
---|
95 | } |
---|
96 | |
---|
97 | uint64 EventProcessor::CalculateTime(uint64 t_offset) |
---|
98 | { |
---|
99 | return(m_time + t_offset); |
---|
100 | } |
---|