CTownHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * CTownHandler.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 "ResourceSet.h"
  13. #include "int3.h"
  14. #include "GameConstants.h"
  15. #include "IHandlerBase.h"
  16. #include "LogicalExpression.h"
  17. #include "battle/BattleHex.h"
  18. class CLegacyConfigParser;
  19. class JsonNode;
  20. class CTown;
  21. class CFaction;
  22. struct BattleHex;
  23. /// a typical building encountered in every castle ;]
  24. /// this is structure available to both client and server
  25. /// contains all mechanics-related data about town structures
  26. class DLL_LINKAGE CBuilding
  27. {
  28. std::string name;
  29. std::string description;
  30. public:
  31. typedef LogicalExpression<BuildingID> TRequired;
  32. CTown * town; // town this building belongs to
  33. TResources resources;
  34. TResources produce;
  35. TRequired requirements;
  36. std::string identifier;
  37. BuildingID bid; //structure ID
  38. BuildingID upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  39. BuildingSubID::EBuildingSubID subId; /// subtype for special buildings, -1 = the building is not special
  40. enum EBuildMode
  41. {
  42. BUILD_NORMAL, // 0 - normal, default
  43. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  44. BUILD_SPECIAL, // 2 - special - building can not be built normally
  45. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  46. } mode;
  47. enum ETowerHeight // for lookup towers and some grails
  48. {
  49. HEIGHT_NO_TOWER = 5, // building has not 'lookout tower' ability
  50. HEIGHT_LOW = 10, // low lookout tower, but castle without lookout tower gives radius 5
  51. HEIGHT_AVERAGE = 15,
  52. HEIGHT_HIGH = 20, // such tower is in the Tower town
  53. HEIGHT_SKYSHIP = std::numeric_limits<int>::max() // grail, open entire map
  54. } height;
  55. static const std::map<std::string, CBuilding::EBuildMode> MODES;
  56. static const std::map<std::string, CBuilding::ETowerHeight> TOWER_TYPES;
  57. CBuilding() : town(nullptr), mode(BUILD_NORMAL) {};
  58. const std::string &Name() const;
  59. const std::string &Description() const;
  60. //return base of upgrade(s) or this
  61. BuildingID getBase() const;
  62. // returns how many times build has to be upgraded to become build
  63. si32 getDistance(BuildingID build) const;
  64. /// input: faction, bid; output: subId, height;
  65. void update792(const BuildingID & bid, BuildingSubID::EBuildingSubID & subId, ETowerHeight & height);
  66. template <typename Handler> void serialize(Handler &h, const int version)
  67. {
  68. h & identifier;
  69. h & town;
  70. h & bid;
  71. h & resources;
  72. h & produce;
  73. h & name;
  74. h & description;
  75. h & requirements;
  76. h & upgrade;
  77. h & mode;
  78. if(version >= 792)
  79. {
  80. h & subId;
  81. h & height;
  82. }
  83. else if(!h.saving)
  84. {
  85. update792(bid, subId, height);
  86. }
  87. if(!h.saving)
  88. deserializeFix();
  89. }
  90. friend class CTownHandler;
  91. private:
  92. void deserializeFix();
  93. };
  94. /// This is structure used only by client
  95. /// Consists of all gui-related data about town structures
  96. /// Should be moved from lib to client
  97. struct DLL_LINKAGE CStructure
  98. {
  99. CBuilding * building; // base building. If null - this structure will be always present on screen
  100. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  101. int3 pos;
  102. std::string defName, borderName, areaName, identifier;
  103. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & pos;
  107. h & defName;
  108. h & borderName;
  109. h & areaName;
  110. h & identifier;
  111. h & building;
  112. h & buildable;
  113. h & hiddenUpgrade;
  114. }
  115. };
  116. struct DLL_LINKAGE SPuzzleInfo
  117. {
  118. ui16 number; //type of puzzle
  119. si16 x, y; //position
  120. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  121. std::string filename; //file with graphic of this puzzle
  122. template <typename Handler> void serialize(Handler &h, const int version)
  123. {
  124. h & number;
  125. h & x;
  126. h & y;
  127. h & whenUncovered;
  128. h & filename;
  129. }
  130. };
  131. class DLL_LINKAGE CFaction
  132. {
  133. public:
  134. CFaction();
  135. ~CFaction();
  136. std::string name; //town name, by default - from TownName.txt
  137. std::string identifier;
  138. TFaction index;
  139. ETerrainType nativeTerrain;
  140. EAlignment::EAlignment alignment;
  141. CTown * town; //NOTE: can be null
  142. std::string creatureBg120;
  143. std::string creatureBg130;
  144. std::vector<SPuzzleInfo> puzzleMap;
  145. template <typename Handler> void serialize(Handler &h, const int version)
  146. {
  147. h & name;
  148. h & identifier;
  149. h & index;
  150. h & nativeTerrain;
  151. h & alignment;
  152. h & town;
  153. h & creatureBg120;
  154. h & creatureBg130;
  155. h & puzzleMap;
  156. }
  157. };
  158. class DLL_LINKAGE CTown
  159. {
  160. public:
  161. CTown();
  162. ~CTown();
  163. // TODO: remove once save and mod compatability not needed
  164. static std::vector<BattleHex> defaultMoatHexes();
  165. std::string getFactionName() const;
  166. std::string getBuildingScope() const;
  167. std::set<si32> getAllBuildings() const;
  168. CFaction * faction;
  169. std::vector<std::string> names; //names of the town instances
  170. /// level -> list of creatures on this tier
  171. // TODO: replace with pointers to CCreature
  172. std::vector<std::vector<CreatureID> > creatures;
  173. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  174. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  175. std::vector<std::string> dwellingNames;
  176. // should be removed at least from configs in favor of auto-detection
  177. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  178. ui32 mageLevel; //max available mage guild level
  179. ui16 primaryRes;
  180. ArtifactID warMachine;
  181. si32 moatDamage;
  182. std::vector<BattleHex> moatHexes;
  183. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  184. // resulting chance = sqrt(town.chance * heroClass.chance)
  185. ui32 defaultTavernChance;
  186. // Client-only data. Should be moved away from lib
  187. struct ClientInfo
  188. {
  189. struct Point
  190. {
  191. si32 x;
  192. si32 y;
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & x;
  196. h & y;
  197. }
  198. };
  199. //icons [fort is present?][build limit reached?] -> index of icon in def files
  200. int icons[2][2];
  201. std::string iconSmall[2][2]; /// icon names used during loading
  202. std::string iconLarge[2][2];
  203. std::string tavernVideo;
  204. std::string musicTheme;
  205. std::string townBackground;
  206. std::string guildBackground;
  207. std::string guildWindow;
  208. std::string buildingsIcons;
  209. std::string hallBackground;
  210. /// vector[row][column] = list of buildings in this slot
  211. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  212. /// list of town screen structures.
  213. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  214. std::vector<ConstTransitivePtr<CStructure> > structures;
  215. std::string siegePrefix;
  216. std::vector<Point> siegePositions;
  217. CreatureID siegeShooter; // shooter creature ID
  218. template <typename Handler> void serialize(Handler &h, const int version)
  219. {
  220. h & icons;
  221. h & iconSmall;
  222. h & iconLarge;
  223. h & tavernVideo;
  224. h & musicTheme;
  225. h & townBackground;
  226. h & guildBackground;
  227. h & guildWindow;
  228. h & buildingsIcons;
  229. h & hallBackground;
  230. h & hallSlots;
  231. h & structures;
  232. h & siegePrefix;
  233. h & siegePositions;
  234. h & siegeShooter;
  235. }
  236. } clientInfo;
  237. template <typename Handler> void serialize(Handler &h, const int version)
  238. {
  239. h & names;
  240. h & faction;
  241. h & creatures;
  242. h & dwellings;
  243. h & dwellingNames;
  244. h & buildings;
  245. h & hordeLvl;
  246. h & mageLevel;
  247. h & primaryRes;
  248. h & warMachine;
  249. h & clientInfo;
  250. h & moatDamage;
  251. if(version >= 758)
  252. {
  253. h & moatHexes;
  254. }
  255. else if(!h.saving)
  256. {
  257. moatHexes = defaultMoatHexes();
  258. }
  259. h & defaultTavernChance;
  260. }
  261. };
  262. class DLL_LINKAGE CTownHandler : public IHandlerBase
  263. {
  264. struct BuildingRequirementsHelper
  265. {
  266. JsonNode json;
  267. CBuilding * building;
  268. CTown * town;
  269. };
  270. std::map<CTown *, JsonNode> warMachinesToLoad;
  271. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  272. const static ETerrainType::EETerrainType defaultGoodTerrain = ETerrainType::EETerrainType::GRASS;
  273. const static ETerrainType::EETerrainType defaultEvilTerrain = ETerrainType::EETerrainType::LAVA;
  274. const static ETerrainType::EETerrainType defaultNeutralTerrain = ETerrainType::EETerrainType::ROUGH;
  275. void initializeRequirements();
  276. void initializeWarMachines();
  277. /// loads CBuilding's into town
  278. void loadBuildingRequirements(CBuilding * building, const JsonNode & source);
  279. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  280. void loadBuildings(CTown * town, const JsonNode & source);
  281. /// loads CStructure's into town
  282. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  283. void loadStructures(CTown &town, const JsonNode & source);
  284. /// loads town hall vector (hallSlots)
  285. void loadTownHall(CTown &town, const JsonNode & source);
  286. void loadSiegeScreen(CTown &town, const JsonNode & source);
  287. void loadClientData(CTown &town, const JsonNode & source);
  288. void loadTown(CTown * town, const JsonNode & source);
  289. void loadPuzzle(CFaction & faction, const JsonNode & source);
  290. ETerrainType::EETerrainType getDefaultTerrainForAlignment(EAlignment::EAlignment aligment) const;
  291. CFaction * loadFromJson(const JsonNode & data, const std::string & identifier);
  292. void loadRandomFaction();
  293. public:
  294. template<typename R, typename K>
  295. static R getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required = true);
  296. template<typename R>
  297. static R getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required = true);
  298. std::vector<ConstTransitivePtr<CFaction> > factions;
  299. CTown * randomTown;
  300. CTownHandler(); //c-tor, set pointer in VLC to this
  301. ~CTownHandler();
  302. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  303. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  304. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  305. void loadCustom() override;
  306. void afterLoadFinalization() override;
  307. std::vector<bool> getDefaultAllowed() const override;
  308. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  309. //json serialization helper
  310. static si32 decodeFaction(const std::string & identifier);
  311. //json serialization helper
  312. static std::string encodeFaction(const si32 index);
  313. template <typename Handler> void serialize(Handler &h, const int version)
  314. {
  315. h & factions;
  316. if(version >= 770)
  317. {
  318. h & randomTown;
  319. }
  320. else if(!h.saving)
  321. {
  322. loadRandomFaction();
  323. }
  324. }
  325. };