map.h 16 KB

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