CTownHandler.h 8.6 KB

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