Otclient  14/8/2020
creatures.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 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"
28 #include "outfit.h"
29 
31 {
38 };
39 
41 {
44 };
45 
47 {
50 };
51 
52 class Spawn : public LuaObject
53 {
54 public:
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 
69 protected:
70  void load(TiXmlElement* node);
71  void save(TiXmlElement* node);
72 
73 private:
75  std::unordered_map<Position, CreatureTypePtr, PositionHasher> m_creatures;
76  friend class CreatureManager;
77 };
78 
79 class CreatureType : public LuaObject
80 {
81 public:
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); }
96 
97  void setRace(CreatureRace race) { m_attribs.set(CreatureAttrRace, race); }
99 
100  CreaturePtr cast();
101 
102 private:
104 };
105 
107 {
108 public:
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);
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 
135 protected:
137 
138 private:
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 
146 
147 #endif
Spawn::getCenterPos
Position getCenterPos()
Definition: creatures.h:62
CreatureManager::getCreatureByLook
const CreatureTypePtr & getCreatureByLook(int look)
Definition: creatures.cpp:356
g_creatures
CreatureManager g_creatures
Definition: creatures.cpp:30
CreatureType::setRace
void setRace(CreatureRace race)
Definition: creatures.h:97
Spawn::getRadius
int32 getRadius()
Definition: creatures.h:59
CreatureType
Definition: creatures.h:79
CreatureManager::clearSpawns
void clearSpawns()
Definition: creatures.cpp:175
CreatureRaceNpc
@ CreatureRaceNpc
Definition: creatures.h:42
CreatureManager::getCreatureByName
const CreatureTypePtr & getCreatureByName(std::string name)
Definition: creatures.cpp:343
CreatureManager::getCreatures
const std::vector< CreatureTypePtr > & getCreatures()
Definition: creatures.h:133
CreatureType::setDirection
void setDirection(Otc::Direction dir)
Definition: creatures.h:94
CreatureType::getRace
CreatureRace getRace()
Definition: creatures.h:98
CreatureType::getName
std::string getName()
Definition: creatures.h:89
Spawn::Spawn
Spawn()=default
Spawn::addCreature
void addCreature(const Position &placePos, const CreatureTypePtr &cType)
Definition: creatures.cpp:121
CreatureType::setSpawnTime
void setSpawnTime(int32 spawnTime)
Definition: creatures.h:85
Spawn::setCenterPos
void setCenterPos(const Position &pos)
Definition: creatures.h:61
CreatureManager
Definition: creatures.h:106
Spawn
Definition: creatures.h:52
CreatureRaceMonster
@ CreatureRaceMonster
Definition: creatures.h:43
CreatureManager::isSpawnLoaded
bool isSpawnLoaded()
Definition: creatures.h:131
stdext::boolean< false >
CreatureManager::deleteSpawn
void deleteSpawn(const SpawnPtr &spawn)
Definition: creatures.cpp:410
stdext::dynamic_storage< uint8 >
SpawnAttrRadius
@ SpawnAttrRadius
Definition: creatures.h:48
luaobject.h
Spawn::setRadius
void setRadius(int32 r)
Definition: creatures.h:58
CreatureManager::CreatureManager
CreatureManager()
Definition: creatures.cpp:170
Otc::Direction
Direction
Definition: const.h:161
CreatureAttrRace
@ CreatureAttrRace
Definition: creatures.h:37
Outfit
Definition: outfit.h:29
CreatureManager::saveSpawns
void saveSpawns(const std::string &fileName)
Definition: creatures.cpp:261
stdext::dynamic_storage::set
void set(const Key &k, const T &value)
Definition: dynamic_storage.h:35
declarations.h
CreatureAttr
CreatureAttr
Definition: creatures.h:30
SpawnAttr
SpawnAttr
Definition: creatures.h:46
CreatureType::cast
CreaturePtr cast()
Definition: creatures.cpp:155
int32
int32_t int32
Definition: types.h:39
CreatureManager::terminate
void terminate()
Definition: creatures.cpp:43
Position
Definition: position.h:33
CreatureManager::getSpawns
std::vector< SpawnPtr > getSpawns()
Definition: creatures.cpp:418
CreatureManager::loadNpcs
void loadNpcs(const std::string &folder)
Definition: creatures.cpp:210
Spawn::save
void save(TiXmlElement *node)
Definition: creatures.cpp:90
CreatureManager::loadCreatureBuffer
void loadCreatureBuffer(const std::string &buffer)
Definition: creatures.cpp:286
CreatureType::setOutfit
void setOutfit(const Outfit &o)
Definition: creatures.h:91
CreatureType::CreatureType
CreatureType()=default
CreatureType::getSpawnTime
int32 getSpawnTime()
Definition: creatures.h:86
Spawn::Spawn
Spawn(int32 radius)
Definition: creatures.h:56
CreatureAttrOutfit
@ CreatureAttrOutfit
Definition: creatures.h:34
CreatureType::CreatureType
CreatureType(const std::string &name)
Definition: creatures.h:83
stdext::dynamic_storage::get
T get(const Key &k) const
Definition: dynamic_storage.h:48
Spawn::getCreatures
std::vector< CreatureTypePtr > getCreatures()
Definition: creatures.cpp:147
outfit.h
CreatureManager::loadSpawns
void loadSpawns(const std::string &fileName)
Definition: creatures.cpp:224
CreatureManager::isLoaded
bool isLoaded()
Definition: creatures.h:130
CreatureManager::getSpawnForPlacePos
SpawnPtr getSpawnForPlacePos(const Position &pos)
Definition: creatures.cpp:379
CreatureType::getOutfit
Outfit getOutfit()
Definition: creatures.h:92
CreatureManager::clear
void clear()
Definition: creatures.h:110
CreatureManager::loadMonsters
void loadMonsters(const std::string &file)
Definition: creatures.cpp:182
CreatureType::setName
void setName(const std::string &name)
Definition: creatures.h:88
stdext::shared_object_ptr< CreatureType >
Spawn::clear
void clear()
Definition: creatures.h:67
CreatureRace
CreatureRace
Definition: creatures.h:40
CreatureType::getDirection
Otc::Direction getDirection()
Definition: creatures.h:95
CreatureManager::getSpawn
SpawnPtr getSpawn(const Position &centerPos)
Definition: creatures.cpp:370
CreatureManager::loadSingleCreature
void loadSingleCreature(const std::string &file)
Definition: creatures.cpp:205
TiXmlElement
Definition: tinyxml.h:943
CreatureManager::internalLoadCreatureBuffer
void internalLoadCreatureBuffer(TiXmlElement *attrib, const CreatureTypePtr &m)
Definition: creatures.cpp:314
CreatureAttrDir
@ CreatureAttrDir
Definition: creatures.h:36
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
CreatureAttrSpawnTime
@ CreatureAttrSpawnTime
Definition: creatures.h:35
Spawn::removeCreature
void removeCreature(const Position &pos)
Definition: creatures.cpp:137
Spawn::load
void load(TiXmlElement *node)
Definition: creatures.cpp:50
CreatureManager::addSpawn
SpawnPtr addSpawn(const Position &centerPos, int radius)
Definition: creatures.cpp:392
uint8
uint8_t uint8
Definition: types.h:37
CreatureAttrPosition
@ CreatureAttrPosition
Definition: creatures.h:32
SpawnAttrCenter
@ SpawnAttrCenter
Definition: creatures.h:49
CreatureAttrName
@ CreatureAttrName
Definition: creatures.h:33