2
0

map.h 16 KB

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