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. VCMI_LIB_NAMESPACE_BEGIN
  22. class CLegacyConfigParser;
  23. class JsonNode;
  24. class CTown;
  25. class CFaction;
  26. struct BattleHex;
  27. class JsonSerializeFormat;
  28. /// a typical building encountered in every castle ;]
  29. /// this is structure available to both client and server
  30. /// contains all mechanics-related data about town structures
  31. class DLL_LINKAGE CBuilding
  32. {
  33. std::string name;
  34. std::string description;
  35. public:
  36. typedef LogicalExpression<BuildingID> TRequired;
  37. CTown * town; // town this building belongs to
  38. TResources resources;
  39. TResources produce;
  40. TRequired requirements;
  41. std::string identifier;
  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. const std::string &Name() const;
  67. const std::string &Description() const;
  68. //return base of upgrade(s) or this
  69. BuildingID getBase() const;
  70. // returns how many times build has to be upgraded to become build
  71. si32 getDistance(BuildingID build) const;
  72. STRONG_INLINE
  73. bool IsTradeBuilding() const
  74. {
  75. return bid == BuildingID::MARKETPLACE || subId == BuildingSubID::ARTIFACT_MERCHANT || subId == BuildingSubID::FREELANCERS_GUILD;
  76. }
  77. STRONG_INLINE
  78. bool IsWeekBonus() const
  79. {
  80. return subId == BuildingSubID::STABLES || subId == BuildingSubID::MANA_VORTEX;
  81. }
  82. STRONG_INLINE
  83. bool IsVisitingBonus() const
  84. {
  85. return subId == BuildingSubID::ATTACK_VISITING_BONUS ||
  86. subId == BuildingSubID::DEFENSE_VISITING_BONUS ||
  87. subId == BuildingSubID::SPELL_POWER_VISITING_BONUS ||
  88. subId == BuildingSubID::KNOWLEDGE_VISITING_BONUS ||
  89. subId == BuildingSubID::EXPERIENCE_VISITING_BONUS ||
  90. subId == BuildingSubID::CUSTOM_VISITING_BONUS;
  91. }
  92. void addNewBonus(std::shared_ptr<Bonus> b, BonusList & bonusList);
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. h & identifier;
  96. h & town;
  97. h & bid;
  98. h & resources;
  99. h & produce;
  100. h & name;
  101. h & description;
  102. h & requirements;
  103. h & upgrade;
  104. h & mode;
  105. h & subId;
  106. h & height;
  107. h & overrideBids;
  108. h & buildingBonuses;
  109. h & onVisitBonuses;
  110. }
  111. friend class CTownHandler;
  112. };
  113. /// This is structure used only by client
  114. /// Consists of all gui-related data about town structures
  115. /// Should be moved from lib to client
  116. struct DLL_LINKAGE CStructure
  117. {
  118. CBuilding * building; // base building. If null - this structure will be always present on screen
  119. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  120. int3 pos;
  121. std::string defName, borderName, areaName, identifier;
  122. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  123. template <typename Handler> void serialize(Handler &h, const int version)
  124. {
  125. h & pos;
  126. h & defName;
  127. h & borderName;
  128. h & areaName;
  129. h & identifier;
  130. h & building;
  131. h & buildable;
  132. h & hiddenUpgrade;
  133. }
  134. };
  135. struct DLL_LINKAGE SPuzzleInfo
  136. {
  137. ui16 number; //type of puzzle
  138. si16 x, y; //position
  139. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  140. std::string filename; //file with graphic of this puzzle
  141. template <typename Handler> void serialize(Handler &h, const int version)
  142. {
  143. h & number;
  144. h & x;
  145. h & y;
  146. h & whenUncovered;
  147. h & filename;
  148. }
  149. };
  150. class DLL_LINKAGE CFaction : public Faction
  151. {
  152. public:
  153. std::string name; //town name, by default - from TownName.txt
  154. std::string identifier;
  155. TFaction index;
  156. TerrainId nativeTerrain;
  157. EAlignment::EAlignment alignment;
  158. bool preferUndergroundPlacement;
  159. CTown * town; //NOTE: can be null
  160. std::string creatureBg120;
  161. std::string creatureBg130;
  162. std::vector<SPuzzleInfo> puzzleMap;
  163. CFaction();
  164. ~CFaction();
  165. int32_t getIndex() const override;
  166. int32_t getIconIndex() const override;
  167. const std::string & getName() const override;
  168. const std::string & getJsonKey() const override;
  169. void registerIcons(const IconRegistar & cb) const override;
  170. FactionID getId() const override;
  171. bool hasTown() const override;
  172. void updateFrom(const JsonNode & data);
  173. void serializeJson(JsonSerializeFormat & handler);
  174. template <typename Handler> void serialize(Handler &h, const int version)
  175. {
  176. h & name;
  177. h & identifier;
  178. h & index;
  179. h & nativeTerrain;
  180. h & alignment;
  181. h & town;
  182. h & creatureBg120;
  183. h & creatureBg130;
  184. h & puzzleMap;
  185. }
  186. };
  187. class DLL_LINKAGE CTown
  188. {
  189. public:
  190. CTown();
  191. ~CTown();
  192. // TODO: remove once save and mod compatability not needed
  193. static std::vector<BattleHex> defaultMoatHexes();
  194. std::string getLocalizedFactionName() const;
  195. std::string getBuildingScope() const;
  196. std::set<si32> getAllBuildings() const;
  197. const CBuilding * getSpecialBuilding(BuildingSubID::EBuildingSubID subID) const;
  198. const std::string getGreeting(BuildingSubID::EBuildingSubID subID) const;
  199. void setGreeting(BuildingSubID::EBuildingSubID subID, const std::string message) const; //may affect only mutable field
  200. BuildingID::EBuildingID getBuildingType(BuildingSubID::EBuildingSubID subID) const;
  201. CFaction * faction;
  202. std::vector<std::string> names; //names of the town instances
  203. /// level -> list of creatures on this tier
  204. // TODO: replace with pointers to CCreature
  205. std::vector<std::vector<CreatureID> > creatures;
  206. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  207. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  208. std::vector<std::string> dwellingNames;
  209. // should be removed at least from configs in favor of auto-detection
  210. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  211. ui32 mageLevel; //max available mage guild level
  212. ui16 primaryRes;
  213. ArtifactID warMachine;
  214. si32 moatDamage;
  215. std::vector<BattleHex> moatHexes;
  216. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  217. // resulting chance = sqrt(town.chance * heroClass.chance)
  218. ui32 defaultTavernChance;
  219. // Client-only data. Should be moved away from lib
  220. struct ClientInfo
  221. {
  222. struct Point
  223. {
  224. si32 x;
  225. si32 y;
  226. template <typename Handler> void serialize(Handler &h, const int version)
  227. {
  228. h & x;
  229. h & y;
  230. }
  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 & names;
  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 & moatDamage;
  288. h & moatHexes;
  289. h & defaultTavernChance;
  290. }
  291. private:
  292. ///generated bonusing buildings messages for all towns of this type.
  293. mutable std::map<BuildingSubID::EBuildingSubID, const std::string> specialMessages; //may be changed by CGTownBuilding::getVisitingBonusGreeting() const
  294. };
  295. class DLL_LINKAGE CTownHandler : public CHandlerBase<FactionID, Faction, CFaction, FactionService>
  296. {
  297. struct BuildingRequirementsHelper
  298. {
  299. JsonNode json;
  300. CBuilding * building;
  301. CTown * town;
  302. };
  303. std::map<CTown *, JsonNode> warMachinesToLoad;
  304. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  305. std::vector<BuildingRequirementsHelper> overriddenBidsToLoad; //list of buildings, which bonuses should be overridden.
  306. static TPropagatorPtr & emptyPropagator();
  307. void initializeRequirements();
  308. void initializeOverridden();
  309. void initializeWarMachines();
  310. /// loads CBuilding's into town
  311. void loadBuildingRequirements(CBuilding * building, const JsonNode & source, std::vector<BuildingRequirementsHelper> & bidsToLoad);
  312. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  313. void loadBuildings(CTown * town, const JsonNode & source);
  314. std::shared_ptr<Bonus> createBonus(CBuilding * build, Bonus::BonusType type, int val, int subtype = -1);
  315. std::shared_ptr<Bonus> createBonus(CBuilding * build, Bonus::BonusType type, int val, TPropagatorPtr & prop, int subtype = -1);
  316. std::shared_ptr<Bonus> createBonusImpl(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr & prop, const std::string & description, int subtype = -1);
  317. /// loads CStructure's into town
  318. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  319. void loadStructures(CTown &town, const JsonNode & source);
  320. /// loads town hall vector (hallSlots)
  321. void loadTownHall(CTown &town, const JsonNode & source);
  322. void loadSiegeScreen(CTown &town, const JsonNode & source);
  323. void loadClientData(CTown &town, const JsonNode & source);
  324. void loadTown(CTown * town, const JsonNode & source);
  325. void loadPuzzle(CFaction & faction, const JsonNode & source);
  326. void loadRandomFaction();
  327. public:
  328. template<typename R, typename K>
  329. static R getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required = true);
  330. template<typename R>
  331. static R getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required = true);
  332. CTown * randomTown;
  333. CTownHandler();
  334. ~CTownHandler();
  335. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  336. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  337. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  338. void addBonusesForVanilaBuilding(CBuilding * building);
  339. void loadCustom() override;
  340. void afterLoadFinalization() override;
  341. std::vector<bool> getDefaultAllowed() const override;
  342. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  343. static void loadSpecialBuildingBonuses(const JsonNode & source, BonusList & bonusList, CBuilding * building);
  344. template <typename Handler> void serialize(Handler &h, const int version)
  345. {
  346. h & objects;
  347. h & randomTown;
  348. }
  349. protected:
  350. const std::vector<std::string> & getTypeNames() const override;
  351. CFaction * loadFromJson(const std::string & scope, const JsonNode & data, const std::string & identifier, size_t index) override;
  352. };
  353. VCMI_LIB_NAMESPACE_END