CTownHandler.cpp 37 KB

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