MiscObjects.cpp 34 KB

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