mapHandler.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * mapHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/int3.h"
  12. #include "../lib/spells/ViewSpellInt.h"
  13. #include "gui/Geometries.h"
  14. #include "SDL.h"
  15. #ifdef IN
  16. #undef IN
  17. #endif
  18. #ifdef OUT
  19. #undef OUT
  20. #endif
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class CGObjectInstance;
  23. class CGHeroInstance;
  24. class CGBoat;
  25. class CMap;
  26. struct TerrainTile;
  27. class PlayerColor;
  28. VCMI_LIB_NAMESPACE_END
  29. struct SDL_Surface;
  30. struct SDL_Rect;
  31. class CAnimation;
  32. class IImage;
  33. class CFadeAnimation;
  34. enum class EWorldViewIcon
  35. {
  36. TOWN = 0,
  37. HERO,
  38. ARTIFACT,
  39. TELEPORT,
  40. GATE,
  41. MINE_WOOD,
  42. MINE_MERCURY,
  43. MINE_STONE,
  44. MINE_SULFUR,
  45. MINE_CRYSTAL,
  46. MINE_GEM,
  47. MINE_GOLD,
  48. RES_WOOD,
  49. RES_MERCURY,
  50. RES_STONE,
  51. RES_SULFUR,
  52. RES_CRYSTAL,
  53. RES_GEM,
  54. RES_GOLD,
  55. };
  56. enum class EMapObjectFadingType
  57. {
  58. NONE,
  59. IN,
  60. OUT
  61. };
  62. enum class EMapAnimRedrawStatus
  63. {
  64. OK,
  65. REDRAW_REQUESTED // map blitter requests quick redraw due to current animation
  66. };
  67. struct TerrainTileObject
  68. {
  69. const CGObjectInstance *obj;
  70. SDL_Rect rect;
  71. int fadeAnimKey;
  72. boost::optional<std::string> ambientSound;
  73. TerrainTileObject(const CGObjectInstance *obj_, SDL_Rect rect_, bool visitablePos = false);
  74. ~TerrainTileObject();
  75. };
  76. struct TerrainTile2
  77. {
  78. std::vector<TerrainTileObject> objects; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  79. };
  80. struct MapDrawingInfo
  81. {
  82. bool scaled;
  83. int3 &topTile; // top-left tile in viewport [in tiles]
  84. std::shared_ptr<const boost::multi_array<ui8, 3>> visibilityMap;
  85. SDL_Rect * drawBounds; // map rect drawing bounds on screen
  86. std::shared_ptr<CAnimation> icons; // holds overlay icons for world view mode
  87. float scale; // map scale for world view mode (only if scaled == true)
  88. bool otherheroAnim;
  89. ui8 anim;
  90. ui8 heroAnim;
  91. int3 movement; // used for smooth map movement
  92. bool puzzleMode;
  93. int3 grailPos; // location of grail for puzzle mode [in tiles]
  94. const std::vector<ObjectPosInfo> * additionalIcons;
  95. bool showAllTerrain; //for expert viewEarth
  96. MapDrawingInfo(int3 &topTile_, std::shared_ptr<const boost::multi_array<ui8, 3>> visibilityMap_, SDL_Rect * drawBounds_, std::shared_ptr<CAnimation> icons_ = nullptr)
  97. : scaled(false),
  98. topTile(topTile_),
  99. visibilityMap(visibilityMap_),
  100. drawBounds(drawBounds_),
  101. icons(icons_),
  102. scale(1.0f),
  103. otherheroAnim(false),
  104. anim(0u),
  105. heroAnim(0u),
  106. movement(int3()),
  107. puzzleMode(false),
  108. grailPos(int3()),
  109. additionalIcons(nullptr),
  110. showAllTerrain(false)
  111. {}
  112. ui8 getHeroAnim() const { return otherheroAnim ? anim : heroAnim; }
  113. };
  114. template <typename T> class PseudoV
  115. {
  116. public:
  117. PseudoV() : offset(0) { }
  118. inline T & operator[](const int & n)
  119. {
  120. return inver[n+offset];
  121. }
  122. inline const T & operator[](const int & n) const
  123. {
  124. return inver[n+offset];
  125. }
  126. void resize(int rest, int before, int after)
  127. {
  128. inver.resize(before + rest + after);
  129. offset=before;
  130. }
  131. int size() const
  132. {
  133. return static_cast<int>(inver.size());
  134. }
  135. private:
  136. int offset;
  137. std::vector<T> inver;
  138. };
  139. class CMapHandler
  140. {
  141. enum class EMapCacheType : ui8
  142. {
  143. TERRAIN, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME, AFTER_LAST
  144. };
  145. /// temporarily caches rescaled frames for map world view redrawing
  146. class CMapCache
  147. {
  148. std::array< std::map<intptr_t, std::shared_ptr<IImage>>, (ui8)EMapCacheType::AFTER_LAST> data;
  149. float worldViewCachedScale;
  150. public:
  151. CMapCache();
  152. /// destroys all cached data (frees surfaces)
  153. void discardWorldViewCache();
  154. /// updates scale and determines if currently cached data is still valid
  155. void updateWorldViewScale(float scale);
  156. /// asks for cached data; @returns cached data if found, new scaled surface otherwise, may return nullptr in case of scaling error
  157. std::shared_ptr<IImage> requestWorldViewCacheOrCreate(EMapCacheType type, std::shared_ptr<IImage> fullSurface);
  158. };
  159. /// helper struct to pass around resolved bitmaps of an object; images can be nullptr if object doesn't have bitmap of that type
  160. struct AnimBitmapHolder
  161. {
  162. std::shared_ptr<IImage> objBitmap; // main object bitmap
  163. std::shared_ptr<IImage> flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
  164. bool isMoving; // indicates if the object is moving (again, heroes/boats only)
  165. AnimBitmapHolder(std::shared_ptr<IImage> objBitmap_ = nullptr, std::shared_ptr<IImage> flagBitmap_ = nullptr, bool moving = false)
  166. : objBitmap(objBitmap_),
  167. flagBitmap(flagBitmap_),
  168. isMoving(moving)
  169. {}
  170. };
  171. class CMapBlitter
  172. {
  173. protected:
  174. const int FRAMES_PER_MOVE_ANIM_GROUP = 8;
  175. CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
  176. int tileSize; // size of a tile drawn on map [in pixels]
  177. int halfTileSizeCeil; // half of the tile size, rounded up
  178. int3 tileCount; // number of tiles in current viewport
  179. int3 topTile; // top-left tile of the viewport
  180. int3 initPos; // starting drawing position [in pixels]
  181. int3 pos; // current position [in tiles]
  182. int3 realPos; // current position [in pixels]
  183. Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
  184. Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
  185. const MapDrawingInfo * info; // data for drawing passed from outside
  186. /// general drawing method, called internally by more specialized ones
  187. virtual void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const = 0;
  188. // first drawing pass
  189. /// draws terrain bitmap (or custom bitmap if applicable) on current tile
  190. virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
  191. /// draws a river segment on current tile
  192. virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
  193. /// draws a road segment on current tile
  194. virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
  195. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  196. virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
  197. virtual void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, bool moving) const;
  198. virtual void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
  199. // second drawing pass
  200. /// current tile: draws overlay over the map, used to draw world view icons
  201. virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
  202. /// draws fog of war on current tile
  203. virtual void drawFow(SDL_Surface * targetSurf) const;
  204. /// draws map border frame on current position
  205. virtual void drawFrame(SDL_Surface * targetSurf) const;
  206. /// draws additional icons (for VIEW_AIR, VIEW_EARTH spells atm)
  207. virtual void drawOverlayEx(SDL_Surface * targetSurf);
  208. // third drawing pass
  209. /// custom post-processing, if needed (used by puzzle view)
  210. virtual void postProcessing(SDL_Surface * targetSurf) const {}
  211. // misc methods
  212. /// initializes frame-drawing (called at the start of every redraw)
  213. virtual void init(const MapDrawingInfo * drawingInfo) = 0;
  214. /// calculates clip region for map viewport
  215. virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0;
  216. virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const;
  217. virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
  218. virtual bool canDrawObject(const CGObjectInstance * obj) const;
  219. virtual bool canDrawCurrentTile() const;
  220. // internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
  221. AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
  222. AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
  223. std::shared_ptr<IImage> findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
  224. std::shared_ptr<IImage> findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
  225. std::shared_ptr<IImage> findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int group, ui8 dir) const;
  226. std::shared_ptr<IImage> findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const;
  227. public:
  228. CMapBlitter(CMapHandler * p);
  229. virtual ~CMapBlitter();
  230. void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
  231. /// helper method that chooses correct bitmap(s) for given object
  232. AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim) const;
  233. };
  234. class CMapNormalBlitter : public CMapBlitter
  235. {
  236. protected:
  237. void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
  238. void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
  239. void init(const MapDrawingInfo * info) override;
  240. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  241. public:
  242. CMapNormalBlitter(CMapHandler * parent);
  243. virtual ~CMapNormalBlitter(){}
  244. };
  245. class CMapWorldViewBlitter : public CMapBlitter
  246. {
  247. private:
  248. std::shared_ptr<IImage> objectToIcon(Obj id, si32 subId, PlayerColor owner) const;
  249. protected:
  250. void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
  251. void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  252. void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
  253. void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, bool moving) const override;
  254. void drawFrame(SDL_Surface * targetSurf) const override {}
  255. void drawOverlayEx(SDL_Surface * targetSurf) override;
  256. void init(const MapDrawingInfo * info) override;
  257. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  258. ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
  259. void calculateWorldViewCameraPos();
  260. public:
  261. CMapWorldViewBlitter(CMapHandler * parent);
  262. virtual ~CMapWorldViewBlitter(){}
  263. };
  264. class CMapPuzzleViewBlitter : public CMapNormalBlitter
  265. {
  266. std::vector<int> unblittableObjects;
  267. void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  268. void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
  269. void postProcessing(SDL_Surface * targetSurf) const override;
  270. bool canDrawObject(const CGObjectInstance * obj) const override;
  271. bool canDrawCurrentTile() const override { return true; }
  272. public:
  273. CMapPuzzleViewBlitter(CMapHandler * parent);
  274. };
  275. CMapCache cache;
  276. CMapBlitter * normalBlitter;
  277. CMapBlitter * worldViewBlitter;
  278. CMapBlitter * puzzleViewBlitter;
  279. std::map<int, std::pair<int3, CFadeAnimation*>> fadeAnims;
  280. int fadeAnimCounter;
  281. CMapBlitter * resolveBlitter(const MapDrawingInfo * info) const;
  282. bool updateObjectsFade();
  283. bool startObjectFade(TerrainTileObject & obj, bool in, int3 pos);
  284. void initObjectRects();
  285. void initBorderGraphics();
  286. void initTerrainGraphics();
  287. void prepareFOWDefs();
  288. public:
  289. boost::multi_array<TerrainTile2, 3> ttiles; //informations about map tiles [z][x][y]
  290. int3 sizes; //map size (x = width, y = height, z = number of levels)
  291. const CMap * map;
  292. // Max number of tiles that will fit in the map screen. Tiles
  293. // can be partial on each edges.
  294. int tilesW;
  295. int tilesH;
  296. // size of each side of the frame around the whole map, in tiles
  297. int frameH;
  298. int frameW;
  299. // Coord in pixels of the top left corner of the top left tile to
  300. // draw. Values range is [-31..0]. A negative value
  301. // implies that part of the tile won't be displayed.
  302. int offsetX;
  303. int offsetY;
  304. //terrain graphics
  305. //FIXME: unique_ptr should be enough, but fails to compile in MSVS 2013
  306. typedef std::map<std::string, std::array<std::shared_ptr<CAnimation>, 4>> TFlippedAnimations; //[type, rotation]
  307. typedef std::map<std::string, std::vector<std::array<std::shared_ptr<IImage>, 4>>> TFlippedCache;//[type, view type, rotation]
  308. TFlippedAnimations terrainAnimations;//[terrain type, rotation]
  309. TFlippedCache terrainImages;//[terrain type, view type, rotation]
  310. TFlippedAnimations roadAnimations;//[road type, rotation]
  311. TFlippedCache roadImages;//[road type, view type, rotation]
  312. TFlippedAnimations riverAnimations;//[river type, rotation]
  313. TFlippedCache riverImages;//[river type, view type, rotation]
  314. //Fog of War cache (not owned)
  315. std::vector<std::shared_ptr<IImage>> FoWfullHide;
  316. boost::multi_array<ui8, 3> hideBitmap; //frame indexes (in FoWfullHide) of graphic that should be used to fully hide a tile
  317. std::vector<std::shared_ptr<IImage>> FoWpartialHide;
  318. //edge graphics
  319. std::unique_ptr<CAnimation> egdeAnimation;
  320. std::vector<std::shared_ptr<IImage>> egdeImages;//cache of links to egdeAnimation (for faster access)
  321. PseudoV< PseudoV< PseudoV <ui8> > > edgeFrames; //frame indexes (in egdeImages) of tile outside of map
  322. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  323. CMapHandler();
  324. ~CMapHandler();
  325. void getTerrainDescr(const int3 & pos, std::string & out, bool isRMB) const; // isRMB = whether Right Mouse Button is clicked
  326. bool printObject(const CGObjectInstance * obj, bool fadein = false); //puts appropriate things to tiles, so obj will be visible on map
  327. bool hideObject(const CGObjectInstance * obj, bool fadeout = false); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  328. bool hasObjectHole(const int3 & pos) const; // Checks if TerrainTile2 tile has a pit remained after digging.
  329. void init();
  330. EMapAnimRedrawStatus drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim = false);
  331. void updateWater();
  332. /// determines if the map is ready to handle new hero movement (not available during fading animations)
  333. bool canStartHeroMovement();
  334. void discardWorldViewCache();
  335. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  336. };