MiscObjects.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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 "../NetPacks.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CSoundBase.h"
  15. #include "../CModHandler.h"
  16. #include "CObjectClassesHandler.h"
  17. #include "../CSpellHandler.h"
  18. #include "../IGameCallback.h"
  19. #include "../CGameState.h"
  20. using namespace boost::assign;
  21. std::map<Obj, std::map<int, std::vector<ObjectInstanceID> > > CGTeleport::objs;
  22. std::vector<std::pair<ObjectInstanceID, ObjectInstanceID> > CGTeleport::gates;
  23. std::map <si32, std::vector<ObjectInstanceID> > CGMagi::eyelist;
  24. ui8 CGObelisk::obeliskCount; //how many obelisks are on map
  25. std::map<TeamID, ui8> CGObelisk::visited; //map: team_id => how many obelisks has been visited
  26. ///helpers
  27. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  28. {
  29. OpenWindow ow;
  30. ow.window = type;
  31. ow.id1 = id1;
  32. ow.id2 = id2;
  33. IObjectInterface::cb->sendAndApply(&ow);
  34. }
  35. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  36. {
  37. InfoWindow iw;
  38. iw.soundID = soundID;
  39. iw.player = playerID;
  40. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  41. IObjectInterface::cb->sendAndApply(&iw);
  42. }
  43. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  44. {
  45. const PlayerColor playerID = h->getOwner();
  46. showInfoDialog(playerID,txtID,soundID);
  47. }
  48. static std::string & visitedTxt(const bool visited)
  49. {
  50. int id = visited ? 352 : 353;
  51. return VLC->generaltexth->allTexts[id];
  52. }
  53. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  54. {
  55. if(what == 10)
  56. players.insert(PlayerColor(val));
  57. }
  58. bool CPlayersVisited::wasVisited( PlayerColor player ) const
  59. {
  60. return vstd::contains(players,player);
  61. }
  62. bool CPlayersVisited::wasVisited( TeamID team ) const
  63. {
  64. for(auto i : players)
  65. {
  66. if(cb->getPlayer(i)->team == team)
  67. return true;
  68. }
  69. return false;
  70. }
  71. std::string CGCreature::getHoverText(PlayerColor player) const
  72. {
  73. if(stacks.empty())
  74. {
  75. //should not happen...
  76. logGlobal->errorStream() << "Invalid stack at tile " << pos << ": subID=" << subID << "; id=" << id;
  77. return "!!!INVALID_STACK!!!";
  78. }
  79. std::string hoverName;
  80. MetaString ms;
  81. int pom = stacks.begin()->second->getQuantityID();
  82. pom = 172 + 3*pom;
  83. ms.addTxt(MetaString::ARRAY_TXT,pom);
  84. ms << " " ;
  85. ms.addTxt(MetaString::CRE_PL_NAMES,subID);
  86. ms.toString(hoverName);
  87. return hoverName;
  88. }
  89. std::string CGCreature::getHoverText(const CGHeroInstance * hero) const
  90. {
  91. std::string hoverName = getHoverText(hero->tempOwner);
  92. const JsonNode & texts = VLC->generaltexth->localizedTexts["adventureMap"]["monsterThreat"];
  93. hoverName += texts["title"].String();
  94. int choice;
  95. double ratio = ((double)getArmyStrength() / hero->getTotalStrength());
  96. if (ratio < 0.1) choice = 0;
  97. else if (ratio < 0.25) choice = 1;
  98. else if (ratio < 0.6) choice = 2;
  99. else if (ratio < 0.9) choice = 3;
  100. else if (ratio < 1.1) choice = 4;
  101. else if (ratio < 1.3) choice = 5;
  102. else if (ratio < 1.8) choice = 6;
  103. else if (ratio < 2.5) choice = 7;
  104. else if (ratio < 4) choice = 8;
  105. else if (ratio < 8) choice = 9;
  106. else if (ratio < 20) choice = 10;
  107. else choice = 11;
  108. hoverName += texts["levels"].Vector()[choice].String();
  109. return hoverName;
  110. }
  111. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  112. {
  113. int action = takenAction(h);
  114. switch( action ) //decide what we do...
  115. {
  116. case FIGHT:
  117. fight(h);
  118. break;
  119. case FLEE: //flee
  120. {
  121. flee(h);
  122. break;
  123. }
  124. case JOIN_FOR_FREE: //join for free
  125. {
  126. BlockingDialog ynd(true,false);
  127. ynd.player = h->tempOwner;
  128. ynd.text.addTxt(MetaString::ADVOB_TXT, 86);
  129. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  130. cb->showBlockingDialog(&ynd);
  131. break;
  132. }
  133. default: //join for gold
  134. {
  135. assert(action > 0);
  136. //ask if player agrees to pay gold
  137. BlockingDialog ynd(true,false);
  138. ynd.player = h->tempOwner;
  139. std::string tmp = VLC->generaltexth->advobtxt[90];
  140. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(getStackCount(SlotID(0))));
  141. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  142. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID]->namePl);
  143. ynd.text << tmp;
  144. cb->showBlockingDialog(&ynd);
  145. break;
  146. }
  147. }
  148. }
  149. void CGCreature::initObj()
  150. {
  151. blockVisit = true;
  152. switch(character)
  153. {
  154. case 0:
  155. character = -4;
  156. break;
  157. case 1:
  158. character = cb->gameState()->getRandomGenerator().nextInt(1, 7);
  159. break;
  160. case 2:
  161. character = cb->gameState()->getRandomGenerator().nextInt(1, 10);
  162. break;
  163. case 3:
  164. character = cb->gameState()->getRandomGenerator().nextInt(4, 10);
  165. break;
  166. case 4:
  167. character = 10;
  168. break;
  169. }
  170. stacks[SlotID(0)]->setType(CreatureID(subID));
  171. TQuantity &amount = stacks[SlotID(0)]->count;
  172. CCreature &c = *VLC->creh->creatures[subID];
  173. if(amount == 0)
  174. {
  175. amount = cb->gameState()->getRandomGenerator().nextInt(c.ammMin, c.ammMax);
  176. if(amount == 0) //armies with 0 creatures are illegal
  177. {
  178. logGlobal->warnStream() << "Problem: stack " << nodeName() << " cannot have 0 creatures. Check properties of " << c.nodeName();
  179. amount = 1;
  180. }
  181. }
  182. formation.randomFormation = cb->gameState()->getRandomGenerator().nextInt();
  183. temppower = stacks[SlotID(0)]->count * 1000;
  184. refusedJoining = false;
  185. }
  186. void CGCreature::newTurn() const
  187. {//Works only for stacks of single type of size up to 2 millions
  188. if (stacks.begin()->second->count < VLC->modh->settings.CREEP_SIZE && cb->getDate(Date::DAY_OF_WEEK) == 1 && cb->getDate(Date::DAY) > 1)
  189. {
  190. ui32 power = temppower * (100 + VLC->modh->settings.WEEKLY_GROWTH)/100;
  191. cb->setObjProperty(id, ObjProperty::MONSTER_COUNT, std::min (power/1000 , (ui32)VLC->modh->settings.CREEP_SIZE)); //set new amount
  192. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, power); //increase temppower
  193. }
  194. if (VLC->modh->modules.STACK_EXP)
  195. cb->setObjProperty(id, ObjProperty::MONSTER_EXP, VLC->modh->settings.NEUTRAL_STACK_EXP); //for testing purpose
  196. }
  197. void CGCreature::setPropertyDer(ui8 what, ui32 val)
  198. {
  199. switch (what)
  200. {
  201. case ObjProperty::MONSTER_COUNT:
  202. stacks[SlotID(0)]->count = val;
  203. break;
  204. case ObjProperty::MONSTER_POWER:
  205. temppower = val;
  206. break;
  207. case ObjProperty::MONSTER_EXP:
  208. giveStackExp(val);
  209. break;
  210. case ObjProperty::MONSTER_RESTORE_TYPE:
  211. formation.basicType = val;
  212. break;
  213. case ObjProperty::MONSTER_REFUSED_JOIN:
  214. refusedJoining = val;
  215. break;
  216. }
  217. }
  218. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  219. {
  220. //calculate relative strength of hero and creatures armies
  221. double relStrength = double(h->getTotalStrength()) / getArmyStrength();
  222. int powerFactor;
  223. if(relStrength >= 7)
  224. powerFactor = 11;
  225. else if(relStrength >= 1)
  226. powerFactor = (int)(2*(relStrength-1));
  227. else if(relStrength >= 0.5)
  228. powerFactor = -1;
  229. else if(relStrength >= 0.333)
  230. powerFactor = -2;
  231. else
  232. powerFactor = -3;
  233. std::set<CreatureID> myKindCres; //what creatures are the same kind as we
  234. const CCreature * myCreature = VLC->creh->creatures[subID];
  235. myKindCres.insert(myCreature->idNumber); //we
  236. myKindCres.insert(myCreature->upgrades.begin(), myCreature->upgrades.end()); //our upgrades
  237. for(ConstTransitivePtr<CCreature> &crea : VLC->creh->creatures)
  238. {
  239. if(vstd::contains(crea->upgrades, myCreature->idNumber)) //it's our base creatures
  240. myKindCres.insert(crea->idNumber);
  241. }
  242. int count = 0, //how many creatures of similar kind has hero
  243. totalCount = 0;
  244. for (auto & elem : h->Slots())
  245. {
  246. if(vstd::contains(myKindCres,elem.second->type->idNumber))
  247. count += elem.second->count;
  248. totalCount += elem.second->count;
  249. }
  250. int sympathy = 0; // 0 if hero have no similar creatures
  251. if(count)
  252. sympathy++; // 1 - if hero have at least 1 similar creature
  253. if(count*2 > totalCount)
  254. sympathy++; // 2 - hero have similar creatures more that 50%
  255. int charisma = powerFactor + h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy;
  256. if(charisma < character) //creatures will fight
  257. return -2;
  258. if (allowJoin)
  259. {
  260. if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy + 1 >= character)
  261. return 0; //join for free
  262. else if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) * 2 + sympathy + 1 >= character)
  263. return VLC->creh->creatures[subID]->cost[6] * getStackCount(SlotID(0)); //join for gold
  264. }
  265. //we are still here - creatures have not joined hero, flee or fight
  266. if (charisma > character)
  267. return -1; //flee
  268. else
  269. return -2; //fight
  270. }
  271. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  272. {
  273. if(refusedJoining)
  274. cb->setObjProperty(id, ObjProperty::MONSTER_REFUSED_JOIN, false);
  275. if(pursue)
  276. {
  277. fight(h);
  278. }
  279. else
  280. {
  281. cb->removeObject(this);
  282. }
  283. }
  284. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  285. {
  286. if(!accept)
  287. {
  288. if(takenAction(h,false) == -1) //they flee
  289. {
  290. cb->setObjProperty(id, ObjProperty::MONSTER_REFUSED_JOIN, true);
  291. flee(h);
  292. }
  293. else //they fight
  294. {
  295. showInfoDialog(h,87,0);//Insulted by your refusal of their offer, the monsters attack!
  296. fight(h);
  297. }
  298. }
  299. else //accepted
  300. {
  301. if (cb->getResource(h->tempOwner, Res::GOLD) < cost) //player don't have enough gold!
  302. {
  303. InfoWindow iw;
  304. iw.player = h->tempOwner;
  305. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  306. cb->showInfoDialog(&iw);
  307. //act as if player refused
  308. joinDecision(h,cost,false);
  309. return;
  310. }
  311. //take gold
  312. if(cost)
  313. cb->giveResource(h->tempOwner,Res::GOLD,-cost);
  314. cb->tryJoiningArmy(this, h, true, true);
  315. }
  316. }
  317. void CGCreature::fight( const CGHeroInstance *h ) const
  318. {
  319. //split stacks
  320. //TODO: multiple creature types in a stack?
  321. int basicType = stacks.begin()->second->type->idNumber;
  322. cb->setObjProperty(id, ObjProperty::MONSTER_RESTORE_TYPE, basicType); //store info about creature stack
  323. double relativePower = static_cast<double>(h->getTotalStrength()) / getArmyStrength();
  324. int stacksCount;
  325. //TODO: number depends on tile type
  326. if (relativePower < 0.5)
  327. {
  328. stacksCount = 7;
  329. }
  330. else if (relativePower < 0.67)
  331. {
  332. stacksCount = 7;
  333. }
  334. else if (relativePower < 1)
  335. {
  336. stacksCount = 6;
  337. }
  338. else if (relativePower < 1.5)
  339. {
  340. stacksCount = 5;
  341. }
  342. else if (relativePower < 2)
  343. {
  344. stacksCount = 4;
  345. }
  346. else
  347. {
  348. stacksCount = 3;
  349. }
  350. SlotID sourceSlot = stacks.begin()->first;
  351. SlotID destSlot;
  352. for (int stacksLeft = stacksCount; stacksLeft > 1; --stacksLeft)
  353. {
  354. int stackSize = stacks.begin()->second->count / stacksLeft;
  355. if (stackSize)
  356. {
  357. if ((destSlot = getFreeSlot()).validSlot())
  358. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, destSlot), stackSize);
  359. else
  360. {
  361. logGlobal->warnStream() <<"Warning! Not enough empty slots to split stack!";
  362. break;
  363. }
  364. }
  365. else break;
  366. }
  367. if (stacksCount > 1)
  368. {
  369. if (formation.randomFormation % 100 < 50) //upgrade
  370. {
  371. SlotID slotId = SlotID(stacks.size() / 2);
  372. const auto & upgrades = getStack(slotId).type->upgrades;
  373. if(!upgrades.empty())
  374. {
  375. auto it = RandomGeneratorUtil::nextItem(upgrades, cb->gameState()->getRandomGenerator());
  376. cb->changeStackType(StackLocation(this, slotId), VLC->creh->creatures[*it]);
  377. }
  378. }
  379. }
  380. cb->startBattleI(h, this);
  381. }
  382. void CGCreature::flee( const CGHeroInstance * h ) const
  383. {
  384. BlockingDialog ynd(true,false);
  385. ynd.player = h->tempOwner;
  386. ynd.text.addTxt(MetaString::ADVOB_TXT,91);
  387. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  388. cb->showBlockingDialog(&ynd);
  389. }
  390. void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  391. {
  392. if(result.winner==0)
  393. {
  394. cb->removeObject(this);
  395. }
  396. else
  397. {
  398. //merge stacks into one
  399. TSlots::const_iterator i;
  400. CCreature * cre = VLC->creh->creatures[formation.basicType];
  401. for (i = stacks.begin(); i != stacks.end(); i++)
  402. {
  403. if (cre->isMyUpgrade(i->second->type))
  404. {
  405. cb->changeStackType (StackLocation(this, i->first), cre); //un-upgrade creatures
  406. }
  407. }
  408. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  409. if(!hasStackAtSlot(SlotID(0)))
  410. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count);
  411. while (stacks.size() > 1) //hopefully that's enough
  412. {
  413. // TODO it's either overcomplicated (if we assume there'll be only one stack) or buggy (if we allow multiple stacks... but that'll also cause troubles elsewhere)
  414. i = stacks.end();
  415. i--;
  416. SlotID slot = getSlotFor(i->second->type);
  417. if (slot == i->first) //no reason to move stack to its own slot
  418. break;
  419. else
  420. cb->moveStack (StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  421. }
  422. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  423. }
  424. }
  425. void CGCreature::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  426. {
  427. auto action = takenAction(hero);
  428. if(!refusedJoining && action >= JOIN_FOR_FREE) //higher means price
  429. joinDecision(hero, action, answer);
  430. else if(action != FIGHT)
  431. fleeDecision(hero, answer);
  432. else
  433. assert(0);
  434. }
  435. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  436. {
  437. int relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  438. if(relations == 2) //we're visiting our mine
  439. {
  440. cb->showGarrisonDialog(id,h->id,true);
  441. return;
  442. }
  443. else if (relations == 1)//ally
  444. return;
  445. if(stacksCount()) //Mine is guarded
  446. {
  447. BlockingDialog ynd(true,false);
  448. ynd.player = h->tempOwner;
  449. ynd.text.addTxt(MetaString::ADVOB_TXT, subID == 7 ? 84 : 187);
  450. cb->showBlockingDialog(&ynd);
  451. return;
  452. }
  453. flagMine(h->tempOwner);
  454. }
  455. void CGMine::newTurn() const
  456. {
  457. if(cb->getDate() == 1)
  458. return;
  459. if (tempOwner == PlayerColor::NEUTRAL)
  460. return;
  461. cb->giveResource(tempOwner, producedResource, producedQuantity);
  462. }
  463. void CGMine::initObj()
  464. {
  465. if(subID >= 7) //Abandoned Mine
  466. {
  467. //set guardians
  468. int howManyTroglodytes = cb->gameState()->getRandomGenerator().nextInt(100, 199);
  469. auto troglodytes = new CStackInstance(CreatureID::TROGLODYTES, howManyTroglodytes);
  470. putStack(SlotID(0), troglodytes);
  471. //after map reading tempOwner placeholds bitmask for allowed resources
  472. std::vector<Res::ERes> possibleResources;
  473. for (int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  474. if(tempOwner.getNum() & 1<<i) //NOTE: reuse of tempOwner
  475. possibleResources.push_back(static_cast<Res::ERes>(i));
  476. assert(!possibleResources.empty());
  477. producedResource = *RandomGeneratorUtil::nextItem(possibleResources, cb->gameState()->getRandomGenerator());
  478. tempOwner = PlayerColor::NEUTRAL;
  479. }
  480. else
  481. {
  482. producedResource = static_cast<Res::ERes>(subID);
  483. if(tempOwner >= PlayerColor::PLAYER_LIMIT)
  484. tempOwner = PlayerColor::NEUTRAL;
  485. }
  486. producedQuantity = defaultResProduction();
  487. }
  488. std::string CGMine::getObjectName() const
  489. {
  490. return VLC->generaltexth->mines.at(subID).first;
  491. }
  492. std::string CGMine::getHoverText(PlayerColor player) const
  493. {
  494. std::string hoverName = getObjectName(); // Sawmill
  495. if (tempOwner != PlayerColor::NEUTRAL)
  496. {
  497. hoverName += "\n";
  498. hoverName += VLC->generaltexth->arraytxt[23 + tempOwner.getNum()]; // owned by Red Player
  499. hoverName += "\n(" + VLC->generaltexth->restypes[producedResource] + ")";
  500. }
  501. for (auto & slot : Slots()) // guarded by a few Pikeman
  502. {
  503. hoverName += "\n";
  504. hoverName += getRoughAmount(slot.first);
  505. hoverName += getCreature(slot.first)->namePl;
  506. }
  507. return hoverName;
  508. }
  509. void CGMine::flagMine(PlayerColor player) const
  510. {
  511. assert(tempOwner != player);
  512. cb->setOwner(this, player); //not ours? flag it!
  513. InfoWindow iw;
  514. iw.soundID = soundBase::FLAGMINE;
  515. iw.text.addTxt(MetaString::MINE_EVNTS,producedResource); //not use subID, abandoned mines uses default mine texts
  516. iw.player = player;
  517. iw.components.push_back(Component(Component::RESOURCE,producedResource,producedQuantity,-1));
  518. cb->showInfoDialog(&iw);
  519. }
  520. ui32 CGMine::defaultResProduction()
  521. {
  522. switch(producedResource)
  523. {
  524. case Res::WOOD:
  525. case Res::ORE:
  526. return 2;
  527. case Res::GOLD:
  528. return 1000;
  529. default:
  530. return 1;
  531. }
  532. }
  533. void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  534. {
  535. if(result.winner == 0) //attacker won
  536. {
  537. if(subID == 7)
  538. {
  539. showInfoDialog(hero->tempOwner, 85, 0);
  540. }
  541. flagMine(hero->tempOwner);
  542. }
  543. }
  544. void CGMine::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  545. {
  546. if(answer)
  547. cb->startBattleI(hero, this);
  548. }
  549. std::string CGResource::getHoverText(PlayerColor player) const
  550. {
  551. return VLC->generaltexth->restypes[subID];
  552. }
  553. void CGResource::initObj()
  554. {
  555. blockVisit = true;
  556. if(!amount)
  557. {
  558. switch(subID)
  559. {
  560. case 6:
  561. amount = cb->gameState()->getRandomGenerator().nextInt(500, 1000);
  562. break;
  563. case 0: case 2:
  564. amount = cb->gameState()->getRandomGenerator().nextInt(6, 10);
  565. break;
  566. default:
  567. amount = cb->gameState()->getRandomGenerator().nextInt(3, 5);
  568. break;
  569. }
  570. }
  571. }
  572. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  573. {
  574. if(stacksCount())
  575. {
  576. if(message.size())
  577. {
  578. BlockingDialog ynd(true,false);
  579. ynd.player = h->getOwner();
  580. ynd.text << message;
  581. cb->showBlockingDialog(&ynd);
  582. }
  583. else
  584. {
  585. blockingDialogAnswered(h, true); //behave as if player accepted battle
  586. }
  587. }
  588. else
  589. {
  590. if(message.length())
  591. {
  592. InfoWindow iw;
  593. iw.player = h->tempOwner;
  594. iw.text << message;
  595. cb->showInfoDialog(&iw);
  596. }
  597. collectRes(h->getOwner());
  598. }
  599. }
  600. void CGResource::collectRes( PlayerColor player ) const
  601. {
  602. cb->giveResource(player, static_cast<Res::ERes>(subID), amount);
  603. ShowInInfobox sii;
  604. sii.player = player;
  605. sii.c = Component(Component::RESOURCE,subID,amount,0);
  606. sii.text.addTxt(MetaString::ADVOB_TXT,113);
  607. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  608. cb->showCompInfo(&sii);
  609. cb->removeObject(this);
  610. }
  611. void CGResource::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  612. {
  613. if(result.winner == 0) //attacker won
  614. collectRes(hero->getOwner());
  615. }
  616. void CGResource::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  617. {
  618. if(answer)
  619. cb->startBattleI(hero, this);
  620. }
  621. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  622. {
  623. ObjectInstanceID destinationid;
  624. switch(ID)
  625. {
  626. case Obj::MONOLITH_ONE_WAY_ENTRANCE: //one way - find corresponding exit monolith
  627. {
  628. if(vstd::contains(objs,Obj::MONOLITH_ONE_WAY_EXIT) && vstd::contains(objs[Obj::MONOLITH_ONE_WAY_EXIT],subID) && objs[Obj::MONOLITH_ONE_WAY_EXIT][subID].size())
  629. {
  630. destinationid = *RandomGeneratorUtil::nextItem(objs[Obj::MONOLITH_ONE_WAY_EXIT][subID], cb->gameState()->getRandomGenerator());
  631. }
  632. else
  633. {
  634. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  635. }
  636. break;
  637. }
  638. case Obj::MONOLITH_TWO_WAY://two way monolith - pick any other one
  639. case Obj::WHIRLPOOL: //Whirlpool
  640. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  641. {
  642. //choose another exit
  643. do
  644. {
  645. destinationid = *RandomGeneratorUtil::nextItem(objs[ID][subID], cb->gameState()->getRandomGenerator());
  646. } while(destinationid == id);
  647. if (ID == Obj::WHIRLPOOL)
  648. {
  649. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  650. {
  651. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  652. { //we can't remove last unit
  653. SlotID targetstack = h->Slots().begin()->first; //slot numbers may vary
  654. for(auto i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  655. {
  656. if (h->getPower(targetstack) > h->getPower(i->first))
  657. {
  658. targetstack = (i->first);
  659. }
  660. }
  661. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  662. vstd::amax(countToTake, 1);
  663. InfoWindow iw;
  664. iw.player = h->tempOwner;
  665. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  666. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  667. cb->showInfoDialog(&iw);
  668. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  669. }
  670. }
  671. }
  672. }
  673. else
  674. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  675. break;
  676. case Obj::SUBTERRANEAN_GATE: //find nearest subterranean gate on the other level
  677. {
  678. destinationid = getMatchingGate(id);
  679. if(destinationid == ObjectInstanceID()) //no exit
  680. {
  681. showInfoDialog(h,153,0);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  682. }
  683. break;
  684. }
  685. }
  686. if(destinationid == ObjectInstanceID())
  687. {
  688. logGlobal->warnStream() << "Cannot find exit... (obj at " << pos << ") :( ";
  689. return;
  690. }
  691. if (ID == Obj::WHIRLPOOL)
  692. {
  693. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  694. auto & tile = *RandomGeneratorUtil::nextItem(tiles, cb->gameState()->getRandomGenerator());
  695. cb->moveHero(h->id, tile + int3(1,0,0), true);
  696. }
  697. else
  698. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  699. }
  700. void CGTeleport::initObj()
  701. {
  702. int si = subID;
  703. switch (ID)
  704. {
  705. case Obj::SUBTERRANEAN_GATE://ignore subterranean gates subid
  706. case Obj::WHIRLPOOL:
  707. {
  708. si = 0;
  709. break;
  710. }
  711. default:
  712. break;
  713. }
  714. objs[ID][si].push_back(id);
  715. }
  716. void CGTeleport::postInit() //matches subterranean gates into pairs
  717. {
  718. //split on underground and surface gates
  719. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  720. for(auto & elem : objs[Obj::SUBTERRANEAN_GATE][0])
  721. {
  722. const CGObjectInstance *hlp = cb->getObj(elem);
  723. gatesSplit[hlp->pos.z].push_back(hlp);
  724. }
  725. //sort by position
  726. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), [](const CGObjectInstance * a, const CGObjectInstance * b)
  727. {
  728. return a->pos < b->pos;
  729. });
  730. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  731. {
  732. const CGObjectInstance *cur = gatesSplit[0][i];
  733. //find nearest underground exit
  734. std::pair<int, si32> best(-1, std::numeric_limits<si32>::max()); //pair<pos_in_vector, distance^2>
  735. for(int j = 0; j < gatesSplit[1].size(); j++)
  736. {
  737. const CGObjectInstance *checked = gatesSplit[1][j];
  738. if(!checked)
  739. continue;
  740. si32 hlp = checked->pos.dist2dSQ(cur->pos);
  741. if(hlp < best.second)
  742. {
  743. best.first = j;
  744. best.second = hlp;
  745. }
  746. }
  747. if(best.first >= 0) //found pair
  748. {
  749. gates.push_back(std::make_pair(cur->id, gatesSplit[1][best.first]->id));
  750. gatesSplit[1][best.first] = nullptr;
  751. }
  752. else
  753. gates.push_back(std::make_pair(cur->id, ObjectInstanceID()));
  754. }
  755. objs.erase(Obj::SUBTERRANEAN_GATE);
  756. }
  757. ObjectInstanceID CGTeleport::getMatchingGate(ObjectInstanceID id)
  758. {
  759. for(auto & gate : gates)
  760. {
  761. if(gate.first == id)
  762. return gate.second;
  763. if(gate.second == id)
  764. return gate.first;
  765. }
  766. return ObjectInstanceID();
  767. }
  768. void CGArtifact::initObj()
  769. {
  770. blockVisit = true;
  771. if(ID == Obj::ARTIFACT)
  772. {
  773. if (!storedArtifact)
  774. {
  775. auto a = new CArtifactInstance();
  776. cb->gameState()->map->addNewArtifactInstance(a);
  777. storedArtifact = a;
  778. }
  779. if(!storedArtifact->artType)
  780. storedArtifact->setType(VLC->arth->artifacts[subID]);
  781. }
  782. if(ID == Obj::SPELL_SCROLL)
  783. subID = 1;
  784. assert(storedArtifact->artType);
  785. assert(storedArtifact->getParentNodes().size());
  786. //assert(storedArtifact->artType->id == subID); //this does not stop desync
  787. }
  788. std::string CGArtifact::getObjectName() const
  789. {
  790. return VLC->arth->artifacts[subID]->Name();
  791. }
  792. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  793. {
  794. if(!stacksCount())
  795. {
  796. InfoWindow iw;
  797. iw.player = h->tempOwner;
  798. switch(ID)
  799. {
  800. case Obj::ARTIFACT:
  801. {
  802. iw.soundID = soundBase::treasure; //play sound only for non-scroll arts
  803. iw.components.push_back(Component(Component::ARTIFACT,subID,0,0));
  804. if(message.length())
  805. iw.text << message;
  806. else
  807. {
  808. if (VLC->arth->artifacts[subID]->EventText().size())
  809. iw.text << std::pair<ui8, ui32> (MetaString::ART_EVNTS, subID);
  810. else //fix for mod artifacts with no event text
  811. {
  812. iw.text.addTxt (MetaString::ADVOB_TXT, 183); //% has found treasure
  813. iw.text.addReplacement (h->name);
  814. }
  815. }
  816. }
  817. break;
  818. case Obj::SPELL_SCROLL:
  819. {
  820. int spellID = storedArtifact->getGivenSpellID();
  821. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  822. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  823. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  824. }
  825. break;
  826. }
  827. cb->showInfoDialog(&iw);
  828. pick(h);
  829. }
  830. else
  831. {
  832. if(message.size())
  833. {
  834. BlockingDialog ynd(true,false);
  835. ynd.player = h->getOwner();
  836. ynd.text << message;
  837. cb->showBlockingDialog(&ynd);
  838. }
  839. else
  840. {
  841. blockingDialogAnswered(h, true);
  842. }
  843. }
  844. }
  845. void CGArtifact::pick(const CGHeroInstance * h) const
  846. {
  847. cb->giveHeroArtifact(h, storedArtifact, ArtifactPosition::FIRST_AVAILABLE);
  848. cb->removeObject(this);
  849. }
  850. void CGArtifact::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  851. {
  852. if(result.winner == 0) //attacker won
  853. pick(hero);
  854. }
  855. void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  856. {
  857. if(answer)
  858. cb->startBattleI(hero, this);
  859. }
  860. void CGWitchHut::initObj()
  861. {
  862. if (allowedAbilities.empty()) //this can happen for RMG. regular maps load abilities from map file
  863. {
  864. for (int i = 0; i < GameConstants::SKILL_QUANTITY; i++)
  865. allowedAbilities.push_back(i);
  866. }
  867. ability = *RandomGeneratorUtil::nextItem(allowedAbilities, cb->gameState()->getRandomGenerator());
  868. }
  869. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  870. {
  871. InfoWindow iw;
  872. iw.soundID = soundBase::gazebo;
  873. iw.player = h->getOwner();
  874. if(!wasVisited(h->tempOwner))
  875. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  876. ui32 txt_id;
  877. if(h->getSecSkillLevel(SecondarySkill(ability))) //you already know this skill
  878. {
  879. txt_id =172;
  880. }
  881. else if(!h->canLearnSkill()) //already all skills slots used
  882. {
  883. txt_id = 173;
  884. }
  885. else //give sec skill
  886. {
  887. iw.components.push_back(Component(Component::SEC_SKILL, ability, 1, 0));
  888. txt_id = 171;
  889. cb->changeSecSkill(h, SecondarySkill(ability), 1, true);
  890. }
  891. iw.text.addTxt(MetaString::ADVOB_TXT,txt_id);
  892. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  893. cb->showInfoDialog(&iw);
  894. }
  895. std::string CGWitchHut::getHoverText(PlayerColor player) const
  896. {
  897. std::string hoverName = getObjectName();
  898. if(wasVisited(player))
  899. {
  900. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  901. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  902. }
  903. return hoverName;
  904. }
  905. std::string CGWitchHut::getHoverText(const CGHeroInstance * hero) const
  906. {
  907. std::string hoverName = getHoverText(hero->tempOwner);
  908. if(hero->getSecSkillLevel(SecondarySkill(ability))) //hero knows that ability
  909. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  910. return hoverName;
  911. }
  912. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  913. {
  914. int message;
  915. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  916. {
  917. message = 78;//"A second drink at the well in one day will not help you."
  918. }
  919. else if(h->mana < h->manaLimit())
  920. {
  921. giveDummyBonus(h->id);
  922. cb->setManaPoints(h->id,h->manaLimit());
  923. message = 77;
  924. }
  925. else
  926. {
  927. message = 79;
  928. }
  929. showInfoDialog(h,message,soundBase::faerie);
  930. }
  931. std::string CGMagicWell::getHoverText(const CGHeroInstance * hero) const
  932. {
  933. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  934. }
  935. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  936. {
  937. InfoWindow iw;
  938. iw.player = h->tempOwner;
  939. switch (ID)
  940. {
  941. case Obj::REDWOOD_OBSERVATORY:
  942. case Obj::PILLAR_OF_FIRE:
  943. {
  944. iw.soundID = soundBase::LIGHTHOUSE;
  945. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==Obj::PILLAR_OF_FIRE));
  946. FoWChange fw;
  947. fw.player = h->tempOwner;
  948. fw.mode = 1;
  949. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  950. cb->sendAndApply (&fw);
  951. break;
  952. }
  953. case Obj::COVER_OF_DARKNESS:
  954. {
  955. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  956. for (auto & player : cb->gameState()->players)
  957. {
  958. if (cb->getPlayerStatus(player.first) == EPlayerStatus::INGAME &&
  959. cb->getPlayerRelations(player.first, h->tempOwner) == PlayerRelations::ENEMIES)
  960. cb->changeFogOfWar(visitablePos(), 20, player.first, true);
  961. }
  962. break;
  963. }
  964. }
  965. cb->showInfoDialog(&iw);
  966. }
  967. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  968. {
  969. if(spell == SpellID::NONE)
  970. {
  971. logGlobal->errorStream() << "Not initialized shrine visited!";
  972. return;
  973. }
  974. if(!wasVisited(h->tempOwner))
  975. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  976. InfoWindow iw;
  977. iw.soundID = soundBase::temple;
  978. iw.player = h->getOwner();
  979. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  980. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  981. iw.text << ".";
  982. if(!h->getArt(ArtifactPosition::SPELLBOOK))
  983. {
  984. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  985. }
  986. else if(ID == Obj::SHRINE_OF_MAGIC_THOUGHT && !h->getSecSkillLevel(SecondarySkill::WISDOM)) //it's third level spell and hero doesn't have wisdom
  987. {
  988. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  989. }
  990. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  991. {
  992. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  993. }
  994. else //give spell
  995. {
  996. std::set<SpellID> spells;
  997. spells.insert(spell);
  998. cb->changeSpells(h, true, spells);
  999. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  1000. }
  1001. cb->showInfoDialog(&iw);
  1002. }
  1003. void CGShrine::initObj()
  1004. {
  1005. if(spell == SpellID::NONE) //spell not set
  1006. {
  1007. int level = ID-87;
  1008. std::vector<SpellID> possibilities;
  1009. cb->getAllowedSpells (possibilities, level);
  1010. if(possibilities.empty())
  1011. {
  1012. logGlobal->errorStream() << "Error: cannot init shrine, no allowed spells!";
  1013. return;
  1014. }
  1015. spell = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1016. }
  1017. }
  1018. std::string CGShrine::getHoverText(PlayerColor player) const
  1019. {
  1020. std::string hoverName = getObjectName();
  1021. if(wasVisited(player))
  1022. {
  1023. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  1024. boost::algorithm::replace_first(hoverName,"%s", spell.toSpell()->name);
  1025. }
  1026. return hoverName;
  1027. }
  1028. std::string CGShrine::getHoverText(const CGHeroInstance * hero) const
  1029. {
  1030. std::string hoverName = getHoverText(hero->tempOwner);
  1031. if(vstd::contains(hero->spells, spell)) //hero knows that spell
  1032. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  1033. return hoverName;
  1034. }
  1035. void CGSignBottle::initObj()
  1036. {
  1037. //if no text is set than we pick random from the predefined ones
  1038. if(message.empty())
  1039. {
  1040. message = *RandomGeneratorUtil::nextItem(VLC->generaltexth->randsign, cb->gameState()->getRandomGenerator());
  1041. }
  1042. if(ID == Obj::OCEAN_BOTTLE)
  1043. {
  1044. blockVisit = true;
  1045. }
  1046. }
  1047. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  1048. {
  1049. InfoWindow iw;
  1050. iw.soundID = soundBase::STORE;
  1051. iw.player = h->getOwner();
  1052. iw.text << message;
  1053. cb->showInfoDialog(&iw);
  1054. if(ID == Obj::OCEAN_BOTTLE)
  1055. cb->removeObject(this);
  1056. }
  1057. //TODO: remove
  1058. //void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  1059. //{
  1060. //
  1061. //}
  1062. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  1063. {
  1064. EBonusType type = bonusType;
  1065. int bid = bonusID;
  1066. //check if the bonus if applicable, if not - give primary skill (always possible)
  1067. int ssl = h->getSecSkillLevel(SecondarySkill(bid)); //current sec skill level, used if bonusType == 1
  1068. if((type == SECONDARY_SKILL
  1069. && ((ssl == 3) || (!ssl && !h->canLearnSkill()))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  1070. || (type == SPELL && (!h->getArt(ArtifactPosition::SPELLBOOK) || vstd::contains(h->spells, (ui32) bid)
  1071. || ( SpellID(bid).toSpell()->level > h->getSecSkillLevel(SecondarySkill::WISDOM) + 2)
  1072. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  1073. {
  1074. type = PRIM_SKILL;
  1075. bid = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS - 1);
  1076. }
  1077. InfoWindow iw;
  1078. iw.soundID = soundBase::gazebo;
  1079. iw.player = h->getOwner();
  1080. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  1081. switch (type)
  1082. {
  1083. case PRIM_SKILL:
  1084. cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(bid),+1);
  1085. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  1086. break;
  1087. case SECONDARY_SKILL:
  1088. cb->changeSecSkill(h,SecondarySkill(bid),+1);
  1089. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  1090. break;
  1091. case SPELL:
  1092. {
  1093. std::set<SpellID> hlp;
  1094. hlp.insert(SpellID(bid));
  1095. cb->changeSpells(h,true,hlp);
  1096. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  1097. }
  1098. break;
  1099. default:
  1100. logGlobal->errorStream() << "Error: wrong bonus type (" << (int)type << ") for Scholar!\n";
  1101. return;
  1102. }
  1103. cb->showInfoDialog(&iw);
  1104. cb->removeObject(this);
  1105. }
  1106. void CGScholar::initObj()
  1107. {
  1108. blockVisit = true;
  1109. if(bonusType == RANDOM)
  1110. {
  1111. bonusType = static_cast<EBonusType>(cb->gameState()->getRandomGenerator().nextInt(2));
  1112. switch(bonusType)
  1113. {
  1114. case PRIM_SKILL:
  1115. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS -1);
  1116. break;
  1117. case SECONDARY_SKILL:
  1118. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::SKILL_QUANTITY -1);
  1119. break;
  1120. case SPELL:
  1121. std::vector<SpellID> possibilities;
  1122. for (int i = 1; i < 6; ++i)
  1123. cb->getAllowedSpells (possibilities, i);
  1124. bonusID = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  1130. {
  1131. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  1132. if (!ally && stacksCount() > 0) {
  1133. //TODO: Find a way to apply magic garrison effects in battle.
  1134. cb->startBattleI(h, this);
  1135. return;
  1136. }
  1137. //New owner.
  1138. if (!ally)
  1139. cb->setOwner(this, h->tempOwner);
  1140. cb->showGarrisonDialog(id, h->id, removableUnits);
  1141. }
  1142. bool CGGarrison::passableFor(PlayerColor player) const
  1143. {
  1144. //FIXME: identical to same method in CGTownInstance
  1145. if ( !stacksCount() )//empty - anyone can visit
  1146. return true;
  1147. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  1148. return false;
  1149. if (cb->getPlayerRelations(tempOwner, player) != PlayerRelations::ENEMIES)
  1150. return true;
  1151. return false;
  1152. }
  1153. void CGGarrison::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  1154. {
  1155. if (result.winner == 0)
  1156. onHeroVisit(hero);
  1157. }
  1158. void CGMagi::initObj()
  1159. {
  1160. if (ID == Obj::EYE_OF_MAGI)
  1161. {
  1162. blockVisit = true;
  1163. eyelist[subID].push_back(id);
  1164. }
  1165. }
  1166. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  1167. {
  1168. if (ID == Obj::HUT_OF_MAGI)
  1169. {
  1170. showInfoDialog(h, 61, soundBase::LIGHTHOUSE);
  1171. if (!eyelist[subID].empty())
  1172. {
  1173. CenterView cv;
  1174. cv.player = h->tempOwner;
  1175. cv.focusTime = 2000;
  1176. FoWChange fw;
  1177. fw.player = h->tempOwner;
  1178. fw.mode = 1;
  1179. for(auto it : eyelist[subID])
  1180. {
  1181. const CGObjectInstance *eye = cb->getObj(it);
  1182. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  1183. cb->sendAndApply(&fw);
  1184. cv.pos = eye->pos;
  1185. cb->sendAndApply(&cv);
  1186. }
  1187. cv.pos = h->getPosition(false);
  1188. cb->sendAndApply(&cv);
  1189. }
  1190. }
  1191. else if (ID == Obj::EYE_OF_MAGI)
  1192. {
  1193. showInfoDialog(h,48,soundBase::invalid);
  1194. }
  1195. }
  1196. void CGBoat::initObj()
  1197. {
  1198. hero = nullptr;
  1199. }
  1200. void CGSirens::initObj()
  1201. {
  1202. blockVisit = true;
  1203. }
  1204. std::string CGSirens::getHoverText(const CGHeroInstance * hero) const
  1205. {
  1206. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  1207. }
  1208. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  1209. {
  1210. InfoWindow iw;
  1211. iw.soundID = soundBase::DANGER;
  1212. iw.player = h->tempOwner;
  1213. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  1214. {
  1215. iw.text.addTxt(MetaString::ADVOB_TXT,133);
  1216. }
  1217. else
  1218. {
  1219. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  1220. TExpType xp = 0;
  1221. for (auto i = h->Slots().begin(); i != h->Slots().end(); i++)
  1222. {
  1223. TQuantity drown = i->second->count * 0.3;
  1224. if(drown)
  1225. {
  1226. cb->changeStackCount(StackLocation(h, i->first), -drown);
  1227. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  1228. }
  1229. }
  1230. if(xp)
  1231. {
  1232. xp = h->calculateXp(xp);
  1233. iw.text.addTxt(MetaString::ADVOB_TXT,132);
  1234. iw.text.addReplacement(xp);
  1235. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, xp, false);
  1236. }
  1237. else
  1238. {
  1239. iw.text.addTxt(MetaString::ADVOB_TXT,134);
  1240. }
  1241. }
  1242. cb->showInfoDialog(&iw);
  1243. }
  1244. CGShipyard::CGShipyard()
  1245. :IShipyard(this)
  1246. {
  1247. }
  1248. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  1249. {
  1250. // H J L K I
  1251. // A x S x B
  1252. // C E G F D
  1253. offsets += int3(-3,0,0), int3(1,0,0), //AB
  1254. int3(-3,1,0), int3(1,1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0), //CDEFG
  1255. int3(-3,-1,0), int3(1,-1,0), int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0); //HIJKL
  1256. }
  1257. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  1258. {
  1259. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  1260. cb->setOwner(this, h->tempOwner);
  1261. auto s = shipyardStatus();
  1262. if(s != IBoatGenerator::GOOD)
  1263. {
  1264. InfoWindow iw;
  1265. iw.player = tempOwner;
  1266. getProblemText(iw.text, h);
  1267. cb->showInfoDialog(&iw);
  1268. }
  1269. else
  1270. {
  1271. openWindow(OpenWindow::SHIPYARD_WINDOW,id.getNum(),h->id.getNum());
  1272. }
  1273. }
  1274. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  1275. {
  1276. if (!wasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  1277. {
  1278. if (cb->getResource(h->tempOwner, Res::GOLD) >= 1000) //if he can afford a map
  1279. {
  1280. //ask if he wants to buy one
  1281. int text=0;
  1282. switch (subID)
  1283. {
  1284. case 0:
  1285. text = 25;
  1286. break;
  1287. case 1:
  1288. text = 26;
  1289. break;
  1290. case 2:
  1291. text = 27;
  1292. break;
  1293. default:
  1294. logGlobal->warnStream() << "Unrecognized subtype of cartographer";
  1295. }
  1296. assert(text);
  1297. BlockingDialog bd (true, false);
  1298. bd.player = h->getOwner();
  1299. bd.soundID = soundBase::LIGHTHOUSE;
  1300. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  1301. cb->showBlockingDialog (&bd);
  1302. }
  1303. else //if he cannot afford
  1304. {
  1305. showInfoDialog(h,28,soundBase::CAVEHEAD);
  1306. }
  1307. }
  1308. else //if he already visited carographer
  1309. {
  1310. showInfoDialog(h,24,soundBase::CAVEHEAD);
  1311. }
  1312. }
  1313. void CCartographer::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1314. {
  1315. if (answer) //if hero wants to buy map
  1316. {
  1317. cb->giveResource (hero->tempOwner, Res::GOLD, -1000);
  1318. FoWChange fw;
  1319. fw.mode = 1;
  1320. fw.player = hero->tempOwner;
  1321. //subIDs of different types of cartographers:
  1322. //water = 0; land = 1; underground = 2;
  1323. cb->getAllTiles (fw.tiles, hero->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  1324. cb->sendAndApply (&fw);
  1325. cb->setObjProperty (id, 10, hero->tempOwner.getNum());
  1326. }
  1327. }
  1328. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  1329. {
  1330. cb->showThievesGuildWindow(h->tempOwner, id);
  1331. }
  1332. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  1333. {
  1334. InfoWindow iw;
  1335. iw.player = h->tempOwner;
  1336. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  1337. assert(ts);
  1338. TeamID team = ts->id;
  1339. if(!wasVisited(team))
  1340. {
  1341. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  1342. cb->sendAndApply(&iw);
  1343. cb->setObjProperty(id, 20, h->tempOwner.getNum()); //increment general visited obelisks counter
  1344. openWindow(OpenWindow::PUZZLE_MAP, h->tempOwner.getNum());
  1345. cb->setObjProperty(id, 10, h->tempOwner.getNum()); //mark that particular obelisk as visited
  1346. }
  1347. else
  1348. {
  1349. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  1350. cb->sendAndApply(&iw);
  1351. }
  1352. }
  1353. void CGObelisk::initObj()
  1354. {
  1355. obeliskCount++;
  1356. }
  1357. std::string CGObelisk::getHoverText(PlayerColor player) const
  1358. {
  1359. return getObjectName() + " " + visitedTxt(wasVisited(player));
  1360. }
  1361. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  1362. {
  1363. CPlayersVisited::setPropertyDer(what, val);
  1364. switch(what)
  1365. {
  1366. case 20:
  1367. assert(val < PlayerColor::PLAYER_LIMIT_I);
  1368. visited[TeamID(val)]++;
  1369. if(visited[TeamID(val)] > obeliskCount)
  1370. {
  1371. logGlobal->errorStream() << "Error: Visited " << visited[TeamID(val)] << "\t\t" << obeliskCount;
  1372. assert(0);
  1373. }
  1374. break;
  1375. }
  1376. }
  1377. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  1378. {
  1379. if(h->tempOwner != tempOwner)
  1380. {
  1381. PlayerColor oldOwner = tempOwner;
  1382. cb->setOwner(this,h->tempOwner); //not ours? flag it!
  1383. showInfoDialog(h,69,soundBase::LIGHTHOUSE);
  1384. giveBonusTo(h->tempOwner);
  1385. if(oldOwner < PlayerColor::PLAYER_LIMIT) //remove bonus from old owner
  1386. {
  1387. RemoveBonus rb(RemoveBonus::PLAYER);
  1388. rb.whoID = oldOwner.getNum();
  1389. rb.source = Bonus::OBJECT;
  1390. rb.id = id.getNum();
  1391. cb->sendAndApply(&rb);
  1392. }
  1393. }
  1394. }
  1395. void CGLighthouse::initObj()
  1396. {
  1397. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  1398. {
  1399. giveBonusTo(tempOwner);
  1400. }
  1401. }
  1402. std::string CGLighthouse::getHoverText(PlayerColor player) const
  1403. {
  1404. //TODO: owned by %s player
  1405. return getObjectName();
  1406. }
  1407. void CGLighthouse::giveBonusTo( PlayerColor player ) const
  1408. {
  1409. GiveBonus gb(GiveBonus::PLAYER);
  1410. gb.bonus.type = Bonus::SEA_MOVEMENT;
  1411. gb.bonus.val = 500;
  1412. gb.id = player.getNum();
  1413. gb.bonus.duration = Bonus::PERMANENT;
  1414. gb.bonus.source = Bonus::OBJECT;
  1415. gb.bonus.sid = id.getNum();
  1416. cb->sendAndApply(&gb);
  1417. }