MiscObjects.cpp 32 KB

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