CSpellWindow.cpp 25 KB

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