CSpellWindow.cpp 25 KB

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