CTownHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "StdInc.h"
  2. #include "CTownHandler.h"
  3. #include "VCMI_Lib.h"
  4. #include "CGeneralTextHandler.h"
  5. #include "JsonNode.h"
  6. #include "GameConstants.h"
  7. #include "Filesystem/CResourceLoader.h"
  8. /*
  9. * CTownHandler.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. const std::string & CBuilding::Name() const
  18. {
  19. if(name.length())
  20. return name;
  21. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  22. return VLC->generaltexth->buildings[tid][bid].first;
  23. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  24. return name;
  25. }
  26. const std::string & CBuilding::Description() const
  27. {
  28. if(description.length())
  29. return description;
  30. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  31. return VLC->generaltexth->buildings[tid][bid].second;
  32. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  33. return description;
  34. }
  35. CBuilding::BuildingType CBuilding::getBase() const
  36. {
  37. const CBuilding * build = this;
  38. while (build->upgrade >= 0)
  39. build = VLC->townh->towns[build->tid].buildings[build->upgrade];
  40. return build->bid;
  41. }
  42. si32 CBuilding::getDistance(CBuilding::BuildingType buildID) const
  43. {
  44. const CBuilding * build = VLC->townh->towns[tid].buildings[buildID];
  45. int distance = 0;
  46. while (build->upgrade >= 0 && build != this)
  47. {
  48. build = VLC->townh->towns[build->tid].buildings[build->upgrade];
  49. distance++;
  50. }
  51. if (build == this)
  52. return distance;
  53. return -1;
  54. }
  55. const std::string & CTown::Name() const
  56. {
  57. if(name.length())
  58. return name;
  59. else
  60. return VLC->generaltexth->townTypes[typeID];
  61. }
  62. const std::vector<std::string> & CTown::Names() const
  63. {
  64. if(names.size())
  65. return names;
  66. else
  67. return VLC->generaltexth->townNames[typeID];
  68. }
  69. CTownHandler::CTownHandler()
  70. {
  71. VLC->townh = this;
  72. }
  73. JsonNode readBuilding(CLegacyConfigParser & parser)
  74. {
  75. JsonNode ret;
  76. JsonNode & cost = ret["cost"];
  77. //note: this code will try to parse mithril as well but wil always return 0 for it
  78. BOOST_FOREACH(const std::string & resID, GameConstants::RESOURCE_NAMES)
  79. cost[resID].Float() = parser.readNumber();
  80. parser.endLine();
  81. return ret;
  82. }
  83. void CTownHandler::loadLegacyData(JsonNode & dest)
  84. {
  85. CLegacyConfigParser parser("DATA/BUILDING.TXT");
  86. dest.Vector().resize(GameConstants::F_NUMBER);
  87. parser.endLine(); // header
  88. parser.endLine();
  89. //Unique buildings
  90. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  91. {
  92. JsonVector & buildList = dest.Vector()[town].Vector();
  93. buildList.resize( 30 ); //prepare vector for first set of buildings
  94. parser.endLine(); //header
  95. parser.endLine();
  96. int buildID = 17;
  97. do
  98. {
  99. buildList[buildID] = readBuilding(parser);
  100. buildID++;
  101. }
  102. while (!parser.isNextEntryEmpty());
  103. }
  104. // Common buildings
  105. parser.endLine(); // header
  106. parser.endLine();
  107. parser.endLine();
  108. int buildID = 0;
  109. do
  110. {
  111. JsonNode building = readBuilding(parser);
  112. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  113. dest.Vector()[town].Vector()[buildID] = building;
  114. buildID++;
  115. }
  116. while (!parser.isNextEntryEmpty());
  117. parser.endLine(); //header
  118. parser.endLine();
  119. //Dwellings
  120. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  121. {
  122. parser.endLine(); //header
  123. parser.endLine();
  124. do
  125. {
  126. dest.Vector()[town].Vector().push_back(readBuilding(parser));
  127. }
  128. while (!parser.isNextEntryEmpty());
  129. }
  130. }
  131. void CTownHandler::loadBuilding(CTown &town, const JsonNode & source)
  132. {
  133. CBuilding * ret = new CBuilding;
  134. static const std::string modes [] = {"normal", "auto", "special", "grail"};
  135. ret->mode = boost::find(modes, source["mode"].String()) - modes;
  136. ret->tid = town.typeID;
  137. ret->bid = source["id"].Float();
  138. ret->name = source["name"].String();
  139. ret->description = source["description"].String();
  140. ret->resources = TResources(source["cost"]);
  141. BOOST_FOREACH(const JsonNode &building, source["requires"].Vector())
  142. ret->requirements.insert(building.Float());
  143. if (!source["upgrades"].isNull())
  144. {
  145. ret->requirements.insert(source["upgrades"].Float());
  146. ret->upgrade = source["upgrades"].Float();
  147. }
  148. else
  149. ret->upgrade = -1;
  150. town.buildings[ret->bid] = ret;
  151. }
  152. void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
  153. {
  154. BOOST_FOREACH(const JsonNode &node, source.Vector())
  155. {
  156. loadBuilding(town, node);
  157. }
  158. }
  159. void CTownHandler::loadStructure(CTown &town, const JsonNode & source)
  160. {
  161. CStructure * ret = new CStructure;
  162. if (source["id"].isNull())
  163. {
  164. ret->building = nullptr;
  165. ret->buildable = nullptr;
  166. }
  167. else
  168. {
  169. ret->building = town.buildings[source["id"].Float()];
  170. if (source["builds"].isNull())
  171. ret->buildable = ret->building;
  172. else
  173. ret->buildable = town.buildings[source["builds"].Float()];
  174. }
  175. ret->pos.x = source["x"].Float();
  176. ret->pos.y = source["y"].Float();
  177. ret->pos.z = source["z"].Float();
  178. ret->hiddenUpgrade = source["hidden"].Bool();
  179. ret->defName = source["animation"].String();
  180. ret->borderName = source["border"].String();
  181. ret->areaName = source["area"].String();
  182. town.clientInfo.structures.push_back(ret);
  183. }
  184. void CTownHandler::loadStructures(CTown &town, const JsonNode & source)
  185. {
  186. BOOST_FOREACH(const JsonNode &node, source.Vector())
  187. {
  188. loadStructure(town, node);
  189. }
  190. }
  191. void CTownHandler::loadTownHall(CTown &town, const JsonNode & source)
  192. {
  193. BOOST_FOREACH(const JsonNode &row, source.Vector())
  194. {
  195. std::vector< std::vector<int> > hallRow;
  196. BOOST_FOREACH(const JsonNode &box, row.Vector())
  197. {
  198. std::vector<int> hallBox;
  199. BOOST_FOREACH(const JsonNode &value, box.Vector())
  200. {
  201. hallBox.push_back(value.Float());
  202. }
  203. hallRow.push_back(hallBox);
  204. }
  205. town.clientInfo.hallSlots.push_back(hallRow);
  206. }
  207. }
  208. void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
  209. {
  210. town.clientInfo.icons[0][0] = source["icons"]["village"]["normal"].Float();
  211. town.clientInfo.icons[0][1] = source["icons"]["village"]["built"].Float();
  212. town.clientInfo.icons[1][0] = source["icons"]["fort"]["normal"].Float();
  213. town.clientInfo.icons[1][1] = source["icons"]["fort"]["built"].Float();
  214. town.clientInfo.hallBackground = source["hallBackground"].String();
  215. town.clientInfo.musicTheme = source["musicTheme"].String();
  216. town.clientInfo.townBackground = source["townBackground"].String();
  217. town.clientInfo.guildWindow = source["guildWindow"].String();
  218. town.clientInfo.buildingsIcons = source["buildingsIcons"].String();
  219. loadTownHall(town, source["hallSlots"]);
  220. loadStructures(town, source["structures"]);
  221. }
  222. void CTownHandler::loadTown(CTown &town, const JsonNode & source)
  223. {
  224. town.bonus = town.typeID;
  225. if (town.bonus==8)
  226. town.bonus=3;
  227. town.mageLevel = source["mageGuild"].Float();
  228. town.primaryRes = source["primaryResource"].Float();
  229. town.warMachine = source["warMachine"].Float();
  230. // Horde building creature level
  231. BOOST_FOREACH(const JsonNode &node, source["horde"].Vector())
  232. {
  233. town.hordeLvl[town.hordeLvl.size()] = node.Float();
  234. }
  235. BOOST_FOREACH(const JsonNode &list, source["creatures"].Vector())
  236. {
  237. std::vector<si32> level;
  238. BOOST_FOREACH(const JsonNode &node, list.Vector())
  239. {
  240. level.push_back(node.Float());
  241. }
  242. town.creatures.push_back(level);
  243. }
  244. loadBuildings(town, source["buildings"]);
  245. loadClientData(town,source);
  246. }
  247. void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
  248. {
  249. faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
  250. std::string prefix = source["prefix"].String();
  251. BOOST_FOREACH(const JsonNode &piece, source["pieces"].Vector())
  252. {
  253. size_t index = faction.puzzleMap.size();
  254. SPuzzleInfo spi;
  255. spi.x = piece["x"].Float();
  256. spi.y = piece["y"].Float();
  257. spi.whenUncovered = piece["index"].Float();
  258. spi.number = index;
  259. // filename calculation
  260. std::ostringstream suffix;
  261. suffix << std::setfill('0') << std::setw(2) << index;
  262. spi.filename = prefix + suffix.str();
  263. faction.puzzleMap.push_back(spi);
  264. }
  265. assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
  266. }
  267. void CTownHandler::loadFactions(const JsonNode &source)
  268. {
  269. BOOST_FOREACH(auto & node, source.Struct())
  270. {
  271. int id = node.second["index"].Float();
  272. CFaction & faction = factions[id];
  273. faction.factionID = id;
  274. faction.name = node.first;
  275. faction.creatureBg120 = node.second["creatureBackground"]["120px"].String();
  276. faction.creatureBg130 = node.second["creatureBackground"]["130px"].String();
  277. if (!node.second["nativeTerrain"].isNull())
  278. {
  279. //get terrain as string and converto to numeric ID
  280. faction.nativeTerrain = boost::find(GameConstants::TERRAIN_NAMES, node.second["nativeTerrain"].String()) - GameConstants::TERRAIN_NAMES;
  281. }
  282. else
  283. faction.nativeTerrain = -1;
  284. if (!node.second["town"].isNull())
  285. {
  286. towns[id].typeID = id;
  287. loadTown(towns[id], node.second["town"]);
  288. }
  289. if (!node.second["puzzleMap"].isNull())
  290. loadPuzzle(faction, node.second["puzzleMap"]);
  291. }
  292. }
  293. void CTownHandler::load()
  294. {
  295. JsonNode buildingsConf(ResourceID("config/buildings.json"));
  296. JsonNode legacyConfig;
  297. loadLegacyData(legacyConfig);
  298. //hardocoded list of H3 factions. Should be only used to convert H3 configs
  299. static const std::string factionName [GameConstants::F_NUMBER] =
  300. {
  301. "castle", "rampart", "tower",
  302. "inferno", "necropolis", "dungeon",
  303. "stronghold", "fortress", "conflux"
  304. };
  305. // semi-manually merge legacy config with towns json
  306. // legacy config have only one item: town buildings stored in 2d vector
  307. for (size_t i=0; i< legacyConfig.Vector().size(); i++)
  308. {
  309. JsonNode & buildings = buildingsConf[factionName[i]]["town"]["buildings"];
  310. BOOST_FOREACH(JsonNode & building, buildings.Vector())
  311. {
  312. JsonNode & legacyFaction = legacyConfig.Vector()[i];
  313. if (vstd::contains(building.Struct(), "id"))
  314. {
  315. //find same buildings in legacy and json configs
  316. JsonNode & legacyBuilding = legacyFaction.Vector()[building["id"].Float()];
  317. if (!legacyBuilding.isNull()) //merge if h3 config was found for this building
  318. JsonNode::merge(building, legacyBuilding);
  319. }
  320. }
  321. }
  322. loadFactions(buildingsConf);
  323. }