CTownHandler.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #pragma once
  2. #include "ConstTransitivePtr.h"
  3. #include "ResourceSet.h"
  4. #include "int3.h"
  5. #include "GameConstants.h"
  6. /*
  7. * CTownHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CLegacyConfigParser;
  16. class JsonNode;
  17. /// a typical building encountered in every castle ;]
  18. /// this is structure available to both client and server
  19. /// contains all mechanics-related data about town structures
  20. class DLL_LINKAGE CBuilding
  21. {
  22. typedef si32 BuildingType;//TODO: replace int with pointer?
  23. std::string name;
  24. std::string description;
  25. public:
  26. TFaction tid;
  27. si32 bid; //town ID and structure ID
  28. TResources resources;
  29. std::set<BuildingType> requirements; /// set of required buildings, includes upgradeOf;
  30. BuildingType upgrade; /// indicates that building "upgrade" can be improved by this, -1 = empty
  31. enum EBuildMode
  32. {
  33. BUILD_NORMAL, // 0 - normal, default
  34. BUILD_AUTO, // 1 - auto - building appears when all requirements are built
  35. BUILD_SPECIAL, // 2 - special - building can not be built normally
  36. BUILD_GRAIL // 3 - grail - building reqires grail to be built
  37. };
  38. ui32 mode;
  39. const std::string &Name() const;
  40. const std::string &Description() const;
  41. //return base of upgrade(s) or this
  42. BuildingType getBase() const;
  43. // returns how many times build has to be upgraded to become build
  44. si32 getDistance(BuildingType build) const;
  45. template <typename Handler> void serialize(Handler &h, const int version)
  46. {
  47. h & tid & 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. class DLL_LINKAGE CTown
  67. {
  68. public:
  69. TFaction typeID;//same as CFaction::factionID
  70. std::vector<std::string> names; //names of the town instances
  71. /// level -> list of creatures on this tier
  72. // TODO: replace with pointers to CCreature
  73. std::vector<std::vector<CreatureID::CreatureID> > creatures;
  74. bmap<int, ConstTransitivePtr<CBuilding> > buildings;
  75. std::vector<std::string> dwellings; //defs for adventure map dwellings for new towns, [0] means tier 1 creatures etc.
  76. std::vector<std::string> dwellingNames;
  77. // should be removed at least from configs in favour of auto-detection
  78. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  79. ui32 mageLevel; //max available mage guild level
  80. ui16 primaryRes;
  81. ArtifactID::ArtifactID warMachine;
  82. // Client-only data. Should be moved away from lib
  83. struct ClientInfo
  84. {
  85. struct Point
  86. {
  87. si32 x;
  88. si32 y;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. { h & x & y; }
  91. };
  92. //icons [fort is present?][build limit reached?] -> index of icon in def files
  93. int icons[2][2];
  94. std::string musicTheme;
  95. std::string townBackground;
  96. std::string guildWindow;
  97. std::string buildingsIcons;
  98. std::string hallBackground;
  99. /// vector[row][column] = list of buildings in this slot
  100. std::vector< std::vector< std::vector<int> > > hallSlots;
  101. /// list of town screen structures.
  102. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  103. std::vector<ConstTransitivePtr<CStructure> > structures;
  104. std::string advMapVillage;
  105. std::string advMapCastle;
  106. std::string advMapCapitol;
  107. std::string siegePrefix;
  108. std::vector<Point> siegePositions;
  109. CreatureID::CreatureID siegeShooter; // shooter creature ID
  110. si32 siegeShooterCropHeight; //trim height for shooters in turrets
  111. template <typename Handler> void serialize(Handler &h, const int version)
  112. {
  113. h & icons & musicTheme & townBackground & guildWindow & buildingsIcons & hallBackground & hallSlots & structures;
  114. h & advMapVillage & advMapCastle & advMapCapitol;
  115. h & siegePrefix & siegePositions & siegeShooter & siegeShooterCropHeight;
  116. }
  117. } clientInfo;
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. h & names & typeID & creatures & dwellings & dwellingNames & buildings & hordeLvl & mageLevel
  121. & primaryRes & warMachine & clientInfo;
  122. }
  123. };
  124. struct DLL_LINKAGE SPuzzleInfo
  125. {
  126. ui16 number; //type of puzzle
  127. si16 x, y; //position
  128. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  129. std::string filename; //file with graphic of this puzzle
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & number & x & y & whenUncovered & filename;
  133. }
  134. };
  135. class CFaction
  136. {
  137. public:
  138. std::string name; //town name, by default - from TownName.txt
  139. TFaction factionID;
  140. ETerrainType::ETerrainType nativeTerrain;
  141. EAlignment::EAlignment alignment;
  142. CreatureID::CreatureID commander;
  143. std::string creatureBg120;
  144. std::string creatureBg130;
  145. std::vector<SPuzzleInfo> puzzleMap;
  146. template <typename Handler> void serialize(Handler &h, const int version)
  147. {
  148. h & name & factionID & nativeTerrain & creatureBg120 & creatureBg130 & puzzleMap;
  149. }
  150. };
  151. class DLL_LINKAGE CTownHandler
  152. {
  153. /// loads CBuilding's into town
  154. void loadBuilding(CTown &town, const JsonNode & source);
  155. void loadBuildings(CTown &town, const JsonNode & source);
  156. /// loads CStructure's into town
  157. void loadStructure(CTown &town, const JsonNode & source);
  158. void loadStructures(CTown &town, const JsonNode & source);
  159. /// loads town hall vector (hallSlots)
  160. void loadTownHall(CTown &town, const JsonNode & source);
  161. void loadSiegeScreen(CTown &town, const JsonNode & source);
  162. void loadClientData(CTown &town, const JsonNode & source);
  163. void loadTown(CTown &town, const JsonNode & source);
  164. void loadPuzzle(CFaction & faction, const JsonNode & source);
  165. /// load all available data from h3 txt(s) into json structure using format similar to vcmi configs
  166. /// returns 2d array [townID] [buildID] of buildings
  167. void loadLegacyData(JsonNode & dest);
  168. public:
  169. std::map<TFaction, CTown> towns;
  170. std::map<TFaction, CFaction> factions;
  171. CTownHandler(); //c-tor, set pointer in VLC to this
  172. /// main loading function for mods, accepts merged JSON source and add all entries from it into game
  173. /// all entries in JSON should be checked for validness before using this function
  174. void load(const JsonNode & source);
  175. /// "entry point" for loading of OH3 town.
  176. /// reads legacy txt's from H3 + vcmi json, merges them
  177. /// and loads resulting structure to game using loadTowns method
  178. void load();
  179. /**
  180. * Gets a list of default allowed factions. OH3 factions are in the range of 0 to 8.
  181. *
  182. * TODO Proposal for town modding: Replace faction id with a unique machine readable town name
  183. * and create a JSON config file or merge it with other configs which describes which
  184. * towns can be used for random map generation / map editor(default map settings).
  185. *
  186. * @return a list of allowed factions, the index which is unique is the faction id
  187. */
  188. std::set<TFaction> getDefaultAllowedFactions() const;
  189. template <typename Handler> void serialize(Handler &h, const int version)
  190. {
  191. h & towns & factions;
  192. }
  193. };