CSpellWindow.cpp 21 KB

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