CTownHandler.cpp 20 KB

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