CTownHandler.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. 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;
  56. h & town;
  57. h & bid;
  58. h & resources;
  59. h & produce;
  60. h & name;
  61. h & description;
  62. h & requirements;
  63. h & upgrade;
  64. h & mode;
  65. }
  66. friend class CTownHandler;
  67. };
  68. /// This is structure used only by client
  69. /// Consists of all gui-related data about town structures
  70. /// Should be moved from lib to client
  71. struct DLL_LINKAGE CStructure
  72. {
  73. CBuilding * building; // base building. If null - this structure will be always present on screen
  74. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  75. int3 pos;
  76. std::string defName, borderName, areaName, identifier;
  77. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  78. template <typename Handler> void serialize(Handler &h, const int version)
  79. {
  80. h & pos;
  81. h & defName;
  82. h & borderName;
  83. h & areaName;
  84. h & identifier;
  85. h & building;
  86. h & buildable;
  87. h & hiddenUpgrade;
  88. }
  89. };
  90. struct DLL_LINKAGE SPuzzleInfo
  91. {
  92. ui16 number; //type of puzzle
  93. si16 x, y; //position
  94. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  95. std::string filename; //file with graphic of this puzzle
  96. template <typename Handler> void serialize(Handler &h, const int version)
  97. {
  98. h & number;
  99. h & x;
  100. h & y;
  101. h & whenUncovered;
  102. h & filename;
  103. }
  104. };
  105. class DLL_LINKAGE CFaction
  106. {
  107. public:
  108. CFaction();
  109. ~CFaction();
  110. std::string name; //town name, by default - from TownName.txt
  111. std::string identifier;
  112. TFaction index;
  113. ETerrainType nativeTerrain;
  114. EAlignment::EAlignment alignment;
  115. CTown * town; //NOTE: can be null
  116. std::string creatureBg120;
  117. std::string creatureBg130;
  118. std::vector<SPuzzleInfo> puzzleMap;
  119. template <typename Handler> void serialize(Handler &h, const int version)
  120. {
  121. h & name;
  122. h & identifier;
  123. h & index;
  124. h & nativeTerrain;
  125. h & alignment;
  126. h & town;
  127. h & creatureBg120;
  128. h & creatureBg130;
  129. h & puzzleMap;
  130. }
  131. };
  132. class DLL_LINKAGE CTown
  133. {
  134. public:
  135. CTown();
  136. ~CTown();
  137. // TODO: remove once save and mod compatability not needed
  138. static std::vector<BattleHex> defaultMoatHexes();
  139. std::string getFactionName() const;
  140. std::string getBuildingScope() const;
  141. std::set<si32> getAllBuildings() const;
  142. CFaction * faction;
  143. std::vector<std::string> names; //names of the town instances
  144. /// level -> list of creatures on this tier
  145. // TODO: replace with pointers to CCreature
  146. std::vector<std::vector<CreatureID> > creatures;
  147. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  148. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  149. std::vector<std::string> dwellingNames;
  150. // should be removed at least from configs in favor of auto-detection
  151. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  152. ui32 mageLevel; //max available mage guild level
  153. ui16 primaryRes;
  154. ArtifactID warMachine;
  155. si32 moatDamage;
  156. std::vector<BattleHex> moatHexes;
  157. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  158. // resulting chance = sqrt(town.chance * heroClass.chance)
  159. ui32 defaultTavernChance;
  160. // Client-only data. Should be moved away from lib
  161. struct ClientInfo
  162. {
  163. struct Point
  164. {
  165. si32 x;
  166. si32 y;
  167. template <typename Handler> void serialize(Handler &h, const int version)
  168. {
  169. h & x;
  170. h & y;
  171. }
  172. };
  173. //icons [fort is present?][build limit reached?] -> index of icon in def files
  174. int icons[2][2];
  175. std::string iconSmall[2][2]; /// icon names used during loading
  176. std::string iconLarge[2][2];
  177. std::string tavernVideo;
  178. std::string musicTheme;
  179. std::string townBackground;
  180. std::string guildBackground;
  181. std::string guildWindow;
  182. std::string buildingsIcons;
  183. std::string hallBackground;
  184. /// vector[row][column] = list of buildings in this slot
  185. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  186. /// list of town screen structures.
  187. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  188. std::vector<ConstTransitivePtr<CStructure> > structures;
  189. std::string siegePrefix;
  190. std::vector<Point> siegePositions;
  191. CreatureID siegeShooter; // shooter creature ID
  192. template <typename Handler> void serialize(Handler &h, const int version)
  193. {
  194. h & icons;
  195. h & iconSmall;
  196. h & iconLarge;
  197. h & tavernVideo;
  198. h & musicTheme;
  199. h & townBackground;
  200. h & guildBackground;
  201. h & guildWindow;
  202. h & buildingsIcons;
  203. h & hallBackground;
  204. h & hallSlots;
  205. h & structures;
  206. h & siegePrefix;
  207. h & siegePositions;
  208. h & siegeShooter;
  209. }
  210. } clientInfo;
  211. template <typename Handler> void serialize(Handler &h, const int version)
  212. {
  213. h & names;
  214. h & faction;
  215. h & creatures;
  216. h & dwellings;
  217. h & dwellingNames;
  218. h & buildings;
  219. h & hordeLvl;
  220. h & mageLevel;
  221. h & primaryRes;
  222. h & warMachine;
  223. h & clientInfo;
  224. h & moatDamage;
  225. if(version >= 758)
  226. {
  227. h & moatHexes;
  228. }
  229. else if(!h.saving)
  230. {
  231. moatHexes = defaultMoatHexes();
  232. }
  233. h & defaultTavernChance;
  234. }
  235. };
  236. class DLL_LINKAGE CTownHandler : public IHandlerBase
  237. {
  238. struct BuildingRequirementsHelper
  239. {
  240. JsonNode json;
  241. CBuilding * building;
  242. CTown * town;
  243. };
  244. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  245. void initializeRequirements();
  246. /// loads CBuilding's into town
  247. void loadBuildingRequirements(CBuilding * building, const JsonNode & source);
  248. void loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source);
  249. void loadBuildings(CTown * town, const JsonNode & source);
  250. /// loads CStructure's into town
  251. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  252. void loadStructures(CTown &town, const JsonNode & source);
  253. /// loads town hall vector (hallSlots)
  254. void loadTownHall(CTown &town, const JsonNode & source);
  255. void loadSiegeScreen(CTown &town, const JsonNode & source);
  256. void loadClientData(CTown &town, const JsonNode & source);
  257. void loadTown(CTown &town, const JsonNode & source);
  258. void loadPuzzle(CFaction & faction, const JsonNode & source);
  259. CFaction * loadFromJson(const JsonNode & data, const std::string & identifier);
  260. void loadRandomFaction();
  261. public:
  262. std::vector<ConstTransitivePtr<CFaction> > factions;
  263. CTown * randomTown;
  264. CTownHandler(); //c-tor, set pointer in VLC to this
  265. ~CTownHandler();
  266. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  267. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  268. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  269. void loadCustom() override;
  270. void afterLoadFinalization() override;
  271. std::vector<bool> getDefaultAllowed() const override;
  272. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  273. //json serialization helper
  274. static si32 decodeFaction(const std::string & identifier);
  275. //json serialization helper
  276. static std::string encodeFaction(const si32 index);
  277. template <typename Handler> void serialize(Handler &h, const int version)
  278. {
  279. h & factions;
  280. if(version >= 770)
  281. {
  282. h & randomTown;
  283. }
  284. else if(!h.saving)
  285. {
  286. loadRandomFaction();
  287. }
  288. }
  289. };