CTownHandler.h 14 KB

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