map.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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{_name, _playerAm, _size, _format, _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. EterrainType tertype; // type of terrain
  66. unsigned char terview; // look of terrain
  67. Eriver nuine; // type of Eriver (0 if there is no Eriver)
  68. unsigned char rivDir; // direction of Eriver
  69. Eroad malle; // type of Eroad (0 if there is no Eriver)
  70. unsigned char roadDir; // direction of Eroad
  71. unsigned char siodmyTajemniczyBajt; //bitfield, info whether this tile is coastal and how to rotate tile graphics
  72. bool visitable; //false = not visitable; true = visitable
  73. bool blocked; //false = free; true = blocked;
  74. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  75. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  76. template <typename Handler> void serialize(Handler &h, const int version)
  77. {
  78. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  79. if(!h.saving)
  80. {
  81. visitable = blocked = false;
  82. //these flags (and obj vectors) will be restored in map serialization
  83. }
  84. }
  85. };
  86. struct DLL_EXPORT SheroName //name of starting hero
  87. {
  88. int heroID;
  89. std::string heroName;
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & heroID & heroName;
  93. }
  94. };
  95. struct DLL_EXPORT PlayerInfo
  96. {
  97. si32 p7, p8, p9;
  98. ui8 canHumanPlay;
  99. ui8 canComputerPlay;
  100. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  101. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  102. ui8 isFactionRandom;
  103. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  104. std::string mainHeroName;
  105. std::vector<SheroName> heroesNames;
  106. ui8 hasMainTown;
  107. ui8 generateHeroAtMainTown;
  108. int3 posOfMainTown;
  109. ui8 team;
  110. ui8 generateHero;
  111. template <typename Handler> void serialize(Handler &h, const int version)
  112. {
  113. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  114. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  115. posOfMainTown & team & generateHero;
  116. }
  117. };
  118. struct DLL_EXPORT LossCondition
  119. {
  120. ElossCon typeOfLossCon;
  121. int3 castlePos;
  122. int3 heroPos;
  123. int timeLimit; // in days
  124. template <typename Handler> void serialize(Handler &h, const int version)
  125. {
  126. h & typeOfLossCon & castlePos & heroPos & timeLimit;
  127. }
  128. };
  129. struct DLL_EXPORT CVictoryCondition
  130. {
  131. EvictoryConditions condition; //ID of condition
  132. ui8 allowNormalVictory, appliesToAI;
  133. 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)
  134. ui32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  135. ui32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3);
  136. template <typename Handler> void serialize(Handler &h, const int version)
  137. {
  138. h & condition & allowNormalVictory & appliesToAI & pos & ID & count;
  139. }
  140. };
  141. struct DLL_EXPORT Rumor
  142. {
  143. std::string name, text;
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & name & text;
  147. }
  148. };
  149. struct DLL_EXPORT DisposedHero
  150. {
  151. ui32 ID;
  152. ui16 portrait; //0xFF - default
  153. std::string name;
  154. ui8 players; //who can hire this hero (bitfield)
  155. template <typename Handler> void serialize(Handler &h, const int version)
  156. {
  157. h & ID & portrait & name & players;
  158. }
  159. };
  160. class DLL_EXPORT CMapEvent
  161. {
  162. public:
  163. std::string name, message;
  164. std::vector<si32> resources; //gained / taken resources
  165. ui8 players; //affected players
  166. ui8 humanAffected;
  167. ui8 computerAffected;
  168. ui32 firstOccurence;
  169. ui32 nextOccurence; //after nextOccurance day event will occur; if it it 0, event occures only one time;
  170. template <typename Handler> void serialize(Handler &h, const int version)
  171. {
  172. h & name & message & resources
  173. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  174. }
  175. bool operator<(const CMapEvent &b) const
  176. {
  177. return firstOccurence < b.firstOccurence;
  178. }
  179. };
  180. class DLL_EXPORT CMapHeader
  181. {
  182. public:
  183. Eformat version; // version of map Eformat
  184. ui8 areAnyPLayers; // if there are any playable players on map
  185. si32 height, width, twoLevel; //sizes
  186. std::string name; //name of map
  187. std::string description; //and description
  188. ui8 difficulty; // 0 easy - 4 impossible
  189. ui8 levelLimit;
  190. LossCondition lossCondition;
  191. CVictoryCondition victoryCondition; //victory conditions
  192. std::vector<PlayerInfo> players; // info about players - size 8
  193. std::vector<ui8> teams; // teams[i] = team of player no i
  194. ui8 howManyTeams;
  195. void initFromMemory(unsigned char *bufor, int &i);
  196. void loadViCLossConditions( unsigned char * bufor, int &i);
  197. void loadPlayerInfo( int &pom, unsigned char * bufor, int &i);
  198. CMapHeader(unsigned char *map); //an argument is a reference to string described a map (unpacked)
  199. CMapHeader();
  200. template <typename Handler> void serialize(Handler &h, const int Version)
  201. {
  202. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers;
  203. h & players & teams & lossCondition & victoryCondition & howManyTeams;
  204. }
  205. };
  206. class DLL_EXPORT CMapInfo : public CMapHeader
  207. {
  208. public:
  209. ui8 seldiff; //selected difficulty (only in saved games)
  210. std::string filename;
  211. std::string date;
  212. int playerAmnt, humenPlayers;
  213. CMapInfo(){};
  214. void countPlayers();
  215. CMapInfo(std::string fname, unsigned char *map);
  216. };
  217. class DLL_EXPORT mapSorter
  218. {
  219. public:
  220. ESortBy sortBy;
  221. bool operator()(CMapHeader *a, CMapHeader *b)
  222. {
  223. switch (sortBy)
  224. {
  225. case _format:
  226. return (a->version<b->version);
  227. break;
  228. case _loscon:
  229. return (a->lossCondition.typeOfLossCon<b->lossCondition.typeOfLossCon);
  230. break;
  231. case _playerAm:
  232. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  233. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  234. for (int i=0;i<8;i++)
  235. {
  236. if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  237. else if (a->players[i].canComputerPlay) {playerAmntA++;}
  238. if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  239. else if (b->players[i].canComputerPlay) {playerAmntB++;}
  240. }
  241. if (playerAmntB!=playerAmntA)
  242. return (playerAmntA<playerAmntB);
  243. else
  244. return (humenPlayersA<humenPlayersB);
  245. break;
  246. case _size:
  247. return (a->width<b->width);
  248. break;
  249. case _viccon:
  250. return (a->victoryCondition.condition < b->victoryCondition.condition);
  251. break;
  252. case _name:
  253. return (a->name<b->name);
  254. break;
  255. default:
  256. return (a->name<b->name);
  257. break;
  258. }
  259. };
  260. mapSorter(ESortBy es):sortBy(es){};
  261. };
  262. struct DLL_EXPORT Mapa : public CMapHeader
  263. {
  264. ui32 checksum;
  265. TerrainTile*** terrain;
  266. std::vector<Rumor> rumors;
  267. std::vector<DisposedHero> disposedHeroes;
  268. std::vector<CGHeroInstance*> predefinedHeroes;
  269. std::vector<CGDefInfo *> defy; // list of .def files with definitions from .h3m (may be custom)
  270. std::vector<ui8> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  271. std::vector<ui8> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  272. std::vector<ui8> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  273. std::vector<ui8> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  274. std::list<CMapEvent*> events;
  275. int3 grailPos;
  276. int grailRadious;
  277. std::vector<CGObjectInstance*> objects;
  278. std::vector<CGHeroInstance*> heroes;
  279. std::vector<CGTownInstance*> towns;
  280. void initFromBytes(unsigned char * bufor); //creates map from decompressed .h3m data
  281. void readEvents( unsigned char * bufor, int &i);
  282. void readObjects( unsigned char * bufor, int &i);
  283. void loadQuest( CQuest * guard, unsigned char * bufor, int & i);
  284. void readDefInfo( unsigned char * bufor, int &i);
  285. void readTerrain( unsigned char * bufor, int &i);
  286. void readPredefinedHeroes( unsigned char * bufor, int &i);
  287. void readHeader( unsigned char * bufor, int &i);
  288. void readRumors( unsigned char * bufor, int &i);
  289. void loadHero( CGObjectInstance * &nobj, unsigned char * bufor, int &i);
  290. void loadTown( CGObjectInstance * &nobj, unsigned char * bufor, int &i);
  291. int loadSeerHut( unsigned char * bufor, int i, CGObjectInstance *& nobj);
  292. void addBlockVisTiles(CGObjectInstance * obj);
  293. void removeBlockVisTiles(CGObjectInstance * obj, bool total=false);
  294. Mapa(std::string filename); //creates map structure from .h3m file
  295. Mapa();
  296. ~Mapa();
  297. TerrainTile &getTile(int3 tile);
  298. CGHeroInstance * getHero(int ID, int mode=0);
  299. bool isInTheMap(int3 pos);
  300. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  301. {
  302. h & static_cast<CMapHeader&>(*this);
  303. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & allowedHeroes & events & grailPos;
  304. //TODO: viccondetails
  305. if(h.saving)
  306. {
  307. //saving terrain
  308. for (int i = 0; i < width ; i++)
  309. for (int j = 0; j < height ; j++)
  310. for (int k = 0; k <= twoLevel ; k++)
  311. h & terrain[i][j][k];
  312. }
  313. else
  314. {
  315. //loading terrain
  316. terrain = new TerrainTile**[width]; // allocate memory
  317. for (int ii=0;ii<width;ii++)
  318. {
  319. terrain[ii] = new TerrainTile*[height]; // allocate memory
  320. for(int jj=0;jj<height;jj++)
  321. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  322. }
  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. //definfos
  329. std::vector<CGDefInfo*> defs;
  330. if(h.saving) //create vector with all defs used on map
  331. {
  332. for(int i=0; i<objects.size(); i++)
  333. if(objects[i])
  334. objects[i]->defInfo->serial = -1; //set serial to serial -1 - indicates that def is not present in defs vector
  335. for(int i=0; i<objects.size(); i++)
  336. {
  337. if(!objects[i]) continue;
  338. CGDefInfo *cur = objects[i]->defInfo;
  339. if(cur->serial < 0)
  340. {
  341. cur->serial = defs.size();
  342. defs.push_back(cur);
  343. }
  344. }
  345. }
  346. h & ((h.saving) ? defs : defy);
  347. //objects
  348. if(h.saving)
  349. {
  350. ui32 hlp = objects.size();
  351. h & hlp;
  352. }
  353. else
  354. {
  355. ui32 hlp;
  356. h & hlp;
  357. objects.resize(hlp);
  358. }
  359. h & CGTeleport::objs;
  360. for(int i=0; i<objects.size(); i++)
  361. {
  362. CGObjectInstance *&obj = objects[i];
  363. h & obj;
  364. if (obj)
  365. {
  366. si32 shlp;
  367. //definfo
  368. h & (h.saving ? (shlp=obj->defInfo->serial) : shlp); //read / write pos of definfo in defs vector
  369. if(!h.saving)
  370. obj->defInfo = defy[shlp];
  371. }
  372. }
  373. if(!h.saving)
  374. {
  375. for(int i=0; i<objects.size(); i++)
  376. {
  377. if(!objects[i]) continue;
  378. if(objects[i]->ID == HEROI_TYPE)
  379. heroes.push_back(static_cast<CGHeroInstance*>(objects[i]));
  380. else if(objects[i]->ID == TOWNI_TYPE)
  381. towns.push_back(static_cast<CGTownInstance*>(objects[i]));
  382. addBlockVisTiles(objects[i]); //recreate blockvis map
  383. }
  384. for(int i=0; i<heroes.size(); i++) //if hero is visiting/garrisoned in town set appropriate pointers
  385. {
  386. int3 vistile = heroes[i]->pos; vistile.x++;
  387. for(int j=0; j<towns.size(); j++)
  388. {
  389. if(vistile == towns[j]->pos) //hero stands on the town entrance
  390. {
  391. if(heroes[i]->inTownGarrison)
  392. towns[j]->garrisonHero = heroes[i];
  393. else
  394. towns[j]->visitingHero = heroes[i];
  395. heroes[i]->visitedTown = towns[j];
  396. }
  397. }
  398. }
  399. }
  400. }
  401. };
  402. #endif // __MAP_H__