MiscObjects.cpp 34 KB

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