map.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #ifndef __MAP_H__
  2. #define __MAP_H__
  3. #ifdef _MSC_VER
  4. #pragma warning (disable : 4482)
  5. #endif
  6. #include <cstring>
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. #include <list>
  11. #include "../global.h"
  12. #ifndef _MSC_VER
  13. #include "../hch/CObjectHandler.h"
  14. #include "../hch/CDefObjInfoHandler.h"
  15. #endif
  16. /*
  17. * map.h, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. class CGDefInfo;
  26. class CGObjectInstance;
  27. class CGHeroInstance;
  28. class CGCreature;
  29. class CQuest;
  30. class CGTownInstance;
  31. struct DLL_EXPORT TerrainTile
  32. {
  33. enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock};
  34. enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver};
  35. enum Eroad {dirtRoad=1, grazvelRoad, cobblestoneRoad};
  36. EterrainType tertype; // type of terrain
  37. unsigned char terview; // look of terrain
  38. Eriver nuine; // type of Eriver (0 if there is no river)
  39. unsigned char rivDir; // direction of Eriver
  40. Eroad malle; // type of Eroad (0 if there is no river)
  41. unsigned char roadDir; // direction of Eroad
  42. unsigned char siodmyTajemniczyBajt; //first two bits - how to rotate terrain graphic (next two - river graphic, next two - road); 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favourable Winds effect
  43. bool visitable; //false = not visitable; true = visitable
  44. bool blocked; //false = free; true = blocked;
  45. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  46. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  47. template <typename Handler> void serialize(Handler &h, const int version)
  48. {
  49. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  50. if(!h.saving)
  51. {
  52. visitable = blocked = false;
  53. //these flags (and obj vectors) will be restored in map serialization
  54. }
  55. }
  56. bool entrableTerrain(const TerrainTile *from = NULL) const; //checks if terrain is not a rock. If from is water/land, same type is also required.
  57. bool entrableTerrain(bool allowLand, bool allowSea) const; //checks if terrain is not a rock. If from is water/land, same type is also required.
  58. bool isClear(const TerrainTile *from = NULL) const; //checks for blocking objs and terraint type (water / land)
  59. };
  60. struct DLL_EXPORT SheroName //name of starting hero
  61. {
  62. int heroID;
  63. std::string heroName;
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & heroID & heroName;
  67. }
  68. };
  69. struct DLL_EXPORT PlayerInfo
  70. {
  71. si32 p7, p8, p9;
  72. ui8 powerPlacehodlers; //q-ty of hero placeholders containing hero type, WARNING: powerPlacehodlers sometimes gives false 0 (eg. even if there is one placeholder), maybe different meaning???
  73. ui8 canHumanPlay;
  74. ui8 canComputerPlay;
  75. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  76. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  77. ui8 isFactionRandom;
  78. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  79. std::string mainHeroName;
  80. std::vector<SheroName> heroesNames;
  81. ui8 hasMainTown;
  82. ui8 generateHeroAtMainTown;
  83. int3 posOfMainTown;
  84. ui8 team;
  85. ui8 generateHero;
  86. PlayerInfo(): p7(0), p8(0), p9(0), canHumanPlay(0), canComputerPlay(0),
  87. AITactic(0), allowedFactions(0), isFactionRandom(0),
  88. mainHeroPortrait(0), hasMainTown(0), generateHeroAtMainTown(0),
  89. team(255), generateHero(0) {};
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  93. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  94. posOfMainTown & team & generateHero;
  95. }
  96. };
  97. struct DLL_EXPORT LossCondition
  98. {
  99. ElossCon typeOfLossCon;
  100. int3 pos;
  101. si32 timeLimit; // in days; -1 if not used
  102. const CGObjectInstance *obj; //set during map parsing: hero/town (depending on typeOfLossCon); NULL if not used
  103. template <typename Handler> void serialize(Handler &h, const int version)
  104. {
  105. h & typeOfLossCon & pos & timeLimit & obj;
  106. }
  107. LossCondition();
  108. };
  109. struct DLL_EXPORT CVictoryCondition
  110. {
  111. EvictoryConditions condition; //ID of condition
  112. ui8 allowNormalVictory, appliesToAI;
  113. int3 pos; //pos of city to upgrade (3); pos of town to build grail, {-1,-1,-1} if not relevant (4); hero pos (5); town pos(6); monster pos (7); destination pos(8)
  114. si32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  115. si32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3);
  116. const CGObjectInstance *obj; //object of specific monster / city / hero instance (NULL if not used); set during map parsing
  117. CVictoryCondition();
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. h & condition & allowNormalVictory & appliesToAI & pos & ID & count & obj;
  121. }
  122. };
  123. struct DLL_EXPORT Rumor
  124. {
  125. std::string name, text;
  126. template <typename Handler> void serialize(Handler &h, const int version)
  127. {
  128. h & name & text;
  129. }
  130. };
  131. struct DLL_EXPORT DisposedHero
  132. {
  133. ui32 ID;
  134. ui16 portrait; //0xFF - default
  135. std::string name;
  136. ui8 players; //who can hire this hero (bitfield)
  137. template <typename Handler> void serialize(Handler &h, const int version)
  138. {
  139. h & ID & portrait & name & players;
  140. }
  141. };
  142. class DLL_EXPORT CMapEvent
  143. {
  144. public:
  145. std::string name, message;
  146. std::vector<si32> resources; //gained / taken resources
  147. ui8 players; //affected players
  148. ui8 humanAffected;
  149. ui8 computerAffected;
  150. ui32 firstOccurence;
  151. ui32 nextOccurence; //after nextOccurance day event will occur; if it it 0, event occures only one time;
  152. template <typename Handler> void serialize(Handler &h, const int version)
  153. {
  154. h & name & message & resources
  155. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  156. }
  157. bool operator<(const CMapEvent &b) const
  158. {
  159. return firstOccurence < b.firstOccurence;
  160. }
  161. };
  162. class DLL_EXPORT CMapHeader
  163. {
  164. public:
  165. enum Eformat {invalid, WoG=0x33, AB=0x15, RoE=0x0e, SoD=0x1c};
  166. Eformat version; // version of map Eformat
  167. ui8 areAnyPLayers; // if there are any playable players on map
  168. si32 height, width, twoLevel; //sizes
  169. std::string name; //name of map
  170. std::string description; //and description
  171. ui8 difficulty; // 0 easy - 4 impossible
  172. ui8 levelLimit;
  173. LossCondition lossCondition;
  174. CVictoryCondition victoryCondition; //victory conditions
  175. std::vector<PlayerInfo> players; // info about players - size 8
  176. ui8 howManyTeams;
  177. std::vector<ui8> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  178. std::vector<ui16> placeholdedHeroes; //ID of types of heroes in placeholders
  179. void initFromMemory(const unsigned char *bufor, int &i);
  180. void loadViCLossConditions( const unsigned char * bufor, int &i);
  181. void loadPlayerInfo( int &pom, const unsigned char * bufor, int &i);
  182. CMapHeader(const unsigned char *map); //an argument is a reference to string described a map (unpacked)
  183. CMapHeader();
  184. virtual ~CMapHeader();
  185. template <typename Handler> void serialize(Handler &h, const int Version)
  186. {
  187. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers;
  188. h & players & lossCondition & victoryCondition & howManyTeams;
  189. }
  190. };
  191. struct DLL_EXPORT Mapa : public CMapHeader
  192. {
  193. ui32 checksum;
  194. TerrainTile*** terrain;
  195. std::vector<Rumor> rumors;
  196. std::vector<DisposedHero> disposedHeroes;
  197. std::vector<CGHeroInstance*> predefinedHeroes;
  198. std::vector<CGDefInfo *> defy; // list of .def files with definitions from .h3m (may be custom)
  199. std::vector<ui8> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  200. std::vector<ui8> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  201. std::vector<ui8> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  202. std::list<CMapEvent*> events;
  203. int3 grailPos;
  204. int grailRadious;
  205. std::vector<CGObjectInstance*> objects;
  206. std::vector<CGHeroInstance*> heroes;
  207. std::vector<CGTownInstance*> towns;
  208. std::map<ui16, CGCreature*> monsters;
  209. std::map<ui16, CGHeroInstance*> heroesToBeat;
  210. void initFromBytes( const unsigned char * bufor); //creates map from decompressed .h3m data
  211. void readEvents( const unsigned char * bufor, int &i);
  212. void readObjects( const unsigned char * bufor, int &i);
  213. void loadQuest( CQuest * guard, const unsigned char * bufor, int & i);
  214. void readDefInfo( const unsigned char * bufor, int &i);
  215. void readTerrain( const unsigned char * bufor, int &i);
  216. void readPredefinedHeroes( const unsigned char * bufor, int &i);
  217. void readHeader( const unsigned char * bufor, int &i);
  218. void readRumors( const unsigned char * bufor, int &i);
  219. void loadHero( CGObjectInstance * &nobj, const unsigned char * bufor, int &i);
  220. void loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid);
  221. int loadSeerHut( const unsigned char * bufor, int i, CGObjectInstance *& nobj);
  222. void checkForObjectives();
  223. void addBlockVisTiles(CGObjectInstance * obj);
  224. void removeBlockVisTiles(CGObjectInstance * obj, bool total=false);
  225. Mapa(std::string filename); //creates map structure from .h3m file
  226. Mapa();
  227. ~Mapa();
  228. TerrainTile &getTile(const int3 & tile);
  229. const TerrainTile &getTile(const int3 & tile) const;
  230. CGHeroInstance * getHero(int ID, int mode=0);
  231. bool isInTheMap(const int3 &pos) const;
  232. bool isWaterTile(const int3 &pos) const; //out-of-pos safe
  233. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  234. {
  235. h & static_cast<CMapHeader&>(*this);
  236. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & allowedHeroes & events & grailPos;
  237. h & monsters & heroesToBeat; //hoprfully serialization is now automagical?
  238. //TODO: viccondetails
  239. if(h.saving)
  240. {
  241. //saving terrain
  242. for (int i = 0; i < width ; i++)
  243. for (int j = 0; j < height ; j++)
  244. for (int k = 0; k <= twoLevel ; k++)
  245. h & terrain[i][j][k];
  246. }
  247. else
  248. {
  249. //loading terrain
  250. terrain = new TerrainTile**[width]; // allocate memory
  251. for (int ii=0;ii<width;ii++)
  252. {
  253. terrain[ii] = new TerrainTile*[height]; // allocate memory
  254. for(int jj=0;jj<height;jj++)
  255. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  256. }
  257. for (int i = 0; i < width ; i++)
  258. for (int j = 0; j < height ; j++)
  259. for (int k = 0; k <= twoLevel ; k++)
  260. h & terrain[i][j][k];
  261. }
  262. h & defy & objects;
  263. // //definfos
  264. // std::vector<CGDefInfo*> defs;
  265. //
  266. // if(h.saving) //create vector with all defs used on map
  267. // {
  268. // for(unsigned int i=0; i<objects.size(); i++)
  269. // if(objects[i])
  270. // objects[i]->defInfo->serial = -1; //set serial to serial -1 - indicates that def is not present in defs vector
  271. //
  272. // for(unsigned int i=0; i<objects.size(); i++)
  273. // {
  274. // if(!objects[i]) continue;
  275. // CGDefInfo *cur = objects[i]->defInfo;
  276. // if(cur->serial < 0)
  277. // {
  278. // cur->serial = defs.size();
  279. // defs.push_back(cur);
  280. // }
  281. // }
  282. // }
  283. //
  284. // h & ((h.saving) ? defs : defy);
  285. // //objects
  286. // if(h.saving)
  287. // {
  288. // ui32 hlp = objects.size();
  289. // h & hlp;
  290. // }
  291. // else
  292. // {
  293. // ui32 hlp;
  294. // h & hlp;
  295. // objects.resize(hlp);
  296. // }
  297. //static members
  298. h & CGTeleport::objs;
  299. h & CGTeleport::gates;
  300. h & CGKeys::playerKeyMap;
  301. h & CGMagi::eyelist;
  302. h & CGPyramid::pyramidConfig;
  303. h & CGObelisk::obeliskCount & CGObelisk::visited;
  304. h & CGTownInstance::merchantArtifacts;
  305. // for(unsigned int i=0; i<objects.size(); i++)
  306. // {
  307. // CGObjectInstance *&obj = objects[i];
  308. // h & obj;
  309. //
  310. // if (obj)
  311. // {
  312. // si32 shlp;
  313. // //definfo
  314. // h & (h.saving ? (shlp=obj->defInfo->serial) : shlp); //read / write pos of definfo in defs vector
  315. // if(!h.saving)
  316. // obj->defInfo = defy[shlp];
  317. // }
  318. // }
  319. if(!h.saving)
  320. {
  321. for(unsigned int i=0; i<objects.size(); i++)
  322. {
  323. if(!objects[i]) continue;
  324. if(objects[i]->ID == HEROI_TYPE)
  325. heroes.push_back(static_cast<CGHeroInstance*>(objects[i]));
  326. else if(objects[i]->ID == TOWNI_TYPE)
  327. towns.push_back(static_cast<CGTownInstance*>(objects[i]));
  328. addBlockVisTiles(objects[i]); //recreate blockvis map
  329. }
  330. for(unsigned int i=0; i<heroes.size(); i++) //if hero is visiting/garrisoned in town set appropriate pointers
  331. {
  332. int3 vistile = heroes[i]->pos; vistile.x++;
  333. for(unsigned int j=0; j<towns.size(); j++)
  334. {
  335. if(vistile == towns[j]->pos) //hero stands on the town entrance
  336. {
  337. if(heroes[i]->inTownGarrison)
  338. {
  339. towns[j]->garrisonHero = heroes[i];
  340. removeBlockVisTiles(heroes[i]);
  341. }
  342. else
  343. {
  344. towns[j]->visitingHero = heroes[i];
  345. }
  346. heroes[i]->visitedTown = towns[j];
  347. break;
  348. }
  349. }
  350. vistile.x -= 2; //manifest pos
  351. const TerrainTile &t = getTile(vistile);
  352. if(t.tertype != TerrainTile::water) continue;
  353. //hero stands on the water - he must be in the boat
  354. for(unsigned int j = 0; j < t.visitableObjects.size(); j++)
  355. {
  356. if(t.visitableObjects[j]->ID == 8)
  357. {
  358. CGBoat *b = static_cast<CGBoat *>(t.visitableObjects[j]);
  359. heroes[i]->boat = b;
  360. b->hero = heroes[i];
  361. removeBlockVisTiles(b);
  362. break;
  363. }
  364. }
  365. } //heroes loop
  366. } //!saving
  367. }
  368. };
  369. #endif // __MAP_H__