globalevent.h

main 127 lines · 3.2 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_GLOBALEVENT_H_B3FB9B848EA3474B9AFC326873947E3C
21#define FS_GLOBALEVENT_H_B3FB9B848EA3474B9AFC326873947E3C
22#include "baseevents.h"
23
24#include "const.h"
25
26enum GlobalEvent_t {
27 GLOBALEVENT_NONE,
28 GLOBALEVENT_TIMER,
29
30 GLOBALEVENT_STARTUP,
31 GLOBALEVENT_SHUTDOWN,
32 GLOBALEVENT_RECORD,
33};
34
35class GlobalEvent;
36using GlobalEvent_ptr = std::unique_ptr<GlobalEvent>;
37using GlobalEventMap = std::map<std::string, GlobalEvent>;
38
39class GlobalEvents final : public BaseEvents
40{
41 public:
42 GlobalEvents();
43 ~GlobalEvents();
44
45 // non-copyable
46 GlobalEvents(const GlobalEvents&) = delete;
47 GlobalEvents& operator=(const GlobalEvents&) = delete;
48
49 void startup() const;
50
51 void timer();
52 void think();
53 void execute(GlobalEvent_t type) const;
54
55 GlobalEventMap getEventMap(GlobalEvent_t type);
56 static void clearMap(GlobalEventMap& map, bool fromLua);
57
58 bool registerLuaEvent(GlobalEvent* event);
59 void clear(bool fromLua) override final;
60
61 private:
62 std::string getScriptBaseName() const override {
63 return "globalevents";
64 }
65
66 Event_ptr getEvent(const std::string& nodeName) override;
67 bool registerEvent(Event_ptr event, const pugi::xml_node& node) override;
68
69 LuaScriptInterface& getScriptInterface() override {
70 return scriptInterface;
71 }
72 LuaScriptInterface scriptInterface;
73
74 GlobalEventMap thinkMap, serverMap, timerMap;
75 int32_t thinkEventId = 0, timerEventId = 0;
76};
77
78class GlobalEvent final : public Event
79{
80 public:
81 explicit GlobalEvent(LuaScriptInterface* interface);
82
83 bool configureEvent(const pugi::xml_node& node) override;
84
85 bool executeRecord(uint32_t current, uint32_t old);
86 bool executeEvent() const;
87
88 GlobalEvent_t getEventType() const {
89 return eventType;
90 }
91 void setEventType(GlobalEvent_t type) {
92 eventType = type;
93 }
94
95 const std::string& getName() const {
96 return name;
97 }
98 void setName(std::string eventName) {
99 name = eventName;
100 }
101
102 uint32_t getInterval() const {
103 return interval;
104 }
105 void setInterval(uint32_t eventInterval) {
106 interval |= eventInterval;
107 }
108
109 int64_t getNextExecution() const {
110 return nextExecution;
111 }
112 void setNextExecution(int64_t time) {
113 nextExecution = time;
114 }
115
116 private:
117 GlobalEvent_t eventType = GLOBALEVENT_NONE;
118
119 std::string getScriptEventName() const override;
120
121 std::string name;
122 int64_t nextExecution = 0;
123 uint32_t interval = 0;
124};
125
126#endif
127
Top