Advertisement
Guest User

Untitled

a guest
May 21st, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. ////////////////////////////////////////////////////////////////////////
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ////////////////////////////////////////////////////////////////////////
  17.  
  18. #ifndef __MAP__
  19. #define __MAP__
  20. #include "tools.h"
  21.  
  22. #include "fileloader.h"
  23. #include "position.h"
  24.  
  25. #include "waypoints.h"
  26. #include "tile.h"
  27.  
  28. class Creature;
  29. class Player;
  30. class Game;
  31. class Tile;
  32. class Map;
  33.  
  34. struct FindPathParams;
  35. struct AStarNode
  36. {
  37. uint16_t x, y;
  38. AStarNode* parent;
  39. int32_t f, g, h;
  40. };
  41.  
  42. using boost::shared_ptr;
  43. #define MAP_MAX_LAYERS 16
  44.  
  45. #define MAX_NODES 512
  46. #define GET_NODE_INDEX(a) (a - &nodes[0])
  47.  
  48. #define MAP_NORMALWALKCOST 10
  49. #define MAP_DIAGONALWALKCOST 25
  50.  
  51. class AStarNodes
  52. {
  53. public:
  54. AStarNodes();
  55. virtual ~AStarNodes() {}
  56.  
  57. void openNode(AStarNode* node);
  58. void closeNode(AStarNode* node);
  59.  
  60. uint32_t countOpenNodes();
  61. uint32_t countClosedNodes();
  62.  
  63. AStarNode* getBestNode();
  64. AStarNode* createOpenNode();
  65. AStarNode* getNodeInList(uint16_t x, uint16_t y);
  66.  
  67. bool isInList(uint16_t x, uint16_t y);
  68. int32_t getEstimatedDistance(uint16_t x, uint16_t y, uint16_t xGoal, uint16_t yGoal);
  69.  
  70. int32_t getMapWalkCost(const Creature* creature, AStarNode* node,
  71. const Tile* neighbourTile, const Position& neighbourPos);
  72. static int32_t getTileWalkCost(const Creature* creature, const Tile* tile);
  73.  
  74. private:
  75. AStarNode nodes[MAX_NODES];
  76.  
  77. std::bitset<MAX_NODES> openNodes;
  78. uint32_t curNode;
  79. };
  80.  
  81. template<class T> class lessPointer: public std::binary_function<T*, T*, bool>
  82. {
  83. public:
  84. bool operator()(T*& t1, T*& t2) {return *t1 < *t2;}
  85. };
  86.  
  87. #define FLOOR_BITS 3
  88. #define FLOOR_SIZE (1 << FLOOR_BITS)
  89. #define FLOOR_MASK (FLOOR_SIZE - 1)
  90.  
  91. struct Floor
  92. {
  93. Floor();
  94. Tile* tiles[FLOOR_SIZE][FLOOR_SIZE];
  95. };
  96.  
  97. class FrozenPathingConditionCall;
  98. class QTreeLeafNode;
  99.  
  100. class QTreeNode
  101. {
  102. public:
  103. QTreeNode();
  104. virtual ~QTreeNode();
  105.  
  106. bool isLeaf() const {return m_isLeaf;}
  107.  
  108. QTreeLeafNode* getLeaf(uint16_t x, uint16_t y);
  109. static QTreeLeafNode* getLeafStatic(QTreeNode* root, uint16_t x, uint16_t y);
  110.  
  111. QTreeLeafNode* createLeaf(uint16_t x, uint16_t y, uint16_t level);
  112.  
  113. protected:
  114. bool m_isLeaf;
  115. QTreeNode* m_child[4];
  116.  
  117. friend class Map;
  118. };
  119.  
  120.  
  121. class QTreeLeafNode : public QTreeNode
  122. {
  123. public:
  124. QTreeLeafNode();
  125. virtual ~QTreeLeafNode();
  126.  
  127. Floor* createFloor(uint16_t z);
  128. Floor* getFloor(uint16_t z){return m_array[z];}
  129.  
  130. QTreeLeafNode* stepSouth(){return m_leafS;}
  131. QTreeLeafNode* stepEast(){return m_leafE;}
  132.  
  133. void addCreature(Creature* c);
  134. void removeCreature(Creature* c);
  135.  
  136. protected:
  137. static bool newLeaf;
  138.  
  139. QTreeLeafNode* m_leafS;
  140. QTreeLeafNode* m_leafE;
  141.  
  142. Floor* m_array[MAP_MAX_LAYERS];
  143. CreatureVector creatureList;
  144.  
  145. friend class Map;
  146. friend class QTreeNode;
  147. };
  148.  
  149. /**
  150. * Map class.
  151. * Holds all the actual map-data
  152. */
  153.  
  154. class Map
  155. {
  156. public:
  157. Map();
  158. virtual ~Map() {}
  159.  
  160. static const int32_t maxViewportX = 11; //min value: maxClientViewportX + 1
  161. static const int32_t maxViewportY = 11; //min value: maxClientViewportY + 1
  162. static const int32_t maxClientViewportX = 8;
  163. static const int32_t maxClientViewportY = 6;
  164.  
  165. /**
  166. * Load a map.
  167. * \returns true if the map was loaded successfully
  168. */
  169. bool loadMap(const std::string& identifier);
  170.  
  171. /**
  172. * Save a map.
  173. * \param identifier file/database to save to
  174. * \returns true if the map was saved successfully
  175. */
  176. bool saveMap();
  177.  
  178. /**
  179. * Get a single tile.
  180. * \returns A pointer to that tile.
  181. */
  182. Tile* getTile(int32_t x, int32_t y, int32_t z);
  183. Tile* getTile(const Position& pos) {return getTile(pos.x, pos.y, pos.z);}
  184.  
  185. /**
  186. * Set a single tile.
  187. * \param a tile to set for the position
  188. */
  189. void setTile(uint16_t _x, uint16_t _y, uint16_t _z, Tile* newTile);
  190. void setTile(const Position& pos, Tile* newTile) {setTile(pos.x, pos.y, pos.z, newTile);}
  191.  
  192. /**
  193. * Place a creature on the map
  194. * \param pos The position to place the creature
  195. * \param creature Creature to place on the map
  196. * \param extendedPos If true, the creature will in first-hand be placed 2 tiles away
  197. * \param forceLogin If true, placing the creature will not fail becase of obstacles (creatures/chests)
  198. */
  199. bool placeCreature(const Position& centerPos, Creature* creature, bool extendedPos = false, bool forceLogin = false);
  200.  
  201. /**
  202. * Remove a creature from the map.
  203. * \param c Creature pointer to the creature to remove
  204. */
  205. bool removeCreature(Creature* c);
  206.  
  207. /**
  208. * Checks if you can throw an object to that position
  209. * \param fromPos from Source point
  210. * \param toPos Destination point
  211. * \param rangex maximum allowed range horizontially
  212. * \param rangey maximum allowed range vertically
  213. * \param checkLineOfSight checks if there is any blocking objects in the way
  214. * \returns The result if you can throw there or not
  215. */
  216. bool canThrowObjectTo(const Position& fromPos, const Position& toPos, bool checkLineOfSight = true,
  217. int32_t rangex = Map::maxClientViewportX, int32_t rangey = Map::maxClientViewportY);
  218.  
  219. /**
  220. * Checks if path is clear from fromPos to toPos
  221. * Notice: This only checks a straight line if the path is clear, for path finding use getPathTo.
  222. * \param fromPos from Source point
  223. * \param toPos Destination point
  224. * \param floorCheck if true then view is not clear if fromPos.z is not the same as toPos.z
  225. * \returns The result if there is no obstacles
  226. */
  227. bool isSightClear(const Position& fromPos, const Position& toPos, bool floorCheck) const;
  228. bool checkSightLine(const Position& fromPos, const Position& toPos) const;
  229.  
  230. /**
  231. * Get the path to a specific position on the map.
  232. * \param creature The creature that wants a path
  233. * \param destPos The position we want a path calculated to
  234. * \param listDir contains a list of directions to the destination
  235. * \param maxDist Maximum distance from our current position to search, default: -1 (no limit)
  236. * \returns returns true if a path was found
  237. */
  238. bool getPathTo(const Creature* creature, const Position& destPos,
  239. std::list<Direction>& listDir, int32_t maxDist = -1);
  240. bool getPathMatching(const Creature* creature, std::list<Direction>& dirList,
  241. const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp);
  242.  
  243. QTreeLeafNode* getLeaf(uint16_t x, uint16_t y) {return root.getLeaf(x, y);}
  244. const Tile* canWalkTo(const Creature* creature, const Position& pos);
  245. Waypoints waypoints;
  246.  
  247. protected:
  248. QTreeNode root;
  249.  
  250. uint32_t mapWidth, mapHeight;
  251. std::string spawnfile, housefile;
  252. StringVec descriptions;
  253.  
  254. SpectatorCache spectatorCache;
  255. void clearSpectatorCache() {spectatorCache.clear();}
  256.  
  257. // Actually scans the map for spectators
  258. void getSpectatorsInternal(SpectatorVec& list, const Position& centerPos, bool checkforduplicate,
  259. int32_t minRangeX, int32_t maxRangeX, int32_t minRangeY, int32_t maxRangeY,
  260. int32_t minRangeZ, int32_t maxRangeZ);
  261. // Use this when a custom spectator vector is needed, this support many
  262. // more parameters than the heavily cached version below.
  263. void getSpectators(SpectatorVec& list, const Position& centerPos, bool checkforduplicate = false, bool multifloor = false,
  264. int32_t minRangeX = 0, int32_t maxRangeX = 0, int32_t minRangeY = 0, int32_t maxRangeY = 0);
  265. // The returned SpectatorVec is a temporary and should not be kept around
  266. // Take special heed in that the vector will be destroyed if any function
  267. // that calls clearSpectatorCache is called.
  268. const SpectatorVec& getSpectators(const Position& centerPos);
  269.  
  270. friend class Game;
  271. friend class IOMap;
  272. };
  273.  
  274. inline void QTreeLeafNode::addCreature(Creature* c)
  275. {
  276. creatureList.push_back(c);
  277. }
  278.  
  279. inline void QTreeLeafNode::removeCreature(Creature* c)
  280. {
  281. CreatureVector::iterator it = std::find(creatureList.begin(), creatureList.end(), c);
  282. assert(it != creatureList.end());
  283. creatureList.erase(it);
  284. }
  285. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement