creatures.h

main 148 lines · 4.9 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef CREATURES_H
24#define CREATURES_H
25
26#include "declarations.h"
27#include <framework/luaengine/luaobject.h>
28#include "outfit.h"
29
30enum CreatureAttr : uint8
31{
32 CreatureAttrPosition = 0,
33 CreatureAttrName = 1,
34 CreatureAttrOutfit = 2,
35 CreatureAttrSpawnTime = 3,
36 CreatureAttrDir = 4,
37 CreatureAttrRace = 5
38};
39
40enum CreatureRace : uint8
41{
42 CreatureRaceNpc = 0,
43 CreatureRaceMonster = 1
44};
45
46enum SpawnAttr : uint8
47{
48 SpawnAttrRadius = 0,
49 SpawnAttrCenter = 1,
50};
51
52class Spawn : public LuaObject
53{
54public:
55 Spawn() = default;
56 Spawn(int32 radius) { setRadius(radius); }
57
58 void setRadius(int32 r) { m_attribs.set(SpawnAttrRadius, r) ;}
59 int32 getRadius() { return m_attribs.get<int32>(SpawnAttrRadius); }
60
61 void setCenterPos(const Position& pos) { m_attribs.set(SpawnAttrCenter, pos); }
62 Position getCenterPos() { return m_attribs.get<Position>(SpawnAttrCenter); }
63
64 std::vector<CreatureTypePtr> getCreatures();
65 void addCreature(const Position& placePos, const CreatureTypePtr& cType);
66 void removeCreature(const Position& pos);
67 void clear() { m_creatures.clear(); }
68
69protected:
70 void load(TiXmlElement* node);
71 void save(TiXmlElement* node);
72
73private:
74 stdext::dynamic_storage<uint8> m_attribs;
75 std::unordered_map<Position, CreatureTypePtr, PositionHasher> m_creatures;
76 friend class CreatureManager;
77};
78
79class CreatureType : public LuaObject
80{
81public:
82 CreatureType() = default;
83 CreatureType(const std::string& name) { setName(name); }
84
85 void setSpawnTime(int32 spawnTime) { m_attribs.set(CreatureAttrSpawnTime, spawnTime); }
86 int32 getSpawnTime() { return m_attribs.get<int32>(CreatureAttrSpawnTime); }
87
88 void setName(const std::string& name) { m_attribs.set(CreatureAttrName, name); }
89 std::string getName() { return m_attribs.get<std::string>(CreatureAttrName); }
90
91 void setOutfit(const Outfit& o) { m_attribs.set(CreatureAttrOutfit, o); }
92 Outfit getOutfit() { return m_attribs.get<Outfit>(CreatureAttrOutfit); }
93
94 void setDirection(Otc::Direction dir) { m_attribs.set(CreatureAttrDir, dir); }
95 Otc::Direction getDirection() { return m_attribs.get<Otc::Direction>(CreatureAttrDir); }
96
97 void setRace(CreatureRace race) { m_attribs.set(CreatureAttrRace, race); }
98 CreatureRace getRace() { return m_attribs.get<CreatureRace>(CreatureAttrRace); }
99
100 CreaturePtr cast();
101
102private:
103 stdext::dynamic_storage<uint8> m_attribs;
104};
105
106class CreatureManager
107{
108public:
109 CreatureManager();
110 void clear() { m_creatures.clear(); }
111 void clearSpawns();
112 void terminate();
113
114 void loadMonsters(const std::string& file);
115 void loadSingleCreature(const std::string& file);
116 void loadNpcs(const std::string& folder);
117 void loadCreatureBuffer(const std::string& buffer);
118 void loadSpawns(const std::string& fileName);
119 void saveSpawns(const std::string& fileName);
120
121 const CreatureTypePtr& getCreatureByName(std::string name);
122 const CreatureTypePtr& getCreatureByLook(int look);
123
124 std::vector<SpawnPtr> getSpawns();
125 SpawnPtr getSpawn(const Position& centerPos);
126 SpawnPtr getSpawnForPlacePos(const Position& pos);
127 SpawnPtr addSpawn(const Position& centerPos, int radius);
128 void deleteSpawn(const SpawnPtr& spawn);
129
130 bool isLoaded() { return m_loaded; }
131 bool isSpawnLoaded() { return m_spawnLoaded; }
132
133 const std::vector<CreatureTypePtr>& getCreatures() { return m_creatures; }
134
135protected:
136 void internalLoadCreatureBuffer(TiXmlElement* elem, const CreatureTypePtr& m);
137
138private:
139 std::vector<CreatureTypePtr> m_creatures;
140 std::unordered_map<Position, SpawnPtr, PositionHasher> m_spawns;
141 stdext::boolean<false> m_loaded, m_spawnLoaded;
142 CreatureTypePtr m_nullCreature;
143};
144
145extern CreatureManager g_creatures;
146
147#endif
148
Top