CTownHandler.cpp 17 KB

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