CTownHandler.h 13 KB

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