CTownHandler.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /// a typical building encountered in every castle ;]
  22. /// this is structure available to both client and server
  23. /// contains all mechanics-related data about town structures
  24. class DLL_LINKAGE CBuilding
  25. {
  26. std::string name;
  27. std::string description;
  28. public:
  29. typedef LogicalExpression<BuildingID> TRequired;
  30. CTown * town; // town this building belongs to
  31. TResources resources;
  32. TResources produce;
  33. TRequired requirements;
  34. std::string identifier;
  35. BuildingID bid; //structure ID
  36. BuildingID upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  37. enum EBuildMode
  38. {
  39. BUILD_NORMAL, // 0 - normal, default
  40. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  41. BUILD_SPECIAL, // 2 - special - building can not be built normally
  42. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  43. } mode;
  44. const std::string &Name() const;
  45. const std::string &Description() const;
  46. //return base of upgrade(s) or this
  47. BuildingID getBase() const;
  48. // returns how many times build has to be upgraded to become build
  49. si32 getDistance(BuildingID build) const;
  50. template <typename Handler> void serialize(Handler &h, const int version)
  51. {
  52. h & identifier & town & bid & resources & produce & name & description & requirements & upgrade & mode;
  53. }
  54. friend class CTownHandler;
  55. };
  56. typedef std::pair<BuildingID, ConstTransitivePtr<CBuilding>> TPairCBuilding;
  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. CFaction * faction;
  108. std::vector<std::string> names; //names of the town instances
  109. /// level -> list of creatures on this tier
  110. // TODO: replace with pointers to CCreature
  111. std::vector<std::vector<CreatureID> > creatures;
  112. std::map<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  113. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  114. std::vector<std::string> dwellingNames;
  115. // should be removed at least from configs in favor of auto-detection
  116. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  117. ui32 mageLevel; //max available mage guild level
  118. ui16 primaryRes;
  119. ArtifactID warMachine;
  120. si32 moatDamage;
  121. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  122. // resulting chance = sqrt(town.chance * heroClass.chance)
  123. ui32 defaultTavernChance;
  124. // Client-only data. Should be moved away from lib
  125. struct ClientInfo
  126. {
  127. struct Point
  128. {
  129. si32 x;
  130. si32 y;
  131. template <typename Handler> void serialize(Handler &h, const int version)
  132. { h & x & y; }
  133. };
  134. //icons [fort is present?][build limit reached?] -> index of icon in def files
  135. int icons[2][2];
  136. std::string iconSmall[2][2]; /// icon names used during loading
  137. std::string iconLarge[2][2];
  138. std::string tavernVideo;
  139. std::string musicTheme;
  140. std::string townBackground;
  141. std::string guildBackground;
  142. std::string guildWindow;
  143. std::string buildingsIcons;
  144. std::string hallBackground;
  145. /// vector[row][column] = list of buildings in this slot
  146. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  147. /// list of town screen structures.
  148. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  149. std::vector<ConstTransitivePtr<CStructure> > structures;
  150. std::string advMapVillage;
  151. std::string advMapCastle;
  152. std::string advMapCapitol;
  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 & advMapVillage & advMapCastle & advMapCapitol & 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 & defaultTavernChance;
  167. auto findNull = [](const std::pair<BuildingID, ConstTransitivePtr<CBuilding>> &building)
  168. { return building.second == nullptr; };
  169. //Fix #1444 corrupted save
  170. while(auto badElem = vstd::tryFindIf(buildings, findNull))
  171. {
  172. logGlobal->errorStream() << "#1444-like bug encountered in CTown::serialize, fixing buildings list by removing bogus entry " << badElem->first << " from " << faction->name;
  173. buildings.erase(badElem->first);
  174. }
  175. }
  176. };
  177. class DLL_LINKAGE CTownHandler : public IHandlerBase
  178. {
  179. struct BuildingRequirementsHelper
  180. {
  181. JsonNode json;
  182. CBuilding * building;
  183. CFaction * faction;
  184. };
  185. std::vector<BuildingRequirementsHelper> requirementsToLoad;
  186. void initializeRequirements();
  187. /// loads CBuilding's into town
  188. void loadBuildingRequirements(CTown &town, CBuilding & building, const JsonNode & source);
  189. void loadBuilding(CTown &town, const std::string & stringID, const JsonNode & source);
  190. void loadBuildings(CTown &town, const JsonNode & source);
  191. /// loads CStructure's into town
  192. void loadStructure(CTown &town, const std::string & stringID, const JsonNode & source);
  193. void loadStructures(CTown &town, const JsonNode & source);
  194. /// loads town hall vector (hallSlots)
  195. void loadTownHall(CTown &town, const JsonNode & source);
  196. void loadSiegeScreen(CTown &town, const JsonNode & source);
  197. void loadClientData(CTown &town, const JsonNode & source);
  198. void loadTown(CTown &town, const JsonNode & source);
  199. void loadPuzzle(CFaction & faction, const JsonNode & source);
  200. CFaction * loadFromJson(const JsonNode & data, std::string identifier);
  201. public:
  202. std::vector<ConstTransitivePtr<CFaction> > factions;
  203. CTownHandler(); //c-tor, set pointer in VLC to this
  204. ~CTownHandler();
  205. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  206. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  207. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  208. void afterLoadFinalization() override;
  209. std::vector<bool> getDefaultAllowed() const override;
  210. template <typename Handler> void serialize(Handler &h, const int version)
  211. {
  212. h & factions;
  213. }
  214. };