2
0

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. 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. CGResource::CGResource()
  554. {
  555. amount = 0;
  556. }
  557. void CGResource::initObj()
  558. {
  559. blockVisit = true;
  560. if(!amount)
  561. {
  562. switch(subID)
  563. {
  564. case 6:
  565. amount = cb->gameState()->getRandomGenerator().nextInt(500, 1000);
  566. break;
  567. case 0: case 2:
  568. amount = cb->gameState()->getRandomGenerator().nextInt(6, 10);
  569. break;
  570. default:
  571. amount = cb->gameState()->getRandomGenerator().nextInt(3, 5);
  572. break;
  573. }
  574. }
  575. }
  576. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  577. {
  578. if(stacksCount())
  579. {
  580. if(message.size())
  581. {
  582. BlockingDialog ynd(true,false);
  583. ynd.player = h->getOwner();
  584. ynd.text << message;
  585. cb->showBlockingDialog(&ynd);
  586. }
  587. else
  588. {
  589. blockingDialogAnswered(h, true); //behave as if player accepted battle
  590. }
  591. }
  592. else
  593. {
  594. if(message.length())
  595. {
  596. InfoWindow iw;
  597. iw.player = h->tempOwner;
  598. iw.text << message;
  599. cb->showInfoDialog(&iw);
  600. }
  601. collectRes(h->getOwner());
  602. }
  603. }
  604. void CGResource::collectRes( PlayerColor player ) const
  605. {
  606. cb->giveResource(player, static_cast<Res::ERes>(subID), amount);
  607. ShowInInfobox sii;
  608. sii.player = player;
  609. sii.c = Component(Component::RESOURCE,subID,amount,0);
  610. sii.text.addTxt(MetaString::ADVOB_TXT,113);
  611. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  612. cb->showCompInfo(&sii);
  613. cb->removeObject(this);
  614. }
  615. void CGResource::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  616. {
  617. if(result.winner == 0) //attacker won
  618. collectRes(hero->getOwner());
  619. }
  620. void CGResource::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  621. {
  622. if(answer)
  623. cb->startBattleI(hero, this);
  624. }
  625. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  626. {
  627. ObjectInstanceID destinationid;
  628. switch(ID)
  629. {
  630. case Obj::MONOLITH_ONE_WAY_ENTRANCE: //one way - find corresponding exit monolith
  631. {
  632. 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())
  633. {
  634. destinationid = *RandomGeneratorUtil::nextItem(objs[Obj::MONOLITH_ONE_WAY_EXIT][subID], cb->gameState()->getRandomGenerator());
  635. }
  636. else
  637. {
  638. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  639. }
  640. break;
  641. }
  642. case Obj::MONOLITH_TWO_WAY://two way monolith - pick any other one
  643. case Obj::WHIRLPOOL: //Whirlpool
  644. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  645. {
  646. //choose another exit
  647. do
  648. {
  649. destinationid = *RandomGeneratorUtil::nextItem(objs[ID][subID], cb->gameState()->getRandomGenerator());
  650. } while(destinationid == id);
  651. if (ID == Obj::WHIRLPOOL)
  652. {
  653. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  654. {
  655. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  656. { //we can't remove last unit
  657. SlotID targetstack = h->Slots().begin()->first; //slot numbers may vary
  658. for(auto i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  659. {
  660. if (h->getPower(targetstack) > h->getPower(i->first))
  661. {
  662. targetstack = (i->first);
  663. }
  664. }
  665. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  666. vstd::amax(countToTake, 1);
  667. InfoWindow iw;
  668. iw.player = h->tempOwner;
  669. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  670. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  671. cb->showInfoDialog(&iw);
  672. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  673. }
  674. }
  675. }
  676. }
  677. else
  678. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  679. break;
  680. case Obj::SUBTERRANEAN_GATE: //find nearest subterranean gate on the other level
  681. {
  682. destinationid = getMatchingGate(id);
  683. if(destinationid == ObjectInstanceID()) //no exit
  684. {
  685. showInfoDialog(h,153,0);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  686. }
  687. break;
  688. }
  689. }
  690. if(destinationid == ObjectInstanceID())
  691. {
  692. logGlobal->warnStream() << "Cannot find exit... (obj at " << pos << ") :( ";
  693. return;
  694. }
  695. if (ID == Obj::WHIRLPOOL)
  696. {
  697. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  698. auto & tile = *RandomGeneratorUtil::nextItem(tiles, cb->gameState()->getRandomGenerator());
  699. cb->moveHero(h->id, tile + int3(1,0,0), true);
  700. }
  701. else
  702. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  703. }
  704. void CGTeleport::initObj()
  705. {
  706. int si = subID;
  707. switch (ID)
  708. {
  709. case Obj::SUBTERRANEAN_GATE://ignore subterranean gates subid
  710. case Obj::WHIRLPOOL:
  711. {
  712. si = 0;
  713. break;
  714. }
  715. default:
  716. break;
  717. }
  718. objs[ID][si].push_back(id);
  719. }
  720. void CGTeleport::postInit() //matches subterranean gates into pairs
  721. {
  722. //split on underground and surface gates
  723. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  724. for(auto & elem : objs[Obj::SUBTERRANEAN_GATE][0])
  725. {
  726. const CGObjectInstance *hlp = cb->getObj(elem);
  727. gatesSplit[hlp->pos.z].push_back(hlp);
  728. }
  729. //sort by position
  730. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), [](const CGObjectInstance * a, const CGObjectInstance * b)
  731. {
  732. return a->pos < b->pos;
  733. });
  734. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  735. {
  736. const CGObjectInstance *cur = gatesSplit[0][i];
  737. //find nearest underground exit
  738. std::pair<int, si32> best(-1, std::numeric_limits<si32>::max()); //pair<pos_in_vector, distance^2>
  739. for(int j = 0; j < gatesSplit[1].size(); j++)
  740. {
  741. const CGObjectInstance *checked = gatesSplit[1][j];
  742. if(!checked)
  743. continue;
  744. si32 hlp = checked->pos.dist2dSQ(cur->pos);
  745. if(hlp < best.second)
  746. {
  747. best.first = j;
  748. best.second = hlp;
  749. }
  750. }
  751. if(best.first >= 0) //found pair
  752. {
  753. gates.push_back(std::make_pair(cur->id, gatesSplit[1][best.first]->id));
  754. gatesSplit[1][best.first] = nullptr;
  755. }
  756. else
  757. gates.push_back(std::make_pair(cur->id, ObjectInstanceID()));
  758. }
  759. objs.erase(Obj::SUBTERRANEAN_GATE);
  760. }
  761. ObjectInstanceID CGTeleport::getMatchingGate(ObjectInstanceID id)
  762. {
  763. for(auto & gate : gates)
  764. {
  765. if(gate.first == id)
  766. return gate.second;
  767. if(gate.second == id)
  768. return gate.first;
  769. }
  770. return ObjectInstanceID();
  771. }
  772. void CGArtifact::initObj()
  773. {
  774. blockVisit = true;
  775. if(ID == Obj::ARTIFACT)
  776. {
  777. if (!storedArtifact)
  778. {
  779. auto a = new CArtifactInstance();
  780. cb->gameState()->map->addNewArtifactInstance(a);
  781. storedArtifact = a;
  782. }
  783. if(!storedArtifact->artType)
  784. storedArtifact->setType(VLC->arth->artifacts[subID]);
  785. }
  786. if(ID == Obj::SPELL_SCROLL)
  787. subID = 1;
  788. assert(storedArtifact->artType);
  789. assert(storedArtifact->getParentNodes().size());
  790. //assert(storedArtifact->artType->id == subID); //this does not stop desync
  791. }
  792. std::string CGArtifact::getObjectName() const
  793. {
  794. return VLC->arth->artifacts[subID]->Name();
  795. }
  796. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  797. {
  798. if(!stacksCount())
  799. {
  800. InfoWindow iw;
  801. iw.player = h->tempOwner;
  802. switch(ID)
  803. {
  804. case Obj::ARTIFACT:
  805. {
  806. iw.soundID = soundBase::treasure; //play sound only for non-scroll arts
  807. iw.components.push_back(Component(Component::ARTIFACT,subID,0,0));
  808. if(message.length())
  809. iw.text << message;
  810. else
  811. {
  812. if (VLC->arth->artifacts[subID]->EventText().size())
  813. iw.text << std::pair<ui8, ui32> (MetaString::ART_EVNTS, subID);
  814. else //fix for mod artifacts with no event text
  815. {
  816. iw.text.addTxt (MetaString::ADVOB_TXT, 183); //% has found treasure
  817. iw.text.addReplacement (h->name);
  818. }
  819. }
  820. }
  821. break;
  822. case Obj::SPELL_SCROLL:
  823. {
  824. int spellID = storedArtifact->getGivenSpellID();
  825. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  826. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  827. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  828. }
  829. break;
  830. }
  831. cb->showInfoDialog(&iw);
  832. pick(h);
  833. }
  834. else
  835. {
  836. if(message.size())
  837. {
  838. BlockingDialog ynd(true,false);
  839. ynd.player = h->getOwner();
  840. ynd.text << message;
  841. cb->showBlockingDialog(&ynd);
  842. }
  843. else
  844. {
  845. blockingDialogAnswered(h, true);
  846. }
  847. }
  848. }
  849. void CGArtifact::pick(const CGHeroInstance * h) const
  850. {
  851. cb->giveHeroArtifact(h, storedArtifact, ArtifactPosition::FIRST_AVAILABLE);
  852. cb->removeObject(this);
  853. }
  854. void CGArtifact::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  855. {
  856. if(result.winner == 0) //attacker won
  857. pick(hero);
  858. }
  859. void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  860. {
  861. if(answer)
  862. cb->startBattleI(hero, this);
  863. }
  864. void CGWitchHut::initObj()
  865. {
  866. if (allowedAbilities.empty()) //this can happen for RMG. regular maps load abilities from map file
  867. {
  868. for (int i = 0; i < GameConstants::SKILL_QUANTITY; i++)
  869. allowedAbilities.push_back(i);
  870. }
  871. ability = *RandomGeneratorUtil::nextItem(allowedAbilities, cb->gameState()->getRandomGenerator());
  872. }
  873. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  874. {
  875. InfoWindow iw;
  876. iw.soundID = soundBase::gazebo;
  877. iw.player = h->getOwner();
  878. if(!wasVisited(h->tempOwner))
  879. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  880. ui32 txt_id;
  881. if(h->getSecSkillLevel(SecondarySkill(ability))) //you already know this skill
  882. {
  883. txt_id =172;
  884. }
  885. else if(!h->canLearnSkill()) //already all skills slots used
  886. {
  887. txt_id = 173;
  888. }
  889. else //give sec skill
  890. {
  891. iw.components.push_back(Component(Component::SEC_SKILL, ability, 1, 0));
  892. txt_id = 171;
  893. cb->changeSecSkill(h, SecondarySkill(ability), 1, true);
  894. }
  895. iw.text.addTxt(MetaString::ADVOB_TXT,txt_id);
  896. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  897. cb->showInfoDialog(&iw);
  898. }
  899. std::string CGWitchHut::getHoverText(PlayerColor player) const
  900. {
  901. std::string hoverName = getObjectName();
  902. if(wasVisited(player))
  903. {
  904. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  905. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  906. }
  907. return hoverName;
  908. }
  909. std::string CGWitchHut::getHoverText(const CGHeroInstance * hero) const
  910. {
  911. std::string hoverName = getHoverText(hero->tempOwner);
  912. if(hero->getSecSkillLevel(SecondarySkill(ability))) //hero knows that ability
  913. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  914. return hoverName;
  915. }
  916. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  917. {
  918. int message;
  919. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  920. {
  921. message = 78;//"A second drink at the well in one day will not help you."
  922. }
  923. else if(h->mana < h->manaLimit())
  924. {
  925. giveDummyBonus(h->id);
  926. cb->setManaPoints(h->id,h->manaLimit());
  927. message = 77;
  928. }
  929. else
  930. {
  931. message = 79;
  932. }
  933. showInfoDialog(h,message,soundBase::faerie);
  934. }
  935. std::string CGMagicWell::getHoverText(const CGHeroInstance * hero) const
  936. {
  937. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  938. }
  939. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  940. {
  941. InfoWindow iw;
  942. iw.player = h->tempOwner;
  943. switch (ID)
  944. {
  945. case Obj::REDWOOD_OBSERVATORY:
  946. case Obj::PILLAR_OF_FIRE:
  947. {
  948. iw.soundID = soundBase::LIGHTHOUSE;
  949. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==Obj::PILLAR_OF_FIRE));
  950. FoWChange fw;
  951. fw.player = h->tempOwner;
  952. fw.mode = 1;
  953. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  954. cb->sendAndApply (&fw);
  955. break;
  956. }
  957. case Obj::COVER_OF_DARKNESS:
  958. {
  959. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  960. for (auto & player : cb->gameState()->players)
  961. {
  962. if (cb->getPlayerStatus(player.first) == EPlayerStatus::INGAME &&
  963. cb->getPlayerRelations(player.first, h->tempOwner) == PlayerRelations::ENEMIES)
  964. cb->changeFogOfWar(visitablePos(), 20, player.first, true);
  965. }
  966. break;
  967. }
  968. }
  969. cb->showInfoDialog(&iw);
  970. }
  971. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  972. {
  973. if(spell == SpellID::NONE)
  974. {
  975. logGlobal->errorStream() << "Not initialized shrine visited!";
  976. return;
  977. }
  978. if(!wasVisited(h->tempOwner))
  979. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  980. InfoWindow iw;
  981. iw.soundID = soundBase::temple;
  982. iw.player = h->getOwner();
  983. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  984. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  985. iw.text << ".";
  986. if(!h->getArt(ArtifactPosition::SPELLBOOK))
  987. {
  988. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  989. }
  990. else if(ID == Obj::SHRINE_OF_MAGIC_THOUGHT && !h->getSecSkillLevel(SecondarySkill::WISDOM)) //it's third level spell and hero doesn't have wisdom
  991. {
  992. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  993. }
  994. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  995. {
  996. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  997. }
  998. else //give spell
  999. {
  1000. std::set<SpellID> spells;
  1001. spells.insert(spell);
  1002. cb->changeSpells(h, true, spells);
  1003. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  1004. }
  1005. cb->showInfoDialog(&iw);
  1006. }
  1007. void CGShrine::initObj()
  1008. {
  1009. if(spell == SpellID::NONE) //spell not set
  1010. {
  1011. int level = ID-87;
  1012. std::vector<SpellID> possibilities;
  1013. cb->getAllowedSpells (possibilities, level);
  1014. if(possibilities.empty())
  1015. {
  1016. logGlobal->errorStream() << "Error: cannot init shrine, no allowed spells!";
  1017. return;
  1018. }
  1019. spell = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1020. }
  1021. }
  1022. std::string CGShrine::getHoverText(PlayerColor player) const
  1023. {
  1024. std::string hoverName = getObjectName();
  1025. if(wasVisited(player))
  1026. {
  1027. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  1028. boost::algorithm::replace_first(hoverName,"%s", spell.toSpell()->name);
  1029. }
  1030. return hoverName;
  1031. }
  1032. std::string CGShrine::getHoverText(const CGHeroInstance * hero) const
  1033. {
  1034. std::string hoverName = getHoverText(hero->tempOwner);
  1035. if(vstd::contains(hero->spells, spell)) //hero knows that spell
  1036. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  1037. return hoverName;
  1038. }
  1039. void CGSignBottle::initObj()
  1040. {
  1041. //if no text is set than we pick random from the predefined ones
  1042. if(message.empty())
  1043. {
  1044. message = *RandomGeneratorUtil::nextItem(VLC->generaltexth->randsign, cb->gameState()->getRandomGenerator());
  1045. }
  1046. if(ID == Obj::OCEAN_BOTTLE)
  1047. {
  1048. blockVisit = true;
  1049. }
  1050. }
  1051. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  1052. {
  1053. InfoWindow iw;
  1054. iw.soundID = soundBase::STORE;
  1055. iw.player = h->getOwner();
  1056. iw.text << message;
  1057. cb->showInfoDialog(&iw);
  1058. if(ID == Obj::OCEAN_BOTTLE)
  1059. cb->removeObject(this);
  1060. }
  1061. //TODO: remove
  1062. //void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  1063. //{
  1064. //
  1065. //}
  1066. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  1067. {
  1068. EBonusType type = bonusType;
  1069. int bid = bonusID;
  1070. //check if the bonus if applicable, if not - give primary skill (always possible)
  1071. int ssl = h->getSecSkillLevel(SecondarySkill(bid)); //current sec skill level, used if bonusType == 1
  1072. if((type == SECONDARY_SKILL
  1073. && ((ssl == 3) || (!ssl && !h->canLearnSkill()))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  1074. || (type == SPELL && (!h->getArt(ArtifactPosition::SPELLBOOK) || vstd::contains(h->spells, (ui32) bid)
  1075. || ( SpellID(bid).toSpell()->level > h->getSecSkillLevel(SecondarySkill::WISDOM) + 2)
  1076. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  1077. {
  1078. type = PRIM_SKILL;
  1079. bid = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS - 1);
  1080. }
  1081. InfoWindow iw;
  1082. iw.soundID = soundBase::gazebo;
  1083. iw.player = h->getOwner();
  1084. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  1085. switch (type)
  1086. {
  1087. case PRIM_SKILL:
  1088. cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(bid),+1);
  1089. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  1090. break;
  1091. case SECONDARY_SKILL:
  1092. cb->changeSecSkill(h,SecondarySkill(bid),+1);
  1093. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  1094. break;
  1095. case SPELL:
  1096. {
  1097. std::set<SpellID> hlp;
  1098. hlp.insert(SpellID(bid));
  1099. cb->changeSpells(h,true,hlp);
  1100. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  1101. }
  1102. break;
  1103. default:
  1104. logGlobal->errorStream() << "Error: wrong bonus type (" << (int)type << ") for Scholar!\n";
  1105. return;
  1106. }
  1107. cb->showInfoDialog(&iw);
  1108. cb->removeObject(this);
  1109. }
  1110. void CGScholar::initObj()
  1111. {
  1112. blockVisit = true;
  1113. if(bonusType == RANDOM)
  1114. {
  1115. bonusType = static_cast<EBonusType>(cb->gameState()->getRandomGenerator().nextInt(2));
  1116. switch(bonusType)
  1117. {
  1118. case PRIM_SKILL:
  1119. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS -1);
  1120. break;
  1121. case SECONDARY_SKILL:
  1122. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::SKILL_QUANTITY -1);
  1123. break;
  1124. case SPELL:
  1125. std::vector<SpellID> possibilities;
  1126. for (int i = 1; i < 6; ++i)
  1127. cb->getAllowedSpells (possibilities, i);
  1128. bonusID = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  1134. {
  1135. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  1136. if (!ally && stacksCount() > 0) {
  1137. //TODO: Find a way to apply magic garrison effects in battle.
  1138. cb->startBattleI(h, this);
  1139. return;
  1140. }
  1141. //New owner.
  1142. if (!ally)
  1143. cb->setOwner(this, h->tempOwner);
  1144. cb->showGarrisonDialog(id, h->id, removableUnits);
  1145. }
  1146. bool CGGarrison::passableFor(PlayerColor player) const
  1147. {
  1148. //FIXME: identical to same method in CGTownInstance
  1149. if ( !stacksCount() )//empty - anyone can visit
  1150. return true;
  1151. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  1152. return false;
  1153. if (cb->getPlayerRelations(tempOwner, player) != PlayerRelations::ENEMIES)
  1154. return true;
  1155. return false;
  1156. }
  1157. void CGGarrison::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  1158. {
  1159. if (result.winner == 0)
  1160. onHeroVisit(hero);
  1161. }
  1162. void CGMagi::initObj()
  1163. {
  1164. if (ID == Obj::EYE_OF_MAGI)
  1165. {
  1166. blockVisit = true;
  1167. eyelist[subID].push_back(id);
  1168. }
  1169. }
  1170. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  1171. {
  1172. if (ID == Obj::HUT_OF_MAGI)
  1173. {
  1174. showInfoDialog(h, 61, soundBase::LIGHTHOUSE);
  1175. if (!eyelist[subID].empty())
  1176. {
  1177. CenterView cv;
  1178. cv.player = h->tempOwner;
  1179. cv.focusTime = 2000;
  1180. FoWChange fw;
  1181. fw.player = h->tempOwner;
  1182. fw.mode = 1;
  1183. fw.waitForDialogs = true;
  1184. for(auto it : eyelist[subID])
  1185. {
  1186. const CGObjectInstance *eye = cb->getObj(it);
  1187. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  1188. cb->sendAndApply(&fw);
  1189. cv.pos = eye->pos;
  1190. cb->sendAndApply(&cv);
  1191. }
  1192. cv.pos = h->getPosition(false);
  1193. cb->sendAndApply(&cv);
  1194. }
  1195. }
  1196. else if (ID == Obj::EYE_OF_MAGI)
  1197. {
  1198. showInfoDialog(h,48,soundBase::invalid);
  1199. }
  1200. }
  1201. void CGBoat::initObj()
  1202. {
  1203. hero = nullptr;
  1204. }
  1205. void CGSirens::initObj()
  1206. {
  1207. blockVisit = true;
  1208. }
  1209. std::string CGSirens::getHoverText(const CGHeroInstance * hero) const
  1210. {
  1211. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  1212. }
  1213. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  1214. {
  1215. InfoWindow iw;
  1216. iw.soundID = soundBase::DANGER;
  1217. iw.player = h->tempOwner;
  1218. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  1219. {
  1220. iw.text.addTxt(MetaString::ADVOB_TXT,133);
  1221. }
  1222. else
  1223. {
  1224. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  1225. TExpType xp = 0;
  1226. for (auto i = h->Slots().begin(); i != h->Slots().end(); i++)
  1227. {
  1228. TQuantity drown = i->second->count * 0.3;
  1229. if(drown)
  1230. {
  1231. cb->changeStackCount(StackLocation(h, i->first), -drown);
  1232. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  1233. }
  1234. }
  1235. if(xp)
  1236. {
  1237. xp = h->calculateXp(xp);
  1238. iw.text.addTxt(MetaString::ADVOB_TXT,132);
  1239. iw.text.addReplacement(xp);
  1240. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, xp, false);
  1241. }
  1242. else
  1243. {
  1244. iw.text.addTxt(MetaString::ADVOB_TXT,134);
  1245. }
  1246. }
  1247. cb->showInfoDialog(&iw);
  1248. }
  1249. CGShipyard::CGShipyard()
  1250. :IShipyard(this)
  1251. {
  1252. }
  1253. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  1254. {
  1255. // H J L K I
  1256. // A x S x B
  1257. // C E G F D
  1258. offsets += 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. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  1263. {
  1264. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  1265. cb->setOwner(this, h->tempOwner);
  1266. auto s = shipyardStatus();
  1267. if(s != IBoatGenerator::GOOD)
  1268. {
  1269. InfoWindow iw;
  1270. iw.player = tempOwner;
  1271. getProblemText(iw.text, h);
  1272. cb->showInfoDialog(&iw);
  1273. }
  1274. else
  1275. {
  1276. openWindow(OpenWindow::SHIPYARD_WINDOW,id.getNum(),h->id.getNum());
  1277. }
  1278. }
  1279. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  1280. {
  1281. if (!wasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  1282. {
  1283. if (cb->getResource(h->tempOwner, Res::GOLD) >= 1000) //if he can afford a map
  1284. {
  1285. //ask if he wants to buy one
  1286. int text=0;
  1287. switch (subID)
  1288. {
  1289. case 0:
  1290. text = 25;
  1291. break;
  1292. case 1:
  1293. text = 26;
  1294. break;
  1295. case 2:
  1296. text = 27;
  1297. break;
  1298. default:
  1299. logGlobal->warnStream() << "Unrecognized subtype of cartographer";
  1300. }
  1301. assert(text);
  1302. BlockingDialog bd (true, false);
  1303. bd.player = h->getOwner();
  1304. bd.soundID = soundBase::LIGHTHOUSE;
  1305. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  1306. cb->showBlockingDialog (&bd);
  1307. }
  1308. else //if he cannot afford
  1309. {
  1310. showInfoDialog(h,28,soundBase::CAVEHEAD);
  1311. }
  1312. }
  1313. else //if he already visited carographer
  1314. {
  1315. showInfoDialog(h,24,soundBase::CAVEHEAD);
  1316. }
  1317. }
  1318. void CCartographer::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1319. {
  1320. if (answer) //if hero wants to buy map
  1321. {
  1322. cb->giveResource (hero->tempOwner, Res::GOLD, -1000);
  1323. FoWChange fw;
  1324. fw.mode = 1;
  1325. fw.player = hero->tempOwner;
  1326. //subIDs of different types of cartographers:
  1327. //water = 0; land = 1; underground = 2;
  1328. cb->getAllTiles (fw.tiles, hero->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  1329. cb->sendAndApply (&fw);
  1330. cb->setObjProperty (id, 10, hero->tempOwner.getNum());
  1331. }
  1332. }
  1333. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  1334. {
  1335. cb->showThievesGuildWindow(h->tempOwner, id);
  1336. }
  1337. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  1338. {
  1339. InfoWindow iw;
  1340. iw.player = h->tempOwner;
  1341. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  1342. assert(ts);
  1343. TeamID team = ts->id;
  1344. if(!wasVisited(team))
  1345. {
  1346. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  1347. cb->sendAndApply(&iw);
  1348. cb->setObjProperty(id, 20, h->tempOwner.getNum()); //increment general visited obelisks counter
  1349. openWindow(OpenWindow::PUZZLE_MAP, h->tempOwner.getNum());
  1350. cb->setObjProperty(id, 10, h->tempOwner.getNum()); //mark that particular obelisk as visited
  1351. }
  1352. else
  1353. {
  1354. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  1355. cb->sendAndApply(&iw);
  1356. }
  1357. }
  1358. void CGObelisk::initObj()
  1359. {
  1360. obeliskCount++;
  1361. }
  1362. std::string CGObelisk::getHoverText(PlayerColor player) const
  1363. {
  1364. return getObjectName() + " " + visitedTxt(wasVisited(player));
  1365. }
  1366. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  1367. {
  1368. CPlayersVisited::setPropertyDer(what, val);
  1369. switch(what)
  1370. {
  1371. case 20:
  1372. assert(val < PlayerColor::PLAYER_LIMIT_I);
  1373. visited[TeamID(val)]++;
  1374. if(visited[TeamID(val)] > obeliskCount)
  1375. {
  1376. logGlobal->errorStream() << "Error: Visited " << visited[TeamID(val)] << "\t\t" << obeliskCount;
  1377. assert(0);
  1378. }
  1379. break;
  1380. }
  1381. }
  1382. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  1383. {
  1384. if(h->tempOwner != tempOwner)
  1385. {
  1386. PlayerColor oldOwner = tempOwner;
  1387. cb->setOwner(this,h->tempOwner); //not ours? flag it!
  1388. showInfoDialog(h,69,soundBase::LIGHTHOUSE);
  1389. giveBonusTo(h->tempOwner);
  1390. if(oldOwner < PlayerColor::PLAYER_LIMIT) //remove bonus from old owner
  1391. {
  1392. RemoveBonus rb(RemoveBonus::PLAYER);
  1393. rb.whoID = oldOwner.getNum();
  1394. rb.source = Bonus::OBJECT;
  1395. rb.id = id.getNum();
  1396. cb->sendAndApply(&rb);
  1397. }
  1398. }
  1399. }
  1400. void CGLighthouse::initObj()
  1401. {
  1402. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  1403. {
  1404. giveBonusTo(tempOwner);
  1405. }
  1406. }
  1407. std::string CGLighthouse::getHoverText(PlayerColor player) const
  1408. {
  1409. //TODO: owned by %s player
  1410. return getObjectName();
  1411. }
  1412. void CGLighthouse::giveBonusTo( PlayerColor player ) const
  1413. {
  1414. GiveBonus gb(GiveBonus::PLAYER);
  1415. gb.bonus.type = Bonus::SEA_MOVEMENT;
  1416. gb.bonus.val = 500;
  1417. gb.id = player.getNum();
  1418. gb.bonus.duration = Bonus::PERMANENT;
  1419. gb.bonus.source = Bonus::OBJECT;
  1420. gb.bonus.sid = id.getNum();
  1421. cb->sendAndApply(&gb);
  1422. }