Guest User

Untitled

a guest
May 20th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2010-2014 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 MAP_H
  24. #define MAP_H
  25.  
  26. #include "creature.h"
  27. #include "houses.h"
  28. #include "towns.h"
  29. #include "creatures.h"
  30. #include "animatedtext.h"
  31. #include "statictext.h"
  32. #include "tile.h"
  33.  
  34. #include <framework/core/clock.h>
  35.  
  36. enum OTBM_ItemAttr
  37. {
  38. OTBM_ATTR_DESCRIPTION = 1,
  39. OTBM_ATTR_EXT_FILE = 2,
  40. OTBM_ATTR_TILE_FLAGS = 3,
  41. OTBM_ATTR_ACTION_ID = 4,
  42. OTBM_ATTR_UNIQUE_ID = 5,
  43. OTBM_ATTR_TEXT = 6,
  44. OTBM_ATTR_DESC = 7,
  45. OTBM_ATTR_TELE_DEST = 8,
  46. OTBM_ATTR_ITEM = 9,
  47. OTBM_ATTR_DEPOT_ID = 10,
  48. OTBM_ATTR_SPAWN_FILE = 11,
  49. OTBM_ATTR_RUNE_CHARGES = 12,
  50. OTBM_ATTR_HOUSE_FILE = 13,
  51. OTBM_ATTR_HOUSEDOORID = 14,
  52. OTBM_ATTR_COUNT = 15,
  53. OTBM_ATTR_DURATION = 16,
  54. OTBM_ATTR_DECAYING_STATE = 17,
  55. OTBM_ATTR_WRITTENDATE = 18,
  56. OTBM_ATTR_WRITTENBY = 19,
  57. OTBM_ATTR_SLEEPERGUID = 20,
  58. OTBM_ATTR_SLEEPSTART = 21,
  59. OTBM_ATTR_CHARGES = 22,
  60. OTBM_ATTR_CONTAINER_ITEMS = 23,
  61. OTBM_ATTR_ATTRIBUTE_MAP = 128,
  62. /// just random numbers, they're not actually used by the binary reader...
  63. OTBM_ATTR_WIDTH = 129,
  64. OTBM_ATTR_HEIGHT = 130
  65. };
  66.  
  67. enum OTBM_NodeTypes_t
  68. {
  69. OTBM_ROOTV2 = 1,
  70. OTBM_MAP_DATA = 2,
  71. OTBM_ITEM_DEF = 3,
  72. OTBM_TILE_AREA = 4,
  73. OTBM_TILE = 5,
  74. OTBM_ITEM = 6,
  75. OTBM_TILE_SQUARE = 7,
  76. OTBM_TILE_REF = 8,
  77. OTBM_SPAWNS = 9,
  78. OTBM_SPAWN_AREA = 10,
  79. OTBM_MONSTER = 11,
  80. OTBM_TOWNS = 12,
  81. OTBM_TOWN = 13,
  82. OTBM_HOUSETILE = 14,
  83. OTBM_WAYPOINTS = 15,
  84. OTBM_WAYPOINT = 16
  85. };
  86.  
  87. enum {
  88. OTCM_SIGNATURE = 0x4D43544F,
  89. OTCM_VERSION = 1
  90. };
  91.  
  92. enum {
  93. BLOCK_SIZE = 32
  94. };
  95.  
  96. enum : uint8 {
  97. Animation_Force,
  98. Animation_Show
  99. };
  100.  
  101. class TileBlock {
  102. public:
  103. TileBlock() { m_tiles.fill(nullptr); }
  104.  
  105. const TilePtr& create(const Position& pos) {
  106. TilePtr& tile = m_tiles[getTileIndex(pos)];
  107. tile = TilePtr(new Tile(pos));
  108. return tile;
  109. }
  110. const TilePtr& getOrCreate(const Position& pos) {
  111. TilePtr& tile = m_tiles[getTileIndex(pos)];
  112. if(!tile)
  113. tile = TilePtr(new Tile(pos));
  114. return tile;
  115. }
  116. const TilePtr& get(const Position& pos) { return m_tiles[getTileIndex(pos)]; }
  117. void remove(const Position& pos) { m_tiles[getTileIndex(pos)] = nullptr; }
  118.  
  119. uint getTileIndex(const Position& pos) { return ((pos.y % BLOCK_SIZE) * BLOCK_SIZE) + (pos.x % BLOCK_SIZE); }
  120.  
  121. const std::array<TilePtr, BLOCK_SIZE*BLOCK_SIZE>& getTiles() const { return m_tiles; }
  122.  
  123. private:
  124. std::array<TilePtr, BLOCK_SIZE*BLOCK_SIZE> m_tiles;
  125. };
  126.  
  127. struct AwareRange
  128. {
  129. int top;
  130. int right;
  131. int bottom;
  132. int left;
  133.  
  134. int horizontal() { return left + right + 1; }
  135. int vertical() { return top + bottom + 1; }
  136. };
  137.  
  138. //@bindsingleton g_map
  139. class Map
  140. {
  141. public:
  142. void init();
  143. void terminate();
  144.  
  145. void addMapView(const MapViewPtr& mapView);
  146. void removeMapView(const MapViewPtr& mapView);
  147. void notificateTileUpdate(const Position& pos);
  148.  
  149. bool loadOtcm(const std::string& fileName);
  150. void saveOtcm(const std::string& fileName);
  151.  
  152. void loadOtbm(const std::string& fileName);
  153. void saveOtbm(const std::string& fileName);
  154.  
  155. // otbm attributes (description, size, etc.)
  156. void setHouseFile(const std::string& file) { m_attribs.set(OTBM_ATTR_HOUSE_FILE, file); }
  157. void setSpawnFile(const std::string& file) { m_attribs.set(OTBM_ATTR_SPAWN_FILE, file); }
  158. void setDescription(const std::string& desc) { m_attribs.set(OTBM_ATTR_DESCRIPTION, desc); }
  159.  
  160. void clearDescriptions() { m_attribs.remove(OTBM_ATTR_DESCRIPTION); }
  161. void setWidth(uint16 w) { m_attribs.set(OTBM_ATTR_WIDTH, w); }
  162. void setHeight(uint16 h) { m_attribs.set(OTBM_ATTR_HEIGHT, h); }
  163.  
  164. std::string getHouseFile() { return m_attribs.get<std::string>(OTBM_ATTR_HOUSE_FILE); }
  165. std::string getSpawnFile() { return m_attribs.get<std::string>(OTBM_ATTR_SPAWN_FILE); }
  166. Size getSize() { return Size(m_attribs.get<uint16>(OTBM_ATTR_WIDTH), m_attribs.get<uint16>(OTBM_ATTR_HEIGHT)); }
  167. std::vector<std::string> getDescriptions() { return stdext::split(m_attribs.get<std::string>(OTBM_ATTR_DESCRIPTION), "\n"); }
  168.  
  169. void clean();
  170. void cleanDynamicThings();
  171. void cleanTexts();
  172.  
  173. // thing related
  174. void addThing(const ThingPtr& thing, const Position& pos, int stackPos = -1);
  175. ThingPtr getThing(const Position& pos, int stackPos);
  176. bool removeThing(const ThingPtr& thing);
  177. bool removeThingByPos(const Position& pos, int stackPos);
  178. void colorizeThing(const ThingPtr& thing, const Color& color);
  179. void removeThingColor(const ThingPtr& thing);
  180.  
  181. StaticTextPtr getStaticText(const Position& pos);
  182.  
  183. // tile related
  184. const TilePtr& createTile(const Position& pos);
  185. template <typename... Items>
  186. const TilePtr& createTileEx(const Position& pos, const Items&... items);
  187. const TilePtr& getOrCreateTile(const Position& pos);
  188. const TilePtr& getTile(const Position& pos);
  189. const TileList getTiles(int floor = -1);
  190. void cleanTile(const Position& pos);
  191.  
  192. // tile zone related
  193. void setShowZone(tileflags_t zone, bool show);
  194. void setShowZones(bool show);
  195. void setZoneColor(tileflags_t flag, const Color& color);
  196. void setZoneOpacity(float opacity) { m_zoneOpacity = opacity; }
  197.  
  198. float getZoneOpacity() { return m_zoneOpacity; }
  199. Color getZoneColor(tileflags_t flag) { return m_zoneColors[flag]; }
  200. tileflags_t getZoneFlags() { return (tileflags_t)m_zoneFlags; }
  201. bool showZones() { return m_zoneFlags != 0; }
  202. bool showZone(tileflags_t zone) { return (m_zoneFlags & zone) == zone; }
  203.  
  204. void setForceShowAnimations(bool force);
  205. bool isForcingAnimations();
  206. bool isShowingAnimations();
  207. void setShowAnimations(bool show);
  208.  
  209. void beginGhostMode(float opacity);
  210. void endGhostMode();
  211.  
  212. std::map<Position, ItemPtr> findItemsById(uint16 clientId, uint32 max);
  213.  
  214. // known creature related
  215. void addCreature(const CreaturePtr& creature);
  216. CreaturePtr getCreatureById(uint32 id);
  217. void removeCreatureById(uint32 id);
  218. std::vector<CreaturePtr> getSightSpectators(const Position& centerPos, bool multiFloor);
  219. std::vector<CreaturePtr> getSpectators(const Position& centerPos, bool multiFloor);
  220. std::vector<CreaturePtr> getSpectatorsInRange(const Position& centerPos, bool multiFloor, int xRange, int yRange);
  221. std::vector<CreaturePtr> getSpectatorsInRangeEx(const Position& centerPos, bool multiFloor, int minXRange, int maxXRange, int minYRange, int maxYRange);
  222.  
  223. void setLight(const Light& light) { m_light = light; }
  224. void setCentralPosition(const Position& centralPosition);
  225.  
  226. bool isLookPossible(const Position& pos);
  227. bool isCovered(const Position& pos, int firstFloor = 0);
  228. bool isCompletelyCovered(const Position& pos, int firstFloor = 0);
  229. bool isAwareOfPosition(const Position& pos);
  230.  
  231. void setAwareRange(const AwareRange& range);
  232. void resetAwareRange();
  233. AwareRange getAwareRange() { return m_awareRange; }
  234.  
  235. Light getLight() { return m_light; }
  236. Position getCentralPosition() { return m_centralPosition; }
  237. int getFirstAwareFloor();
  238. int getLastAwareFloor();
  239. const std::vector<MissilePtr>& getFloorMissiles(int z) { return m_floorMissiles[z]; }
  240.  
  241. std::vector<AnimatedTextPtr> getAnimatedTexts() { return m_animatedTexts; }
  242. std::vector<StaticTextPtr> getStaticTexts() { return m_staticTexts; }
  243.  
  244. std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> findPath(const Position& start, const Position& goal, int maxComplexity, int flags = 0);
  245.  
  246. private:
  247. void removeUnawareThings();
  248. uint getBlockIndex(const Position& pos) { return ((pos.y / BLOCK_SIZE) * (65536 / BLOCK_SIZE)) + (pos.x / BLOCK_SIZE); }
  249.  
  250. std::unordered_map<uint, TileBlock> m_tileBlocks[Otc::MAX_Z+1];
  251. std::unordered_map<uint32, CreaturePtr> m_knownCreatures;
  252. std::array<std::vector<MissilePtr>, Otc::MAX_Z+1> m_floorMissiles;
  253. std::vector<AnimatedTextPtr> m_animatedTexts;
  254. std::vector<StaticTextPtr> m_staticTexts;
  255. std::vector<MapViewPtr> m_mapViews;
  256. std::unordered_map<Position, std::string, PositionHasher> m_waypoints;
  257.  
  258. uint8 m_animationFlags;
  259. uint32 m_zoneFlags;
  260. std::array<Color, TILESTATE_LAST> m_zoneColors;
  261. float m_zoneOpacity;
  262.  
  263. Light m_light;
  264. Position m_centralPosition;
  265. Rect m_tilesRect;
  266.  
  267. stdext::packed_storage<uint8> m_attribs;
  268. AwareRange m_awareRange;
  269. static TilePtr m_nulltile;
  270. };
  271.  
  272. extern Map g_map;
  273.  
  274. #endif
  275.  
  276. /* vim: set ts=4 sw=4 et: */
Add Comment
Please, Sign In to add comment