CTownHandler.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #include "ConstTransitivePtr.h"
  3. #include "ResourceSet.h"
  4. #include "int3.h"
  5. #include "GameConstants.h"
  6. #include "IHandlerBase.h"
  7. #include "LogicalExpression.h"
  8. #include "battle/BattleHex.h"
  9. /*
  10. * CTownHandler.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. class CLegacyConfigParser;
  19. class JsonNode;
  20. class CTown;
  21. class CFaction;
  22. struct BattleHex;
  23. /// a typical building encountered in every castle ;]
  24. /// this is structure available to both client and server
  25. /// contains all mechanics-related data about town structures
  26. class DLL_LINKAGE CBuilding
  27. {
  28. std::string name;
  29. std::string description;
  30. public:
  31. typedef LogicalExpression<BuildingID> TRequired;
  32. CTown * town; // town this building belongs to
  33. TResources resources;
  34. TResources produce;
  35. TRequired requirements;
  36. std::string identifier;
  37. BuildingID bid; //structure ID
  38. BuildingID upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  39. enum EBuildMode
  40. {
  41. BUILD_NORMAL, // 0 - normal, default
  42. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  43. BUILD_SPECIAL, // 2 - special - building can not be built normally
  44. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  45. } mode;
  46. CBuilding();
  47. const std::string &Name() const;
  48. const std::string &Description() const;
  49. //return base of upgrade(s) or this
  50. BuildingID getBase() const;
  51. // returns how many times build has to be upgraded to become build
  52. si32 getDistance(BuildingID build) const;
  53. template <typename Handler> void serialize(Handler &h, const int version)
  54. {
  55. h & identifier & town & bid & resources & produce & name & description & requirements & upgrade & mode;
  56. }
  57. friend class CTownHandler;
  58. };
  59. /// This is structure used only by client
  60. /// Consists of all gui-related data about town structures
  61. /// Should be moved from lib to client
  62. struct DLL_LINKAGE CStructure
  63. {
  64. CBuilding * building; // base building. If null - this structure will be always present on screen
  65. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  66. int3 pos;
  67. std::string defName, borderName, areaName, identifier;
  68. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & pos & defName & borderName & areaName & identifier & building & buildable & hiddenUpgrade;
  72. }
  73. };
  74. struct DLL_LINKAGE SPuzzleInfo
  75. {
  76. ui16 number; //type of puzzle
  77. si16 x, y; //position
  78. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  79. std::string filename; //file with graphic of this puzzle
  80. template <typename Handler> void serialize(Handler &h, const int version)
  81. {
  82. h & number & x & y & whenUncovered & filename;
  83. }
  84. };
  85. class DLL_LINKAGE CFaction
  86. {
  87. public:
  88. CFaction();
  89. ~CFaction();
  90. std::string name; //town name, by default - from TownName.txt
  91. std::string identifier;
  92. TFaction index;
  93. ETerrainType nativeTerrain;
  94. EAlignment::EAlignment alignment;
  95. CTown * town; //NOTE: can be null
  96. std::string creatureBg120;
  97. std::string creatureBg130;
  98. std::vector<SPuzzleInfo> puzzleMap;
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & name & identifier & index & nativeTerrain & alignment & town & creatureBg120 & creatureBg130 & puzzleMap;
  102. }
  103. };
  104. class DLL_LINKAGE CTown
  105. {
  106. public:
  107. CTown();
  108. ~CTown();
  109. // TODO: remove once save and mod compatability not needed
  110. static std::vector<BattleHex> defaultMoatHexes();
  111. std::string getFactionName() const;
  112. std::string getBuildingScope() const;
  113. std::set<si32> getAllBuildings() const;
  114. CFaction * faction;
  115. std::vector<std::string> names; //names of the town instances
  116. /// level -> list of creatures on this tier
  117. // TODO: replace with pointers to CCreature
  118. std::vector<std::vector<CreatureID> > creatures;
  119. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  120. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  121. std::vector<std::string> dwellingNames;
  122. // should be removed at least from configs in favor of auto-detection
  123. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  124. ui32 mageLevel; //max available mage guild level
  125. ui16 primaryRes;
  126. ArtifactID warMachine;
  127. si32 moatDamage;
  128. std::vector<BattleHex> moatHexes;
  129. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  130. // resulting chance = sqrt(town.chance * heroClass.chance)
  131. ui32 defaultTavernChance;
  132. // Client-only data. Should be moved away from lib
  133. struct ClientInfo
  134. {
  135. struct Point
  136. {
  137. si32 x;
  138. si32 y;
  139. template <typename Handler> void serialize(Handler &h, const int version)
  140. { h & x & y; }
  141. };
  142. //icons [fort is present?][build limit reached?] -> index of icon in def files
  143. int icons[2][2];
  144. std::string iconSmall[2][2]; /// icon names used during loading
  145. std::string iconLarge[2][2];
  146. std::string tavernVideo;
  147. std::string musicTheme;
  148. std::string townBackground;
  149. std::string guildBackground;
  150. std::string guildWindow;
  151. std::string buildingsIcons;
  152. std::string hallBackground;
  153. /// vector[row][column] = list of buildings in this slot
  154. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  155. /// list of town screen structures.
  156. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  157. std::vector<ConstTransitivePtr<CStructure> > structures;
  158. std::string siegePrefix;
  159. std::vector<Point> siegePositions;
  160. CreatureID siegeShooter; // shooter creature ID
  161. template <typename Handler> void serialize(Handler &h, const int version)
  162. {
  163. h & icons & iconSmall & iconLarge & tavernVideo & musicTheme & townBackground & guildBackground & guildWindow & buildingsIcons & hallBackground;
  164. h & hallSlots & structures;
  165. h & siegePrefix & siegePositions & siegeShooter;
  166. }
  167. } clientInfo;
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. h & names & faction & creatures & dwellings & dwellingNames & buildings & hordeLvl & mageLevel
  171. & primaryRes & warMachine & clientInfo & moatDamage;
  172. if(version >= 758)
  173. {
  174. h & moatHexes;
  175. }
  176. else if(!h.saving)
  177. {
  178. moatHexes = defaultMoatHexes();
  179. }
  180. h & defaultTavernChance;
  181. }
  182. };
  183. class DLL_LINKAGE CTownHandler : public IHandlerBase
  184. {
  185. struct BuildingRequirementsHelper
  186. {
  187. JsonNode json;
  188. CBuilding * building;
  189. CTown * town;
  190. };
  191. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  192. void initializeRequirements();
  193. /// loads CBuilding's into town
  194. void loadBuildingRequirements(CBuilding * building, const JsonNode & source);
  195. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  196. void loadBuildings(CTown * town, const JsonNode & source);
  197. /// loads CStructure's into town
  198. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  199. void loadStructures(CTown &town, const JsonNode & source);
  200. /// loads town hall vector (hallSlots)
  201. void loadTownHall(CTown &town, const JsonNode & source);
  202. void loadSiegeScreen(CTown &town, const JsonNode & source);
  203. void loadClientData(CTown &town, const JsonNode & source);
  204. void loadTown(CTown &town, const JsonNode & source);
  205. void loadPuzzle(CFaction & faction, const JsonNode & source);
  206. CFaction * loadFromJson(const JsonNode & data, const std::string & identifier);
  207. void loadRandomFaction();
  208. public:
  209. std::vector<ConstTransitivePtr<CFaction> > factions;
  210. CTown * randomTown;
  211. CTownHandler(); //c-tor, set pointer in VLC to this
  212. ~CTownHandler();
  213. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  214. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  215. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  216. void loadCustom() override;
  217. void afterLoadFinalization() override;
  218. std::vector<bool> getDefaultAllowed() const override;
  219. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  220. //json serialization helper
  221. static si32 decodeFaction(const std::string & identifier);
  222. //json serialization helper
  223. static std::string encodeFaction(const si32 index);
  224. template <typename Handler> void serialize(Handler &h, const int version)
  225. {
  226. h & factions;
  227. if(version >= 770)
  228. {
  229. h & randomTown;
  230. }
  231. else if(!h.saving)
  232. {
  233. loadRandomFaction();
  234. }
  235. }
  236. };