CTownHandler.cpp 37 KB

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