CTownHandler.h 9.4 KB

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