2
0

CSpellWindow.cpp 27 KB

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