CSpellWindow.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 "UIFramework/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. 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. 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(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. 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 CStatusBar(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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[460].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[453].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[452].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[459].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[454].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[457].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[455].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[456].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[458].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[450].first)), boost::bind(&CStatusBar::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(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[451].first)), boost::bind(&CStatusBar::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(int b=0; b<4; ++b)
  199. delete schoolBorders[b];
  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(int g=0; g<12; ++g)
  213. {
  214. delete spellAreas[g];
  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. CSDL_Ext::printAtMiddle(mana.str(), pos.x+435, pos.y +426, FONT_SMALL, Colors::Jasmine, 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(int b=0; b<12; ++b)
  300. {
  301. spellAreas[b]->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<ui32> spellsCurSite;
  341. for(std::set<ui32>::const_iterator it = mySpells.begin(); it != mySpells.end(); ++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<ui32>(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<ui32>(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(-1);
  396. }
  397. }
  398. }
  399. else
  400. {
  401. spellAreas[0]->setSpell(-1);
  402. spellAreas[1]->setSpell(-1);
  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(-1);
  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(int g=0; g<12; ++g)
  426. {
  427. spellAreas[g]->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(int g=0; g<12; ++g)
  445. {
  446. spellAreas[g]->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. default://to get rid of warnings
  487. break;
  488. }
  489. //alt + 1234567890-= casts spell from 1 - 12 slot
  490. if(LOCPLINT->altPressed())
  491. {
  492. SDLKey hlpKey = key.keysym.sym;
  493. if(CGuiHandler::isNumKey(hlpKey, false))
  494. {
  495. if(hlpKey == SDLK_KP_PLUS)
  496. hlpKey = SDLK_EQUALS;
  497. else
  498. hlpKey = CGuiHandler::numToDigit(hlpKey);
  499. }
  500. 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};
  501. int index = -1;
  502. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  503. if(index >= ARRAY_COUNT(spellSelectors))
  504. return;
  505. //try casting spell
  506. spellAreas[index]->clickLeft(false, true);
  507. }
  508. }
  509. }
  510. Uint8 CSpellWindow::pagesWithinCurrentTab()
  511. {
  512. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  513. }
  514. void CSpellWindow::teleportTo( int town, const CGHeroInstance * hero )
  515. {
  516. const CGTownInstance * dest = LOCPLINT->cb->getTown(town);
  517. LOCPLINT->cb->castSpell(hero, Spells::TOWN_PORTAL, dest->visitablePos());
  518. }
  519. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  520. {
  521. this->pos = pos;
  522. this->owner = owner;
  523. addUsedEvents(LCLICK | RCLICK | HOVER);
  524. spellCost = mySpell = whichSchool = schoolLevel = -1;
  525. }
  526. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  527. {
  528. if(!down && mySpell!=-1)
  529. {
  530. const CSpell *sp = CGI->spellh->spells[mySpell];
  531. int spellCost = owner->myInt->cb->getSpellCost(sp, owner->myHero);
  532. if(spellCost > owner->myHero->mana) //insufficient mana
  533. {
  534. char msgBuf[500];
  535. sprintf(msgBuf, CGI->generaltexth->allTexts[206].c_str(), spellCost, owner->myHero->mana);
  536. owner->myInt->showInfoDialog(std::string(msgBuf));
  537. return;
  538. }
  539. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  540. if((sp->combatSpell && !owner->myInt->battleInt)
  541. || (!sp->combatSpell && owner->myInt->battleInt))
  542. {
  543. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell, 0));
  544. LOCPLINT->showInfoDialog(sp->descriptions[schoolLevel], hlp);
  545. return;
  546. }
  547. //we will cast a spell
  548. if(sp->combatSpell && owner->myInt->battleInt && owner->myInt->cb->battleCanCastSpell()) //if battle window is open
  549. {
  550. ESpellCastProblem::ESpellCastProblem problem = owner->myInt->cb->battleCanCastThisSpell(sp);
  551. switch (problem)
  552. {
  553. case ESpellCastProblem::OK:
  554. {
  555. int spell = mySpell;
  556. owner->fexitb();
  557. owner->myInt->battleInt->castThisSpell(spell);
  558. }
  559. break;
  560. case ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED:
  561. {
  562. std::string text = CGI->generaltexth->allTexts[538], summoner, elemental, caster;
  563. std::vector<const CStack *> stacks = owner->myInt->cb->battleGetStacks();
  564. BOOST_FOREACH(const CStack * s, stacks)
  565. {
  566. if(vstd::contains(s->state, EBattleStackState::SUMMONED))
  567. {
  568. elemental = s->getCreature()->namePl;
  569. summoner = owner->myInt->cb->battleGetFightingHero(!s->attackerOwned)->name;
  570. break;
  571. }
  572. }
  573. if (owner->myHero->type->sex)
  574. { //female
  575. caster = CGI->generaltexth->allTexts[540];
  576. }
  577. else
  578. { //male
  579. caster = CGI->generaltexth->allTexts[539];
  580. }
  581. text = boost::str(boost::format(text) % summoner % elemental % caster);
  582. owner->myInt->showInfoDialog(text);
  583. }
  584. break;
  585. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  586. {
  587. //Recanter's Cloak or similar effect. Try to retrieve bonus
  588. const Bonus *b = owner->myHero->getBonusLocalFirst(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
  589. //TODO what about other values and non-artifact sources?
  590. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  591. {
  592. std::string artName = CGI->arth->artifacts[b->sid]->Name();
  593. //The %s prevents %s from casting 3rd level or higher spells.
  594. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[536])
  595. % artName % owner->myHero->name));
  596. }
  597. else
  598. {
  599. // General message:
  600. // %s recites the incantations but they seem to have no effect.
  601. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  602. text = boost::str(boost::format(text) % caster);
  603. owner->myInt->showInfoDialog(text);
  604. }
  605. }
  606. break;
  607. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  608. {
  609. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[185]);
  610. }
  611. break;
  612. }
  613. }
  614. else if(!sp->combatSpell && !owner->myInt->battleInt) //adventure spell
  615. {
  616. using namespace Spells;
  617. int spell = mySpell;
  618. const CGHeroInstance *h = owner->myHero;
  619. owner->fexitb();
  620. switch(spell)
  621. {
  622. case SUMMON_BOAT:
  623. {
  624. int3 pos = h->bestLocation();
  625. if(pos.x < 0)
  626. {
  627. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[334]); //There is no place to put the boat.
  628. return;
  629. }
  630. }
  631. break;
  632. case SCUTTLE_BOAT:
  633. case DIMENSION_DOOR:
  634. adventureInt->enterCastingMode(sp);
  635. return;
  636. case VISIONS:
  637. case VIEW_EARTH:
  638. case DISGUISE:
  639. case VIEW_AIR:
  640. case FLY:
  641. case WATER_WALK:
  642. break;
  643. case TOWN_PORTAL:
  644. {
  645. std::vector <int> availableTowns;
  646. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
  647. if (Towns.empty())
  648. {
  649. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  650. return;
  651. }
  652. if (h->getSpellSchoolLevel(CGI->spellh->spells[spell]) < 2) //not advanced or expert - teleport to nearest available city
  653. {
  654. int nearest = -1; //nearest town's ID
  655. double dist = -1;
  656. for (int g=0; g<Towns.size(); ++g)
  657. {
  658. const CGTownInstance * dest = LOCPLINT->cb->getTown(Towns[g]->id);
  659. double curDist = dest->pos.dist2d(h->pos);
  660. if (nearest == -1 || curDist < dist)
  661. {
  662. nearest = g;
  663. dist = curDist;
  664. }
  665. }
  666. if ( Towns[nearest]->visitingHero )
  667. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[123]);
  668. else
  669. {
  670. const CGTownInstance * town = LOCPLINT->cb->getTown(Towns[nearest]->id);
  671. LOCPLINT->cb->castSpell(h, spell, town->visitablePos());// - town->getVisitableOffset());
  672. }
  673. }
  674. else
  675. { //let the player choose
  676. for(size_t i=0;i<Towns.size();i++)
  677. {
  678. const CGTownInstance *t = Towns[i];
  679. if (t->visitingHero == NULL) //empty town and this is
  680. {
  681. availableTowns.push_back(t->id);//add to the list
  682. }
  683. }
  684. if (availableTowns.empty())
  685. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  686. else
  687. GH.pushInt (new CObjectListWindow(availableTowns,
  688. new CPicture(graphics->spellscr->ourImages[spell].bitmap, 0, 0, false),
  689. CGI->generaltexth->jktexts[40], CGI->generaltexth->jktexts[41],
  690. boost::bind (&CSpellWindow::teleportTo, owner, _1, h)));
  691. }
  692. return;
  693. }
  694. break;
  695. default:
  696. assert(0);
  697. }
  698. //can return earlier in some cases
  699. LOCPLINT->cb->castSpell(h, spell);
  700. }
  701. }
  702. }
  703. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  704. {
  705. if(down && mySpell != -1)
  706. {
  707. std::string dmgInfo;
  708. const CGHeroInstance * hero = owner->myHero;
  709. int causedDmg = owner->myInt->cb->estimateSpellDamage( CGI->spellh->spells[mySpell], (hero ? hero : NULL));
  710. if(causedDmg == 0 || mySpell == 57) //Titan's Lightning Bolt already has damage info included
  711. dmgInfo = "";
  712. else
  713. {
  714. dmgInfo = CGI->generaltexth->allTexts[343];
  715. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  716. }
  717. SDL_Surface *spellBox = CMessage::drawBoxTextBitmapSub(
  718. owner->myInt->playerID,
  719. CGI->spellh->spells[mySpell]->descriptions[schoolLevel] + dmgInfo, this->owner->spells->ourImages[mySpell].bitmap,
  720. CGI->spellh->spells[mySpell]->name,30,30);
  721. CInfoPopup *vinya = new CInfoPopup(spellBox, true);
  722. GH.pushInt(vinya);
  723. }
  724. }
  725. void CSpellWindow::SpellArea::hover(bool on)
  726. {
  727. //Hoverable::hover(on);
  728. if(mySpell != -1)
  729. {
  730. if(on)
  731. {
  732. std::ostringstream ss;
  733. ss<<CGI->spellh->spells[mySpell]->name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->spells[mySpell]->level]<<")";
  734. owner->statusBar->print(ss.str());
  735. }
  736. else
  737. {
  738. owner->statusBar->clear();
  739. }
  740. }
  741. }
  742. void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
  743. {
  744. if(mySpell < 0)
  745. return;
  746. const CSpell * spell = CGI->spellh->spells[mySpell];
  747. blitAt(owner->spells->ourImages[mySpell].bitmap, pos.x, pos.y, to);
  748. blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school)
  749. SDL_Color firstLineColor, secondLineColor;
  750. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  751. {
  752. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  753. firstLineColor = Colors::Cornsilk;
  754. secondLineColor = unavailableSpell;
  755. }
  756. else
  757. {
  758. firstLineColor = Colors::Jasmine;
  759. secondLineColor = Colors::Cornsilk;
  760. }
  761. //printing spell's name
  762. CSDL_Ext::printAtMiddle(spell->name, pos.x + 39, pos.y + 70, FONT_TINY, firstLineColor, to);
  763. //printing lvl
  764. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[171 + spell->level], pos.x + 39, pos.y + 82, FONT_TINY, secondLineColor, to);
  765. //printing cost
  766. std::ostringstream ss;
  767. ss << CGI->generaltexth->allTexts[387] << ": " << spellCost;
  768. CSDL_Ext::printAtMiddle(ss.str(), pos.x + 39, pos.y + 94, FONT_TINY, secondLineColor, to);
  769. }
  770. void CSpellWindow::SpellArea::setSpell(int spellID)
  771. {
  772. mySpell = spellID;
  773. if(mySpell < 0)
  774. return;
  775. const CSpell * spell = CGI->spellh->spells[mySpell];
  776. schoolLevel = owner->myHero->getSpellSchoolLevel(spell, &whichSchool);
  777. spellCost = owner->myInt->cb->getSpellCost(spell, owner->myHero);
  778. }