CTownHandler.h 8.8 KB

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