CSpellWindow.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. #include "StdInc.h"
  2. #include "CSpellWindow.h"
  3. #include "Graphics.h"
  4. #include "CDefHandler.h"
  5. #include "../lib/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->spells.size(); ++v)
  75. {
  76. if( !CGI->spellh->spells[v]->creatureAbility && myHero->canCastThisSpell(CGI->spellh->spells[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->spells[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 = CDefHandler::giveDef("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::print, 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::print, 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::print, 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::print, 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::print, 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::print, 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::print, 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::print, 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::print, 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::print, 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::print, 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 ? LOCPLINT->spellbookSettings.spellbookLastTabBattle : LOCPLINT->spellbookSettings.spellbookLastTabAdvmap;
  186. currentPage = battleSpellsOnly ? LOCPLINT->spellbookSettings.spellbookLastPageBattle : LOCPLINT->spellbookSettings.spellbokLastPageAdvmap;
  187. vstd::abetween(currentPage, 0, pagesWithinCurrentTab());
  188. computeSpellsPerArea();
  189. addUsedEvents(KEYBOARD);
  190. }
  191. CSpellWindow::~CSpellWindow()
  192. {
  193. SDL_FreeSurface(leftCorner);
  194. SDL_FreeSurface(rightCorner);
  195. delete spells;
  196. delete spellTab;
  197. delete schools;
  198. for(auto & elem : schoolBorders)
  199. delete elem;
  200. delete exitBtn;
  201. delete battleSpells;
  202. delete adventureSpells;
  203. delete manaPoints;
  204. delete statusBar;
  205. delete selectSpellsA;
  206. delete selectSpellsE;
  207. delete selectSpellsF;
  208. delete selectSpellsW;
  209. delete selectSpellsAll;
  210. delete lCorner;
  211. delete rCorner;
  212. for(auto & elem : spellAreas)
  213. {
  214. delete elem;
  215. }
  216. }
  217. void CSpellWindow::fexitb()
  218. {
  219. (LOCPLINT->battleInt ? LOCPLINT->spellbookSettings.spellbookLastTabBattle : LOCPLINT->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  220. (LOCPLINT->battleInt ? LOCPLINT->spellbookSettings.spellbookLastPageBattle : LOCPLINT->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  221. GH.popIntTotally(this);
  222. }
  223. void CSpellWindow::fadvSpellsb()
  224. {
  225. if (battleSpellsOnly == true)
  226. {
  227. turnPageRight();
  228. battleSpellsOnly = false;
  229. currentPage = 0;
  230. }
  231. computeSpellsPerArea();
  232. }
  233. void CSpellWindow::fbattleSpellsb()
  234. {
  235. if (battleSpellsOnly == false)
  236. {
  237. turnPageLeft();
  238. battleSpellsOnly = true;
  239. currentPage = 0;
  240. }
  241. computeSpellsPerArea();
  242. }
  243. void CSpellWindow::fmanaPtsb()
  244. {
  245. }
  246. void CSpellWindow::selectSchool(int school)
  247. {
  248. if (selectedTab != school)
  249. {
  250. turnPageRight();
  251. selectedTab = school;
  252. currentPage = 0;
  253. }
  254. computeSpellsPerArea();
  255. }
  256. void CSpellWindow::fLcornerb()
  257. {
  258. if(currentPage>0)
  259. {
  260. turnPageLeft();
  261. --currentPage;
  262. }
  263. computeSpellsPerArea();
  264. GH.breakEventHandling();
  265. }
  266. void CSpellWindow::fRcornerb()
  267. {
  268. if((currentPage + 1) < (pagesWithinCurrentTab()))
  269. {
  270. turnPageRight();
  271. ++currentPage;
  272. }
  273. computeSpellsPerArea();
  274. GH.breakEventHandling();
  275. }
  276. void CSpellWindow::showAll(SDL_Surface * to)
  277. {
  278. CWindowObject::showAll(to);
  279. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  280. std::ostringstream mana;
  281. mana<<myHero->mana;
  282. printAtMiddleLoc(mana.str(), 435, 426, FONT_SMALL, Colors::YELLOW, to);
  283. statusBar->showAll(to);
  284. //printing school images
  285. if(selectedTab!=4 && currentPage == 0)
  286. {
  287. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  288. }
  289. //printing corners
  290. if(currentPage!=0)
  291. {
  292. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  293. }
  294. if((currentPage+1) < (pagesWithinCurrentTab()) )
  295. {
  296. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  297. }
  298. //printing spell info
  299. for(auto & elem : spellAreas)
  300. {
  301. elem->showAll(to);
  302. }
  303. }
  304. void CSpellWindow::show(SDL_Surface * to)
  305. {
  306. statusBar->show(to);
  307. }
  308. class SpellbookSpellSorter
  309. {
  310. public:
  311. bool operator()(const int & a, const int & b)
  312. {
  313. const CSpell & A = *CGI->spellh->spells[a];
  314. const CSpell & B = *CGI->spellh->spells[b];
  315. if(A.level<B.level)
  316. return true;
  317. if(A.level>B.level)
  318. return false;
  319. if(A.air && !B.air)
  320. return true;
  321. if(!A.air && B.air)
  322. return false;
  323. if(A.fire && !B.fire)
  324. return true;
  325. if(!A.fire && B.fire)
  326. return false;
  327. if(A.water && !B.water)
  328. return true;
  329. if(!A.water && B.water)
  330. return false;
  331. if(A.earth && !B.earth)
  332. return true;
  333. if(!A.earth && B.earth)
  334. return false;
  335. return A.name < B.name;
  336. }
  337. } spellsorter;
  338. void CSpellWindow::computeSpellsPerArea()
  339. {
  340. std::vector<SpellID> spellsCurSite;
  341. for(auto it = mySpells.cbegin(); it != mySpells.cend(); ++it)
  342. {
  343. if(CGI->spellh->spells[*it]->combatSpell ^ !battleSpellsOnly
  344. && ((CGI->spellh->spells[*it]->air && selectedTab == 0) ||
  345. (CGI->spellh->spells[*it]->fire && selectedTab == 1) ||
  346. (CGI->spellh->spells[*it]->water && selectedTab == 2) ||
  347. (CGI->spellh->spells[*it]->earth && selectedTab == 3) ||
  348. selectedTab == 4 )
  349. )
  350. {
  351. spellsCurSite.push_back(*it);
  352. }
  353. }
  354. std::sort(spellsCurSite.begin(), spellsCurSite.end(), spellsorter);
  355. if(selectedTab == 4)
  356. {
  357. if(spellsCurSite.size() > 12)
  358. {
  359. spellsCurSite = std::vector<SpellID>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  360. if(spellsCurSite.size() > 12)
  361. {
  362. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  363. }
  364. }
  365. }
  366. else //selectedTab == 0, 1, 2 or 3
  367. {
  368. if(spellsCurSite.size() > 10)
  369. {
  370. if(currentPage == 0)
  371. {
  372. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  373. }
  374. else
  375. {
  376. spellsCurSite = std::vector<SpellID>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  377. if(spellsCurSite.size() > 12)
  378. {
  379. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  380. }
  381. }
  382. }
  383. }
  384. //applying
  385. if(selectedTab == 4 || currentPage != 0)
  386. {
  387. for(size_t c=0; c<12; ++c)
  388. {
  389. if(c<spellsCurSite.size())
  390. {
  391. spellAreas[c]->setSpell(spellsCurSite[c]);
  392. }
  393. else
  394. {
  395. spellAreas[c]->setSpell(SpellID::NONE);
  396. }
  397. }
  398. }
  399. else
  400. {
  401. spellAreas[0]->setSpell(SpellID::NONE);
  402. spellAreas[1]->setSpell(SpellID::NONE);
  403. for(size_t c=0; c<10; ++c)
  404. {
  405. if(c<spellsCurSite.size())
  406. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  407. else
  408. spellAreas[c+2]->setSpell(SpellID::NONE);
  409. }
  410. }
  411. redraw();
  412. }
  413. void CSpellWindow::activate()
  414. {
  415. CIntObject::activate();
  416. exitBtn->activate();
  417. battleSpells->activate();
  418. adventureSpells->activate();
  419. manaPoints->activate();
  420. selectSpellsA->activate();
  421. selectSpellsE->activate();
  422. selectSpellsF->activate();
  423. selectSpellsW->activate();
  424. selectSpellsAll->activate();
  425. for(auto & elem : spellAreas)
  426. {
  427. elem->activate();
  428. }
  429. lCorner->activate();
  430. rCorner->activate();
  431. }
  432. void CSpellWindow::deactivate()
  433. {
  434. CIntObject::deactivate();
  435. exitBtn->deactivate();
  436. battleSpells->deactivate();
  437. adventureSpells->deactivate();
  438. manaPoints->deactivate();
  439. selectSpellsA->deactivate();
  440. selectSpellsE->deactivate();
  441. selectSpellsF->deactivate();
  442. selectSpellsW->deactivate();
  443. selectSpellsAll->deactivate();
  444. for(auto & elem : spellAreas)
  445. {
  446. elem->deactivate();
  447. }
  448. lCorner->deactivate();
  449. rCorner->deactivate();
  450. }
  451. void CSpellWindow::turnPageLeft()
  452. {
  453. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  454. }
  455. void CSpellWindow::turnPageRight()
  456. {
  457. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  458. }
  459. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  460. {
  461. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  462. fexitb();
  463. if(key.state == SDL_PRESSED)
  464. {
  465. switch(key.keysym.sym)
  466. {
  467. case SDLK_LEFT:
  468. fLcornerb();
  469. break;
  470. case SDLK_RIGHT:
  471. fRcornerb();
  472. break;
  473. case SDLK_UP:
  474. case SDLK_DOWN:
  475. {
  476. bool down = key.keysym.sym == SDLK_DOWN;
  477. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  478. int index = -1;
  479. while(schoolsOrder[++index] != selectedTab);
  480. index += (down ? 1 : -1);
  481. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  482. if(selectedTab != schoolsOrder[index])
  483. selectSchool(schoolsOrder[index]);
  484. break;
  485. }
  486. case SDLK_c:
  487. fbattleSpellsb();
  488. break;
  489. case SDLK_a:
  490. fadvSpellsb();
  491. break;
  492. default://to get rid of warnings
  493. break;
  494. }
  495. //alt + 1234567890-= casts spell from 1 - 12 slot
  496. if(LOCPLINT->altPressed())
  497. {
  498. SDLKey hlpKey = key.keysym.sym;
  499. if(CGuiHandler::isNumKey(hlpKey, false))
  500. {
  501. if(hlpKey == SDLK_KP_PLUS)
  502. hlpKey = SDLK_EQUALS;
  503. else
  504. hlpKey = CGuiHandler::numToDigit(hlpKey);
  505. }
  506. 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};
  507. int index = -1;
  508. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  509. if(index >= ARRAY_COUNT(spellSelectors))
  510. return;
  511. //try casting spell
  512. spellAreas[index]->clickLeft(false, true);
  513. }
  514. }
  515. }
  516. Uint8 CSpellWindow::pagesWithinCurrentTab()
  517. {
  518. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  519. }
  520. void CSpellWindow::teleportTo( int town, const CGHeroInstance * hero )
  521. {
  522. const CGTownInstance * dest = LOCPLINT->cb->getTown(ObjectInstanceID(town));
  523. LOCPLINT->cb->castSpell(hero, SpellID::TOWN_PORTAL, dest->visitablePos());
  524. }
  525. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  526. {
  527. this->pos = pos;
  528. this->owner = owner;
  529. addUsedEvents(LCLICK | RCLICK | HOVER);
  530. spellCost = whichSchool = schoolLevel = -1;
  531. mySpell = SpellID::NONE;
  532. }
  533. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  534. {
  535. if(!down && mySpell!=-1)
  536. {
  537. const CSpell *sp = CGI->spellh->spells[mySpell];
  538. int spellCost = owner->myInt->cb->getSpellCost(sp, owner->myHero);
  539. if(spellCost > owner->myHero->mana) //insufficient mana
  540. {
  541. char msgBuf[500];
  542. sprintf(msgBuf, CGI->generaltexth->allTexts[206].c_str(), spellCost, owner->myHero->mana);
  543. owner->myInt->showInfoDialog(std::string(msgBuf));
  544. return;
  545. }
  546. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  547. if((sp->combatSpell && !owner->myInt->battleInt)
  548. || (!sp->combatSpell && owner->myInt->battleInt))
  549. {
  550. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell, 0));
  551. LOCPLINT->showInfoDialog(sp->descriptions[schoolLevel], hlp);
  552. return;
  553. }
  554. //we will cast a spell
  555. if(sp->combatSpell && owner->myInt->battleInt && owner->myInt->cb->battleCanCastSpell()) //if battle window is open
  556. {
  557. ESpellCastProblem::ESpellCastProblem problem = owner->myInt->cb->battleCanCastThisSpell(sp);
  558. switch (problem)
  559. {
  560. case ESpellCastProblem::OK:
  561. {
  562. int spell = mySpell;
  563. owner->fexitb();
  564. owner->myInt->battleInt->castThisSpell(spell);
  565. }
  566. break;
  567. case ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED:
  568. {
  569. std::string text = CGI->generaltexth->allTexts[538], summoner, elemental, caster;
  570. std::vector<const CStack *> stacks = owner->myInt->cb->battleGetStacks();
  571. for(const CStack * s : stacks)
  572. {
  573. if(vstd::contains(s->state, EBattleStackState::SUMMONED))
  574. {
  575. elemental = s->getCreature()->namePl;
  576. summoner = owner->myInt->cb->battleGetHeroInfo(!s->attackerOwned).name;
  577. break;
  578. }
  579. }
  580. if (owner->myHero->type->sex)
  581. { //female
  582. caster = CGI->generaltexth->allTexts[540];
  583. }
  584. else
  585. { //male
  586. caster = CGI->generaltexth->allTexts[539];
  587. }
  588. text = boost::str(boost::format(text) % summoner % elemental % caster);
  589. owner->myInt->showInfoDialog(text);
  590. }
  591. break;
  592. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  593. {
  594. //Recanter's Cloak or similar effect. Try to retrieve bonus
  595. const Bonus *b = owner->myHero->getBonusLocalFirst(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
  596. //TODO what about other values and non-artifact sources?
  597. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  598. {
  599. std::string artName = CGI->arth->artifacts[b->sid]->Name();
  600. //The %s prevents %s from casting 3rd level or higher spells.
  601. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[536])
  602. % artName % owner->myHero->name));
  603. }
  604. else
  605. {
  606. // General message:
  607. // %s recites the incantations but they seem to have no effect.
  608. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  609. text = boost::str(boost::format(text) % caster);
  610. owner->myInt->showInfoDialog(text);
  611. }
  612. }
  613. break;
  614. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  615. {
  616. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[185]);
  617. }
  618. break;
  619. }
  620. }
  621. else if(!sp->combatSpell && !owner->myInt->battleInt) //adventure spell
  622. {
  623. SpellID spell = mySpell;
  624. const CGHeroInstance *h = owner->myHero;
  625. owner->fexitb();
  626. switch(spell)
  627. {
  628. case SpellID::SUMMON_BOAT:
  629. {
  630. int3 pos = h->bestLocation();
  631. if(pos.x < 0)
  632. {
  633. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[334]); //There is no place to put the boat.
  634. return;
  635. }
  636. }
  637. break;
  638. case SpellID::SCUTTLE_BOAT:
  639. case SpellID::DIMENSION_DOOR:
  640. adventureInt->enterCastingMode(sp);
  641. return;
  642. case SpellID::VISIONS:
  643. case SpellID::VIEW_EARTH:
  644. case SpellID::DISGUISE:
  645. case SpellID::VIEW_AIR:
  646. case SpellID::FLY:
  647. case SpellID::WATER_WALK:
  648. break;
  649. case SpellID::TOWN_PORTAL:
  650. {
  651. std::vector <int> availableTowns;
  652. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
  653. if (Towns.empty())
  654. {
  655. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  656. return;
  657. }
  658. if (h->getSpellSchoolLevel(CGI->spellh->spells[spell]) < 2) //not advanced or expert - teleport to nearest available city
  659. {
  660. int nearest = -1; //nearest town's ID
  661. double dist = -1;
  662. for (int g=0; g<Towns.size(); ++g)
  663. {
  664. const CGTownInstance * dest = LOCPLINT->cb->getTown(Towns[g]->id);
  665. double curDist = dest->pos.dist2d(h->pos);
  666. if (nearest == -1 || curDist < dist)
  667. {
  668. nearest = g;
  669. dist = curDist;
  670. }
  671. }
  672. if ( Towns[nearest]->visitingHero )
  673. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[123]);
  674. else
  675. {
  676. const CGTownInstance * town = LOCPLINT->cb->getTown(Towns[nearest]->id);
  677. LOCPLINT->cb->castSpell(h, spell, town->visitablePos());// - town->getVisitableOffset());
  678. }
  679. }
  680. else
  681. { //let the player choose
  682. for(auto & Town : Towns)
  683. {
  684. const CGTownInstance *t = Town;
  685. if (t->visitingHero == nullptr) //empty town and this is
  686. {
  687. availableTowns.push_back(t->id.getNum());//add to the list
  688. }
  689. }
  690. if (availableTowns.empty())
  691. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  692. else
  693. GH.pushInt (new CObjectListWindow(availableTowns,
  694. new CPicture(graphics->spellscr->ourImages[spell].bitmap, 0, 0, false),
  695. CGI->generaltexth->jktexts[40], CGI->generaltexth->jktexts[41],
  696. boost::bind (&CSpellWindow::teleportTo, owner, _1, h)));
  697. }
  698. return;
  699. }
  700. break;
  701. default:
  702. assert(0);
  703. }
  704. //can return earlier in some cases
  705. LOCPLINT->cb->castSpell(h, spell);
  706. }
  707. }
  708. }
  709. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  710. {
  711. if(down && mySpell != -1)
  712. {
  713. std::string dmgInfo;
  714. const CGHeroInstance * hero = owner->myHero;
  715. int causedDmg = owner->myInt->cb->estimateSpellDamage( CGI->spellh->spells[mySpell], (hero ? hero : nullptr));
  716. if(causedDmg == 0 || mySpell == 57) //Titan's Lightning Bolt already has damage info included
  717. dmgInfo = "";
  718. else
  719. {
  720. dmgInfo = CGI->generaltexth->allTexts[343];
  721. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  722. }
  723. CRClickPopup::createAndPush(CGI->spellh->spells[mySpell]->descriptions[schoolLevel] + dmgInfo,
  724. new CComponent(CComponent::spell, mySpell));
  725. }
  726. }
  727. void CSpellWindow::SpellArea::hover(bool on)
  728. {
  729. //Hoverable::hover(on);
  730. if(mySpell != -1)
  731. {
  732. if(on)
  733. {
  734. std::ostringstream ss;
  735. ss<<CGI->spellh->spells[mySpell]->name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->spells[mySpell]->level]<<")";
  736. owner->statusBar->print(ss.str());
  737. }
  738. else
  739. {
  740. owner->statusBar->clear();
  741. }
  742. }
  743. }
  744. void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
  745. {
  746. if(mySpell < 0)
  747. return;
  748. const CSpell * spell = mySpell.toSpell();
  749. blitAt(owner->spells->ourImages[mySpell].bitmap, pos.x, pos.y, to);
  750. blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school)
  751. SDL_Color firstLineColor, secondLineColor;
  752. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  753. {
  754. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  755. firstLineColor = Colors::WHITE;
  756. secondLineColor = unavailableSpell;
  757. }
  758. else
  759. {
  760. firstLineColor = Colors::YELLOW;
  761. secondLineColor = Colors::WHITE;
  762. }
  763. //printing spell's name
  764. printAtMiddleLoc(spell->name, 39, 70, FONT_TINY, firstLineColor, to);
  765. //printing lvl
  766. printAtMiddleLoc(CGI->generaltexth->allTexts[171 + spell->level], 39, 82, FONT_TINY, secondLineColor, to);
  767. //printing cost
  768. std::ostringstream ss;
  769. ss << CGI->generaltexth->allTexts[387] << ": " << spellCost;
  770. printAtMiddleLoc(ss.str(), 39, 94, FONT_TINY, secondLineColor, to);
  771. }
  772. void CSpellWindow::SpellArea::setSpell(SpellID spellID)
  773. {
  774. mySpell = spellID;
  775. if(mySpell < 0)
  776. return;
  777. const CSpell * spell = CGI->spellh->spells[mySpell];
  778. schoolLevel = owner->myHero->getSpellSchoolLevel(spell, &whichSchool);
  779. spellCost = owner->myInt->cb->getSpellCost(spell, owner->myHero);
  780. }