CMap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * CMap.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../ConstTransitivePtr.h"
  12. #include "../mapObjects/MiscObjects.h" // To serialize static props
  13. #include "../mapObjects/CQuest.h" // To serialize static props
  14. #include "../mapObjects/CGTownInstance.h" // To serialize static props
  15. #include "../ResourceSet.h"
  16. #include "../int3.h"
  17. #include "../GameConstants.h"
  18. #include "../LogicalExpression.h"
  19. #include "CMapDefines.h"
  20. class CArtifactInstance;
  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. class CMapEditManager;
  31. /// The hero name struct consists of the hero id and the hero name.
  32. struct DLL_LINKAGE SHeroName
  33. {
  34. SHeroName();
  35. int heroId;
  36. std::string heroName;
  37. template <typename Handler>
  38. void serialize(Handler & h, const int version)
  39. {
  40. h & heroId & heroName;
  41. }
  42. };
  43. /// The player info constains data about which factions are allowed, AI tactical settings,
  44. /// the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  45. struct DLL_LINKAGE PlayerInfo
  46. {
  47. PlayerInfo();
  48. /// Gets the default faction id or -1 for a random faction.
  49. si8 defaultCastle() const;
  50. /// Gets the default hero id or -1 for a random hero.
  51. si8 defaultHero() const;
  52. bool canAnyonePlay() const;
  53. bool hasCustomMainHero() const;
  54. bool canHumanPlay;
  55. bool canComputerPlay;
  56. EAiTactic::EAiTactic aiTactic; /// The default value is EAiTactic::RANDOM.
  57. std::set<TFaction> allowedFactions;
  58. bool isFactionRandom;
  59. si32 mainCustomHeroPortrait; /// The default value is -1.
  60. std::string mainCustomHeroName;
  61. si32 mainCustomHeroId; /// ID of custom hero (only if portrait and hero name are set, otherwise unpredicted value), -1 if none (not always -1)
  62. std::vector<SHeroName> heroesNames; /// list of placed heroes on the map
  63. bool hasMainTown; /// The default value is false.
  64. bool generateHeroAtMainTown; /// The default value is false.
  65. int3 posOfMainTown;
  66. TeamID team; /// The default value is 255 representing that the player belongs to no team.
  67. bool hasRandomHero; /// Player has a random hero
  68. bool generateHero; /// Unused.
  69. si32 p7; /// Unknown and unused.
  70. /// Unused. Count of hero placeholders containing hero type.
  71. /// WARNING: powerPlaceholders sometimes gives false 0 (eg. even if there is one placeholder), maybe different meaning ???
  72. ui8 powerPlaceholders;
  73. template <typename Handler>
  74. void serialize(Handler & h, const int version)
  75. {
  76. h & p7 & hasRandomHero & mainCustomHeroId & canHumanPlay & canComputerPlay & aiTactic & allowedFactions & isFactionRandom &
  77. mainCustomHeroPortrait & mainCustomHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  78. posOfMainTown & team & generateHero;
  79. }
  80. };
  81. /// The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  82. struct DLL_LINKAGE EventCondition
  83. {
  84. enum EWinLoseType {
  85. HAVE_ARTIFACT, // type - required artifact
  86. HAVE_CREATURES, // type - creatures to collect, value - amount to collect
  87. HAVE_RESOURCES, // type - resource ID, value - amount to collect
  88. HAVE_BUILDING, // position - town, optional, type - building to build
  89. CONTROL, // position - position of object, optional, type - type of object
  90. DESTROY, // position - position of object, optional, type - type of object
  91. TRANSPORT, // position - where artifact should be transported, type - type of artifact
  92. DAYS_PASSED, // value - number of days from start of the game
  93. IS_HUMAN, // value - 0 = player is AI, 1 = player is human
  94. DAYS_WITHOUT_TOWN, // value - how long player can live without town, 0=instakill
  95. STANDARD_WIN, // normal defeat all enemies condition
  96. CONST_VALUE // condition that always evaluates to "value" (0 = false, 1 = true)
  97. };
  98. EventCondition(EWinLoseType condition = STANDARD_WIN);
  99. EventCondition(EWinLoseType condition, si32 value, si32 objectType, int3 position = int3(-1, -1, -1));
  100. const CGObjectInstance * object; // object that was at specified position on start
  101. si32 value;
  102. si32 objectType;
  103. int3 position;
  104. EWinLoseType condition;
  105. template <typename Handler>
  106. void serialize(Handler & h, const int version)
  107. {
  108. h & object & value & objectType & position & condition;
  109. }
  110. };
  111. typedef LogicalExpression<EventCondition> EventExpression;
  112. struct DLL_LINKAGE EventEffect
  113. {
  114. enum EType
  115. {
  116. VICTORY,
  117. DEFEAT
  118. };
  119. /// effect type, using EType enum
  120. si8 type;
  121. /// message that will be sent to other players
  122. std::string toOtherMessage;
  123. template <typename Handler>
  124. void serialize(Handler & h, const int version)
  125. {
  126. h & type & toOtherMessage;
  127. }
  128. };
  129. struct DLL_LINKAGE TriggeredEvent
  130. {
  131. /// base condition that must be evaluated
  132. EventExpression trigger;
  133. /// string identifier read from config file (e.g. captureKreelah)
  134. std::string identifier;
  135. /// string-description, for use in UI (capture town to win)
  136. std::string description;
  137. /// Message that will be displayed when this event is triggered (You captured town. You won!)
  138. std::string onFulfill;
  139. /// Effect of this event. TODO: refactor into something more flexible
  140. EventEffect effect;
  141. template <typename Handler>
  142. void serialize(Handler & h, const int version)
  143. {
  144. h & identifier & trigger & description & onFulfill & effect;
  145. }
  146. };
  147. /// The rumor struct consists of a rumor name and text.
  148. struct DLL_LINKAGE Rumor
  149. {
  150. std::string name;
  151. std::string text;
  152. template <typename Handler>
  153. void serialize(Handler & h, const int version)
  154. {
  155. h & name & text;
  156. }
  157. };
  158. /// The disposed hero struct describes which hero can be hired from which player.
  159. struct DLL_LINKAGE DisposedHero
  160. {
  161. DisposedHero();
  162. ui32 heroId;
  163. ui16 portrait; /// The portrait id of the hero, 0xFF is default.
  164. std::string name;
  165. ui8 players; /// Who can hire this hero (bitfield).
  166. template <typename Handler>
  167. void serialize(Handler & h, const int version)
  168. {
  169. h & heroId & portrait & name & players;
  170. }
  171. };
  172. namespace EMapFormat
  173. {
  174. enum EMapFormat
  175. {
  176. INVALID = 0,
  177. // HEX DEC
  178. ROE = 0x0e, // 14
  179. AB = 0x15, // 21
  180. SOD = 0x1c, // 28
  181. // HOTA = 0x1e ... 0x20 // 28 ... 30
  182. WOG = 0x33 // 51
  183. };
  184. }
  185. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  186. class DLL_LINKAGE CMapHeader
  187. {
  188. void setupEvents();
  189. public:
  190. static const int MAP_SIZE_SMALL;
  191. static const int MAP_SIZE_MIDDLE;
  192. static const int MAP_SIZE_LARGE;
  193. static const int MAP_SIZE_XLARGE;
  194. CMapHeader();
  195. virtual ~CMapHeader();
  196. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  197. si32 height; /// The default value is 72.
  198. si32 width; /// The default value is 72.
  199. bool twoLevel; /// The default value is true.
  200. std::string name;
  201. std::string description;
  202. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  203. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  204. /// maximum level for heroes. This is the default value.
  205. ui8 levelLimit;
  206. std::string victoryMessage;
  207. std::string defeatMessage;
  208. ui16 victoryIconIndex;
  209. ui16 defeatIconIndex;
  210. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  211. ui8 howManyTeams;
  212. std::vector<bool> allowedHeroes;
  213. std::vector<ui16> placeholdedHeroes;
  214. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  215. /// "main quests" of the map that describe victory and loss conditions
  216. std::vector<TriggeredEvent> triggeredEvents;
  217. template <typename Handler>
  218. void serialize(Handler & h, const int Version)
  219. {
  220. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  221. h & players & howManyTeams & allowedHeroes & triggeredEvents;
  222. h & victoryMessage & victoryIconIndex & defeatMessage & defeatIconIndex;
  223. }
  224. };
  225. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  226. class DLL_LINKAGE CMap : public CMapHeader
  227. {
  228. public:
  229. CMap();
  230. ~CMap();
  231. void initTerrain();
  232. CMapEditManager * getEditManager();
  233. TerrainTile & getTile(const int3 & tile);
  234. const TerrainTile & getTile(const int3 & tile) const;
  235. bool isInTheMap(const int3 & pos) const;
  236. bool isWaterTile(const int3 & pos) const;
  237. bool checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const;
  238. int3 guardingCreaturePosition (int3 pos) const;
  239. void addBlockVisTiles(CGObjectInstance * obj);
  240. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  241. void calculateGuardingGreaturePositions();
  242. void addNewArtifactInstance(CArtifactInstance * art);
  243. void eraseArtifactInstance(CArtifactInstance * art);
  244. void addQuest(CGObjectInstance * quest);
  245. /// Gets object of specified type on requested position
  246. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, Obj::EObj type);
  247. CGHeroInstance * getHero(int heroId);
  248. /// Sets the victory/loss condition objectives ??
  249. void checkForObjectives();
  250. ui32 checksum;
  251. std::vector<Rumor> rumors;
  252. std::vector<DisposedHero> disposedHeroes;
  253. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  254. std::vector<bool> allowedSpell;
  255. std::vector<bool> allowedArtifact;
  256. std::vector<bool> allowedAbilities;
  257. std::list<CMapEvent> events;
  258. int3 grailPos;
  259. int grailRadious;
  260. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  261. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  262. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  263. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  264. std::vector< ConstTransitivePtr<CQuest> > quests;
  265. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  266. //Helper lists
  267. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  268. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  269. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  270. std::map<si32, ObjectInstanceID> questIdentifierToId;
  271. std::unique_ptr<CMapEditManager> editManager;
  272. int3 ***guardingCreaturePositions;
  273. private:
  274. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  275. TerrainTile*** terrain;
  276. public:
  277. template <typename Handler>
  278. void serialize(Handler &h, const int formatVersion)
  279. {
  280. h & static_cast<CMapHeader&>(*this);
  281. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  282. h & artInstances & quests & allHeroes;
  283. h & questIdentifierToId;
  284. //TODO: viccondetails
  285. int level = twoLevel ? 2 : 1;
  286. if(h.saving)
  287. {
  288. // Save terrain
  289. for(int i = 0; i < width ; ++i)
  290. {
  291. for(int j = 0; j < height ; ++j)
  292. {
  293. for(int k = 0; k < level; ++k)
  294. {
  295. h & terrain[i][j][k];
  296. h & guardingCreaturePositions[i][j][k];
  297. }
  298. }
  299. }
  300. }
  301. else
  302. {
  303. // Load terrain
  304. terrain = new TerrainTile**[width];
  305. guardingCreaturePositions = new int3**[width];
  306. for(int i = 0; i < width; ++i)
  307. {
  308. terrain[i] = new TerrainTile*[height];
  309. guardingCreaturePositions[i] = new int3*[height];
  310. for(int j = 0; j < height; ++j)
  311. {
  312. terrain[i][j] = new TerrainTile[level];
  313. guardingCreaturePositions[i][j] = new int3[level];
  314. }
  315. }
  316. for(int i = 0; i < width ; ++i)
  317. {
  318. for(int j = 0; j < height ; ++j)
  319. {
  320. for(int k = 0; k < level; ++k)
  321. {
  322. h & terrain[i][j][k];
  323. h & guardingCreaturePositions[i][j][k];
  324. }
  325. }
  326. }
  327. }
  328. h & objects;
  329. h & heroesOnMap & teleportChannels & towns & artInstances;
  330. // static members
  331. h & CGKeys::playerKeyMap;
  332. h & CGMagi::eyelist;
  333. h & CGObelisk::obeliskCount & CGObelisk::visited;
  334. h & CGTownInstance::merchantArtifacts;
  335. h & CGTownInstance::universitySkills;
  336. }
  337. };