CGDwelling.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * CGDwelling.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 "CGDwelling.h"
  12. #include "../callback/IGameInfoCallback.h"
  13. #include "../callback/IGameEventCallback.h"
  14. #include "../callback/IGameRandomizer.h"
  15. #include "../serializer/JsonSerializeFormat.h"
  16. #include "../entities/faction/CTownHandler.h"
  17. #include "../mapping/CMap.h"
  18. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  19. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  20. #include "../mapObjectConstructors/DwellingInstanceConstructor.h"
  21. #include "../mapObjects/CGHeroInstance.h"
  22. #include "../mapObjects/CGTownInstance.h"
  23. #include "../networkPacks/StackLocation.h"
  24. #include "../networkPacks/PacksForClient.h"
  25. #include "../networkPacks/PacksForClientBattle.h"
  26. #include "../gameState/CGameState.h"
  27. #include "../CPlayerState.h"
  28. #include "../IGameSettings.h"
  29. #include "../CConfigHandler.h"
  30. #include "../texts/CGeneralTextHandler.h"
  31. #include <vstd/RNG.h>
  32. VCMI_LIB_NAMESPACE_BEGIN
  33. void CGDwellingRandomizationInfo::serializeJson(JsonSerializeFormat & handler)
  34. {
  35. handler.serializeString("sameAsTown", instanceId);
  36. handler.serializeIdArray("allowedFactions", allowedFactions);
  37. handler.serializeInt("minLevel", minLevel, static_cast<uint8_t>(1));
  38. handler.serializeInt("maxLevel", maxLevel, static_cast<uint8_t>(7));
  39. if(!handler.saving)
  40. {
  41. //todo: safely allow any level > 7
  42. vstd::abetween<uint8_t>(minLevel, 1, 7);
  43. vstd::abetween<uint8_t>(maxLevel, minLevel, 7);
  44. }
  45. }
  46. CGDwelling::CGDwelling(IGameInfoCallback *cb):
  47. CGDwelling(cb, BonusNodeType::ARMY)
  48. {}
  49. CGDwelling::CGDwelling(IGameInfoCallback *cb, BonusNodeType nodeType):
  50. CArmedInstance(cb, nodeType, false)
  51. {}
  52. CGDwelling::~CGDwelling() = default;
  53. FactionID CGDwelling::randomizeFaction(IGameRandomizer & gameRandomizer)
  54. {
  55. if (ID == Obj::RANDOM_DWELLING_FACTION)
  56. return FactionID(subID.getNum());
  57. assert(ID == Obj::RANDOM_DWELLING || ID == Obj::RANDOM_DWELLING_LVL);
  58. assert(randomizationInfo.has_value());
  59. if (!randomizationInfo)
  60. return FactionID::CASTLE;
  61. CGTownInstance * linkedTown = nullptr;
  62. if (!randomizationInfo->instanceId.empty())
  63. {
  64. auto iter = cb->gameState().getMap().instanceNames.find(randomizationInfo->instanceId);
  65. if(iter == cb->gameState().getMap().instanceNames.end())
  66. logGlobal->error("Map object not found: %s", randomizationInfo->instanceId);
  67. linkedTown = dynamic_cast<CGTownInstance *>(iter->second.get());
  68. }
  69. if (randomizationInfo->identifier != 0)
  70. {
  71. for(auto & townID : cb->gameState().getMap().getAllTowns())
  72. {
  73. auto town = cb->gameState().getTown(townID);
  74. if(town && town->identifier == randomizationInfo->identifier)
  75. {
  76. linkedTown = town;
  77. break;
  78. }
  79. }
  80. }
  81. if (linkedTown)
  82. {
  83. if(linkedTown->ID==Obj::RANDOM_TOWN)
  84. linkedTown->pickRandomObject(gameRandomizer); //we have to randomize the castle first
  85. assert(linkedTown->ID == Obj::TOWN);
  86. if(linkedTown->ID==Obj::TOWN)
  87. return linkedTown->getFactionID();
  88. }
  89. if(!randomizationInfo->allowedFactions.empty())
  90. return *RandomGeneratorUtil::nextItem(randomizationInfo->allowedFactions, gameRandomizer.getDefault());
  91. std::vector<FactionID> potentialPicks;
  92. for (FactionID faction(0); faction < FactionID(LIBRARY->townh->size()); ++faction)
  93. if (LIBRARY->factions()->getById(faction)->hasTown())
  94. potentialPicks.push_back(faction);
  95. assert(!potentialPicks.empty());
  96. return *RandomGeneratorUtil::nextItem(potentialPicks, gameRandomizer.getDefault());
  97. }
  98. int CGDwelling::randomizeLevel(vstd::RNG & rand)
  99. {
  100. if (ID == Obj::RANDOM_DWELLING_LVL)
  101. return subID.getNum();
  102. assert(ID == Obj::RANDOM_DWELLING || ID == Obj::RANDOM_DWELLING_FACTION);
  103. assert(randomizationInfo.has_value());
  104. if (!randomizationInfo)
  105. return rand.nextInt(1, 7) - 1;
  106. if(randomizationInfo->minLevel == randomizationInfo->maxLevel)
  107. return randomizationInfo->minLevel - 1;
  108. return rand.nextInt(randomizationInfo->minLevel, randomizationInfo->maxLevel) - 1;
  109. }
  110. void CGDwelling::pickRandomObject(IGameRandomizer & gameRandomizer)
  111. {
  112. if (ID == Obj::RANDOM_DWELLING || ID == Obj::RANDOM_DWELLING_LVL || ID == Obj::RANDOM_DWELLING_FACTION)
  113. {
  114. FactionID faction = randomizeFaction(gameRandomizer);
  115. int level = randomizeLevel(gameRandomizer.getDefault());
  116. assert(faction != FactionID::NONE && faction != FactionID::NEUTRAL);
  117. assert(level >= 0 && level <= 6);
  118. randomizationInfo.reset();
  119. CreatureID cid = (*LIBRARY->townh)[faction]->town->creatures[level][0];
  120. //NOTE: this will pick last dwelling with this creature (Mantis #900)
  121. //check for block map equality is better but more complex solution
  122. auto testID = [&](const Obj & primaryID) -> MapObjectSubID
  123. {
  124. auto dwellingIDs = LIBRARY->objtypeh->knownSubObjects(primaryID);
  125. for (MapObjectSubID entry : dwellingIDs)
  126. {
  127. const auto * handler = dynamic_cast<const DwellingInstanceConstructor *>(LIBRARY->objtypeh->getHandlerFor(primaryID, entry).get());
  128. if (!handler->isBannedForRandomDwelling() && handler->producesCreature(cid.toCreature()))
  129. return MapObjectSubID(entry);
  130. }
  131. return MapObjectSubID();
  132. };
  133. ID = Obj::CREATURE_GENERATOR1;
  134. subID = testID(Obj::CREATURE_GENERATOR1);
  135. if (subID == MapObjectSubID())
  136. {
  137. ID = Obj::CREATURE_GENERATOR4;
  138. subID = testID(Obj::CREATURE_GENERATOR4);
  139. }
  140. if (subID == MapObjectSubID())
  141. {
  142. logGlobal->error("Error: failed to find dwelling for %s of level %d", (*LIBRARY->townh)[faction]->getNameTranslated(), int(level));
  143. ID = Obj::CREATURE_GENERATOR1;
  144. subID = *RandomGeneratorUtil::nextItem(LIBRARY->objtypeh->knownSubObjects(Obj::CREATURE_GENERATOR1), gameRandomizer.getDefault());
  145. }
  146. setType(ID, subID);
  147. }
  148. }
  149. void CGDwelling::initObj(IGameRandomizer & gameRandomizer)
  150. {
  151. switch(ID.toEnum())
  152. {
  153. case Obj::CREATURE_GENERATOR1:
  154. case Obj::CREATURE_GENERATOR4:
  155. case Obj::WAR_MACHINE_FACTORY:
  156. {
  157. getObjectHandler()->configureObject(this, gameRandomizer);
  158. assert(!creatures.empty());
  159. assert(!creatures[0].second.empty());
  160. break;
  161. }
  162. case Obj::REFUGEE_CAMP:
  163. //is handled within newturn func
  164. break;
  165. default:
  166. assert(0);
  167. break;
  168. }
  169. }
  170. void CGDwelling::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  171. {
  172. switch (what)
  173. {
  174. case ObjProperty::AVAILABLE_CREATURE:
  175. creatures.resize(1);
  176. creatures[0].second.resize(1);
  177. creatures[0].second[0] = identifier.as<CreatureID>();
  178. break;
  179. }
  180. }
  181. void CGDwelling::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  182. {
  183. if(ID == Obj::REFUGEE_CAMP)
  184. {
  185. ChangeObjectVisitors cow;
  186. cow.mode = ChangeObjectVisitors::VISITOR_CLEAR;
  187. cow.object = id;
  188. gameEvents.sendAndApply(cow);
  189. cow.mode = ChangeObjectVisitors::VISITOR_ADD_PLAYER;
  190. cow.hero = h->id;
  191. gameEvents.sendAndApply(cow);
  192. if (!creatures[0].first) //Refugee Camp, no available cres
  193. {
  194. InfoWindow iw;
  195. iw.type = EInfoWindowMode::AUTO;
  196. iw.player = h->tempOwner;
  197. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted. Perhaps you should try next week.
  198. iw.text.replaceName(ID, subID);
  199. gameEvents.sendAndApply(iw);
  200. return;
  201. }
  202. }
  203. PlayerRelations relations = cb->getPlayerRelations( h->tempOwner, tempOwner );
  204. if ( relations == PlayerRelations::ALLIES )
  205. return;//do not allow recruiting or capturing
  206. if(relations == PlayerRelations::ENEMIES && stacksCount() > 0) //object is guarded, owned by enemy
  207. {
  208. BlockingDialog bd(true,false);
  209. bd.player = h->tempOwner;
  210. bd.text.appendLocalString(EMetaText::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards?
  211. bd.text.replaceTextID(getObjectHandler()->getNameTextID());
  212. if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
  213. bd.text.replaceRawString(CCreature::getQuantityRangeStringForId(Slots().begin()->second->getQuantityID()));
  214. else
  215. bd.text.replaceLocalString(EMetaText::ARRAY_TXT, 173 + (int)Slots().begin()->second->getQuantityID()*3);
  216. bd.text.replaceName(*Slots().begin()->second);
  217. gameEvents.showBlockingDialog(this, &bd);
  218. return;
  219. }
  220. // TODO this shouldn't be hardcoded
  221. if(relations == PlayerRelations::ENEMIES && ID != Obj::WAR_MACHINE_FACTORY && ID != Obj::REFUGEE_CAMP)
  222. {
  223. gameEvents.setOwner(this, h->tempOwner);
  224. }
  225. BlockingDialog bd (true,false);
  226. bd.player = h->tempOwner;
  227. if(ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4)
  228. {
  229. const size_t count = std::min<size_t>(creatures.size(), 4);
  230. constexpr std::array dwellingVisitTextID = {
  231. "core.advevent.35", // 0 creatures, should not happen
  232. "core.advevent.35",
  233. "vcmi.adventureMap.dwelling2",
  234. "vcmi.adventureMap.dwelling3",
  235. "core.advevent.36"
  236. };
  237. bd.text.appendTextID(dwellingVisitTextID[count]);
  238. bd.text.replaceTextID(getObjectHandler()->getNameTextID());
  239. for(const auto & elem : creatures)
  240. bd.text.replaceNamePlural(elem.second[0]);
  241. }
  242. else if(ID == Obj::REFUGEE_CAMP)
  243. {
  244. bd.text.appendLocalString(EMetaText::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  245. bd.text.replaceName(ID, subID);
  246. for(const auto & elem : creatures)
  247. bd.text.replaceNamePlural(elem.second[0]);
  248. }
  249. else if(ID == Obj::WAR_MACHINE_FACTORY)
  250. bd.text.appendLocalString(EMetaText::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  251. else
  252. throw std::runtime_error("Illegal dwelling!");
  253. if(ID == Obj::REFUGEE_CAMP || (ID == Obj::CREATURE_GENERATOR1 && LIBRARY->creatures()->getById(creatures[0].second[0])->getLevel() != 1))
  254. {
  255. bd.flags |= BlockingDialog::SAFE_TO_AUTOACCEPT;
  256. }
  257. gameEvents.showBlockingDialog(this, &bd);
  258. }
  259. void CGDwelling::newTurn(IGameEventCallback & gameEvents, IGameRandomizer & gameRandomizer) const
  260. {
  261. if(cb->getDate(Date::DAY_OF_WEEK) != 1) //not first day of week
  262. return;
  263. //town growths and War Machines Factories are handled separately
  264. if(ID == Obj::TOWN || ID == Obj::WAR_MACHINE_FACTORY)
  265. return;
  266. if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
  267. {
  268. ChangeObjectVisitors cow;
  269. cow.mode = ChangeObjectVisitors::VISITOR_CLEAR;
  270. cow.object = id;
  271. gameEvents.sendAndApply(cow);
  272. gameEvents.setObjPropertyID(id, ObjProperty::AVAILABLE_CREATURE, gameRandomizer.rollCreature());
  273. }
  274. bool change = false;
  275. SetAvailableCreatures sac;
  276. sac.creatures = creatures;
  277. sac.tid = id;
  278. for (size_t i = 0; i < creatures.size(); i++)
  279. {
  280. if(!creatures[i].second.empty())
  281. {
  282. bool creaturesAccumulate = false;
  283. if (tempOwner.isValidPlayer())
  284. creaturesAccumulate = cb->getSettings().getBoolean(EGameSettings::DWELLINGS_ACCUMULATE_WHEN_OWNED);
  285. else
  286. creaturesAccumulate = cb->getSettings().getBoolean(EGameSettings::DWELLINGS_ACCUMULATE_WHEN_NEUTRAL);
  287. const CCreature * cre =creatures[i].second[0].toCreature();
  288. TQuantity amount = cre->getGrowth() * (1 + cre->valOfBonuses(BonusType::CREATURE_GROWTH_PERCENT)/100) + cre->valOfBonuses(BonusType::CREATURE_GROWTH, BonusCustomSubtype::creatureLevel(cre->getLevel()));
  289. if (creaturesAccumulate && ID != Obj::REFUGEE_CAMP) //camp should not try to accumulate different kinds of creatures
  290. sac.creatures[i].first += amount;
  291. else
  292. sac.creatures[i].first = amount;
  293. change = true;
  294. }
  295. }
  296. if(change)
  297. gameEvents.sendAndApply(sac);
  298. updateGuards(gameEvents);
  299. }
  300. bool CGDwelling::wasVisited (PlayerColor player) const
  301. {
  302. return cb->getPlayerState(player)->visitedObjects.count(id) != 0;
  303. }
  304. std::vector<Component> CGDwelling::getPopupComponents(PlayerColor player) const
  305. {
  306. bool visitedByOwner = getOwner() == player;
  307. bool isDwelling = ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4;
  308. bool isRefugeeCamp = ID == Obj::REFUGEE_CAMP;
  309. bool canSeeUnitTypes = isDwelling || (isRefugeeCamp && wasVisited(player));
  310. bool canSeeUnitAmount = (isDwelling && visitedByOwner) || (isRefugeeCamp && wasVisited(player));
  311. std::vector<Component> result;
  312. if (creatures.empty())
  313. return result;
  314. if (canSeeUnitTypes)
  315. {
  316. for (auto const & creatureLevel : creatures)
  317. {
  318. for (auto const & creature : creatureLevel.second)
  319. {
  320. if (canSeeUnitAmount)
  321. result.emplace_back(ComponentType::CREATURE, creature, creatures.front().first);
  322. else
  323. result.emplace_back(ComponentType::CREATURE, creature);
  324. }
  325. }
  326. }
  327. return result;
  328. }
  329. void CGDwelling::updateGuards(IGameEventCallback & gameEvents) const
  330. {
  331. //TODO: store custom guard config and use it
  332. //TODO: store boolean flag for guards
  333. bool guarded = false;
  334. //default condition - creatures are of level 5 or higher
  335. for (auto creatureEntry : creatures)
  336. {
  337. if (LIBRARY->creatures()->getById(creatureEntry.second.at(0))->getLevel() >= 5 && ID != Obj::REFUGEE_CAMP)
  338. {
  339. guarded = true;
  340. break;
  341. }
  342. }
  343. if (guarded)
  344. {
  345. for (auto creatureEntry : creatures)
  346. {
  347. const CCreature * crea = creatureEntry.second.at(0).toCreature();
  348. SlotID slot = getSlotFor(crea->getId());
  349. if (hasStackAtSlot(slot)) //stack already exists, overwrite it
  350. {
  351. ChangeStackCount csc;
  352. csc.army = this->id;
  353. csc.slot = slot;
  354. csc.count = crea->getGrowth() * 3;
  355. csc.mode = ChangeValueMode::ABSOLUTE;
  356. gameEvents.sendAndApply(csc);
  357. }
  358. else //slot is empty, create whole new stack
  359. {
  360. InsertNewStack ns;
  361. ns.army = this->id;
  362. ns.slot = slot;
  363. ns.type = crea->getId();
  364. ns.count = crea->getGrowth() * 3;
  365. gameEvents.sendAndApply(ns);
  366. }
  367. }
  368. }
  369. }
  370. void CGDwelling::heroAcceptsCreatures(IGameEventCallback & gameEvents, const CGHeroInstance *h) const
  371. {
  372. CreatureID crid = creatures[0].second[0];
  373. auto *crs = crid.toCreature();
  374. TQuantity count = creatures[0].first;
  375. if(crs->getLevel() == 1 && ID != Obj::REFUGEE_CAMP) //first level - creatures are for free
  376. {
  377. if(count) //there are available creatures
  378. {
  379. if (cb->getSettings().getBoolean(EGameSettings::DWELLINGS_MERGE_ON_RECRUIT))
  380. {
  381. SlotID testSlot = h->getSlotFor(crid);
  382. if(!testSlot.validSlot()) //no available slot - try merging army of visiting hero
  383. {
  384. std::pair<SlotID, SlotID> toMerge;
  385. if (h->mergeableStacks(toMerge))
  386. {
  387. gameEvents.moveStack(StackLocation(h->id, toMerge.first), StackLocation(h->id, toMerge.second), -1); //merge toMerge.first into toMerge.second
  388. assert(!h->hasStackAtSlot(toMerge.first)); //we have now a new free slot
  389. }
  390. }
  391. }
  392. SlotID slot = h->getSlotFor(crid);
  393. if(!slot.validSlot()) //no available slot
  394. {
  395. InfoWindow iw;
  396. iw.type = EInfoWindowMode::AUTO;
  397. iw.player = h->tempOwner;
  398. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  399. iw.text.replaceNamePlural(crid);
  400. gameEvents.showInfoDialog(&iw);
  401. }
  402. else //give creatures
  403. {
  404. SetAvailableCreatures sac;
  405. sac.tid = id;
  406. sac.creatures = creatures;
  407. sac.creatures[0].first = 0;
  408. InfoWindow iw;
  409. iw.type = EInfoWindowMode::AUTO;
  410. iw.player = h->tempOwner;
  411. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 423); //%d %s join your army.
  412. iw.text.replaceNumber(count);
  413. iw.text.replaceNamePlural(crid);
  414. gameEvents.showInfoDialog(&iw);
  415. gameEvents.sendAndApply(sac);
  416. gameEvents.addToSlot(StackLocation(h->id, slot), crs, count);
  417. }
  418. }
  419. else //there no creatures
  420. {
  421. InfoWindow iw;
  422. iw.type = EInfoWindowMode::AUTO;
  423. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 422); //There are no %s here to recruit.
  424. iw.text.replaceNamePlural(crid);
  425. iw.player = h->tempOwner;
  426. gameEvents.sendAndApply(iw);
  427. }
  428. }
  429. else
  430. {
  431. if(ID == Obj::WAR_MACHINE_FACTORY) //pick available War Machines
  432. {
  433. //there is 1 war machine available to recruit if hero doesn't have one
  434. SetAvailableCreatures sac;
  435. sac.tid = id;
  436. sac.creatures = creatures;
  437. for (auto & entry : sac.creatures)
  438. {
  439. CreatureID creature = entry.second.at(0);
  440. ArtifactID warMachine = creature.toCreature()->warMachine;
  441. if (h->hasArt(warMachine, true, false))
  442. entry.first = 0;
  443. else
  444. entry.first = 1;
  445. }
  446. gameEvents.sendAndApply(sac);
  447. }
  448. auto windowMode = (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::REFUGEE_CAMP) ? EOpenWindowMode::RECRUITMENT_FIRST : EOpenWindowMode::RECRUITMENT_ALL;
  449. gameEvents.showObjectWindow(this, windowMode, h, true);
  450. }
  451. }
  452. void CGDwelling::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
  453. {
  454. if (result.winner == BattleSide::ATTACKER)
  455. {
  456. onHeroVisit(gameEvents, hero);
  457. }
  458. }
  459. void CGDwelling::blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const
  460. {
  461. auto relations = cb->getPlayerRelations(getOwner(), hero->getOwner());
  462. if(stacksCount() > 0 && relations == PlayerRelations::ENEMIES) //guards present
  463. {
  464. if(answer)
  465. gameEvents.startBattle(hero, this);
  466. }
  467. else if(answer)
  468. {
  469. heroAcceptsCreatures(gameEvents, hero);
  470. }
  471. }
  472. void CGDwelling::serializeJsonOptions(JsonSerializeFormat & handler)
  473. {
  474. switch (ID.toEnum())
  475. {
  476. case Obj::WAR_MACHINE_FACTORY:
  477. case Obj::REFUGEE_CAMP:
  478. //do nothing
  479. break;
  480. case Obj::RANDOM_DWELLING:
  481. case Obj::RANDOM_DWELLING_LVL:
  482. case Obj::RANDOM_DWELLING_FACTION:
  483. if (!handler.saving)
  484. randomizationInfo = CGDwellingRandomizationInfo();
  485. randomizationInfo->serializeJson(handler);
  486. [[fallthrough]];
  487. default:
  488. serializeJsonOwner(handler);
  489. break;
  490. }
  491. }
  492. const IOwnableObject * CGDwelling::asOwnable() const
  493. {
  494. switch (ID.toEnum())
  495. {
  496. case Obj::WAR_MACHINE_FACTORY:
  497. case Obj::REFUGEE_CAMP:
  498. return nullptr; // can't be owned
  499. default:
  500. return this;
  501. }
  502. }
  503. ResourceSet CGDwelling::dailyIncome() const
  504. {
  505. return {};
  506. }
  507. std::vector<CreatureID> CGDwelling::providedCreatures() const
  508. {
  509. if (ID == Obj::WAR_MACHINE_FACTORY || ID == Obj::REFUGEE_CAMP)
  510. return {};
  511. std::vector<CreatureID> result;
  512. for (const auto & level : creatures)
  513. result.insert(result.end(), level.second.begin(), level.second.end());
  514. return result;
  515. }
  516. VCMI_LIB_NAMESPACE_END