CTownHandler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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/CResourceLoader.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[build->upgrade];
  35. return build->bid;
  36. }
  37. si32 CBuilding::getDistance(BuildingID buildID) const
  38. {
  39. const CBuilding * build = town->buildings[buildID];
  40. int distance = 0;
  41. while (build->upgrade >= 0 && build != this)
  42. {
  43. build = build->town->buildings[build->upgrade];
  44. distance++;
  45. }
  46. if (build == this)
  47. return distance;
  48. return -1;
  49. }
  50. CFaction::CFaction()
  51. {
  52. }
  53. CFaction::~CFaction()
  54. {
  55. delete town;
  56. }
  57. CTown::CTown()
  58. {
  59. }
  60. CTown::~CTown()
  61. {
  62. BOOST_FOREACH(auto & build, buildings)
  63. build.second.dellNull();
  64. BOOST_FOREACH(auto & str, clientInfo.structures)
  65. str.dellNull();
  66. }
  67. CTownHandler::CTownHandler()
  68. {
  69. VLC->townh = this;
  70. }
  71. CTownHandler::~CTownHandler()
  72. {
  73. BOOST_FOREACH(auto faction, factions)
  74. faction.dellNull();
  75. }
  76. JsonNode readBuilding(CLegacyConfigParser & parser)
  77. {
  78. JsonNode ret;
  79. JsonNode & cost = ret["cost"];
  80. //note: this code will try to parse mithril as well but wil always return 0 for it
  81. BOOST_FOREACH(const std::string & resID, GameConstants::RESOURCE_NAMES)
  82. cost[resID].Float() = parser.readNumber();
  83. cost.Struct().erase("mithril"); // erase mithril to avoid confusing validator
  84. parser.endLine();
  85. return ret;
  86. }
  87. std::vector<JsonNode> CTownHandler::loadLegacyData(size_t dataSize)
  88. {
  89. std::vector<JsonNode> dest(dataSize);
  90. factions.resize(dataSize);
  91. auto getBuild = [&](size_t town, size_t building) -> JsonNode &
  92. {
  93. return dest[town]["town"]["buildings"][EBuildingType::names[building]];
  94. };
  95. CLegacyConfigParser parser("DATA/BUILDING.TXT");
  96. parser.endLine(); // header
  97. parser.endLine();
  98. //Unique buildings
  99. for (size_t town=0; town<dataSize; town++)
  100. {
  101. parser.endLine(); //header
  102. parser.endLine();
  103. int buildID = 17;
  104. do
  105. {
  106. getBuild(town, buildID) = readBuilding(parser);
  107. buildID++;
  108. }
  109. while (!parser.isNextEntryEmpty());
  110. }
  111. // Common buildings
  112. parser.endLine(); // header
  113. parser.endLine();
  114. parser.endLine();
  115. int buildID = 0;
  116. do
  117. {
  118. JsonNode building = readBuilding(parser);
  119. for (size_t town=0; town<dataSize; town++)
  120. getBuild(town, buildID) = building;
  121. buildID++;
  122. }
  123. while (!parser.isNextEntryEmpty());
  124. parser.endLine(); //header
  125. parser.endLine();
  126. //Dwellings
  127. for (size_t town=0; town<dataSize; town++)
  128. {
  129. parser.endLine(); //header
  130. parser.endLine();
  131. for (size_t i=0; i<14; i++)
  132. {
  133. getBuild(town, 30+i) = readBuilding(parser);
  134. }
  135. }
  136. {
  137. CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
  138. for(int building=0; building<15; building++)
  139. {
  140. std::string name = parser.readString();
  141. std::string descr = parser.readString();
  142. parser.endLine();
  143. for(int j=0; j<dataSize; j++)
  144. {
  145. getBuild(j, building)["name"].String() = name;
  146. getBuild(j, building)["description"].String() = descr;
  147. }
  148. }
  149. parser.endLine(); // silo
  150. parser.endLine(); // blacksmith //unused entries
  151. parser.endLine(); // moat
  152. //shipyard with the ship
  153. std::string name = parser.readString();
  154. std::string descr = parser.readString();
  155. parser.endLine();
  156. for(int town=0; town<dataSize; town++)
  157. {
  158. getBuild(town, 20)["name"].String() = name;
  159. getBuild(town, 20)["description"].String() = descr;
  160. }
  161. //blacksmith
  162. for(int town=0; town<dataSize; town++)
  163. {
  164. getBuild(town, 16)["name"].String() = parser.readString();
  165. getBuild(town, 16)["description"].String() = parser.readString();
  166. parser.endLine();
  167. }
  168. }
  169. {
  170. CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
  171. for(int town=0; town<dataSize; town++)
  172. {
  173. for(int build=0; build<9; build++)
  174. {
  175. getBuild(town, 17 + build)["name"].String() = parser.readString();
  176. getBuild(town, 17 + build)["description"].String() = parser.readString();
  177. parser.endLine();
  178. }
  179. getBuild(town, 26)["name"].String() = parser.readString(); // Grail
  180. getBuild(town, 26)["description"].String() = parser.readString();
  181. parser.endLine();
  182. getBuild(town, 15)["name"].String() = parser.readString(); // Resource silo
  183. getBuild(town, 15)["description"].String() = parser.readString();
  184. parser.endLine();
  185. }
  186. }
  187. {
  188. CLegacyConfigParser parser("DATA/DWELLING.TXT");
  189. for(int town=0; town<dataSize; town++)
  190. {
  191. for(int build=0; build<14; build++)
  192. {
  193. getBuild(town, 30 + build)["name"].String() = parser.readString();
  194. getBuild(town, 30 + build)["description"].String() = parser.readString();
  195. parser.endLine();
  196. }
  197. }
  198. }
  199. {
  200. CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
  201. CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
  202. size_t townID=0;
  203. do
  204. {
  205. dest[townID]["name"].String() = typeParser.readString();
  206. for (int i=0; i<NAMES_PER_TOWN; i++)
  207. {
  208. JsonNode name;
  209. name.String() = nameParser.readString();
  210. dest[townID]["town"]["names"].Vector().push_back(name);
  211. nameParser.endLine();
  212. }
  213. townID++;
  214. }
  215. while (typeParser.endLine());
  216. }
  217. return dest;
  218. }
  219. void CTownHandler::loadBuilding(CTown &town, const JsonNode & source)
  220. {
  221. CBuilding * ret = new CBuilding;
  222. static const std::string modes [] = {"normal", "auto", "special", "grail"};
  223. ret->mode = static_cast<CBuilding::EBuildMode>(boost::find(modes, source["mode"].String()) - modes);
  224. ret->town = &town;
  225. ret->bid = BuildingID(source["id"].Float());
  226. ret->name = source["name"].String();
  227. ret->description = source["description"].String();
  228. ret->resources = TResources(source["cost"]);
  229. BOOST_FOREACH(const JsonNode &building, source["requires"].Vector())
  230. ret->requirements.insert(BuildingID(building.Float()));
  231. if (!source["upgrades"].isNull())
  232. {
  233. ret->requirements.insert(BuildingID(source["upgrades"].Float()));
  234. ret->upgrade = BuildingID(source["upgrades"].Float());
  235. }
  236. else
  237. ret->upgrade = BuildingID::NONE;
  238. town.buildings[ret->bid] = ret;
  239. }
  240. void CTownHandler::loadBuildings(CTown &town, const JsonNode & source)
  241. {
  242. if (source.getType() == JsonNode::DATA_VECTOR)
  243. {
  244. BOOST_FOREACH(auto &node, source.Vector())
  245. {
  246. if (!node.isNull())
  247. loadBuilding(town, node);
  248. }
  249. }
  250. else
  251. {
  252. BOOST_FOREACH(auto &node, source.Struct())
  253. {
  254. if (!node.second.isNull())
  255. loadBuilding(town, node.second);
  256. }
  257. }
  258. }
  259. void CTownHandler::loadStructure(CTown &town, const JsonNode & source)
  260. {
  261. CStructure * ret = new CStructure;
  262. if (source["id"].isNull())
  263. {
  264. ret->building = nullptr;
  265. ret->buildable = nullptr;
  266. }
  267. else
  268. {
  269. ret->building = town.buildings[BuildingID(source["id"].Float())];
  270. if (source["builds"].isNull())
  271. ret->buildable = ret->building;
  272. else
  273. ret->buildable = town.buildings[BuildingID(source["builds"].Float())];
  274. }
  275. ret->pos.x = source["x"].Float();
  276. ret->pos.y = source["y"].Float();
  277. ret->pos.z = source["z"].Float();
  278. ret->hiddenUpgrade = source["hidden"].Bool();
  279. ret->defName = source["animation"].String();
  280. ret->borderName = source["border"].String();
  281. ret->areaName = source["area"].String();
  282. town.clientInfo.structures.push_back(ret);
  283. }
  284. void CTownHandler::loadStructures(CTown &town, const JsonNode & source)
  285. {
  286. if (source.getType() == JsonNode::DATA_VECTOR)
  287. {
  288. BOOST_FOREACH(auto &node, source.Vector())
  289. {
  290. if (!node.isNull())
  291. loadStructure(town, node);
  292. }
  293. }
  294. else
  295. {
  296. BOOST_FOREACH(auto &node, source.Struct())
  297. {
  298. if (!node.second.isNull())
  299. loadStructure(town, node.second);
  300. }
  301. }
  302. }
  303. void CTownHandler::loadTownHall(CTown &town, const JsonNode & source)
  304. {
  305. BOOST_FOREACH(const JsonNode &row, source.Vector())
  306. {
  307. std::vector< std::vector<BuildingID> > hallRow;
  308. BOOST_FOREACH(const JsonNode &box, row.Vector())
  309. {
  310. std::vector<BuildingID> hallBox;
  311. BOOST_FOREACH(const JsonNode &value, box.Vector())
  312. {
  313. hallBox.push_back(BuildingID(value.Float()));
  314. }
  315. hallRow.push_back(hallBox);
  316. }
  317. town.clientInfo.hallSlots.push_back(hallRow);
  318. }
  319. }
  320. CTown::ClientInfo::Point JsonToPoint(const JsonNode & node)
  321. {
  322. CTown::ClientInfo::Point ret;
  323. ret.x = node["x"].Float();
  324. ret.y = node["y"].Float();
  325. return ret;
  326. }
  327. void CTownHandler::loadSiegeScreen(CTown &town, const JsonNode & source)
  328. {
  329. town.clientInfo.siegePrefix = source["imagePrefix"].String();
  330. VLC->modh->identifiers.requestIdentifier(std::string("creature.") + source["shooter"].String(), [&town](si32 creature)
  331. {
  332. town.clientInfo.siegeShooter = CreatureID(creature);
  333. });
  334. town.clientInfo.siegeShooterCropHeight = source["shooterHeight"].Float();
  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. void CTownHandler::loadClientData(CTown &town, const JsonNode & source)
  359. {
  360. town.clientInfo.icons[0][0] = source["icons"]["village"]["normal"].Float();
  361. town.clientInfo.icons[0][1] = source["icons"]["village"]["built"].Float();
  362. town.clientInfo.icons[1][0] = source["icons"]["fort"]["normal"].Float();
  363. town.clientInfo.icons[1][1] = source["icons"]["fort"]["built"].Float();
  364. town.clientInfo.hallBackground = source["hallBackground"].String();
  365. town.clientInfo.musicTheme = source["musicTheme"].String();
  366. town.clientInfo.townBackground = source["townBackground"].String();
  367. town.clientInfo.guildWindow = source["guildWindow"].String();
  368. town.clientInfo.buildingsIcons = source["buildingsIcons"].String();
  369. town.clientInfo.advMapVillage = source["adventureMap"]["village"].String();
  370. town.clientInfo.advMapCastle = source["adventureMap"]["castle"].String();
  371. town.clientInfo.advMapCapitol = source["adventureMap"]["capitol"].String();
  372. const JsonNode *value = &source["adventureMap"]["dwellings"];
  373. if (!value->isNull())
  374. {
  375. BOOST_FOREACH (const JsonNode &d, value->Vector())
  376. {
  377. town.dwellings.push_back (d["graphics"].String());
  378. town.dwellingNames.push_back (d["name"].String());
  379. }
  380. }
  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 == boost::end(GameConstants::RESOURCE_NAMES))
  389. town.primaryRes = Res::WOOD_AND_ORE; //Wood + Ore
  390. else
  391. town.primaryRes = resIter - boost::begin(GameConstants::RESOURCE_NAMES);
  392. VLC->modh->identifiers.requestIdentifier(std::string("creature." + source["warMachine"].String()),
  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. BOOST_FOREACH(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(std::string("creature.") + level[j].String(), [=, &town](si32 creature)
  414. {
  415. town.creatures[i][j] = CreatureID(creature);
  416. });
  417. }
  418. }
  419. /// set chance of specific hero class to appear in this town
  420. BOOST_FOREACH(auto &node, source["tavern"].Struct())
  421. {
  422. int chance = node.second.Float();
  423. VLC->modh->identifiers.requestIdentifier("heroClass." + node.first, [=, &town](si32 classID)
  424. {
  425. VLC->heroh->classes.heroClasses[classID]->selectionProbability[town.faction->index] = chance;
  426. });
  427. }
  428. BOOST_FOREACH(auto &node, source["guildSpells"].Struct())
  429. {
  430. int chance = node.second.Float();
  431. VLC->modh->identifiers.requestIdentifier("spell." + node.first, [=, &town](si32 spellID)
  432. {
  433. SpellID(spellID).toSpell()->probabilities[town.faction->index] = chance;
  434. });
  435. }
  436. loadBuildings(town, source["buildings"]);
  437. loadClientData(town,source);
  438. }
  439. void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source)
  440. {
  441. faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
  442. std::string prefix = source["prefix"].String();
  443. BOOST_FOREACH(const JsonNode &piece, source["pieces"].Vector())
  444. {
  445. size_t index = faction.puzzleMap.size();
  446. SPuzzleInfo spi;
  447. spi.x = piece["x"].Float();
  448. spi.y = piece["y"].Float();
  449. spi.whenUncovered = piece["index"].Float();
  450. spi.number = index;
  451. // filename calculation
  452. std::ostringstream suffix;
  453. suffix << std::setfill('0') << std::setw(2) << index;
  454. spi.filename = prefix + suffix.str();
  455. faction.puzzleMap.push_back(spi);
  456. }
  457. assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
  458. }
  459. CFaction * CTownHandler::loadFromJson(const JsonNode &source)
  460. {
  461. CFaction * faction = new CFaction();
  462. faction->name = source["name"].String();
  463. VLC->modh->identifiers.requestIdentifier ("creature." + source["commander"].String(),
  464. [=](si32 commanderID)
  465. {
  466. faction->commander = CreatureID(commanderID);
  467. });
  468. faction->creatureBg120 = source["creatureBackground"]["120px"].String();
  469. faction->creatureBg130 = source["creatureBackground"]["130px"].String();
  470. faction->nativeTerrain = ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES,
  471. source["nativeTerrain"].String()));
  472. int alignment = vstd::find_pos(EAlignment::names, source["alignment"].String());
  473. if (alignment == -1)
  474. faction->alignment = EAlignment::NEUTRAL;
  475. else
  476. faction->alignment = static_cast<EAlignment::EAlignment>(alignment);
  477. if (!source["town"].isNull())
  478. {
  479. faction->town = new CTown;
  480. faction->town->faction = faction;
  481. loadTown(*faction->town, source["town"]);
  482. }
  483. else
  484. faction->town = nullptr;
  485. if (!source["puzzleMap"].isNull())
  486. loadPuzzle(*faction, source["puzzleMap"]);
  487. return faction;
  488. }
  489. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  490. {
  491. auto object = loadFromJson(data);
  492. object->index = factions.size();
  493. factions.push_back(object);
  494. VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
  495. }
  496. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  497. {
  498. auto object = loadFromJson(data);
  499. object->index = index;
  500. assert(factions[index] == nullptr); // ensure that this id was not loaded before
  501. factions[index] = object;
  502. VLC->modh->identifiers.registerObject(scope, "faction", name, object->index);
  503. }
  504. std::vector<bool> CTownHandler::getDefaultAllowed() const
  505. {
  506. std::vector<bool> allowedFactions;
  507. BOOST_FOREACH(auto town, factions)
  508. {
  509. allowedFactions.push_back(town->town != nullptr);
  510. }
  511. return allowedFactions;
  512. }