CTownHandler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. return name;
  20. }
  21. const std::string & CBuilding::Description() const
  22. {
  23. return description;
  24. }
  25. CBuilding::BuildingType CBuilding::getBase() const
  26. {
  27. const CBuilding * build = this;
  28. while (build->upgrade >= 0)
  29. build = VLC->townh->towns[build->tid].buildings[build->upgrade];
  30. return build->bid;
  31. }
  32. si32 CBuilding::getDistance(CBuilding::BuildingType buildID) const
  33. {
  34. const CBuilding * build = VLC->townh->towns[tid].buildings[buildID];
  35. int distance = 0;
  36. while (build->upgrade >= 0 && build != this)
  37. {
  38. build = VLC->townh->towns[build->tid].buildings[build->upgrade];
  39. distance++;
  40. }
  41. if (build == this)
  42. return distance;
  43. return -1;
  44. }
  45. CTownHandler::CTownHandler()
  46. {
  47. VLC->townh = this;
  48. }
  49. JsonNode readBuilding(CLegacyConfigParser & parser)
  50. {
  51. JsonNode ret;
  52. JsonNode & cost = ret["cost"];
  53. //note: this code will try to parse mithril as well but wil always return 0 for it
  54. BOOST_FOREACH(const std::string & resID, GameConstants::RESOURCE_NAMES)
  55. cost[resID].Float() = parser.readNumber();
  56. parser.endLine();
  57. return ret;
  58. }
  59. void CTownHandler::loadLegacyData(JsonNode & dest)
  60. {
  61. CLegacyConfigParser parser("DATA/BUILDING.TXT");
  62. dest.Vector().resize(GameConstants::F_NUMBER);
  63. parser.endLine(); // header
  64. parser.endLine();
  65. //Unique buildings
  66. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  67. {
  68. JsonVector & buildList = dest.Vector()[town]["buildings"].Vector();
  69. buildList.resize( 30 ); //prepare vector for first set of buildings
  70. parser.endLine(); //header
  71. parser.endLine();
  72. int buildID = 17;
  73. do
  74. {
  75. buildList[buildID] = readBuilding(parser);
  76. buildID++;
  77. }
  78. while (!parser.isNextEntryEmpty());
  79. }
  80. // Common buildings
  81. parser.endLine(); // header
  82. parser.endLine();
  83. parser.endLine();
  84. int buildID = 0;
  85. do
  86. {
  87. JsonNode building = readBuilding(parser);
  88. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  89. dest.Vector()[town]["buildings"].Vector()[buildID] = building;
  90. buildID++;
  91. }
  92. while (!parser.isNextEntryEmpty());
  93. parser.endLine(); //header
  94. parser.endLine();
  95. //Dwellings
  96. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  97. {
  98. parser.endLine(); //header
  99. parser.endLine();
  100. do
  101. {
  102. dest.Vector()[town]["buildings"].Vector().push_back(readBuilding(parser));
  103. }
  104. while (!parser.isNextEntryEmpty());
  105. }
  106. {
  107. CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
  108. for(int building=0; building<15; building++)
  109. {
  110. std::string name = parser.readString();
  111. std::string descr = parser.readString();
  112. parser.endLine();
  113. for(int j=0; j<GameConstants::F_NUMBER; j++)
  114. {
  115. JsonVector & buildings = dest.Vector()[j]["buildings"].Vector();
  116. buildings[building]["name"].String() = name;
  117. buildings[building]["description"].String() = descr;
  118. }
  119. }
  120. parser.endLine(); // silo
  121. parser.endLine(); // blacksmith //unused entries
  122. parser.endLine(); // moat
  123. //shipyard with the ship
  124. std::string name = parser.readString();
  125. std::string descr = parser.readString();
  126. parser.endLine();
  127. for(int town=0; town<GameConstants::F_NUMBER; town++)
  128. {
  129. JsonVector & buildings = dest.Vector()[town]["buildings"].Vector();
  130. buildings[20]["name"].String() = name;
  131. buildings[20]["description"].String() = descr;
  132. }
  133. //blacksmith
  134. for(int town=0; town<GameConstants::F_NUMBER; town++)
  135. {
  136. JsonVector & buildings = dest.Vector()[town]["buildings"].Vector();
  137. buildings[16]["name"].String() = parser.readString();
  138. buildings[16]["description"].String() = parser.readString();
  139. parser.endLine();
  140. }
  141. }
  142. {
  143. CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
  144. for(int town=0; town<GameConstants::F_NUMBER; town++)
  145. {
  146. JsonVector & buildings = dest.Vector()[town]["buildings"].Vector();
  147. for(int build=0; build<9; build++)
  148. {
  149. buildings[17+build]["name"].String() = parser.readString();
  150. buildings[17+build]["description"].String() = parser.readString();
  151. parser.endLine();
  152. }
  153. buildings[26]["name"].String() = parser.readString(); // Grail
  154. buildings[26]["description"].String() = parser.readString();
  155. parser.endLine();
  156. buildings[15]["name"].String() = parser.readString(); // Resource silo
  157. buildings[15]["description"].String() = parser.readString();
  158. parser.endLine();
  159. }
  160. }
  161. {
  162. CLegacyConfigParser parser("DATA/DWELLING.TXT");
  163. for(int town=0; town<GameConstants::F_NUMBER; town++)
  164. {
  165. JsonVector & buildings = dest.Vector()[town]["buildings"].Vector();
  166. for(int build=0; build<14; build++)
  167. {
  168. buildings[30+build]["name"].String() = parser.readString();
  169. buildings[30+build]["description"].String() = parser.readString();
  170. parser.endLine();
  171. }
  172. }
  173. }
  174. {
  175. CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
  176. CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
  177. size_t townID=0;
  178. do
  179. {
  180. JsonNode & town = dest.Vector()[townID];
  181. town["name"].String() = typeParser.readString();
  182. for (int i=0; i<GameConstants::NAMES_PER_TOWN; i++)
  183. {
  184. JsonNode name;
  185. name.String() = nameParser.readString();
  186. town["names"].Vector().push_back(name);
  187. nameParser.endLine();
  188. }
  189. townID++;
  190. }
  191. while (typeParser.endLine());
  192. }
  193. }
  194. void CTownHandler::loadBuilding(CTown &town, const JsonNode & source)
  195. {
  196. CBuilding * ret = new CBuilding;
  197. static const std::string modes [] = {"normal", "auto", "special", "grail"};
  198. ret->mode = boost::find(modes, source["mode"].String()) - modes;
  199. ret->tid = town.typeID;
  200. ret->bid = source["id"].Float();
  201. ret->name = source["name"].String();
  202. ret->description = source["description"].String();
  203. ret->resources = TResources(source["cost"]);
  204. BOOST_FOREACH(const JsonNode &building, source["requires"].Vector())
  205. ret->requirements.insert(building.Float());
  206. if (!source["upgrades"].isNull())
  207. {
  208. ret->requirements.insert(source["upgrades"].Float());
  209. ret->upgrade = source["upgrades"].Float();
  210. }
  211. else
  212. ret->upgrade = -1;
  213. town.buildings[ret->bid] = ret;
  214. }
  215. void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
  216. {
  217. BOOST_FOREACH(const JsonNode &node, source.Vector())
  218. {
  219. loadBuilding(town, node);
  220. }
  221. }
  222. void CTownHandler::loadStructure(CTown &town, const JsonNode & source)
  223. {
  224. CStructure * ret = new CStructure;
  225. if (source["id"].isNull())
  226. {
  227. ret->building = nullptr;
  228. ret->buildable = nullptr;
  229. }
  230. else
  231. {
  232. ret->building = town.buildings[source["id"].Float()];
  233. if (source["builds"].isNull())
  234. ret->buildable = ret->building;
  235. else
  236. ret->buildable = town.buildings[source["builds"].Float()];
  237. }
  238. ret->pos.x = source["x"].Float();
  239. ret->pos.y = source["y"].Float();
  240. ret->pos.z = source["z"].Float();
  241. ret->hiddenUpgrade = source["hidden"].Bool();
  242. ret->defName = source["animation"].String();
  243. ret->borderName = source["border"].String();
  244. ret->areaName = source["area"].String();
  245. town.clientInfo.structures.push_back(ret);
  246. }
  247. void CTownHandler::loadStructures(CTown &town, const JsonNode & source)
  248. {
  249. BOOST_FOREACH(const JsonNode &node, source.Vector())
  250. {
  251. loadStructure(town, node);
  252. }
  253. }
  254. void CTownHandler::loadTownHall(CTown &town, const JsonNode & source)
  255. {
  256. BOOST_FOREACH(const JsonNode &row, source.Vector())
  257. {
  258. std::vector< std::vector<int> > hallRow;
  259. BOOST_FOREACH(const JsonNode &box, row.Vector())
  260. {
  261. std::vector<int> hallBox;
  262. BOOST_FOREACH(const JsonNode &value, box.Vector())
  263. {
  264. hallBox.push_back(value.Float());
  265. }
  266. hallRow.push_back(hallBox);
  267. }
  268. town.clientInfo.hallSlots.push_back(hallRow);
  269. }
  270. }
  271. CTown::ClientInfo::Point JsonToPoint(const JsonNode & node)
  272. {
  273. CTown::ClientInfo::Point ret;
  274. ret.x = node["x"].Float();
  275. ret.y = node["y"].Float();
  276. return ret;
  277. }
  278. void CTownHandler::loadSiegeScreen(CTown &town, const JsonNode & source)
  279. {
  280. town.clientInfo.siegePrefix = source["imagePrefix"].String();
  281. town.clientInfo.siegeShooter = source["shooter"].Float();
  282. town.clientInfo.siegeShooterCropHeight = source["shooterHeight"].Float();
  283. auto & pos = town.clientInfo.siegePositions;
  284. pos.resize(21);
  285. pos[8] = JsonToPoint(source["towers"]["top"]["tower"]);
  286. pos[17] = JsonToPoint(source["towers"]["top"]["battlement"]);
  287. pos[20] = JsonToPoint(source["towers"]["top"]["creature"]);
  288. pos[2] = JsonToPoint(source["towers"]["keep"]["tower"]);
  289. pos[15] = JsonToPoint(source["towers"]["keep"]["battlement"]);
  290. pos[18] = JsonToPoint(source["towers"]["keep"]["creature"]);
  291. pos[3] = JsonToPoint(source["towers"]["bottom"]["tower"]);
  292. pos[16] = JsonToPoint(source["towers"]["bottom"]["battlement"]);
  293. pos[19] = JsonToPoint(source["towers"]["bottom"]["creature"]);
  294. pos[9] = JsonToPoint(source["gate"]["gate"]);
  295. pos[10] = JsonToPoint(source["gate"]["arch"]);
  296. pos[7] = JsonToPoint(source["walls"]["upper"]);
  297. pos[6] = JsonToPoint(source["walls"]["upperMid"]);
  298. pos[5] = JsonToPoint(source["walls"]["bottomMid"]);
  299. pos[4] = JsonToPoint(source["walls"]["bottom"]);
  300. pos[13] = JsonToPoint(source["moat"]["moat"]);
  301. pos[14] = JsonToPoint(source["moat"]["bank"]);
  302. pos[11] = JsonToPoint(source["static"]["bottom"]);
  303. pos[12] = JsonToPoint(source["static"]["top"]);
  304. pos[1] = JsonToPoint(source["static"]["background"]);
  305. }
  306. void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
  307. {
  308. town.clientInfo.icons[0][0] = source["icons"]["village"]["normal"].Float();
  309. town.clientInfo.icons[0][1] = source["icons"]["village"]["built"].Float();
  310. town.clientInfo.icons[1][0] = source["icons"]["fort"]["normal"].Float();
  311. town.clientInfo.icons[1][1] = source["icons"]["fort"]["built"].Float();
  312. town.clientInfo.hallBackground = source["hallBackground"].String();
  313. town.clientInfo.musicTheme = source["musicTheme"].String();
  314. town.clientInfo.townBackground = source["townBackground"].String();
  315. town.clientInfo.guildWindow = source["guildWindow"].String();
  316. town.clientInfo.buildingsIcons = source["buildingsIcons"].String();
  317. town.clientInfo.advMapVillage = source["adventureMap"]["village"].String();
  318. town.clientInfo.advMapCastle = source["adventureMap"]["castle"].String();
  319. town.clientInfo.advMapCapitol = source["adventureMap"]["capitol"].String();
  320. loadTownHall(town, source["hallSlots"]);
  321. loadStructures(town, source["structures"]);
  322. loadSiegeScreen(town, source["siege"]);
  323. }
  324. void CTownHandler::loadTown(CTown &town, const JsonNode & source)
  325. {
  326. town.mageLevel = source["mageGuild"].Float();
  327. town.primaryRes = source["primaryResource"].Float();
  328. town.warMachine = source["warMachine"].Float();
  329. town.names = source["names"].StdVector<std::string>();
  330. // Horde building creature level
  331. BOOST_FOREACH(const JsonNode &node, source["horde"].Vector())
  332. {
  333. town.hordeLvl[town.hordeLvl.size()] = node.Float();
  334. }
  335. BOOST_FOREACH(const JsonNode &list, source["creatures"].Vector())
  336. {
  337. std::vector<TCreature> level;
  338. BOOST_FOREACH(const JsonNode &node, list.Vector())
  339. {
  340. level.push_back(node.Float());
  341. }
  342. town.creatures.push_back(level);
  343. }
  344. loadBuildings(town, source["buildings"]);
  345. loadClientData(town,source);
  346. }
  347. void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
  348. {
  349. faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
  350. std::string prefix = source["prefix"].String();
  351. BOOST_FOREACH(const JsonNode &piece, source["pieces"].Vector())
  352. {
  353. size_t index = faction.puzzleMap.size();
  354. SPuzzleInfo spi;
  355. spi.x = piece["x"].Float();
  356. spi.y = piece["y"].Float();
  357. spi.whenUncovered = piece["index"].Float();
  358. spi.number = index;
  359. // filename calculation
  360. std::ostringstream suffix;
  361. suffix << std::setfill('0') << std::setw(2) << index;
  362. spi.filename = prefix + suffix.str();
  363. faction.puzzleMap.push_back(spi);
  364. }
  365. assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
  366. }
  367. void CTownHandler::load(const JsonNode &source)
  368. {
  369. BOOST_FOREACH(auto & node, source.Struct())
  370. {
  371. int id = node.second["index"].Float();
  372. CFaction & faction = factions[id];
  373. faction.factionID = id;
  374. faction.name = node.second["name"].String();
  375. faction.creatureBg120 = node.second["creatureBackground"]["120px"].String();
  376. faction.creatureBg130 = node.second["creatureBackground"]["130px"].String();
  377. faction.nativeTerrain = vstd::find_pos(GameConstants::TERRAIN_NAMES, node.second["nativeTerrain"].String());
  378. int alignment = vstd::find_pos(EAlignment::names, node.second["alignment"].String());
  379. if (alignment == -1)
  380. faction.alignment = EAlignment::NEUTRAL;
  381. else
  382. faction.alignment = alignment;
  383. if (!node.second["town"].isNull())
  384. {
  385. towns[id].typeID = id;
  386. loadTown(towns[id], node.second["town"]);
  387. }
  388. if (!node.second["puzzleMap"].isNull())
  389. loadPuzzle(faction, node.second["puzzleMap"]);
  390. tlog3 << "Added faction: " << node.first << "\n";
  391. }
  392. }
  393. void CTownHandler::load()
  394. {
  395. JsonNode buildingsConf(ResourceID("config/buildings.json"));
  396. JsonNode legacyConfig;
  397. loadLegacyData(legacyConfig);
  398. //hardcoded list of H3 factions. Should be only used to convert H3 configs
  399. static const std::string factionName [GameConstants::F_NUMBER] =
  400. {
  401. "castle", "rampart", "tower",
  402. "inferno", "necropolis", "dungeon",
  403. "stronghold", "fortress", "conflux"
  404. };
  405. // semi-manually merge legacy config with towns json
  406. for (size_t i=0; i< legacyConfig.Vector().size(); i++)
  407. {
  408. JsonNode & legacyFaction = legacyConfig.Vector()[i];
  409. JsonNode & outputFaction = buildingsConf[factionName[i]];
  410. if (outputFaction["name"].isNull())
  411. outputFaction["name"] = legacyFaction["name"];
  412. if (!outputFaction["town"].isNull())
  413. {
  414. if (outputFaction["town"]["names"].isNull())
  415. outputFaction["town"]["names"] = legacyFaction["names"];
  416. JsonNode & outputBuildings = outputFaction["town"]["buildings"];
  417. JsonVector & legacyBuildings = legacyFaction["buildings"].Vector();
  418. BOOST_FOREACH(JsonNode & building, outputBuildings.Vector())
  419. {
  420. if (vstd::contains(building.Struct(), "id") &&
  421. legacyBuildings.size() > building["id"].Float() )
  422. {
  423. //find same buildings in legacy and json configs
  424. JsonNode & legacyBuilding = legacyBuildings[building["id"].Float()];
  425. if (!legacyBuilding.isNull()) //merge if h3 config was found for this building
  426. JsonNode::merge(building, legacyBuilding);
  427. }
  428. }
  429. }
  430. }
  431. load(buildingsConf);
  432. }