map.h 17 KB

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