CSpellWindow.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. #include "StdInc.h"
  2. #include "CSpellWindow.h"
  3. #include "CAdvmapInterface.h"
  4. #include "GUIClasses.h"
  5. #include "../CBitmapHandler.h"
  6. #include "../CDefHandler.h"
  7. #include "../CGameInfo.h"
  8. #include "../CMessage.h"
  9. #include "../CMT.h"
  10. #include "../CPlayerInterface.h"
  11. #include "../CVideoHandler.h"
  12. #include "../Graphics.h"
  13. #include "../battle/CBattleInterface.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/SDL_Extensions.h"
  16. #include "../widgets/MiscWidgets.h"
  17. #include "../../CCallback.h"
  18. #include "../../lib/BattleState.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/CGeneralTextHandler.h"
  21. #include "../../lib/CHeroHandler.h"
  22. #include "../../lib/CSpellHandler.h"
  23. #include "../../lib/GameConstants.h"
  24. /*
  25. * CSpellWindow.cpp, part of VCMI engine
  26. *
  27. * Authors: listed in file AUTHORS in main folder
  28. *
  29. * License: GNU General Public License v2.0 or later
  30. * Full text of license available in license.txt file, in main folder
  31. *
  32. */
  33. SpellbookInteractiveArea::SpellbookInteractiveArea(const SDL_Rect & myRect, std::function<void()> funcL,
  34. const std::string & textR, std::function<void()> funcHon, std::function<void()> funcHoff, CPlayerInterface * _myInt)
  35. {
  36. addUsedEvents(LCLICK | RCLICK | HOVER);
  37. pos = myRect;
  38. onLeft = funcL;
  39. textOnRclick = textR;
  40. onHoverOn = funcHon;
  41. onHoverOff = funcHoff;
  42. myInt = _myInt;
  43. }
  44. void SpellbookInteractiveArea::clickLeft(tribool down, bool previousState)
  45. {
  46. if(!down)
  47. {
  48. onLeft();
  49. }
  50. }
  51. void SpellbookInteractiveArea::clickRight(tribool down, bool previousState)
  52. {
  53. adventureInt->handleRightClick(textOnRclick, down);
  54. }
  55. void SpellbookInteractiveArea::hover(bool on)
  56. {
  57. //Hoverable::hover(on);
  58. if(on)
  59. {
  60. onHoverOn();
  61. }
  62. else
  63. {
  64. onHoverOff();
  65. }
  66. }
  67. CSpellWindow::CSpellWindow(const SDL_Rect &, const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  68. CWindowObject(PLAYER_COLORED, "SpelBack"),
  69. battleSpellsOnly(openOnBattleSpells),
  70. selectedTab(4),
  71. currentPage(0),
  72. myHero(_myHero),
  73. myInt(_myInt)
  74. {
  75. //initializing castable spells
  76. for(ui32 v=0; v<CGI->spellh->objects.size(); ++v)
  77. {
  78. if( !CGI->spellh->objects[v]->creatureAbility && myHero->canCastThisSpell(CGI->spellh->objects[v]) )
  79. mySpells.insert(SpellID(v));
  80. }
  81. //initializing sizes of spellbook's parts
  82. for(auto & elem : sitesPerTabAdv)
  83. elem = 0;
  84. for(auto & elem : sitesPerTabBattle)
  85. elem = 0;
  86. for(auto g : mySpells)
  87. {
  88. const CSpell &s = *CGI->spellh->objects[g];
  89. Uint8 *sitesPerOurTab = s.combatSpell ? sitesPerTabBattle : sitesPerTabAdv;
  90. ++sitesPerOurTab[4];
  91. if(s.air)
  92. ++sitesPerOurTab[0];
  93. if(s.fire)
  94. ++sitesPerOurTab[1];
  95. if(s.water)
  96. ++sitesPerOurTab[2];
  97. if(s.earth)
  98. ++sitesPerOurTab[3];
  99. }
  100. if(sitesPerTabAdv[4] % 12 == 0)
  101. sitesPerTabAdv[4]/=12;
  102. else
  103. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  104. for(int v=0; v<4; ++v)
  105. {
  106. if(sitesPerTabAdv[v] <= 10)
  107. sitesPerTabAdv[v] = 1;
  108. else
  109. {
  110. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  111. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  112. else
  113. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  114. }
  115. }
  116. if(sitesPerTabBattle[4] % 12 == 0)
  117. sitesPerTabBattle[4]/=12;
  118. else
  119. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  120. for(int v=0; v<4; ++v)
  121. {
  122. if(sitesPerTabBattle[v] <= 10)
  123. sitesPerTabBattle[v] = 1;
  124. else
  125. {
  126. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  127. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  128. else
  129. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  130. }
  131. }
  132. //numbers of spell pages computed
  133. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  134. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  135. spells = new CAnimation("Spells.def");
  136. spellTab = CDefHandler::giveDef("SpelTab.def");
  137. schools = CDefHandler::giveDef("Schools.def");
  138. schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
  139. schoolBorders[1] = CDefHandler::giveDef("SplevF.def");
  140. schoolBorders[2] = CDefHandler::giveDef("SplevW.def");
  141. schoolBorders[3] = CDefHandler::giveDef("SplevE.def");
  142. statusBar = new CGStatusBar(7 + pos.x, 569 + pos.y, "Spelroll.bmp");
  143. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  144. exitBtn = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fexitb, this), CGI->generaltexth->zelp[460].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[460].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  145. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  146. battleSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fbattleSpellsb, this), CGI->generaltexth->zelp[453].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[453].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  147. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  148. adventureSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fadvSpellsb, this), CGI->generaltexth->zelp[452].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[452].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  149. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  150. manaPoints = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fmanaPtsb, this), CGI->generaltexth->zelp[459].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[459].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  151. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  152. selectSpellsA = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 0), CGI->generaltexth->zelp[454].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[454].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  153. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  154. selectSpellsE = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 3), CGI->generaltexth->zelp[457].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[457].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  155. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  156. selectSpellsF = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 1), CGI->generaltexth->zelp[455].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[455].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  157. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  158. selectSpellsW = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 2), CGI->generaltexth->zelp[456].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[456].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  159. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  160. selectSpellsAll = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 4), CGI->generaltexth->zelp[458].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[458].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  161. temp_rect = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y);
  162. lCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fLcornerb, this), CGI->generaltexth->zelp[450].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[450].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  163. temp_rect = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y);
  164. rCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fRcornerb, this), CGI->generaltexth->zelp[451].second, boost::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[451].first)), boost::bind(&CGStatusBar::clear, statusBar), myInt);
  165. //areas for spells
  166. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  167. for(int v=0; v<12; ++v)
  168. {
  169. temp_rect = genRect(65, 78, xpos, ypos);
  170. spellAreas[v] = new SpellArea(temp_rect, this);
  171. if(v == 5) //to right page
  172. {
  173. xpos = 336 + pos.x; ypos = 90 + pos.y;
  174. }
  175. else
  176. {
  177. if(v%2 == 0)
  178. {
  179. xpos+=85;
  180. }
  181. else
  182. {
  183. xpos -= 85; ypos+=97;
  184. }
  185. }
  186. }
  187. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  188. currentPage = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  189. // spellbook last page battle index is not reset after battle, so this needs to stay here
  190. vstd::abetween(currentPage, 0, pagesWithinCurrentTab() - 1);
  191. computeSpellsPerArea();
  192. addUsedEvents(KEYBOARD);
  193. }
  194. CSpellWindow::~CSpellWindow()
  195. {
  196. SDL_FreeSurface(leftCorner);
  197. SDL_FreeSurface(rightCorner);
  198. spells->unload();
  199. delete spells;
  200. delete spellTab;
  201. delete schools;
  202. for(auto & elem : schoolBorders)
  203. delete elem;
  204. delete exitBtn;
  205. delete battleSpells;
  206. delete adventureSpells;
  207. delete manaPoints;
  208. delete statusBar;
  209. delete selectSpellsA;
  210. delete selectSpellsE;
  211. delete selectSpellsF;
  212. delete selectSpellsW;
  213. delete selectSpellsAll;
  214. delete lCorner;
  215. delete rCorner;
  216. for(auto & elem : spellAreas)
  217. {
  218. delete elem;
  219. }
  220. }
  221. void CSpellWindow::fexitb()
  222. {
  223. (LOCPLINT->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  224. (LOCPLINT->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  225. GH.popIntTotally(this);
  226. }
  227. void CSpellWindow::fadvSpellsb()
  228. {
  229. if (battleSpellsOnly == true)
  230. {
  231. turnPageRight();
  232. battleSpellsOnly = false;
  233. currentPage = 0;
  234. }
  235. computeSpellsPerArea();
  236. }
  237. void CSpellWindow::fbattleSpellsb()
  238. {
  239. if (battleSpellsOnly == false)
  240. {
  241. turnPageLeft();
  242. battleSpellsOnly = true;
  243. currentPage = 0;
  244. }
  245. computeSpellsPerArea();
  246. }
  247. void CSpellWindow::fmanaPtsb()
  248. {
  249. }
  250. void CSpellWindow::selectSchool(int school)
  251. {
  252. if (selectedTab != school)
  253. {
  254. if (selectedTab < school)
  255. turnPageLeft();
  256. else
  257. turnPageRight();
  258. selectedTab = school;
  259. currentPage = 0;
  260. }
  261. computeSpellsPerArea();
  262. }
  263. void CSpellWindow::fLcornerb()
  264. {
  265. if(currentPage>0)
  266. {
  267. turnPageLeft();
  268. --currentPage;
  269. }
  270. computeSpellsPerArea();
  271. GH.breakEventHandling();
  272. }
  273. void CSpellWindow::fRcornerb()
  274. {
  275. if((currentPage + 1) < (pagesWithinCurrentTab()))
  276. {
  277. turnPageRight();
  278. ++currentPage;
  279. }
  280. computeSpellsPerArea();
  281. GH.breakEventHandling();
  282. }
  283. void CSpellWindow::showAll(SDL_Surface * to)
  284. {
  285. CWindowObject::showAll(to);
  286. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  287. std::ostringstream mana;
  288. mana<<myHero->mana;
  289. printAtMiddleLoc(mana.str(), 435, 426, FONT_SMALL, Colors::YELLOW, to);
  290. statusBar->showAll(to);
  291. //printing school images
  292. if(selectedTab!=4 && currentPage == 0)
  293. {
  294. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  295. }
  296. //printing corners
  297. if(currentPage!=0)
  298. {
  299. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  300. }
  301. if((currentPage+1) < (pagesWithinCurrentTab()) )
  302. {
  303. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  304. }
  305. //printing spell info
  306. for(auto & elem : spellAreas)
  307. {
  308. elem->showAll(to);
  309. }
  310. }
  311. void CSpellWindow::show(SDL_Surface * to)
  312. {
  313. statusBar->show(to);
  314. }
  315. class SpellbookSpellSorter
  316. {
  317. public:
  318. bool operator()(const int & a, const int & b)
  319. {
  320. const CSpell & A = *CGI->spellh->objects[a];
  321. const CSpell & B = *CGI->spellh->objects[b];
  322. if(A.level<B.level)
  323. return true;
  324. if(A.level>B.level)
  325. return false;
  326. if(A.air && !B.air)
  327. return true;
  328. if(!A.air && B.air)
  329. return false;
  330. if(A.fire && !B.fire)
  331. return true;
  332. if(!A.fire && B.fire)
  333. return false;
  334. if(A.water && !B.water)
  335. return true;
  336. if(!A.water && B.water)
  337. return false;
  338. if(A.earth && !B.earth)
  339. return true;
  340. if(!A.earth && B.earth)
  341. return false;
  342. return A.name < B.name;
  343. }
  344. } spellsorter;
  345. void CSpellWindow::computeSpellsPerArea()
  346. {
  347. std::vector<SpellID> spellsCurSite;
  348. for(auto it = mySpells.cbegin(); it != mySpells.cend(); ++it)
  349. {
  350. if(CGI->spellh->objects[*it]->combatSpell ^ !battleSpellsOnly
  351. && ((CGI->spellh->objects[*it]->air && selectedTab == 0) ||
  352. (CGI->spellh->objects[*it]->fire && selectedTab == 1) ||
  353. (CGI->spellh->objects[*it]->water && selectedTab == 2) ||
  354. (CGI->spellh->objects[*it]->earth && selectedTab == 3) ||
  355. selectedTab == 4 )
  356. )
  357. {
  358. spellsCurSite.push_back(*it);
  359. }
  360. }
  361. std::sort(spellsCurSite.begin(), spellsCurSite.end(), spellsorter);
  362. if(selectedTab == 4)
  363. {
  364. if(spellsCurSite.size() > 12)
  365. {
  366. spellsCurSite = std::vector<SpellID>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  367. if(spellsCurSite.size() > 12)
  368. {
  369. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  370. }
  371. }
  372. }
  373. else //selectedTab == 0, 1, 2 or 3
  374. {
  375. if(spellsCurSite.size() > 10)
  376. {
  377. if(currentPage == 0)
  378. {
  379. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  380. }
  381. else
  382. {
  383. spellsCurSite = std::vector<SpellID>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  384. if(spellsCurSite.size() > 12)
  385. {
  386. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  387. }
  388. }
  389. }
  390. }
  391. //applying
  392. if(selectedTab == 4 || currentPage != 0)
  393. {
  394. for(size_t c=0; c<12; ++c)
  395. {
  396. if(c<spellsCurSite.size())
  397. {
  398. spellAreas[c]->setSpell(spellsCurSite[c]);
  399. }
  400. else
  401. {
  402. spellAreas[c]->setSpell(SpellID::NONE);
  403. }
  404. }
  405. }
  406. else
  407. {
  408. spellAreas[0]->setSpell(SpellID::NONE);
  409. spellAreas[1]->setSpell(SpellID::NONE);
  410. for(size_t c=0; c<10; ++c)
  411. {
  412. if(c<spellsCurSite.size())
  413. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  414. else
  415. spellAreas[c+2]->setSpell(SpellID::NONE);
  416. }
  417. }
  418. redraw();
  419. }
  420. void CSpellWindow::activate()
  421. {
  422. CIntObject::activate();
  423. exitBtn->activate();
  424. battleSpells->activate();
  425. adventureSpells->activate();
  426. manaPoints->activate();
  427. selectSpellsA->activate();
  428. selectSpellsE->activate();
  429. selectSpellsF->activate();
  430. selectSpellsW->activate();
  431. selectSpellsAll->activate();
  432. for(auto & elem : spellAreas)
  433. {
  434. elem->activate();
  435. }
  436. lCorner->activate();
  437. rCorner->activate();
  438. }
  439. void CSpellWindow::deactivate()
  440. {
  441. CIntObject::deactivate();
  442. exitBtn->deactivate();
  443. battleSpells->deactivate();
  444. adventureSpells->deactivate();
  445. manaPoints->deactivate();
  446. selectSpellsA->deactivate();
  447. selectSpellsE->deactivate();
  448. selectSpellsF->deactivate();
  449. selectSpellsW->deactivate();
  450. selectSpellsAll->deactivate();
  451. for(auto & elem : spellAreas)
  452. {
  453. elem->deactivate();
  454. }
  455. lCorner->deactivate();
  456. rCorner->deactivate();
  457. }
  458. void CSpellWindow::turnPageLeft()
  459. {
  460. if (settings["video"]["spellbookAnimation"].Bool())
  461. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  462. }
  463. void CSpellWindow::turnPageRight()
  464. {
  465. if (settings["video"]["spellbookAnimation"].Bool())
  466. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  467. }
  468. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  469. {
  470. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  471. fexitb();
  472. if(key.state == SDL_PRESSED)
  473. {
  474. switch(key.keysym.sym)
  475. {
  476. case SDLK_LEFT:
  477. fLcornerb();
  478. break;
  479. case SDLK_RIGHT:
  480. fRcornerb();
  481. break;
  482. case SDLK_UP:
  483. case SDLK_DOWN:
  484. {
  485. bool down = key.keysym.sym == SDLK_DOWN;
  486. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  487. int index = -1;
  488. while(schoolsOrder[++index] != selectedTab);
  489. index += (down ? 1 : -1);
  490. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  491. if(selectedTab != schoolsOrder[index])
  492. selectSchool(schoolsOrder[index]);
  493. break;
  494. }
  495. case SDLK_c:
  496. fbattleSpellsb();
  497. break;
  498. case SDLK_a:
  499. fadvSpellsb();
  500. break;
  501. default://to get rid of warnings
  502. break;
  503. }
  504. //alt + 1234567890-= casts spell from 1 - 12 slot
  505. if(LOCPLINT->altPressed())
  506. {
  507. SDLKey hlpKey = key.keysym.sym;
  508. if(CGuiHandler::isNumKey(hlpKey, false))
  509. {
  510. if(hlpKey == SDLK_KP_PLUS)
  511. hlpKey = SDLK_EQUALS;
  512. else
  513. hlpKey = CGuiHandler::numToDigit(hlpKey);
  514. }
  515. static const SDLKey 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};
  516. int index = -1;
  517. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  518. if(index >= ARRAY_COUNT(spellSelectors))
  519. return;
  520. //try casting spell
  521. spellAreas[index]->clickLeft(false, true);
  522. }
  523. }
  524. }
  525. Uint8 CSpellWindow::pagesWithinCurrentTab()
  526. {
  527. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  528. }
  529. void CSpellWindow::teleportTo( int town, const CGHeroInstance * hero )
  530. {
  531. const CGTownInstance * dest = LOCPLINT->cb->getTown(ObjectInstanceID(town));
  532. LOCPLINT->cb->castSpell(hero, SpellID::TOWN_PORTAL, dest->visitablePos());
  533. }
  534. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  535. {
  536. this->pos = pos;
  537. this->owner = owner;
  538. addUsedEvents(LCLICK | RCLICK | HOVER);
  539. spellCost = whichSchool = schoolLevel = -1;
  540. mySpell = SpellID::NONE;
  541. }
  542. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  543. {
  544. if(!down && mySpell!=-1)
  545. {
  546. const CSpell *sp = CGI->spellh->objects[mySpell];
  547. int spellCost = owner->myInt->cb->getSpellCost(sp, owner->myHero);
  548. if(spellCost > owner->myHero->mana) //insufficient mana
  549. {
  550. char msgBuf[500];
  551. sprintf(msgBuf, CGI->generaltexth->allTexts[206].c_str(), spellCost, owner->myHero->mana);
  552. owner->myInt->showInfoDialog(std::string(msgBuf));
  553. return;
  554. }
  555. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  556. if((sp->combatSpell && !owner->myInt->battleInt)
  557. || (!sp->combatSpell && owner->myInt->battleInt))
  558. {
  559. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell, 0));
  560. LOCPLINT->showInfoDialog(sp->getLevelInfo(schoolLevel).description, hlp);
  561. return;
  562. }
  563. //we will cast a spell
  564. if(sp->combatSpell && owner->myInt->battleInt && owner->myInt->cb->battleCanCastSpell()) //if battle window is open
  565. {
  566. ESpellCastProblem::ESpellCastProblem problem = owner->myInt->cb->battleCanCastThisSpell(sp);
  567. switch (problem)
  568. {
  569. case ESpellCastProblem::OK:
  570. {
  571. int spell = mySpell;
  572. owner->fexitb();
  573. owner->myInt->battleInt->castThisSpell(spell);
  574. }
  575. break;
  576. case ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED:
  577. {
  578. std::string text = CGI->generaltexth->allTexts[538], summoner, elemental, caster;
  579. std::vector<const CStack *> stacks = owner->myInt->cb->battleGetStacks();
  580. for(const CStack * s : stacks)
  581. {
  582. if(vstd::contains(s->state, EBattleStackState::SUMMONED))
  583. {
  584. elemental = s->getCreature()->namePl;
  585. summoner = owner->myInt->cb->battleGetHeroInfo(!s->attackerOwned).name;
  586. break;
  587. }
  588. }
  589. if (owner->myHero->type->sex)
  590. { //female
  591. caster = CGI->generaltexth->allTexts[540];
  592. }
  593. else
  594. { //male
  595. caster = CGI->generaltexth->allTexts[539];
  596. }
  597. text = boost::str(boost::format(text) % summoner % elemental % caster);
  598. owner->myInt->showInfoDialog(text);
  599. }
  600. break;
  601. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  602. {
  603. //Recanter's Cloak or similar effect. Try to retrieve bonus
  604. const Bonus *b = owner->myHero->getBonusLocalFirst(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
  605. //TODO what about other values and non-artifact sources?
  606. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  607. {
  608. std::string artName = CGI->arth->artifacts[b->sid]->Name();
  609. //The %s prevents %s from casting 3rd level or higher spells.
  610. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[536])
  611. % artName % owner->myHero->name));
  612. }
  613. else
  614. {
  615. // General message:
  616. // %s recites the incantations but they seem to have no effect.
  617. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  618. text = boost::str(boost::format(text) % caster);
  619. owner->myInt->showInfoDialog(text);
  620. }
  621. }
  622. break;
  623. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  624. {
  625. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[185]);
  626. }
  627. break;
  628. }
  629. }
  630. else if(!sp->combatSpell && !owner->myInt->battleInt) //adventure spell
  631. {
  632. SpellID spell = mySpell;
  633. const CGHeroInstance *h = owner->myHero;
  634. owner->fexitb();
  635. switch(spell)
  636. {
  637. case SpellID::SUMMON_BOAT:
  638. {
  639. int3 pos = h->bestLocation();
  640. if(pos.x < 0)
  641. {
  642. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[334]); //There is no place to put the boat.
  643. return;
  644. }
  645. }
  646. break;
  647. case SpellID::SCUTTLE_BOAT:
  648. case SpellID::DIMENSION_DOOR:
  649. adventureInt->enterCastingMode(sp);
  650. return;
  651. case SpellID::VISIONS:
  652. case SpellID::VIEW_EARTH:
  653. case SpellID::DISGUISE:
  654. case SpellID::VIEW_AIR:
  655. case SpellID::FLY:
  656. case SpellID::WATER_WALK:
  657. break;
  658. case SpellID::TOWN_PORTAL:
  659. {
  660. std::vector <int> availableTowns;
  661. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
  662. if (Towns.empty())
  663. {
  664. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  665. return;
  666. }
  667. if (h->getSpellSchoolLevel(CGI->spellh->objects[spell]) < 2) //not advanced or expert - teleport to nearest available city
  668. {
  669. auto nearest = Towns.cbegin(); //nearest town's iterator
  670. si32 dist = LOCPLINT->cb->getTown((*nearest)->id)->pos.dist2dSQ(h->pos);
  671. for (auto i = nearest + 1; i != Towns.cend(); ++i)
  672. {
  673. const CGTownInstance * dest = LOCPLINT->cb->getTown((*i)->id);
  674. si32 curDist = dest->pos.dist2dSQ(h->pos);
  675. if (curDist < dist)
  676. {
  677. nearest = i;
  678. dist = curDist;
  679. }
  680. }
  681. if ((*nearest)->visitingHero)
  682. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[123]);
  683. else
  684. {
  685. const CGTownInstance * town = LOCPLINT->cb->getTown((*nearest)->id);
  686. LOCPLINT->cb->castSpell(h, spell, town->visitablePos());// - town->getVisitableOffset());
  687. }
  688. }
  689. else
  690. { //let the player choose
  691. for(auto & Town : Towns)
  692. {
  693. const CGTownInstance *t = Town;
  694. if (t->visitingHero == nullptr) //empty town and this is
  695. {
  696. availableTowns.push_back(t->id.getNum());//add to the list
  697. }
  698. }
  699. if (availableTowns.empty())
  700. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  701. else
  702. GH.pushInt (new CObjectListWindow(availableTowns,
  703. new CAnimImage("SPELLSCR",spell),
  704. CGI->generaltexth->jktexts[40], CGI->generaltexth->jktexts[41],
  705. boost::bind (&CSpellWindow::teleportTo, owner, _1, h)));
  706. }
  707. return;
  708. }
  709. break;
  710. default:
  711. assert(0);
  712. }
  713. //can return earlier in some cases
  714. LOCPLINT->cb->castSpell(h, spell);
  715. }
  716. }
  717. }
  718. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  719. {
  720. if(down && mySpell != -1)
  721. {
  722. std::string dmgInfo;
  723. const CGHeroInstance * hero = owner->myHero;
  724. int causedDmg = owner->myInt->cb->estimateSpellDamage( CGI->spellh->objects[mySpell], (hero ? hero : nullptr));
  725. if(causedDmg == 0 || mySpell == 57) //Titan's Lightning Bolt already has damage info included
  726. dmgInfo = "";
  727. else
  728. {
  729. dmgInfo = CGI->generaltexth->allTexts[343];
  730. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  731. }
  732. CRClickPopup::createAndPush(CGI->spellh->objects[mySpell]->getLevelInfo(schoolLevel).description + dmgInfo,
  733. new CComponent(CComponent::spell, mySpell));
  734. }
  735. }
  736. void CSpellWindow::SpellArea::hover(bool on)
  737. {
  738. //Hoverable::hover(on);
  739. if(mySpell != -1)
  740. {
  741. if(on)
  742. {
  743. std::ostringstream ss;
  744. ss<<CGI->spellh->objects[mySpell]->name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->objects[mySpell]->level]<<")";
  745. owner->statusBar->setText(ss.str());
  746. }
  747. else
  748. {
  749. owner->statusBar->clear();
  750. }
  751. }
  752. }
  753. void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
  754. {
  755. if(mySpell < 0)
  756. return;
  757. const CSpell * spell = mySpell.toSpell();
  758. owner->spells->load(mySpell);
  759. IImage * icon = owner->spells->getImage(mySpell,0,false);
  760. if(icon != nullptr)
  761. icon->draw(to, pos.x, pos.y);
  762. else
  763. logGlobal->errorStream() << __FUNCTION__ << ": failed to load spell icon for spell with id " << mySpell;
  764. blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school)
  765. SDL_Color firstLineColor, secondLineColor;
  766. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  767. {
  768. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  769. firstLineColor = Colors::WHITE;
  770. secondLineColor = unavailableSpell;
  771. }
  772. else
  773. {
  774. firstLineColor = Colors::YELLOW;
  775. secondLineColor = Colors::WHITE;
  776. }
  777. //printing spell's name
  778. printAtMiddleLoc(spell->name, 39, 70, FONT_TINY, firstLineColor, to);
  779. //printing lvl
  780. printAtMiddleLoc(CGI->generaltexth->allTexts[171 + spell->level], 39, 82, FONT_TINY, secondLineColor, to);
  781. //printing cost
  782. std::ostringstream ss;
  783. ss << CGI->generaltexth->allTexts[387] << ": " << spellCost;
  784. printAtMiddleLoc(ss.str(), 39, 94, FONT_TINY, secondLineColor, to);
  785. }
  786. void CSpellWindow::SpellArea::setSpell(SpellID spellID)
  787. {
  788. mySpell = spellID;
  789. if(mySpell < 0)
  790. return;
  791. const CSpell * spell = CGI->spellh->objects[mySpell];
  792. schoolLevel = owner->myHero->getSpellSchoolLevel(spell, &whichSchool);
  793. spellCost = owner->myInt->cb->getSpellCost(spell, owner->myHero);
  794. }