CTownHandler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. bool preferUndergroundPlacement;
  175. CTown * town; //NOTE: can be null
  176. std::string creatureBg120;
  177. std::string creatureBg130;
  178. std::vector<SPuzzleInfo> puzzleMap;
  179. CFaction();
  180. ~CFaction();
  181. int32_t getIndex() const override;
  182. int32_t getIconIndex() const override;
  183. const std::string & getName() const override;
  184. const std::string & getJsonKey() const override;
  185. void registerIcons(const IconRegistar & cb) const override;
  186. FactionID getId() const override;
  187. bool hasTown() const override;
  188. void updateFrom(const JsonNode & data);
  189. void serializeJson(JsonSerializeFormat & handler);
  190. template <typename Handler> void serialize(Handler &h, const int version)
  191. {
  192. h & name;
  193. h & identifier;
  194. h & index;
  195. h & nativeTerrain;
  196. h & alignment;
  197. h & town;
  198. h & creatureBg120;
  199. h & creatureBg130;
  200. h & puzzleMap;
  201. }
  202. };
  203. class DLL_LINKAGE CTown
  204. {
  205. public:
  206. CTown();
  207. ~CTown();
  208. // TODO: remove once save and mod compatability not needed
  209. static std::vector<BattleHex> defaultMoatHexes();
  210. std::string getLocalizedFactionName() const;
  211. std::string getBuildingScope() const;
  212. std::set<si32> getAllBuildings() const;
  213. const CBuilding * getSpecialBuilding(BuildingSubID::EBuildingSubID subID) const;
  214. const std::string getGreeting(BuildingSubID::EBuildingSubID subID) const;
  215. void setGreeting(BuildingSubID::EBuildingSubID subID, const std::string message) const; //may affect only mutable field
  216. BuildingID::EBuildingID getBuildingType(BuildingSubID::EBuildingSubID subID) const;
  217. CFaction * faction;
  218. std::vector<std::string> names; //names of the town instances
  219. /// level -> list of creatures on this tier
  220. // TODO: replace with pointers to CCreature
  221. std::vector<std::vector<CreatureID> > creatures;
  222. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  223. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  224. std::vector<std::string> dwellingNames;
  225. // should be removed at least from configs in favor of auto-detection
  226. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  227. ui32 mageLevel; //max available mage guild level
  228. ui16 primaryRes;
  229. ArtifactID warMachine;
  230. si32 moatDamage;
  231. std::vector<BattleHex> moatHexes;
  232. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  233. // resulting chance = sqrt(town.chance * heroClass.chance)
  234. ui32 defaultTavernChance;
  235. // Client-only data. Should be moved away from lib
  236. struct ClientInfo
  237. {
  238. struct Point
  239. {
  240. si32 x;
  241. si32 y;
  242. template <typename Handler> void serialize(Handler &h, const int version)
  243. {
  244. h & x;
  245. h & y;
  246. }
  247. };
  248. //icons [fort is present?][build limit reached?] -> index of icon in def files
  249. int icons[2][2];
  250. std::string iconSmall[2][2]; /// icon names used during loading
  251. std::string iconLarge[2][2];
  252. std::string tavernVideo;
  253. std::string musicTheme;
  254. std::string townBackground;
  255. std::string guildBackground;
  256. std::string guildWindow;
  257. std::string buildingsIcons;
  258. std::string hallBackground;
  259. /// vector[row][column] = list of buildings in this slot
  260. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  261. /// list of town screen structures.
  262. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  263. std::vector<ConstTransitivePtr<CStructure> > structures;
  264. std::string siegePrefix;
  265. std::vector<Point> siegePositions;
  266. CreatureID siegeShooter; // shooter creature ID
  267. template <typename Handler> void serialize(Handler &h, const int version)
  268. {
  269. h & icons;
  270. h & iconSmall;
  271. h & iconLarge;
  272. h & tavernVideo;
  273. h & musicTheme;
  274. h & townBackground;
  275. h & guildBackground;
  276. h & guildWindow;
  277. h & buildingsIcons;
  278. h & hallBackground;
  279. h & hallSlots;
  280. h & structures;
  281. h & siegePrefix;
  282. h & siegePositions;
  283. h & siegeShooter;
  284. }
  285. } clientInfo;
  286. template <typename Handler> void serialize(Handler &h, const int version)
  287. {
  288. h & names;
  289. h & faction;
  290. h & creatures;
  291. h & dwellings;
  292. h & dwellingNames;
  293. h & buildings;
  294. h & hordeLvl;
  295. h & mageLevel;
  296. h & primaryRes;
  297. h & warMachine;
  298. h & clientInfo;
  299. h & moatDamage;
  300. if(version >= 758)
  301. {
  302. h & moatHexes;
  303. }
  304. else if(!h.saving)
  305. {
  306. moatHexes = defaultMoatHexes();
  307. }
  308. h & defaultTavernChance;
  309. }
  310. private:
  311. ///generated bonusing buildings messages for all towns of this type.
  312. mutable std::map<BuildingSubID::EBuildingSubID, const std::string> specialMessages; //may be changed by CGTownBuilding::getVisitingBonusGreeting() const
  313. };
  314. class DLL_LINKAGE CTownHandler : public CHandlerBase<FactionID, Faction, CFaction, FactionService>
  315. {
  316. struct BuildingRequirementsHelper
  317. {
  318. JsonNode json;
  319. CBuilding * building;
  320. CTown * town;
  321. };
  322. std::map<CTown *, JsonNode> warMachinesToLoad;
  323. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  324. std::vector<BuildingRequirementsHelper> overriddenBidsToLoad; //list of buildings, which bonuses should be overridden.
  325. const static ETerrainType::EETerrainType defaultGoodTerrain = ETerrainType::EETerrainType::GRASS;
  326. const static ETerrainType::EETerrainType defaultEvilTerrain = ETerrainType::EETerrainType::LAVA;
  327. const static ETerrainType::EETerrainType defaultNeutralTerrain = ETerrainType::EETerrainType::ROUGH;
  328. static TPropagatorPtr & emptyPropagator();
  329. void initializeRequirements();
  330. void initializeOverridden();
  331. void initializeWarMachines();
  332. /// loads CBuilding's into town
  333. void loadBuildingRequirements(CBuilding * building, const JsonNode & source, std::vector<BuildingRequirementsHelper> & bidsToLoad);
  334. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  335. void loadBuildings(CTown * town, const JsonNode & source);
  336. std::shared_ptr<Bonus> createBonus(CBuilding * build, Bonus::BonusType type, int val, int subtype = -1);
  337. std::shared_ptr<Bonus> createBonus(CBuilding * build, Bonus::BonusType type, int val, TPropagatorPtr & prop, int subtype = -1);
  338. std::shared_ptr<Bonus> createBonusImpl(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr & prop, const std::string & description, int subtype = -1);
  339. /// loads CStructure's into town
  340. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  341. void loadStructures(CTown &town, const JsonNode & source);
  342. /// loads town hall vector (hallSlots)
  343. void loadTownHall(CTown &town, const JsonNode & source);
  344. void loadSiegeScreen(CTown &town, const JsonNode & source);
  345. void loadClientData(CTown &town, const JsonNode & source);
  346. void loadTown(CTown * town, const JsonNode & source);
  347. void loadPuzzle(CFaction & faction, const JsonNode & source);
  348. ETerrainType::EETerrainType getDefaultTerrainForAlignment(EAlignment::EAlignment aligment) const;
  349. void loadRandomFaction();
  350. public:
  351. template<typename R, typename K>
  352. static R getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required = true);
  353. template<typename R>
  354. static R getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required = true);
  355. CTown * randomTown;
  356. CTownHandler();
  357. ~CTownHandler();
  358. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  359. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  360. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  361. void addBonusesForVanilaBuilding(CBuilding * building);
  362. void loadCustom() override;
  363. void afterLoadFinalization() override;
  364. std::vector<bool> getDefaultAllowed() const override;
  365. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  366. static void loadSpecialBuildingBonuses(const JsonNode & source, BonusList & bonusList, CBuilding * building);
  367. template <typename Handler> void serialize(Handler &h, const int version)
  368. {
  369. h & objects;
  370. if(version >= 770)
  371. {
  372. h & randomTown;
  373. }
  374. else if(!h.saving)
  375. {
  376. loadRandomFaction();
  377. }
  378. }
  379. protected:
  380. const std::vector<std::string> & getTypeNames() const override;
  381. CFaction * loadFromJson(const std::string & scope, const JsonNode & data, const std::string & identifier, size_t index) override;
  382. };