map.h 15 KB

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