CTownHandler.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * CTownHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CTownHandler.h"
  12. #include "VCMI_Lib.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "JsonNode.h"
  15. #include "StringConstants.h"
  16. #include "CCreatureHandler.h"
  17. #include "CModHandler.h"
  18. #include "CHeroHandler.h"
  19. #include "CArtHandler.h"
  20. #include "GameSettings.h"
  21. #include "TerrainHandler.h"
  22. #include "spells/CSpellHandler.h"
  23. #include "filesystem/Filesystem.h"
  24. #include "bonuses/Bonus.h"
  25. #include "bonuses/Propagators.h"
  26. #include "ResourceSet.h"
  27. #include "mapObjectConstructors/AObjectTypeHandler.h"
  28. #include "mapObjectConstructors/CObjectClassesHandler.h"
  29. VCMI_LIB_NAMESPACE_BEGIN
  30. const int NAMES_PER_TOWN=16; // number of town names per faction in H3 files. Json can define any number
  31. const std::map<std::string, CBuilding::EBuildMode> CBuilding::MODES =
  32. {
  33. { "normal", CBuilding::BUILD_NORMAL },
  34. { "auto", CBuilding::BUILD_AUTO },
  35. { "special", CBuilding::BUILD_SPECIAL },
  36. { "grail", CBuilding::BUILD_GRAIL }
  37. };
  38. const std::map<std::string, CBuilding::ETowerHeight> CBuilding::TOWER_TYPES =
  39. {
  40. { "low", CBuilding::HEIGHT_LOW },
  41. { "average", CBuilding::HEIGHT_AVERAGE },
  42. { "high", CBuilding::HEIGHT_HIGH },
  43. { "skyship", CBuilding::HEIGHT_SKYSHIP }
  44. };
  45. std::string CBuilding::getJsonKey() const
  46. {
  47. return modScope + ':' + identifier;;
  48. }
  49. std::string CBuilding::getNameTranslated() const
  50. {
  51. return VLC->generaltexth->translate(getNameTextID());
  52. }
  53. std::string CBuilding::getDescriptionTranslated() const
  54. {
  55. return VLC->generaltexth->translate(getDescriptionTextID());
  56. }
  57. std::string CBuilding::getBaseTextID() const
  58. {
  59. return TextIdentifier("building", modScope, town->faction->identifier, identifier).get();
  60. }
  61. std::string CBuilding::getNameTextID() const
  62. {
  63. return TextIdentifier(getBaseTextID(), "name").get();
  64. }
  65. std::string CBuilding::getDescriptionTextID() const
  66. {
  67. return TextIdentifier(getBaseTextID(), "description").get();
  68. }
  69. BuildingID CBuilding::getBase() const
  70. {
  71. const CBuilding * build = this;
  72. while (build->upgrade >= 0)
  73. {
  74. build = build->town->buildings.at(build->upgrade);
  75. }
  76. return build->bid;
  77. }
  78. si32 CBuilding::getDistance(const BuildingID & buildID) const
  79. {
  80. const CBuilding * build = town->buildings.at(buildID);
  81. int distance = 0;
  82. while (build->upgrade >= 0 && build != this)
  83. {
  84. build = build->town->buildings.at(build->upgrade);
  85. distance++;
  86. }
  87. if (build == this)
  88. return distance;
  89. return -1;
  90. }
  91. void CBuilding::addNewBonus(const std::shared_ptr<Bonus> & b, BonusList & bonusList) const
  92. {
  93. bonusList.push_back(b);
  94. }
  95. CFaction::~CFaction()
  96. {
  97. if (town)
  98. {
  99. delete town;
  100. town = nullptr;
  101. }
  102. }
  103. int32_t CFaction::getIndex() const
  104. {
  105. return index;
  106. }
  107. int32_t CFaction::getIconIndex() const
  108. {
  109. return index; //???
  110. }
  111. std::string CFaction::getJsonKey() const
  112. {
  113. return modScope + ':' + identifier;;
  114. }
  115. void CFaction::registerIcons(const IconRegistar & cb) const
  116. {
  117. if(town)
  118. {
  119. auto & info = town->clientInfo;
  120. cb(info.icons[0][0], 0, "ITPT", info.iconLarge[0][0]);
  121. cb(info.icons[0][1], 0, "ITPT", info.iconLarge[0][1]);
  122. cb(info.icons[1][0], 0, "ITPT", info.iconLarge[1][0]);
  123. cb(info.icons[1][1], 0, "ITPT", info.iconLarge[1][1]);
  124. cb(info.icons[0][0] + 2, 0, "ITPA", info.iconSmall[0][0]);
  125. cb(info.icons[0][1] + 2, 0, "ITPA", info.iconSmall[0][1]);
  126. cb(info.icons[1][0] + 2, 0, "ITPA", info.iconSmall[1][0]);
  127. cb(info.icons[1][1] + 2, 0, "ITPA", info.iconSmall[1][1]);
  128. cb(index, 1, "CPRSMALL", info.towerIconSmall);
  129. cb(index, 1, "TWCRPORT", info.towerIconLarge);
  130. }
  131. }
  132. std::string CFaction::getNameTranslated() const
  133. {
  134. return VLC->generaltexth->translate(getNameTextID());
  135. }
  136. std::string CFaction::getNameTextID() const
  137. {
  138. return TextIdentifier("faction", modScope, identifier, "name").get();
  139. }
  140. FactionID CFaction::getId() const
  141. {
  142. return FactionID(index);
  143. }
  144. FactionID CFaction::getFaction() const
  145. {
  146. return FactionID(index);
  147. }
  148. bool CFaction::hasTown() const
  149. {
  150. return town != nullptr;
  151. }
  152. EAlignment CFaction::getAlignment() const
  153. {
  154. return alignment;
  155. }
  156. EBoatId CFaction::getBoatType() const
  157. {
  158. return boatType.toEnum();
  159. }
  160. TerrainId CFaction::getNativeTerrain() const
  161. {
  162. return nativeTerrain;
  163. }
  164. void CFaction::updateFrom(const JsonNode & data)
  165. {
  166. }
  167. void CFaction::serializeJson(JsonSerializeFormat & handler)
  168. {
  169. }
  170. CTown::CTown()
  171. : faction(nullptr), mageLevel(0), primaryRes(0), moatAbility(SpellID::NONE), defaultTavernChance(0)
  172. {
  173. }
  174. CTown::~CTown()
  175. {
  176. for(auto & build : buildings)
  177. build.second.dellNull();
  178. for(auto & str : clientInfo.structures)
  179. str.dellNull();
  180. }
  181. std::string CTown::getRandomNameTranslated(size_t index) const
  182. {
  183. return VLC->generaltexth->translate(getRandomNameTextID(index));
  184. }
  185. std::string CTown::getRandomNameTextID(size_t index) const
  186. {
  187. return TextIdentifier("faction", faction->modScope, faction->identifier, "randomName", index).get();
  188. }
  189. size_t CTown::getRandomNamesCount() const
  190. {
  191. return namesCount;
  192. }
  193. std::string CTown::getBuildingScope() const
  194. {
  195. if(faction == nullptr)
  196. //no faction == random faction
  197. return "building";
  198. else
  199. return "building." + faction->getJsonKey();
  200. }
  201. std::set<si32> CTown::getAllBuildings() const
  202. {
  203. std::set<si32> res;
  204. for(const auto & b : buildings)
  205. {
  206. res.insert(b.first.num);
  207. }
  208. return res;
  209. }
  210. const CBuilding * CTown::getSpecialBuilding(BuildingSubID::EBuildingSubID subID) const
  211. {
  212. for(const auto & kvp : buildings)
  213. {
  214. if(kvp.second->subId == subID)
  215. return buildings.at(kvp.first);
  216. }
  217. return nullptr;
  218. }
  219. BuildingID::EBuildingID CTown::getBuildingType(BuildingSubID::EBuildingSubID subID) const
  220. {
  221. const auto * building = getSpecialBuilding(subID);
  222. return building == nullptr ? BuildingID::NONE : building->bid.num;
  223. }
  224. std::string CTown::getGreeting(BuildingSubID::EBuildingSubID subID) const
  225. {
  226. return CTownHandler::getMappedValue<const std::string, BuildingSubID::EBuildingSubID>(subID, std::string(), specialMessages, false);
  227. }
  228. void CTown::setGreeting(BuildingSubID::EBuildingSubID subID, const std::string & message) const
  229. {
  230. specialMessages.insert(std::pair<BuildingSubID::EBuildingSubID, const std::string>(subID, message));
  231. }
  232. CTownHandler::CTownHandler():
  233. randomTown(new CTown()),
  234. randomFaction(new CFaction())
  235. {
  236. randomFaction->town = randomTown;
  237. randomTown->faction = randomFaction;
  238. randomFaction->identifier = "random";
  239. randomFaction->modScope = "core";
  240. }
  241. CTownHandler::~CTownHandler()
  242. {
  243. delete randomTown;
  244. }
  245. JsonNode readBuilding(CLegacyConfigParser & parser)
  246. {
  247. JsonNode ret;
  248. JsonNode & cost = ret["cost"];
  249. //note: this code will try to parse mithril as well but wil always return 0 for it
  250. for(const std::string & resID : GameConstants::RESOURCE_NAMES)
  251. cost[resID].Float() = parser.readNumber();
  252. cost.Struct().erase("mithril"); // erase mithril to avoid confusing validator
  253. parser.endLine();
  254. return ret;
  255. }
  256. TPropagatorPtr & CTownHandler::emptyPropagator()
  257. {
  258. static TPropagatorPtr emptyProp(nullptr);
  259. return emptyProp;
  260. }
  261. std::vector<JsonNode> CTownHandler::loadLegacyData()
  262. {
  263. size_t dataSize = VLC->settings()->getInteger(EGameSettings::TEXTS_FACTION);
  264. std::vector<JsonNode> dest(dataSize);
  265. objects.resize(dataSize);
  266. auto getBuild = [&](size_t town, size_t building) -> JsonNode &
  267. {
  268. return dest[town]["town"]["buildings"][EBuildingType::names[building]];
  269. };
  270. CLegacyConfigParser parser("DATA/BUILDING.TXT");
  271. parser.endLine(); // header
  272. parser.endLine();
  273. //Unique buildings
  274. for (size_t town=0; town<dataSize; town++)
  275. {
  276. parser.endLine(); //header
  277. parser.endLine();
  278. int buildID = 17;
  279. do
  280. {
  281. getBuild(town, buildID) = readBuilding(parser);
  282. buildID++;
  283. }
  284. while (!parser.isNextEntryEmpty());
  285. }
  286. // Common buildings
  287. parser.endLine(); // header
  288. parser.endLine();
  289. parser.endLine();
  290. int buildID = 0;
  291. do
  292. {
  293. JsonNode building = readBuilding(parser);
  294. for (size_t town=0; town<dataSize; town++)
  295. getBuild(town, buildID) = building;
  296. buildID++;
  297. }
  298. while (!parser.isNextEntryEmpty());
  299. parser.endLine(); //header
  300. parser.endLine();
  301. //Dwellings
  302. for (size_t town=0; town<dataSize; town++)
  303. {
  304. parser.endLine(); //header
  305. parser.endLine();
  306. for (size_t i=0; i<14; i++)
  307. {
  308. getBuild(town, 30+i) = readBuilding(parser);
  309. }
  310. }
  311. {
  312. CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
  313. for(int building=0; building<15; building++)
  314. {
  315. std::string name = parser.readString();
  316. std::string descr = parser.readString();
  317. parser.endLine();
  318. for(int j=0; j<dataSize; j++)
  319. {
  320. getBuild(j, building)["name"].String() = name;
  321. getBuild(j, building)["description"].String() = descr;
  322. }
  323. }
  324. parser.endLine(); // silo
  325. parser.endLine(); // blacksmith //unused entries
  326. parser.endLine(); // moat
  327. //shipyard with the ship
  328. std::string name = parser.readString();
  329. std::string descr = parser.readString();
  330. parser.endLine();
  331. for(int town=0; town<dataSize; town++)
  332. {
  333. getBuild(town, 20)["name"].String() = name;
  334. getBuild(town, 20)["description"].String() = descr;
  335. }
  336. //blacksmith
  337. for(int town=0; town<dataSize; town++)
  338. {
  339. getBuild(town, 16)["name"].String() = parser.readString();
  340. getBuild(town, 16)["description"].String() = parser.readString();
  341. parser.endLine();
  342. }
  343. }
  344. {
  345. CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
  346. for(int town=0; town<dataSize; town++)
  347. {
  348. for(int build=0; build<9; build++)
  349. {
  350. getBuild(town, 17 + build)["name"].String() = parser.readString();
  351. getBuild(town, 17 + build)["description"].String() = parser.readString();
  352. parser.endLine();
  353. }
  354. getBuild(town, 26)["name"].String() = parser.readString(); // Grail
  355. getBuild(town, 26)["description"].String() = parser.readString();
  356. parser.endLine();
  357. getBuild(town, 15)["name"].String() = parser.readString(); // Resource silo
  358. getBuild(town, 15)["description"].String() = parser.readString();
  359. parser.endLine();
  360. }
  361. }
  362. {
  363. CLegacyConfigParser parser("DATA/DWELLING.TXT");
  364. for(int town=0; town<dataSize; town++)
  365. {
  366. for(int build=0; build<14; build++)
  367. {
  368. getBuild(town, 30 + build)["name"].String() = parser.readString();
  369. getBuild(town, 30 + build)["description"].String() = parser.readString();
  370. parser.endLine();
  371. }
  372. }
  373. }
  374. {
  375. CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
  376. CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
  377. size_t townID=0;
  378. do
  379. {
  380. dest[townID]["name"].String() = typeParser.readString();
  381. for (int i=0; i<NAMES_PER_TOWN; i++)
  382. {
  383. JsonNode name;
  384. name.String() = nameParser.readString();
  385. dest[townID]["town"]["names"].Vector().push_back(name);
  386. nameParser.endLine();
  387. }
  388. townID++;
  389. }
  390. while (typeParser.endLine());
  391. }
  392. return dest;
  393. }
  394. void CTownHandler::loadBuildingRequirements(CBuilding * building, const JsonNode & source, std::vector<BuildingRequirementsHelper> & bidsToLoad) const
  395. {
  396. if (source.isNull())
  397. return;
  398. BuildingRequirementsHelper hlp;
  399. hlp.building = building;
  400. hlp.town = building->town;
  401. hlp.json = source;
  402. bidsToLoad.push_back(hlp);
  403. }
  404. template<typename R, typename K>
  405. R CTownHandler::getMappedValue(const K key, const R defval, const std::map<K, R> & map, bool required)
  406. {
  407. auto it = map.find(key);
  408. if(it != map.end())
  409. return it->second;
  410. if(required)
  411. logMod->warn("Warning: Property: '%s' is unknown. Correct the typo or update VCMI.", key);
  412. return defval;
  413. }
  414. template<typename R>
  415. R CTownHandler::getMappedValue(const JsonNode & node, const R defval, const std::map<std::string, R> & map, bool required)
  416. {
  417. if(!node.isNull() && node.getType() == JsonNode::JsonType::DATA_STRING)
  418. return getMappedValue<R, std::string>(node.String(), defval, map, required);
  419. return defval;
  420. }
  421. void CTownHandler::addBonusesForVanilaBuilding(CBuilding * building) const
  422. {
  423. std::shared_ptr<Bonus> b;
  424. static TPropagatorPtr playerPropagator = std::make_shared<CPropagatorNodeType>(CBonusSystemNode::ENodeTypes::PLAYER);
  425. if(building->bid == BuildingID::TAVERN)
  426. {
  427. b = createBonus(building, BonusType::MORALE, +1);
  428. }
  429. switch(building->subId)
  430. {
  431. case BuildingSubID::BROTHERHOOD_OF_SWORD:
  432. b = createBonus(building, BonusType::MORALE, +2);
  433. building->overrideBids.insert(BuildingID::TAVERN);
  434. break;
  435. case BuildingSubID::FOUNTAIN_OF_FORTUNE:
  436. b = createBonus(building, BonusType::LUCK, +2);
  437. break;
  438. case BuildingSubID::SPELL_POWER_GARRISON_BONUS:
  439. b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER);
  440. break;
  441. case BuildingSubID::ATTACK_GARRISON_BONUS:
  442. b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::ATTACK);
  443. break;
  444. case BuildingSubID::DEFENSE_GARRISON_BONUS:
  445. b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE);
  446. break;
  447. case BuildingSubID::LIGHTHOUSE:
  448. b = createBonus(building, BonusType::MOVEMENT, +500, playerPropagator, 0);
  449. break;
  450. }
  451. if(b)
  452. building->addNewBonus(b, building->buildingBonuses);
  453. }
  454. std::shared_ptr<Bonus> CTownHandler::createBonus(CBuilding * build, BonusType type, int val, int subtype) const
  455. {
  456. return createBonus(build, type, val, emptyPropagator(), subtype);
  457. }
  458. std::shared_ptr<Bonus> CTownHandler::createBonus(CBuilding * build, BonusType type, int val, TPropagatorPtr & prop, int subtype) const
  459. {
  460. std::ostringstream descr;
  461. descr << build->getNameTranslated();
  462. return createBonusImpl(build->bid, type, val, prop, descr.str(), subtype);
  463. }
  464. std::shared_ptr<Bonus> CTownHandler::createBonusImpl(const BuildingID & building,
  465. BonusType type,
  466. int val,
  467. TPropagatorPtr & prop,
  468. const std::string & description,
  469. int subtype) const
  470. {
  471. auto b = std::make_shared<Bonus>(BonusDuration::PERMANENT, type, BonusSource::TOWN_STRUCTURE, val, building, description, subtype);
  472. if(prop)
  473. b->addPropagator(prop);
  474. return b;
  475. }
  476. void CTownHandler::loadSpecialBuildingBonuses(const JsonNode & source, BonusList & bonusList, CBuilding * building)
  477. {
  478. for(const auto & b : source.Vector())
  479. {
  480. auto bonus = JsonUtils::parseBuildingBonus(b, building->bid, building->getNameTranslated());
  481. if(bonus == nullptr)
  482. continue;
  483. bonus->sid = Bonus::getSid32(building->town->faction->getIndex(), building->bid);
  484. //JsonUtils::parseBuildingBonus produces UNKNOWN type propagator instead of empty.
  485. if(bonus->propagator != nullptr
  486. && bonus->propagator->getPropagatorType() == CBonusSystemNode::ENodeTypes::UNKNOWN)
  487. bonus->addPropagator(emptyPropagator());
  488. building->addNewBonus(bonus, bonusList);
  489. }
  490. }
  491. void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, const JsonNode & source)
  492. {
  493. assert(stringID.find(':') == std::string::npos);
  494. assert(!source.meta.empty());
  495. auto * ret = new CBuilding();
  496. ret->bid = getMappedValue<BuildingID, std::string>(stringID, BuildingID::NONE, MappedKeys::BUILDING_NAMES_TO_TYPES, false);
  497. ret->subId = BuildingSubID::NONE;
  498. if(ret->bid == BuildingID::NONE && !source["id"].isNull())
  499. {
  500. // FIXME: A lot of false-positives with no clear way to handle them in mods
  501. //logMod->warn("Building %s: id field is deprecated", stringID);
  502. ret->bid = source["id"].isNull() ? BuildingID(BuildingID::NONE) : BuildingID(source["id"].Float());
  503. }
  504. if (ret->bid == BuildingID::NONE)
  505. logMod->error("Building '%s' isn't recognized and won't work properly. Correct the typo or update VCMI.", stringID);
  506. ret->mode = ret->bid == BuildingID::GRAIL
  507. ? CBuilding::BUILD_GRAIL
  508. : getMappedValue<CBuilding::EBuildMode>(source["mode"], CBuilding::BUILD_NORMAL, CBuilding::MODES);
  509. ret->height = getMappedValue<CBuilding::ETowerHeight>(source["height"], CBuilding::HEIGHT_NO_TOWER, CBuilding::TOWER_TYPES);
  510. ret->identifier = stringID;
  511. ret->modScope = source.meta;
  512. ret->town = town;
  513. VLC->generaltexth->registerString(source.meta, ret->getNameTextID(), source["name"].String());
  514. VLC->generaltexth->registerString(source.meta, ret->getDescriptionTextID(), source["description"].String());
  515. ret->resources = TResources(source["cost"]);
  516. ret->produce = TResources(source["produce"]);
  517. if(ret->bid == BuildingID::TAVERN)
  518. addBonusesForVanilaBuilding(ret);
  519. else if(ret->bid.IsSpecialOrGrail())
  520. {
  521. loadSpecialBuildingBonuses(source["bonuses"], ret->buildingBonuses, ret);
  522. if(ret->buildingBonuses.empty())
  523. {
  524. ret->subId = getMappedValue<BuildingSubID::EBuildingSubID>(source["type"], BuildingSubID::NONE, MappedKeys::SPECIAL_BUILDINGS);
  525. addBonusesForVanilaBuilding(ret);
  526. }
  527. loadSpecialBuildingBonuses(source["onVisitBonuses"], ret->onVisitBonuses, ret);
  528. if(!ret->onVisitBonuses.empty())
  529. {
  530. if(ret->subId == BuildingSubID::NONE)
  531. ret->subId = BuildingSubID::CUSTOM_VISITING_BONUS;
  532. for(auto & bonus : ret->onVisitBonuses)
  533. bonus->sid = Bonus::getSid32(ret->town->faction->getIndex(), ret->bid);
  534. }
  535. if(source["type"].String() == "configurable" && ret->subId == BuildingSubID::NONE)
  536. {
  537. ret->subId = BuildingSubID::CUSTOM_VISITING_REWARD;
  538. ret->rewardableObjectInfo.init(source, ret->getBaseTextID());
  539. }
  540. }
  541. //MODS COMPATIBILITY FOR 0.96
  542. if(!ret->produce.nonZero())
  543. {
  544. switch (ret->bid) {
  545. break; case BuildingID::VILLAGE_HALL: ret->produce[EGameResID::GOLD] = 500;
  546. break; case BuildingID::TOWN_HALL : ret->produce[EGameResID::GOLD] = 1000;
  547. break; case BuildingID::CITY_HALL : ret->produce[EGameResID::GOLD] = 2000;
  548. break; case BuildingID::CAPITOL : ret->produce[EGameResID::GOLD] = 4000;
  549. break; case BuildingID::GRAIL : ret->produce[EGameResID::GOLD] = 5000;
  550. break; case BuildingID::RESOURCE_SILO :
  551. {
  552. switch (ret->town->primaryRes.toEnum())
  553. {
  554. case EGameResID::GOLD:
  555. ret->produce[ret->town->primaryRes] = 500;
  556. break;
  557. case EGameResID::WOOD_AND_ORE:
  558. ret->produce[EGameResID::WOOD] = 1;
  559. ret->produce[EGameResID::ORE] = 1;
  560. break;
  561. default:
  562. ret->produce[ret->town->primaryRes] = 1;
  563. break;
  564. }
  565. }
  566. }
  567. }
  568. loadBuildingRequirements(ret, source["requires"], requirementsToLoad);
  569. if(ret->bid.IsSpecialOrGrail())
  570. loadBuildingRequirements(ret, source["overrides"], overriddenBidsToLoad);
  571. if (!source["upgrades"].isNull())
  572. {
  573. // building id and upgrades can't be the same
  574. if(stringID == source["upgrades"].String())
  575. {
  576. throw std::runtime_error(boost::str(boost::format("Building with ID '%s' of town '%s' can't be an upgrade of the same building.") %
  577. stringID % ret->town->faction->getNameTranslated()));
  578. }
  579. VLC->modh->identifiers.requestIdentifier(ret->town->getBuildingScope(), source["upgrades"], [=](si32 identifier)
  580. {
  581. ret->upgrade = BuildingID(identifier);
  582. });
  583. }
  584. else
  585. ret->upgrade = BuildingID::NONE;
  586. ret->town->buildings[ret->bid] = ret;
  587. registerObject(source.meta, ret->town->getBuildingScope(), ret->identifier, ret->bid);
  588. }
  589. void CTownHandler::loadBuildings(CTown * town, const JsonNode & source)
  590. {
  591. if(source.isStruct())
  592. {
  593. for(const auto & node : source.Struct())
  594. {
  595. if (!node.second.isNull())
  596. loadBuilding(town, node.first, node.second);
  597. }
  598. }
  599. }
  600. void CTownHandler::loadStructure(CTown &town, const std::string & stringID, const JsonNode & source) const
  601. {
  602. auto * ret = new CStructure();
  603. ret->building = nullptr;
  604. ret->buildable = nullptr;
  605. VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->getJsonKey(), stringID, [=, &town](si32 identifier) mutable
  606. {
  607. ret->building = town.buildings[BuildingID(identifier)];
  608. });
  609. if (source["builds"].isNull())
  610. {
  611. VLC->modh->identifiers.tryRequestIdentifier( source.meta, "building." + town.faction->getJsonKey(), stringID, [=, &town](si32 identifier) mutable
  612. {
  613. ret->building = town.buildings[BuildingID(identifier)];
  614. });
  615. }
  616. else
  617. {
  618. VLC->modh->identifiers.requestIdentifier("building." + town.faction->getJsonKey(), source["builds"], [=, &town](si32 identifier) mutable
  619. {
  620. ret->buildable = town.buildings[BuildingID(identifier)];
  621. });
  622. }
  623. ret->identifier = stringID;
  624. ret->pos.x = static_cast<si32>(source["x"].Float());
  625. ret->pos.y = static_cast<si32>(source["y"].Float());
  626. ret->pos.z = static_cast<si32>(source["z"].Float());
  627. ret->hiddenUpgrade = source["hidden"].Bool();
  628. ret->defName = source["animation"].String();
  629. ret->borderName = source["border"].String();
  630. ret->areaName = source["area"].String();
  631. town.clientInfo.structures.emplace_back(ret);
  632. }
  633. void CTownHandler::loadStructures(CTown &town, const JsonNode & source) const
  634. {
  635. for(const auto & node : source.Struct())
  636. {
  637. if (!node.second.isNull())
  638. loadStructure(town, node.first, node.second);
  639. }
  640. }
  641. void CTownHandler::loadTownHall(CTown &town, const JsonNode & source) const
  642. {
  643. auto & dstSlots = town.clientInfo.hallSlots;
  644. const auto & srcSlots = source.Vector();
  645. dstSlots.resize(srcSlots.size());
  646. for(size_t i=0; i<dstSlots.size(); i++)
  647. {
  648. auto & dstRow = dstSlots[i];
  649. const auto & srcRow = srcSlots[i].Vector();
  650. dstRow.resize(srcRow.size());
  651. for(size_t j=0; j < dstRow.size(); j++)
  652. {
  653. auto & dstBox = dstRow[j];
  654. const auto & srcBox = srcRow[j].Vector();
  655. dstBox.resize(srcBox.size());
  656. for(size_t k=0; k<dstBox.size(); k++)
  657. {
  658. auto & dst = dstBox[k];
  659. const auto & src = srcBox[k];
  660. VLC->modh->identifiers.requestIdentifier("building." + town.faction->getJsonKey(), src, [&](si32 identifier)
  661. {
  662. dst = BuildingID(identifier);
  663. });
  664. }
  665. }
  666. }
  667. }
  668. Point JsonToPoint(const JsonNode & node)
  669. {
  670. if(!node.isStruct())
  671. return Point::makeInvalid();
  672. Point ret;
  673. ret.x = static_cast<si32>(node["x"].Float());
  674. ret.y = static_cast<si32>(node["y"].Float());
  675. return ret;
  676. }
  677. void CTownHandler::loadSiegeScreen(CTown &town, const JsonNode & source) const
  678. {
  679. town.clientInfo.siegePrefix = source["imagePrefix"].String();
  680. town.clientInfo.towerIconSmall = source["towerIconSmall"].String();
  681. town.clientInfo.towerIconLarge = source["towerIconLarge"].String();
  682. VLC->modh->identifiers.requestIdentifier("creature", source["shooter"], [&town](si32 creature)
  683. {
  684. auto crId = CreatureID(creature);
  685. if((*VLC->creh)[crId]->animation.missleFrameAngles.empty())
  686. logMod->error("Mod '%s' error: Creature '%s' on the Archer's tower is not a shooter. Mod should be fixed. Siege will not work properly!"
  687. , town.faction->getNameTranslated()
  688. , (*VLC->creh)[crId]->getNameSingularTranslated());
  689. town.clientInfo.siegeShooter = crId;
  690. });
  691. auto & pos = town.clientInfo.siegePositions;
  692. pos.resize(21);
  693. pos[8] = JsonToPoint(source["towers"]["top"]["tower"]);
  694. pos[17] = JsonToPoint(source["towers"]["top"]["battlement"]);
  695. pos[20] = JsonToPoint(source["towers"]["top"]["creature"]);
  696. pos[2] = JsonToPoint(source["towers"]["keep"]["tower"]);
  697. pos[15] = JsonToPoint(source["towers"]["keep"]["battlement"]);
  698. pos[18] = JsonToPoint(source["towers"]["keep"]["creature"]);
  699. pos[3] = JsonToPoint(source["towers"]["bottom"]["tower"]);
  700. pos[16] = JsonToPoint(source["towers"]["bottom"]["battlement"]);
  701. pos[19] = JsonToPoint(source["towers"]["bottom"]["creature"]);
  702. pos[9] = JsonToPoint(source["gate"]["gate"]);
  703. pos[10] = JsonToPoint(source["gate"]["arch"]);
  704. pos[7] = JsonToPoint(source["walls"]["upper"]);
  705. pos[6] = JsonToPoint(source["walls"]["upperMid"]);
  706. pos[5] = JsonToPoint(source["walls"]["bottomMid"]);
  707. pos[4] = JsonToPoint(source["walls"]["bottom"]);
  708. pos[13] = JsonToPoint(source["moat"]["moat"]);
  709. pos[14] = JsonToPoint(source["moat"]["bank"]);
  710. pos[11] = JsonToPoint(source["static"]["bottom"]);
  711. pos[12] = JsonToPoint(source["static"]["top"]);
  712. pos[1] = JsonToPoint(source["static"]["background"]);
  713. }
  714. static void readIcon(JsonNode source, std::string & small, std::string & large)
  715. {
  716. if (source.getType() == JsonNode::JsonType::DATA_STRUCT) // don't crash on old format
  717. {
  718. small = source["small"].String();
  719. large = source["large"].String();
  720. }
  721. }
  722. void CTownHandler::loadClientData(CTown &town, const JsonNode & source) const
  723. {
  724. CTown::ClientInfo & info = town.clientInfo;
  725. readIcon(source["icons"]["village"]["normal"], info.iconSmall[0][0], info.iconLarge[0][0]);
  726. readIcon(source["icons"]["village"]["built"], info.iconSmall[0][1], info.iconLarge[0][1]);
  727. readIcon(source["icons"]["fort"]["normal"], info.iconSmall[1][0], info.iconLarge[1][0]);
  728. readIcon(source["icons"]["fort"]["built"], info.iconSmall[1][1], info.iconLarge[1][1]);
  729. info.hallBackground = source["hallBackground"].String();
  730. info.musicTheme = source["musicTheme"].String();
  731. info.townBackground = source["townBackground"].String();
  732. info.guildWindow = source["guildWindow"].String();
  733. info.buildingsIcons = source["buildingsIcons"].String();
  734. //left for back compatibility - will be removed later
  735. if(!source["guildBackground"].String().empty())
  736. info.guildBackground = source["guildBackground"].String();
  737. else
  738. info.guildBackground = "TPMAGE.bmp";
  739. if(!source["tavernVideo"].String().empty())
  740. info.tavernVideo = source["tavernVideo"].String();
  741. else
  742. info.tavernVideo = "TAVERN.BIK";
  743. //end of legacy assignment
  744. loadTownHall(town, source["hallSlots"]);
  745. loadStructures(town, source["structures"]);
  746. loadSiegeScreen(town, source["siege"]);
  747. }
  748. void CTownHandler::loadTown(CTown * town, const JsonNode & source)
  749. {
  750. const auto * resIter = boost::find(GameConstants::RESOURCE_NAMES, source["primaryResource"].String());
  751. if(resIter == std::end(GameConstants::RESOURCE_NAMES))
  752. town->primaryRes = GameResID(EGameResID::WOOD_AND_ORE); //Wood + Ore
  753. else
  754. town->primaryRes = GameResID(resIter - std::begin(GameConstants::RESOURCE_NAMES));
  755. warMachinesToLoad[town] = source["warMachine"];
  756. town->mageLevel = static_cast<ui32>(source["mageGuild"].Float());
  757. town->namesCount = 0;
  758. for(const auto & name : source["names"].Vector())
  759. {
  760. VLC->generaltexth->registerString(town->faction->modScope, town->getRandomNameTextID(town->namesCount), name.String());
  761. town->namesCount += 1;
  762. }
  763. if (!source["moatAbility"].isNull()) // VCMI 1.2 compatibility code
  764. {
  765. VLC->modh->identifiers.requestIdentifier( "spell", source["moatAbility"], [=](si32 ability)
  766. {
  767. town->moatAbility = SpellID(ability);
  768. });
  769. }
  770. else
  771. {
  772. VLC->modh->identifiers.requestIdentifier( source.meta, "spell", "castleMoat", [=](si32 ability)
  773. {
  774. town->moatAbility = SpellID(ability);
  775. });
  776. }
  777. // Horde building creature level
  778. for(const JsonNode &node : source["horde"].Vector())
  779. town->hordeLvl[static_cast<int>(town->hordeLvl.size())] = static_cast<int>(node.Float());
  780. // town needs to have exactly 2 horde entries. Validation will take care of 2+ entries
  781. // but anything below 2 must be handled here
  782. for (size_t i=source["horde"].Vector().size(); i<2; i++)
  783. town->hordeLvl[static_cast<int>(i)] = -1;
  784. const JsonVector & creatures = source["creatures"].Vector();
  785. town->creatures.resize(creatures.size());
  786. for (size_t i=0; i< creatures.size(); i++)
  787. {
  788. const JsonVector & level = creatures[i].Vector();
  789. town->creatures[i].resize(level.size());
  790. for (size_t j=0; j<level.size(); j++)
  791. {
  792. VLC->modh->identifiers.requestIdentifier("creature", level[j], [=](si32 creature)
  793. {
  794. town->creatures[i][j] = CreatureID(creature);
  795. });
  796. }
  797. }
  798. town->defaultTavernChance = static_cast<ui32>(source["defaultTavern"].Float());
  799. /// set chance of specific hero class to appear in this town
  800. for(const auto & node : source["tavern"].Struct())
  801. {
  802. int chance = static_cast<int>(node.second.Float());
  803. VLC->modh->identifiers.requestIdentifier(node.second.meta, "heroClass",node.first, [=](si32 classID)
  804. {
  805. VLC->heroh->classes[HeroClassID(classID)]->selectionProbability[town->faction->getId()] = chance;
  806. });
  807. }
  808. for(const auto & node : source["guildSpells"].Struct())
  809. {
  810. int chance = static_cast<int>(node.second.Float());
  811. VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=](si32 spellID)
  812. {
  813. VLC->spellh->objects.at(spellID)->probabilities[town->faction->getId()] = chance;
  814. });
  815. }
  816. for(const JsonNode & d : source["adventureMap"]["dwellings"].Vector())
  817. {
  818. town->dwellings.push_back(d["graphics"].String());
  819. town->dwellingNames.push_back(d["name"].String());
  820. }
  821. loadBuildings(town, source["buildings"]);
  822. loadClientData(*town, source);
  823. }
  824. void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source) const
  825. {
  826. faction.puzzleMap.reserve(GameConstants::PUZZLE_MAP_PIECES);
  827. std::string prefix = source["prefix"].String();
  828. for(const JsonNode &piece : source["pieces"].Vector())
  829. {
  830. size_t index = faction.puzzleMap.size();
  831. SPuzzleInfo spi;
  832. spi.x = static_cast<si16>(piece["x"].Float());
  833. spi.y = static_cast<si16>(piece["y"].Float());
  834. spi.whenUncovered = static_cast<ui16>(piece["index"].Float());
  835. spi.number = static_cast<ui16>(index);
  836. // filename calculation
  837. std::ostringstream suffix;
  838. suffix << std::setfill('0') << std::setw(2) << index;
  839. spi.filename = prefix + suffix.str();
  840. faction.puzzleMap.push_back(spi);
  841. }
  842. assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES);
  843. }
  844. CFaction * CTownHandler::loadFromJson(const std::string & scope, const JsonNode & source, const std::string & identifier, size_t index)
  845. {
  846. assert(identifier.find(':') == std::string::npos);
  847. auto * faction = new CFaction();
  848. faction->index = static_cast<FactionID>(index);
  849. faction->modScope = scope;
  850. faction->identifier = identifier;
  851. VLC->generaltexth->registerString(scope, faction->getNameTextID(), source["name"].String());
  852. faction->creatureBg120 = source["creatureBackground"]["120px"].String();
  853. faction->creatureBg130 = source["creatureBackground"]["130px"].String();
  854. faction->boatType = EBoatId::CASTLE; //Do not crash
  855. if (!source["boat"].isNull())
  856. {
  857. VLC->modh->identifiers.requestIdentifier("core:boat", source["boat"], [=](int32_t boatTypeID)
  858. {
  859. faction->boatType = BoatId(boatTypeID);
  860. });
  861. }
  862. int alignment = vstd::find_pos(GameConstants::ALIGNMENT_NAMES, source["alignment"].String());
  863. if (alignment == -1)
  864. faction->alignment = EAlignment::NEUTRAL;
  865. else
  866. faction->alignment = static_cast<EAlignment>(alignment);
  867. auto preferUndergound = source["preferUndergroundPlacement"];
  868. faction->preferUndergroundPlacement = preferUndergound.isNull() ? false : preferUndergound.Bool();
  869. // NOTE: semi-workaround - normally, towns are supposed to have native terrains.
  870. // Towns without one are exceptions. So, vcmi requires nativeTerrain to be defined
  871. // But allows it to be defined with explicit value of "none" if town should not have native terrain
  872. // This is better than allowing such terrain-less towns silently, leading to issues with RMG
  873. faction->nativeTerrain = ETerrainId::NONE;
  874. if ( !source["nativeTerrain"].isNull() && source["nativeTerrain"].String() != "none")
  875. {
  876. VLC->modh->identifiers.requestIdentifier("terrain", source["nativeTerrain"], [=](int32_t index){
  877. faction->nativeTerrain = TerrainId(index);
  878. auto const & terrain = VLC->terrainTypeHandler->getById(faction->nativeTerrain);
  879. if (!terrain->isSurface() && !terrain->isUnderground())
  880. logMod->warn("Faction %s has terrain %s as native, but terrain is not suitable for either surface or subterranean layers!", faction->getJsonKey(), terrain->getJsonKey());
  881. });
  882. }
  883. if (!source["town"].isNull())
  884. {
  885. faction->town = new CTown();
  886. faction->town->faction = faction;
  887. loadTown(faction->town, source["town"]);
  888. }
  889. else
  890. faction->town = nullptr;
  891. if (!source["puzzleMap"].isNull())
  892. loadPuzzle(*faction, source["puzzleMap"]);
  893. return faction;
  894. }
  895. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  896. {
  897. auto * object = loadFromJson(scope, data, name, objects.size());
  898. objects.emplace_back(object);
  899. if (object->town)
  900. {
  901. auto & info = object->town->clientInfo;
  902. info.icons[0][0] = 8 + object->index * 4 + 0;
  903. info.icons[0][1] = 8 + object->index * 4 + 1;
  904. info.icons[1][0] = 8 + object->index * 4 + 2;
  905. info.icons[1][1] = 8 + object->index * 4 + 3;
  906. VLC->modh->identifiers.requestIdentifier(scope, "object", "town", [=](si32 index)
  907. {
  908. // register town once objects are loaded
  909. JsonNode config = data["town"]["mapObject"];
  910. config["faction"].String() = name;
  911. config["faction"].meta = scope;
  912. if (config.meta.empty())// MODS COMPATIBILITY FOR 0.96
  913. config.meta = scope;
  914. VLC->objtypeh->loadSubObject(object->identifier, config, index, object->index);
  915. // MODS COMPATIBILITY FOR 0.96
  916. const auto & advMap = data["town"]["adventureMap"];
  917. if (!advMap.isNull())
  918. {
  919. logMod->warn("Outdated town mod. Will try to generate valid templates out of fort");
  920. JsonNode config;
  921. config["animation"] = advMap["castle"];
  922. VLC->objtypeh->getHandlerFor(index, object->index)->addTemplate(config);
  923. }
  924. });
  925. }
  926. registerObject(scope, "faction", name, object->index);
  927. }
  928. void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  929. {
  930. auto * object = loadFromJson(scope, data, name, index);
  931. if (objects.size() > index)
  932. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  933. else
  934. objects.resize(index + 1);
  935. objects[index] = object;
  936. if (object->town)
  937. {
  938. auto & info = object->town->clientInfo;
  939. info.icons[0][0] = (GameConstants::F_NUMBER + object->index) * 2 + 0;
  940. info.icons[0][1] = (GameConstants::F_NUMBER + object->index) * 2 + 1;
  941. info.icons[1][0] = object->index * 2 + 0;
  942. info.icons[1][1] = object->index * 2 + 1;
  943. VLC->modh->identifiers.requestIdentifier(scope, "object", "town", [=](si32 index)
  944. {
  945. // register town once objects are loaded
  946. JsonNode config = data["town"]["mapObject"];
  947. config["faction"].String() = name;
  948. config["faction"].meta = scope;
  949. VLC->objtypeh->loadSubObject(object->identifier, config, index, object->index);
  950. });
  951. }
  952. registerObject(scope, "faction", name, object->index);
  953. }
  954. void CTownHandler::loadRandomFaction()
  955. {
  956. static const ResourceID randomFactionPath("config/factions/random.json");
  957. JsonNode randomFactionJson(randomFactionPath);
  958. randomFactionJson.setMeta(CModHandler::scopeBuiltin(), true);
  959. loadBuildings(randomTown, randomFactionJson["random"]["town"]["buildings"]);
  960. }
  961. void CTownHandler::loadCustom()
  962. {
  963. loadRandomFaction();
  964. }
  965. void CTownHandler::afterLoadFinalization()
  966. {
  967. initializeRequirements();
  968. initializeOverridden();
  969. initializeWarMachines();
  970. }
  971. void CTownHandler::initializeRequirements()
  972. {
  973. // must be done separately after all ID's are known
  974. for (auto & requirement : requirementsToLoad)
  975. {
  976. requirement.building->requirements = CBuilding::TRequired(requirement.json, [&](const JsonNode & node) -> BuildingID
  977. {
  978. if (node.Vector().size() > 1)
  979. {
  980. logMod->error("Unexpected length of town buildings requirements: %d", node.Vector().size());
  981. logMod->error("Entry contains: ");
  982. logMod->error(node.toJson());
  983. }
  984. auto index = VLC->modh->identifiers.getIdentifier(requirement.town->getBuildingScope(), node[0]);
  985. if (!index.has_value())
  986. {
  987. logMod->error("Unknown building in town buildings: %s", node[0].String());
  988. return BuildingID::NONE;
  989. }
  990. return BuildingID(index.value());
  991. });
  992. }
  993. requirementsToLoad.clear();
  994. }
  995. void CTownHandler::initializeOverridden()
  996. {
  997. for(auto & bidHelper : overriddenBidsToLoad)
  998. {
  999. auto jsonNode = bidHelper.json;
  1000. auto scope = bidHelper.town->getBuildingScope();
  1001. for(const auto & b : jsonNode.Vector())
  1002. {
  1003. auto bid = BuildingID(VLC->modh->identifiers.getIdentifier(scope, b).value());
  1004. bidHelper.building->overrideBids.insert(bid);
  1005. }
  1006. }
  1007. overriddenBidsToLoad.clear();
  1008. }
  1009. void CTownHandler::initializeWarMachines()
  1010. {
  1011. // must be done separately after all objects are loaded
  1012. for(auto & p : warMachinesToLoad)
  1013. {
  1014. CTown * t = p.first;
  1015. JsonNode creatureKey = p.second;
  1016. auto ret = VLC->modh->identifiers.getIdentifier("creature", creatureKey, false);
  1017. if(ret)
  1018. {
  1019. const CCreature * creature = CreatureID(*ret).toCreature();
  1020. t->warMachine = creature->warMachine;
  1021. }
  1022. }
  1023. warMachinesToLoad.clear();
  1024. }
  1025. std::vector<bool> CTownHandler::getDefaultAllowed() const
  1026. {
  1027. std::vector<bool> allowedFactions;
  1028. allowedFactions.reserve(objects.size());
  1029. for(auto town : objects)
  1030. {
  1031. allowedFactions.push_back(town->town != nullptr);
  1032. }
  1033. return allowedFactions;
  1034. }
  1035. std::set<FactionID> CTownHandler::getAllowedFactions(bool withTown) const
  1036. {
  1037. std::set<FactionID> allowedFactions;
  1038. std::vector<bool> allowed;
  1039. if (withTown)
  1040. allowed = getDefaultAllowed();
  1041. else
  1042. allowed.resize( objects.size(), true);
  1043. for (size_t i=0; i<allowed.size(); i++)
  1044. if (allowed[i])
  1045. allowedFactions.insert(static_cast<FactionID>(i));
  1046. return allowedFactions;
  1047. }
  1048. const std::vector<std::string> & CTownHandler::getTypeNames() const
  1049. {
  1050. static const std::vector<std::string> typeNames = { "faction", "town" };
  1051. return typeNames;
  1052. }
  1053. VCMI_LIB_NAMESPACE_END