CSpellWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. #include "CSpellWindow.h"
  2. #include "Graphics.h"
  3. #include "CDefHandler.h"
  4. #include "../lib/CObjectHandler.h"
  5. #include "../lib/CSpellHandler.h"
  6. #include "../lib/CGeneralTextHandler.h"
  7. #include "CVideoHandler.h"
  8. #include "CAdvmapInterface.h"
  9. #include "CBattleInterface.h"
  10. #include "CGameInfo.h"
  11. #include "SDL_Extensions.h"
  12. #include "CMessage.h"
  13. #include "CPlayerInterface.h"
  14. #include "../CCallback.h"
  15. #include <boost/bind.hpp>
  16. #include <sstream>
  17. #include <boost/algorithm/string/replace.hpp>
  18. #include <boost/lexical_cast.hpp>
  19. #include "CBitmapHandler.h"
  20. /*
  21. * CSpellWindow.cpp, part of VCMI engine
  22. *
  23. * Authors: listed in file AUTHORS in main folder
  24. *
  25. * License: GNU General Public License v2.0 or later
  26. * Full text of license available in license.txt file, in main folder
  27. *
  28. */
  29. extern SDL_Surface * screen;
  30. extern SDL_Color tytulowy, zwykly, darkTitle;
  31. SpellbookInteractiveArea::SpellbookInteractiveArea(const SDL_Rect & myRect, boost::function<void()> funcL,
  32. const std::string & textR, boost::function<void()> funcHon, boost::function<void()> funcHoff, CPlayerInterface * _myInt)
  33. {
  34. used = 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 & myRect, const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  66. battleSpellsOnly(openOnBattleSpells),
  67. selectedTab(4),
  68. currentPage(0),
  69. myHero(_myHero),
  70. myInt(_myInt)
  71. {
  72. //initializing castable spells
  73. for(ui32 v=0; v<CGI->spellh->spells.size(); ++v)
  74. {
  75. if( !CGI->spellh->spells[v]->creatureAbility && myHero->canCastThisSpell(CGI->spellh->spells[v]) )
  76. mySpells.insert(v);
  77. }
  78. //initializing sizes of spellbook's parts
  79. for(int b=0; b<5; ++b)
  80. sitesPerTabAdv[b] = 0;
  81. for(int b=0; b<5; ++b)
  82. sitesPerTabBattle[b] = 0;
  83. for(std::set<ui32>::const_iterator g = mySpells.begin(); g!=mySpells.end(); ++g)
  84. {
  85. const CSpell &s = *CGI->spellh->spells[*g];
  86. Uint8 *sitesPerOurTab = s.combatSpell ? sitesPerTabBattle : sitesPerTabAdv;
  87. ++sitesPerOurTab[4];
  88. if(s.air)
  89. ++sitesPerOurTab[0];
  90. if(s.fire)
  91. ++sitesPerOurTab[1];
  92. if(s.water)
  93. ++sitesPerOurTab[2];
  94. if(s.earth)
  95. ++sitesPerOurTab[3];
  96. }
  97. if(sitesPerTabAdv[4] % 12 == 0)
  98. sitesPerTabAdv[4]/=12;
  99. else
  100. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  101. for(int v=0; v<4; ++v)
  102. {
  103. if(sitesPerTabAdv[v] <= 10)
  104. sitesPerTabAdv[v] = 1;
  105. else
  106. {
  107. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  108. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  109. else
  110. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  111. }
  112. }
  113. if(sitesPerTabBattle[4] % 12 == 0)
  114. sitesPerTabBattle[4]/=12;
  115. else
  116. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  117. for(int v=0; v<4; ++v)
  118. {
  119. if(sitesPerTabBattle[v] <= 10)
  120. sitesPerTabBattle[v] = 1;
  121. else
  122. {
  123. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  124. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  125. else
  126. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  127. }
  128. }
  129. //numbers of spell pages computed
  130. pos = myRect;
  131. background = BitmapHandler::loadBitmap("SpelBack.bmp");
  132. graphics->blueToPlayersAdv(background, myHero->tempOwner);
  133. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  134. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  135. spells = CDefHandler::giveDef("Spells.def");
  136. spellTab = CDefHandler::giveDef("SpelTab.def");
  137. schools = CDefHandler::giveDef("Schools.def");
  138. schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
  139. schoolBorders[1] = CDefHandler::giveDef("SplevF.def");
  140. schoolBorders[2] = CDefHandler::giveDef("SplevW.def");
  141. schoolBorders[3] = CDefHandler::giveDef("SplevE.def");
  142. statusBar = new CStatusBar(7 + pos.x, 569 + pos.y, "Spelroll.bmp");
  143. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  144. exitBtn = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fexitb, this), CGI->generaltexth->zelp[460].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[460].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  145. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  146. battleSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fbattleSpellsb, this), CGI->generaltexth->zelp[453].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[453].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  147. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  148. adventureSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fadvSpellsb, this), CGI->generaltexth->zelp[452].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[452].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  149. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  150. manaPoints = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fmanaPtsb, this), CGI->generaltexth->zelp[459].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[459].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  151. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  152. selectSpellsA = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 0), CGI->generaltexth->zelp[454].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[454].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  153. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  154. selectSpellsE = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 3), CGI->generaltexth->zelp[457].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[457].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  155. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  156. selectSpellsF = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 1), CGI->generaltexth->zelp[455].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[455].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  157. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  158. selectSpellsW = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 2), CGI->generaltexth->zelp[456].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[456].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  159. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  160. selectSpellsAll = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::selectSchool, this, 4), CGI->generaltexth->zelp[458].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[458].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  161. temp_rect = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y);
  162. lCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fLcornerb, this), CGI->generaltexth->zelp[450].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[450].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  163. temp_rect = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y);
  164. rCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fRcornerb, this), CGI->generaltexth->zelp[451].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[451].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  165. //areas for spells
  166. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  167. for(int v=0; v<12; ++v)
  168. {
  169. temp_rect = genRect(65, 78, xpos, ypos);
  170. spellAreas[v] = new SpellArea(temp_rect, this);
  171. if(v == 5) //to right page
  172. {
  173. xpos = 336 + pos.x; ypos = 90 + pos.y;
  174. }
  175. else
  176. {
  177. if(v%2 == 0)
  178. {
  179. xpos+=85;
  180. }
  181. else
  182. {
  183. xpos -= 85; ypos+=97;
  184. }
  185. }
  186. }
  187. selectedTab = battleSpellsOnly ? LOCPLINT->spellbookSettings.spellbookLastTabBattle : LOCPLINT->spellbookSettings.spellbookLastTabAdvmap;
  188. currentPage = battleSpellsOnly ? LOCPLINT->spellbookSettings.spellbookLastPageBattle : LOCPLINT->spellbookSettings.spellbokLastPageAdvmap;
  189. abetw(currentPage, 0, pagesWithinCurrentTab());
  190. computeSpellsPerArea();
  191. }
  192. CSpellWindow::~CSpellWindow()
  193. {
  194. SDL_FreeSurface(background);
  195. SDL_FreeSurface(leftCorner);
  196. SDL_FreeSurface(rightCorner);
  197. delete spells;
  198. delete spellTab;
  199. delete schools;
  200. for(int b=0; b<4; ++b)
  201. delete schoolBorders[b];
  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(int g=0; g<12; ++g)
  215. {
  216. delete spellAreas[g];
  217. }
  218. }
  219. void CSpellWindow::fexitb()
  220. {
  221. (LOCPLINT->battleInt ? LOCPLINT->spellbookSettings.spellbookLastTabBattle : LOCPLINT->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  222. (LOCPLINT->battleInt ? LOCPLINT->spellbookSettings.spellbookLastPageBattle : LOCPLINT->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. turnPageRight();
  253. selectedTab = school;
  254. currentPage = 0;
  255. }
  256. computeSpellsPerArea();
  257. }
  258. void CSpellWindow::fLcornerb()
  259. {
  260. if(currentPage>0)
  261. {
  262. turnPageLeft();
  263. --currentPage;
  264. }
  265. computeSpellsPerArea();
  266. GH.breakEventHandling();
  267. }
  268. void CSpellWindow::fRcornerb()
  269. {
  270. if((currentPage + 1) < (pagesWithinCurrentTab()))
  271. {
  272. turnPageRight();
  273. ++currentPage;
  274. }
  275. computeSpellsPerArea();
  276. GH.breakEventHandling();
  277. }
  278. void CSpellWindow::showAll(SDL_Surface *to)
  279. {
  280. CSDL_Ext::blitSurface(background, NULL, to, &pos);
  281. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  282. std::ostringstream mana;
  283. mana<<myHero->mana;
  284. CSDL_Ext::printAtMiddle(mana.str(), pos.x+435, pos.y +426, FONT_SMALL, tytulowy, to);
  285. statusBar->showAll(to);
  286. //printing school images
  287. if(selectedTab!=4 && currentPage == 0)
  288. {
  289. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  290. }
  291. //printing corners
  292. if(currentPage!=0)
  293. {
  294. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  295. }
  296. if((currentPage+1) < (pagesWithinCurrentTab()) )
  297. {
  298. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  299. }
  300. //printing spell info
  301. for(int b=0; b<12; ++b)
  302. {
  303. spellAreas[b]->showAll(to);
  304. }
  305. }
  306. class SpellbookSpellSorter
  307. {
  308. public:
  309. bool operator()(const int & a, const int & b)
  310. {
  311. const CSpell & A = *CGI->spellh->spells[a];
  312. const CSpell & B = *CGI->spellh->spells[b];
  313. if(A.level<B.level)
  314. return true;
  315. if(A.level>B.level)
  316. return false;
  317. if(A.air && !B.air)
  318. return true;
  319. if(!A.air && B.air)
  320. return false;
  321. if(A.fire && !B.fire)
  322. return true;
  323. if(!A.fire && B.fire)
  324. return false;
  325. if(A.water && !B.water)
  326. return true;
  327. if(!A.water && B.water)
  328. return false;
  329. if(A.earth && !B.earth)
  330. return true;
  331. if(!A.earth && B.earth)
  332. return false;
  333. return A.name < B.name;
  334. }
  335. } spellsorter;
  336. void CSpellWindow::computeSpellsPerArea()
  337. {
  338. std::vector<ui32> spellsCurSite;
  339. for(std::set<ui32>::const_iterator it = mySpells.begin(); it != mySpells.end(); ++it)
  340. {
  341. if(CGI->spellh->spells[*it]->combatSpell ^ !battleSpellsOnly
  342. && ((CGI->spellh->spells[*it]->air && selectedTab == 0) ||
  343. (CGI->spellh->spells[*it]->fire && selectedTab == 1) ||
  344. (CGI->spellh->spells[*it]->water && selectedTab == 2) ||
  345. (CGI->spellh->spells[*it]->earth && selectedTab == 3) ||
  346. selectedTab == 4 )
  347. )
  348. {
  349. spellsCurSite.push_back(*it);
  350. }
  351. }
  352. std::sort(spellsCurSite.begin(), spellsCurSite.end(), spellsorter);
  353. if(selectedTab == 4)
  354. {
  355. if(spellsCurSite.size() > 12)
  356. {
  357. spellsCurSite = std::vector<ui32>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  358. if(spellsCurSite.size() > 12)
  359. {
  360. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  361. }
  362. }
  363. }
  364. else //selectedTab == 0, 1, 2 or 3
  365. {
  366. if(spellsCurSite.size() > 10)
  367. {
  368. if(currentPage == 0)
  369. {
  370. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  371. }
  372. else
  373. {
  374. spellsCurSite = std::vector<ui32>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  375. if(spellsCurSite.size() > 12)
  376. {
  377. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  378. }
  379. }
  380. }
  381. }
  382. //applying
  383. if(selectedTab == 4 || currentPage != 0)
  384. {
  385. for(size_t c=0; c<12; ++c)
  386. {
  387. if(c<spellsCurSite.size())
  388. {
  389. spellAreas[c]->setSpell(spellsCurSite[c]);
  390. }
  391. else
  392. {
  393. spellAreas[c]->setSpell(-1);
  394. }
  395. }
  396. }
  397. else
  398. {
  399. spellAreas[0]->setSpell(-1);
  400. spellAreas[1]->setSpell(-1);
  401. for(size_t c=0; c<10; ++c)
  402. {
  403. if(c<spellsCurSite.size())
  404. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  405. else
  406. spellAreas[c+2]->setSpell(-1);
  407. }
  408. }
  409. redraw();
  410. }
  411. void CSpellWindow::activate()
  412. {
  413. activateKeys();
  414. exitBtn->activate();
  415. battleSpells->activate();
  416. adventureSpells->activate();
  417. manaPoints->activate();
  418. selectSpellsA->activate();
  419. selectSpellsE->activate();
  420. selectSpellsF->activate();
  421. selectSpellsW->activate();
  422. selectSpellsAll->activate();
  423. for(int g=0; g<12; ++g)
  424. {
  425. spellAreas[g]->activate();
  426. }
  427. lCorner->activate();
  428. rCorner->activate();
  429. }
  430. void CSpellWindow::deactivate()
  431. {
  432. deactivateKeys();
  433. exitBtn->deactivate();
  434. battleSpells->deactivate();
  435. adventureSpells->deactivate();
  436. manaPoints->deactivate();
  437. selectSpellsA->deactivate();
  438. selectSpellsE->deactivate();
  439. selectSpellsF->deactivate();
  440. selectSpellsW->deactivate();
  441. selectSpellsAll->deactivate();
  442. for(int g=0; g<12; ++g)
  443. {
  444. spellAreas[g]->deactivate();
  445. }
  446. lCorner->deactivate();
  447. rCorner->deactivate();
  448. }
  449. void CSpellWindow::turnPageLeft()
  450. {
  451. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  452. }
  453. void CSpellWindow::turnPageRight()
  454. {
  455. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  456. }
  457. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  458. {
  459. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  460. fexitb();
  461. if(key.state == SDL_PRESSED)
  462. {
  463. switch(key.keysym.sym)
  464. {
  465. case SDLK_LEFT:
  466. fLcornerb();
  467. break;
  468. case SDLK_RIGHT:
  469. fRcornerb();
  470. break;
  471. case SDLK_UP:
  472. case SDLK_DOWN:
  473. {
  474. bool down = key.keysym.sym == SDLK_DOWN;
  475. static const int schoolsOrder[] = {0, 3, 1, 2, 4};
  476. int index = -1;
  477. while(schoolsOrder[++index] != selectedTab);
  478. index += (down ? 1 : -1);
  479. abetw(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  480. if(selectedTab != schoolsOrder[index])
  481. selectSchool(schoolsOrder[index]);
  482. break;
  483. }
  484. default://to get rid of warnings
  485. break;
  486. }
  487. //alt + 1234567890-= casts spell from 1 - 12 slot
  488. if(LOCPLINT->altPressed())
  489. {
  490. SDLKey hlpKey = key.keysym.sym;
  491. if(isNumKey(hlpKey, false))
  492. {
  493. if(hlpKey == SDLK_KP_PLUS)
  494. hlpKey = SDLK_EQUALS;
  495. else
  496. hlpKey = numToDigit(hlpKey);
  497. }
  498. 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};
  499. int index = -1;
  500. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  501. if(index >= ARRAY_COUNT(spellSelectors))
  502. return;
  503. //try casting spell
  504. spellAreas[index]->clickLeft(false, true);
  505. }
  506. }
  507. }
  508. Uint8 CSpellWindow::pagesWithinCurrentTab()
  509. {
  510. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  511. }
  512. void CSpellWindow::teleportTo( int town, const CGHeroInstance * hero )
  513. {
  514. const CGTownInstance * dest = LOCPLINT->cb->getTownInfo(town, 1);
  515. LOCPLINT->cb->castSpell(hero, Spells::TOWN_PORTAL, dest->visitablePos() + hero->getVisitableOffset());
  516. }
  517. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  518. {
  519. this->pos = pos;
  520. this->owner = owner;
  521. used = LCLICK | RCLICK | HOVER;
  522. spellCost = mySpell = whichSchool = schoolLevel = -1;
  523. }
  524. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  525. {
  526. if(!down && mySpell!=-1)
  527. {
  528. const CSpell *sp = CGI->spellh->spells[mySpell];
  529. int spellCost = owner->myInt->cb->getSpellCost(sp, owner->myHero);
  530. if(spellCost > owner->myHero->mana) //insufficient mana
  531. {
  532. char msgBuf[500];
  533. sprintf(msgBuf, CGI->generaltexth->allTexts[206].c_str(), spellCost, owner->myHero->mana);
  534. owner->myInt->showInfoDialog(std::string(msgBuf));
  535. return;
  536. }
  537. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  538. if((sp->combatSpell && !owner->myInt->battleInt)
  539. || (!sp->combatSpell && owner->myInt->battleInt))
  540. {
  541. std::vector<SComponent*> hlp(1, new SComponent(SComponent::spell, mySpell, 0));
  542. LOCPLINT->showInfoDialog(sp->descriptions[schoolLevel], hlp);
  543. return;
  544. }
  545. //we will cast a spell
  546. if(sp->combatSpell && owner->myInt->battleInt && owner->myInt->cb->battleCanCastSpell()) //if battle window is open
  547. {
  548. int spell = mySpell;
  549. owner->fexitb();
  550. owner->myInt->battleInt->castThisSpell(spell);
  551. }
  552. else if(!sp->combatSpell && !owner->myInt->battleInt) //adventure spell
  553. {
  554. using namespace Spells;
  555. int spell = mySpell;
  556. const CGHeroInstance *h = owner->myHero;
  557. owner->fexitb();
  558. switch(spell)
  559. {
  560. case SUMMON_BOAT:
  561. {
  562. int3 pos = h->bestLocation();
  563. if(pos.x < 0)
  564. {
  565. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[334]); //There is no place to put the boat.
  566. return;
  567. }
  568. }
  569. break;
  570. case SCUTTLE_BOAT:
  571. case DIMENSION_DOOR:
  572. adventureInt->enterCastingMode(sp);
  573. return;
  574. case VISIONS:
  575. case VIEW_EARTH:
  576. case DISGUISE:
  577. case VIEW_AIR:
  578. case FLY:
  579. case WATER_WALK:
  580. break;
  581. case TOWN_PORTAL:
  582. {
  583. std::vector <int> availableTowns;
  584. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(false);
  585. for(size_t i=0;i<Towns.size();i++)
  586. {
  587. const CGTownInstance *t = Towns[i];
  588. if (t->visitingHero == NULL) //empty town and this is
  589. {
  590. availableTowns.push_back(t->id);//add to the list
  591. }
  592. }
  593. if (h->getSpellSchoolLevel(CGI->spellh->spells[spell]) < 3) //not expert - teleport to nearest available city
  594. {
  595. int nearest = -1; //nearest town's ID
  596. double dist = -1;
  597. for (int g=0; g<availableTowns.size(); ++g)
  598. {
  599. const CGTownInstance * dest = LOCPLINT->cb->getTownInfo(availableTowns[g], 1);
  600. double curDist = dest->pos.dist2d(h->pos);
  601. if (nearest == -1 || curDist < dist)
  602. {
  603. nearest = g;
  604. dist = curDist;
  605. }
  606. }
  607. LOCPLINT->cb->castSpell(h, spell,
  608. LOCPLINT->cb->getTownInfo(availableTowns[nearest], 1)->visitablePos() + h->getVisitableOffset());
  609. }
  610. else
  611. { //let the player choose
  612. GH.pushInt (new CObjectListWindow(availableTowns,
  613. new CPicture(graphics->spellscr->ourImages[spell].bitmap, 0, 0, false),
  614. CGI->generaltexth->jktexts[40], CGI->generaltexth->jktexts[41],
  615. boost::bind (&CSpellWindow::teleportTo, owner, _1, h)));
  616. }
  617. return;
  618. }
  619. break;
  620. default:
  621. assert(0);
  622. }
  623. //can return earlier in some cases
  624. LOCPLINT->cb->castSpell(h, spell);
  625. }
  626. }
  627. }
  628. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  629. {
  630. if(down && mySpell != -1)
  631. {
  632. std::string dmgInfo;
  633. const CGHeroInstance * hero = owner->myHero;
  634. int causedDmg = owner->myInt->cb->estimateSpellDamage( CGI->spellh->spells[mySpell], (hero ? hero : NULL));
  635. if(causedDmg == 0)
  636. dmgInfo = "";
  637. else
  638. {
  639. dmgInfo = CGI->generaltexth->allTexts[343];
  640. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  641. }
  642. SDL_Surface *spellBox = CMessage::drawBoxTextBitmapSub(
  643. owner->myInt->playerID,
  644. CGI->spellh->spells[mySpell]->descriptions[schoolLevel] + dmgInfo, this->owner->spells->ourImages[mySpell].bitmap,
  645. CGI->spellh->spells[mySpell]->name,30,30);
  646. CInfoPopup *vinya = new CInfoPopup(spellBox, true);
  647. GH.pushInt(vinya);
  648. }
  649. }
  650. void CSpellWindow::SpellArea::hover(bool on)
  651. {
  652. //Hoverable::hover(on);
  653. if(mySpell != -1)
  654. {
  655. if(on)
  656. {
  657. std::ostringstream ss;
  658. ss<<CGI->spellh->spells[mySpell]->name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->spells[mySpell]->level]<<")";
  659. owner->statusBar->print(ss.str());
  660. }
  661. else
  662. {
  663. owner->statusBar->clear();
  664. }
  665. }
  666. }
  667. void CSpellWindow::SpellArea::showAll(SDL_Surface *to)
  668. {
  669. if(mySpell < 0)
  670. return;
  671. const CSpell * spell = CGI->spellh->spells[mySpell];
  672. blitAt(owner->spells->ourImages[mySpell].bitmap, pos.x, pos.y, to);
  673. blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school)
  674. SDL_Color firstLineColor, secondLineColor;
  675. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  676. {
  677. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  678. firstLineColor = zwykly;
  679. secondLineColor = unavailableSpell;
  680. }
  681. else
  682. {
  683. firstLineColor = tytulowy;
  684. secondLineColor = zwykly;
  685. }
  686. //printing spell's name
  687. CSDL_Ext::printAtMiddle(spell->name, pos.x + 39, pos.y + 70, FONT_TINY, firstLineColor, to);
  688. //printing lvl
  689. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[171 + spell->level], pos.x + 39, pos.y + 82, FONT_TINY, secondLineColor, to);
  690. //printing cost
  691. std::ostringstream ss;
  692. ss << CGI->generaltexth->allTexts[387] << ": " << spellCost;
  693. CSDL_Ext::printAtMiddle(ss.str(), pos.x + 39, pos.y + 94, FONT_TINY, secondLineColor, to);
  694. }
  695. void CSpellWindow::SpellArea::setSpell(int spellID)
  696. {
  697. mySpell = spellID;
  698. if(mySpell < 0)
  699. return;
  700. const CSpell * spell = CGI->spellh->spells[mySpell];
  701. schoolLevel = owner->myHero->getSpellSchoolLevel(spell, &whichSchool);
  702. spellCost = owner->myInt->cb->getSpellCost(spell, owner->myHero);
  703. }