CTownHandler.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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<TCreature> > creatures;
  74. bmap<int, ConstTransitivePtr<CBuilding> > buildings;
  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, warMachine;
  79. // Client-only data. Should be moved away from lib
  80. struct ClientInfo
  81. {
  82. struct Point
  83. {
  84. si32 x;
  85. si32 y;
  86. template <typename Handler> void serialize(Handler &h, const int version)
  87. { h & x & y; }
  88. };
  89. //icons [fort is present?][build limit reached?] -> index of icon in def files
  90. int icons[2][2];
  91. std::string musicTheme;
  92. std::string townBackground;
  93. std::string guildWindow;
  94. std::string buildingsIcons;
  95. std::string hallBackground;
  96. /// vector[row][column] = list of buildings in this slot
  97. std::vector< std::vector< std::vector<int> > > hallSlots;
  98. /// list of town screen structures.
  99. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  100. std::vector<ConstTransitivePtr<CStructure> > structures;
  101. std::string advMapVillage;
  102. std::string advMapCastle;
  103. std::string advMapCapitol;
  104. std::string siegePrefix;
  105. std::vector<Point> siegePositions;
  106. TCreature siegeShooter; // shooter creature ID
  107. si32 siegeShooterCropHeight; //trim height for shooters in turrets
  108. template <typename Handler> void serialize(Handler &h, const int version)
  109. {
  110. h & icons & musicTheme & townBackground & guildWindow & buildingsIcons & hallBackground & hallSlots & structures;
  111. h & advMapVillage & advMapCastle & advMapCapitol;
  112. h & siegePrefix & siegePositions & siegeShooter & siegeShooterCropHeight;
  113. }
  114. } clientInfo;
  115. template <typename Handler> void serialize(Handler &h, const int version)
  116. {
  117. h & names & typeID & creatures & buildings & hordeLvl & mageLevel
  118. & primaryRes & warMachine & clientInfo;
  119. }
  120. friend class CTownHandler;
  121. };
  122. struct DLL_LINKAGE SPuzzleInfo
  123. {
  124. ui16 number; //type of puzzle
  125. si16 x, y; //position
  126. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  127. std::string filename; //file with graphic of this puzzle
  128. template <typename Handler> void serialize(Handler &h, const int version)
  129. {
  130. h & number & x & y & whenUncovered & filename;
  131. }
  132. };
  133. class CFaction
  134. {
  135. public:
  136. std::string name; //town name, by default - from TownName.txt
  137. TFaction factionID;
  138. ui8 nativeTerrain;
  139. ui8 alignment; // uses EAlignment enum
  140. std::string creatureBg120;
  141. std::string creatureBg130;
  142. std::vector<SPuzzleInfo> puzzleMap;
  143. template <typename Handler> void serialize(Handler &h, const int version)
  144. {
  145. h & name & factionID & nativeTerrain & creatureBg120 & creatureBg130 & puzzleMap;
  146. }
  147. };
  148. class DLL_LINKAGE CTownHandler
  149. {
  150. /// loads CBuilding's into town
  151. void loadBuilding(CTown &town, const JsonNode & source);
  152. void loadBuildings(CTown &town, const JsonNode & source);
  153. /// loads CStructure's into town
  154. void loadStructure(CTown &town, const JsonNode & source);
  155. void loadStructures(CTown &town, const JsonNode & source);
  156. /// loads town hall vector (hallSlots)
  157. void loadTownHall(CTown &town, const JsonNode & source);
  158. void loadSiegeScreen(CTown &town, const JsonNode & source);
  159. void loadClientData(CTown &town, const JsonNode & source);
  160. void loadTown(CTown &town, const JsonNode & source);
  161. void loadPuzzle(CFaction & faction, const JsonNode & source);
  162. /// load all available data from h3 txt(s) into json structure using format similar to vcmi configs
  163. /// returns 2d array [townID] [buildID] of buildings
  164. void loadLegacyData(JsonNode & dest);
  165. public:
  166. std::map<TFaction, CTown> towns;
  167. std::map<TFaction, CFaction> factions;
  168. CTownHandler(); //c-tor, set pointer in VLC to this
  169. /// main loading function for mods, accepts merged JSON source and add all entries from it into game
  170. /// all entries in JSON should be checked for validness before using this function
  171. void load(const JsonNode & source);
  172. /// "entry point" for loading of OH3 town.
  173. /// reads legacy txt's from H3 + vcmi json, merges them
  174. /// and loads resulting structure to game using loadTowns method
  175. void load();
  176. /**
  177. * Gets a list of default allowed factions. OH3 factions are in the range of 0 to 8.
  178. *
  179. * TODO Proposal for town modding: Replace faction id with a unique machine readable town name
  180. * and create a JSON config file or merge it with other configs which describes which
  181. * towns can be used for random map generation / map editor(default map settings).
  182. *
  183. * @return a list of allowed factions, the index which is unique is the faction id
  184. */
  185. std::set<TFaction> getDefaultAllowedFactions() const;
  186. template <typename Handler> void serialize(Handler &h, const int version)
  187. {
  188. h & towns & factions;
  189. }
  190. };