MiscObjects.cpp 40 KB

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