TrinityCore
EventProcessor.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __EVENTPROCESSOR_H
19#define __EVENTPROCESSOR_H
20
21#include "Define.h"
22#include "Duration.h"
23#include "Random.h"
24#include <map>
25#include <type_traits>
26
27class EventProcessor;
28
29// Note. All times are in milliseconds here.
30
32{
33 friend class EventProcessor;
34
35 enum class AbortState : uint8
36 {
37 STATE_RUNNING,
38 STATE_ABORT_SCHEDULED,
39 STATE_ABORTED
40 };
41
42 public:
44 : m_abortState(AbortState::STATE_RUNNING), m_addTime(0), m_execTime(0) { }
45
46 virtual ~BasicEvent() { } // override destructor to perform some actions on event removal
47
48 // this method executes when the event is triggered
49 // return false if event does not want to be deleted
50 // e_time is execution time, p_time is update interval
51 virtual bool Execute(uint64 /*e_time*/, uint32 /*p_time*/) { return true; }
52
53 virtual bool IsDeletable() const { return true; } // this event can be safely deleted
54
55 virtual void Abort(uint64 /*e_time*/) { } // this method executes when the event is aborted
56
57 // Aborts the event at the next update tick
58 void ScheduleAbort();
59
60 private:
61 void SetAborted();
62 bool IsRunning() const { return (m_abortState == AbortState::STATE_RUNNING); }
63 bool IsAbortScheduled() const { return (m_abortState == AbortState::STATE_ABORT_SCHEDULED); }
64 bool IsAborted() const { return (m_abortState == AbortState::STATE_ABORTED); }
65
66 AbortState m_abortState; // set by externals when the event is aborted, aborted events don't execute
67
68 // these can be used for time offset control
69 uint64 m_addTime; // time when the event was added to queue, filled by event handler
70 uint64 m_execTime; // planned time of next execution, filled by event handler
71};
72
73template<typename T>
75{
76public:
77 LambdaBasicEvent(T&& callback) : BasicEvent(), _callback(std::move(callback)) { }
78
79 bool Execute(uint64, uint32) override
80 {
81 _callback();
82 return true;
83 }
84
85private:
86
88};
89
90template<typename T>
91using is_lambda_event = std::enable_if_t<!std::is_base_of_v<BasicEvent, std::remove_pointer_t<std::remove_cvref_t<T>>>>;
92
94{
95 public:
96 EventProcessor() : m_time(0) { }
98
99 void Update(uint32 p_time);
100 void KillAllEvents(bool force);
101
102 void AddEvent(BasicEvent* event, Milliseconds e_time, bool set_addtime = true);
103 template<typename T>
104 is_lambda_event<T> AddEvent(T&& event, Milliseconds e_time, bool set_addtime = true) { AddEvent(new LambdaBasicEvent<T>(std::move(event)), e_time, set_addtime); }
105 void AddEventAtOffset(BasicEvent* event, Milliseconds offset) { AddEvent(event, CalculateTime(offset)); }
106 void AddEventAtOffset(BasicEvent* event, Milliseconds offset, Milliseconds offset2) { AddEvent(event, CalculateTime(randtime(offset, offset2))); }
107 template<typename T>
108 is_lambda_event<T> AddEventAtOffset(T&& event, Milliseconds offset) { AddEventAtOffset(new LambdaBasicEvent<T>(std::move(event)), offset); }
109 template<typename T>
110 is_lambda_event<T> AddEventAtOffset(T&& event, Milliseconds offset, Milliseconds offset2) { AddEventAtOffset(new LambdaBasicEvent<T>(std::move(event)), offset, offset2); }
111 void ModifyEventTime(BasicEvent* event, Milliseconds newTime);
112 Milliseconds CalculateTime(Milliseconds t_offset) const { return Milliseconds(m_time) + t_offset; }
113 std::multimap<uint64, BasicEvent*> const& GetEvents() const { return m_events; }
114
115 protected:
117 std::multimap<uint64, BasicEvent*> m_events;
118};
119
120#endif
uint8_t uint8
Definition: Define.h:144
#define TC_COMMON_API
Definition: Define.h:99
uint64_t uint64
Definition: Define.h:141
uint32_t uint32
Definition: Define.h:142
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
std::enable_if_t<!std::is_base_of_v< BasicEvent, std::remove_pointer_t< std::remove_cvref_t< T > > > > is_lambda_event
Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition: Random.cpp:62
bool IsAbortScheduled() const
bool IsAborted() const
virtual void Abort(uint64)
virtual ~BasicEvent()
virtual bool IsDeletable() const
virtual bool Execute(uint64, uint32)
uint64 m_addTime
bool IsRunning() const
uint64 m_execTime
AbortState m_abortState
std::multimap< uint64, BasicEvent * > m_events
std::multimap< uint64, BasicEvent * > const & GetEvents() const
is_lambda_event< T > AddEvent(T &&event, Milliseconds e_time, bool set_addtime=true)
is_lambda_event< T > AddEventAtOffset(T &&event, Milliseconds offset)
void AddEventAtOffset(BasicEvent *event, Milliseconds offset)
void AddEventAtOffset(BasicEvent *event, Milliseconds offset, Milliseconds offset2)
is_lambda_event< T > AddEventAtOffset(T &&event, Milliseconds offset, Milliseconds offset2)
Milliseconds CalculateTime(Milliseconds t_offset) const
LambdaBasicEvent(T &&callback)
bool Execute(uint64, uint32) override
void Update(VignetteData &vignette, WorldObject const *owner)
Definition: Vignette.cpp:93
STL namespace.