MiscObjects.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * MiscObjects.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 "MiscObjects.h"
  12. #include "../bonuses/Propagators.h"
  13. #include "../callback/IGameInfoCallback.h"
  14. #include "../callback/IGameEventCallback.h"
  15. #include "../callback/IGameRandomizer.h"
  16. #include "../constants/StringConstants.h"
  17. #include "../entities/artifact/ArtifactUtils.h"
  18. #include "../entities/artifact/CArtifact.h"
  19. #include "../entities/ResourceTypeHandler.h"
  20. #include "../CConfigHandler.h"
  21. #include "../texts/CGeneralTextHandler.h"
  22. #include "../CSkillHandler.h"
  23. #include "../spells/CSpellHandler.h"
  24. #include "../gameState/CGameState.h"
  25. #include "../mapping/CMap.h"
  26. #include "../CPlayerState.h"
  27. #include "../StartInfo.h"
  28. #include "../serializer/JsonSerializeFormat.h"
  29. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  30. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  31. #include "../mapObjectConstructors/CommonConstructors.h"
  32. #include "../mapObjects/CGHeroInstance.h"
  33. #include "../networkPacks/PacksForClient.h"
  34. #include "../networkPacks/PacksForClientBattle.h"
  35. #include "../networkPacks/StackLocation.h"
  36. #include "../lib/gameState/UpgradeInfo.h"
  37. #include <vstd/RNG.h>
  38. VCMI_LIB_NAMESPACE_BEGIN
  39. ///helpers
  40. static std::string visitedTxt(const bool visited)
  41. {
  42. int id = visited ? 352 : 353;
  43. return LIBRARY->generaltexth->allTexts[id];
  44. }
  45. void CTeamVisited::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  46. {
  47. if(what == ObjProperty::VISITED)
  48. players.insert(identifier.as<PlayerColor>());
  49. }
  50. bool CTeamVisited::wasVisited(PlayerColor player) const
  51. {
  52. return wasVisited(cb->getPlayerState(player)->team);
  53. }
  54. bool CTeamVisited::wasVisited(const CGHeroInstance * h) const
  55. {
  56. return wasVisited(h->tempOwner);
  57. }
  58. bool CTeamVisited::wasVisited(const TeamID & team) const
  59. {
  60. for(const auto & i : players)
  61. {
  62. if(cb->getPlayerState(i)->team == team)
  63. return true;
  64. }
  65. return false;
  66. }
  67. //CGMine
  68. std::shared_ptr<MineInstanceConstructor> CGMine::getResourceHandler() const
  69. {
  70. const auto & baseHandler = getObjectHandler();
  71. const auto & ourHandler = std::dynamic_pointer_cast<MineInstanceConstructor>(baseHandler);
  72. return ourHandler;
  73. }
  74. void CGMine::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  75. {
  76. auto relations = cb->getPlayerRelations(h->tempOwner, tempOwner);
  77. if(relations == PlayerRelations::SAME_PLAYER) //we're visiting our mine
  78. {
  79. gameEvents.showGarrisonDialog(id,h->id,true);
  80. return;
  81. }
  82. else if (relations == PlayerRelations::ALLIES)//ally
  83. return;
  84. if(stacksCount()) //Mine is guarded
  85. {
  86. BlockingDialog ynd(true,false);
  87. ynd.player = h->tempOwner;
  88. ynd.text.appendLocalString(EMetaText::ADVOB_TXT, isAbandoned() ? 84 : 187); //TODO: alternative text for custom guards
  89. gameEvents.showBlockingDialog(this, &ynd);
  90. return;
  91. }
  92. flagMine(gameEvents, h->tempOwner);
  93. }
  94. void CGMine::initObj(IGameRandomizer & gameRandomizer)
  95. {
  96. if(isAbandoned())
  97. {
  98. //set guardians
  99. int howManyGuards = gameRandomizer.getDefault().nextInt(abandonedMineGuards.minAmount, abandonedMineGuards.maxAmount);
  100. auto guards = std::make_unique<CStackInstance>(cb, abandonedMineGuards.creature, howManyGuards);
  101. putStack(SlotID(0), std::move(guards));
  102. assert(!abandonedMineResources.empty());
  103. if (!abandonedMineResources.empty())
  104. {
  105. producedResource = *RandomGeneratorUtil::nextItem(abandonedMineResources, gameRandomizer.getDefault());
  106. }
  107. else
  108. {
  109. logGlobal->error("Abandoned mine at (%s) has no valid resource candidates!", anchorPos().toString());
  110. producedResource = GameResID::GOLD;
  111. }
  112. }
  113. else
  114. {
  115. if(getResourceHandler()->getResourceType() == GameResID::NONE) // fallback
  116. producedResource = GameResID(getObjTypeIndex().getNum());
  117. else
  118. producedResource = getResourceHandler()->getResourceType();
  119. }
  120. producedQuantity = defaultResProduction();
  121. }
  122. bool CGMine::isAbandoned() const
  123. {
  124. return subID.getNum() >= 7 && getResourceHandler()->getResourceType() == GameResID::NONE;
  125. }
  126. const IOwnableObject * CGMine::asOwnable() const
  127. {
  128. return this;
  129. }
  130. std::vector<CreatureID> CGMine::providedCreatures() const
  131. {
  132. return {};
  133. }
  134. ResourceSet CGMine::dailyIncome() const
  135. {
  136. ResourceSet result;
  137. for (GameResID k : LIBRARY->resourceTypeHandler->getAllObjects())
  138. result[k] += valOfBonuses(BonusType::GENERATE_RESOURCE, BonusSubtypeID(k));
  139. result[producedResource] += defaultResProduction();
  140. const auto & playerSettings = cb->getPlayerSettings(getOwner());
  141. result.applyHandicap(playerSettings->handicap.percentIncome);
  142. return result;
  143. }
  144. std::string CGMine::getObjectName() const
  145. {
  146. if(getResourceHandler()->getResourceType() == GameResID::NONE || getObjTypeIndex() < GameConstants::RESOURCE_QUANTITY)
  147. return LIBRARY->generaltexth->translate("core.minename", getObjTypeIndex());
  148. else
  149. return getResourceHandler()->getNameTranslated();
  150. }
  151. std::string CGMine::getHoverText(PlayerColor player) const
  152. {
  153. std::string hoverName = CArmedInstance::getHoverText(player);
  154. if (tempOwner != PlayerColor::NEUTRAL)
  155. hoverName += "\n(" + producedResource.toResource()->getNameTranslated() + ")";
  156. if(stacksCount())
  157. {
  158. hoverName += "\n";
  159. hoverName += LIBRARY->generaltexth->allTexts[202]; //Guarded by
  160. hoverName += " ";
  161. hoverName += getArmyDescription();
  162. }
  163. return hoverName;
  164. }
  165. void CGMine::flagMine(IGameEventCallback & gameEvents, const PlayerColor & player) const
  166. {
  167. assert(tempOwner != player);
  168. gameEvents.setOwner(this, player); //not ours? flag it!
  169. InfoWindow iw;
  170. iw.type = EInfoWindowMode::AUTO;
  171. if(getResourceHandler()->getResourceType() == GameResID::NONE || getObjTypeIndex() < GameConstants::RESOURCE_QUANTITY)
  172. iw.text.appendTextID(TextIdentifier("core.mineevnt", producedResource.getNum()).get()); //not use subID, abandoned mines uses default mine texts
  173. else
  174. iw.text.appendRawString(getResourceHandler()->getDescriptionTranslated());
  175. iw.player = player;
  176. iw.components.emplace_back(ComponentType::RESOURCE_PER_DAY, producedResource, getProducedQuantity());
  177. gameEvents.showInfoDialog(&iw);
  178. }
  179. ui32 CGMine::defaultResProduction() const
  180. {
  181. if(isAbandoned())
  182. {
  183. switch(producedResource.toEnum())
  184. {
  185. case EGameResID::WOOD:
  186. case EGameResID::ORE:
  187. return 2;
  188. case EGameResID::GOLD:
  189. return 1000;
  190. default:
  191. return 1;
  192. }
  193. }
  194. return getResourceHandler()->getDefaultQuantity();
  195. }
  196. ui32 CGMine::getProducedQuantity() const
  197. {
  198. auto * playerSettings = cb->getPlayerSettings(getOwner());
  199. // always round up income - we don't want mines to always produce zero if handicap in use
  200. return vstd::divideAndCeil(producedQuantity * playerSettings->handicap.percentIncome, 100);
  201. }
  202. void CGMine::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
  203. {
  204. if(result.winner == BattleSide::ATTACKER) //attacker won
  205. {
  206. if(isAbandoned())
  207. {
  208. hero->showInfoDialog(gameEvents, 85); //TODO: alternative text for custom guards
  209. }
  210. flagMine(gameEvents, hero->tempOwner);
  211. }
  212. }
  213. void CGMine::blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const
  214. {
  215. if(answer)
  216. gameEvents.startBattle(hero, this);
  217. }
  218. void CGMine::serializeJsonOptions(JsonSerializeFormat & handler)
  219. {
  220. CArmedInstance::serializeJsonOptions(handler);
  221. serializeJsonOwner(handler);
  222. if(isAbandoned())
  223. handler.serializeIdArray<GameResID>("possibleResources", abandonedMineResources);
  224. }
  225. bool CGTeleport::isEntrance() const
  226. {
  227. return type == BOTH || type == ENTRANCE;
  228. }
  229. bool CGTeleport::isExit() const
  230. {
  231. return type == BOTH || type == EXIT;
  232. }
  233. bool CGTeleport::isChannelEntrance(const ObjectInstanceID & id) const
  234. {
  235. return vstd::contains(getAllEntrances(), id);
  236. }
  237. bool CGTeleport::isChannelExit(const ObjectInstanceID & id) const
  238. {
  239. return vstd::contains(getAllExits(), id);
  240. }
  241. std::vector<ObjectInstanceID> CGTeleport::getAllEntrances(bool excludeCurrent) const
  242. {
  243. auto ret = cb->getTeleportChannelEntrances(channel);
  244. if(excludeCurrent)
  245. vstd::erase_if_present(ret, id);
  246. return ret;
  247. }
  248. std::vector<ObjectInstanceID> CGTeleport::getAllExits(bool excludeCurrent) const
  249. {
  250. auto ret = cb->getTeleportChannelExits(channel);
  251. if(excludeCurrent)
  252. vstd::erase_if_present(ret, id);
  253. return ret;
  254. }
  255. ObjectInstanceID CGTeleport::getRandomExit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  256. {
  257. auto passableExits = getPassableExits(cb->gameState(), h, getAllExits(true));
  258. if(!passableExits.empty())
  259. return *RandomGeneratorUtil::nextItem(passableExits, gameEvents.getRandomGenerator());
  260. return ObjectInstanceID();
  261. }
  262. bool CGTeleport::isTeleport(const CGObjectInstance * obj)
  263. {
  264. return dynamic_cast<const CGTeleport *>(obj) != nullptr;
  265. }
  266. bool CGTeleport::isConnected(const CGTeleport * src, const CGTeleport * dst)
  267. {
  268. return src && dst && src->isChannelExit(dst->id);
  269. }
  270. bool CGTeleport::isConnected(const CGObjectInstance * src, const CGObjectInstance * dst)
  271. {
  272. const auto * srcObj = dynamic_cast<const CGTeleport *>(src);
  273. const auto * dstObj = dynamic_cast<const CGTeleport *>(dst);
  274. return isConnected(srcObj, dstObj);
  275. }
  276. bool CGTeleport::isExitPassable(const IGameInfoCallback & gameInfo, const CGHeroInstance * h, const CGObjectInstance * obj)
  277. {
  278. ObjectInstanceID topObjectID = gameInfo.getTile(obj->visitablePos())->topVisitableObj();
  279. const CGObjectInstance * topObject = gameInfo.getObjInstance(topObjectID);
  280. if(topObject->ID == Obj::HERO)
  281. {
  282. if(h->id == topObject->id) // Just to be sure it's won't happen.
  283. return false;
  284. // Check if it's friendly hero or not
  285. if(gameInfo.getPlayerRelations(h->tempOwner, topObject->tempOwner) != PlayerRelations::ENEMIES)
  286. {
  287. // Exchange between heroes only possible via subterranean gates
  288. if(!dynamic_cast<const CGSubterraneanGate *>(obj))
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. std::vector<ObjectInstanceID> CGTeleport::getPassableExits(const IGameInfoCallback & gameInfo, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits)
  295. {
  296. vstd::erase_if(exits, [&](const ObjectInstanceID & exit) -> bool
  297. {
  298. return !isExitPassable(gameInfo, h, gameInfo.getObj(exit));
  299. });
  300. return exits;
  301. }
  302. void CGTeleport::addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj)
  303. {
  304. std::shared_ptr<TeleportChannel> tc;
  305. if(channelsList.find(obj->channel) == channelsList.end())
  306. {
  307. tc = std::make_shared<TeleportChannel>();
  308. channelsList.insert(std::make_pair(obj->channel, tc));
  309. }
  310. else
  311. tc = channelsList[obj->channel];
  312. if(obj->isEntrance() && !vstd::contains(tc->entrances, obj->id))
  313. tc->entrances.push_back(obj->id);
  314. if(obj->isExit() && !vstd::contains(tc->exits, obj->id))
  315. tc->exits.push_back(obj->id);
  316. if(!tc->entrances.empty() && !tc->exits.empty()
  317. && (tc->entrances.size() != 1 || tc->entrances != tc->exits))
  318. {
  319. tc->passability = TeleportChannel::PASSABLE;
  320. }
  321. }
  322. TeleportChannelID CGMonolith::findMeChannel(const std::vector<Obj> & IDs, MapObjectSubID SubID) const
  323. {
  324. for(auto teleportObj : cb->gameState().getMap().getObjects<CGMonolith>())
  325. {
  326. if(vstd::contains(IDs, teleportObj->ID) && teleportObj->subID == SubID)
  327. return teleportObj->channel;
  328. }
  329. return TeleportChannelID();
  330. }
  331. void CGMonolith::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  332. {
  333. TeleportDialog td(h->id, channel);
  334. if(isEntrance())
  335. {
  336. if(cb->isTeleportChannelBidirectional(channel) && 1 < cb->getTeleportChannelExits(channel).size())
  337. {
  338. auto exits = cb->getTeleportChannelExits(channel);
  339. for(const auto & exit : exits)
  340. {
  341. td.exits.push_back(std::make_pair(exit, cb->getObj(exit)->visitablePos()));
  342. }
  343. }
  344. if(cb->isTeleportChannelImpassable(channel))
  345. {
  346. logGlobal->debug("Cannot find corresponding exit monolith for %d at %s", id.getNum(), anchorPos().toString());
  347. td.impassable = true;
  348. }
  349. else if(getRandomExit(gameEvents, h) == ObjectInstanceID())
  350. logGlobal->debug("All exits blocked for monolith %d at %s", id.getNum(), anchorPos().toString());
  351. }
  352. else
  353. h->showInfoDialog(gameEvents, 70);
  354. gameEvents.showTeleportDialog(&td);
  355. }
  356. void CGMonolith::teleportDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const
  357. {
  358. int3 dPos;
  359. auto randomExit = getRandomExit(gameEvents, hero);
  360. auto realExits = getAllExits(true);
  361. if(!isEntrance() // Do nothing if hero visited exit only object
  362. || (exits.empty() && realExits.empty()) // Do nothing if there no exits on this channel
  363. || ObjectInstanceID() == randomExit) // Do nothing if all exits are blocked by friendly hero and it's not subterranean gate
  364. {
  365. return;
  366. }
  367. else if(vstd::isValidIndex(exits, answer))
  368. dPos = exits[answer].second;
  369. else
  370. dPos = cb->getObj(randomExit)->visitablePos();
  371. gameEvents.moveHero(hero->id, hero->convertFromVisitablePos(dPos), EMovementMode::MONOLITH);
  372. }
  373. void CGMonolith::initObj(IGameRandomizer & gameRandomizer)
  374. {
  375. std::vector<Obj> IDs;
  376. IDs.push_back(ID);
  377. switch(ID.toEnum())
  378. {
  379. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  380. type = ENTRANCE;
  381. IDs.emplace_back(Obj::MONOLITH_ONE_WAY_EXIT);
  382. break;
  383. case Obj::MONOLITH_ONE_WAY_EXIT:
  384. type = EXIT;
  385. IDs.emplace_back(Obj::MONOLITH_ONE_WAY_ENTRANCE);
  386. break;
  387. case Obj::MONOLITH_TWO_WAY:
  388. default:
  389. type = BOTH;
  390. break;
  391. }
  392. channel = findMeChannel(IDs, subID);
  393. if(channel == TeleportChannelID())
  394. channel = TeleportChannelID(static_cast<si32>(cb->gameState().getMap().teleportChannels.size()));
  395. addToChannel(cb->gameState().getMap().teleportChannels, this);
  396. }
  397. void CGSubterraneanGate::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  398. {
  399. TeleportDialog td(h->id, channel);
  400. if(cb->isTeleportChannelImpassable(channel))
  401. {
  402. h->showInfoDialog(gameEvents, 153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  403. logGlobal->debug("Cannot find exit subterranean gate for %d at %s", id.getNum(), anchorPos().toString());
  404. td.impassable = true;
  405. }
  406. else
  407. {
  408. auto exit = getRandomExit(gameEvents, h);
  409. td.exits.push_back(std::make_pair(exit, cb->getObj(exit)->visitablePos()));
  410. }
  411. gameEvents.showTeleportDialog(&td);
  412. }
  413. void CGSubterraneanGate::initObj(IGameRandomizer & gameRandomizer)
  414. {
  415. type = BOTH;
  416. }
  417. void CGSubterraneanGate::postInit(IGameInfoCallback * cb) //matches subterranean gates into pairs
  418. {
  419. // for > 2 layers it's still choosing the nearest in x/y axis independent from level
  420. // collect all gates
  421. auto allGates = cb->gameState().getMap().getObjects<CGSubterraneanGate>();
  422. // sort by position for deterministic behavior
  423. std::sort(allGates.begin(), allGates.end(), [](const CGObjectInstance * a, const CGObjectInstance * b)
  424. {
  425. return a->visitablePos() < b->visitablePos();
  426. });
  427. auto assignToChannel = [&](CGSubterraneanGate * obj)
  428. {
  429. if(obj->channel == TeleportChannelID())
  430. { // if object not linked to channel then create new channel
  431. obj->channel = TeleportChannelID(static_cast<si32>(cb->gameState().getMap().teleportChannels.size()));
  432. addToChannel(cb->gameState().getMap().teleportChannels, obj);
  433. }
  434. };
  435. for(size_t i = 0; i < allGates.size(); i++)
  436. {
  437. CGSubterraneanGate * objCurrent = allGates[i];
  438. if(objCurrent->channel != TeleportChannelID())
  439. continue;
  440. // find nearest gate on any other layer
  441. std::pair<int, si32> best(-1, std::numeric_limits<si32>::max()); //pair<pos_in_vector, distance^2>
  442. for(size_t j = 0; j < allGates.size(); j++)
  443. {
  444. if(i == j)
  445. continue;
  446. CGSubterraneanGate * checked = allGates[j];
  447. if(checked->channel != TeleportChannelID())
  448. continue;
  449. // Prefer pairing surface → underground
  450. if(objCurrent->visitablePos().z > checked->visitablePos().z)
  451. continue;
  452. if(checked->visitablePos().z == objCurrent->visitablePos().z)
  453. continue;
  454. si32 hlp = checked->visitablePos().dist2dSQ(objCurrent->visitablePos());
  455. if(hlp < best.second)
  456. {
  457. best.first = static_cast<int>(j);
  458. best.second = hlp;
  459. }
  460. }
  461. assignToChannel(objCurrent);
  462. if(best.first >= 0) //found pair
  463. {
  464. allGates[best.first]->channel = objCurrent->channel;
  465. addToChannel(cb->gameState().getMap().teleportChannels, allGates[best.first]);
  466. }
  467. }
  468. // assign empty channels to any remaining unpaired gates
  469. for(auto & i : allGates)
  470. assignToChannel(i);
  471. }
  472. void CGWhirlpool::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  473. {
  474. TeleportDialog td(h->id, channel);
  475. if(cb->isTeleportChannelImpassable(channel))
  476. {
  477. logGlobal->debug("Cannot find exit whirlpool for %d at %s", id.getNum(), anchorPos().toString());
  478. td.impassable = true;
  479. }
  480. else if(getRandomExit(gameEvents, h) == ObjectInstanceID())
  481. logGlobal->debug("All exits are blocked for whirlpool %d at %s", id.getNum(), anchorPos().toString());
  482. if(!isProtected(h))
  483. {
  484. SlotID targetstack = h->Slots().begin()->first; //slot numbers may vary
  485. for(auto i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  486. {
  487. if(h->getPower(targetstack) > h->getPower(i->first))
  488. targetstack = (i->first);
  489. }
  490. auto countToTake = static_cast<TQuantity>(h->getStackCount(targetstack) * 0.5);
  491. vstd::amax(countToTake, 1);
  492. InfoWindow iw;
  493. iw.type = EInfoWindowMode::AUTO;
  494. iw.player = h->tempOwner;
  495. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 168);
  496. iw.components.emplace_back(ComponentType::CREATURE, h->getCreature(targetstack)->getId(), -countToTake);
  497. gameEvents.showInfoDialog(&iw);
  498. gameEvents.changeStackCount(StackLocation(h->id, targetstack), -countToTake, ChangeValueMode::RELATIVE);
  499. }
  500. else
  501. {
  502. auto exits = getAllExits();
  503. for(const auto & exit : exits)
  504. {
  505. auto blockedPosList = cb->getObj(exit)->getBlockedPos();
  506. for(const auto & bPos : blockedPosList)
  507. td.exits.push_back(std::make_pair(exit, bPos));
  508. }
  509. }
  510. gameEvents.showTeleportDialog(&td);
  511. }
  512. void CGWhirlpool::teleportDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const
  513. {
  514. int3 dPos;
  515. auto realExits = getAllExits();
  516. if(exits.empty() && realExits.empty())
  517. return;
  518. else if(vstd::isValidIndex(exits, answer))
  519. dPos = exits[answer].second;
  520. else
  521. {
  522. auto exit = getRandomExit(gameEvents, hero);
  523. if(exit == ObjectInstanceID())
  524. return;
  525. const auto * obj = cb->getObj(exit);
  526. std::set<int3> tiles = obj->getBlockedPos();
  527. dPos = *RandomGeneratorUtil::nextItem(tiles, gameEvents.getRandomGenerator());
  528. }
  529. gameEvents.moveHero(hero->id, hero->convertFromVisitablePos(dPos), EMovementMode::MONOLITH);
  530. }
  531. bool CGWhirlpool::isProtected(const CGHeroInstance * h)
  532. {
  533. return h->hasBonusOfType(BonusType::WHIRLPOOL_PROTECTION)
  534. || (h->stacksCount() == 1 && h->Slots().begin()->second->getCount() == 1)
  535. || (h->stacksCount() == 0 && h->getCommander() && h->getCommander()->alive);
  536. }
  537. const CArtifactInstance * CGArtifact::getArtifactInstance() const
  538. {
  539. return cb->getArtInstance(storedArtifact);
  540. }
  541. ArtifactID CGArtifact::getArtifactType() const
  542. {
  543. if(ID == Obj::SPELL_SCROLL)
  544. return ArtifactID::SPELL_SCROLL;
  545. assert(ID == Obj::ARTIFACT);
  546. return getObjTypeIndex().getNum();
  547. }
  548. void CGArtifact::pickRandomObject(IGameRandomizer & gameRandomizer)
  549. {
  550. switch(ID.toEnum())
  551. {
  552. case MapObjectID::RANDOM_ART:
  553. subID = gameRandomizer.rollArtifact();
  554. break;
  555. case MapObjectID::RANDOM_TREASURE_ART:
  556. subID = gameRandomizer.rollArtifact(EArtifactClass::ART_TREASURE);
  557. break;
  558. case MapObjectID::RANDOM_MINOR_ART:
  559. subID = gameRandomizer.rollArtifact(EArtifactClass::ART_MINOR);
  560. break;
  561. case MapObjectID::RANDOM_MAJOR_ART:
  562. subID = gameRandomizer.rollArtifact(EArtifactClass::ART_MAJOR);
  563. break;
  564. case MapObjectID::RANDOM_RELIC_ART:
  565. subID = gameRandomizer.rollArtifact(EArtifactClass::ART_RELIC);
  566. break;
  567. }
  568. if (ID != MapObjectID::SPELL_SCROLL && ID != MapObjectID::ARTIFACT)
  569. {
  570. ID = MapObjectID::ARTIFACT;
  571. setType(ID, subID);
  572. }
  573. else if (ID != MapObjectID::SPELL_SCROLL)
  574. ID = MapObjectID::ARTIFACT;
  575. }
  576. void CGArtifact::setArtifactInstance(const CArtifactInstance * instance)
  577. {
  578. storedArtifact = instance->getId();
  579. }
  580. void CGArtifact::initObj(IGameRandomizer & gameRandomizer)
  581. {
  582. assert(ID == Obj::ARTIFACT || ID == Obj::SPELL_SCROLL);
  583. blockVisit = true;
  584. if(ID == Obj::ARTIFACT)
  585. {
  586. assert(getArtifactType().hasValue());
  587. if (!storedArtifact.hasValue())
  588. setArtifactInstance(cb->gameState().createArtifact(getArtifactType()));
  589. }
  590. if(ID == Obj::SPELL_SCROLL)
  591. subID = 1;
  592. assert(getArtifactInstance()->getType());
  593. assert(!getArtifactInstance()->getParentNodes().empty());
  594. }
  595. std::string CGArtifact::getObjectName() const
  596. {
  597. if(ID == Obj::SPELL_SCROLL || ID == Obj::ARTIFACT)
  598. return getArtifactType().toEntity(LIBRARY)->getNameTranslated();
  599. // random artifact
  600. return CGObjectInstance::getObjectName();
  601. }
  602. std::string CGArtifact::getPopupText(PlayerColor player) const
  603. {
  604. if (settings["general"]["enableUiEnhancements"].Bool())
  605. {
  606. std::string description = getArtifactType().toEntity(LIBRARY)->getDescriptionTranslated();
  607. if (getArtifactType() == ArtifactID::SPELL_SCROLL)
  608. ArtifactUtils::insertScrrollSpellName(description, SpellID::NONE); // erase text placeholder
  609. return description;
  610. }
  611. else
  612. return getObjectName();
  613. }
  614. std::string CGArtifact::getPopupText(const CGHeroInstance * hero) const
  615. {
  616. return getPopupText(hero->getOwner());
  617. }
  618. std::vector<Component> CGArtifact::getPopupComponents(PlayerColor player) const
  619. {
  620. return {
  621. Component(ComponentType::ARTIFACT, getArtifactType())
  622. };
  623. }
  624. void CGArtifact::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  625. {
  626. if(!stacksCount())
  627. {
  628. InfoWindow iw;
  629. iw.type = EInfoWindowMode::AUTO;
  630. iw.player = h->tempOwner;
  631. if(getArtifactInstance()->getType()->canBePutAt(h))
  632. {
  633. switch (ID.toEnum())
  634. {
  635. case Obj::ARTIFACT:
  636. {
  637. iw.components.emplace_back(ComponentType::ARTIFACT, getArtifactType());
  638. if(!message.empty())
  639. iw.text = message;
  640. else
  641. iw.text.appendTextID(getArtifactType().toArtifact()->getEventTextID());
  642. }
  643. break;
  644. case Obj::SPELL_SCROLL:
  645. {
  646. SpellID spell = getArtifactInstance()->getScrollSpellID();
  647. iw.components.emplace_back(ComponentType::SPELL, spell);
  648. if(!message.empty())
  649. iw.text = message;
  650. else
  651. {
  652. iw.text.appendLocalString(EMetaText::ADVOB_TXT,135);
  653. iw.text.replaceName(spell);
  654. }
  655. }
  656. break;
  657. }
  658. }
  659. else
  660. {
  661. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 2);
  662. }
  663. gameEvents.showInfoDialog(&iw);
  664. pick(gameEvents, h);
  665. }
  666. else
  667. {
  668. switch(ID.toEnum())
  669. {
  670. case Obj::ARTIFACT:
  671. {
  672. BlockingDialog ynd(true,false);
  673. ynd.player = h->getOwner();
  674. if(!message.empty())
  675. ynd.text = message;
  676. else
  677. {
  678. // TODO: Guard text is more complex in H3, see mantis issue 2325 for details
  679. ynd.text.appendLocalString(EMetaText::GENERAL_TXT, 420);
  680. ynd.text.replaceRawString("");
  681. ynd.text.replaceRawString(getArmyDescription());
  682. ynd.text.replaceLocalString(EMetaText::GENERAL_TXT, 43); // creatures
  683. }
  684. gameEvents.showBlockingDialog(this, &ynd);
  685. }
  686. break;
  687. case Obj::SPELL_SCROLL:
  688. {
  689. if(!message.empty())
  690. {
  691. BlockingDialog ynd(true,false);
  692. ynd.player = h->getOwner();
  693. ynd.text = message;
  694. gameEvents.showBlockingDialog(this, &ynd);
  695. }
  696. else
  697. blockingDialogAnswered(gameEvents, h, true);
  698. }
  699. break;
  700. }
  701. }
  702. }
  703. void CGArtifact::pick(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  704. {
  705. if(gameEvents.putArtifact(ArtifactLocation(h->id, ArtifactPosition::FIRST_AVAILABLE), getArtifactInstance()->getId()))
  706. gameEvents.removeObject(this, h->getOwner());
  707. }
  708. BattleField CGArtifact::getBattlefield() const
  709. {
  710. return BattleField::NONE;
  711. }
  712. void CGArtifact::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
  713. {
  714. if(result.winner == BattleSide::ATTACKER) //attacker won
  715. pick(gameEvents, hero);
  716. }
  717. void CGArtifact::blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const
  718. {
  719. if(answer)
  720. gameEvents.startBattle(hero, this);
  721. }
  722. void CGArtifact::serializeJsonOptions(JsonSerializeFormat& handler)
  723. {
  724. handler.serializeStruct("guardMessage", message);
  725. CArmedInstance::serializeJsonOptions(handler);
  726. if(!handler.saving && !handler.getCurrent()["guards"].Vector().empty())
  727. CCreatureSet::serializeJson(handler, "guards", 7);
  728. if(handler.saving && ID == Obj::SPELL_SCROLL)
  729. {
  730. const auto & b = getArtifactInstance()->getFirstBonus(Selector::type()(BonusType::SPELL));
  731. SpellID spellId(b->subtype.as<SpellID>());
  732. handler.serializeId("spell", spellId, SpellID::NONE);
  733. }
  734. }
  735. void CGSignBottle::initObj(IGameRandomizer & gameRandomizer)
  736. {
  737. //if no text is set than we pick random from the predefined ones
  738. if(message.empty())
  739. {
  740. auto vector = LIBRARY->generaltexth->findStringsWithPrefix("core.randsign");
  741. std::string messageIdentifier = *RandomGeneratorUtil::nextItem(vector, gameRandomizer.getDefault());
  742. message.appendTextID(messageIdentifier);
  743. }
  744. if(ID == Obj::OCEAN_BOTTLE)
  745. {
  746. blockVisit = true;
  747. }
  748. }
  749. void CGSignBottle::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  750. {
  751. InfoWindow iw;
  752. iw.player = h->getOwner();
  753. iw.text = message;
  754. gameEvents.showInfoDialog(&iw);
  755. if(ID == Obj::OCEAN_BOTTLE)
  756. gameEvents.removeObject(this, h->getOwner());
  757. }
  758. void CGSignBottle::serializeJsonOptions(JsonSerializeFormat& handler)
  759. {
  760. handler.serializeStruct("text", message);
  761. }
  762. const IOwnableObject * CGGarrison::asOwnable() const
  763. {
  764. return this;
  765. }
  766. ResourceSet CGGarrison::dailyIncome() const
  767. {
  768. ResourceSet result;
  769. for (GameResID k : LIBRARY->resourceTypeHandler->getAllObjects())
  770. result[k] += valOfBonuses(BonusType::GENERATE_RESOURCE, BonusSubtypeID(k));
  771. return result;
  772. }
  773. std::vector<CreatureID> CGGarrison::providedCreatures() const
  774. {
  775. return {};
  776. }
  777. void CGGarrison::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  778. {
  779. auto relations = cb->getPlayerRelations(h->tempOwner, tempOwner);
  780. if (relations == PlayerRelations::ENEMIES && stacksCount() > 0) {
  781. //TODO: Find a way to apply magic garrison effects in battle.
  782. gameEvents.startBattle(h, this);
  783. return;
  784. }
  785. //New owner.
  786. if (relations == PlayerRelations::ENEMIES)
  787. gameEvents.setOwner(this, h->tempOwner);
  788. gameEvents.showGarrisonDialog(id, h->id, removableUnits);
  789. }
  790. bool CGGarrison::passableFor(PlayerColor player) const
  791. {
  792. //FIXME: identical to same method in CGTownInstance
  793. if ( !stacksCount() )//empty - anyone can visit
  794. return true;
  795. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  796. return false;
  797. if (cb->getPlayerRelations(tempOwner, player) != PlayerRelations::ENEMIES)
  798. return true;
  799. return false;
  800. }
  801. void CGGarrison::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
  802. {
  803. if (result.winner == BattleSide::ATTACKER)
  804. onHeroVisit(gameEvents, hero);
  805. }
  806. void CGGarrison::serializeJsonOptions(JsonSerializeFormat& handler)
  807. {
  808. handler.serializeBool("removableUnits", removableUnits);
  809. serializeJsonOwner(handler);
  810. CArmedInstance::serializeJsonOptions(handler);
  811. }
  812. void CGGarrison::initObj(IGameRandomizer & gameRandomizer)
  813. {
  814. if(this->subID == MapObjectSubID::decode(this->ID, "antiMagic"))
  815. addAntimagicGarrisonBonus();
  816. }
  817. void CGGarrison::addAntimagicGarrisonBonus()
  818. {
  819. auto bonus = std::make_shared<Bonus>();
  820. bonus->type = BonusType::BLOCK_ALL_MAGIC;
  821. bonus->source = BonusSource::OBJECT_TYPE;
  822. bonus->sid = BonusSourceID(this->ID);
  823. bonus->propagator = std::make_shared<CPropagatorNodeType>(BonusNodeType::BATTLE_WIDE);
  824. bonus->duration = BonusDuration::PERMANENT;
  825. this->addNewBonus(bonus);
  826. }
  827. void CGMagi::initObj(IGameRandomizer & gameRandomizer)
  828. {
  829. if (ID == Obj::EYE_OF_MAGI)
  830. blockVisit = true;
  831. }
  832. void CGMagi::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  833. {
  834. if (ID == Obj::HUT_OF_MAGI)
  835. {
  836. h->showInfoDialog(gameEvents, 61);
  837. std::vector<const CGObjectInstance *> eyes;
  838. for (const auto & object : cb->gameState().getMap().getObjects<CGMagi>())
  839. {
  840. if (object->ID == Obj::EYE_OF_MAGI && object->subID == this->subID)
  841. eyes.push_back(object);
  842. }
  843. if (!eyes.empty())
  844. {
  845. CenterView cv;
  846. cv.player = h->tempOwner;
  847. cv.focusTime = 2000;
  848. FoWChange fw;
  849. fw.player = h->tempOwner;
  850. fw.mode = ETileVisibility::REVEALED;
  851. fw.waitForDialogs = true;
  852. for(const auto & eye : eyes)
  853. {
  854. cb->getTilesInRange (fw.tiles, eye->visitablePos(), 10, ETileVisibility::HIDDEN, h->tempOwner);
  855. gameEvents.sendAndApply(fw);
  856. cv.pos = eye->visitablePos();
  857. gameEvents.sendAndApply(cv);
  858. }
  859. cv.pos = h->visitablePos();
  860. cv.focusTime = 0;
  861. gameEvents.sendAndApply(cv);
  862. }
  863. }
  864. else if (ID == Obj::EYE_OF_MAGI)
  865. {
  866. h->showInfoDialog(gameEvents, 48);
  867. }
  868. }
  869. CGBoat::CGBoat(IGameInfoCallback * cb)
  870. : CGObjectInstance(cb)
  871. , CBonusSystemNode(BonusNodeType::BOAT)
  872. {
  873. direction = 4;
  874. layer = EPathfindingLayer::SAIL;
  875. }
  876. bool CGBoat::isCoastVisitable() const
  877. {
  878. return true;
  879. }
  880. void CGBoat::setBoardedHero(const CGHeroInstance * hero)
  881. {
  882. if (hero)
  883. boardedHeroID = hero->id;
  884. else
  885. boardedHeroID = ObjectInstanceID();
  886. }
  887. const CGHeroInstance * CGBoat::getBoardedHero() const
  888. {
  889. if (boardedHeroID.hasValue())
  890. return cb->getHero(boardedHeroID);
  891. else
  892. return nullptr;
  893. }
  894. void CGSirens::initObj(IGameRandomizer & gameRandomizer)
  895. {
  896. blockVisit = true;
  897. }
  898. std::string CGSirens::getHoverText(const CGHeroInstance * hero) const
  899. {
  900. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID)));
  901. }
  902. void CGSirens::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  903. {
  904. InfoWindow iw;
  905. iw.player = h->tempOwner;
  906. if(h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID))) //has already visited Sirens
  907. {
  908. iw.type = EInfoWindowMode::AUTO;
  909. iw.text.appendLocalString(EMetaText::ADVOB_TXT,133);
  910. }
  911. else
  912. {
  913. giveDummyBonus(gameEvents, h->id, BonusDuration::ONE_BATTLE);
  914. TExpType xp = 0;
  915. for (auto i = h->Slots().begin(); i != h->Slots().end(); i++)
  916. {
  917. // 1-sized stacks are not affected by sirens
  918. if (i->second->getCount() == 1)
  919. continue;
  920. // tested H3 behavior: 30% (rounded up) of stack drowns
  921. TQuantity drown = std::ceil(i->second->getCount() * 0.3);
  922. if(drown)
  923. {
  924. gameEvents.changeStackCount(StackLocation(h->id, i->first), -drown, ChangeValueMode::RELATIVE);
  925. xp += drown * i->second->getType()->getMaxHealth();
  926. }
  927. }
  928. if(xp)
  929. {
  930. xp = h->calculateXp(static_cast<int>(xp));
  931. iw.text.appendLocalString(EMetaText::ADVOB_TXT,132);
  932. iw.text.replaceNumber(static_cast<int>(xp));
  933. gameEvents.giveExperience(h, xp);
  934. }
  935. else
  936. {
  937. iw.text.appendLocalString(EMetaText::ADVOB_TXT,134);
  938. }
  939. }
  940. gameEvents.showInfoDialog(&iw);
  941. }
  942. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  943. {
  944. // H J L K I
  945. // A x S x B
  946. // C E G F D
  947. offsets = {
  948. {-2, 0, 0}, // A
  949. {+2, 0, 0}, // B
  950. {-2, 1, 0}, // C
  951. {+2, 1, 0}, // D
  952. {-1, 1, 0}, // E
  953. {+1, 1, 0}, // F
  954. {0, 1, 0}, // G
  955. {-2, -1, 0}, // H
  956. {+2, -1, 0}, // I
  957. {-1, -1, 0}, // G
  958. {+1, -1, 0}, // K
  959. {0, -1, 0}, // L
  960. };
  961. }
  962. const IObjectInterface * CGShipyard::getObject() const
  963. {
  964. return this;
  965. }
  966. void CGShipyard::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  967. {
  968. if(cb->getPlayerRelations(tempOwner, h->tempOwner) == PlayerRelations::ENEMIES)
  969. gameEvents.setOwner(this, h->tempOwner);
  970. if(shipyardStatus() != IBoatGenerator::GOOD)
  971. {
  972. InfoWindow iw;
  973. iw.type = EInfoWindowMode::AUTO;
  974. iw.player = tempOwner;
  975. getProblemText(iw.text, h);
  976. gameEvents.showInfoDialog(&iw);
  977. }
  978. else
  979. {
  980. gameEvents.showObjectWindow(this, EOpenWindowMode::SHIPYARD_WINDOW, h, false);
  981. }
  982. }
  983. void CGShipyard::serializeJsonOptions(JsonSerializeFormat& handler)
  984. {
  985. serializeJsonOwner(handler);
  986. }
  987. BoatId CGShipyard::getBoatType() const
  988. {
  989. return createdBoat;
  990. }
  991. const IOwnableObject * CGShipyard::asOwnable() const
  992. {
  993. return this;
  994. }
  995. ResourceSet CGShipyard::dailyIncome() const
  996. {
  997. return {};
  998. }
  999. std::vector<CreatureID> CGShipyard::providedCreatures() const
  1000. {
  1001. return {};
  1002. }
  1003. void CGDenOfthieves::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  1004. {
  1005. gameEvents.showObjectWindow(this, EOpenWindowMode::THIEVES_GUILD, h, false);
  1006. }
  1007. void CGObelisk::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  1008. {
  1009. InfoWindow iw;
  1010. iw.type = EInfoWindowMode::AUTO;
  1011. iw.player = h->tempOwner;
  1012. const TeamState *ts = cb->getPlayerTeam(h->tempOwner);
  1013. assert(ts);
  1014. TeamID team = ts->id;
  1015. if(!wasVisited(team))
  1016. {
  1017. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 96);
  1018. gameEvents.sendAndApply(iw);
  1019. // increment general visited obelisks counter
  1020. gameEvents.setObjPropertyID(id, ObjProperty::OBELISK_VISITED, team);
  1021. gameEvents.showObjectWindow(this, EOpenWindowMode::PUZZLE_MAP, h, false);
  1022. // mark that particular obelisk as visited for all players in the team
  1023. for(const auto & color : ts->players)
  1024. {
  1025. gameEvents.setObjPropertyID(id, ObjProperty::VISITED, color);
  1026. }
  1027. }
  1028. else
  1029. {
  1030. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 97);
  1031. gameEvents.sendAndApply(iw);
  1032. }
  1033. }
  1034. void CGObelisk::initObj(IGameRandomizer & gameRandomizer)
  1035. {
  1036. cb->gameState().getMap().obeliskCount++;
  1037. }
  1038. std::string CGObelisk::getHoverText(PlayerColor player) const
  1039. {
  1040. return getObjectName() + " " + visitedTxt(wasVisited(player));
  1041. }
  1042. std::string CGObelisk::getObjectDescription(PlayerColor player) const
  1043. {
  1044. return visitedTxt(wasVisited(player));
  1045. }
  1046. void CGObelisk::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  1047. {
  1048. switch(what)
  1049. {
  1050. case ObjProperty::OBELISK_VISITED:
  1051. {
  1052. auto progress = ++cb->gameState().getMap().obelisksVisited[identifier.as<TeamID>()];
  1053. logGlobal->debug("Player %d: obelisk progress %d / %d", identifier.getNum(), static_cast<int>(progress) , static_cast<int>(cb->gameState().getMap().obeliskCount));
  1054. if(progress > cb->gameState().getMap().obeliskCount)
  1055. {
  1056. logGlobal->error("Visited %d of %d", static_cast<int>(progress), cb->gameState().getMap().obeliskCount);
  1057. throw std::runtime_error("Player visited " + std::to_string(progress) + " obelisks out of " + std::to_string(cb->gameState().getMap().obeliskCount) + " present on map!");
  1058. }
  1059. break;
  1060. }
  1061. default:
  1062. CTeamVisited::setPropertyDer(what, identifier);
  1063. break;
  1064. }
  1065. }
  1066. void HillFort::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  1067. {
  1068. gameEvents.showObjectWindow(this, EOpenWindowMode::HILL_FORT_WINDOW, h, false);
  1069. }
  1070. void HillFort::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const
  1071. {
  1072. int32_t level = stack.getType()->getLevel();
  1073. int32_t index = std::clamp<int32_t>(level - 1, 0, upgradeCostPercentage.size() - 1);
  1074. int costModifier = upgradeCostPercentage[index];
  1075. for(const auto & nid : stack.getCreature()->upgrades)
  1076. {
  1077. info.addUpgrade(nid, stack.getType(), costModifier);
  1078. }
  1079. }
  1080. std::string HillFort::getPopupText(PlayerColor player) const
  1081. {
  1082. MetaString message = MetaString::createFromRawString("{%s}\r\n\r\n%s");
  1083. message.replaceName(ID, subID);
  1084. message.replaceTextID(getDescriptionToolTip());
  1085. return message.toString();
  1086. }
  1087. std::string HillFort::getPopupText(const CGHeroInstance * hero) const
  1088. {
  1089. return getPopupText(hero->getOwner());
  1090. }
  1091. std::string HillFort::getDescriptionToolTip() const
  1092. {
  1093. return TextIdentifier(getObjectHandler()->getBaseTextID(), "description").get();
  1094. }
  1095. std::string HillFort::getUnavailableUpgradeMessage() const
  1096. {
  1097. assert(getObjectHandler()->getModScope() != "core");
  1098. return TextIdentifier(getObjectHandler()->getBaseTextID(), "unavailableUpgradeMessage").get();
  1099. }
  1100. VCMI_LIB_NAMESPACE_END