CTownHandler.h 14 KB

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