map.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 "CObjectHandler.h"
  14. #include "CDefObjInfoHandler.h"
  15. #endif
  16. #include "ConstTransitivePtr.h"
  17. /*
  18. * map.h, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. class CArtifactInstance;
  27. class CGDefInfo;
  28. class CGObjectInstance;
  29. class CGHeroInstance;
  30. class CGCreature;
  31. class CQuest;
  32. class CGTownInstance;
  33. class IModableArt;
  34. struct DLL_EXPORT TerrainTile
  35. {
  36. enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock};
  37. enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver};
  38. enum Eroad {dirtRoad=1, grazvelRoad, cobblestoneRoad};
  39. EterrainType tertype; // type of terrain
  40. unsigned char terview; // look of terrain
  41. Eriver nuine; // type of Eriver (0 if there is no river)
  42. unsigned char rivDir; // direction of Eriver
  43. Eroad malle; // type of Eroad (0 if there is no river)
  44. unsigned char roadDir; // direction of Eroad
  45. unsigned char siodmyTajemniczyBajt; //first two bits - how to rotate terrain graphic (next two - river graphic, next two - road); 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favourable Winds effect
  46. bool visitable; //false = not visitable; true = visitable
  47. bool blocked; //false = free; true = blocked;
  48. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  49. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  50. template <typename Handler> void serialize(Handler &h, const int version)
  51. {
  52. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  53. if(!h.saving)
  54. {
  55. visitable = blocked = false;
  56. //these flags (and obj vectors) will be restored in map serialization
  57. }
  58. }
  59. bool entrableTerrain(const TerrainTile *from = NULL) const; //checks if terrain is not a rock. If from is water/land, same type is also required.
  60. bool entrableTerrain(bool allowLand, bool allowSea) const; //checks if terrain is not a rock. If from is water/land, same type is also required.
  61. bool isClear(const TerrainTile *from = NULL) const; //checks for blocking objs and terraint type (water / land)
  62. };
  63. struct DLL_EXPORT SheroName //name of starting hero
  64. {
  65. int heroID;
  66. std::string heroName;
  67. template <typename Handler> void serialize(Handler &h, const int version)
  68. {
  69. h & heroID & heroName;
  70. }
  71. };
  72. struct DLL_EXPORT PlayerInfo
  73. {
  74. si32 p7, p8, p9;
  75. ui8 powerPlacehodlers; //q-ty of hero placeholders containing hero type, WARNING: powerPlacehodlers sometimes gives false 0 (eg. even if there is one placeholder), maybe different meaning???
  76. ui8 canHumanPlay;
  77. ui8 canComputerPlay;
  78. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  79. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  80. ui8 isFactionRandom;
  81. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  82. std::string mainHeroName;
  83. std::vector<SheroName> heroesNames;
  84. ui8 hasMainTown;
  85. ui8 generateHeroAtMainTown;
  86. int3 posOfMainTown;
  87. ui8 team;
  88. ui8 generateHero;
  89. PlayerInfo(): p7(0), p8(0), p9(0), canHumanPlay(0), canComputerPlay(0),
  90. AITactic(0), allowedFactions(0), isFactionRandom(0),
  91. mainHeroPortrait(0), hasMainTown(0), generateHeroAtMainTown(0),
  92. team(255), generateHero(0) {};
  93. si8 defaultCastle() const
  94. {
  95. si8 ret = -2;
  96. for (int j = 0; j < F_NUMBER && ret != -1; j++) //we start with none and find matching faction. if more than one, then set to random
  97. {
  98. if((1 << j) & allowedFactions)
  99. {
  100. if (ret >= 0) //we've already assigned a castle and another one is possible -> set random and let player choose
  101. ret = -1; //breaks
  102. if (ret == -2) //first available castle - pick
  103. ret = j;
  104. }
  105. }
  106. return ret;
  107. }
  108. si8 defaultHero(bool isMapRoE = false) const
  109. {
  110. if ( ((generateHeroAtMainTown || isMapRoE) && hasMainTown) //we will generate hero in front of main town
  111. || p8) //random hero
  112. return -1;
  113. else
  114. return -2;
  115. }
  116. template <typename Handler> void serialize(Handler &h, const int version)
  117. {
  118. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  119. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  120. posOfMainTown & team & generateHero;
  121. }
  122. };
  123. struct DLL_EXPORT LossCondition
  124. {
  125. ElossCon typeOfLossCon;
  126. int3 pos;
  127. si32 timeLimit; // in days; -1 if not used
  128. const CGObjectInstance *obj; //set during map parsing: hero/town (depending on typeOfLossCon); NULL if not used
  129. template <typename Handler> void serialize(Handler &h, const int version)
  130. {
  131. h & typeOfLossCon & pos & timeLimit & obj;
  132. }
  133. LossCondition();
  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. si32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  141. si32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3);
  142. const CGObjectInstance *obj; //object of specific monster / city / hero instance (NULL if not used); set during map parsing
  143. CVictoryCondition();
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & condition & allowNormalVictory & appliesToAI & pos & ID & count & obj;
  147. }
  148. };
  149. struct DLL_EXPORT Rumor
  150. {
  151. std::string name, text;
  152. template <typename Handler> void serialize(Handler &h, const int version)
  153. {
  154. h & name & text;
  155. }
  156. };
  157. struct DLL_EXPORT DisposedHero
  158. {
  159. ui32 ID;
  160. ui16 portrait; //0xFF - default
  161. std::string name;
  162. ui8 players; //who can hire this hero (bitfield)
  163. template <typename Handler> void serialize(Handler &h, const int version)
  164. {
  165. h & ID & portrait & name & players;
  166. }
  167. };
  168. class DLL_EXPORT CMapEvent
  169. {
  170. public:
  171. std::string name, message;
  172. std::vector<si32> resources; //gained / taken resources
  173. ui8 players; //affected players
  174. ui8 humanAffected;
  175. ui8 computerAffected;
  176. ui32 firstOccurence;
  177. ui32 nextOccurence; //after nextOccurance day event will occur; if it it 0, event occures only one time;
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & name & message & resources
  181. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  182. }
  183. bool operator<(const CMapEvent &b) const
  184. {
  185. return firstOccurence < b.firstOccurence;
  186. }
  187. };
  188. class DLL_EXPORT CCastleEvent: public CMapEvent
  189. {
  190. public:
  191. std::set<si32> buildings;//build specific buildings
  192. std::vector<si32> creatures;//additional creatures in i-th level dwelling
  193. CGTownInstance * town;//owner of this event
  194. template <typename Handler> void serialize(Handler &h, const int version)
  195. {
  196. h & static_cast<CMapEvent&>(*this);
  197. h & buildings & creatures;
  198. }
  199. };
  200. class DLL_EXPORT CMapHeader
  201. {
  202. public:
  203. enum Eformat {invalid, WoG=0x33, AB=0x15, RoE=0x0e, SoD=0x1c};
  204. Eformat version; // version of map Eformat
  205. ui8 areAnyPLayers; // if there are any playable players on map
  206. si32 height, width, twoLevel; //sizes
  207. std::string name; //name of map
  208. std::string description; //and description
  209. ui8 difficulty; // 0 easy - 4 impossible
  210. ui8 levelLimit;
  211. LossCondition lossCondition;
  212. CVictoryCondition victoryCondition; //victory conditions
  213. std::vector<PlayerInfo> players; // info about players - size 8
  214. ui8 howManyTeams;
  215. std::vector<ui8> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  216. std::vector<ui16> placeholdedHeroes; //ID of types of heroes in placeholders
  217. void initFromMemory(const unsigned char *bufor, int &i);
  218. void loadViCLossConditions( const unsigned char * bufor, int &i);
  219. void loadPlayerInfo( int &pom, const unsigned char * bufor, int &i);
  220. CMapHeader(const unsigned char *map); //an argument is a reference to string described a map (unpacked)
  221. CMapHeader();
  222. virtual ~CMapHeader();
  223. template <typename Handler> void serialize(Handler &h, const int Version)
  224. {
  225. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers;
  226. h & players & lossCondition & victoryCondition & howManyTeams;
  227. }
  228. };
  229. struct DLL_EXPORT Mapa : public CMapHeader
  230. {
  231. ui32 checksum;
  232. TerrainTile*** terrain;
  233. std::vector<Rumor> rumors;
  234. std::vector<DisposedHero> disposedHeroes;
  235. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  236. std::vector<ConstTransitivePtr<CGDefInfo> > defy; // list of .def files with definitions from .h3m (may be custom)
  237. std::vector<ui8> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  238. std::vector<ui8> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  239. std::vector<ui8> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  240. std::list<ConstTransitivePtr<CMapEvent> > events;
  241. int3 grailPos;
  242. int grailRadious;
  243. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  244. std::vector< ConstTransitivePtr<CGHeroInstance> > heroes;
  245. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  246. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances; //stores all artifacts
  247. bmap<ui16, ConstTransitivePtr<CGCreature> > monsters;
  248. bmap<ui16, ConstTransitivePtr<CGHeroInstance> > heroesToBeat;
  249. void initFromBytes( const unsigned char * bufor); //creates map from decompressed .h3m data
  250. void readEvents( const unsigned char * bufor, int &i);
  251. void readObjects( const unsigned char * bufor, int &i);
  252. void loadQuest( CQuest * guard, const unsigned char * bufor, int & i);
  253. void readDefInfo( const unsigned char * bufor, int &i);
  254. void readTerrain( const unsigned char * bufor, int &i);
  255. void readPredefinedHeroes( const unsigned char * bufor, int &i);
  256. void readHeader( const unsigned char * bufor, int &i);
  257. void readRumors( const unsigned char * bufor, int &i);
  258. CGObjectInstance *loadHero(const unsigned char * bufor, int &i);
  259. void loadArtifactsOfHero(const unsigned char * bufor, int & i, CGHeroInstance * nhi);
  260. bool loadArtifactToSlot(CGHeroInstance *h, int slot, const unsigned char * bufor, int &i);
  261. void loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid);
  262. int loadSeerHut( const unsigned char * bufor, int i, CGObjectInstance *& nobj);
  263. CArtifactInstance *createArt(int aid);
  264. void checkForObjectives();
  265. void addNewArtifactInstance(CArtifactInstance *art);
  266. void addBlockVisTiles(CGObjectInstance * obj);
  267. void removeBlockVisTiles(CGObjectInstance * obj, bool total=false);
  268. Mapa(std::string filename); //creates map structure from .h3m file
  269. Mapa();
  270. ~Mapa();
  271. TerrainTile &getTile(const int3 & tile);
  272. const TerrainTile &getTile(const int3 & tile) const;
  273. CGHeroInstance * getHero(int ID, int mode=0);
  274. bool isInTheMap(const int3 &pos) const;
  275. bool isWaterTile(const int3 &pos) const; //out-of-pos safe
  276. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  277. {
  278. h & static_cast<CMapHeader&>(*this);
  279. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & allowedHeroes & events & grailPos;
  280. h & monsters;
  281. h & heroesToBeat;
  282. h & artInstances; //hopefully serialization is now automagical?
  283. //TODO: viccondetails
  284. if(h.saving)
  285. {
  286. //saving terrain
  287. for (int i = 0; i < width ; i++)
  288. for (int j = 0; j < height ; j++)
  289. for (int k = 0; k <= twoLevel ; k++)
  290. h & terrain[i][j][k];
  291. }
  292. else
  293. {
  294. //loading terrain
  295. terrain = new TerrainTile**[width]; // allocate memory
  296. for (int ii=0;ii<width;ii++)
  297. {
  298. terrain[ii] = new TerrainTile*[height]; // allocate memory
  299. for(int jj=0;jj<height;jj++)
  300. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  301. }
  302. for (int i = 0; i < width ; i++)
  303. for (int j = 0; j < height ; j++)
  304. for (int k = 0; k <= twoLevel ; k++)
  305. h & terrain[i][j][k];
  306. }
  307. h & defy & objects;
  308. // //definfos
  309. // std::vector<CGDefInfo*> defs;
  310. //
  311. // if(h.saving) //create vector with all defs used on map
  312. // {
  313. // for(unsigned int i=0; i<objects.size(); i++)
  314. // if(objects[i])
  315. // objects[i]->defInfo->serial = -1; //set serial to serial -1 - indicates that def is not present in defs vector
  316. //
  317. // for(unsigned int i=0; i<objects.size(); i++)
  318. // {
  319. // if(!objects[i]) continue;
  320. // CGDefInfo *cur = objects[i]->defInfo;
  321. // if(cur->serial < 0)
  322. // {
  323. // cur->serial = defs.size();
  324. // defs.push_back(cur);
  325. // }
  326. // }
  327. // }
  328. //
  329. // h & ((h.saving) ? defs : defy);
  330. // //objects
  331. // if(h.saving)
  332. // {
  333. // ui32 hlp = objects.size();
  334. // h & hlp;
  335. // }
  336. // else
  337. // {
  338. // ui32 hlp;
  339. // h & hlp;
  340. // objects.resize(hlp);
  341. // }
  342. //static members
  343. h & CGTeleport::objs;
  344. h & CGTeleport::gates;
  345. h & CGKeys::playerKeyMap;
  346. h & CGMagi::eyelist;
  347. h & CGObelisk::obeliskCount & CGObelisk::visited;
  348. h & CGTownInstance::merchantArtifacts;
  349. // for(unsigned int i=0; i<objects.size(); i++)
  350. // {
  351. // CGObjectInstance *&obj = objects[i];
  352. // h & obj;
  353. //
  354. // if (obj)
  355. // {
  356. // si32 shlp;
  357. // //definfo
  358. // h & (h.saving ? (shlp=obj->defInfo->serial) : shlp); //read / write pos of definfo in defs vector
  359. // if(!h.saving)
  360. // obj->defInfo = defy[shlp];
  361. // }
  362. // }
  363. if(!h.saving)
  364. {
  365. for(unsigned int i=0; i<objects.size(); i++)
  366. {
  367. if(!objects[i]) continue;
  368. if(objects[i]->ID == HEROI_TYPE)
  369. heroes.push_back(static_cast<CGHeroInstance*>(+objects[i]));
  370. else if(objects[i]->ID == TOWNI_TYPE)
  371. towns.push_back(static_cast<CGTownInstance*>(+objects[i]));
  372. addBlockVisTiles(objects[i]); //recreate blockvis map
  373. }
  374. for(unsigned int i=0; i<heroes.size(); i++) //if hero is visiting/garrisoned in town set appropriate pointers
  375. {
  376. int3 vistile = heroes[i]->pos; vistile.x++;
  377. for(unsigned int j=0; j<towns.size(); j++)
  378. {
  379. if(vistile == towns[j]->pos) //hero stands on the town entrance
  380. {
  381. if(heroes[i]->inTownGarrison)
  382. {
  383. towns[j]->garrisonHero = heroes[i];
  384. removeBlockVisTiles(heroes[i]);
  385. }
  386. else
  387. {
  388. towns[j]->visitingHero = heroes[i];
  389. }
  390. heroes[i]->visitedTown = towns[j];
  391. break;
  392. }
  393. }
  394. vistile.x -= 2; //manifest pos
  395. const TerrainTile &t = getTile(vistile);
  396. if(t.tertype != TerrainTile::water) continue;
  397. //hero stands on the water - he must be in the boat
  398. for(unsigned int j = 0; j < t.visitableObjects.size(); j++)
  399. {
  400. if(t.visitableObjects[j]->ID == 8)
  401. {
  402. CGBoat *b = static_cast<CGBoat *>(t.visitableObjects[j]);
  403. heroes[i]->boat = b;
  404. b->hero = heroes[i];
  405. removeBlockVisTiles(b);
  406. break;
  407. }
  408. }
  409. } //heroes loop
  410. } //!saving
  411. }
  412. };
  413. #endif // __MAP_H__