CTownHandler.cpp 22 KB

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