CTownHandler.h 14 KB

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