CSpellWindow.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. #include "StdInc.h"
  2. #include "CSpellWindow.h"
  3. #include "CAdvmapInterface.h"
  4. #include "GUIClasses.h"
  5. #include "InfoWindows.h"
  6. #include "../CBitmapHandler.h"
  7. #include "../CDefHandler.h"
  8. #include "../CGameInfo.h"
  9. #include "../CMessage.h"
  10. #include "../CMT.h"
  11. #include "../CPlayerInterface.h"
  12. #include "../CVideoHandler.h"
  13. #include "../Graphics.h"
  14. #include "../battle/CBattleInterface.h"
  15. #include "../gui/CAnimation.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/SDL_Extensions.h"
  18. #include "../widgets/MiscWidgets.h"
  19. #include "../widgets/CComponent.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/BattleState.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/CHeroHandler.h"
  25. #include "../../lib/spells/CSpellHandler.h"
  26. #include "../../lib/GameConstants.h"
  27. #include "../../lib/CGameState.h"
  28. #include "../../lib/mapObjects/CGTownInstance.h"
  29. /*
  30. * CSpellWindow.cpp, part of VCMI engine
  31. *
  32. * Authors: listed in file AUTHORS in main folder
  33. *
  34. * License: GNU General Public License v2.0 or later
  35. * Full text of license available in license.txt file, in main folder
  36. *
  37. */
  38. SpellbookInteractiveArea::SpellbookInteractiveArea(const SDL_Rect & myRect, std::function<void()> funcL,
  39. const std::string & textR, std::function<void()> funcHon, std::function<void()> funcHoff, CPlayerInterface * _myInt)
  40. {
  41. addUsedEvents(LCLICK | RCLICK | HOVER);
  42. pos = myRect;
  43. onLeft = funcL;
  44. textOnRclick = textR;
  45. onHoverOn = funcHon;
  46. onHoverOff = funcHoff;
  47. myInt = _myInt;
  48. }
  49. void SpellbookInteractiveArea::clickLeft(tribool down, bool previousState)
  50. {
  51. if(!down)
  52. {
  53. onLeft();
  54. }
  55. }
  56. void SpellbookInteractiveArea::clickRight(tribool down, bool previousState)
  57. {
  58. adventureInt->handleRightClick(textOnRclick, down);
  59. }
  60. void SpellbookInteractiveArea::hover(bool on)
  61. {
  62. //Hoverable::hover(on);
  63. if(on)
  64. {
  65. onHoverOn();
  66. }
  67. else
  68. {
  69. onHoverOff();
  70. }
  71. }
  72. CSpellWindow::CSpellWindow(const SDL_Rect &, const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  73. CWindowObject(PLAYER_COLORED, "SpelBack"),
  74. battleSpellsOnly(openOnBattleSpells),
  75. selectedTab(4),
  76. currentPage(0),
  77. myHero(_myHero),
  78. myInt(_myInt)
  79. {
  80. //initializing castable spells
  81. for(ui32 v=0; v<CGI->spellh->objects.size(); ++v)
  82. {
  83. if( !CGI->spellh->objects[v]->creatureAbility && myHero->canCastThisSpell(CGI->spellh->objects[v]) )
  84. mySpells.insert(SpellID(v));
  85. }
  86. //initializing sizes of spellbook's parts
  87. for(auto & elem : sitesPerTabAdv)
  88. elem = 0;
  89. for(auto & elem : sitesPerTabBattle)
  90. elem = 0;
  91. for(auto g : mySpells)
  92. {
  93. const CSpell &s = *CGI->spellh->objects[g];
  94. Uint8 *sitesPerOurTab = s.combatSpell ? sitesPerTabBattle : sitesPerTabAdv;
  95. ++sitesPerOurTab[4];
  96. s.forEachSchool([&sitesPerOurTab](const SpellSchoolInfo & school, bool & stop)
  97. {
  98. ++sitesPerOurTab[(ui8)school.id];
  99. });
  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. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  135. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  136. spells = new CAnimation("Spells.def");
  137. spellTab = CDefHandler::giveDef("SpelTab.def");
  138. schools = CDefHandler::giveDef("Schools.def");
  139. schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
  140. schoolBorders[1] = CDefHandler::giveDef("SplevF.def");
  141. schoolBorders[2] = CDefHandler::giveDef("SplevW.def");
  142. schoolBorders[3] = CDefHandler::giveDef("SplevE.def");
  143. statusBar = new CGStatusBar(7 + pos.x, 569 + pos.y, "Spelroll.bmp");
  144. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  145. exitBtn = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::fexitb, this), CGI->generaltexth->zelp[460].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[460].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  146. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  147. battleSpells = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::fbattleSpellsb, this), CGI->generaltexth->zelp[453].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[453].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  148. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  149. adventureSpells = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::fadvSpellsb, this), CGI->generaltexth->zelp[452].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[452].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  150. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  151. manaPoints = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::fmanaPtsb, this), CGI->generaltexth->zelp[459].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[459].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  152. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  153. selectSpellsA = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 0), CGI->generaltexth->zelp[454].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[454].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  154. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  155. selectSpellsE = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 3), CGI->generaltexth->zelp[457].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[457].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  156. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  157. selectSpellsF = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 1), CGI->generaltexth->zelp[455].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[455].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  158. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  159. selectSpellsW = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 2), CGI->generaltexth->zelp[456].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[456].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  160. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  161. selectSpellsAll = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), CGI->generaltexth->zelp[458].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[458].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  162. temp_rect = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y);
  163. lCorner = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), CGI->generaltexth->zelp[450].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[450].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  164. temp_rect = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y);
  165. rCorner = new SpellbookInteractiveArea(temp_rect, std::bind(&CSpellWindow::fRcornerb, this), CGI->generaltexth->zelp[451].second, std::bind(&CGStatusBar::setText, statusBar, (CGI->generaltexth->zelp[451].first)), std::bind(&CGStatusBar::clear, statusBar), myInt);
  166. //areas for spells
  167. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  168. for(int v=0; v<12; ++v)
  169. {
  170. temp_rect = genRect(65, 78, xpos, ypos);
  171. spellAreas[v] = new SpellArea(temp_rect, this);
  172. if(v == 5) //to right page
  173. {
  174. xpos = 336 + pos.x; ypos = 90 + pos.y;
  175. }
  176. else
  177. {
  178. if(v%2 == 0)
  179. {
  180. xpos+=85;
  181. }
  182. else
  183. {
  184. xpos -= 85; ypos+=97;
  185. }
  186. }
  187. }
  188. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  189. currentPage = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  190. // spellbook last page battle index is not reset after battle, so this needs to stay here
  191. vstd::abetween(currentPage, 0, pagesWithinCurrentTab() - 1);
  192. computeSpellsPerArea();
  193. addUsedEvents(KEYBOARD);
  194. }
  195. CSpellWindow::~CSpellWindow()
  196. {
  197. SDL_FreeSurface(leftCorner);
  198. SDL_FreeSurface(rightCorner);
  199. spells->unload();
  200. delete spells;
  201. delete spellTab;
  202. delete schools;
  203. for(auto & elem : schoolBorders)
  204. delete elem;
  205. delete exitBtn;
  206. delete battleSpells;
  207. delete adventureSpells;
  208. delete manaPoints;
  209. delete statusBar;
  210. delete selectSpellsA;
  211. delete selectSpellsE;
  212. delete selectSpellsF;
  213. delete selectSpellsW;
  214. delete selectSpellsAll;
  215. delete lCorner;
  216. delete rCorner;
  217. for(auto & elem : spellAreas)
  218. {
  219. delete elem;
  220. }
  221. }
  222. void CSpellWindow::fexitb()
  223. {
  224. (LOCPLINT->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  225. (LOCPLINT->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  226. GH.popIntTotally(this);
  227. }
  228. void CSpellWindow::fadvSpellsb()
  229. {
  230. if (battleSpellsOnly == true)
  231. {
  232. turnPageRight();
  233. battleSpellsOnly = false;
  234. currentPage = 0;
  235. }
  236. computeSpellsPerArea();
  237. }
  238. void CSpellWindow::fbattleSpellsb()
  239. {
  240. if (battleSpellsOnly == false)
  241. {
  242. turnPageLeft();
  243. battleSpellsOnly = true;
  244. currentPage = 0;
  245. }
  246. computeSpellsPerArea();
  247. }
  248. void CSpellWindow::fmanaPtsb()
  249. {
  250. }
  251. void CSpellWindow::selectSchool(int school)
  252. {
  253. if (selectedTab != school)
  254. {
  255. if (selectedTab < school)
  256. turnPageLeft();
  257. else
  258. turnPageRight();
  259. selectedTab = school;
  260. currentPage = 0;
  261. }
  262. computeSpellsPerArea();
  263. }
  264. void CSpellWindow::fLcornerb()
  265. {
  266. if(currentPage>0)
  267. {
  268. turnPageLeft();
  269. --currentPage;
  270. }
  271. computeSpellsPerArea();
  272. GH.breakEventHandling();
  273. }
  274. void CSpellWindow::fRcornerb()
  275. {
  276. if((currentPage + 1) < (pagesWithinCurrentTab()))
  277. {
  278. turnPageRight();
  279. ++currentPage;
  280. }
  281. computeSpellsPerArea();
  282. GH.breakEventHandling();
  283. }
  284. void CSpellWindow::showAll(SDL_Surface * to)
  285. {
  286. CWindowObject::showAll(to);
  287. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  288. std::ostringstream mana;
  289. mana<<myHero->mana;
  290. printAtMiddleLoc(mana.str(), 435, 426, FONT_SMALL, Colors::YELLOW, to);
  291. statusBar->showAll(to);
  292. //printing school images
  293. if(selectedTab!=4 && currentPage == 0)
  294. {
  295. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  296. }
  297. //printing corners
  298. if(currentPage!=0)
  299. {
  300. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  301. }
  302. if((currentPage+1) < (pagesWithinCurrentTab()) )
  303. {
  304. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  305. }
  306. //printing spell info
  307. for(auto & elem : spellAreas)
  308. {
  309. elem->showAll(to);
  310. }
  311. }
  312. void CSpellWindow::show(SDL_Surface * to)
  313. {
  314. statusBar->show(to);
  315. }
  316. class SpellbookSpellSorter
  317. {
  318. public:
  319. bool operator()(const int & a, const int & b)
  320. {
  321. const CSpell & A = *CGI->spellh->objects[a];
  322. const CSpell & B = *CGI->spellh->objects[b];
  323. if(A.level<B.level)
  324. return true;
  325. if(A.level>B.level)
  326. return false;
  327. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  328. {
  329. if(A.school.at((ESpellSchool)schoolId) && !B.school.at((ESpellSchool)schoolId))
  330. return true;
  331. if(!A.school.at((ESpellSchool)schoolId) && B.school.at((ESpellSchool)schoolId))
  332. return false;
  333. }
  334. return A.name < B.name;
  335. }
  336. } spellsorter;
  337. void CSpellWindow::computeSpellsPerArea()
  338. {
  339. std::vector<SpellID> spellsCurSite;
  340. for(const SpellID & spellID : mySpells)
  341. {
  342. CSpell * s = spellID.toSpell();
  343. if(s->combatSpell ^ !battleSpellsOnly
  344. && ((selectedTab == 4) || (s->school[(ESpellSchool)selectedTab]))
  345. )
  346. {
  347. spellsCurSite.push_back(spellID);
  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<SpellID>(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<SpellID>(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(SpellID::NONE);
  392. }
  393. }
  394. }
  395. else
  396. {
  397. spellAreas[0]->setSpell(SpellID::NONE);
  398. spellAreas[1]->setSpell(SpellID::NONE);
  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(SpellID::NONE);
  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(auto & elem : spellAreas)
  422. {
  423. elem->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(auto & elem : spellAreas)
  441. {
  442. elem->deactivate();
  443. }
  444. lCorner->deactivate();
  445. rCorner->deactivate();
  446. }
  447. void CSpellWindow::turnPageLeft()
  448. {
  449. if (settings["video"]["spellbookAnimation"].Bool())
  450. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  451. }
  452. void CSpellWindow::turnPageRight()
  453. {
  454. if (settings["video"]["spellbookAnimation"].Bool())
  455. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  456. }
  457. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  458. {
  459. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  460. fexitb();
  461. if(key.state == SDL_PRESSED)
  462. {
  463. switch(key.keysym.sym)
  464. {
  465. case SDLK_LEFT:
  466. fLcornerb();
  467. break;
  468. case SDLK_RIGHT:
  469. fRcornerb();
  470. break;
  471. case SDLK_UP:
  472. case SDLK_DOWN:
  473. {
  474. bool down = key.keysym.sym == SDLK_DOWN;
  475. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  476. int index = -1;
  477. while(schoolsOrder[++index] != selectedTab);
  478. index += (down ? 1 : -1);
  479. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  480. if(selectedTab != schoolsOrder[index])
  481. selectSchool(schoolsOrder[index]);
  482. break;
  483. }
  484. case SDLK_c:
  485. fbattleSpellsb();
  486. break;
  487. case SDLK_a:
  488. fadvSpellsb();
  489. break;
  490. default://to get rid of warnings
  491. break;
  492. }
  493. //alt + 1234567890-= casts spell from 1 - 12 slot
  494. if(LOCPLINT->altPressed())
  495. {
  496. SDLKey hlpKey = key.keysym.sym;
  497. if(CGuiHandler::isNumKey(hlpKey, false))
  498. {
  499. if(hlpKey == SDLK_KP_PLUS)
  500. hlpKey = SDLK_EQUALS;
  501. else
  502. hlpKey = CGuiHandler::numToDigit(hlpKey);
  503. }
  504. 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};
  505. int index = -1;
  506. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  507. if(index >= ARRAY_COUNT(spellSelectors))
  508. return;
  509. //try casting spell
  510. spellAreas[index]->clickLeft(false, true);
  511. }
  512. }
  513. }
  514. Uint8 CSpellWindow::pagesWithinCurrentTab()
  515. {
  516. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  517. }
  518. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  519. {
  520. this->pos = pos;
  521. this->owner = owner;
  522. addUsedEvents(LCLICK | RCLICK | HOVER);
  523. spellCost = whichSchool = schoolLevel = -1;
  524. mySpell = SpellID::NONE;
  525. }
  526. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  527. {
  528. if(!down && mySpell != SpellID::NONE)
  529. {
  530. const CSpell * sp = mySpell.toSpell();
  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->isCombatSpell() && !owner->myInt->battleInt)
  541. || (sp->isAdventureSpell() && owner->myInt->battleInt))
  542. {
  543. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell, 0));
  544. LOCPLINT->showInfoDialog(sp->getLevelInfo(schoolLevel).description, 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. owner->fexitb();
  556. owner->myInt->battleInt->castThisSpell(mySpell);
  557. }
  558. break;
  559. case ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED:
  560. {
  561. std::string text = CGI->generaltexth->allTexts[538], summoner, elemental, caster;
  562. std::vector<const CStack *> stacks = owner->myInt->cb->battleGetStacks();
  563. for(const CStack * s : stacks)
  564. {
  565. if(vstd::contains(s->state, EBattleStackState::SUMMONED))
  566. {
  567. elemental = s->getCreature()->namePl;
  568. summoner = owner->myInt->cb->battleGetHeroInfo(!s->attackerOwned).name;
  569. break;
  570. }
  571. }
  572. if (owner->myHero->type->sex)
  573. { //female
  574. caster = CGI->generaltexth->allTexts[540];
  575. }
  576. else
  577. { //male
  578. caster = CGI->generaltexth->allTexts[539];
  579. }
  580. text = boost::str(boost::format(text) % summoner % elemental % caster);
  581. owner->myInt->showInfoDialog(text);
  582. }
  583. break;
  584. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  585. {
  586. //Recanter's Cloak or similar effect. Try to retrieve bonus
  587. const Bonus *b = owner->myHero->getBonusLocalFirst(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
  588. //TODO what about other values and non-artifact sources?
  589. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  590. {
  591. std::string artName = CGI->arth->artifacts[b->sid]->Name();
  592. //The %s prevents %s from casting 3rd level or higher spells.
  593. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[536])
  594. % artName % owner->myHero->name));
  595. }
  596. else
  597. {
  598. // General message:
  599. // %s recites the incantations but they seem to have no effect.
  600. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  601. text = boost::str(boost::format(text) % caster);
  602. owner->myInt->showInfoDialog(text);
  603. }
  604. }
  605. break;
  606. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  607. {
  608. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[185]);
  609. }
  610. break;
  611. }
  612. }
  613. else if(sp->isAdventureSpell() && !owner->myInt->battleInt) //adventure spell and not in battle
  614. {
  615. const CGHeroInstance *h = owner->myHero;
  616. owner->fexitb();
  617. if(mySpell == SpellID::TOWN_PORTAL)
  618. {
  619. //special case
  620. //todo: move to mechanics
  621. std::vector <int> availableTowns;
  622. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
  623. if (Towns.empty())
  624. {
  625. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  626. return;
  627. }
  628. if (h->getSpellSchoolLevel(sp) < 2) //not advanced or expert - teleport to nearest available city
  629. {
  630. auto nearest = Towns.cbegin(); //nearest town's iterator
  631. si32 dist = LOCPLINT->cb->getTown((*nearest)->id)->pos.dist2dSQ(h->pos);
  632. for (auto i = nearest + 1; i != Towns.cend(); ++i)
  633. {
  634. const CGTownInstance * dest = LOCPLINT->cb->getTown((*i)->id);
  635. si32 curDist = dest->pos.dist2dSQ(h->pos);
  636. if (curDist < dist)
  637. {
  638. nearest = i;
  639. dist = curDist;
  640. }
  641. }
  642. if ((*nearest)->visitingHero)
  643. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[123]);
  644. else
  645. {
  646. const CGTownInstance * town = LOCPLINT->cb->getTown((*nearest)->id);
  647. LOCPLINT->cb->castSpell(h, mySpell, town->visitablePos());// - town->getVisitableOffset());
  648. }
  649. }
  650. else
  651. { //let the player choose
  652. for(auto & Town : Towns)
  653. {
  654. const CGTownInstance *t = Town;
  655. if (t->visitingHero == nullptr) //empty town and this is
  656. {
  657. availableTowns.push_back(t->id.getNum());//add to the list
  658. }
  659. }
  660. auto castTownPortal = [h](int townId)
  661. {
  662. const CGTownInstance * dest = LOCPLINT->cb->getTown(ObjectInstanceID(townId));
  663. LOCPLINT->cb->castSpell(h, SpellID::TOWN_PORTAL, dest->visitablePos());
  664. };
  665. if (availableTowns.empty())
  666. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[124]);
  667. else
  668. GH.pushInt (new CObjectListWindow(availableTowns,
  669. new CAnimImage("SPELLSCR",mySpell),
  670. CGI->generaltexth->jktexts[40], CGI->generaltexth->jktexts[41],
  671. castTownPortal));
  672. }
  673. return;
  674. }
  675. if(mySpell == SpellID::SUMMON_BOAT)
  676. {
  677. //special case
  678. //todo: move to mechanics
  679. int3 pos = h->bestLocation();
  680. if(pos.x < 0)
  681. {
  682. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[334]); //There is no place to put the boat.
  683. return;
  684. }
  685. }
  686. if(sp->getTargetType() == CSpell::LOCATION)
  687. {
  688. adventureInt->enterCastingMode(sp);
  689. }
  690. else if(sp->getTargetType() == CSpell::NO_TARGET)
  691. {
  692. LOCPLINT->cb->castSpell(h, mySpell);
  693. }
  694. else
  695. {
  696. logGlobal->error("Invalid spell target type");
  697. }
  698. }
  699. }
  700. }
  701. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  702. {
  703. if(down && mySpell != -1)
  704. {
  705. std::string dmgInfo;
  706. const CGHeroInstance * hero = owner->myHero;
  707. int causedDmg = owner->myInt->cb->estimateSpellDamage( CGI->spellh->objects[mySpell], (hero ? hero : nullptr));
  708. if(causedDmg == 0 || mySpell == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  709. dmgInfo = "";
  710. else
  711. {
  712. dmgInfo = CGI->generaltexth->allTexts[343];
  713. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  714. }
  715. CRClickPopup::createAndPush(CGI->spellh->objects[mySpell]->getLevelInfo(schoolLevel).description + dmgInfo,
  716. new CComponent(CComponent::spell, mySpell));
  717. }
  718. }
  719. void CSpellWindow::SpellArea::hover(bool on)
  720. {
  721. //Hoverable::hover(on);
  722. if(mySpell != -1)
  723. {
  724. if(on)
  725. {
  726. std::ostringstream ss;
  727. ss<<CGI->spellh->objects[mySpell]->name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->objects[mySpell]->level]<<")";
  728. owner->statusBar->setText(ss.str());
  729. }
  730. else
  731. {
  732. owner->statusBar->clear();
  733. }
  734. }
  735. }
  736. void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
  737. {
  738. if(mySpell < 0)
  739. return;
  740. const CSpell * spell = mySpell.toSpell();
  741. owner->spells->load(mySpell);
  742. IImage * icon = owner->spells->getImage(mySpell,0,false);
  743. if(icon != nullptr)
  744. icon->draw(to, pos.x, pos.y);
  745. else
  746. logGlobal->errorStream() << __FUNCTION__ << ": failed to load spell icon for spell with id " << mySpell;
  747. blitAt(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->ourImages[schoolLevel].bitmap, pos.x, pos.y, to); //printing border (indicates level of magic school)
  748. SDL_Color firstLineColor, secondLineColor;
  749. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  750. {
  751. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  752. firstLineColor = Colors::WHITE;
  753. secondLineColor = unavailableSpell;
  754. }
  755. else
  756. {
  757. firstLineColor = Colors::YELLOW;
  758. secondLineColor = Colors::WHITE;
  759. }
  760. //printing spell's name
  761. printAtMiddleLoc(spell->name, 39, 70, FONT_TINY, firstLineColor, to);
  762. //printing lvl
  763. printAtMiddleLoc(CGI->generaltexth->allTexts[171 + spell->level], 39, 82, FONT_TINY, secondLineColor, to);
  764. //printing cost
  765. std::ostringstream ss;
  766. ss << CGI->generaltexth->allTexts[387] << ": " << spellCost;
  767. printAtMiddleLoc(ss.str(), 39, 94, FONT_TINY, secondLineColor, to);
  768. }
  769. void CSpellWindow::SpellArea::setSpell(SpellID spellID)
  770. {
  771. mySpell = spellID;
  772. if(mySpell < 0)
  773. return;
  774. const CSpell * spell = CGI->spellh->objects[mySpell];
  775. schoolLevel = owner->myHero->getSpellSchoolLevel(spell, &whichSchool);
  776. spellCost = owner->myInt->cb->getSpellCost(spell, owner->myHero);
  777. }