CTownHandler.cpp 15 KB

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