CTownHandler.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "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. CFaction * faction;
  112. std::vector<std::string> names; //names of the town instances
  113. /// level -> list of creatures on this tier
  114. // TODO: replace with pointers to CCreature
  115. std::vector<std::vector<CreatureID> > creatures;
  116. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  117. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  118. std::vector<std::string> dwellingNames;
  119. // should be removed at least from configs in favor of auto-detection
  120. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  121. ui32 mageLevel; //max available mage guild level
  122. ui16 primaryRes;
  123. ArtifactID warMachine;
  124. si32 moatDamage;
  125. std::vector<BattleHex> moatHexes;
  126. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  127. // resulting chance = sqrt(town.chance * heroClass.chance)
  128. ui32 defaultTavernChance;
  129. // Client-only data. Should be moved away from lib
  130. struct ClientInfo
  131. {
  132. struct Point
  133. {
  134. si32 x;
  135. si32 y;
  136. template <typename Handler> void serialize(Handler &h, const int version)
  137. { h & x & y; }
  138. };
  139. //icons [fort is present?][build limit reached?] -> index of icon in def files
  140. int icons[2][2];
  141. std::string iconSmall[2][2]; /// icon names used during loading
  142. std::string iconLarge[2][2];
  143. std::string tavernVideo;
  144. std::string musicTheme;
  145. std::string townBackground;
  146. std::string guildBackground;
  147. std::string guildWindow;
  148. std::string buildingsIcons;
  149. std::string hallBackground;
  150. /// vector[row][column] = list of buildings in this slot
  151. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  152. /// list of town screen structures.
  153. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  154. std::vector<ConstTransitivePtr<CStructure> > structures;
  155. std::string siegePrefix;
  156. std::vector<Point> siegePositions;
  157. CreatureID siegeShooter; // shooter creature ID
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & icons & iconSmall & iconLarge & tavernVideo & musicTheme & townBackground & guildBackground & guildWindow & buildingsIcons & hallBackground;
  161. h & hallSlots & structures;
  162. h & siegePrefix & siegePositions & siegeShooter;
  163. }
  164. } clientInfo;
  165. template <typename Handler> void serialize(Handler &h, const int version)
  166. {
  167. h & names & faction & creatures & dwellings & dwellingNames & buildings & hordeLvl & mageLevel
  168. & primaryRes & warMachine & clientInfo & moatDamage;
  169. if(version >= 758)
  170. {
  171. h & moatHexes;
  172. }
  173. else if(!h.saving)
  174. {
  175. moatHexes = defaultMoatHexes();
  176. }
  177. h & defaultTavernChance;
  178. auto findNull = [](const std::pair<BuildingID, ConstTransitivePtr<CBuilding>> &building)
  179. { return building.second == nullptr; };
  180. //Fix #1444 corrupted save
  181. while(auto badElem = vstd::tryFindIf(buildings, findNull))
  182. {
  183. logGlobal->errorStream() << "#1444-like bug encountered in CTown::serialize, fixing buildings list by removing bogus entry " << badElem->first << " from " << faction->name;
  184. buildings.erase(badElem->first);
  185. }
  186. }
  187. };
  188. class DLL_LINKAGE CTownHandler : public IHandlerBase
  189. {
  190. struct BuildingRequirementsHelper
  191. {
  192. JsonNode json;
  193. CBuilding * building;
  194. CFaction * faction;
  195. };
  196. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  197. void initializeRequirements();
  198. /// loads CBuilding's into town
  199. void loadBuildingRequirements(CTown &town, CBuilding & building, const JsonNode & source);
  200. void loadBuilding(CTown &town, const std::string & stringID, const JsonNode & source);
  201. void loadBuildings(CTown &town, const JsonNode & source);
  202. /// loads CStructure's into town
  203. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  204. void loadStructures(CTown &town, const JsonNode & source);
  205. /// loads town hall vector (hallSlots)
  206. void loadTownHall(CTown &town, const JsonNode & source);
  207. void loadSiegeScreen(CTown &town, const JsonNode & source);
  208. void loadClientData(CTown &town, const JsonNode & source);
  209. void loadTown(CTown &town, const JsonNode & source);
  210. void loadPuzzle(CFaction & faction, const JsonNode & source);
  211. CFaction * loadFromJson(const JsonNode & data, const std::string & identifier);
  212. public:
  213. std::vector<ConstTransitivePtr<CFaction> > factions;
  214. CTownHandler(); //c-tor, set pointer in VLC to this
  215. ~CTownHandler();
  216. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  217. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  218. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  219. void afterLoadFinalization() override;
  220. std::vector<bool> getDefaultAllowed() const override;
  221. std::set<TFaction> getAllowedFactions(bool withTown = true) const;
  222. //json serialization helper
  223. static si32 decodeFaction(const std::string & identifier);
  224. //json serialization helper
  225. static std::string encodeFaction(const si32 index);
  226. template <typename Handler> void serialize(Handler &h, const int version)
  227. {
  228. h & factions;
  229. }
  230. };