CSpellWindow.cpp 26 KB

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