CTownHandler.h 9.0 KB

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