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