CTownHandler.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 CFaction
  78. {
  79. public:
  80. std::string name; //town name, by default - from TownName.txt
  81. TFaction index;
  82. ETerrainType nativeTerrain;
  83. EAlignment::EAlignment alignment;
  84. CreatureID commander;
  85. CTown * town; //NOTE: can be null
  86. std::string creatureBg120;
  87. std::string creatureBg130;
  88. std::vector<SPuzzleInfo> puzzleMap;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & name & index & nativeTerrain & creatureBg120 & creatureBg130 & puzzleMap;
  92. }
  93. };
  94. class DLL_LINKAGE CTown
  95. {
  96. public:
  97. CFaction * faction;
  98. std::vector<std::string> names; //names of the town instances
  99. /// level -> list of creatures on this tier
  100. // TODO: replace with pointers to CCreature
  101. std::vector<std::vector<CreatureID> > creatures;
  102. bmap<BuildingID, ConstTransitivePtr<CBuilding> > buildings;
  103. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  104. std::vector<std::string> dwellingNames;
  105. // should be removed at least from configs in favour of auto-detection
  106. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  107. ui32 mageLevel; //max available mage guild level
  108. ui16 primaryRes;
  109. ArtifactID warMachine;
  110. si32 moatDamage;
  111. // Client-only data. Should be moved away from lib
  112. struct ClientInfo
  113. {
  114. struct Point
  115. {
  116. si32 x;
  117. si32 y;
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. { h & x & y; }
  120. };
  121. //icons [fort is present?][build limit reached?] -> index of icon in def files
  122. int icons[2][2];
  123. std::string musicTheme;
  124. std::string townBackground;
  125. std::string guildWindow;
  126. std::string buildingsIcons;
  127. std::string hallBackground;
  128. /// vector[row][column] = list of buildings in this slot
  129. std::vector< std::vector< std::vector<BuildingID> > > hallSlots;
  130. /// list of town screen structures.
  131. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  132. std::vector<ConstTransitivePtr<CStructure> > structures;
  133. std::string advMapVillage;
  134. std::string advMapCastle;
  135. std::string advMapCapitol;
  136. std::string siegePrefix;
  137. std::vector<Point> siegePositions;
  138. CreatureID siegeShooter; // shooter creature ID
  139. si32 siegeShooterCropHeight; //trim height for shooters in turrets
  140. template <typename Handler> void serialize(Handler &h, const int version)
  141. {
  142. h & icons & musicTheme & townBackground & guildWindow & buildingsIcons & hallBackground & hallSlots & structures;
  143. h & advMapVillage & advMapCastle & advMapCapitol;
  144. h & siegePrefix & siegePositions & siegeShooter & siegeShooterCropHeight;
  145. }
  146. } clientInfo;
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. h & names & faction & creatures & dwellings & dwellingNames & buildings & hordeLvl & mageLevel
  150. & primaryRes & warMachine & clientInfo & moatDamage;
  151. }
  152. };
  153. class DLL_LINKAGE CTownHandler : public IHandlerBase
  154. {
  155. /// loads CBuilding's into town
  156. void loadBuilding(CTown &town, const JsonNode & source);
  157. void loadBuildings(CTown &town, const JsonNode & source);
  158. /// loads CStructure's into town
  159. void loadStructure(CTown &town, const JsonNode & source);
  160. void loadStructures(CTown &town, const JsonNode & source);
  161. /// loads town hall vector (hallSlots)
  162. void loadTownHall(CTown &town, const JsonNode & source);
  163. void loadSiegeScreen(CTown &town, const JsonNode & source);
  164. void loadClientData(CTown &town, const JsonNode & source);
  165. void loadTown(CTown &town, const JsonNode & source);
  166. void loadPuzzle(CFaction & faction, const JsonNode & source);
  167. CFaction * loadFromJson(const JsonNode & data);
  168. public:
  169. std::vector<ConstTransitivePtr<CFaction> > factions;
  170. CTownHandler(); //c-tor, set pointer in VLC to this
  171. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  172. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  173. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  174. std::vector<bool> getDefaultAllowed() const override;
  175. template <typename Handler> void serialize(Handler &h, const int version)
  176. {
  177. h & factions;
  178. }
  179. };