MiscObjects.cpp 33 KB

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