CTownHandler.h 7.8 KB

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