CTownHandler.h 14 KB

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