CTownHandler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. si32 tid, bid; //town ID and structure ID
  27. TResources resources;
  28. std::set<BuildingType> requirements; /// set of required buildings, includes upgradeOf;
  29. BuildingType 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. };
  37. ui32 mode;
  38. const std::string &Name() const;
  39. const std::string &Description() const;
  40. //return base of upgrade(s) or this
  41. BuildingType getBase() const;
  42. // returns how many times build has to be upgraded to become build
  43. si32 getDistance(BuildingType build) const;
  44. template <typename Handler> void serialize(Handler &h, const int version)
  45. {
  46. h & tid & bid & resources & name & description & requirements & upgrade & mode;
  47. }
  48. friend class CTownHandler;
  49. };
  50. /// This is structure used only by client
  51. /// Consists of all gui-related data about town structures
  52. /// Should be moved from lib to client
  53. struct DLL_LINKAGE CStructure
  54. {
  55. CBuilding * building; // base building. If null - this structure will be always present on screen
  56. CBuilding * buildable; // building that will be used to determine built building and visible cost. Usually same as "building"
  57. bool hiddenUpgrade; // used only if "building" is upgrade, if true - structure on town screen will behave exactly like parent (mouse clicks, hover texts, etc)
  58. int3 pos;
  59. std::string defName, borderName, areaName;
  60. template <typename Handler> void serialize(Handler &h, const int version)
  61. {
  62. h & pos & defName & borderName & areaName & building & buildable;
  63. }
  64. };
  65. class DLL_LINKAGE CTown
  66. {
  67. std::string name;
  68. std::string description;
  69. std::vector<std::string> names; //names of the town instances
  70. public:
  71. ui32 typeID;//also works as factionID
  72. /// level -> list of creatures on this tier
  73. // TODO: replace with pointers to CCreature
  74. std::vector<std::vector<si32> > creatures;
  75. bmap<int, ConstTransitivePtr<CBuilding> > buildings;
  76. // should be removed at least from configs in favour of auto-detection
  77. std::map<int,int> hordeLvl; //[0] - first horde building creature level; [1] - second horde building (-1 if not present)
  78. ui32 mageLevel; //max available mage guild level
  79. int bonus; //pic number
  80. ui16 primaryRes, warMachine;
  81. // Client-only data. Should be moved away from lib
  82. struct ClientInfo
  83. {
  84. //icons [fort is present?][build limit reached?] -> index of icon in def files
  85. int icons[2][2];
  86. std::string musicTheme;
  87. std::string townBackground;
  88. std::string guildWindow;
  89. std::string buildingsIcons;
  90. std::string hallBackground;
  91. /// vector[row][column] = list of buildings in this slot
  92. std::vector< std::vector< std::vector<int> > > hallSlots;
  93. /// list of town screen structures.
  94. /// NOTE: index in vector is meaningless. Vector used instead of list for a bit faster access
  95. std::vector<ConstTransitivePtr<CStructure> > structures;
  96. template <typename Handler> void serialize(Handler &h, const int version)
  97. {
  98. h & icons & musicTheme & townBackground & guildWindow & buildingsIcons & hallBackground & hallSlots & structures;
  99. }
  100. } clientInfo;
  101. const std::vector<std::string> & Names() const;
  102. const std::string & Name() const;
  103. template <typename Handler> void serialize(Handler &h, const int version)
  104. {
  105. h & name & names & typeID & creatures & buildings & hordeLvl & mageLevel & bonus
  106. & primaryRes & warMachine & clientInfo;
  107. }
  108. friend class CTownHandler;
  109. };
  110. struct DLL_LINKAGE SPuzzleInfo
  111. {
  112. ui16 number; //type of puzzle
  113. si16 x, y; //position
  114. ui16 whenUncovered; //determines the sequnce of discovering (the lesser it is the sooner puzzle will be discovered)
  115. std::string filename; //file with graphic of this puzzle
  116. template <typename Handler> void serialize(Handler &h, const int version)
  117. {
  118. h & number & x & y & whenUncovered & filename;
  119. }
  120. };
  121. class CFaction
  122. {
  123. public:
  124. std::string name; //reference name, usually lower case
  125. ui32 factionID;
  126. ui32 nativeTerrain;
  127. std::string creatureBg120;
  128. std::string creatureBg130;
  129. std::vector<SPuzzleInfo> puzzleMap;
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & name & factionID & nativeTerrain & creatureBg120 & creatureBg130 & puzzleMap;
  133. }
  134. };
  135. class DLL_LINKAGE CTownHandler
  136. {
  137. /// loads CBuilding's into town
  138. void loadBuilding(CTown &town, const JsonNode & source);
  139. void loadBuildings(CTown &town, const JsonNode & source);
  140. /// loads CStructure's into town
  141. void loadStructure(CTown &town, const JsonNode & source);
  142. void loadStructures(CTown &town, const JsonNode & source);
  143. /// loads town hall vector (hallSlots)
  144. void loadTownHall(CTown &town, const JsonNode & source);
  145. void loadClientData(CTown &town, const JsonNode & source);
  146. void loadTown(CTown &town, const JsonNode & source);
  147. void loadPuzzle(CFaction & faction, const JsonNode & source);
  148. /// main loading function, accepts merged JSON source and add all entries from it into game
  149. /// all entries in JSON should be checked for validness before using this function
  150. void loadFactions(const JsonNode & source);
  151. /// load all available data from h3 txt(s) into json structure using format similar to vcmi configs
  152. /// returns 2d array [townID] [buildID] of buildings
  153. void loadLegacyData(JsonNode & dest);
  154. public:
  155. std::map<ui32, CTown> towns;
  156. std::map<ui32, CFaction> factions;
  157. CTownHandler(); //c-tor, set pointer in VLC to this
  158. /// "entry point" for towns loading.
  159. /// reads legacy txt's from H3 + vcmi json, merges them
  160. /// and loads resulting structure to game using loadTowns method
  161. /// in future may require loaded Creature Handler
  162. void load();
  163. template <typename Handler> void serialize(Handler &h, const int version)
  164. {
  165. h & towns & factions;
  166. }
  167. };