weapons.h

main 312 lines · 8.3 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_WEAPONS_H_69D1993478AA42948E24C0B90B8F5BF5
21#define FS_WEAPONS_H_69D1993478AA42948E24C0B90B8F5BF5
22
23#include "luascript.h"
24#include "player.h"
25#include "baseevents.h"
26#include "combat.h"
27#include "const.h"
28#include "vocation.h"
29
30extern Vocations g_vocations;
31
32class Weapon;
33class WeaponMelee;
34class WeaponDistance;
35class WeaponWand;
36
37using Weapon_ptr = std::unique_ptr<Weapon>;
38
39class Weapons final : public BaseEvents
40{
41 public:
42 Weapons();
43 ~Weapons();
44
45 // non-copyable
46 Weapons(const Weapons&) = delete;
47 Weapons& operator=(const Weapons&) = delete;
48
49 void loadDefaults();
50 const Weapon* getWeapon(const Item* item) const;
51
52 static int32_t getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue);
53 static int32_t getMaxWeaponDamage(uint32_t level, int32_t attackSkill, int32_t attackValue, float attackFactor);
54
55 bool registerLuaEvent(Weapon* event);
56 void clear(bool fromLua) override final;
57
58 private:
59 LuaScriptInterface& getScriptInterface() override;
60 std::string getScriptBaseName() const override;
61 Event_ptr getEvent(const std::string& nodeName) override;
62 bool registerEvent(Event_ptr event, const pugi::xml_node& node) override;
63
64 std::map<uint32_t, Weapon*> weapons;
65
66 LuaScriptInterface scriptInterface { "Weapon Interface" };
67};
68
69class Weapon : public Event
70{
71 public:
72 explicit Weapon(LuaScriptInterface* interface) : Event(interface) {}
73
74 bool configureEvent(const pugi::xml_node& node) override;
75 bool loadFunction(const pugi::xml_attribute&, bool) final {
76 return true;
77 }
78 virtual void configureWeapon(const ItemType& it);
79 virtual bool interruptSwing() const {
80 return false;
81 }
82
83 int32_t playerWeaponCheck(Player* player, Creature* target, uint8_t shootRange) const;
84 static bool useFist(Player* player, Creature* target);
85 virtual bool useWeapon(Player* player, Item* item, Creature* target) const;
86
87 virtual int32_t getWeaponDamage(const Player* player, const Creature* target, const Item* item, bool maxDamage = false) const = 0;
88 virtual int32_t getElementDamage(const Player* player, const Creature* target, const Item* item) const = 0;
89 virtual CombatType_t getElementType() const = 0;
90
91 uint16_t getID() const {
92 return id;
93 }
94 void setID(uint16_t newId) {
95 id = newId;
96 }
97
98 uint32_t getReqLevel() const {
99 return level;
100 }
101 void setRequiredLevel(uint32_t reqlvl) {
102 level = reqlvl;
103 }
104
105 uint32_t getReqMagLv() const {
106 return magLevel;
107 }
108 void setRequiredMagLevel(uint32_t reqlvl) {
109 magLevel = reqlvl;
110 }
111
112 bool isPremium() const {
113 return premium;
114 }
115 void setNeedPremium(bool prem) {
116 premium = prem;
117 }
118
119 bool isWieldedUnproperly() const {
120 return wieldUnproperly;
121 }
122 void setWieldUnproperly(bool unproperly) {
123 wieldUnproperly = unproperly;
124 }
125
126 uint32_t getMana() const {
127 return mana;
128 }
129 void setMana(uint32_t m) {
130 mana = m;
131 }
132
133 uint32_t getManaPercent() const {
134 return manaPercent;
135 }
136 void setManaPercent(uint32_t m) {
137 manaPercent = m;
138 }
139
140 int32_t getHealth() const {
141 return health;
142 }
143 void setHealth(int32_t h) {
144 health = h;
145 }
146
147 uint32_t getHealthPercent() const {
148 return healthPercent;
149 }
150 void setHealthPercent(uint32_t m) {
151 healthPercent = m;
152 }
153
154 uint32_t getSoul() const {
155 return soul;
156 }
157 void setSoul(uint32_t s) {
158 soul = s;
159 }
160
161 uint8_t getBreakChance() const {
162 return breakChance;
163 }
164 void setBreakChance(uint8_t b) {
165 breakChance = b;
166 }
167
168 bool isEnabled() const {
169 return enabled;
170 }
171 void setIsEnabled(bool e) {
172 enabled = e;
173 }
174
175 uint32_t getWieldInfo() const {
176 return wieldInfo;
177 }
178 void setWieldInfo(uint32_t info) {
179 wieldInfo |= info;
180 }
181
182 void addVocWeaponMap(std::string vocName) {
183 int32_t vocationId = g_vocations.getVocationId(vocName);
184 if (vocationId != -1) {
185 vocWeaponMap[vocationId] = true;
186 }
187 }
188
189 const std::string& getVocationString() const {
190 return vocationString;
191 }
192 void setVocationString(const std::string& str) {
193 vocationString = str;
194 }
195
196 WeaponAction_t action = WEAPONACTION_NONE;
197 CombatParams params;
198 WeaponType_t weaponType;
199 std::map<uint16_t, bool> vocWeaponMap;
200
201 protected:
202 void internalUseWeapon(Player* player, Item* item, Creature* target, int32_t damageModifier) const;
203 void internalUseWeapon(Player* player, Item* item, Tile* tile) const;
204
205 uint16_t id = 0;
206
207 private:
208 virtual bool getSkillType(const Player*, const Item*, skills_t&, uint32_t&) const {
209 return false;
210 }
211
212 uint32_t getManaCost(const Player* player) const;
213 int32_t getHealthCost(const Player* player) const;
214
215 uint32_t level = 0;
216 uint32_t magLevel = 0;
217 uint32_t mana = 0;
218 uint32_t manaPercent = 0;
219 uint32_t health = 0;
220 uint32_t healthPercent = 0;
221 uint32_t soul = 0;
222 uint32_t wieldInfo = WIELDINFO_NONE;
223 uint8_t breakChance = 0;
224 bool enabled = true;
225 bool premium = false;
226 bool wieldUnproperly = false;
227 std::string vocationString = "";
228
229 std::string getScriptEventName() const override final;
230
231 bool executeUseWeapon(Player* player, const LuaVariant& var) const;
232 void onUsedWeapon(Player* player, Item* item, Tile* destTile) const;
233
234 static void decrementItemCount(Item* item);
235
236 friend class Combat;
237};
238
239class WeaponMelee final : public Weapon
240{
241 public:
242 explicit WeaponMelee(LuaScriptInterface* interface);
243
244 void configureWeapon(const ItemType& it) override;
245
246 bool useWeapon(Player* player, Item* item, Creature* target) const override;
247
248 int32_t getWeaponDamage(const Player* player, const Creature* target, const Item* item, bool maxDamage = false) const override;
249 int32_t getElementDamage(const Player* player, const Creature* target, const Item* item) const override;
250 CombatType_t getElementType() const override { return elementType; }
251
252 private:
253 bool getSkillType(const Player* player, const Item* item, skills_t& skill, uint32_t& skillpoint) const override;
254
255 CombatType_t elementType = COMBAT_NONE;
256 uint16_t elementDamage = 0;
257};
258
259class WeaponDistance final : public Weapon
260{
261 public:
262 explicit WeaponDistance(LuaScriptInterface* interface);
263
264 void configureWeapon(const ItemType& it) override;
265 bool interruptSwing() const override {
266 return true;
267 }
268
269 bool useWeapon(Player* player, Item* item, Creature* target) const override;
270
271 int32_t getWeaponDamage(const Player* player, const Creature* target, const Item* item, bool maxDamage = false) const override;
272 int32_t getElementDamage(const Player* player, const Creature* target, const Item* item) const override;
273 CombatType_t getElementType() const override { return elementType; }
274
275 private:
276 bool getSkillType(const Player* player, const Item* item, skills_t& skill, uint32_t& skillpoint) const override;
277
278 CombatType_t elementType = COMBAT_NONE;
279 uint16_t elementDamage = 0;
280};
281
282class WeaponWand final : public Weapon
283{
284 public:
285 explicit WeaponWand(LuaScriptInterface* interface) : Weapon(interface) {}
286
287 bool configureEvent(const pugi::xml_node& node) override;
288 void configureWeapon(const ItemType& it) override;
289
290 int32_t getWeaponDamage(const Player* player, const Creature* target, const Item* item, bool maxDamage = false) const override;
291 int32_t getElementDamage(const Player*, const Creature*, const Item*) const override { return 0; }
292 CombatType_t getElementType() const override { return COMBAT_NONE; }
293
294 void setMinChange(int32_t change) {
295 minChange = change;
296 }
297
298 void setMaxChange(int32_t change) {
299 maxChange = change;
300 }
301
302 private:
303 bool getSkillType(const Player*, const Item*, skills_t&, uint32_t&) const override {
304 return false;
305 }
306
307 int32_t minChange = 0;
308 int32_t maxChange = 0;
309};
310
311#endif
312
Top