map.h 17 KB

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