map.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 "global.h"
  11. #ifndef _MSC_VER
  12. #include "hch/CObjectHandler.h"
  13. #include "hch/CDefObjInfoHandler.h"
  14. #endif
  15. class CGDefInfo;
  16. class CGObjectInstance;
  17. class CGHeroInstance;
  18. class CQuest;
  19. class CGTownInstance;
  20. enum ESortBy{_name, _playerAm, _size, _format, _viccon, _loscon};
  21. enum EDefType {TOWN_DEF, HERO_DEF, CREATURES_DEF, SEERHUT_DEF, RESOURCE_DEF, TERRAINOBJ_DEF,
  22. EVENTOBJ_DEF, SIGN_DEF, GARRISON_DEF, ARTIFACT_DEF, WITCHHUT_DEF, SCHOLAR_DEF, PLAYERONLY_DEF,
  23. SHRINE_DEF, SPELLSCROLL_DEF, PANDORA_DEF, GRAIL_DEF, CREGEN_DEF, CREGEN2_DEF, CREGEN3_DEF,
  24. BORDERGUARD_DEF, HEROPLACEHOLDER_DEF};
  25. class DLL_EXPORT CSpecObjInfo
  26. {
  27. public:
  28. virtual ~CSpecObjInfo(){};
  29. };
  30. class DLL_EXPORT CCreGenObjInfo : public CSpecObjInfo
  31. {
  32. public:
  33. unsigned char player; //owner
  34. bool asCastle;
  35. int identifier;
  36. unsigned char castles[2]; //allowed castles
  37. };
  38. class DLL_EXPORT CCreGen2ObjInfo : public CSpecObjInfo
  39. {
  40. public:
  41. unsigned char player; //owner
  42. bool asCastle;
  43. int identifier;
  44. unsigned char castles[2]; //allowed castles
  45. unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  46. };
  47. class DLL_EXPORT CCreGen3ObjInfo : public CSpecObjInfo
  48. {
  49. public:
  50. unsigned char player; //owner
  51. unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  52. };
  53. struct DLL_EXPORT Sresource
  54. {
  55. std::string resName; //name of this resource
  56. int amount; //it can be greater and lesser than 0
  57. };
  58. struct DLL_EXPORT TimeEvent
  59. {
  60. std::string eventName;
  61. std::string message;
  62. std::vector<Sresource> decIncRes; //decreases / increases of resources
  63. unsigned int whichPlayers; //which players are affected by this event (+1 - first, +2 - second, +4 - third, +8 - fourth etc.)
  64. bool areHumansAffected;
  65. bool areCompsAffected;
  66. int firstAfterNDays; //how many days after appears this event
  67. int nextAfterNDays; //how many days after the epperance before appaers this event
  68. };
  69. struct DLL_EXPORT TerrainTile
  70. {
  71. EterrainType tertype; // type of terrain
  72. unsigned char terview; // look of terrain
  73. Eriver nuine; // type of Eriver (0 if there is no Eriver)
  74. unsigned char rivDir; // direction of Eriver
  75. Eroad malle; // type of Eroad (0 if there is no Eriver)
  76. unsigned char roadDir; // direction of Eroad
  77. unsigned char siodmyTajemniczyBajt; //bitfield, info whether this tile is coastal and how to rotate tile graphics
  78. bool visitable; //false = not visitable; true = visitable
  79. bool blocked; //false = free; true = blocked;
  80. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  81. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  82. template <typename Handler> void serialize(Handler &h, const int version)
  83. {
  84. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  85. if(!h.saving)
  86. {
  87. visitable = blocked = false;
  88. //these flags (and obj vectors) will be restored in map serialization
  89. }
  90. }
  91. };
  92. struct DLL_EXPORT SheroName //name of starting hero
  93. {
  94. int heroID;
  95. std::string heroName;
  96. template <typename Handler> void serialize(Handler &h, const int version)
  97. {
  98. h & heroID & heroName;
  99. }
  100. };
  101. struct DLL_EXPORT PlayerInfo
  102. {
  103. si32 p7, p8, p9;
  104. ui8 canHumanPlay;
  105. ui8 canComputerPlay;
  106. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  107. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  108. ui8 isFactionRandom;
  109. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  110. std::string mainHeroName;
  111. std::vector<SheroName> heroesNames;
  112. ui8 hasMainTown;
  113. ui8 generateHeroAtMainTown;
  114. int3 posOfMainTown;
  115. ui8 team;
  116. ui8 generateHero;
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  120. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  121. posOfMainTown & team & generateHero;
  122. }
  123. };
  124. struct DLL_EXPORT LossCondition
  125. {
  126. ElossCon typeOfLossCon;
  127. int3 castlePos;
  128. int3 heroPos;
  129. int timeLimit; // in days
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & typeOfLossCon & castlePos & heroPos & timeLimit;
  133. }
  134. };
  135. struct DLL_EXPORT CVictoryCondition
  136. {
  137. EvictoryConditions condition; //ID of condition
  138. ui8 allowNormalVictory, appliesToAI;
  139. 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)
  140. ui32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  141. ui32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3);
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & condition & allowNormalVictory & appliesToAI & pos & ID & count;
  145. }
  146. };
  147. struct DLL_EXPORT Rumor
  148. {
  149. std::string name, text;
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & name & text;
  153. }
  154. };
  155. struct DLL_EXPORT DisposedHero
  156. {
  157. ui32 ID;
  158. ui16 portrait; //0xFF - default
  159. std::string name;
  160. ui8 players; //who can hire this hero (bitfield)
  161. template <typename Handler> void serialize(Handler &h, const int version)
  162. {
  163. h & ID & portrait & name & players;
  164. }
  165. };
  166. class DLL_EXPORT CMapEvent
  167. {
  168. public:
  169. std::string name, message;
  170. si32 wood, mercury, ore, sulfur, crystal, gems, gold; //gained / taken resources
  171. ui8 players; //affected players
  172. ui8 humanAffected;
  173. ui8 computerAffected;
  174. ui32 firstOccurence;
  175. ui32 nextOccurence; //after nextOccurance day event will occure; if it it 0, event occures only one time;
  176. template <typename Handler> void serialize(Handler &h, const int version)
  177. {
  178. h & name & message & wood & mercury & ore & sulfur & crystal & gems & gold
  179. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  180. }
  181. };
  182. class DLL_EXPORT CMapHeader
  183. {
  184. public:
  185. Eformat version; // version of map Eformat
  186. ui8 areAnyPLayers; // if there are any playable players on map
  187. si32 height, width, twoLevel; //sizes
  188. std::string name; //name of map
  189. std::string description; //and description
  190. ui8 difficulty; // 0 easy - 4 impossible
  191. ui8 levelLimit;
  192. LossCondition lossCondition;
  193. CVictoryCondition victoryCondition; //victory conditions
  194. std::vector<PlayerInfo> players; // info about players - size 8
  195. std::vector<ui8> teams; // teams[i] = team of player no i
  196. ui8 howManyTeams;
  197. void initFromMemory(unsigned char *bufor, int &i);
  198. void loadViCLossConditions( unsigned char * bufor, int &i);
  199. void loadPlayerInfo( int &pom, unsigned char * bufor, int &i);
  200. CMapHeader(unsigned char *map); //an argument is a reference to string described a map (unpacked)
  201. CMapHeader();
  202. template <typename Handler> void serialize(Handler &h, const int Version)
  203. {
  204. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers;
  205. h & players & teams & lossCondition & victoryCondition & howManyTeams;
  206. }
  207. };
  208. class DLL_EXPORT CMapInfo : public CMapHeader
  209. {
  210. public:
  211. ui8 seldiff; //selected difficulty (only in saved games)
  212. std::string filename;
  213. std::string date;
  214. int playerAmnt, humenPlayers;
  215. CMapInfo(){};
  216. void countPlayers();
  217. CMapInfo(std::string fname, unsigned char *map);
  218. };
  219. class DLL_EXPORT mapSorter
  220. {
  221. public:
  222. ESortBy sortBy;
  223. bool operator()(const CMapHeader & a, const CMapHeader& b)
  224. {
  225. switch (sortBy)
  226. {
  227. case _format:
  228. return (a.version<b.version);
  229. break;
  230. case _loscon:
  231. return (a.lossCondition.typeOfLossCon<b.lossCondition.typeOfLossCon);
  232. break;
  233. case _playerAm:
  234. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  235. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  236. for (int i=0;i<8;i++)
  237. {
  238. if (a.players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  239. else if (a.players[i].canComputerPlay) {playerAmntA++;}
  240. if (b.players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  241. else if (b.players[i].canComputerPlay) {playerAmntB++;}
  242. }
  243. if (playerAmntB!=playerAmntA)
  244. return (playerAmntA<playerAmntB);
  245. else
  246. return (humenPlayersA<humenPlayersB);
  247. break;
  248. case _size:
  249. return (a.width<b.width);
  250. break;
  251. case _viccon:
  252. return (a.victoryCondition.condition < b.victoryCondition.condition);
  253. break;
  254. case _name:
  255. return (a.name<b.name);
  256. break;
  257. default:
  258. return (a.name<b.name);
  259. break;
  260. }
  261. };
  262. mapSorter(ESortBy es):sortBy(es){};
  263. };
  264. struct DLL_EXPORT Mapa : public CMapHeader
  265. {
  266. ui32 checksum;
  267. TerrainTile*** terrain;
  268. std::vector<Rumor> rumors;
  269. std::vector<DisposedHero> disposedHeroes;
  270. std::vector<CGHeroInstance*> predefinedHeroes;
  271. std::vector<CGDefInfo *> defy; // list of .def files with definitions from .h3m (may be custom)
  272. std::vector<ui8> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  273. std::vector<ui8> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  274. std::vector<ui8> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  275. std::vector<ui8> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  276. std::vector<CMapEvent> events;
  277. int3 grailPos;
  278. int grailRadious;
  279. std::vector<CGObjectInstance*> objects;
  280. std::vector<CGHeroInstance*> heroes;
  281. std::vector<CGTownInstance*> towns;
  282. void initFromBytes(unsigned char * bufor); //creates map from decompressed .h3m data
  283. void readEvents( unsigned char * bufor, int &i);
  284. void readObjects( unsigned char * bufor, int &i);
  285. void loadQuest( CQuest * guard, unsigned char * bufor, int & i);
  286. void readDefInfo( unsigned char * bufor, int &i);
  287. void readTerrain( unsigned char * bufor, int &i);
  288. void readPredefinedHeroes( unsigned char * bufor, int &i);
  289. void readHeader( unsigned char * bufor, int &i);
  290. void readRumors( unsigned char * bufor, int &i);
  291. void loadHero( CGObjectInstance * &nobj, unsigned char * bufor, int &i);
  292. void loadTown( CGObjectInstance * &nobj, unsigned char * bufor, int &i);
  293. int loadSeerHut( unsigned char * bufor, int i, CGObjectInstance *& nobj);
  294. void addBlockVisTiles(CGObjectInstance * obj);
  295. void removeBlockVisTiles(CGObjectInstance * obj);
  296. Mapa(std::string filename); //creates map structure from .h3m file
  297. Mapa();
  298. ~Mapa();
  299. CGHeroInstance * getHero(int ID, int mode=0);
  300. bool isInTheMap(int3 pos);
  301. template <typename TObject, typename Handler> void serializeObj(Handler &h, const int version, TObject ** obj)
  302. {
  303. h & *obj;
  304. }
  305. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  306. {
  307. h & static_cast<CMapHeader&>(*this);
  308. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & allowedHeroes & events & grailPos;
  309. //TODO: viccondetails
  310. if(h.saving)
  311. {
  312. //saving terrain
  313. for (int i = 0; i < width ; i++)
  314. for (int j = 0; j < height ; j++)
  315. for (int k = 0; k <= twoLevel ; k++)
  316. h & terrain[i][j][k];
  317. }
  318. else
  319. {
  320. //loading terrain
  321. terrain = new TerrainTile**[width]; // allocate memory
  322. for (int ii=0;ii<width;ii++)
  323. {
  324. terrain[ii] = new TerrainTile*[height]; // allocate memory
  325. for(int jj=0;jj<height;jj++)
  326. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  327. }
  328. for (int i = 0; i < width ; i++)
  329. for (int j = 0; j < height ; j++)
  330. for (int k = 0; k <= twoLevel ; k++)
  331. h & terrain[i][j][k];
  332. }
  333. //definfos
  334. std::vector<CGDefInfo*> defs;
  335. if(h.saving) //create vector with all defs used on map
  336. {
  337. for(int i=0; i<objects.size(); i++)
  338. if(objects[i])
  339. objects[i]->defInfo->serial = -1; //set serial to serial -1 - indicates that def is not present in defs vector
  340. for(int i=0; i<objects.size(); i++)
  341. {
  342. if(!objects[i]) continue;
  343. CGDefInfo *cur = objects[i]->defInfo;
  344. if(cur->serial < 0)
  345. {
  346. cur->serial = defs.size();
  347. defs.push_back(cur);
  348. }
  349. }
  350. }
  351. h & ((h.saving) ? defs : defy);
  352. //objects
  353. if(h.saving)
  354. {
  355. ui32 hlp = objects.size();
  356. h & hlp;
  357. }
  358. else
  359. {
  360. ui32 hlp;
  361. h & hlp;
  362. objects.resize(hlp);
  363. }
  364. h & CGTeleport::objs;
  365. for(int i=0; i<objects.size(); i++)
  366. {
  367. CGObjectInstance *&obj = objects[i];
  368. ui32 hlp;
  369. si32 shlp;
  370. h & (h.saving ? (hlp=obj->ID) : hlp);
  371. switch(hlp)
  372. {
  373. #define SERIALIZE(TYPE) ( serializeObj<TYPE>( h,version,(TYPE**) (&obj) ) )
  374. case 34: case 70: case 62:
  375. SERIALIZE(CGHeroInstance);
  376. break;
  377. case 98: case 77:
  378. SERIALIZE(CGTownInstance);
  379. break;
  380. case 26: //for event objects
  381. SERIALIZE(CGEvent);
  382. break;
  383. case 4: //arena
  384. case 51: //Mercenary Camp
  385. case 23: //Marletto Tower
  386. case 61: // Star Axis
  387. case 32: // Garden of Revelation
  388. case 100: //Learning Stone
  389. case 102: //Tree of Knowledge
  390. SERIALIZE(CGVisitableOPH);
  391. break;
  392. case 55: //mystical garden
  393. case 112://windmill
  394. case 109://water wheel
  395. SERIALIZE(CGVisitableOPW);
  396. break;
  397. case 43: //teleport
  398. case 44: //teleport
  399. case 45: //teleport
  400. case 103://subterranean gate
  401. SERIALIZE(CGTeleport);
  402. break;
  403. case 12: //campfire
  404. case 101: //treasure chest
  405. SERIALIZE(CGPickable);
  406. break;
  407. case 54: //Monster
  408. case 71: case 72: case 73: case 74: case 75: // Random Monster 1 - 4
  409. case 162: case 163: case 164:
  410. SERIALIZE(CGCreature);
  411. break;
  412. case 59: case 91: //ocean bottle and sign
  413. SERIALIZE(CGSignBottle);
  414. break;
  415. case 83: //seer's hut
  416. SERIALIZE(CGSeerHut);
  417. break;
  418. case 113: //witch hut
  419. SERIALIZE(CGWitchHut);
  420. break;
  421. case 81: //scholar
  422. SERIALIZE(CGScholar);
  423. break;
  424. case 33: case 219: //garrison
  425. SERIALIZE(CGGarrison);
  426. break;
  427. case 5: //artifact
  428. case 65: case 66: case 67: case 68: case 69: //random artifact
  429. case 93: //spell scroll
  430. SERIALIZE(CGArtifact);
  431. break;
  432. case 76: case 79: //random resource; resource
  433. SERIALIZE(CGResource);
  434. break;
  435. case 53:
  436. SERIALIZE(CGMine);
  437. break;
  438. case 88: case 89: case 90: //spell shrine
  439. SERIALIZE(CGShrine);
  440. break;
  441. case 6:
  442. SERIALIZE(CGPandoraBox);
  443. break;
  444. case 217:
  445. case 216:
  446. case 218:
  447. //TODO cregen
  448. SERIALIZE(CGObjectInstance);
  449. break;
  450. case 215:
  451. SERIALIZE(CGQuestGuard);
  452. break;
  453. default:
  454. SERIALIZE(CGObjectInstance);
  455. }
  456. #undef SERIALIZE
  457. //definfo
  458. h & (h.saving ? (shlp=obj->defInfo->serial) : shlp); //read / write pos of definfo in defs vector
  459. if(!h.saving)
  460. obj->defInfo = defy[shlp];
  461. }
  462. if(!h.saving)
  463. {
  464. for(int i=0; i<objects.size(); i++)
  465. {
  466. if(objects[i]->ID == 34)
  467. heroes.push_back(static_cast<CGHeroInstance*>(objects[i]));
  468. else if(objects[i]->ID == 98)
  469. towns.push_back(static_cast<CGTownInstance*>(objects[i]));
  470. addBlockVisTiles(objects[i]); //recreate blockvis map
  471. }
  472. for(int i=0; i<heroes.size(); i++) //if hero is visiting/garrisoned in town set appropriate pointers
  473. {
  474. int3 vistile = heroes[i]->pos; vistile.x++;
  475. for(int j=0; j<towns.size(); j++)
  476. {
  477. if(vistile == towns[j]->pos) //hero stands on the town entrance
  478. {
  479. if(heroes[i]->inTownGarrison)
  480. towns[j]->garrisonHero = heroes[i];
  481. else
  482. towns[j]->visitingHero = heroes[i];
  483. heroes[i]->visitedTown = towns[j];
  484. }
  485. }
  486. }
  487. }
  488. }
  489. };
  490. #endif // __MAP_H__