map.h 17 KB

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