CTownHandler.cpp 17 KB

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