map.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 CQuest;
  29. class CGTownInstance;
  30. enum ESortBy{_playerAm, _size, _format, _name, _viccon, _loscon};
  31. enum EDefType {TOWN_DEF, HERO_DEF, CREATURES_DEF, SEERHUT_DEF, RESOURCE_DEF, TERRAINOBJ_DEF,
  32. EVENTOBJ_DEF, SIGN_DEF, GARRISON_DEF, ARTIFACT_DEF, WITCHHUT_DEF, SCHOLAR_DEF, PLAYERONLY_DEF,
  33. SHRINE_DEF, SPELLSCROLL_DEF, PANDORA_DEF, GRAIL_DEF, CREGEN_DEF, CREGEN2_DEF, CREGEN3_DEF,
  34. BORDERGUARD_DEF, HEROPLACEHOLDER_DEF};
  35. class DLL_EXPORT CSpecObjInfo
  36. {
  37. public:
  38. virtual ~CSpecObjInfo(){};
  39. };
  40. class DLL_EXPORT CCreGenObjInfo : public CSpecObjInfo
  41. {
  42. public:
  43. unsigned char player; //owner
  44. bool asCastle;
  45. int identifier;
  46. unsigned char castles[2]; //allowed castles
  47. };
  48. class DLL_EXPORT CCreGen2ObjInfo : public CSpecObjInfo
  49. {
  50. public:
  51. unsigned char player; //owner
  52. bool asCastle;
  53. int identifier;
  54. unsigned char castles[2]; //allowed castles
  55. unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  56. };
  57. class DLL_EXPORT CCreGen3ObjInfo : public CSpecObjInfo
  58. {
  59. public:
  60. unsigned char player; //owner
  61. unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  62. };
  63. struct DLL_EXPORT TerrainTile
  64. {
  65. enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock};
  66. enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver};
  67. enum Eroad {dirtRoad=1, grazvelRoad, cobblestoneRoad};
  68. EterrainType tertype; // type of terrain
  69. unsigned char terview; // look of terrain
  70. Eriver nuine; // type of Eriver (0 if there is no river)
  71. unsigned char rivDir; // direction of Eriver
  72. Eroad malle; // type of Eroad (0 if there is no river)
  73. unsigned char roadDir; // direction of Eroad
  74. unsigned char siodmyTajemniczyBajt; //bitfield, info whether this tile is coastal and how to rotate tile graphics
  75. bool visitable; //false = not visitable; true = visitable
  76. bool blocked; //false = free; true = blocked;
  77. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  78. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  79. template <typename Handler> void serialize(Handler &h, const int version)
  80. {
  81. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  82. if(!h.saving)
  83. {
  84. visitable = blocked = false;
  85. //these flags (and obj vectors) will be restored in map serialization
  86. }
  87. }
  88. };
  89. struct DLL_EXPORT SheroName //name of starting hero
  90. {
  91. int heroID;
  92. std::string heroName;
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. h & heroID & heroName;
  96. }
  97. };
  98. struct DLL_EXPORT PlayerInfo
  99. {
  100. si32 p7, p8, p9;
  101. ui8 canHumanPlay;
  102. ui8 canComputerPlay;
  103. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  104. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  105. ui8 isFactionRandom;
  106. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  107. std::string mainHeroName;
  108. std::vector<SheroName> heroesNames;
  109. ui8 hasMainTown;
  110. ui8 generateHeroAtMainTown;
  111. int3 posOfMainTown;
  112. ui8 team;
  113. ui8 generateHero;
  114. PlayerInfo(): p7(0), p8(0), p9(0), canHumanPlay(0), canComputerPlay(0),
  115. AITactic(0), allowedFactions(0), isFactionRandom(0),
  116. mainHeroPortrait(0), hasMainTown(0), generateHeroAtMainTown(0),
  117. team(255), generateHero(0) {};
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  121. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  122. posOfMainTown & team & generateHero;
  123. }
  124. };
  125. struct DLL_EXPORT LossCondition
  126. {
  127. ElossCon typeOfLossCon;
  128. int3 pos;
  129. si32 timeLimit; // in days; -1 if not used
  130. const CGObjectInstance *obj; //set during map parsing: hero/town (depending on typeOfLossCon); NULL if not used
  131. template <typename Handler> void serialize(Handler &h, const int version)
  132. {
  133. h & typeOfLossCon & pos & timeLimit & obj;
  134. }
  135. LossCondition();
  136. };
  137. struct DLL_EXPORT CVictoryCondition
  138. {
  139. EvictoryConditions condition; //ID of condition
  140. ui8 allowNormalVictory, appliesToAI;
  141. 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)
  142. si32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  143. si32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3);
  144. const CGObjectInstance *obj; //object of specific monster / city / hero instance (NULL if not used); set during map parsing
  145. CVictoryCondition();
  146. template <typename Handler> void serialize(Handler &h, const int version)
  147. {
  148. h & condition & allowNormalVictory & appliesToAI & pos & ID & count & obj;
  149. }
  150. };
  151. struct DLL_EXPORT Rumor
  152. {
  153. std::string name, text;
  154. template <typename Handler> void serialize(Handler &h, const int version)
  155. {
  156. h & name & text;
  157. }
  158. };
  159. struct DLL_EXPORT DisposedHero
  160. {
  161. ui32 ID;
  162. ui16 portrait; //0xFF - default
  163. std::string name;
  164. ui8 players; //who can hire this hero (bitfield)
  165. template <typename Handler> void serialize(Handler &h, const int version)
  166. {
  167. h & ID & portrait & name & players;
  168. }
  169. };
  170. class DLL_EXPORT CMapEvent
  171. {
  172. public:
  173. std::string name, message;
  174. std::vector<si32> resources; //gained / taken resources
  175. ui8 players; //affected players
  176. ui8 humanAffected;
  177. ui8 computerAffected;
  178. ui32 firstOccurence;
  179. ui32 nextOccurence; //after nextOccurance day event will occur; if it it 0, event occures only one time;
  180. template <typename Handler> void serialize(Handler &h, const int version)
  181. {
  182. h & name & message & resources
  183. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  184. }
  185. bool operator<(const CMapEvent &b) const
  186. {
  187. return firstOccurence < b.firstOccurence;
  188. }
  189. };
  190. class DLL_EXPORT CMapHeader
  191. {
  192. public:
  193. enum Eformat {invalid, WoG=0x33, AB=0x15, RoE=0x0e, SoD=0x1c};
  194. Eformat version; // version of map Eformat
  195. ui8 areAnyPLayers; // if there are any playable players on map
  196. si32 height, width, twoLevel; //sizes
  197. std::string name; //name of map
  198. std::string description; //and description
  199. ui8 difficulty; // 0 easy - 4 impossible
  200. ui8 levelLimit;
  201. LossCondition lossCondition;
  202. CVictoryCondition victoryCondition; //victory conditions
  203. std::vector<PlayerInfo> players; // info about players - size 8
  204. ui8 howManyTeams;
  205. std::vector<ui8> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  206. void initFromMemory(const unsigned char *bufor, int &i);
  207. void loadViCLossConditions( const unsigned char * bufor, int &i);
  208. void loadPlayerInfo( int &pom, const unsigned char * bufor, int &i);
  209. CMapHeader(const unsigned char *map); //an argument is a reference to string described a map (unpacked)
  210. CMapHeader();
  211. virtual ~CMapHeader();
  212. template <typename Handler> void serialize(Handler &h, const int Version)
  213. {
  214. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers;
  215. h & players & lossCondition & victoryCondition & howManyTeams;
  216. }
  217. };
  218. class DLL_EXPORT CMapInfo : public CMapHeader
  219. {
  220. public:
  221. ui8 seldiff; //selected difficulty (only in saved games)
  222. std::string filename;
  223. std::string date;
  224. int playerAmnt, humenPlayers;
  225. CMapInfo();
  226. CMapInfo(const std::string &fname, const unsigned char *map);
  227. void init(const std::string &fname, const unsigned char *map);
  228. void countPlayers();
  229. };
  230. class DLL_EXPORT mapSorter
  231. {
  232. public:
  233. ESortBy sortBy;
  234. bool operator()(const CMapHeader *a, const CMapHeader *b)
  235. {
  236. switch (sortBy)
  237. {
  238. case _format:
  239. return (a->version<b->version);
  240. break;
  241. case _loscon:
  242. return (a->lossCondition.typeOfLossCon<b->lossCondition.typeOfLossCon);
  243. break;
  244. case _playerAm:
  245. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  246. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  247. for (int i=0;i<8;i++)
  248. {
  249. if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  250. else if (a->players[i].canComputerPlay) {playerAmntA++;}
  251. if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  252. else if (b->players[i].canComputerPlay) {playerAmntB++;}
  253. }
  254. if (playerAmntB!=playerAmntA)
  255. return (playerAmntA<playerAmntB);
  256. else
  257. return (humenPlayersA<humenPlayersB);
  258. break;
  259. case _size:
  260. return (a->width<b->width);
  261. break;
  262. case _viccon:
  263. return (a->victoryCondition.condition < b->victoryCondition.condition);
  264. break;
  265. case _name:
  266. return (a->name<b->name);
  267. break;
  268. default:
  269. return (a->name<b->name);
  270. break;
  271. }
  272. };
  273. mapSorter(ESortBy es):sortBy(es){};
  274. };
  275. struct DLL_EXPORT Mapa : public CMapHeader
  276. {
  277. ui32 checksum;
  278. TerrainTile*** terrain;
  279. std::vector<Rumor> rumors;
  280. std::vector<DisposedHero> disposedHeroes;
  281. std::vector<CGHeroInstance*> predefinedHeroes;
  282. std::vector<CGDefInfo *> defy; // list of .def files with definitions from .h3m (may be custom)
  283. std::vector<ui8> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  284. std::vector<ui8> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  285. std::vector<ui8> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  286. std::list<CMapEvent*> events;
  287. int3 grailPos;
  288. int grailRadious;
  289. std::vector<CGObjectInstance*> objects;
  290. std::vector<CGHeroInstance*> heroes;
  291. std::vector<CGTownInstance*> towns;
  292. void initFromBytes( const unsigned char * bufor); //creates map from decompressed .h3m data
  293. void readEvents( const unsigned char * bufor, int &i);
  294. void readObjects( const unsigned char * bufor, int &i);
  295. void loadQuest( CQuest * guard, const unsigned char * bufor, int & i);
  296. void readDefInfo( const unsigned char * bufor, int &i);
  297. void readTerrain( const unsigned char * bufor, int &i);
  298. void readPredefinedHeroes( const unsigned char * bufor, int &i);
  299. void readHeader( const unsigned char * bufor, int &i);
  300. void readRumors( const unsigned char * bufor, int &i);
  301. void loadHero( CGObjectInstance * &nobj, const unsigned char * bufor, int &i);
  302. void loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid);
  303. int loadSeerHut( const unsigned char * bufor, int i, CGObjectInstance *& nobj);
  304. void checkForObjectives();
  305. void addBlockVisTiles(CGObjectInstance * obj);
  306. void removeBlockVisTiles(CGObjectInstance * obj, bool total=false);
  307. Mapa(std::string filename); //creates map structure from .h3m file
  308. Mapa();
  309. ~Mapa();
  310. TerrainTile &getTile(int3 tile);
  311. const TerrainTile &getTile(int3 tile) const;
  312. CGHeroInstance * getHero(int ID, int mode=0);
  313. bool isInTheMap(const int3 &pos) const;
  314. bool isWaterTile(const int3 &pos) const; //out-of-pos safe
  315. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  316. {
  317. h & static_cast<CMapHeader&>(*this);
  318. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & allowedHeroes & events & grailPos;
  319. //TODO: viccondetails
  320. if(h.saving)
  321. {
  322. //saving terrain
  323. for (int i = 0; i < width ; i++)
  324. for (int j = 0; j < height ; j++)
  325. for (int k = 0; k <= twoLevel ; k++)
  326. h & terrain[i][j][k];
  327. }
  328. else
  329. {
  330. //loading terrain
  331. terrain = new TerrainTile**[width]; // allocate memory
  332. for (int ii=0;ii<width;ii++)
  333. {
  334. terrain[ii] = new TerrainTile*[height]; // allocate memory
  335. for(int jj=0;jj<height;jj++)
  336. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  337. }
  338. for (int i = 0; i < width ; i++)
  339. for (int j = 0; j < height ; j++)
  340. for (int k = 0; k <= twoLevel ; k++)
  341. h & terrain[i][j][k];
  342. }
  343. //definfos
  344. std::vector<CGDefInfo*> defs;
  345. if(h.saving) //create vector with all defs used on map
  346. {
  347. for(unsigned int i=0; i<objects.size(); i++)
  348. if(objects[i])
  349. objects[i]->defInfo->serial = -1; //set serial to serial -1 - indicates that def is not present in defs vector
  350. for(unsigned int i=0; i<objects.size(); i++)
  351. {
  352. if(!objects[i]) continue;
  353. CGDefInfo *cur = objects[i]->defInfo;
  354. if(cur->serial < 0)
  355. {
  356. cur->serial = defs.size();
  357. defs.push_back(cur);
  358. }
  359. }
  360. }
  361. h & ((h.saving) ? defs : defy);
  362. //objects
  363. if(h.saving)
  364. {
  365. ui32 hlp = objects.size();
  366. h & hlp;
  367. }
  368. else
  369. {
  370. ui32 hlp;
  371. h & hlp;
  372. objects.resize(hlp);
  373. }
  374. //static structures
  375. h & CGTeleport::objs;
  376. h & CGTeleport::gates;
  377. h & CGKeys::playerKeyMap;
  378. h & CGMagi::eyelist;
  379. h & CGPyramid::pyramidConfig;
  380. for(unsigned int i=0; i<objects.size(); i++)
  381. {
  382. CGObjectInstance *&obj = objects[i];
  383. h & obj;
  384. if (obj)
  385. {
  386. si32 shlp;
  387. //definfo
  388. h & (h.saving ? (shlp=obj->defInfo->serial) : shlp); //read / write pos of definfo in defs vector
  389. if(!h.saving)
  390. obj->defInfo = defy[shlp];
  391. }
  392. }
  393. if(!h.saving)
  394. {
  395. for(unsigned int i=0; i<objects.size(); i++)
  396. {
  397. if(!objects[i]) continue;
  398. if(objects[i]->ID == HEROI_TYPE)
  399. heroes.push_back(static_cast<CGHeroInstance*>(objects[i]));
  400. else if(objects[i]->ID == TOWNI_TYPE)
  401. towns.push_back(static_cast<CGTownInstance*>(objects[i]));
  402. addBlockVisTiles(objects[i]); //recreate blockvis map
  403. }
  404. for(unsigned int i=0; i<heroes.size(); i++) //if hero is visiting/garrisoned in town set appropriate pointers
  405. {
  406. int3 vistile = heroes[i]->pos; vistile.x++;
  407. for(unsigned int j=0; j<towns.size(); j++)
  408. {
  409. if(vistile == towns[j]->pos) //hero stands on the town entrance
  410. {
  411. if(heroes[i]->inTownGarrison)
  412. {
  413. towns[j]->garrisonHero = heroes[i];
  414. removeBlockVisTiles(heroes[i]);
  415. }
  416. else
  417. {
  418. towns[j]->visitingHero = heroes[i];
  419. }
  420. heroes[i]->visitedTown = towns[j];
  421. break;
  422. }
  423. }
  424. vistile.x -= 2; //manifest pos
  425. const TerrainTile &t = getTile(vistile);
  426. if(t.tertype != TerrainTile::water) continue;
  427. //hero stands on the water - he must be in the boat
  428. for(unsigned int j = 0; j < t.visitableObjects.size(); j++)
  429. {
  430. if(t.visitableObjects[j]->ID == 8)
  431. {
  432. CGBoat *b = static_cast<CGBoat *>(t.visitableObjects[j]);
  433. heroes[i]->boat = b;
  434. b->hero = heroes[i];
  435. removeBlockVisTiles(b);
  436. break;
  437. }
  438. }
  439. } //heroes loop
  440. } //!saving
  441. }
  442. };
  443. #endif // __MAP_H__