MiscObjects.cpp 33 KB

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