map.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #ifndef MAPD_H
  2. #define MAPD_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 "global.h"
  11. class CGDefInfo;
  12. class CGObjectInstance;
  13. class CGHeroInstance;
  14. class CGTownInstance;
  15. enum ESortBy{_name, _playerAm, _size, _format, _viccon, _loscon};
  16. enum EDefType {TOWN_DEF, HERO_DEF, CREATURES_DEF, SEERHUT_DEF, RESOURCE_DEF, TERRAINOBJ_DEF,
  17. EVENTOBJ_DEF, SIGN_DEF, GARRISON_DEF, ARTIFACT_DEF, WITCHHUT_DEF, SCHOLAR_DEF, PLAYERONLY_DEF,
  18. SHRINE_DEF, SPELLSCROLL_DEF, PANDORA_DEF, GRAIL_DEF, CREGEN_DEF, CREGEN2_DEF, CREGEN3_DEF,
  19. BORDERGUARD_DEF, HEROPLACEHOLDER_DEF};
  20. class DLL_EXPORT CSpecObjInfo
  21. {
  22. public:
  23. virtual ~CSpecObjInfo(){};
  24. };
  25. class DLL_EXPORT CCreGenObjInfo : public CSpecObjInfo
  26. {
  27. public:
  28. unsigned char player; //owner
  29. bool asCastle;
  30. int identifier;
  31. unsigned char castles[2]; //allowed castles
  32. };
  33. class DLL_EXPORT CCreGen2ObjInfo : public CSpecObjInfo
  34. {
  35. public:
  36. unsigned char player; //owner
  37. bool asCastle;
  38. int identifier;
  39. unsigned char castles[2]; //allowed castles
  40. unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  41. };
  42. class DLL_EXPORT CCreGen3ObjInfo : public CSpecObjInfo
  43. {
  44. public:
  45. unsigned char player; //owner
  46. unsigned char minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  47. };
  48. struct DLL_EXPORT Sresource
  49. {
  50. std::string resName; //name of this resource
  51. int amount; //it can be greater and lesser than 0
  52. };
  53. struct DLL_EXPORT TimeEvent
  54. {
  55. std::string eventName;
  56. std::string message;
  57. std::vector<Sresource> decIncRes; //decreases / increases of resources
  58. unsigned int whichPlayers; //which players are affected by this event (+1 - first, +2 - second, +4 - third, +8 - fourth etc.)
  59. bool areHumansAffected;
  60. bool areCompsAffected;
  61. int firstAfterNDays; //how many days after appears this event
  62. int nextAfterNDays; //how many days after the epperance before appaers this event
  63. };
  64. struct DLL_EXPORT TerrainTile
  65. {
  66. EterrainType tertype; // type of terrain
  67. unsigned char terview; // look of terrain
  68. Eriver nuine; // type of Eriver (0 if there is no Eriver)
  69. unsigned char rivDir; // direction of Eriver
  70. Eroad malle; // type of Eroad (0 if there is no Eriver)
  71. unsigned char roadDir; // direction of Eroad
  72. unsigned char siodmyTajemniczyBajt; //bitfield, info whether this tile is coastal and how to rotate tile graphics
  73. bool visitable; //false = not visitable; true = visitable
  74. bool blocked; //false = free; true = blocked;
  75. std::vector <CGObjectInstance*> visitableObjects; //pointers to objects hero can visit while being on this tile
  76. std::vector <CGObjectInstance*> blockingObjects; //pointers to objects that are blocking this tile
  77. template <typename Handler> void serialize(Handler &h, const int version)
  78. {
  79. h & tertype & terview & nuine & rivDir & malle &roadDir & siodmyTajemniczyBajt;
  80. }
  81. };
  82. struct DLL_EXPORT SheroName //name of starting hero
  83. {
  84. int heroID;
  85. std::string heroName;
  86. template <typename Handler> void serialize(Handler &h, const int version)
  87. {
  88. h & heroID & heroName;
  89. }
  90. };
  91. struct DLL_EXPORT PlayerInfo
  92. {
  93. si32 p7, p8, p9;
  94. ui8 canHumanPlay;
  95. ui8 canComputerPlay;
  96. ui32 AITactic; //(00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  97. ui32 allowedFactions; //(01 - castle; 02 - rampart; 04 - tower; 08 - inferno; 16 - necropolis; 32 - dungeon; 64 - stronghold; 128 - fortress; 256 - conflux);
  98. ui8 isFactionRandom;
  99. ui32 mainHeroPortrait; //it's ID of hero with choosen portrait; 255 if standard
  100. std::string mainHeroName;
  101. std::vector<SheroName> heroesNames;
  102. ui8 hasMainTown;
  103. ui8 generateHeroAtMainTown;
  104. int3 posOfMainTown;
  105. ui8 team;
  106. ui8 generateHero;
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & p7 & p8 & p9 & canHumanPlay & canComputerPlay & AITactic & allowedFactions & isFactionRandom &
  110. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  111. posOfMainTown & team & generateHero;
  112. }
  113. };
  114. struct DLL_EXPORT LossCondition
  115. {
  116. ElossCon typeOfLossCon;
  117. int3 castlePos;
  118. int3 heroPos;
  119. int timeLimit; // in days
  120. template <typename Handler> void serialize(Handler &h, const int version)
  121. {
  122. h & typeOfLossCon & castlePos & heroPos & timeLimit;
  123. }
  124. };
  125. struct DLL_EXPORT CspecificVictoryConidtions
  126. {
  127. bool allowNormalVictory;
  128. bool appliesToAI;
  129. };
  130. struct DLL_EXPORT VicCon0 : public CspecificVictoryConidtions //acquire artifact
  131. {
  132. int ArtifactID;
  133. };
  134. struct DLL_EXPORT VicCon1 : public CspecificVictoryConidtions //accumulate creatures
  135. {
  136. int monsterID;
  137. int neededQuantity;
  138. };
  139. struct DLL_EXPORT VicCon2 : public CspecificVictoryConidtions // accumulate resources
  140. {
  141. int resourceID;
  142. int neededQuantity;
  143. };
  144. struct DLL_EXPORT VicCon3 : public CspecificVictoryConidtions // upgrade specific town
  145. {
  146. int3 posOfCity;
  147. int councilNeededLevel; //0 - town; 1 - city; 2 - capitol
  148. int fortNeededLevel;// 0 - fort; 1 - citadel; 2 - castle
  149. };
  150. struct DLL_EXPORT VicCon4 : public CspecificVictoryConidtions // build grail structure
  151. {
  152. bool anyLocation;
  153. int3 whereBuildGrail;
  154. };
  155. struct DLL_EXPORT VicCon5 : public CspecificVictoryConidtions // defeat a specific hero
  156. {
  157. int3 locationOfHero;
  158. };
  159. struct DLL_EXPORT VicCon6 : public CspecificVictoryConidtions // capture a specific town
  160. {
  161. int3 locationOfTown;
  162. };
  163. struct DLL_EXPORT VicCon7 : public CspecificVictoryConidtions // defeat a specific monster
  164. {
  165. int3 locationOfMonster;
  166. };
  167. struct DLL_EXPORT VicCona : public CspecificVictoryConidtions //transport specific artifact
  168. {
  169. int artifactID;
  170. int3 destinationPlace;
  171. };
  172. struct DLL_EXPORT Rumor
  173. {
  174. std::string name, text;
  175. template <typename Handler> void serialize(Handler &h, const int version)
  176. {
  177. h & name & text;
  178. }
  179. };
  180. struct DLL_EXPORT DisposedHero
  181. {
  182. ui32 ID;
  183. ui16 portrait; //0xFF - default
  184. std::string name;
  185. ui8 players; //who can hire this hero (bitfield)
  186. template <typename Handler> void serialize(Handler &h, const int version)
  187. {
  188. h & ID & portrait & name & players;
  189. }
  190. };
  191. class DLL_EXPORT CMapEvent
  192. {
  193. public:
  194. std::string name, message;
  195. si32 wood, mercury, ore, sulfur, crystal, gems, gold; //gained / taken resources
  196. ui8 players; //affected players
  197. ui8 humanAffected;
  198. ui8 computerAffected;
  199. ui32 firstOccurence;
  200. ui32 nextOccurence; //after nextOccurance day event will occure; if it it 0, event occures only one time;
  201. template <typename Handler> void serialize(Handler &h, const int version)
  202. {
  203. h & name & message & wood & mercury & ore & sulfur & crystal & gems & gold
  204. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  205. }
  206. };
  207. class DLL_EXPORT CMapHeader
  208. {
  209. public:
  210. Eformat version; // version of map Eformat
  211. bool areAnyPLayers; // if there are any playable players on map
  212. int height, width;
  213. bool twoLevel; // if map has underground level
  214. std::string name; //name of map
  215. std::string description; //and description
  216. int difficulty; // 0 easy - 4 impossible
  217. int levelLimit;
  218. LossCondition lossCondition;
  219. EvictoryConditions victoryCondition; //victory conditions
  220. CspecificVictoryConidtions * vicConDetails; // used only if vistory conditions aren't standard
  221. PlayerInfo players[8]; // info about players
  222. std::vector<int> teams; // teams[i] = team of player no i
  223. int howManyTeams;
  224. CMapHeader(unsigned char *map); //an argument is a reference to string described a map (unpacked)
  225. };
  226. class DLL_EXPORT CMapInfo : public CMapHeader
  227. {
  228. public:
  229. std::string filename;
  230. int playerAmnt, humenPlayers;
  231. CMapInfo(std::string fname, unsigned char *map):CMapHeader(map),filename(fname)
  232. {
  233. playerAmnt=humenPlayers=0;
  234. for (int i=0;i<PLAYER_LIMIT;i++)
  235. {
  236. if (players[i].canHumanPlay) {playerAmnt++;humenPlayers++;}
  237. else if (players[i].canComputerPlay) {playerAmnt++;}
  238. }
  239. };
  240. };
  241. class DLL_EXPORT mapSorter
  242. {
  243. public:
  244. ESortBy sortBy;
  245. bool operator()(const CMapHeader & a, const CMapHeader& b)
  246. {
  247. switch (sortBy)
  248. {
  249. case _format:
  250. return (a.version<b.version);
  251. break;
  252. case _loscon:
  253. return (a.lossCondition.typeOfLossCon<b.lossCondition.typeOfLossCon);
  254. break;
  255. case _playerAm:
  256. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  257. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  258. for (int i=0;i<8;i++)
  259. {
  260. if (a.players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  261. else if (a.players[i].canComputerPlay) {playerAmntA++;}
  262. if (b.players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  263. else if (b.players[i].canComputerPlay) {playerAmntB++;}
  264. }
  265. if (playerAmntB!=playerAmntA)
  266. return (playerAmntA<playerAmntB);
  267. else
  268. return (humenPlayersA<humenPlayersB);
  269. break;
  270. case _size:
  271. return (a.width<b.width);
  272. break;
  273. case _viccon:
  274. return (a.victoryCondition<b.victoryCondition);
  275. break;
  276. case _name:
  277. return (a.name<b.name);
  278. break;
  279. default:
  280. return (a.name<b.name);
  281. break;
  282. }
  283. };
  284. mapSorter(ESortBy es):sortBy(es){};
  285. };
  286. struct DLL_EXPORT Mapa
  287. {
  288. Eformat version; // version of map Eformat
  289. ui32 checksum;
  290. int twoLevel; // if map has underground level
  291. int difficulty; // 0 easy - 4 impossible
  292. int levelLimit;
  293. bool areAnyPLayers; // if there are any playable players on map
  294. std::string name; //name of map
  295. std::string description; //and description
  296. int height, width;
  297. TerrainTile*** terrain;
  298. std::vector<Rumor> rumors;
  299. std::vector<DisposedHero> disposedHeroes;
  300. std::vector<CGHeroInstance*> predefinedHeroes;
  301. std::vector<CGDefInfo *> defy; // list of .def files with definitions from .h3m (may be custom)
  302. std::set<CGDefInfo *> defs; // other defInfos - for randomized objects, objects added or modified by scripts
  303. PlayerInfo players[8]; // info about players
  304. std::vector<int> teams; // teams[i] = team of player no i
  305. LossCondition lossCondition;
  306. EvictoryConditions victoryCondition; //victory conditions
  307. CspecificVictoryConidtions * vicConDetails; // used only if vistory conditions aren't standard
  308. int howManyTeams;
  309. std::vector<bool> allowedSpell; //allowedSpell[spell_ID] - if the spell is allowed
  310. std::vector<bool> allowedArtifact; //allowedArtifact[artifact_ID] - if the artifact is allowed
  311. std::vector<bool> allowedAbilities; //allowedAbilities[ability_ID] - if the ability is allowed
  312. std::vector<bool> allowedHeroes; //allowedHeroes[hero_ID] - if the hero is allowed
  313. std::vector<CMapEvent> events;
  314. int3 grailPos;
  315. int grailRadious;
  316. std::vector<CGObjectInstance*> objects;
  317. std::vector<CGHeroInstance*> heroes;
  318. std::vector<CGTownInstance*> towns;
  319. void initFromBytes(unsigned char * bufor); //creates map from decompressed .h3m data
  320. void readEvents( unsigned char * bufor, int &i);
  321. void readObjects( unsigned char * bufor, int &i);
  322. void readDefInfo( unsigned char * bufor, int &i);
  323. void readTerrain( unsigned char * bufor, int &i);
  324. void readPredefinedHeroes( unsigned char * bufor, int &i);
  325. void readHeader( unsigned char * bufor, int &i);
  326. void readRumors( unsigned char * bufor, int &i);
  327. void loadViCLossConditions( unsigned char * bufor, int &i);
  328. void loadPlayerInfo( int &pom, unsigned char * bufor, int &i);
  329. void loadHero( CGObjectInstance * &nobj, unsigned char * bufor, int &i);
  330. void loadTown( CGObjectInstance * &nobj, unsigned char * bufor, int &i);
  331. int loadSeerHut( unsigned char * bufor, int i, CGObjectInstance *& nobj);
  332. void addBlockVisTiles(CGObjectInstance * obj);
  333. void removeBlockVisTiles(CGObjectInstance * obj);
  334. Mapa(std::string filename); //creates map structure from .h3m file
  335. CGHeroInstance * getHero(int ID, int mode=0);
  336. bool isInTheMap(int3 pos);
  337. template <typename Handler> void serialize(Handler &h, const int version)
  338. {
  339. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & rumors & defy & defs
  340. & players & teams & lossCondition & victoryCondition & howManyTeams & allowedSpell & allowedAbilities
  341. & allowedArtifact &allowedHeroes & events;
  342. //TODO: viccondetails
  343. if(h.saving)
  344. {
  345. //saving terrain
  346. for (int i = 0; i < width ; i++)
  347. for (int j = 0; j < height ; j++)
  348. for (int k = 0; k <= twoLevel ; k++)
  349. h & terrain[i][j][k];
  350. }
  351. else
  352. {
  353. //loading terrain
  354. terrain = new TerrainTile**[width]; // allocate memory
  355. for (int ii=0;ii<width;ii++)
  356. {
  357. terrain[ii] = new TerrainTile*[height]; // allocate memory
  358. for(int jj=0;jj<height;jj++)
  359. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  360. }
  361. for (int i = 0; i < width ; i++)
  362. for (int j = 0; j < height ; j++)
  363. for (int k = 0; k <= twoLevel ; k++)
  364. h & terrain[i][j][k];
  365. }
  366. //TODO: recreate blockvis maps
  367. }
  368. };
  369. #endif //MAPD_H