Advertisement
Tequilaribs

map.h

Jun 20th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. /**
  2. * The Forgotten Server - a free and open-source MMORPG server emulator
  3. * Copyright (C) 2016 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_MAP_H_E3953D57C058461F856F5221D359DAFA
  21. #define FS_MAP_H_E3953D57C058461F856F5221D359DAFA
  22.  
  23. #include "position.h"
  24. #include "item.h"
  25. #include "fileloader.h"
  26.  
  27. #include "tools.h"
  28. #include "tile.h"
  29. #include "town.h"
  30. #include "house.h"
  31. #include "spawn.h"
  32.  
  33. class Creature;
  34. class Player;
  35. class Game;
  36. class Tile;
  37. class Map;
  38.  
  39. static constexpr int32_t MAP_MAX_LAYERS = 16;
  40. static const int32_t maxViewportX = 15; //min value: maxClientViewportX + 1
  41. static const int32_t maxViewportY = 13; //min value: maxClientViewportY + 1
  42. static const int32_t maxClientViewportX = 14;
  43. static const int32_t maxClientViewportY = 12;
  44.  
  45. struct FindPathParams;
  46. struct AStarNode {
  47. AStarNode* parent;
  48. int_fast32_t f;
  49. uint16_t x, y;
  50. };
  51.  
  52. static constexpr int32_t MAX_NODES = 512;
  53.  
  54. static constexpr int32_t MAP_NORMALWALKCOST = 10;
  55. static constexpr int32_t MAP_DIAGONALWALKCOST = 25;
  56.  
  57. class AStarNodes
  58. {
  59. public:
  60. AStarNodes(uint32_t x, uint32_t y);
  61.  
  62. AStarNode* createOpenNode(AStarNode* parent, uint32_t x, uint32_t y, int_fast32_t f);
  63. AStarNode* getBestNode();
  64. void closeNode(AStarNode* node);
  65. void openNode(AStarNode* node);
  66. int_fast32_t getClosedNodes() const;
  67. AStarNode* getNodeByPosition(uint32_t x, uint32_t y);
  68.  
  69. static int_fast32_t getMapWalkCost(AStarNode* node, const Position& neighborPos);
  70. static int_fast32_t getTileWalkCost(const Creature& creature, const Tile* tile);
  71.  
  72. private:
  73. AStarNode nodes[MAX_NODES];
  74. bool openNodes[MAX_NODES];
  75. std::unordered_map<uint32_t, AStarNode*> nodeTable;
  76. size_t curNode;
  77. int_fast32_t closedNodes;
  78. };
  79.  
  80. typedef std::map<Position, SpectatorVec> SpectatorCache;
  81.  
  82. static constexpr int32_t FLOOR_BITS = 3;
  83. static constexpr int32_t FLOOR_SIZE = (1 << FLOOR_BITS);
  84. static constexpr int32_t FLOOR_MASK = (FLOOR_SIZE - 1);
  85.  
  86. struct Floor {
  87. constexpr Floor() = default;
  88. ~Floor();
  89.  
  90. // non-copyable
  91. Floor(const Floor&) = delete;
  92. Floor& operator=(const Floor&) = delete;
  93.  
  94. Tile* tiles[FLOOR_SIZE][FLOOR_SIZE] = {};
  95. };
  96.  
  97. class FrozenPathingConditionCall;
  98. class QTreeLeafNode;
  99.  
  100. class QTreeNode
  101. {
  102. public:
  103. constexpr QTreeNode() = default;
  104. virtual ~QTreeNode();
  105.  
  106. // non-copyable
  107. QTreeNode(const QTreeNode&) = delete;
  108. QTreeNode& operator=(const QTreeNode&) = delete;
  109.  
  110. bool isLeaf() const {
  111. return leaf;
  112. }
  113.  
  114. QTreeLeafNode* getLeaf(uint32_t x, uint32_t y);
  115.  
  116. template<typename Leaf, typename Node>
  117. inline static Leaf getLeafStatic(Node node, uint32_t x, uint32_t y)
  118. {
  119. do {
  120. node = node->child[((x & 0x8000) >> 15) | ((y & 0x8000) >> 14)];
  121. if (!node) {
  122. return nullptr;
  123. }
  124.  
  125. x <<= 1;
  126. y <<= 1;
  127. } while (!node->leaf);
  128. return static_cast<Leaf>(node);
  129. }
  130.  
  131. QTreeLeafNode* createLeaf(uint32_t x, uint32_t y, uint32_t level);
  132.  
  133. protected:
  134. QTreeNode* child[4] = {};
  135.  
  136. bool leaf = false;
  137.  
  138. friend class Map;
  139. };
  140.  
  141. class QTreeLeafNode final : public QTreeNode
  142. {
  143. public:
  144. QTreeLeafNode() { leaf = true; newLeaf = true; }
  145. ~QTreeLeafNode();
  146.  
  147. // non-copyable
  148. QTreeLeafNode(const QTreeLeafNode&) = delete;
  149. QTreeLeafNode& operator=(const QTreeLeafNode&) = delete;
  150.  
  151. Floor* createFloor(uint32_t z);
  152. Floor* getFloor(uint8_t z) const {
  153. return array[z];
  154. }
  155.  
  156. void addCreature(Creature* c);
  157. void removeCreature(Creature* c);
  158.  
  159. protected:
  160. static bool newLeaf;
  161. QTreeLeafNode* leafS = nullptr;
  162. QTreeLeafNode* leafE = nullptr;
  163. Floor* array[MAP_MAX_LAYERS] = {};
  164. CreatureVector creature_list;
  165. CreatureVector player_list;
  166.  
  167. friend class Map;
  168. friend class QTreeNode;
  169. };
  170.  
  171. /**
  172. * Map class.
  173. * Holds all the actual map-data
  174. */
  175.  
  176. class Map
  177. {
  178. public:
  179. static constexpr int32_t maxViewportX = 11; //min value: maxClientViewportX + 1
  180. static constexpr int32_t maxViewportY = 11; //min value: maxClientViewportY + 1
  181. static constexpr int32_t maxClientViewportX = 8;
  182. static constexpr int32_t maxClientViewportY = 6;
  183.  
  184. uint32_t clean() const;
  185.  
  186. /**
  187. * Load a map.
  188. * \returns true if the map was loaded successfully
  189. */
  190. bool loadMap(const std::string& identifier, bool loadHouses);
  191.  
  192. /**
  193. * Save a map.
  194. * \returns true if the map was saved successfully
  195. */
  196. static bool save();
  197.  
  198. /**
  199. * Get a single tile.
  200. * \returns A pointer to that tile.
  201. */
  202. Tile* getTile(uint16_t x, uint16_t y, uint8_t z) const;
  203. inline Tile* getTile(const Position& pos) const {
  204. return getTile(pos.x, pos.y, pos.z);
  205. }
  206.  
  207. /**
  208. * Set a single tile.
  209. */
  210. void setTile(uint16_t x, uint16_t y, uint8_t z, Tile* newTile);
  211. void setTile(const Position& pos, Tile* newTile) {
  212. setTile(pos.x, pos.y, pos.z, newTile);
  213. }
  214.  
  215. /**
  216. * Place a creature on the map
  217. * \param centerPos The position to place the creature
  218. * \param creature Creature to place on the map
  219. * \param extendedPos If true, the creature will in first-hand be placed 2 tiles away
  220. * \param forceLogin If true, placing the creature will not fail becase of obstacles (creatures/chests)
  221. */
  222. bool placeCreature(const Position& centerPos, Creature* creature, bool extendedPos = false, bool forceLogin = false);
  223.  
  224. void moveCreature(Creature& creature, Tile& newTile, bool forceTeleport = false);
  225.  
  226. void getSpectators(SpectatorVec& list, const Position& centerPos, bool multifloor = false, bool onlyPlayers = false,
  227. int32_t minRangeX = 0, int32_t maxRangeX = 0,
  228. int32_t minRangeY = 0, int32_t maxRangeY = 0);
  229.  
  230. void clearSpectatorCache();
  231.  
  232. /**
  233. * Checks if you can throw an object to that position
  234. * \param fromPos from Source point
  235. * \param toPos Destination point
  236. * \param rangex maximum allowed range horizontially
  237. * \param rangey maximum allowed range vertically
  238. * \param checkLineOfSight checks if there is any blocking objects in the way
  239. * \returns The result if you can throw there or not
  240. */
  241. bool canThrowObjectTo(const Position& fromPos, const Position& toPos, bool checkLineOfSight = true,
  242. int32_t rangex = Map::maxClientViewportX, int32_t rangey = Map::maxClientViewportY) const;
  243.  
  244. /**
  245. * Checks if path is clear from fromPos to toPos
  246. * Notice: This only checks a straight line if the path is clear, for path finding use getPathTo.
  247. * \param fromPos from Source point
  248. * \param toPos Destination point
  249. * \param floorCheck if true then view is not clear if fromPos.z is not the same as toPos.z
  250. * \returns The result if there is no obstacles
  251. */
  252. bool isSightClear(const Position& fromPos, const Position& toPos, bool floorCheck) const;
  253. bool checkSightLine(const Position& fromPos, const Position& toPos) const;
  254.  
  255. const Tile* canWalkTo(const Creature& creature, const Position& pos) const;
  256.  
  257. bool getPathMatching(const Creature& creature, std::forward_list<Direction>& dirList,
  258. const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp) const;
  259.  
  260. std::map<std::string, Position> waypoints;
  261.  
  262. QTreeLeafNode* getQTNode(uint16_t x, uint16_t y) {
  263. return QTreeNode::getLeafStatic<QTreeLeafNode*, QTreeNode*>(&root, x, y);
  264. }
  265.  
  266. Spawns spawns;
  267. Towns towns;
  268. Houses houses;
  269. protected:
  270. SpectatorCache spectatorCache;
  271. SpectatorCache playersSpectatorCache;
  272.  
  273. QTreeNode root;
  274.  
  275. std::string spawnfile;
  276. std::string housefile;
  277.  
  278. uint32_t width = 0;
  279. uint32_t height = 0;
  280.  
  281. // Actually scans the map for spectators
  282. void getSpectatorsInternal(SpectatorVec& list, const Position& centerPos,
  283. int32_t minRangeX, int32_t maxRangeX,
  284. int32_t minRangeY, int32_t maxRangeY,
  285. int32_t minRangeZ, int32_t maxRangeZ, bool onlyPlayers) const;
  286.  
  287. friend class Game;
  288. friend class IOMap;
  289. };
  290.  
  291. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement