map.h 16 KB

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