MiscObjects.cpp 34 KB

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