CTownHandler.h 13 KB

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