CTownHandler.cpp 22 KB

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