CTownHandler.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. };
  121. struct DLL_LINKAGE SPuzzleInfo
  122. {
  123. ui16 number; //type of puzzle
  124. si16 x, y; //position
  125. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  126. std::string filename; //file with graphic of this puzzle
  127. template <typename Handler> void serialize(Handler &h, const int version)
  128. {
  129. h & number & x & y & whenUncovered & filename;
  130. }
  131. };
  132. class CFaction
  133. {
  134. public:
  135. std::string name; //town name, by default - from TownName.txt
  136. TFaction factionID;
  137. ui8 nativeTerrain;
  138. ui8 alignment; // uses EAlignment enum
  139. std::string creatureBg120;
  140. std::string creatureBg130;
  141. std::vector<SPuzzleInfo> puzzleMap;
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & name & factionID & nativeTerrain & creatureBg120 & creatureBg130 & puzzleMap;
  145. }
  146. };
  147. class DLL_LINKAGE CTownHandler
  148. {
  149. /// loads CBuilding's into town
  150. void loadBuilding(CTown &town, const JsonNode & source);
  151. void loadBuildings(CTown &town, const JsonNode & source);
  152. /// loads CStructure's into town
  153. void loadStructure(CTown &town, const JsonNode & source);
  154. void loadStructures(CTown &town, const JsonNode & source);
  155. /// loads town hall vector (hallSlots)
  156. void loadTownHall(CTown &town, const JsonNode & source);
  157. void loadSiegeScreen(CTown &town, const JsonNode & source);
  158. void loadClientData(CTown &town, const JsonNode & source);
  159. void loadTown(CTown &town, const JsonNode & source);
  160. void loadPuzzle(CFaction & faction, const JsonNode & source);
  161. /// load all available data from h3 txt(s) into json structure using format similar to vcmi configs
  162. /// returns 2d array [townID] [buildID] of buildings
  163. void loadLegacyData(JsonNode & dest);
  164. public:
  165. std::map<TFaction, CTown> towns;
  166. std::map<TFaction, CFaction> factions;
  167. CTownHandler(); //c-tor, set pointer in VLC to this
  168. /// main loading function for mods, accepts merged JSON source and add all entries from it into game
  169. /// all entries in JSON should be checked for validness before using this function
  170. void load(const JsonNode & source);
  171. /// "entry point" for loading of OH3 town.
  172. /// reads legacy txt's from H3 + vcmi json, merges them
  173. /// and loads resulting structure to game using loadTowns method
  174. void load();
  175. /**
  176. * Gets a list of default allowed factions. OH3 factions are in the range of 0 to 8.
  177. *
  178. * TODO Proposal for town modding: Replace faction id with a unique machine readable town name
  179. * and create a JSON config file or merge it with other configs which describes which
  180. * towns can be used for random map generation / map editor(default map settings).
  181. *
  182. * @return a list of allowed factions, the index which is unique is the faction id
  183. */
  184. std::set<TFaction> getDefaultAllowedFactions() const;
  185. template <typename Handler> void serialize(Handler &h, const int version)
  186. {
  187. h & towns & factions;
  188. }
  189. };