CTownHandler.cpp 17 KB

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