CTownHandler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <vcmi/Faction.h>
  12. #include <vcmi/FactionService.h>
  13. #include "ConstTransitivePtr.h"
  14. #include "ResourceSet.h"
  15. #include "int3.h"
  16. #include "GameConstants.h"
  17. #include "IHandlerBase.h"
  18. #include "LogicalExpression.h"
  19. #include "battle/BattleHex.h"
  20. #include "bonuses/Bonus.h"
  21. #include "bonuses/BonusList.h"
  22. #include "Point.h"
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. class CLegacyConfigParser;
  25. class JsonNode;
  26. class CTown;
  27. class CFaction;
  28. struct BattleHex;
  29. class JsonSerializeFormat;
  30. /// a typical building encountered in every castle ;]
  31. /// this is structure available to both client and server
  32. /// contains all mechanics-related data about town structures
  33. class DLL_LINKAGE CBuilding
  34. {
  35. std::string modScope;
  36. std::string identifier;
  37. public:
  38. using TRequired = LogicalExpression<BuildingID>;
  39. CTown * town; // town this building belongs to
  40. TResources resources;
  41. TResources produce;
  42. TRequired requirements;
  43. BuildingID bid; //structure ID
  44. BuildingID upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  45. BuildingSubID::EBuildingSubID subId; /// subtype for special buildings, -1 = the building is not special
  46. std::set<BuildingID> overrideBids; /// the building which bonuses should be overridden with bonuses of the current building
  47. BonusList buildingBonuses;
  48. BonusList onVisitBonuses;
  49. enum EBuildMode
  50. {
  51. BUILD_NORMAL, // 0 - normal, default
  52. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  53. BUILD_SPECIAL, // 2 - special - building can not be built normally
  54. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  55. } mode;
  56. enum ETowerHeight // for lookup towers and some grails
  57. {
  58. HEIGHT_NO_TOWER = 5, // building has not 'lookout tower' ability
  59. HEIGHT_LOW = 10, // low lookout tower, but castle without lookout tower gives radius 5
  60. HEIGHT_AVERAGE = 15,
  61. HEIGHT_HIGH = 20, // such tower is in the Tower town
  62. HEIGHT_SKYSHIP = std::numeric_limits<int>::max() // grail, open entire map
  63. } height;
  64. static const std::map<std::string, CBuilding::EBuildMode> MODES;
  65. static const std::map<std::string, CBuilding::ETowerHeight> TOWER_TYPES;
  66. CBuilding() : town(nullptr), mode(BUILD_NORMAL) {};
  67. std::string getJsonKey() const;
  68. std::string getNameTranslated() const;
  69. std::string getDescriptionTranslated() const;
  70. std::string getNameTextID() const;
  71. std::string getDescriptionTextID() const;
  72. //return base of upgrade(s) or this
  73. BuildingID getBase() const;
  74. // returns how many times build has to be upgraded to become build
  75. si32 getDistance(const BuildingID & build) const;
  76. STRONG_INLINE
  77. bool IsTradeBuilding() const
  78. {
  79. return bid == BuildingID::MARKETPLACE || subId == BuildingSubID::ARTIFACT_MERCHANT || subId == BuildingSubID::FREELANCERS_GUILD;
  80. }
  81. STRONG_INLINE
  82. bool IsWeekBonus() const
  83. {
  84. return subId == BuildingSubID::STABLES || subId == BuildingSubID::MANA_VORTEX;
  85. }
  86. STRONG_INLINE
  87. bool IsVisitingBonus() const
  88. {
  89. return subId == BuildingSubID::ATTACK_VISITING_BONUS ||
  90. subId == BuildingSubID::DEFENSE_VISITING_BONUS ||
  91. subId == BuildingSubID::SPELL_POWER_VISITING_BONUS ||
  92. subId == BuildingSubID::KNOWLEDGE_VISITING_BONUS ||
  93. subId == BuildingSubID::EXPERIENCE_VISITING_BONUS ||
  94. subId == BuildingSubID::CUSTOM_VISITING_BONUS;
  95. }
  96. void addNewBonus(const std::shared_ptr<Bonus> & b, BonusList & bonusList) const;
  97. template <typename Handler> void serialize(Handler &h, const int version)
  98. {
  99. h & modScope;
  100. h & identifier;
  101. h & town;
  102. h & bid;
  103. h & resources;
  104. h & produce;
  105. h & requirements;
  106. h & upgrade;
  107. h & mode;
  108. h & subId;
  109. h & height;
  110. h & overrideBids;
  111. h & buildingBonuses;
  112. h & onVisitBonuses;
  113. }
  114. friend class CTownHandler;
  115. };
  116. /// This is structure used only by client
  117. /// Consists of all gui-related data about town structures
  118. /// Should be moved from lib to client
  119. struct DLL_LINKAGE CStructure
  120. {
  121. CBuilding * building; // base building. If null - this structure will be always present on screen
  122. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  123. int3 pos;
  124. std::string defName, borderName, areaName, identifier;
  125. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  126. template <typename Handler> void serialize(Handler &h, const int version)
  127. {
  128. h & pos;
  129. h & defName;
  130. h & borderName;
  131. h & areaName;
  132. h & identifier;
  133. h & building;
  134. h & buildable;
  135. h & hiddenUpgrade;
  136. }
  137. };
  138. struct DLL_LINKAGE SPuzzleInfo
  139. {
  140. ui16 number; //type of puzzle
  141. si16 x, y; //position
  142. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  143. std::string filename; //file with graphic of this puzzle
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & number;
  147. h & x;
  148. h & y;
  149. h & whenUncovered;
  150. h & filename;
  151. }
  152. };
  153. class DLL_LINKAGE CFaction : public Faction
  154. {
  155. friend class CTownHandler;
  156. friend class CBuilding;
  157. friend class CTown;
  158. std::string modScope;
  159. std::string identifier;
  160. FactionID index = FactionID::NEUTRAL;
  161. FactionID getFaction() const override; //This function should not be used
  162. public:
  163. TerrainId nativeTerrain;
  164. EAlignment alignment = EAlignment::NEUTRAL;
  165. bool preferUndergroundPlacement = false;
  166. CTown * town = nullptr; //NOTE: can be null
  167. std::string creatureBg120;
  168. std::string creatureBg130;
  169. std::vector<SPuzzleInfo> puzzleMap;
  170. CFaction() = default;
  171. ~CFaction();
  172. int32_t getIndex() const override;
  173. int32_t getIconIndex() const override;
  174. std::string getJsonKey() const override;
  175. void registerIcons(const IconRegistar & cb) const override;
  176. FactionID getId() const override;
  177. std::string getNameTranslated() const override;
  178. std::string getNameTextID() const override;
  179. bool hasTown() const override;
  180. TerrainId getNativeTerrain() const override;
  181. EAlignment getAlignment() const override;
  182. void updateFrom(const JsonNode & data);
  183. void serializeJson(JsonSerializeFormat & handler);
  184. template <typename Handler> void serialize(Handler &h, const int version)
  185. {
  186. h & modScope;
  187. h & identifier;
  188. h & index;
  189. h & nativeTerrain;
  190. h & alignment;
  191. h & town;
  192. h & creatureBg120;
  193. h & creatureBg130;
  194. h & puzzleMap;
  195. }
  196. };
  197. class DLL_LINKAGE CTown
  198. {
  199. friend class CTownHandler;
  200. size_t namesCount = 0;
  201. public:
  202. CTown();
  203. ~CTown();
  204. std::string getBuildingScope() const;
  205. std::set<si32> getAllBuildings() const;
  206. const CBuilding * getSpecialBuilding(BuildingSubID::EBuildingSubID subID) const;
  207. std::string getGreeting(BuildingSubID::EBuildingSubID subID) const;
  208. void setGreeting(BuildingSubID::EBuildingSubID subID, const std::string & message) const; //may affect only mutable field
  209. BuildingID::EBuildingID getBuildingType(BuildingSubID::EBuildingSubID subID) const;
  210. std::string getRandomNameTranslated(size_t index) const;
  211. std::string getRandomNameTextID(size_t index) const;
  212. size_t getRandomNamesCount() const;
  213. CFaction * faction;
  214. /// level -> list of creatures on this tier
  215. // TODO: replace with pointers to CCreature
  216. std::vector<std::vector<CreatureID> > creatures;
  217. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  218. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  219. std::vector<std::string> dwellingNames;
  220. // should be removed at least from configs in favor of auto-detection
  221. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  222. ui32 mageLevel; //max available mage guild level
  223. GameResID primaryRes;
  224. ArtifactID warMachine;
  225. SpellID moatAbility;
  226. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  227. // resulting chance = sqrt(town.chance * heroClass.chance)
  228. ui32 defaultTavernChance;
  229. // Client-only data. Should be moved away from lib
  230. struct ClientInfo
  231. {
  232. //icons [fort is present?][build limit reached?] -> index of icon in def files
  233. int icons[2][2];
  234. std::string iconSmall[2][2]; /// icon names used during loading
  235. std::string iconLarge[2][2];
  236. std::string tavernVideo;
  237. std::string musicTheme;
  238. std::string townBackground;
  239. std::string guildBackground;
  240. std::string guildWindow;
  241. std::string buildingsIcons;
  242. std::string hallBackground;
  243. /// vector[row][column] = list of buildings in this slot
  244. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  245. /// list of town screen structures.
  246. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  247. std::vector<ConstTransitivePtr<CStructure> > structures;
  248. std::string siegePrefix;
  249. std::vector<Point> siegePositions;
  250. CreatureID siegeShooter; // shooter creature ID
  251. std::string towerIconSmall;
  252. std::string towerIconLarge;
  253. template <typename Handler> void serialize(Handler &h, const int version)
  254. {
  255. h & icons;
  256. h & iconSmall;
  257. h & iconLarge;
  258. h & tavernVideo;
  259. h & musicTheme;
  260. h & townBackground;
  261. h & guildBackground;
  262. h & guildWindow;
  263. h & buildingsIcons;
  264. h & hallBackground;
  265. h & hallSlots;
  266. h & structures;
  267. h & siegePrefix;
  268. h & siegePositions;
  269. h & siegeShooter;
  270. h & towerIconSmall;
  271. h & towerIconLarge;
  272. }
  273. } clientInfo;
  274. template <typename Handler> void serialize(Handler &h, const int version)
  275. {
  276. h & namesCount;
  277. h & faction;
  278. h & creatures;
  279. h & dwellings;
  280. h & dwellingNames;
  281. h & buildings;
  282. h & hordeLvl;
  283. h & mageLevel;
  284. h & primaryRes;
  285. h & warMachine;
  286. h & clientInfo;
  287. h & moatAbility;
  288. h & defaultTavernChance;
  289. }
  290. private:
  291. ///generated bonusing buildings messages for all towns of this type.
  292. mutable std::map<BuildingSubID::EBuildingSubID, const std::string> specialMessages; //may be changed by CGTownBuilding::getVisitingBonusGreeting() const
  293. };
  294. class DLL_LINKAGE CTownHandler : public CHandlerBase<FactionID, Faction, CFaction, FactionService>
  295. {
  296. struct BuildingRequirementsHelper
  297. {
  298. JsonNode json;
  299. CBuilding * building;
  300. CTown * town;
  301. };
  302. std::map<CTown *, JsonNode> warMachinesToLoad;
  303. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  304. std::vector<BuildingRequirementsHelper> overriddenBidsToLoad; //list of buildings, which bonuses should be overridden.
  305. static TPropagatorPtr & emptyPropagator();
  306. void initializeRequirements();
  307. void initializeOverridden();
  308. void initializeWarMachines();
  309. /// loads CBuilding's into town
  310. void loadBuildingRequirements(CBuilding * building, const JsonNode & source, std::vector<BuildingRequirementsHelper> & bidsToLoad) const;
  311. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  312. void loadBuildings(CTown * town, const JsonNode & source);
  313. std::shared_ptr<Bonus> createBonus(CBuilding * build, BonusType type, int val, int subtype = -1) const;
  314. std::shared_ptr<Bonus> createBonus(CBuilding * build, BonusType type, int val, TPropagatorPtr & prop, int subtype = -1) const;
  315. std::shared_ptr<Bonus> createBonusImpl(const BuildingID & building,
  316. BonusType type,
  317. int val,
  318. TPropagatorPtr & prop,
  319. const std::string & description,
  320. int subtype = -1) const;
  321. /// loads CStructure's into town
  322. void loadStructure(CTown & town, const std::string & stringID, const JsonNode & source) const;
  323. void loadStructures(CTown & town, const JsonNode & source) const;
  324. /// loads town hall vector (hallSlots)
  325. void loadTownHall(CTown & town, const JsonNode & source) const;
  326. void loadSiegeScreen(CTown & town, const JsonNode & source) const;
  327. void loadClientData(CTown & town, const JsonNode & source) const;
  328. void loadTown(CTown * town, const JsonNode & source);
  329. void loadPuzzle(CFaction & faction, const JsonNode & source) const;
  330. void loadRandomFaction();
  331. public:
  332. template<typename R, typename K>
  333. static R getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required = true);
  334. template<typename R>
  335. static R getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required = true);
  336. CTown * randomTown;
  337. CFaction * randomFaction;
  338. CTownHandler();
  339. ~CTownHandler();
  340. std::vector<JsonNode> loadLegacyData() override;
  341. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  342. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  343. void addBonusesForVanilaBuilding(CBuilding * building) const;
  344. void loadCustom() override;
  345. void afterLoadFinalization() override;
  346. std::vector<bool> getDefaultAllowed() const override;
  347. std::set<FactionID> getAllowedFactions(bool withTown = true) const;
  348. static void loadSpecialBuildingBonuses(const JsonNode & source, BonusList & bonusList, CBuilding * building);
  349. template <typename Handler> void serialize(Handler &h, const int version)
  350. {
  351. h & objects;
  352. h & randomTown;
  353. }
  354. protected:
  355. const std::vector<std::string> & getTypeNames() const override;
  356. CFaction * loadFromJson(const std::string & scope, const JsonNode & data, const std::string & identifier, size_t index) override;
  357. };
  358. VCMI_LIB_NAMESPACE_END