mapHandler.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef MAPHANDLER_H
  2. #define MAPHANDLER_H
  3. #include "global.h"
  4. #include <SDL.h>
  5. #include "hch/CDefHandler.h"
  6. #include <boost/logic/tribool.hpp>
  7. #include <list>
  8. #include <set>
  9. const int Woff = 12; //width of map's frame
  10. const int Hoff = 8;
  11. class CGObjectInstance;
  12. class CGHeroInstance;
  13. struct Mapa;
  14. class CGDefInfo;
  15. struct TerrainTile2
  16. {
  17. int3 pos; //this tile's position
  18. EterrainType terType; //type of terrain tile
  19. Eroad malle; //type of road
  20. unsigned char roaddir; //type of road tile
  21. Eriver nuine; //type of river
  22. unsigned char rivdir; //type of river tile
  23. std::vector<SDL_Surface *> terbitmap; //frames of terrain animation
  24. std::vector<SDL_Surface *> rivbitmap; //frames of river animation
  25. std::vector<SDL_Surface *> roadbitmap; //frames of road animation
  26. bool visitable; //false = not visitable; true = visitable
  27. bool blocked; //false = free; true = blocked;
  28. std::vector < std::pair<CGObjectInstance*,SDL_Rect> > objects; //poiters to objects being on this tile with rects to be easier to blit this tile on screen
  29. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero is visiting being on this tile
  30. };
  31. //pathfinder
  32. // map<int,int> iDTerenu=>koszt_pola
  33. // map<int,int> IDdrogi=>koszt_drogi
  34. template <typename T> class PseudoV
  35. {
  36. public:
  37. int offset;
  38. std::vector<T> inver;
  39. PseudoV(){};
  40. PseudoV(std::vector<T> &src, int offset, const T& fill)
  41. {
  42. inver.resize(Offset*2+rest);
  43. offset=Offset;
  44. for(int i=0; i<offset;i++)
  45. inver[i] = fill;
  46. for(int i=0;i<src.size();i++)
  47. inver[offset+i] = src[i];
  48. for(int i=src.size(); i<src.size()+offset;i++)
  49. inver[offset+i] = fill;
  50. }
  51. inline T & operator[](int n)
  52. {
  53. return inver[n+offset];
  54. }
  55. void resize(int rest,int Offset)
  56. {
  57. inver.resize(Offset*2+rest);
  58. offset=Offset;
  59. }
  60. int size() const
  61. {
  62. return inver.size();
  63. }
  64. };
  65. class CMapHandler
  66. {
  67. public:
  68. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles;
  69. int3 sizes;
  70. Mapa * map;
  71. std::set<int> usedHeroes;
  72. CDefHandler * fullHide;
  73. CDefHandler * partialHide;
  74. std::vector< std::vector< std::vector<unsigned char> > > visibility; //true means that pointed place is visible //not used now
  75. //std::vector< std::vector<char> > undVisibility; //true means that pointed place is visible
  76. std::vector<CDefHandler *> roadDefs;
  77. std::vector<CDefHandler *> staticRiverDefs;
  78. std::vector<CDefHandler*> defs;
  79. std::map<std::string, CDefHandler*> loadedDefs; //pointers to loaded defs (key is filename, uppercase)
  80. std::vector<std::vector<std::vector<unsigned char> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  81. void loadDefs();
  82. char & visAccess(int x, int y);
  83. char & undVisAccess(int x, int y);
  84. SDL_Surface * getVisBitmap(int x, int y, std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl);
  85. int getCost(int3 & a, int3 & b, const CGHeroInstance * hero);
  86. std::vector< std::string > getObjDescriptions(int3 pos); //returns desriptions of objects blocking given position
  87. std::vector< CGObjectInstance * > getVisitableObjs(int3 pos); //returns vector of visitable objects at certain position
  88. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  89. std::string getDefName(int id, int subid); //returns name of def for object with given id and subid
  90. bool printObject(CGObjectInstance * obj); //puts appropriate things to ttiles, so obj will be visible on map
  91. bool hideObject(CGObjectInstance * obj); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  92. bool removeObject(CGObjectInstance * obj); //removes object from each place in VCMI (I hope)
  93. bool recalculateHideVisPos(int3& pos); //recalculates position for hidden / visitable positions
  94. bool recalculateHideVisPosUnderObj(CGObjectInstance * obj, bool withBorder = false); //recalculates position for hidden / visitable positions under given object
  95. void init();
  96. int pickHero(int owner);
  97. std::pair<int,int> pickObject(CGObjectInstance *obj);
  98. void randomizeObject(CGObjectInstance *cur);
  99. void calculateBlockedPos();
  100. void initObjectRects();
  101. void borderAndTerrainBitmapInit();
  102. void roadsRiverTerrainInit();
  103. void prepareFOWDefs();
  104. void randomizeObjects();
  105. SDL_Surface * terrainRect(int x, int y, int dx, int dy, int level=0, unsigned char anim=0, std::vector< std::vector< std::vector<unsigned char> > > * visibilityMap = NULL, bool otherHeroAnim = false, unsigned char heroAnim = 0, SDL_Surface * extSurf = NULL, SDL_Rect * extRect = NULL); //if extSurf is specified, blit to it
  106. SDL_Surface * terrBitmap(int x, int y);
  107. SDL_Surface * undTerrBitmap(int x, int y);
  108. std::string getRandomizedDefName(CGDefInfo* di, CGObjectInstance * obj = NULL); //objinstance needed only for heroes and towns
  109. unsigned char getHeroFrameNum(const unsigned char & dir, const bool & isMoving) const; //terrainRect helper function
  110. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  111. static unsigned char 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. };
  113. #endif //MAPHANDLER_H