CSpellWindow.cpp 26 KB

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