CSpellWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. #include "CSpellWindow.h"
  2. #include "Graphics.h"
  3. #include "../hch/CDefHandler.h"
  4. #include "../hch/CObjectHandler.h"
  5. #include "../hch/CSpellHandler.h"
  6. #include "../hch/CGeneralTextHandler.h"
  7. #include "../hch/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. /*
  20. * CSpellWindow.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  28. extern SDL_Surface * screen;
  29. extern SDL_Color tytulowy, zwykly, darkTitle;
  30. SpellbookInteractiveArea::SpellbookInteractiveArea(const SDL_Rect & myRect, boost::function<void()> funcL,
  31. const std::string & textR, boost::function<void()> funcHon, boost::function<void()> funcHoff, CPlayerInterface * _myInt)
  32. {
  33. pos = myRect;
  34. onLeft = funcL;
  35. textOnRclick = textR;
  36. onHoverOn = funcHon;
  37. onHoverOff = funcHoff;
  38. myInt = _myInt;
  39. }
  40. void SpellbookInteractiveArea::clickLeft(tribool down, bool previousState)
  41. {
  42. if(!down)
  43. {
  44. onLeft();
  45. }
  46. }
  47. void SpellbookInteractiveArea::clickRight(tribool down, bool previousState)
  48. {
  49. adventureInt->handleRightClick(textOnRclick, down, this);
  50. }
  51. void SpellbookInteractiveArea::hover(bool on)
  52. {
  53. //Hoverable::hover(on);
  54. if(on)
  55. {
  56. onHoverOn();
  57. }
  58. else
  59. {
  60. onHoverOff();
  61. }
  62. }
  63. void SpellbookInteractiveArea::activate()
  64. {
  65. activateLClick();
  66. activateRClick();
  67. activateHover();
  68. }
  69. void SpellbookInteractiveArea::deactivate()
  70. {
  71. deactivateLClick();
  72. deactivateRClick();
  73. deactivateHover();
  74. }
  75. CSpellWindow::CSpellWindow(const SDL_Rect & myRect, const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  76. battleSpellsOnly(openOnBattleSpells),
  77. selectedTab(4),
  78. spellSite(0),
  79. myHero(_myHero),
  80. myInt(_myInt)
  81. {
  82. //initializing castable spells
  83. for(ui32 v=0; v<CGI->spellh->spells.size(); ++v)
  84. {
  85. if( !CGI->spellh->spells[v].creatureAbility && myHero->canCastThisSpell(&CGI->spellh->spells[v]) )
  86. mySpells.insert(v);
  87. }
  88. //initializing schools' levels
  89. for(int b=0; b<4; ++b) schoolLvls[b] = 0;
  90. for(size_t b=0; b<myHero->secSkills.size(); ++b)
  91. {
  92. switch(myHero->secSkills[b].first)
  93. {
  94. case 14: //fire magic
  95. schoolLvls[1] = myHero->secSkills[b].second;
  96. break;
  97. case 15: //air magic
  98. schoolLvls[0] = myHero->secSkills[b].second;
  99. break;
  100. case 16: //water magic
  101. schoolLvls[2] = myHero->secSkills[b].second;
  102. break;
  103. case 17: //earth magic
  104. schoolLvls[3] = myHero->secSkills[b].second;
  105. break;
  106. }
  107. }
  108. //initializing sizes of spellbook's parts
  109. for(int b=0; b<5; ++b)
  110. sitesPerTabAdv[b] = 0;
  111. for(int b=0; b<5; ++b)
  112. sitesPerTabBattle[b] = 0;
  113. for(std::set<ui32>::const_iterator g = mySpells.begin(); g!=mySpells.end(); ++g)
  114. {
  115. if(CGI->spellh->spells[*g].combatSpell)
  116. {
  117. ++(sitesPerTabBattle[4]);
  118. }
  119. else
  120. {
  121. ++(sitesPerTabAdv[4]);
  122. }
  123. if(CGI->spellh->spells[*g].air)
  124. {
  125. if(CGI->spellh->spells[*g].combatSpell)
  126. {
  127. ++(sitesPerTabBattle[0]);
  128. }
  129. else
  130. {
  131. ++(sitesPerTabAdv[0]);
  132. }
  133. }
  134. if(CGI->spellh->spells[*g].fire)
  135. {
  136. if(CGI->spellh->spells[*g].combatSpell)
  137. {
  138. ++(sitesPerTabBattle[1]);
  139. }
  140. else
  141. {
  142. ++(sitesPerTabAdv[1]);
  143. }
  144. }
  145. if(CGI->spellh->spells[*g].water)
  146. {
  147. if(CGI->spellh->spells[*g].combatSpell)
  148. {
  149. ++(sitesPerTabBattle[2]);
  150. }
  151. else
  152. {
  153. ++(sitesPerTabAdv[2]);
  154. }
  155. }
  156. if(CGI->spellh->spells[*g].earth)
  157. {
  158. if(CGI->spellh->spells[*g].combatSpell)
  159. {
  160. ++(sitesPerTabBattle[3]);
  161. }
  162. else
  163. {
  164. ++(sitesPerTabAdv[3]);
  165. }
  166. }
  167. }
  168. if(sitesPerTabAdv[4] % 12 == 0)
  169. sitesPerTabAdv[4]/=12;
  170. else
  171. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  172. for(int v=0; v<4; ++v)
  173. {
  174. if(sitesPerTabAdv[v] <= 10)
  175. sitesPerTabAdv[v] = 1;
  176. else
  177. {
  178. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  179. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  180. else
  181. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  182. }
  183. }
  184. if(sitesPerTabBattle[4] % 12 == 0)
  185. sitesPerTabBattle[4]/=12;
  186. else
  187. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  188. for(int v=0; v<4; ++v)
  189. {
  190. if(sitesPerTabBattle[v] <= 10)
  191. sitesPerTabBattle[v] = 1;
  192. else
  193. {
  194. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  195. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  196. else
  197. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  198. }
  199. }
  200. //numbers of spell pages computed
  201. pos = myRect;
  202. background = BitmapHandler::loadBitmap("SpelBack.bmp");
  203. graphics->blueToPlayersAdv(background, myHero->tempOwner);
  204. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  205. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  206. spells = CDefHandler::giveDef("Spells.def");
  207. spellTab = CDefHandler::giveDef("SpelTab.def");
  208. schools = CDefHandler::giveDef("Schools.def");
  209. schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
  210. schoolBorders[1] = CDefHandler::giveDef("SplevF.def");
  211. schoolBorders[2] = CDefHandler::giveDef("SplevW.def");
  212. schoolBorders[3] = CDefHandler::giveDef("SplevE.def");
  213. statusBar = new CStatusBar(7 + pos.x, 569 + pos.y, "Spelroll.bmp");
  214. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  215. 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);
  216. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  217. 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);
  218. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  219. 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);
  220. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  221. 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);
  222. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  223. selectSpellsA = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsAb, this), CGI->generaltexth->zelp[454].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[454].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  224. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  225. selectSpellsE = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsEb, this), CGI->generaltexth->zelp[457].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[457].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  226. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  227. selectSpellsF = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsFb, this), CGI->generaltexth->zelp[455].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[455].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  228. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  229. selectSpellsW = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsWb, this), CGI->generaltexth->zelp[456].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[456].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  230. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  231. selectSpellsAll = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsAllb, this), CGI->generaltexth->zelp[458].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[458].first)), boost::bind(&CStatusBar::clear, statusBar), myInt);
  232. temp_rect = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y);
  233. 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);
  234. temp_rect = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y);
  235. 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);
  236. //areas for spells
  237. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  238. for(int v=0; v<12; ++v)
  239. {
  240. temp_rect = genRect(65, 78, xpos, ypos);
  241. spellAreas[v] = new SpellArea(temp_rect, this);
  242. if(v == 5) //to right page
  243. {
  244. xpos = 336 + pos.x; ypos = 90 + pos.y;
  245. }
  246. else
  247. {
  248. if(v%2 == 0)
  249. {
  250. xpos+=85;
  251. }
  252. else
  253. {
  254. xpos -= 85; ypos+=97;
  255. }
  256. }
  257. }
  258. computeSpellsPerArea();
  259. }
  260. CSpellWindow::~CSpellWindow()
  261. {
  262. SDL_FreeSurface(background);
  263. SDL_FreeSurface(leftCorner);
  264. SDL_FreeSurface(rightCorner);
  265. delete spells;
  266. delete spellTab;
  267. delete schools;
  268. for(int b=0; b<4; ++b)
  269. delete schoolBorders[b];
  270. delete exitBtn;
  271. delete battleSpells;
  272. delete adventureSpells;
  273. delete manaPoints;
  274. delete statusBar;
  275. delete selectSpellsA;
  276. delete selectSpellsE;
  277. delete selectSpellsF;
  278. delete selectSpellsW;
  279. delete selectSpellsAll;
  280. delete lCorner;
  281. delete rCorner;
  282. for(int g=0; g<12; ++g)
  283. {
  284. delete spellAreas[g];
  285. }
  286. }
  287. void CSpellWindow::fexitb()
  288. {
  289. GH.popIntTotally(this);
  290. }
  291. void CSpellWindow::fadvSpellsb()
  292. {
  293. if (battleSpellsOnly == true) {
  294. turnPageRight();
  295. battleSpellsOnly = false;
  296. spellSite = 0;
  297. }
  298. computeSpellsPerArea();
  299. }
  300. void CSpellWindow::fbattleSpellsb()
  301. {
  302. if (battleSpellsOnly == false) {
  303. turnPageLeft();
  304. battleSpellsOnly = true;
  305. spellSite = 0;
  306. }
  307. computeSpellsPerArea();
  308. }
  309. void CSpellWindow::fmanaPtsb()
  310. {
  311. }
  312. void CSpellWindow::fspellsAb()
  313. {
  314. if (selectedTab != 0) {
  315. turnPageRight();
  316. selectedTab = 0;
  317. spellSite = 0;
  318. }
  319. computeSpellsPerArea();
  320. }
  321. void CSpellWindow::fspellsEb()
  322. {
  323. if (selectedTab != 3) {
  324. turnPageRight();
  325. selectedTab = 3;
  326. spellSite = 0;
  327. }
  328. computeSpellsPerArea();
  329. }
  330. void CSpellWindow::fspellsFb()
  331. {
  332. if (selectedTab != 1) {
  333. turnPageRight();
  334. selectedTab = 1;
  335. spellSite = 0;
  336. }
  337. computeSpellsPerArea();
  338. }
  339. void CSpellWindow::fspellsWb()
  340. {
  341. if (selectedTab != 2) {
  342. turnPageRight();
  343. selectedTab = 2;
  344. spellSite = 0;
  345. }
  346. computeSpellsPerArea();
  347. }
  348. void CSpellWindow::fspellsAllb()
  349. {
  350. if (selectedTab != 4) {
  351. turnPageRight();
  352. selectedTab = 4;
  353. spellSite = 0;
  354. }
  355. computeSpellsPerArea();
  356. }
  357. void CSpellWindow::fLcornerb()
  358. {
  359. if(spellSite>0) {
  360. turnPageLeft();
  361. --spellSite;
  362. }
  363. computeSpellsPerArea();
  364. }
  365. void CSpellWindow::fRcornerb()
  366. {
  367. if((spellSite + 1) < (battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab])) {
  368. turnPageRight();
  369. ++spellSite;
  370. }
  371. computeSpellsPerArea();
  372. }
  373. void CSpellWindow::show(SDL_Surface *to)
  374. {
  375. SDL_BlitSurface(background, NULL, to, &pos);
  376. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  377. std::ostringstream mana;
  378. mana<<myHero->mana;
  379. CSDL_Ext::printAtMiddle(mana.str(), pos.x+435, pos.y +426, FONT_SMALL, tytulowy, to);
  380. statusBar->show(to);
  381. //printing school images
  382. if(selectedTab!=4 && spellSite == 0)
  383. {
  384. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  385. }
  386. //printing corners
  387. if(spellSite!=0)
  388. {
  389. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  390. }
  391. if((spellSite+1) < (battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab]) )
  392. {
  393. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  394. }
  395. //printing spell info
  396. for(int b=0; b<12; ++b)
  397. {
  398. if(spellAreas[b]->mySpell == -1)
  399. continue;
  400. const CSpell * spell = &CGI->spellh->spells[spellAreas[b]->mySpell];
  401. //int b2 = -1; //TODO use me
  402. blitAt(spells->ourImages[spellAreas[b]->mySpell].bitmap, spellAreas[b]->pos.x, spellAreas[b]->pos.y, to);
  403. Uint8 bestSchool = -1,
  404. bestslvl = myHero->getSpellSchoolLevel( spell );
  405. if(spell->air)
  406. bestSchool = 0;
  407. if(spell->fire)
  408. bestSchool = 1;
  409. if(spell->water)
  410. bestSchool = 2;
  411. if(spell->earth)
  412. bestSchool = 3;
  413. //printing border (indicates level of magic school)
  414. blitAt(schoolBorders[bestSchool]->ourImages[bestslvl].bitmap, spellAreas[b]->pos.x, spellAreas[b]->pos.y, to);
  415. SDL_Color firstLineColor, secondLineColor;
  416. if(myInt->cb->getSpellCost(spell, myHero) > myHero->mana) //hero cannot cast this spell
  417. {
  418. firstLineColor = zwykly;
  419. secondLineColor = darkTitle;
  420. }
  421. else
  422. {
  423. firstLineColor = tytulowy;
  424. secondLineColor = zwykly;
  425. }
  426. //printing spell's name
  427. CSDL_Ext::printAtMiddle(spell->name, spellAreas[b]->pos.x + 39, spellAreas[b]->pos.y + 70, FONT_TINY, firstLineColor, to);
  428. //printing lvl
  429. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[171 + spell->level], spellAreas[b]->pos.x + 39, spellAreas[b]->pos.y + 82, FONT_TINY, secondLineColor, to);
  430. //printing cost
  431. std::ostringstream ss;
  432. ss<<CGI->generaltexth->allTexts[387]<<": "<<myInt->cb->getSpellCost(spell, myHero);
  433. CSDL_Ext::printAtMiddle(ss.str(), spellAreas[b]->pos.x + 39, spellAreas[b]->pos.y + 94, FONT_TINY, secondLineColor, to);
  434. }
  435. }
  436. class SpellbookSpellSorter
  437. {
  438. public:
  439. bool operator()(const int & a, const int & b)
  440. {
  441. CSpell A = CGI->spellh->spells[a];
  442. CSpell B = CGI->spellh->spells[b];
  443. if(A.level<B.level)
  444. return true;
  445. if(A.level>B.level)
  446. return false;
  447. if(A.air && !B.air)
  448. return true;
  449. if(!A.air && B.air)
  450. return false;
  451. if(A.fire && !B.fire)
  452. return true;
  453. if(!A.fire && B.fire)
  454. return false;
  455. if(A.water && !B.water)
  456. return true;
  457. if(!A.water && B.water)
  458. return false;
  459. if(A.earth && !B.earth)
  460. return true;
  461. if(!A.earth && B.earth)
  462. return false;
  463. return A.name < B.name;
  464. }
  465. } spellsorter;
  466. void CSpellWindow::computeSpellsPerArea()
  467. {
  468. std::vector<ui32> spellsCurSite;
  469. for(std::set<ui32>::const_iterator it = mySpells.begin(); it != mySpells.end(); ++it)
  470. {
  471. if(CGI->spellh->spells[*it].combatSpell ^ !battleSpellsOnly
  472. && ((CGI->spellh->spells[*it].air && selectedTab == 0) ||
  473. (CGI->spellh->spells[*it].fire && selectedTab == 1) ||
  474. (CGI->spellh->spells[*it].water && selectedTab == 2) ||
  475. (CGI->spellh->spells[*it].earth && selectedTab == 3) ||
  476. selectedTab == 4 )
  477. )
  478. {
  479. spellsCurSite.push_back(*it);
  480. }
  481. }
  482. std::sort(spellsCurSite.begin(), spellsCurSite.end(), spellsorter);
  483. if(selectedTab == 4)
  484. {
  485. if(spellsCurSite.size() > 12)
  486. {
  487. spellsCurSite = std::vector<ui32>(spellsCurSite.begin() + spellSite*12, spellsCurSite.end());
  488. if(spellsCurSite.size() > 12)
  489. {
  490. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  491. }
  492. }
  493. }
  494. else //selectedTab == 0, 1, 2 or 3
  495. {
  496. if(spellsCurSite.size() > 10)
  497. {
  498. if(spellSite == 0)
  499. {
  500. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  501. }
  502. else
  503. {
  504. spellsCurSite = std::vector<ui32>(spellsCurSite.begin() + (spellSite-1)*12 + 10, spellsCurSite.end());
  505. if(spellsCurSite.size() > 12)
  506. {
  507. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  508. }
  509. }
  510. }
  511. }
  512. //applying
  513. if(selectedTab == 4 || spellSite != 0)
  514. {
  515. for(size_t c=0; c<12; ++c)
  516. {
  517. if(c<spellsCurSite.size())
  518. {
  519. spellAreas[c]->mySpell = spellsCurSite[c];
  520. }
  521. else
  522. {
  523. spellAreas[c]->mySpell = -1;
  524. }
  525. }
  526. }
  527. else
  528. {
  529. spellAreas[0]->mySpell = -1;
  530. spellAreas[1]->mySpell = -1;
  531. for(size_t c=0; c<10; ++c)
  532. {
  533. if(c<spellsCurSite.size())
  534. spellAreas[c+2]->mySpell = spellsCurSite[c];
  535. else
  536. spellAreas[c+2]->mySpell = -1;
  537. }
  538. }
  539. }
  540. void CSpellWindow::activate()
  541. {
  542. activateKeys();
  543. exitBtn->activate();
  544. battleSpells->activate();
  545. adventureSpells->activate();
  546. manaPoints->activate();
  547. selectSpellsA->activate();
  548. selectSpellsE->activate();
  549. selectSpellsF->activate();
  550. selectSpellsW->activate();
  551. selectSpellsAll->activate();
  552. lCorner->activate();
  553. rCorner->activate();
  554. for(int g=0; g<12; ++g)
  555. {
  556. spellAreas[g]->activate();
  557. }
  558. }
  559. void CSpellWindow::deactivate()
  560. {
  561. deactivateKeys();
  562. exitBtn->deactivate();
  563. battleSpells->deactivate();
  564. adventureSpells->deactivate();
  565. manaPoints->deactivate();
  566. selectSpellsA->deactivate();
  567. selectSpellsE->deactivate();
  568. selectSpellsF->deactivate();
  569. selectSpellsW->deactivate();
  570. selectSpellsAll->deactivate();
  571. lCorner->deactivate();
  572. rCorner->deactivate();
  573. for(int g=0; g<12; ++g)
  574. {
  575. spellAreas[g]->deactivate();
  576. }
  577. }
  578. void CSpellWindow::turnPageLeft()
  579. {
  580. CGI->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  581. }
  582. void CSpellWindow::turnPageRight()
  583. {
  584. CGI->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  585. }
  586. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  587. {
  588. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  589. fexitb();
  590. }
  591. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  592. {
  593. this->pos = pos;
  594. this->owner = owner;
  595. }
  596. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  597. {
  598. if(!down && mySpell!=-1)
  599. {
  600. const CSpell *sp = &CGI->spellh->spells[mySpell];
  601. int spellCost = owner->myInt->cb->getSpellCost(sp, owner->myHero);
  602. //we will cast a spell
  603. if(owner->myInt->battleInt && owner->myInt->cb->battleCanCastSpell() && spellCost <= owner->myHero->mana) //if battle window is open
  604. {
  605. int spell = mySpell;
  606. owner->fexitb();
  607. owner->myInt->battleInt->castThisSpell(spell);
  608. }
  609. else //adventure spell
  610. {
  611. //insufficient mana
  612. if(spellCost > owner->myHero->mana)
  613. {
  614. char msgBuf[500];
  615. sprintf(msgBuf, CGI->generaltexth->allTexts[206].c_str(), spellCost, owner->myHero->mana);
  616. owner->myInt->showInfoDialog(std::string(msgBuf));
  617. }
  618. else
  619. {
  620. int spell = mySpell;
  621. const CGHeroInstance *h = owner->myHero;
  622. owner->fexitb();
  623. switch(spell)
  624. {
  625. case 0: //summon boat
  626. {
  627. int3 pos = h->bestLocation();
  628. if(pos.x < 0)
  629. {
  630. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[334]); //There is no place to put the boat.
  631. return;
  632. }
  633. }
  634. }
  635. LOCPLINT->cb->castSpell(h, spell);
  636. }
  637. }
  638. }
  639. }
  640. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  641. {
  642. if(down && mySpell != -1)
  643. {
  644. std::string dmgInfo;
  645. int causedDmg = owner->myInt->cb->estimateSpellDamage( &CGI->spellh->spells[mySpell] );
  646. if(causedDmg == 0)
  647. dmgInfo = "";
  648. else
  649. {
  650. dmgInfo = CGI->generaltexth->allTexts[343];
  651. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  652. }
  653. SDL_Surface *spellBox = CMessage::drawBoxTextBitmapSub(
  654. owner->myInt->playerID,
  655. CGI->spellh->spells[mySpell].descriptions[0] + dmgInfo, this->owner->spells->ourImages[mySpell].bitmap,
  656. CGI->spellh->spells[mySpell].name,30,30);
  657. CInfoPopup *vinya = new CInfoPopup(spellBox, true);
  658. GH.pushInt(vinya);
  659. }
  660. }
  661. void CSpellWindow::SpellArea::hover(bool on)
  662. {
  663. //Hoverable::hover(on);
  664. if(mySpell != -1)
  665. {
  666. if(on)
  667. {
  668. std::ostringstream ss;
  669. ss<<CGI->spellh->spells[mySpell].name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->spells[mySpell].level]<<")";
  670. owner->statusBar->print(ss.str());
  671. }
  672. else
  673. {
  674. owner->statusBar->clear();
  675. }
  676. }
  677. }
  678. void CSpellWindow::SpellArea::activate()
  679. {
  680. activateLClick();
  681. activateRClick();
  682. activateHover();
  683. }
  684. void CSpellWindow::SpellArea::deactivate()
  685. {
  686. deactivateLClick();
  687. deactivateRClick();
  688. deactivateHover();
  689. }