CTownHandler.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. #include "CDefObjInfoHandler.h"
  14. #include "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. if (source.Vector()[0].getType() == JsonNode::DATA_FLOAT)
  230. {
  231. // MODS COMPATIBILITY
  232. CBuilding::TRequired::OperatorAll required;
  233. for(const JsonNode &building : source.Vector())
  234. required.expressions.push_back(BuildingID(building.Float()));
  235. building.requirements = CBuilding::TRequired(required);
  236. }
  237. else
  238. {
  239. BuildingRequirementsHelper hlp;
  240. hlp.building = &building;
  241. hlp.faction = town.faction;
  242. hlp.json = source;
  243. requirementsToLoad.push_back(hlp);
  244. }
  245. }
  246. void CTownHandler::loadBuilding(CTown &town, const std::string & stringID, const JsonNode & source)
  247. {
  248. auto ret = new CBuilding;
  249. static const std::string modes [] = {"normal", "auto", "special", "grail"};
  250. ret->mode = static_cast<CBuilding::EBuildMode>(boost::find(modes, source["mode"].String()) - modes);
  251. ret->identifier = stringID;
  252. ret->town = &town;
  253. ret->bid = BuildingID(source["id"].Float());
  254. ret->name = source["name"].String();
  255. ret->description = source["description"].String();
  256. ret->resources = TResources(source["cost"]);
  257. loadBuildingRequirements(town, *ret, source["requires"]);
  258. if (!source["upgrades"].isNull())
  259. {
  260. //MODS COMPATIBILITY
  261. if (source["upgrades"].getType() == JsonNode::DATA_FLOAT)
  262. ret->upgrade = BuildingID(source["upgrades"].Float());
  263. else
  264. {
  265. // building id and upgrades can't be the same
  266. if(stringID == source["upgrades"].String())
  267. {
  268. throw std::runtime_error(boost::str(boost::format("Building with ID '%s' of town '%s' can't be an upgrade of the same building.") %
  269. stringID % town.faction->name));
  270. }
  271. VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, source["upgrades"], [=](si32 identifier)
  272. {
  273. ret->upgrade = BuildingID(identifier);
  274. });
  275. }
  276. }
  277. else
  278. ret->upgrade = BuildingID::NONE;
  279. town.buildings[ret->bid] = ret;
  280. VLC->modh->identifiers.registerObject(source.meta, "building." + town.faction->identifier, ret->identifier, ret->bid);
  281. }
  282. void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
  283. {
  284. for(auto &node : source.Struct())
  285. {
  286. if (!node.second.isNull())
  287. {
  288. loadBuilding(town, node.first, node.second);
  289. }
  290. }
  291. }
  292. void CTownHandler::loadStructure(CTown &town, const std::string & stringID, const JsonNode & source)
  293. {
  294. auto ret = new CStructure;
  295. //Note: MODS COMPATIBILITY CODE
  296. ret->building = nullptr;
  297. ret->buildable = nullptr;
  298. VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->identifier, stringID, [=, &town](si32 identifier) mutable
  299. {
  300. ret->building = town.buildings[BuildingID(identifier)];
  301. });
  302. if (source["builds"].isNull())
  303. {
  304. VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->identifier, stringID, [=, &town](si32 identifier) mutable
  305. {
  306. ret->building = town.buildings[BuildingID(identifier)];
  307. });
  308. }
  309. else
  310. {
  311. if (source["builds"].getType() == JsonNode::DATA_FLOAT)
  312. {
  313. ret->buildable = town.buildings[BuildingID(source["builds"].Float())];
  314. }
  315. else
  316. {
  317. VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, source["builds"], [=, &town](si32 identifier) mutable
  318. {
  319. ret->buildable = town.buildings[BuildingID(identifier)];
  320. });
  321. }
  322. }
  323. ret->identifier = stringID;
  324. ret->pos.x = source["x"].Float();
  325. ret->pos.y = source["y"].Float();
  326. ret->pos.z = source["z"].Float();
  327. ret->hiddenUpgrade = source["hidden"].Bool();
  328. ret->defName = source["animation"].String();
  329. ret->borderName = source["border"].String();
  330. ret->areaName = source["area"].String();
  331. town.clientInfo.structures.push_back(ret);
  332. }
  333. void CTownHandler::loadStructures(CTown &town, const JsonNode & source)
  334. {
  335. for(auto &node : source.Struct())
  336. {
  337. if (!node.second.isNull())
  338. loadStructure(town, node.first, node.second);
  339. }
  340. }
  341. void CTownHandler::loadTownHall(CTown &town, const JsonNode & source)
  342. {
  343. auto & dstSlots = town.clientInfo.hallSlots;
  344. auto & srcSlots = source.Vector();
  345. dstSlots.resize(srcSlots.size());
  346. for(size_t i=0; i<dstSlots.size(); i++)
  347. {
  348. auto & dstRow = dstSlots[i];
  349. auto & srcRow = srcSlots[i].Vector();
  350. dstRow.resize(srcRow.size());
  351. for(size_t j=0; j < dstRow.size(); j++)
  352. {
  353. auto & dstBox = dstRow[j];
  354. auto & srcBox = srcRow[j].Vector();
  355. dstBox.resize(srcBox.size());
  356. for(size_t k=0; k<dstBox.size(); k++)
  357. {
  358. auto & dst = dstBox[k];
  359. auto & src = srcBox[k];
  360. //MODS COMPATIBILITY
  361. if (src.getType() == JsonNode::DATA_FLOAT)
  362. dst = BuildingID(src.Float());
  363. else
  364. {
  365. VLC->modh->identifiers.requestIdentifier("building." + town.faction->identifier, src, [&](si32 identifier)
  366. {
  367. dst = BuildingID(identifier);
  368. });
  369. }
  370. }
  371. }
  372. }
  373. }
  374. CTown::ClientInfo::Point JsonToPoint(const JsonNode & node)
  375. {
  376. CTown::ClientInfo::Point ret;
  377. ret.x = node["x"].Float();
  378. ret.y = node["y"].Float();
  379. return ret;
  380. }
  381. void CTownHandler::loadSiegeScreen(CTown &town, const JsonNode & source)
  382. {
  383. town.clientInfo.siegePrefix = source["imagePrefix"].String();
  384. VLC->modh->identifiers.requestIdentifier("creature", source["shooter"], [&town](si32 creature)
  385. {
  386. town.clientInfo.siegeShooter = CreatureID(creature);
  387. });
  388. auto & pos = town.clientInfo.siegePositions;
  389. pos.resize(21);
  390. pos[8] = JsonToPoint(source["towers"]["top"]["tower"]);
  391. pos[17] = JsonToPoint(source["towers"]["top"]["battlement"]);
  392. pos[20] = JsonToPoint(source["towers"]["top"]["creature"]);
  393. pos[2] = JsonToPoint(source["towers"]["keep"]["tower"]);
  394. pos[15] = JsonToPoint(source["towers"]["keep"]["battlement"]);
  395. pos[18] = JsonToPoint(source["towers"]["keep"]["creature"]);
  396. pos[3] = JsonToPoint(source["towers"]["bottom"]["tower"]);
  397. pos[16] = JsonToPoint(source["towers"]["bottom"]["battlement"]);
  398. pos[19] = JsonToPoint(source["towers"]["bottom"]["creature"]);
  399. pos[9] = JsonToPoint(source["gate"]["gate"]);
  400. pos[10] = JsonToPoint(source["gate"]["arch"]);
  401. pos[7] = JsonToPoint(source["walls"]["upper"]);
  402. pos[6] = JsonToPoint(source["walls"]["upperMid"]);
  403. pos[5] = JsonToPoint(source["walls"]["bottomMid"]);
  404. pos[4] = JsonToPoint(source["walls"]["bottom"]);
  405. pos[13] = JsonToPoint(source["moat"]["moat"]);
  406. pos[14] = JsonToPoint(source["moat"]["bank"]);
  407. pos[11] = JsonToPoint(source["static"]["bottom"]);
  408. pos[12] = JsonToPoint(source["static"]["top"]);
  409. pos[1] = JsonToPoint(source["static"]["background"]);
  410. }
  411. static void readIcon(JsonNode source, std::string & small, std::string & large)
  412. {
  413. if (source.getType() == JsonNode::DATA_STRUCT) // don't crash on old format
  414. {
  415. small = source["small"].String();
  416. large = source["large"].String();
  417. }
  418. }
  419. void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
  420. {
  421. CTown::ClientInfo & info = town.clientInfo;
  422. readIcon(source["icons"]["village"]["normal"], info.iconSmall[0][0], info.iconLarge[0][0]);
  423. readIcon(source["icons"]["village"]["built"], info.iconSmall[0][1], info.iconLarge[0][1]);
  424. readIcon(source["icons"]["fort"]["normal"], info.iconSmall[1][0], info.iconLarge[1][0]);
  425. readIcon(source["icons"]["fort"]["built"], info.iconSmall[1][1], info.iconLarge[1][1]);
  426. info.hallBackground = source["hallBackground"].String();
  427. info.musicTheme = source["musicTheme"].String();
  428. info.townBackground = source["townBackground"].String();
  429. info.guildWindow = source["guildWindow"].String();
  430. info.buildingsIcons = source["buildingsIcons"].String();
  431. //left for back compatibility - will be removed later
  432. if (source["guildBackground"].String() != "")
  433. info.guildBackground = source["guildBackground"].String();
  434. else
  435. info.guildBackground = "TPMAGE.bmp";
  436. if (source["tavernVideo"].String() != "")
  437. info.tavernVideo = source["tavernVideo"].String();
  438. else
  439. info.tavernVideo = "TAVERN.BIK";
  440. //end of legacy assignment
  441. info.advMapVillage = source["adventureMap"]["village"].String();
  442. info.advMapCastle = source["adventureMap"]["castle"].String();
  443. info.advMapCapitol = source["adventureMap"]["capitol"].String();
  444. loadTownHall(town, source["hallSlots"]);
  445. loadStructures(town, source["structures"]);
  446. loadSiegeScreen(town, source["siege"]);
  447. }
  448. void CTownHandler::loadTown(CTown &town, const JsonNode & source)
  449. {
  450. auto resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String());
  451. if (resIter == std::end(GameConstants::RESOURCE_NAMES))
  452. town.primaryRes = Res::WOOD_AND_ORE; //Wood + Ore
  453. else
  454. town.primaryRes = resIter - std::begin(GameConstants::RESOURCE_NAMES);
  455. VLC->modh->identifiers.requestIdentifier("creature", source["warMachine"],
  456. [&town](si32 creature)
  457. {
  458. town.warMachine = CArtHandler::creatureToMachineID(CreatureID(creature));
  459. });
  460. town.moatDamage = source["moatDamage"].Float();
  461. town.mageLevel = source["mageGuild"].Float();
  462. town.names = source["names"].convertTo<std::vector<std::string> >();
  463. // Horde building creature level
  464. for(const JsonNode &node : source["horde"].Vector())
  465. town.hordeLvl[town.hordeLvl.size()] = node.Float();
  466. // town needs to have exactly 2 horde entries. Validation will take care of 2+ entries
  467. // but anything below 2 must be handled here
  468. for (size_t i=source["horde"].Vector().size(); i<2; i++)
  469. town.hordeLvl[i] = -1;
  470. const JsonVector & creatures = source["creatures"].Vector();
  471. town.creatures.resize(creatures.size());
  472. for (size_t i=0; i< creatures.size(); i++)
  473. {
  474. const JsonVector & level = creatures[i].Vector();
  475. town.creatures[i].resize(level.size());
  476. for (size_t j=0; j<level.size(); j++)
  477. {
  478. VLC->modh->identifiers.requestIdentifier("creature", level[j], [=, &town](si32 creature)
  479. {
  480. town.creatures[i][j] = CreatureID(creature);
  481. });
  482. }
  483. }
  484. town.defaultTavernChance = source["defaultTavern"].Float();
  485. /// set chance of specific hero class to appear in this town
  486. for(auto &node : source["tavern"].Struct())
  487. {
  488. int chance = node.second.Float();
  489. VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=, &town](si32 classID)
  490. {
  491. VLC->heroh->classes.heroClasses[classID]->selectionProbability[town.faction->index] = chance;
  492. });
  493. }
  494. for(auto &node : source["guildSpells"].Struct())
  495. {
  496. int chance = node.second.Float();
  497. VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=, &town](si32 spellID)
  498. {
  499. SpellID(spellID).toSpell()->probabilities[town.faction->index] = chance;
  500. });
  501. }
  502. for (const JsonNode &d : source["adventureMap"]["dwellings"].Vector())
  503. {
  504. town.dwellings.push_back (d["graphics"].String());
  505. town.dwellingNames.push_back (d["name"].String());
  506. }
  507. loadBuildings(town, source["buildings"]);
  508. loadClientData(town,source);
  509. }
  510. void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
  511. {
  512. faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
  513. std::string prefix = source["prefix"].String();
  514. for(const JsonNode &piece : source["pieces"].Vector())
  515. {
  516. size_t index = faction.puzzleMap.size();
  517. SPuzzleInfo spi;
  518. spi.x = piece["x"].Float();
  519. spi.y = piece["y"].Float();
  520. spi.whenUncovered = piece["index"].Float();
  521. spi.number = index;
  522. // filename calculation
  523. std::ostringstream suffix;
  524. suffix << std::setfill('0') << std::setw(2) << index;
  525. spi.filename = prefix + suffix.str();
  526. faction.puzzleMap.push_back(spi);
  527. }
  528. assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
  529. }
  530. CFaction * CTownHandler::loadFromJson(const JsonNode &source, std::string identifier)
  531. {
  532. auto faction = new CFaction();
  533. faction->name = source["name"].String();
  534. faction->identifier = identifier;
  535. //FIXME: MODS COMPATIBILITY
  536. if (!source["commander"].isNull())
  537. {
  538. VLC->modh->identifiers.requestIdentifier ("creature", source["commander"],
  539. [=](si32 commanderID)
  540. {
  541. for (auto ptr : VLC->heroh->classes.heroClasses)
  542. {
  543. if (ptr->commander == nullptr && ptr->faction == faction->index)
  544. ptr->commander = VLC->creh->creatures[commanderID];
  545. }
  546. });
  547. }
  548. faction->creatureBg120 = source["creatureBackground"]["120px"].String();
  549. faction->creatureBg130 = source["creatureBackground"]["130px"].String();
  550. faction->nativeTerrain = ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES,
  551. source["nativeTerrain"].String()));
  552. int alignment = vstd::find_pos(EAlignment::names, source["alignment"].String());
  553. if (alignment == -1)
  554. faction->alignment = EAlignment::NEUTRAL;
  555. else
  556. faction->alignment = static_cast<EAlignment::EAlignment>(alignment);
  557. if (!source["town"].isNull())
  558. {
  559. faction->town = new CTown;
  560. faction->town->faction = faction;
  561. loadTown(*faction->town, source["town"]);
  562. }
  563. else
  564. faction->town = nullptr;
  565. if (!source["puzzleMap"].isNull())
  566. loadPuzzle(*faction, source["puzzleMap"]);
  567. return faction;
  568. }
  569. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  570. {
  571. auto object = loadFromJson(data, name);
  572. object->index = factions.size();
  573. if (object->town)
  574. {
  575. auto & info = object->town->clientInfo;
  576. info.icons[0][0] = 8 + object->index * 4 + 0;
  577. info.icons[0][1] = 8 + object->index * 4 + 1;
  578. info.icons[1][0] = 8 + object->index * 4 + 2;
  579. info.icons[1][1] = 8 + object->index * 4 + 3;
  580. }
  581. factions.push_back(object);
  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. if (object->town)
  589. {
  590. auto & info = object->town->clientInfo;
  591. info.icons[0][0] = (GameConstants::F_NUMBER + object->index) * 2 + 0;
  592. info.icons[0][1] = (GameConstants::F_NUMBER + object->index) * 2 + 1;
  593. info.icons[1][0] = object->index * 2 + 0;
  594. info.icons[1][1] = object->index * 2 + 1;
  595. }
  596. assert(factions[index] == nullptr); // ensure that this id was not loaded before
  597. factions[index] = object;
  598. VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
  599. }
  600. void CTownHandler::afterLoadFinalization()
  601. {
  602. initializeRequirements();
  603. ObjectTemplate base = VLC->dobjinfo->pickCandidates(Obj::TOWN, 0).front();
  604. for (CFaction * fact : factions)
  605. {
  606. if (fact->town)
  607. {
  608. base.animationFile = fact->town->clientInfo.advMapCastle;
  609. base.subid = fact->index;
  610. // replace existing (if any) and add new template.
  611. // Necessary for objects added via mods that don't have any templates in H3
  612. VLC->dobjinfo->eraseAll(Obj::TOWN, fact->index);
  613. VLC->dobjinfo->registerTemplate(base);
  614. assert(fact->town->dwellings.size() == fact->town->dwellingNames.size());
  615. for (size_t i=0; i<fact->town->dwellings.size(); i++)
  616. {
  617. ObjectTemplate base = VLC->dobjinfo->pickCandidates(Obj::CREATURE_GENERATOR1, 0).front();
  618. //both unupgraded and upgraded get same dwelling
  619. for (auto cre : fact->town->creatures[i])
  620. {
  621. base.subid = 80 + cre;
  622. base.animationFile = fact->town->dwellings[i];
  623. if (VLC->objh->cregens.count(cre) == 0)
  624. {
  625. VLC->dobjinfo->registerTemplate(base);
  626. VLC->objh->cregens[80 + cre] = cre; //map of dwelling -> creature id
  627. }
  628. }
  629. }
  630. }
  631. }
  632. }
  633. void CTownHandler::initializeRequirements()
  634. {
  635. // must be done separately after all ID's are known
  636. for (auto & requirement : requirementsToLoad)
  637. {
  638. requirement.building->requirements = CBuilding::TRequired(requirement.json, [&](const JsonNode & node)
  639. {
  640. return BuildingID(VLC->modh->identifiers.getIdentifier("building." + requirement.faction->identifier, node.Vector()[0]).get());
  641. });
  642. }
  643. requirementsToLoad.clear();
  644. }
  645. std::vector<bool> CTownHandler::getDefaultAllowed() const
  646. {
  647. std::vector<bool> allowedFactions;
  648. for(auto town : factions)
  649. {
  650. allowedFactions.push_back(town->town != nullptr);
  651. }
  652. return allowedFactions;
  653. }