CTownHandler.cpp 20 KB

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