CTownHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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/Filesystem.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 = build->town->buildings.at(build->upgrade);
  35. return build->bid;
  36. }
  37. si32 CBuilding::getDistance(BuildingID buildID) const
  38. {
  39. const CBuilding * build = town->buildings.at(buildID);
  40. int distance = 0;
  41. while (build->upgrade >= 0 && build != this)
  42. {
  43. build = build->town->buildings.at(build->upgrade);
  44. distance++;
  45. }
  46. if (build == this)
  47. return distance;
  48. return -1;
  49. }
  50. CFaction::CFaction()
  51. {
  52. town = nullptr;
  53. }
  54. CFaction::~CFaction()
  55. {
  56. delete town;
  57. }
  58. CTown::CTown()
  59. {
  60. }
  61. CTown::~CTown()
  62. {
  63. for(auto & build : buildings)
  64. build.second.dellNull();
  65. for(auto & str : clientInfo.structures)
  66. str.dellNull();
  67. }
  68. CTownHandler::CTownHandler()
  69. {
  70. VLC->townh = this;
  71. }
  72. CTownHandler::~CTownHandler()
  73. {
  74. for(auto faction : factions)
  75. faction.dellNull();
  76. }
  77. JsonNode readBuilding(CLegacyConfigParser & parser)
  78. {
  79. JsonNode ret;
  80. JsonNode & cost = ret["cost"];
  81. //note: this code will try to parse mithril as well but wil always return 0 for it
  82. for(const std::string & resID : GameConstants::RESOURCE_NAMES)
  83. cost[resID].Float() = parser.readNumber();
  84. cost.Struct().erase("mithril"); // erase mithril to avoid confusing validator
  85. parser.endLine();
  86. return ret;
  87. }
  88. std::vector<JsonNode> CTownHandler::loadLegacyData(size_t dataSize)
  89. {
  90. std::vector<JsonNode> dest(dataSize);
  91. factions.resize(dataSize);
  92. auto getBuild = [&](size_t town, size_t building) -> JsonNode &
  93. {
  94. return dest[town]["town"]["buildings"][EBuildingType::names[building]];
  95. };
  96. CLegacyConfigParser parser("DATA/BUILDING.TXT");
  97. parser.endLine(); // header
  98. parser.endLine();
  99. //Unique buildings
  100. for (size_t town=0; town<dataSize; town++)
  101. {
  102. parser.endLine(); //header
  103. parser.endLine();
  104. int buildID = 17;
  105. do
  106. {
  107. getBuild(town, buildID) = readBuilding(parser);
  108. buildID++;
  109. }
  110. while (!parser.isNextEntryEmpty());
  111. }
  112. // Common buildings
  113. parser.endLine(); // header
  114. parser.endLine();
  115. parser.endLine();
  116. int buildID = 0;
  117. do
  118. {
  119. JsonNode building = readBuilding(parser);
  120. for (size_t town=0; town<dataSize; town++)
  121. getBuild(town, buildID) = building;
  122. buildID++;
  123. }
  124. while (!parser.isNextEntryEmpty());
  125. parser.endLine(); //header
  126. parser.endLine();
  127. //Dwellings
  128. for (size_t town=0; town<dataSize; town++)
  129. {
  130. parser.endLine(); //header
  131. parser.endLine();
  132. for (size_t i=0; i<14; i++)
  133. {
  134. getBuild(town, 30+i) = readBuilding(parser);
  135. }
  136. }
  137. {
  138. CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
  139. for(int building=0; building<15; building++)
  140. {
  141. std::string name = parser.readString();
  142. std::string descr = parser.readString();
  143. parser.endLine();
  144. for(int j=0; j<dataSize; j++)
  145. {
  146. getBuild(j, building)["name"].String() = name;
  147. getBuild(j, building)["description"].String() = descr;
  148. }
  149. }
  150. parser.endLine(); // silo
  151. parser.endLine(); // blacksmith //unused entries
  152. parser.endLine(); // moat
  153. //shipyard with the ship
  154. std::string name = parser.readString();
  155. std::string descr = parser.readString();
  156. parser.endLine();
  157. for(int town=0; town<dataSize; town++)
  158. {
  159. getBuild(town, 20)["name"].String() = name;
  160. getBuild(town, 20)["description"].String() = descr;
  161. }
  162. //blacksmith
  163. for(int town=0; town<dataSize; town++)
  164. {
  165. getBuild(town, 16)["name"].String() = parser.readString();
  166. getBuild(town, 16)["description"].String() = parser.readString();
  167. parser.endLine();
  168. }
  169. }
  170. {
  171. CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
  172. for(int town=0; town<dataSize; town++)
  173. {
  174. for(int build=0; build<9; build++)
  175. {
  176. getBuild(town, 17 + build)["name"].String() = parser.readString();
  177. getBuild(town, 17 + build)["description"].String() = parser.readString();
  178. parser.endLine();
  179. }
  180. getBuild(town, 26)["name"].String() = parser.readString(); // Grail
  181. getBuild(town, 26)["description"].String() = parser.readString();
  182. parser.endLine();
  183. getBuild(town, 15)["name"].String() = parser.readString(); // Resource silo
  184. getBuild(town, 15)["description"].String() = parser.readString();
  185. parser.endLine();
  186. }
  187. }
  188. {
  189. CLegacyConfigParser parser("DATA/DWELLING.TXT");
  190. for(int town=0; town<dataSize; town++)
  191. {
  192. for(int build=0; build<14; build++)
  193. {
  194. getBuild(town, 30 + build)["name"].String() = parser.readString();
  195. getBuild(town, 30 + build)["description"].String() = parser.readString();
  196. parser.endLine();
  197. }
  198. }
  199. }
  200. {
  201. CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
  202. CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
  203. size_t townID=0;
  204. do
  205. {
  206. dest[townID]["name"].String() = typeParser.readString();
  207. for (int i=0; i<NAMES_PER_TOWN; i++)
  208. {
  209. JsonNode name;
  210. name.String() = nameParser.readString();
  211. dest[townID]["town"]["names"].Vector().push_back(name);
  212. nameParser.endLine();
  213. }
  214. townID++;
  215. }
  216. while (typeParser.endLine());
  217. }
  218. return dest;
  219. }
  220. void CTownHandler::loadBuildingRequirements(CTown &town, CBuilding & building, const JsonNode & source)
  221. {
  222. if (source.isNull())
  223. return;
  224. if (source.Vector()[0].getType() == JsonNode::DATA_FLOAT)
  225. {
  226. // MODS COMPATIBILITY
  227. CBuilding::TRequired::OperatorAll required;
  228. for(const JsonNode &building : source.Vector())
  229. required.expressions.push_back(BuildingID(building.Float()));
  230. building.requirements = CBuilding::TRequired(required);
  231. }
  232. else
  233. {
  234. BuildingRequirementsHelper hlp;
  235. hlp.building = &building;
  236. hlp.faction = town.faction;
  237. hlp.json = source;
  238. requirementsToLoad.push_back(hlp);
  239. }
  240. }
  241. void CTownHandler::loadBuilding(CTown &town, const std::string & stringID, const JsonNode & source)
  242. {
  243. auto ret = new CBuilding;
  244. static const std::string modes [] = {"normal", "auto", "special", "grail"};
  245. ret->mode = static_cast<CBuilding::EBuildMode>(boost::find(modes, source["mode"].String()) - modes);
  246. ret->identifier = stringID;
  247. ret->town = &town;
  248. ret->bid = BuildingID(source["id"].Float());
  249. ret->name = source["name"].String();
  250. ret->description = source["description"].String();
  251. ret->resources = TResources(source["cost"]);
  252. loadBuildingRequirements(town, *ret, source["requires"]);
  253. if (!source["upgrades"].isNull())
  254. {
  255. //MODS COMPATIBILITY
  256. if (source["upgrades"].getType() == JsonNode::DATA_FLOAT)
  257. ret->upgrade = BuildingID(source["upgrades"].Float());
  258. else
  259. {
  260. VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, source["upgrades"], [=](si32 identifier)
  261. {
  262. ret->upgrade = BuildingID(identifier);
  263. });
  264. }
  265. }
  266. else
  267. ret->upgrade = BuildingID::NONE;
  268. town.buildings[ret->bid] = ret;
  269. VLC->modh->identifiers.registerObject(source.meta, "building." + town.faction->identifier, ret->identifier, ret->bid);
  270. }
  271. void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
  272. {
  273. for(auto &node : source.Struct())
  274. {
  275. if (!node.second.isNull())
  276. {
  277. loadBuilding(town, node.first, node.second);
  278. }
  279. }
  280. }
  281. void CTownHandler::loadStructure(CTown &town, const std::string & stringID, const JsonNode & source)
  282. {
  283. auto ret = new CStructure;
  284. //Note: MODS COMPATIBILITY CODE
  285. ret->building = nullptr;
  286. ret->buildable = nullptr;
  287. VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->identifier, stringID, [=, &town](si32 identifier) mutable
  288. {
  289. ret->building = town.buildings[BuildingID(identifier)];
  290. });
  291. if (source["builds"].isNull())
  292. {
  293. VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->identifier, stringID, [=, &town](si32 identifier) mutable
  294. {
  295. ret->building = town.buildings[BuildingID(identifier)];
  296. });
  297. }
  298. else
  299. {
  300. if (source["builds"].getType() == JsonNode::DATA_FLOAT)
  301. {
  302. ret->buildable = town.buildings[BuildingID(source["builds"].Float())];
  303. }
  304. else
  305. {
  306. VLC->modh->identifiers.tryRequestIdentifier("building." + town.faction->identifier, source["builds"], [=, &town](si32 identifier) mutable
  307. {
  308. ret->buildable = town.buildings[BuildingID(identifier)];
  309. });
  310. }
  311. }
  312. ret->identifier = stringID;
  313. ret->pos.x = source["x"].Float();
  314. ret->pos.y = source["y"].Float();
  315. ret->pos.z = source["z"].Float();
  316. ret->hiddenUpgrade = source["hidden"].Bool();
  317. ret->defName = source["animation"].String();
  318. ret->borderName = source["border"].String();
  319. ret->areaName = source["area"].String();
  320. town.clientInfo.structures.push_back(ret);
  321. }
  322. void CTownHandler::loadStructures(CTown &town, const JsonNode & source)
  323. {
  324. for(auto &node : source.Struct())
  325. {
  326. if (!node.second.isNull())
  327. loadStructure(town, node.first, node.second);
  328. }
  329. }
  330. void CTownHandler::loadTownHall(CTown &town, const JsonNode & source)
  331. {
  332. auto & dstSlots = town.clientInfo.hallSlots;
  333. auto & srcSlots = source.Vector();
  334. dstSlots.resize(srcSlots.size());
  335. for(size_t i=0; i<dstSlots.size(); i++)
  336. {
  337. auto & dstRow = dstSlots[i];
  338. auto & srcRow = srcSlots[i].Vector();
  339. dstRow.resize(srcRow.size());
  340. for(size_t j=0; j < dstRow.size(); j++)
  341. {
  342. auto & dstBox = dstRow[j];
  343. auto & srcBox = srcRow[j].Vector();
  344. dstBox.resize(srcBox.size());
  345. for(size_t k=0; k<dstBox.size(); k++)
  346. {
  347. auto & dst = dstBox[k];
  348. auto & src = srcBox[k];
  349. //MODS COMPATIBILITY
  350. if (src.getType() == JsonNode::DATA_FLOAT)
  351. dst = BuildingID(src.Float());
  352. else
  353. {
  354. VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, src, [&](si32 identifier)
  355. {
  356. dst = BuildingID(identifier);
  357. });
  358. }
  359. }
  360. }
  361. }
  362. }
  363. CTown::ClientInfo::Point JsonToPoint(const JsonNode & node)
  364. {
  365. CTown::ClientInfo::Point ret;
  366. ret.x = node["x"].Float();
  367. ret.y = node["y"].Float();
  368. return ret;
  369. }
  370. void CTownHandler::loadSiegeScreen(CTown &town, const JsonNode & source)
  371. {
  372. town.clientInfo.siegePrefix = source["imagePrefix"].String();
  373. VLC->modh->identifiers.requestIdentifier("creature", source["shooter"], [&town](si32 creature)
  374. {
  375. town.clientInfo.siegeShooter = CreatureID(creature);
  376. });
  377. auto & pos = town.clientInfo.siegePositions;
  378. pos.resize(21);
  379. pos[8] = JsonToPoint(source["towers"]["top"]["tower"]);
  380. pos[17] = JsonToPoint(source["towers"]["top"]["battlement"]);
  381. pos[20] = JsonToPoint(source["towers"]["top"]["creature"]);
  382. pos[2] = JsonToPoint(source["towers"]["keep"]["tower"]);
  383. pos[15] = JsonToPoint(source["towers"]["keep"]["battlement"]);
  384. pos[18] = JsonToPoint(source["towers"]["keep"]["creature"]);
  385. pos[3] = JsonToPoint(source["towers"]["bottom"]["tower"]);
  386. pos[16] = JsonToPoint(source["towers"]["bottom"]["battlement"]);
  387. pos[19] = JsonToPoint(source["towers"]["bottom"]["creature"]);
  388. pos[9] = JsonToPoint(source["gate"]["gate"]);
  389. pos[10] = JsonToPoint(source["gate"]["arch"]);
  390. pos[7] = JsonToPoint(source["walls"]["upper"]);
  391. pos[6] = JsonToPoint(source["walls"]["upperMid"]);
  392. pos[5] = JsonToPoint(source["walls"]["bottomMid"]);
  393. pos[4] = JsonToPoint(source["walls"]["bottom"]);
  394. pos[13] = JsonToPoint(source["moat"]["moat"]);
  395. pos[14] = JsonToPoint(source["moat"]["bank"]);
  396. pos[11] = JsonToPoint(source["static"]["bottom"]);
  397. pos[12] = JsonToPoint(source["static"]["top"]);
  398. pos[1] = JsonToPoint(source["static"]["background"]);
  399. }
  400. static void readIcon(JsonNode source, std::string & small, std::string & large)
  401. {
  402. if (source.getType() == JsonNode::DATA_STRUCT) // don't crash on old format
  403. {
  404. small = source["small"].String();
  405. large = source["large"].String();
  406. }
  407. }
  408. void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
  409. {
  410. CTown::ClientInfo & info = town.clientInfo;
  411. readIcon(source["icons"]["village"]["normal"], info.iconSmall[0][0], info.iconLarge[0][0]);
  412. readIcon(source["icons"]["village"]["built"], info.iconSmall[0][1], info.iconLarge[0][1]);
  413. readIcon(source["icons"]["fort"]["normal"], info.iconSmall[1][0], info.iconLarge[1][0]);
  414. readIcon(source["icons"]["fort"]["built"], info.iconSmall[1][1], info.iconLarge[1][1]);
  415. info.hallBackground = source["hallBackground"].String();
  416. info.musicTheme = source["musicTheme"].String();
  417. info.townBackground = source["townBackground"].String();
  418. info.guildWindow = source["guildWindow"].String();
  419. info.buildingsIcons = source["buildingsIcons"].String();
  420. info.advMapVillage = source["adventureMap"]["village"].String();
  421. info.advMapCastle = source["adventureMap"]["castle"].String();
  422. info.advMapCapitol = source["adventureMap"]["capitol"].String();
  423. loadTownHall(town, source["hallSlots"]);
  424. loadStructures(town, source["structures"]);
  425. loadSiegeScreen(town, source["siege"]);
  426. }
  427. void CTownHandler::loadTown(CTown &town, const JsonNode & source)
  428. {
  429. auto resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String());
  430. if (resIter == std::end(GameConstants::RESOURCE_NAMES))
  431. town.primaryRes = Res::WOOD_AND_ORE; //Wood + Ore
  432. else
  433. town.primaryRes = resIter - std::begin(GameConstants::RESOURCE_NAMES);
  434. VLC->modh->identifiers.requestIdentifier("creature", source["warMachine"],
  435. [&town](si32 creature)
  436. {
  437. town.warMachine = CArtHandler::creatureToMachineID(CreatureID(creature));
  438. });
  439. town.moatDamage = source["moatDamage"].Float();
  440. town.mageLevel = source["mageGuild"].Float();
  441. town.names = source["names"].convertTo<std::vector<std::string> >();
  442. // Horde building creature level
  443. for(const JsonNode &node : source["horde"].Vector())
  444. town.hordeLvl[town.hordeLvl.size()] = node.Float();
  445. // town needs to have exactly 2 horde entries. Validation will take care of 2+ entries
  446. // but anything below 2 must be handled here
  447. for (size_t i=source["horde"].Vector().size(); i<2; i++)
  448. town.hordeLvl[i] = -1;
  449. const JsonVector & creatures = source["creatures"].Vector();
  450. town.creatures.resize(creatures.size());
  451. for (size_t i=0; i< creatures.size(); i++)
  452. {
  453. const JsonVector & level = creatures[i].Vector();
  454. town.creatures[i].resize(level.size());
  455. for (size_t j=0; j<level.size(); j++)
  456. {
  457. VLC->modh->identifiers.requestIdentifier("creature", level[j], [=, &town](si32 creature)
  458. {
  459. town.creatures[i][j] = CreatureID(creature);
  460. });
  461. }
  462. }
  463. town.defaultTavernChance = source["defaultTavern"].Float();
  464. /// set chance of specific hero class to appear in this town
  465. for(auto &node : source["tavern"].Struct())
  466. {
  467. int chance = node.second.Float();
  468. VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=, &town](si32 classID)
  469. {
  470. VLC->heroh->classes.heroClasses[classID]->selectionProbability[town.faction->index] = chance;
  471. });
  472. }
  473. for(auto &node : source["guildSpells"].Struct())
  474. {
  475. int chance = node.second.Float();
  476. VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=, &town](si32 spellID)
  477. {
  478. SpellID(spellID).toSpell()->probabilities[town.faction->index] = chance;
  479. });
  480. }
  481. for (const JsonNode &d : source["adventureMap"]["dwellings"].Vector())
  482. {
  483. town.dwellings.push_back (d["graphics"].String());
  484. town.dwellingNames.push_back (d["name"].String());
  485. }
  486. loadBuildings(town, source["buildings"]);
  487. loadClientData(town,source);
  488. }
  489. void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
  490. {
  491. faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
  492. std::string prefix = source["prefix"].String();
  493. for(const JsonNode &piece : source["pieces"].Vector())
  494. {
  495. size_t index = faction.puzzleMap.size();
  496. SPuzzleInfo spi;
  497. spi.x = piece["x"].Float();
  498. spi.y = piece["y"].Float();
  499. spi.whenUncovered = piece["index"].Float();
  500. spi.number = index;
  501. // filename calculation
  502. std::ostringstream suffix;
  503. suffix << std::setfill('0') << std::setw(2) << index;
  504. spi.filename = prefix + suffix.str();
  505. faction.puzzleMap.push_back(spi);
  506. }
  507. assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
  508. }
  509. CFaction * CTownHandler::loadFromJson(const JsonNode &source, std::string identifier)
  510. {
  511. auto faction = new CFaction();
  512. faction->name = source["name"].String();
  513. faction->identifier = identifier;
  514. VLC->modh->identifiers.requestIdentifier ("creature", source["commander"],
  515. [=](si32 commanderID)
  516. {
  517. faction->commander = CreatureID(commanderID);
  518. });
  519. faction->creatureBg120 = source["creatureBackground"]["120px"].String();
  520. faction->creatureBg130 = source["creatureBackground"]["130px"].String();
  521. faction->nativeTerrain = ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES,
  522. source["nativeTerrain"].String()));
  523. int alignment = vstd::find_pos(EAlignment::names, source["alignment"].String());
  524. if (alignment == -1)
  525. faction->alignment = EAlignment::NEUTRAL;
  526. else
  527. faction->alignment = static_cast<EAlignment::EAlignment>(alignment);
  528. if (!source["town"].isNull())
  529. {
  530. faction->town = new CTown;
  531. faction->town->faction = faction;
  532. loadTown(*faction->town, source["town"]);
  533. }
  534. else
  535. faction->town = nullptr;
  536. if (!source["puzzleMap"].isNull())
  537. loadPuzzle(*faction, source["puzzleMap"]);
  538. return faction;
  539. }
  540. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  541. {
  542. auto object = loadFromJson(data, name);
  543. object->index = factions.size();
  544. if (object->town)
  545. {
  546. auto & info = object->town->clientInfo;
  547. info.icons[0][0] = 8 + object->index * 4 + 0;
  548. info.icons[0][1] = 8 + object->index * 4 + 1;
  549. info.icons[1][0] = 8 + object->index * 4 + 2;
  550. info.icons[1][1] = 8 + object->index * 4 + 3;
  551. }
  552. factions.push_back(object);
  553. VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
  554. }
  555. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  556. {
  557. auto object = loadFromJson(data, name);
  558. object->index = index;
  559. if (object->town)
  560. {
  561. auto & info = object->town->clientInfo;
  562. info.icons[0][0] = (GameConstants::F_NUMBER + object->index) * 2 + 0;
  563. info.icons[0][1] = (GameConstants::F_NUMBER + object->index) * 2 + 1;
  564. info.icons[1][0] = object->index * 2 + 0;
  565. info.icons[1][1] = object->index * 2 + 1;
  566. }
  567. assert(factions[index] == nullptr); // ensure that this id was not loaded before
  568. factions[index] = object;
  569. VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
  570. }
  571. void CTownHandler::afterLoadFinalization()
  572. {
  573. initializeRequirements();
  574. }
  575. void CTownHandler::initializeRequirements()
  576. {
  577. // must be done separately after all ID's are known
  578. for (auto & requirement : requirementsToLoad)
  579. {
  580. requirement.building->requirements = CBuilding::TRequired(requirement.json, [&](const JsonNode & node)
  581. {
  582. return BuildingID(VLC->modh->identifiers.getIdentifier("building." + requirement.faction->identifier, node.Vector()[0]).get());
  583. });
  584. }
  585. requirementsToLoad.clear();
  586. }
  587. std::vector<bool> CTownHandler::getDefaultAllowed() const
  588. {
  589. std::vector<bool> allowedFactions;
  590. for(auto town : factions)
  591. {
  592. allowedFactions.push_back(town->town != nullptr);
  593. }
  594. return allowedFactions;
  595. }