MiscObjects.cpp 34 KB

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