CSpellWindow.cpp 25 KB

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