mapHandler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include "../lib/int3.h"
  3. #include "SDL.h"
  4. /*
  5. * mapHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CGObjectInstance;
  14. class CGHeroInstance;
  15. class CMap;
  16. class CGDefInfo;
  17. class CGObjectInstance;
  18. class CDefHandler;
  19. struct TerrainTile;
  20. struct SDL_Surface;
  21. //struct SDL_Rect;
  22. class CDefEssential;
  23. struct TerrainTile2
  24. {
  25. SDL_Surface * terbitmap; //bitmap of terrain
  26. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > objects; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  27. TerrainTile2();
  28. };
  29. template <typename T> class PseudoV
  30. {
  31. public:
  32. PseudoV() : offset(0) { }
  33. PseudoV(std::vector<T> &src, int rest, int before, int after, const T& fill) : offset(before)
  34. {
  35. inver.resize(before + rest + after);
  36. for(int i=0; i<before;i++)
  37. inver[i] = fill;
  38. for(int i=0;i<src.size();i++)
  39. inver[offset+i] = src[i];
  40. for(int i=src.size(); i<src.size()+after;i++)
  41. inver[offset+i] = fill;
  42. }
  43. inline T & operator[](const int & n)
  44. {
  45. return inver[n+offset];
  46. }
  47. inline const T & operator[](const int & n) const
  48. {
  49. return inver[n+offset];
  50. }
  51. void resize(int rest, int before, int after)
  52. {
  53. inver.resize(before + rest + after);
  54. offset=before;
  55. }
  56. int size() const
  57. {
  58. return inver.size();
  59. }
  60. private:
  61. int offset;
  62. std::vector<T> inver;
  63. };
  64. class CMapHandler
  65. {
  66. public:
  67. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  68. int3 sizes; //map size (x = width, y = height, z = number of levels)
  69. const CMap * map;
  70. // Max number of tiles that will fit in the map screen. Tiles
  71. // can be partial on each edges.
  72. int tilesW;
  73. int tilesH;
  74. // size of each side of the frame around the whole map, in tiles
  75. int frameH;
  76. int frameW;
  77. // Coord in pixels of the top left corner of the top left tile to
  78. // draw. Values range is [-31..0]. A negative value
  79. // implies that part of the tile won't be displayed.
  80. int offsetX;
  81. int offsetY;
  82. //std::set<int> usedHeroes;
  83. std::vector<std::vector<SDL_Surface *> > terrainGraphics; // [terrain id] [view type] [rotation type]
  84. std::vector<CDefEssential *> roadDefs;
  85. std::vector<CDefEssential *> staticRiverDefs;
  86. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  87. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  88. static const bool MARK_BLOCKED_POSITIONS;
  89. static const bool MARK_VISITABLE_POSITIONS;
  90. CMapHandler(); //c-tor
  91. ~CMapHandler(); //d-tor
  92. std::pair<SDL_Surface *, bool> getVisBitmap(const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap) const; //returns appropriate bitmap and info if alpha blitting is necessary
  93. ui8 getPhaseShift(const CGObjectInstance *object) const;
  94. std::vector< std::string > getObjDescriptions(int3 pos); //returns desriptions of objects blocking given position
  95. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  96. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  97. bool printObject(const CGObjectInstance * obj); //puts appropriate things to ttiles, so obj will be visible on map
  98. bool hideObject(const CGObjectInstance * obj); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  99. bool removeObject(CGObjectInstance * obj); //removes object from each place in VCMI (I hope)
  100. void initHeroDef(const CGHeroInstance * h);
  101. void init();
  102. void calculateBlockedPos();
  103. void initObjectRects();
  104. void borderAndTerrainBitmapInit();
  105. void roadsRiverTerrainInit();
  106. void prepareFOWDefs();
  107. void terrainRect(int3 top_tile, ui8 anim, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, bool otherHeroAnim, ui8 heroAnim, SDL_Surface * extSurf, const SDL_Rect * extRect, int moveX, int moveY, bool puzzleMode, int3 grailPosRel) const;
  108. void updateWater();
  109. ui8 getHeroFrameNum(ui8 dir, bool isMoving) const; //terrainRect helper function
  110. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  111. static ui8 getDir(const int3 & a, const int3 & b); //returns direction number in range 0 - 7 (0 is left top, clockwise) [direction: form a to b]
  112. };