CTownHandler.cpp 24 KB

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