CTownHandler.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #pragma once
  2. #include "ConstTransitivePtr.h"
  3. #include "ResourceSet.h"
  4. #include "int3.h"
  5. #include "GameConstants.h"
  6. #include "IHandlerBase.h"
  7. /*
  8. * CTownHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CLegacyConfigParser;
  17. class JsonNode;
  18. class CTown;
  19. /// a typical building encountered in every castle ;]
  20. /// this is structure available to both client and server
  21. /// contains all mechanics-related data about town structures
  22. class DLL_LINKAGE CBuilding
  23. {
  24. std::string name;
  25. std::string description;
  26. public:
  27. CTown * town; // town this building belongs to
  28. BuildingID bid; //structure ID
  29. TResources resources;
  30. std::set<BuildingID> requirements; /// set of required buildings, includes upgradeOf;
  31. BuildingID upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  32. enum EBuildMode
  33. {
  34. BUILD_NORMAL, // 0 - normal, default
  35. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  36. BUILD_SPECIAL, // 2 - special - building can not be built normally
  37. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  38. } mode;
  39. const std::string &Name() const;
  40. const std::string &Description() const;
  41. //return base of upgrade(s) or this
  42. BuildingID getBase() const;
  43. // returns how many times build has to be upgraded to become build
  44. si32 getDistance(BuildingID build) const;
  45. template <typename Handler> void serialize(Handler &h, const int version)
  46. {
  47. h & town & bid & resources & name & description & requirements & upgrade & mode;
  48. }
  49. friend class CTownHandler;
  50. };
  51. /// This is structure used only by client
  52. /// Consists of all gui-related data about town structures
  53. /// Should be moved from lib to client
  54. struct DLL_LINKAGE CStructure
  55. {
  56. CBuilding * building; // base building. If null - this structure will be always present on screen
  57. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  58. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  59. int3 pos;
  60. std::string defName, borderName, areaName;
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & pos & defName & borderName & areaName & building & buildable;
  64. }
  65. };
  66. struct DLL_LINKAGE SPuzzleInfo
  67. {
  68. ui16 number; //type of puzzle
  69. si16 x, y; //position
  70. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  71. std::string filename; //file with graphic of this puzzle
  72. template <typename Handler> void serialize(Handler &h, const int version)
  73. {
  74. h & number & x & y & whenUncovered & filename;
  75. }
  76. };
  77. class DLL_LINKAGE CFaction
  78. {
  79. public:
  80. CFaction();
  81. ~CFaction();
  82. std::string name; //town name, by default - from TownName.txt
  83. TFaction index;
  84. ETerrainType nativeTerrain;
  85. EAlignment::EAlignment alignment;
  86. CreatureID commander;
  87. CTown * town; //NOTE: can be null
  88. std::string creatureBg120;
  89. std::string creatureBg130;
  90. std::vector<SPuzzleInfo> puzzleMap;
  91. template <typename Handler> void serialize(Handler &h, const int version)
  92. {
  93. h & name & index & nativeTerrain & creatureBg120 & creatureBg130 & puzzleMap;
  94. }
  95. };
  96. class DLL_LINKAGE CTown
  97. {
  98. public:
  99. CTown();
  100. ~CTown();
  101. CFaction * faction;
  102. std::vector<std::string> names; //names of the town instances
  103. /// level -> list of creatures on this tier
  104. // TODO: replace with pointers to CCreature
  105. std::vector<std::vector<CreatureID> > creatures;
  106. bmap<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  107. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  108. std::vector<std::string> dwellingNames;
  109. // should be removed at least from configs in favour of auto-detection
  110. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  111. ui32 mageLevel; //max available mage guild level
  112. ui16 primaryRes;
  113. ArtifactID warMachine;
  114. si32 moatDamage;
  115. // Client-only data. Should be moved away from lib
  116. struct ClientInfo
  117. {
  118. struct Point
  119. {
  120. si32 x;
  121. si32 y;
  122. template <typename Handler> void serialize(Handler &h, const int version)
  123. { h & x & y; }
  124. };
  125. //icons [fort is present?][build limit reached?] -> index of icon in def files
  126. int icons[2][2];
  127. std::string musicTheme;
  128. std::string townBackground;
  129. std::string guildWindow;
  130. std::string buildingsIcons;
  131. std::string hallBackground;
  132. /// vector[row][column] = list of buildings in this slot
  133. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  134. /// list of town screen structures.
  135. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  136. std::vector<ConstTransitivePtr<CStructure> > structures;
  137. std::string advMapVillage;
  138. std::string advMapCastle;
  139. std::string advMapCapitol;
  140. std::string siegePrefix;
  141. std::vector<Point> siegePositions;
  142. CreatureID siegeShooter; // shooter creature ID
  143. si32 siegeShooterCropHeight; //trim height for shooters in turrets
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & icons & musicTheme & townBackground & guildWindow & buildingsIcons & hallBackground & hallSlots & structures;
  147. h & advMapVillage & advMapCastle & advMapCapitol;
  148. h & siegePrefix & siegePositions & siegeShooter & siegeShooterCropHeight;
  149. }
  150. } clientInfo;
  151. template <typename Handler> void serialize(Handler &h, const int version)
  152. {
  153. h & names & faction & creatures & dwellings & dwellingNames & buildings & hordeLvl & mageLevel
  154. & primaryRes & warMachine & clientInfo & moatDamage;
  155. }
  156. };
  157. class DLL_LINKAGE CTownHandler : public IHandlerBase
  158. {
  159. /// loads CBuilding's into town
  160. void loadBuilding(CTown &town, const JsonNode & source);
  161. void loadBuildings(CTown &town, const JsonNode & source);
  162. /// loads CStructure's into town
  163. void loadStructure(CTown &town, const JsonNode & source);
  164. void loadStructures(CTown &town, const JsonNode & source);
  165. /// loads town hall vector (hallSlots)
  166. void loadTownHall(CTown &town, const JsonNode & source);
  167. void loadSiegeScreen(CTown &town, const JsonNode & source);
  168. void loadClientData(CTown &town, const JsonNode & source);
  169. void loadTown(CTown &town, const JsonNode & source);
  170. void loadPuzzle(CFaction & faction, const JsonNode & source);
  171. CFaction * loadFromJson(const JsonNode & data);
  172. public:
  173. std::vector<ConstTransitivePtr<CFaction> > factions;
  174. CTownHandler(); //c-tor, set pointer in VLC to this
  175. ~CTownHandler();
  176. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  177. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  178. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  179. std::vector<bool> getDefaultAllowed() const override;
  180. template <typename Handler> void serialize(Handler &h, const int version)
  181. {
  182. h & factions;
  183. }
  184. };