CSpellWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. #include "StdInc.h"
  2. #include "CSpellWindow.h"
  3. #include "../../lib/ScopeGuard.h"
  4. #include "CAdvmapInterface.h"
  5. #include "GUIClasses.h"
  6. #include "InfoWindows.h"
  7. #include "CCastleInterface.h"
  8. #include "../CGameInfo.h"
  9. #include "../CMessage.h"
  10. #include "../CMT.h"
  11. #include "../CPlayerInterface.h"
  12. #include "../CVideoHandler.h"
  13. #include "../Graphics.h"
  14. #include "../battle/CBattleInterface.h"
  15. #include "../gui/CAnimation.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/SDL_Extensions.h"
  18. #include "../widgets/MiscWidgets.h"
  19. #include "../widgets/CComponent.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../../CCallback.h"
  22. #include "../../lib/CStack.h"
  23. #include "../../lib/CConfigHandler.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/CHeroHandler.h"
  26. #include "../../lib/spells/CSpellHandler.h"
  27. #include "../../lib/GameConstants.h"
  28. #include "../../lib/CGameState.h"
  29. #include "../../lib/mapObjects/CGTownInstance.h"
  30. /*
  31. * CSpellWindow.cpp, part of VCMI engine
  32. *
  33. * Authors: listed in file AUTHORS in main folder
  34. *
  35. * License: GNU General Public License v2.0 or later
  36. * Full text of license available in license.txt file, in main folder
  37. *
  38. */
  39. CSpellWindow::InteractiveArea::InteractiveArea(const SDL_Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  40. {
  41. addUsedEvents(LCLICK | RCLICK | HOVER);
  42. pos = myRect;
  43. onLeft = funcL;
  44. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  45. helpText = CGI->generaltexth->zelp[helpTextId].second;
  46. owner = _owner;
  47. }
  48. void CSpellWindow::InteractiveArea::clickLeft(tribool down, bool previousState)
  49. {
  50. if(!down)
  51. onLeft();
  52. }
  53. void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState)
  54. {
  55. adventureInt->handleRightClick(helpText, down);
  56. }
  57. void CSpellWindow::InteractiveArea::hover(bool on)
  58. {
  59. if(on)
  60. owner->statusBar->setText(hoverText);
  61. else
  62. owner->statusBar->clear();
  63. }
  64. class SpellbookSpellSorter
  65. {
  66. public:
  67. bool operator()(const CSpell * A, const CSpell * B)
  68. {
  69. if(A->level < B->level)
  70. return true;
  71. if(A->level > B->level)
  72. return false;
  73. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  74. {
  75. if(A->school.at((ESpellSchool)schoolId) && !B->school.at((ESpellSchool)schoolId))
  76. return true;
  77. if(!A->school.at((ESpellSchool)schoolId) && B->school.at((ESpellSchool)schoolId))
  78. return false;
  79. }
  80. return A->name < B->name;
  81. }
  82. } spellsorter;
  83. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  84. CWindowObject(PLAYER_COLORED, "SpelBack"),
  85. battleSpellsOnly(openOnBattleSpells),
  86. selectedTab(4),
  87. currentPage(0),
  88. myHero(_myHero),
  89. myInt(_myInt)
  90. {
  91. OBJ_CONSTRUCTION_CAPTURING_ALL;
  92. //initializing castable spells
  93. mySpells.reserve(CGI->spellh->objects.size());
  94. for(const CSpell * spell : CGI->spellh->objects)
  95. {
  96. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  97. mySpells.push_back(spell);
  98. }
  99. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  100. //initializing sizes of spellbook's parts
  101. for(auto & elem : sitesPerTabAdv)
  102. elem = 0;
  103. for(auto & elem : sitesPerTabBattle)
  104. elem = 0;
  105. for(const auto spell : mySpells)
  106. {
  107. int * sitesPerOurTab = spell->isCombatSpell() ? sitesPerTabBattle : sitesPerTabAdv;
  108. ++sitesPerOurTab[4];
  109. spell->forEachSchool([&sitesPerOurTab](const SpellSchoolInfo & school, bool & stop)
  110. {
  111. ++sitesPerOurTab[(ui8)school.id];
  112. });
  113. }
  114. if(sitesPerTabAdv[4] % 12 == 0)
  115. sitesPerTabAdv[4]/=12;
  116. else
  117. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  118. for(int v=0; v<4; ++v)
  119. {
  120. if(sitesPerTabAdv[v] <= 10)
  121. sitesPerTabAdv[v] = 1;
  122. else
  123. {
  124. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  125. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  126. else
  127. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  128. }
  129. }
  130. if(sitesPerTabBattle[4] % 12 == 0)
  131. sitesPerTabBattle[4]/=12;
  132. else
  133. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  134. for(int v=0; v<4; ++v)
  135. {
  136. if(sitesPerTabBattle[v] <= 10)
  137. sitesPerTabBattle[v] = 1;
  138. else
  139. {
  140. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  141. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  142. else
  143. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  144. }
  145. }
  146. //numbers of spell pages computed
  147. leftCorner = new CPicture("SpelTrnL.bmp", 97, 77);
  148. rightCorner = new CPicture("SpelTrnR.bmp", 487, 72);
  149. spells = std::make_shared<CAnimation>("Spells");
  150. spellTab = new CAnimImage("SpelTab", selectedTab, 0, 524, 88);
  151. schools = new CAnimImage("Schools",0,0,117,74);
  152. schoolBorders[0] = std::make_shared<CAnimation>("SplevA.def");
  153. schoolBorders[1] = std::make_shared<CAnimation>("SplevF.def");
  154. schoolBorders[2] = std::make_shared<CAnimation>("SplevW.def");
  155. schoolBorders[3] = std::make_shared<CAnimation>("SplevE.def");
  156. for(auto item : schoolBorders)
  157. item->load();
  158. mana = new CLabel(435, 426, FONT_SMALL, CENTER, Colors::YELLOW, boost::lexical_cast<std::string>(myHero->mana));
  159. statusBar = new CGStatusBar(7, 569, "Spelroll.bmp");
  160. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  161. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fexitb, this), 460, this);
  162. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  163. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this);
  164. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  165. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fadvSpellsb, this), 452, this);
  166. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  167. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fmanaPtsb, this), 459, this);
  168. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  169. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 0), 454, this);
  170. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  171. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 3), 457, this);
  172. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  173. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 1), 455, this);
  174. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  175. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 2), 456, this);
  176. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  177. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this);
  178. temp_rect = genRect(leftCorner->bg->h, leftCorner->bg->w, 97 + pos.x, 77 + pos.y);
  179. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), 450, this);
  180. temp_rect = genRect(rightCorner->bg->h, rightCorner->bg->w, 487 + pos.x, 72 + pos.y);
  181. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fRcornerb, this), 451, this);
  182. //areas for spells
  183. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  184. for(int v=0; v<12; ++v)
  185. {
  186. temp_rect = genRect(65, 78, xpos, ypos);
  187. spellAreas[v] = new SpellArea(temp_rect, this);
  188. if(v == 5) //to right page
  189. {
  190. xpos = 336 + pos.x; ypos = 90 + pos.y;
  191. }
  192. else
  193. {
  194. if(v%2 == 0)
  195. {
  196. xpos+=85;
  197. }
  198. else
  199. {
  200. xpos -= 85; ypos+=97;
  201. }
  202. }
  203. }
  204. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  205. spellTab->setFrame(selectedTab, 0);
  206. int cp = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  207. // spellbook last page battle index is not reset after battle, so this needs to stay here
  208. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  209. setCurrentPage(cp);
  210. computeSpellsPerArea();
  211. addUsedEvents(KEYBOARD);
  212. }
  213. CSpellWindow::~CSpellWindow()
  214. {
  215. for(auto item : schoolBorders)
  216. item->unload();
  217. spells->unload();
  218. }
  219. void CSpellWindow::fexitb()
  220. {
  221. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  222. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  223. GH.popIntTotally(this);
  224. }
  225. void CSpellWindow::fadvSpellsb()
  226. {
  227. if (battleSpellsOnly == true)
  228. {
  229. turnPageRight();
  230. battleSpellsOnly = false;
  231. setCurrentPage(0);
  232. }
  233. computeSpellsPerArea();
  234. }
  235. void CSpellWindow::fbattleSpellsb()
  236. {
  237. if (battleSpellsOnly == false)
  238. {
  239. turnPageLeft();
  240. battleSpellsOnly = true;
  241. setCurrentPage(0);
  242. }
  243. computeSpellsPerArea();
  244. }
  245. void CSpellWindow::fmanaPtsb()
  246. {
  247. }
  248. void CSpellWindow::selectSchool(int school)
  249. {
  250. if (selectedTab != school)
  251. {
  252. if (selectedTab < school)
  253. turnPageLeft();
  254. else
  255. turnPageRight();
  256. selectedTab = school;
  257. spellTab->setFrame(selectedTab, 0);
  258. setCurrentPage(0);
  259. }
  260. computeSpellsPerArea();
  261. }
  262. void CSpellWindow::fLcornerb()
  263. {
  264. if(currentPage>0)
  265. {
  266. turnPageLeft();
  267. setCurrentPage(currentPage - 1);
  268. }
  269. computeSpellsPerArea();
  270. GH.breakEventHandling();
  271. }
  272. void CSpellWindow::fRcornerb()
  273. {
  274. if((currentPage + 1) < (pagesWithinCurrentTab()))
  275. {
  276. turnPageRight();
  277. setCurrentPage(currentPage + 1);
  278. }
  279. computeSpellsPerArea();
  280. GH.breakEventHandling();
  281. }
  282. void CSpellWindow::show(SDL_Surface * to)
  283. {
  284. statusBar->show(to);
  285. }
  286. void CSpellWindow::computeSpellsPerArea()
  287. {
  288. std::vector<const CSpell *> spellsCurSite;
  289. spellsCurSite.reserve(mySpells.size());
  290. for(const CSpell * spell : mySpells)
  291. {
  292. if(spell->isCombatSpell() ^ !battleSpellsOnly
  293. && ((selectedTab == 4) || spell->school.at((ESpellSchool)selectedTab))
  294. )
  295. {
  296. spellsCurSite.push_back(spell);
  297. }
  298. }
  299. if(selectedTab == 4)
  300. {
  301. if(spellsCurSite.size() > 12)
  302. {
  303. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  304. if(spellsCurSite.size() > 12)
  305. {
  306. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  307. }
  308. }
  309. }
  310. else //selectedTab == 0, 1, 2 or 3
  311. {
  312. if(spellsCurSite.size() > 10)
  313. {
  314. if(currentPage == 0)
  315. {
  316. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  317. }
  318. else
  319. {
  320. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  321. if(spellsCurSite.size() > 12)
  322. {
  323. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  324. }
  325. }
  326. }
  327. }
  328. //applying
  329. if(selectedTab == 4 || currentPage != 0)
  330. {
  331. for(size_t c=0; c<12; ++c)
  332. {
  333. if(c < spellsCurSite.size())
  334. {
  335. spellAreas[c]->setSpell(spellsCurSite[c]);
  336. }
  337. else
  338. {
  339. spellAreas[c]->setSpell(nullptr);
  340. }
  341. }
  342. }
  343. else
  344. {
  345. spellAreas[0]->setSpell(nullptr);
  346. spellAreas[1]->setSpell(nullptr);
  347. for(size_t c=0; c<10; ++c)
  348. {
  349. if(c < spellsCurSite.size())
  350. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  351. else
  352. spellAreas[c+2]->setSpell(nullptr);
  353. }
  354. }
  355. redraw();
  356. }
  357. void CSpellWindow::setCurrentPage(int value)
  358. {
  359. currentPage = value;
  360. schools->visible = selectedTab!=4 && currentPage == 0;
  361. if(selectedTab != 4)
  362. schools->setFrame(selectedTab, 0);
  363. leftCorner->visible = currentPage != 0;
  364. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  365. mana->setText(boost::lexical_cast<std::string>(myHero->mana));//just in case, it will be possible to cast spell without closing book
  366. }
  367. void CSpellWindow::turnPageLeft()
  368. {
  369. if (settings["video"]["spellbookAnimation"].Bool())
  370. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  371. }
  372. void CSpellWindow::turnPageRight()
  373. {
  374. if (settings["video"]["spellbookAnimation"].Bool())
  375. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  376. }
  377. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  378. {
  379. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  380. {
  381. fexitb();
  382. return;
  383. }
  384. if(key.state == SDL_PRESSED)
  385. {
  386. switch(key.keysym.sym)
  387. {
  388. case SDLK_LEFT:
  389. fLcornerb();
  390. break;
  391. case SDLK_RIGHT:
  392. fRcornerb();
  393. break;
  394. case SDLK_UP:
  395. case SDLK_DOWN:
  396. {
  397. bool down = key.keysym.sym == SDLK_DOWN;
  398. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  399. int index = -1;
  400. while(schoolsOrder[++index] != selectedTab);
  401. index += (down ? 1 : -1);
  402. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  403. if(selectedTab != schoolsOrder[index])
  404. selectSchool(schoolsOrder[index]);
  405. break;
  406. }
  407. case SDLK_c:
  408. fbattleSpellsb();
  409. break;
  410. case SDLK_a:
  411. fadvSpellsb();
  412. break;
  413. default://to get rid of warnings
  414. break;
  415. }
  416. //alt + 1234567890-= casts spell from 1 - 12 slot
  417. if(myInt->altPressed())
  418. {
  419. SDL_Keycode hlpKey = key.keysym.sym;
  420. if(CGuiHandler::isNumKey(hlpKey, false))
  421. {
  422. if(hlpKey == SDLK_KP_PLUS)
  423. hlpKey = SDLK_EQUALS;
  424. else
  425. hlpKey = CGuiHandler::numToDigit(hlpKey);
  426. }
  427. static const SDL_Keycode spellSelectors[] = {SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5, SDLK_6, SDLK_7, SDLK_8, SDLK_9, SDLK_0, SDLK_MINUS, SDLK_EQUALS};
  428. int index = -1;
  429. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  430. if(index >= ARRAY_COUNT(spellSelectors))
  431. return;
  432. //try casting spell
  433. spellAreas[index]->clickLeft(false, true);
  434. }
  435. }
  436. }
  437. int CSpellWindow::pagesWithinCurrentTab()
  438. {
  439. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  440. }
  441. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  442. {
  443. this->pos = pos;
  444. this->owner = owner;
  445. addUsedEvents(LCLICK | RCLICK | HOVER);
  446. spellCost = whichSchool = schoolLevel = -1;
  447. mySpell = nullptr;
  448. schoolBorder = nullptr;
  449. OBJ_CONSTRUCTION_CAPTURING_ALL;
  450. image = new CAnimImage(owner->spells, 0, 0);
  451. image->visible = false;
  452. name = new CLabel(39, 70, FONT_TINY, CENTER);
  453. level = new CLabel(39, 82, FONT_TINY, CENTER);
  454. cost = new CLabel(39, 94, FONT_TINY, CENTER);
  455. for(auto l : {name, level, cost})
  456. l->autoRedraw = false;
  457. }
  458. CSpellWindow::SpellArea::~SpellArea()
  459. {
  460. }
  461. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  462. {
  463. if(mySpell && !down)
  464. {
  465. int spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  466. if(spellCost > owner->myHero->mana) //insufficient mana
  467. {
  468. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  469. return;
  470. }
  471. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  472. if((mySpell->isCombatSpell() && !owner->myInt->battleInt)
  473. || (mySpell->isAdventureSpell() && (owner->myInt->battleInt || owner->myInt->castleInt)))
  474. {
  475. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell->id, 0));
  476. owner->myInt->showInfoDialog(mySpell->getLevelInfo(schoolLevel).description, hlp);
  477. return;
  478. }
  479. //we will cast a spell
  480. if(mySpell->isCombatSpell() && owner->myInt->battleInt) //if battle window is open
  481. {
  482. ESpellCastProblem::ESpellCastProblem problem = mySpell->canBeCast(owner->myInt->cb.get(), ECastingMode::HERO_CASTING, owner->myHero);
  483. switch (problem)
  484. {
  485. case ESpellCastProblem::OK:
  486. {
  487. owner->myInt->battleInt->castThisSpell(mySpell->id);
  488. owner->fexitb();
  489. return;
  490. }
  491. break;
  492. case ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED:
  493. {
  494. std::string text = CGI->generaltexth->allTexts[538], elemental, caster;
  495. const PlayerColor player = owner->myInt->playerID;
  496. const TStacks stacks = owner->myInt->cb->battleGetStacksIf([player](const CStack * s)
  497. {
  498. return s->owner == player
  499. && vstd::contains(s->state, EBattleStackState::SUMMONED)
  500. && !s->isClone();
  501. });
  502. for(const CStack * s : stacks)
  503. {
  504. elemental = s->getCreature()->namePl;
  505. }
  506. if (owner->myHero->type->sex)
  507. { //female
  508. caster = CGI->generaltexth->allTexts[540];
  509. }
  510. else
  511. { //male
  512. caster = CGI->generaltexth->allTexts[539];
  513. }
  514. std::string summoner = owner->myHero->name;
  515. text = boost::str(boost::format(text) % summoner % elemental % caster);
  516. owner->myInt->showInfoDialog(text);
  517. }
  518. break;
  519. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  520. {
  521. //Recanter's Cloak or similar effect. Try to retrieve bonus
  522. const auto b = owner->myHero->getBonusLocalFirst(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
  523. //TODO what about other values and non-artifact sources?
  524. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  525. {
  526. std::string artName = CGI->arth->artifacts[b->sid]->Name();
  527. //The %s prevents %s from casting 3rd level or higher spells.
  528. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[536])
  529. % artName % owner->myHero->name));
  530. }
  531. else if(b && b->source == Bonus::TERRAIN_OVERLAY && b->sid == BFieldType::CURSED_GROUND)
  532. {
  533. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[537]);
  534. }
  535. else
  536. {
  537. // General message:
  538. // %s recites the incantations but they seem to have no effect.
  539. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  540. text = boost::str(boost::format(text) % caster);
  541. owner->myInt->showInfoDialog(text);
  542. }
  543. }
  544. break;
  545. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  546. {
  547. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[185]);
  548. }
  549. break;
  550. default:
  551. {
  552. // General message:
  553. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  554. text = boost::str(boost::format(text) % caster);
  555. owner->myInt->showInfoDialog(text);
  556. }
  557. }
  558. }
  559. else if(mySpell->isAdventureSpell() && !owner->myInt->battleInt) //adventure spell and not in battle
  560. {
  561. const CGHeroInstance *h = owner->myHero;
  562. GH.popInt(owner);
  563. auto guard = vstd::makeScopeGuard([this]
  564. {
  565. owner->myInt->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  566. owner->myInt->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  567. });
  568. if(mySpell->getTargetType() == CSpell::LOCATION)
  569. adventureInt->enterCastingMode(mySpell);
  570. else if(mySpell->getTargetType() == CSpell::NO_TARGET)
  571. owner->myInt->cb->castSpell(h, mySpell->id);
  572. else
  573. logGlobal->error("Invalid spell target type");
  574. }
  575. }
  576. }
  577. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  578. {
  579. if(mySpell && down)
  580. {
  581. std::string dmgInfo;
  582. int causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  583. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  584. dmgInfo = "";
  585. else
  586. {
  587. dmgInfo = CGI->generaltexth->allTexts[343];
  588. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  589. }
  590. CRClickPopup::createAndPush(mySpell->getLevelInfo(schoolLevel).description + dmgInfo,
  591. new CComponent(CComponent::spell, mySpell->id));
  592. }
  593. }
  594. void CSpellWindow::SpellArea::hover(bool on)
  595. {
  596. if(mySpell)
  597. {
  598. if(on)
  599. owner->statusBar->setText(boost::to_string(boost::format("%s (%s)") % mySpell->name % CGI->generaltexth->allTexts[171+mySpell->level]));
  600. else
  601. owner->statusBar->clear();
  602. }
  603. }
  604. void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
  605. {
  606. if(mySpell)
  607. {
  608. //printing border (indicates level of magic school)
  609. if(schoolBorder)
  610. schoolBorder->draw(to, pos.x, pos.y);
  611. CIntObject::showAll(to);
  612. }
  613. }
  614. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  615. {
  616. schoolBorder = nullptr;
  617. image->visible = false;
  618. name->setText("");
  619. level->setText("");
  620. cost->setText("");
  621. mySpell = spell;
  622. if(mySpell)
  623. {
  624. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  625. spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  626. image->setFrame(mySpell->id);
  627. image->visible = true;
  628. schoolBorder = owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->getImage(schoolLevel,0);
  629. SDL_Color firstLineColor, secondLineColor;
  630. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  631. {
  632. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  633. firstLineColor = Colors::WHITE;
  634. secondLineColor = unavailableSpell;
  635. }
  636. else
  637. {
  638. firstLineColor = Colors::YELLOW;
  639. secondLineColor = Colors::WHITE;
  640. }
  641. name->color = firstLineColor;
  642. name->setText(mySpell->name);
  643. level->color = secondLineColor;
  644. if(schoolLevel > 0)
  645. {
  646. boost::format fmt("%s/%s");
  647. fmt % CGI->generaltexth->allTexts[171 + mySpell->level];
  648. fmt % CGI->generaltexth->levels.at(3+(schoolLevel-1));//lines 4-6
  649. level->setText(fmt.str());
  650. }
  651. else
  652. level->setText(CGI->generaltexth->allTexts[171 + mySpell->level]);
  653. cost->color = secondLineColor;
  654. boost::format costfmt("%s: %d");
  655. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  656. cost->setText(costfmt.str());
  657. }
  658. }