creatureevent.h

main 133 lines · 4.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_CREATUREEVENT_H_73FCAF4608CB41399D53C919316646A9
21#define FS_CREATUREEVENT_H_73FCAF4608CB41399D53C919316646A9
22
23#include "luascript.h"
24#include "baseevents.h"
25#include "enums.h"
26
27class CreatureEvent;
28using CreatureEvent_ptr = std::unique_ptr<CreatureEvent>;
29
30enum CreatureEventType_t {
31 CREATURE_EVENT_NONE,
32 CREATURE_EVENT_LOGIN,
33 CREATURE_EVENT_LOGOUT,
34 CREATURE_EVENT_THINK,
35 CREATURE_EVENT_PREPAREDEATH,
36 CREATURE_EVENT_DEATH,
37 CREATURE_EVENT_KILL,
38 CREATURE_EVENT_ADVANCE,
39 CREATURE_EVENT_MODALWINDOW,
40 CREATURE_EVENT_TEXTEDIT,
41 CREATURE_EVENT_HEALTHCHANGE,
42 CREATURE_EVENT_MANACHANGE,
43 CREATURE_EVENT_EXTENDED_OPCODE, // otclient additional network opcodes
44};
45
46class CreatureEvent final : public Event
47{
48 public:
49 explicit CreatureEvent(LuaScriptInterface* interface);
50
51 bool configureEvent(const pugi::xml_node& node) override;
52
53 CreatureEventType_t getEventType() const {
54 return type;
55 }
56 void setEventType(CreatureEventType_t eventType) {
57 type = eventType;
58 }
59 const std::string& getName() const {
60 return eventName;
61 }
62 void setName(const std::string& name) {
63 eventName = name;
64 }
65 bool isLoaded() const {
66 return loaded;
67 }
68 void setLoaded(bool b) {
69 loaded = b;
70 }
71
72 void clearEvent();
73 void copyEvent(CreatureEvent* creatureEvent);
74
75 //scripting
76 bool executeOnLogin(Player* player) const;
77 bool executeOnLogout(Player* player) const;
78 bool executeOnThink(Creature* creature, uint32_t interval);
79 bool executeOnPrepareDeath(Creature* creature, Creature* killer);
80 bool executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified);
81 void executeOnKill(Creature* creature, Creature* target);
82 bool executeAdvance(Player* player, skills_t, uint32_t, uint32_t);
83 void executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId);
84 bool executeTextEdit(Player* player, Item* item, const std::string& text);
85 void executeHealthChange(Creature* creature, Creature* attacker, CombatDamage& damage);
86 void executeManaChange(Creature* creature, Creature* attacker, CombatDamage& damage);
87 void executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer);
88 //
89
90 private:
91 std::string getScriptEventName() const override;
92
93 std::string eventName;
94 CreatureEventType_t type;
95 bool loaded;
96};
97
98class CreatureEvents final : public BaseEvents
99{
100 public:
101 CreatureEvents();
102
103 // non-copyable
104 CreatureEvents(const CreatureEvents&) = delete;
105 CreatureEvents& operator=(const CreatureEvents&) = delete;
106
107 // global events
108 bool playerLogin(Player* player) const;
109 bool playerLogout(Player* player) const;
110 bool playerAdvance(Player* player, skills_t, uint32_t, uint32_t);
111
112 CreatureEvent* getEventByName(const std::string& name, bool forceLoaded = true);
113
114 bool registerLuaEvent(CreatureEvent* event);
115 void clear(bool fromLua) override final;
116
117 void removeInvalidEvents();
118
119 private:
120 LuaScriptInterface& getScriptInterface() override;
121 std::string getScriptBaseName() const override;
122 Event_ptr getEvent(const std::string& nodeName) override;
123 bool registerEvent(Event_ptr event, const pugi::xml_node& node) override;
124
125 //creature events
126 using CreatureEventMap = std::map<std::string, CreatureEvent>;
127 CreatureEventMap creatureEvents;
128
129 LuaScriptInterface scriptInterface;
130};
131
132#endif
133
Top