CTownHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "bonuses/Bonus.h"
  21. #include "bonuses/BonusList.h"
  22. #include "Point.h"
  23. #include "rewardable/Info.h"
  24. #include "filesystem/ResourcePath.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. class CLegacyConfigParser;
  27. class JsonNode;
  28. class CTown;
  29. class CFaction;
  30. struct BattleHex;
  31. class JsonSerializeFormat;
  32. /// a typical building encountered in every castle ;]
  33. /// this is structure available to both client and server
  34. /// contains all mechanics-related data about town structures
  35. class DLL_LINKAGE CBuilding
  36. {
  37. std::string modScope;
  38. std::string identifier;
  39. public:
  40. using TRequired = LogicalExpression<BuildingID>;
  41. CTown * town; // town this building belongs to
  42. TResources resources;
  43. TResources produce;
  44. TRequired requirements;
  45. BuildingID bid; //structure ID
  46. BuildingID upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  47. BuildingSubID::EBuildingSubID subId; /// subtype for special buildings, -1 = the building is not special
  48. std::set<BuildingID> overrideBids; /// the building which bonuses should be overridden with bonuses of the current building
  49. BonusList buildingBonuses;
  50. BonusList onVisitBonuses;
  51. Rewardable::Info rewardableObjectInfo; ///configurable rewards for special buildings
  52. enum EBuildMode
  53. {
  54. BUILD_NORMAL, // 0 - normal, default
  55. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  56. BUILD_SPECIAL, // 2 - special - building can not be built normally
  57. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  58. } mode;
  59. enum ETowerHeight // for lookup towers and some grails
  60. {
  61. HEIGHT_NO_TOWER = 5, // building has not 'lookout tower' ability
  62. HEIGHT_LOW = 10, // low lookout tower, but castle without lookout tower gives radius 5
  63. HEIGHT_AVERAGE = 15,
  64. HEIGHT_HIGH = 20, // such tower is in the Tower town
  65. HEIGHT_SKYSHIP = std::numeric_limits<int>::max() // grail, open entire map
  66. } height;
  67. static const std::map<std::string, CBuilding::EBuildMode> MODES;
  68. static const std::map<std::string, CBuilding::ETowerHeight> TOWER_TYPES;
  69. CBuilding() : town(nullptr), mode(BUILD_NORMAL) {};
  70. const BuildingTypeUniqueID getUniqueTypeID() const;
  71. std::string getJsonKey() const;
  72. std::string getNameTranslated() const;
  73. std::string getDescriptionTranslated() const;
  74. std::string getBaseTextID() const;
  75. std::string getNameTextID() const;
  76. std::string getDescriptionTextID() const;
  77. //return base of upgrade(s) or this
  78. BuildingID getBase() const;
  79. // returns how many times build has to be upgraded to become build
  80. si32 getDistance(const BuildingID & build) const;
  81. STRONG_INLINE
  82. bool IsTradeBuilding() const
  83. {
  84. return bid == BuildingID::MARKETPLACE || subId == BuildingSubID::ARTIFACT_MERCHANT || subId == BuildingSubID::FREELANCERS_GUILD;
  85. }
  86. STRONG_INLINE
  87. bool IsWeekBonus() const
  88. {
  89. return subId == BuildingSubID::STABLES || subId == BuildingSubID::MANA_VORTEX;
  90. }
  91. STRONG_INLINE
  92. bool IsVisitingBonus() const
  93. {
  94. return subId == BuildingSubID::ATTACK_VISITING_BONUS ||
  95. subId == BuildingSubID::DEFENSE_VISITING_BONUS ||
  96. subId == BuildingSubID::SPELL_POWER_VISITING_BONUS ||
  97. subId == BuildingSubID::KNOWLEDGE_VISITING_BONUS ||
  98. subId == BuildingSubID::EXPERIENCE_VISITING_BONUS ||
  99. subId == BuildingSubID::CUSTOM_VISITING_BONUS;
  100. }
  101. void addNewBonus(const std::shared_ptr<Bonus> & b, BonusList & bonusList) const;
  102. friend class CTownHandler;
  103. };
  104. /// This is structure used only by client
  105. /// Consists of all gui-related data about town structures
  106. /// Should be moved from lib to client
  107. struct DLL_LINKAGE CStructure
  108. {
  109. CBuilding * building; // base building. If null - this structure will be always present on screen
  110. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  111. int3 pos;
  112. AnimationPath defName;
  113. ImagePath borderName;
  114. ImagePath areaName;
  115. std::string identifier;
  116. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  117. };
  118. struct DLL_LINKAGE SPuzzleInfo
  119. {
  120. ui16 number; //type of puzzle
  121. si16 x, y; //position
  122. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  123. ImagePath filename; //file with graphic of this puzzle
  124. };
  125. class DLL_LINKAGE CFaction : public Faction
  126. {
  127. friend class CTownHandler;
  128. friend class CBuilding;
  129. friend class CTown;
  130. std::string modScope;
  131. std::string identifier;
  132. FactionID index = FactionID::NEUTRAL;
  133. FactionID getFaction() const override; //This function should not be used
  134. public:
  135. TerrainId nativeTerrain;
  136. EAlignment alignment = EAlignment::NEUTRAL;
  137. bool preferUndergroundPlacement = false;
  138. /// Boat that will be used by town shipyard (if any)
  139. /// and for placing heroes directly on boat (in map editor, water prisons & taverns)
  140. BoatId boatType = BoatId::CASTLE;
  141. CTown * town = nullptr; //NOTE: can be null
  142. ImagePath creatureBg120;
  143. ImagePath creatureBg130;
  144. std::vector<SPuzzleInfo> puzzleMap;
  145. CFaction() = default;
  146. ~CFaction();
  147. int32_t getIndex() const override;
  148. int32_t getIconIndex() const override;
  149. std::string getJsonKey() const override;
  150. void registerIcons(const IconRegistar & cb) const override;
  151. FactionID getId() const override;
  152. std::string getNameTranslated() const override;
  153. std::string getNameTextID() const override;
  154. bool hasTown() const override;
  155. TerrainId getNativeTerrain() const override;
  156. EAlignment getAlignment() const override;
  157. BoatId getBoatType() const override;
  158. void updateFrom(const JsonNode & data);
  159. void serializeJson(JsonSerializeFormat & handler);
  160. };
  161. class DLL_LINKAGE CTown
  162. {
  163. friend class CTownHandler;
  164. size_t namesCount = 0;
  165. public:
  166. CTown();
  167. ~CTown();
  168. std::string getBuildingScope() const;
  169. std::set<si32> getAllBuildings() const;
  170. const CBuilding * getSpecialBuilding(BuildingSubID::EBuildingSubID subID) const;
  171. std::string getGreeting(BuildingSubID::EBuildingSubID subID) const;
  172. void setGreeting(BuildingSubID::EBuildingSubID subID, const std::string & message) const; //may affect only mutable field
  173. BuildingID getBuildingType(BuildingSubID::EBuildingSubID subID) const;
  174. std::string getRandomNameTextID(size_t index) const;
  175. size_t getRandomNamesCount() const;
  176. CFaction * faction;
  177. /// level -> list of creatures on this tier
  178. // TODO: replace with pointers to CCreature
  179. std::vector<std::vector<CreatureID> > creatures;
  180. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  181. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  182. std::vector<std::string> dwellingNames;
  183. // should be removed at least from configs in favor of auto-detection
  184. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  185. ui32 mageLevel; //max available mage guild level
  186. GameResID primaryRes;
  187. ArtifactID warMachine;
  188. SpellID moatAbility;
  189. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  190. // resulting chance = sqrt(town.chance * heroClass.chance)
  191. ui32 defaultTavernChance;
  192. // Client-only data. Should be moved away from lib
  193. struct ClientInfo
  194. {
  195. //icons [fort is present?][build limit reached?] -> index of icon in def files
  196. int icons[2][2];
  197. std::string iconSmall[2][2]; /// icon names used during loading
  198. std::string iconLarge[2][2];
  199. VideoPath tavernVideo;
  200. AudioPath musicTheme;
  201. ImagePath townBackground;
  202. ImagePath guildBackground;
  203. ImagePath guildWindow;
  204. AnimationPath buildingsIcons;
  205. ImagePath hallBackground;
  206. /// vector[row][column] = list of buildings in this slot
  207. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  208. /// list of town screen structures.
  209. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  210. std::vector<ConstTransitivePtr<CStructure> > structures;
  211. std::string siegePrefix;
  212. std::vector<Point> siegePositions;
  213. CreatureID siegeShooter; // shooter creature ID
  214. std::string towerIconSmall;
  215. std::string towerIconLarge;
  216. } clientInfo;
  217. private:
  218. ///generated bonusing buildings messages for all towns of this type.
  219. mutable std::map<BuildingSubID::EBuildingSubID, const std::string> specialMessages; //may be changed by CGTownBuilding::getVisitingBonusGreeting() const
  220. };
  221. class DLL_LINKAGE CTownHandler : public CHandlerBase<FactionID, Faction, CFaction, FactionService>
  222. {
  223. struct BuildingRequirementsHelper
  224. {
  225. JsonNode json;
  226. CBuilding * building;
  227. CTown * town;
  228. };
  229. std::map<CTown *, JsonNode> warMachinesToLoad;
  230. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  231. std::vector<BuildingRequirementsHelper> overriddenBidsToLoad; //list of buildings, which bonuses should be overridden.
  232. static const TPropagatorPtr & emptyPropagator();
  233. void initializeRequirements();
  234. void initializeOverridden();
  235. void initializeWarMachines();
  236. /// loads CBuilding's into town
  237. void loadBuildingRequirements(CBuilding * building, const JsonNode & source, std::vector<BuildingRequirementsHelper> & bidsToLoad) const;
  238. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  239. void loadBuildings(CTown * town, const JsonNode & source);
  240. std::shared_ptr<Bonus> createBonus(CBuilding * build, BonusType type, int val) const;
  241. std::shared_ptr<Bonus> createBonus(CBuilding * build, BonusType type, int val, BonusSubtypeID subtype) const;
  242. std::shared_ptr<Bonus> createBonus(CBuilding * build, BonusType type, int val, BonusSubtypeID subtype, const TPropagatorPtr & prop) const;
  243. /// loads CStructure's into town
  244. void loadStructure(CTown & town, const std::string & stringID, const JsonNode & source) const;
  245. void loadStructures(CTown & town, const JsonNode & source) const;
  246. /// loads town hall vector (hallSlots)
  247. void loadTownHall(CTown & town, const JsonNode & source) const;
  248. void loadSiegeScreen(CTown & town, const JsonNode & source) const;
  249. void loadClientData(CTown & town, const JsonNode & source) const;
  250. void loadTown(CTown * town, const JsonNode & source);
  251. void loadPuzzle(CFaction & faction, const JsonNode & source) const;
  252. void loadRandomFaction();
  253. public:
  254. template<typename R, typename K>
  255. static R getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required = true);
  256. template<typename R>
  257. static R getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required = true);
  258. CTown * randomTown;
  259. CFaction * randomFaction;
  260. CTownHandler();
  261. ~CTownHandler();
  262. std::vector<JsonNode> loadLegacyData() override;
  263. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  264. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  265. void addBonusesForVanilaBuilding(CBuilding * building) const;
  266. void loadCustom() override;
  267. void afterLoadFinalization() override;
  268. std::set<FactionID> getDefaultAllowed() const;
  269. std::set<FactionID> getAllowedFactions(bool withTown = true) const;
  270. static void loadSpecialBuildingBonuses(const JsonNode & source, BonusList & bonusList, CBuilding * building);
  271. protected:
  272. const std::vector<std::string> & getTypeNames() const override;
  273. CFaction * loadFromJson(const std::string & scope, const JsonNode & data, const std::string & identifier, size_t index) override;
  274. };
  275. VCMI_LIB_NAMESPACE_END