CSpellWindow.cpp 27 KB

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