MiscObjects.cpp 34 KB

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