map.h 13 KB

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