CTownHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. STRONG_INLINE
  65. bool IsTradeBuilding() const
  66. {
  67. return bid == BuildingID::MARKETPLACE || subId == BuildingSubID::ARTIFACT_MERCHANT || subId == BuildingSubID::FREELANCERS_GUILD;
  68. }
  69. STRONG_INLINE
  70. bool IsWeekBonus() const
  71. {
  72. return subId == BuildingSubID::STABLES || subId == BuildingSubID::MANA_VORTEX;
  73. }
  74. STRONG_INLINE
  75. bool IsVisitingBonus() const
  76. {
  77. return subId == BuildingSubID::ATTACK_VISITING_BONUS ||
  78. subId == BuildingSubID::DEFENSE_VISITING_BONUS ||
  79. subId == BuildingSubID::SPELL_POWER_VISITING_BONUS ||
  80. subId == BuildingSubID::KNOWLEDGE_VISITING_BONUS ||
  81. subId == BuildingSubID::EXPERIENCE_VISITING_BONUS;
  82. }
  83. /// input: faction, bid; output: subId, height;
  84. void update792(const BuildingID & bid, BuildingSubID::EBuildingSubID & subId, ETowerHeight & height);
  85. template <typename Handler> void serialize(Handler &h, const int version)
  86. {
  87. h & identifier;
  88. h & town;
  89. h & bid;
  90. h & resources;
  91. h & produce;
  92. h & name;
  93. h & description;
  94. h & requirements;
  95. h & upgrade;
  96. h & mode;
  97. if(version >= 792)
  98. {
  99. h & subId;
  100. h & height;
  101. }
  102. else if(!h.saving)
  103. {
  104. update792(bid, subId, height);
  105. }
  106. if(!h.saving)
  107. deserializeFix();
  108. }
  109. friend class CTownHandler;
  110. private:
  111. void deserializeFix();
  112. };
  113. /// This is structure used only by client
  114. /// Consists of all gui-related data about town structures
  115. /// Should be moved from lib to client
  116. struct DLL_LINKAGE CStructure
  117. {
  118. CBuilding * building; // base building. If null - this structure will be always present on screen
  119. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  120. int3 pos;
  121. std::string defName, borderName, areaName, identifier;
  122. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  123. template <typename Handler> void serialize(Handler &h, const int version)
  124. {
  125. h & pos;
  126. h & defName;
  127. h & borderName;
  128. h & areaName;
  129. h & identifier;
  130. h & building;
  131. h & buildable;
  132. h & hiddenUpgrade;
  133. }
  134. };
  135. struct DLL_LINKAGE SPuzzleInfo
  136. {
  137. ui16 number; //type of puzzle
  138. si16 x, y; //position
  139. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  140. std::string filename; //file with graphic of this puzzle
  141. template <typename Handler> void serialize(Handler &h, const int version)
  142. {
  143. h & number;
  144. h & x;
  145. h & y;
  146. h & whenUncovered;
  147. h & filename;
  148. }
  149. };
  150. class DLL_LINKAGE CFaction
  151. {
  152. public:
  153. CFaction();
  154. ~CFaction();
  155. std::string name; //town name, by default - from TownName.txt
  156. std::string identifier;
  157. TFaction index;
  158. ETerrainType nativeTerrain;
  159. EAlignment::EAlignment alignment;
  160. CTown * town; //NOTE: can be null
  161. std::string creatureBg120;
  162. std::string creatureBg130;
  163. std::vector<SPuzzleInfo> puzzleMap;
  164. template <typename Handler> void serialize(Handler &h, const int version)
  165. {
  166. h & name;
  167. h & identifier;
  168. h & index;
  169. h & nativeTerrain;
  170. h & alignment;
  171. h & town;
  172. h & creatureBg120;
  173. h & creatureBg130;
  174. h & puzzleMap;
  175. }
  176. };
  177. class DLL_LINKAGE CTown
  178. {
  179. public:
  180. CTown();
  181. ~CTown();
  182. // TODO: remove once save and mod compatability not needed
  183. static std::vector<BattleHex> defaultMoatHexes();
  184. std::string getFactionName() const;
  185. std::string getBuildingScope() const;
  186. std::set<si32> getAllBuildings() const;
  187. const CBuilding * getSpecialBuilding(BuildingSubID::EBuildingSubID subID) const;
  188. BuildingID::EBuildingID getBuildingType(BuildingSubID::EBuildingSubID subID) const;
  189. CFaction * faction;
  190. std::vector<std::string> names; //names of the town instances
  191. /// level -> list of creatures on this tier
  192. // TODO: replace with pointers to CCreature
  193. std::vector<std::vector<CreatureID> > creatures;
  194. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  195. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  196. std::vector<std::string> dwellingNames;
  197. // should be removed at least from configs in favor of auto-detection
  198. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  199. ui32 mageLevel; //max available mage guild level
  200. ui16 primaryRes;
  201. ArtifactID warMachine;
  202. si32 moatDamage;
  203. std::vector<BattleHex> moatHexes;
  204. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  205. // resulting chance = sqrt(town.chance * heroClass.chance)
  206. ui32 defaultTavernChance;
  207. // Client-only data. Should be moved away from lib
  208. struct ClientInfo
  209. {
  210. struct Point
  211. {
  212. si32 x;
  213. si32 y;
  214. template <typename Handler> void serialize(Handler &h, const int version)
  215. {
  216. h & x;
  217. h & y;
  218. }
  219. };
  220. //icons [fort is present?][build limit reached?] -> index of icon in def files
  221. int icons[2][2];
  222. std::string iconSmall[2][2]; /// icon names used during loading
  223. std::string iconLarge[2][2];
  224. std::string tavernVideo;
  225. std::string musicTheme;
  226. std::string townBackground;
  227. std::string guildBackground;
  228. std::string guildWindow;
  229. std::string buildingsIcons;
  230. std::string hallBackground;
  231. /// vector[row][column] = list of buildings in this slot
  232. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  233. /// list of town screen structures.
  234. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  235. std::vector<ConstTransitivePtr<CStructure> > structures;
  236. std::string siegePrefix;
  237. std::vector<Point> siegePositions;
  238. CreatureID siegeShooter; // shooter creature ID
  239. template <typename Handler> void serialize(Handler &h, const int version)
  240. {
  241. h & icons;
  242. h & iconSmall;
  243. h & iconLarge;
  244. h & tavernVideo;
  245. h & musicTheme;
  246. h & townBackground;
  247. h & guildBackground;
  248. h & guildWindow;
  249. h & buildingsIcons;
  250. h & hallBackground;
  251. h & hallSlots;
  252. h & structures;
  253. h & siegePrefix;
  254. h & siegePositions;
  255. h & siegeShooter;
  256. }
  257. } clientInfo;
  258. template <typename Handler> void serialize(Handler &h, const int version)
  259. {
  260. h & names;
  261. h & faction;
  262. h & creatures;
  263. h & dwellings;
  264. h & dwellingNames;
  265. h & buildings;
  266. h & hordeLvl;
  267. h & mageLevel;
  268. h & primaryRes;
  269. h & warMachine;
  270. h & clientInfo;
  271. h & moatDamage;
  272. if(version >= 758)
  273. {
  274. h & moatHexes;
  275. }
  276. else if(!h.saving)
  277. {
  278. moatHexes = defaultMoatHexes();
  279. }
  280. h & defaultTavernChance;
  281. }
  282. };
  283. class DLL_LINKAGE CTownHandler : public IHandlerBase
  284. {
  285. struct BuildingRequirementsHelper
  286. {
  287. JsonNode json;
  288. CBuilding * building;
  289. CTown * town;
  290. };
  291. std::map<CTown *, JsonNode> warMachinesToLoad;
  292. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  293. const static ETerrainType::EETerrainType defaultGoodTerrain = ETerrainType::EETerrainType::GRASS;
  294. const static ETerrainType::EETerrainType defaultEvilTerrain = ETerrainType::EETerrainType::LAVA;
  295. const static ETerrainType::EETerrainType defaultNeutralTerrain = ETerrainType::EETerrainType::ROUGH;
  296. void initializeRequirements();
  297. void initializeWarMachines();
  298. /// loads CBuilding's into town
  299. void loadBuildingRequirements(CBuilding * building, const JsonNode & source);
  300. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  301. void loadBuildings(CTown * town, const JsonNode & source);
  302. /// loads CStructure's into town
  303. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  304. void loadStructures(CTown &town, const JsonNode & source);
  305. /// loads town hall vector (hallSlots)
  306. void loadTownHall(CTown &town, const JsonNode & source);
  307. void loadSiegeScreen(CTown &town, const JsonNode & source);
  308. void loadClientData(CTown &town, const JsonNode & source);
  309. void loadTown(CTown * town, const JsonNode & source);
  310. void loadPuzzle(CFaction & faction, const JsonNode & source);
  311. ETerrainType::EETerrainType getDefaultTerrainForAlignment(EAlignment::EAlignment aligment) const;
  312. CFaction * loadFromJson(const JsonNode & data, const std::string & identifier);
  313. void loadRandomFaction();
  314. public:
  315. template<typename R, typename K>
  316. static R getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required = true);
  317. template<typename R>
  318. static R getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required = true);
  319. std::vector<ConstTransitivePtr<CFaction> > factions;
  320. CTown * randomTown;
  321. CTownHandler(); //c-tor, set pointer in VLC to this
  322. ~CTownHandler();
  323. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  324. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  325. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  326. void loadCustom() override;
  327. void afterLoadFinalization() override;
  328. std::vector<bool> getDefaultAllowed() const override;
  329. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  330. //json serialization helper
  331. static si32 decodeFaction(const std::string & identifier);
  332. //json serialization helper
  333. static std::string encodeFaction(const si32 index);
  334. template <typename Handler> void serialize(Handler &h, const int version)
  335. {
  336. h & factions;
  337. if(version >= 770)
  338. {
  339. h & randomTown;
  340. }
  341. else if(!h.saving)
  342. {
  343. loadRandomFaction();
  344. }
  345. }
  346. };