CSpellWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 "GUIClasses.h"
  14. #include "InfoWindows.h"
  15. #include "CCastleInterface.h"
  16. #include "../CGameInfo.h"
  17. #include "../CMT.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CVideoHandler.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../widgets/MiscWidgets.h"
  23. #include "../widgets/CComponent.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../adventureMap/CAdvMapInt.h"
  26. #include "../render/CAnimation.h"
  27. #include "../../CCallback.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/spells/CSpellHandler.h"
  31. #include "../../lib/spells/Problem.h"
  32. #include "../../lib/GameConstants.h"
  33. #include "../../lib/mapObjects/CGHeroInstance.h"
  34. #include <SDL_events.h>
  35. CSpellWindow::InteractiveArea::InteractiveArea(const Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  36. {
  37. addUsedEvents(LCLICK | RCLICK | HOVER);
  38. pos = myRect;
  39. onLeft = funcL;
  40. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  41. helpText = CGI->generaltexth->zelp[helpTextId].second;
  42. owner = _owner;
  43. }
  44. void CSpellWindow::InteractiveArea::clickLeft(tribool down, bool previousState)
  45. {
  46. if(!down)
  47. onLeft();
  48. }
  49. void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState)
  50. {
  51. adventureInt->handleRightClick(helpText, down);
  52. }
  53. void CSpellWindow::InteractiveArea::hover(bool on)
  54. {
  55. if(on)
  56. owner->statusBar->write(hoverText);
  57. else
  58. owner->statusBar->clear();
  59. }
  60. class SpellbookSpellSorter
  61. {
  62. public:
  63. bool operator()(const CSpell * A, const CSpell * B)
  64. {
  65. if(A->level < B->level)
  66. return true;
  67. if(A->level > B->level)
  68. return false;
  69. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  70. {
  71. if(A->school.at((ESpellSchool)schoolId) && !B->school.at((ESpellSchool)schoolId))
  72. return true;
  73. if(!A->school.at((ESpellSchool)schoolId) && B->school.at((ESpellSchool)schoolId))
  74. return false;
  75. }
  76. return A->getNameTranslated() < B->getNameTranslated();
  77. }
  78. } spellsorter;
  79. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  80. CWindowObject(PLAYER_COLORED, "SpelBack"),
  81. battleSpellsOnly(openOnBattleSpells),
  82. selectedTab(4),
  83. currentPage(0),
  84. myHero(_myHero),
  85. myInt(_myInt)
  86. {
  87. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  88. //initializing castable spells
  89. mySpells.reserve(CGI->spellh->objects.size());
  90. for(const CSpell * spell : CGI->spellh->objects)
  91. {
  92. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  93. mySpells.push_back(spell);
  94. }
  95. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  96. //initializing sizes of spellbook's parts
  97. for(auto & elem : sitesPerTabAdv)
  98. elem = 0;
  99. for(auto & elem : sitesPerTabBattle)
  100. elem = 0;
  101. for(const auto spell : mySpells)
  102. {
  103. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  104. ++sitesPerOurTab[4];
  105. spell->forEachSchool([&sitesPerOurTab](const spells::SchoolInfo & school, bool & stop)
  106. {
  107. ++sitesPerOurTab[(ui8)school.id];
  108. });
  109. }
  110. if(sitesPerTabAdv[4] % 12 == 0)
  111. sitesPerTabAdv[4]/=12;
  112. else
  113. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  114. for(int v=0; v<4; ++v)
  115. {
  116. if(sitesPerTabAdv[v] <= 10)
  117. sitesPerTabAdv[v] = 1;
  118. else
  119. {
  120. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  121. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  122. else
  123. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  124. }
  125. }
  126. if(sitesPerTabBattle[4] % 12 == 0)
  127. sitesPerTabBattle[4]/=12;
  128. else
  129. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  130. for(int v=0; v<4; ++v)
  131. {
  132. if(sitesPerTabBattle[v] <= 10)
  133. sitesPerTabBattle[v] = 1;
  134. else
  135. {
  136. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  137. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  138. else
  139. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  140. }
  141. }
  142. //numbers of spell pages computed
  143. leftCorner = std::make_shared<CPicture>("SpelTrnL.bmp", 97, 77);
  144. rightCorner = std::make_shared<CPicture>("SpelTrnR.bmp", 487, 72);
  145. spellIcons = std::make_shared<CAnimation>("Spells");
  146. schoolTab = std::make_shared<CAnimImage>("SpelTab", selectedTab, 0, 524, 88);
  147. schoolPicture = std::make_shared<CAnimImage>("Schools", 0, 0, 117, 74);
  148. schoolBorders[0] = std::make_shared<CAnimation>("SplevA.def");
  149. schoolBorders[1] = std::make_shared<CAnimation>("SplevF.def");
  150. schoolBorders[2] = std::make_shared<CAnimation>("SplevW.def");
  151. schoolBorders[3] = std::make_shared<CAnimation>("SplevE.def");
  152. for(auto item : schoolBorders)
  153. item->preload();
  154. mana = std::make_shared<CLabel>(435, 426, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, boost::lexical_cast<std::string>(myHero->mana));
  155. statusBar = CGStatusBar::create(7, 569, "Spelroll.bmp");
  156. Rect temp_rect = CSDL_Ext::genRect(45, 35, 479 + pos.x, 405 + pos.y);
  157. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fexitb, this), 460, this));
  158. temp_rect = CSDL_Ext::genRect(45, 35, 221 + pos.x, 405 + pos.y);
  159. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this));
  160. temp_rect = CSDL_Ext::genRect(45, 35, 355 + pos.x, 405 + pos.y);
  161. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fadvSpellsb, this), 452, this));
  162. temp_rect = CSDL_Ext::genRect(45, 35, 418 + pos.x, 405 + pos.y);
  163. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fmanaPtsb, this), 459, this));
  164. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 94 + pos.y);
  165. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  166. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 151 + pos.y);
  167. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  168. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 210 + pos.y);
  169. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  170. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 270 + pos.y);
  171. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  172. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 330 + pos.y);
  173. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  174. temp_rect = CSDL_Ext::genRect(leftCorner->pos.h, leftCorner->pos.w, 97 + pos.x, 77 + pos.y);
  175. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), 450, this));
  176. temp_rect = CSDL_Ext::genRect(rightCorner->pos.h, rightCorner->pos.w, 487 + pos.x, 72 + pos.y);
  177. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fRcornerb, this), 451, this));
  178. //areas for spells
  179. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  180. for(int v=0; v<12; ++v)
  181. {
  182. temp_rect = CSDL_Ext::genRect(65, 78, xpos, ypos);
  183. spellAreas[v] = std::make_shared<SpellArea>(temp_rect, this);
  184. if(v == 5) //to right page
  185. {
  186. xpos = 336 + pos.x; ypos = 90 + pos.y;
  187. }
  188. else
  189. {
  190. if(v%2 == 0)
  191. {
  192. xpos+=85;
  193. }
  194. else
  195. {
  196. xpos -= 85; ypos+=97;
  197. }
  198. }
  199. }
  200. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  201. schoolTab->setFrame(selectedTab, 0);
  202. int cp = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  203. // spellbook last page battle index is not reset after battle, so this needs to stay here
  204. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  205. setCurrentPage(cp);
  206. computeSpellsPerArea();
  207. addUsedEvents(KEYBOARD);
  208. }
  209. CSpellWindow::~CSpellWindow()
  210. {
  211. }
  212. void CSpellWindow::fexitb()
  213. {
  214. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  215. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  216. close();
  217. }
  218. void CSpellWindow::fadvSpellsb()
  219. {
  220. if(battleSpellsOnly == true)
  221. {
  222. turnPageRight();
  223. battleSpellsOnly = false;
  224. setCurrentPage(0);
  225. }
  226. computeSpellsPerArea();
  227. }
  228. void CSpellWindow::fbattleSpellsb()
  229. {
  230. if(battleSpellsOnly == false)
  231. {
  232. turnPageLeft();
  233. battleSpellsOnly = true;
  234. setCurrentPage(0);
  235. }
  236. computeSpellsPerArea();
  237. }
  238. void CSpellWindow::fmanaPtsb()
  239. {
  240. }
  241. void CSpellWindow::selectSchool(int school)
  242. {
  243. if(selectedTab != school)
  244. {
  245. if(selectedTab < school)
  246. turnPageLeft();
  247. else
  248. turnPageRight();
  249. selectedTab = school;
  250. schoolTab->setFrame(selectedTab, 0);
  251. setCurrentPage(0);
  252. }
  253. computeSpellsPerArea();
  254. }
  255. void CSpellWindow::fLcornerb()
  256. {
  257. if(currentPage>0)
  258. {
  259. turnPageLeft();
  260. setCurrentPage(currentPage - 1);
  261. }
  262. computeSpellsPerArea();
  263. GH.breakEventHandling();
  264. }
  265. void CSpellWindow::fRcornerb()
  266. {
  267. if((currentPage + 1) < (pagesWithinCurrentTab()))
  268. {
  269. turnPageRight();
  270. setCurrentPage(currentPage + 1);
  271. }
  272. computeSpellsPerArea();
  273. GH.breakEventHandling();
  274. }
  275. void CSpellWindow::show(SDL_Surface * to)
  276. {
  277. statusBar->show(to);
  278. }
  279. void CSpellWindow::computeSpellsPerArea()
  280. {
  281. std::vector<const CSpell *> spellsCurSite;
  282. spellsCurSite.reserve(mySpells.size());
  283. for(const CSpell * spell : mySpells)
  284. {
  285. if(spell->isCombat() ^ !battleSpellsOnly
  286. && ((selectedTab == 4) || spell->school.at((ESpellSchool)selectedTab))
  287. )
  288. {
  289. spellsCurSite.push_back(spell);
  290. }
  291. }
  292. if(selectedTab == 4)
  293. {
  294. if(spellsCurSite.size() > 12)
  295. {
  296. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  297. if(spellsCurSite.size() > 12)
  298. {
  299. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  300. }
  301. }
  302. }
  303. else //selectedTab == 0, 1, 2 or 3
  304. {
  305. if(spellsCurSite.size() > 10)
  306. {
  307. if(currentPage == 0)
  308. {
  309. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  310. }
  311. else
  312. {
  313. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  314. if(spellsCurSite.size() > 12)
  315. {
  316. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  317. }
  318. }
  319. }
  320. }
  321. //applying
  322. if(selectedTab == 4 || currentPage != 0)
  323. {
  324. for(size_t c=0; c<12; ++c)
  325. {
  326. if(c < spellsCurSite.size())
  327. {
  328. spellAreas[c]->setSpell(spellsCurSite[c]);
  329. }
  330. else
  331. {
  332. spellAreas[c]->setSpell(nullptr);
  333. }
  334. }
  335. }
  336. else
  337. {
  338. spellAreas[0]->setSpell(nullptr);
  339. spellAreas[1]->setSpell(nullptr);
  340. for(size_t c=0; c<10; ++c)
  341. {
  342. if(c < spellsCurSite.size())
  343. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  344. else
  345. spellAreas[c+2]->setSpell(nullptr);
  346. }
  347. }
  348. redraw();
  349. }
  350. void CSpellWindow::setCurrentPage(int value)
  351. {
  352. currentPage = value;
  353. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  354. if(selectedTab != 4)
  355. schoolPicture->setFrame(selectedTab, 0);
  356. leftCorner->visible = currentPage != 0;
  357. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  358. mana->setText(boost::lexical_cast<std::string>(myHero->mana));//just in case, it will be possible to cast spell without closing book
  359. }
  360. void CSpellWindow::turnPageLeft()
  361. {
  362. if(settings["video"]["spellbookAnimation"].Bool())
  363. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15);
  364. }
  365. void CSpellWindow::turnPageRight()
  366. {
  367. if(settings["video"]["spellbookAnimation"].Bool())
  368. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15);
  369. }
  370. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  371. {
  372. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  373. {
  374. fexitb();
  375. return;
  376. }
  377. if(key.state == SDL_PRESSED)
  378. {
  379. switch(key.keysym.sym)
  380. {
  381. case SDLK_LEFT:
  382. fLcornerb();
  383. break;
  384. case SDLK_RIGHT:
  385. fRcornerb();
  386. break;
  387. case SDLK_UP:
  388. case SDLK_DOWN:
  389. {
  390. bool down = key.keysym.sym == SDLK_DOWN;
  391. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  392. int index = -1;
  393. while(schoolsOrder[++index] != selectedTab);
  394. index += (down ? 1 : -1);
  395. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  396. if(selectedTab != schoolsOrder[index])
  397. selectSchool(schoolsOrder[index]);
  398. break;
  399. }
  400. case SDLK_c:
  401. fbattleSpellsb();
  402. break;
  403. case SDLK_a:
  404. fadvSpellsb();
  405. break;
  406. default://to get rid of warnings
  407. break;
  408. }
  409. //alt + 1234567890-= casts spell from 1 - 12 slot
  410. if(GH.isKeyboardAltDown())
  411. {
  412. SDL_Keycode hlpKey = key.keysym.sym;
  413. if(CGuiHandler::isNumKey(hlpKey, false))
  414. {
  415. if(hlpKey == SDLK_KP_PLUS)
  416. hlpKey = SDLK_EQUALS;
  417. else
  418. hlpKey = CGuiHandler::numToDigit(hlpKey);
  419. }
  420. 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};
  421. int index = -1;
  422. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  423. if(index >= ARRAY_COUNT(spellSelectors))
  424. return;
  425. //try casting spell
  426. spellAreas[index]->clickLeft(false, true);
  427. }
  428. }
  429. }
  430. int CSpellWindow::pagesWithinCurrentTab()
  431. {
  432. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  433. }
  434. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  435. {
  436. this->pos = pos;
  437. this->owner = owner;
  438. addUsedEvents(LCLICK | RCLICK | HOVER);
  439. schoolLevel = -1;
  440. mySpell = nullptr;
  441. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  442. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  443. image->visible = false;
  444. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  445. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  446. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  447. for(auto l : {name, level, cost})
  448. l->setAutoRedraw(false);
  449. }
  450. CSpellWindow::SpellArea::~SpellArea() = default;
  451. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  452. {
  453. if(mySpell && !down)
  454. {
  455. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  456. if(spellCost > owner->myHero->mana) //insufficient mana
  457. {
  458. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  459. return;
  460. }
  461. //anything that is not combat spell is adventure spell
  462. //this not an error in general to cast even creature ability with hero
  463. const bool combatSpell = mySpell->isCombat();
  464. if(combatSpell == mySpell->isAdventure())
  465. {
  466. logGlobal->error("Spell have invalid flags");
  467. return;
  468. }
  469. const bool inCombat = owner->myInt->battleInt != nullptr;
  470. const bool inCastle = owner->myInt->castleInt != nullptr;
  471. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  472. if((combatSpell ^ inCombat) || inCastle)
  473. {
  474. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(CComponent::spell, mySpell->id, 0));
  475. owner->myInt->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  476. }
  477. else if(combatSpell)
  478. {
  479. spells::detail::ProblemImpl problem;
  480. if(mySpell->canBeCast(problem, owner->myInt->cb.get(), spells::Mode::HERO, owner->myHero))
  481. {
  482. owner->myInt->battleInt->castThisSpell(mySpell->id);
  483. owner->fexitb();
  484. }
  485. else
  486. {
  487. std::vector<std::string> texts;
  488. problem.getAll(texts);
  489. if(!texts.empty())
  490. owner->myInt->showInfoDialog(texts.front());
  491. else
  492. owner->myInt->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  493. }
  494. }
  495. else //adventure spell
  496. {
  497. const CGHeroInstance * h = owner->myHero;
  498. GH.popInts(1);
  499. auto guard = vstd::makeScopeGuard([this]()
  500. {
  501. owner->myInt->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  502. owner->myInt->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  503. });
  504. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  505. adventureInt->enterCastingMode(mySpell);
  506. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  507. owner->myInt->cb->castSpell(h, mySpell->id);
  508. else
  509. logGlobal->error("Invalid spell target type");
  510. }
  511. }
  512. }
  513. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  514. {
  515. if(mySpell && down)
  516. {
  517. std::string dmgInfo;
  518. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  519. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  520. dmgInfo.clear();
  521. else
  522. {
  523. dmgInfo = CGI->generaltexth->allTexts[343];
  524. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  525. }
  526. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(CComponent::spell, mySpell->id));
  527. }
  528. }
  529. void CSpellWindow::SpellArea::hover(bool on)
  530. {
  531. if(mySpell)
  532. {
  533. if(on)
  534. owner->statusBar->write(boost::to_string(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->level]));
  535. else
  536. owner->statusBar->clear();
  537. }
  538. }
  539. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  540. {
  541. schoolBorder.reset();
  542. image->visible = false;
  543. name->setText("");
  544. level->setText("");
  545. cost->setText("");
  546. mySpell = spell;
  547. if(mySpell)
  548. {
  549. int32_t whichSchool = 0; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  550. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  551. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  552. image->setFrame(mySpell->id);
  553. image->visible = true;
  554. {
  555. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  556. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab], schoolLevel);
  557. }
  558. SDL_Color firstLineColor, secondLineColor;
  559. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  560. {
  561. firstLineColor = Colors::WHITE;
  562. secondLineColor = Colors::ORANGE;
  563. }
  564. else
  565. {
  566. firstLineColor = Colors::YELLOW;
  567. secondLineColor = Colors::WHITE;
  568. }
  569. name->color = firstLineColor;
  570. name->setText(mySpell->getNameTranslated());
  571. level->color = secondLineColor;
  572. if(schoolLevel > 0)
  573. {
  574. boost::format fmt("%s/%s");
  575. fmt % CGI->generaltexth->allTexts[171 + mySpell->level];
  576. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  577. level->setText(fmt.str());
  578. }
  579. else
  580. level->setText(CGI->generaltexth->allTexts[171 + mySpell->level]);
  581. cost->color = secondLineColor;
  582. boost::format costfmt("%s: %d");
  583. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  584. cost->setText(costfmt.str());
  585. }
  586. }