map.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 which describes a single terrain tile
  35. struct DLL_EXPORT TerrainTile
  36. {
  37. enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock};
  38. enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver};
  39. enum Eroad {dirtRoad=1, grazvelRoad, cobblestoneRoad};
  40. EterrainType tertype; // type of terrain
  41. unsigned char terview; // look of terrain
  42. Eriver nuine; // type of Eriver (0 if there is no river)
  43. unsigned char rivDir; // direction of Eriver
  44. Eroad malle; // type of Eroad (0 if there is no river)
  45. unsigned char roadDir; // direction of Eroad
  46. 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
  47. bool visitable; //false = not visitable; true = visitable
  48. bool blocked; //false = free; true = blocked;
  49. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  50. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  51. template <typename Handler> void serialize(Handler &h, const int version)
  52. {
  53. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  54. if(!h.saving)
  55. {
  56. visitable = blocked = false;
  57. //these flags (and obj vectors) will be restored in map serialization
  58. }
  59. }
  60. bool entrableTerrain(const TerrainTile *from = NULL) const; //checks if terrain is not a rock. If from is water/land, same type is also required.
  61. bool entrableTerrain(bool allowLand, bool allowSea) const; //checks if terrain is not a rock. If from is water/land, same type is also required.
  62. bool isClear(const TerrainTile *from = NULL) const; //checks for blocking objs and terraint type (water / land)
  63. };
  64. /// name of starting hero
  65. struct DLL_EXPORT SheroName
  66. {
  67. int heroID;
  68. std::string heroName;
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & heroID & heroName;
  72. }
  73. };
  74. /// Player information regarding map. Which factions are allowed, AI tactic setting, main hero name,
  75. /// position of main town,...
  76. struct DLL_EXPORT PlayerInfo
  77. {
  78. si32 p7, p8, p9;
  79. 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???
  80. ui8 canHumanPlay;
  81. ui8 canComputerPlay;
  82. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  83. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  84. ui8 isFactionRandom;
  85. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  86. std::string mainHeroName;
  87. std::vector<SheroName> heroesNames;
  88. ui8 hasMainTown;
  89. ui8 generateHeroAtMainTown;
  90. int3 posOfMainTown;
  91. ui8 team;
  92. ui8 generateHero;
  93. PlayerInfo(): p7(0), p8(0), p9(0), canHumanPlay(0), canComputerPlay(0),
  94. AITactic(0), allowedFactions(0), isFactionRandom(0),
  95. mainHeroPortrait(0), hasMainTown(0), generateHeroAtMainTown(0),
  96. team(255), generateHero(0) {};
  97. si8 defaultCastle() const
  98. {
  99. si8 ret = -2;
  100. 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
  101. {
  102. if((1 << j) & allowedFactions)
  103. {
  104. if (ret >= 0) //we've already assigned a castle and another one is possible -> set random and let player choose
  105. ret = -1; //breaks
  106. if (ret == -2) //first available castle - pick
  107. ret = j;
  108. }
  109. }
  110. return ret;
  111. }
  112. si8 defaultHero() const
  113. {
  114. if ((generateHeroAtMainTown && hasMainTown) //we will generate hero in front of main town
  115. || p8) //random hero
  116. return -1;
  117. else
  118. return -2;
  119. }
  120. template <typename Handler> void serialize(Handler &h, const int version)
  121. {
  122. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  123. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  124. posOfMainTown & team & generateHero;
  125. }
  126. };
  127. /// Small struct which holds information about the loss condition
  128. struct DLL_EXPORT LossCondition
  129. {
  130. ElossCon typeOfLossCon;
  131. int3 pos;
  132. si32 timeLimit; // in days; -1 if not used
  133. const CGObjectInstance *obj; //set during map parsing: hero/town (depending on typeOfLossCon); NULL if not used
  134. template <typename Handler> void serialize(Handler &h, const int version)
  135. {
  136. h & typeOfLossCon & pos & timeLimit & obj;
  137. }
  138. LossCondition();
  139. };
  140. /// Small struct which holds information about the victory condition
  141. struct DLL_EXPORT CVictoryCondition
  142. {
  143. EvictoryConditions condition; //ID of condition
  144. ui8 allowNormalVictory, appliesToAI;
  145. 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)
  146. si32 ID; //artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  147. si32 count; //needed count for creatures (1) / resource (2); upgraded town hall level (3);
  148. const CGObjectInstance *obj; //object of specific monster / city / hero instance (NULL if not used); set during map parsing
  149. CVictoryCondition();
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & condition & allowNormalVictory & appliesToAI & pos & ID & count & obj;
  153. }
  154. };
  155. /// Struct which holds a name and the rumor text
  156. struct DLL_EXPORT Rumor
  157. {
  158. std::string name, text;
  159. template <typename Handler> void serialize(Handler &h, const int version)
  160. {
  161. h & name & text;
  162. }
  163. };
  164. /// Struct which describes who can hire this hero
  165. struct DLL_EXPORT DisposedHero
  166. {
  167. ui32 ID;
  168. ui16 portrait; //0xFF - default
  169. std::string name;
  170. ui8 players; //who can hire this hero (bitfield)
  171. template <typename Handler> void serialize(Handler &h, const int version)
  172. {
  173. h & ID & portrait & name & players;
  174. }
  175. };
  176. /// Class which manages map events.
  177. class DLL_EXPORT CMapEvent
  178. {
  179. public:
  180. std::string name, message;
  181. std::vector<si32> resources; //gained / taken resources
  182. ui8 players; //affected players
  183. ui8 humanAffected;
  184. ui8 computerAffected;
  185. ui32 firstOccurence;
  186. ui32 nextOccurence; //after nextOccurance day event will occur; if it it 0, event occures only one time;
  187. template <typename Handler> void serialize(Handler &h, const int version)
  188. {
  189. h & name & message & resources
  190. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  191. }
  192. bool operator<(const CMapEvent &b) const
  193. {
  194. return firstOccurence < b.firstOccurence;
  195. }
  196. bool operator<=(const CMapEvent &b) const
  197. {
  198. return firstOccurence <= b.firstOccurence;
  199. }
  200. };
  201. /// Sub-class derived by CMapEvent; This event can build specific buildings or add
  202. /// additional creatures in a town.
  203. class DLL_EXPORT CCastleEvent: public CMapEvent
  204. {
  205. public:
  206. std::set<si32> buildings;//build specific buildings
  207. std::vector<si32> creatures;//additional creatures in i-th level dwelling
  208. CGTownInstance * town;//owner of this event
  209. template <typename Handler> void serialize(Handler &h, const int version)
  210. {
  211. h & static_cast<CMapEvent&>(*this);
  212. h & buildings & creatures;
  213. }
  214. };
  215. /// Holds information about loss/victory condition, map format, version, players, height, width,...
  216. class DLL_EXPORT CMapHeader
  217. {
  218. public:
  219. enum Eformat {invalid, WoG=0x33, AB=0x15, RoE=0x0e, SoD=0x1c};
  220. Eformat version; // version of map Eformat
  221. ui8 areAnyPLayers; // if there are any playable players on map
  222. si32 height, width, twoLevel; //sizes
  223. std::string name; //name of map
  224. std::string description; //and description
  225. ui8 difficulty; // 0 easy - 4 impossible
  226. ui8 levelLimit;
  227. LossCondition lossCondition;
  228. CVictoryCondition victoryCondition; //victory conditions
  229. std::vector<PlayerInfo> players; // info about players - size 8
  230. ui8 howManyTeams;
  231. std::vector<ui8> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  232. std::vector<ui16> placeholdedHeroes; //ID of types of heroes in placeholders
  233. void initFromMemory(const unsigned char *bufor, int &i);
  234. void loadViCLossConditions( const unsigned char * bufor, int &i);
  235. void loadPlayerInfo( int &pom, const unsigned char * bufor, int &i);
  236. CMapHeader(const unsigned char *map); //an argument is a reference to string described a map (unpacked)
  237. CMapHeader();
  238. virtual ~CMapHeader();
  239. template <typename Handler> void serialize(Handler &h, const int Version)
  240. {
  241. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPLayers;
  242. h & players & lossCondition & victoryCondition & howManyTeams;
  243. }
  244. };
  245. /// Extends the base class and adds further map information like rumors, disposed heroes,
  246. /// allowed spells, artifacts, abilities and such things.
  247. struct DLL_EXPORT Mapa : public CMapHeader
  248. {
  249. ui32 checksum;
  250. TerrainTile*** terrain;
  251. std::vector<Rumor> rumors;
  252. std::vector<DisposedHero> disposedHeroes;
  253. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  254. std::vector<ConstTransitivePtr<CGDefInfo> > defy; // list of .def files with definitions from .h3m (may be custom)
  255. std::vector<ui8> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  256. std::vector<ui8> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  257. std::vector<ui8> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  258. std::list<ConstTransitivePtr<CMapEvent> > events;
  259. int3 grailPos;
  260. int grailRadious;
  261. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  262. std::vector< ConstTransitivePtr<CGHeroInstance> > heroes;
  263. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  264. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances; //stores all artifacts
  265. //bmap<ui16, ConstTransitivePtr<CGCreature> > monsters;
  266. //bmap<ui16, ConstTransitivePtr<CGHeroInstance> > heroesToBeat;
  267. bmap<si32, si32> questIdentifierToId;
  268. void initFromBytes( const unsigned char * bufor); //creates map from decompressed .h3m data
  269. void readEvents( const unsigned char * bufor, int &i);
  270. void readObjects( const unsigned char * bufor, int &i);
  271. void loadQuest( CQuest * guard, const unsigned char * bufor, int & i);
  272. void readDefInfo( const unsigned char * bufor, int &i);
  273. void readTerrain( const unsigned char * bufor, int &i);
  274. void readPredefinedHeroes( const unsigned char * bufor, int &i);
  275. void readHeader( const unsigned char * bufor, int &i);
  276. void readRumors( const unsigned char * bufor, int &i);
  277. CGObjectInstance *loadHero(const unsigned char * bufor, int &i, int idToBeGiven);
  278. void loadArtifactsOfHero(const unsigned char * bufor, int & i, CGHeroInstance * nhi);
  279. bool loadArtifactToSlot(CGHeroInstance *h, int slot, const unsigned char * bufor, int &i);
  280. void loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid);
  281. int loadSeerHut( const unsigned char * bufor, int i, CGObjectInstance *& nobj);
  282. CArtifactInstance *createArt(int aid, int spellID = -1);
  283. void addNewArtifactInstance(CArtifactInstance *art);
  284. void eraseArtifactInstance(CArtifactInstance *art);
  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(unsigned int 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(unsigned int 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(unsigned int 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(unsigned int i=0; i<objects.size(); i++)
  385. {
  386. if(!objects[i]) continue;
  387. if(objects[i]->ID == HEROI_TYPE)
  388. heroes.push_back(static_cast<CGHeroInstance*>(+objects[i]));
  389. else if(objects[i]->ID == TOWNI_TYPE)
  390. towns.push_back(static_cast<CGTownInstance*>(+objects[i]));
  391. addBlockVisTiles(objects[i]); //recreate blockvis map
  392. }
  393. for(unsigned int 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(unsigned int 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(unsigned int 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. };
  432. #endif // __MAP_H__