CSpellWindow.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. CSpellWindow::InteractiveArea::InteractiveArea(const SDL_Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  41. {
  42. addUsedEvents(LCLICK | RCLICK | HOVER);
  43. pos = myRect;
  44. onLeft = funcL;
  45. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  46. helpText = CGI->generaltexth->zelp[helpTextId].second;
  47. owner = _owner;
  48. }
  49. void CSpellWindow::InteractiveArea::clickLeft(tribool down, bool previousState)
  50. {
  51. if(!down)
  52. onLeft();
  53. }
  54. void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState)
  55. {
  56. adventureInt->handleRightClick(helpText, down);
  57. }
  58. void CSpellWindow::InteractiveArea::hover(bool on)
  59. {
  60. if(on)
  61. owner->statusBar->setText(hoverText);
  62. else
  63. owner->statusBar->clear();
  64. }
  65. CSpellWindow::CSpellWindow(const SDL_Rect &, const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  66. CWindowObject(PLAYER_COLORED, "SpelBack"),
  67. battleSpellsOnly(openOnBattleSpells),
  68. selectedTab(4),
  69. currentPage(0),
  70. myHero(_myHero),
  71. myInt(_myInt)
  72. {
  73. //initializing castable spells
  74. for(const auto & spell : CGI->spellh->objects)
  75. {
  76. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell.get()))
  77. mySpells.insert(spell->id);
  78. }
  79. //initializing sizes of spellbook's parts
  80. for(auto & elem : sitesPerTabAdv)
  81. elem = 0;
  82. for(auto & elem : sitesPerTabBattle)
  83. elem = 0;
  84. for(const auto & g : mySpells)
  85. {
  86. const CSpell * spell = g.toSpell();
  87. int * sitesPerOurTab = spell->isCombatSpell() ? sitesPerTabBattle : sitesPerTabAdv;
  88. ++sitesPerOurTab[4];
  89. spell->forEachSchool([&sitesPerOurTab](const SpellSchoolInfo & school, bool & stop)
  90. {
  91. ++sitesPerOurTab[(ui8)school.id];
  92. });
  93. }
  94. if(sitesPerTabAdv[4] % 12 == 0)
  95. sitesPerTabAdv[4]/=12;
  96. else
  97. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  98. for(int v=0; v<4; ++v)
  99. {
  100. if(sitesPerTabAdv[v] <= 10)
  101. sitesPerTabAdv[v] = 1;
  102. else
  103. {
  104. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  105. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  106. else
  107. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  108. }
  109. }
  110. if(sitesPerTabBattle[4] % 12 == 0)
  111. sitesPerTabBattle[4]/=12;
  112. else
  113. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  114. for(int v=0; v<4; ++v)
  115. {
  116. if(sitesPerTabBattle[v] <= 10)
  117. sitesPerTabBattle[v] = 1;
  118. else
  119. {
  120. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  121. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  122. else
  123. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  124. }
  125. }
  126. //numbers of spell pages computed
  127. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  128. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  129. spells = new CAnimation("Spells.def");
  130. spellTab = CDefHandler::giveDef("SpelTab.def");
  131. schools = CDefHandler::giveDef("Schools.def");
  132. schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
  133. schoolBorders[1] = CDefHandler::giveDef("SplevF.def");
  134. schoolBorders[2] = CDefHandler::giveDef("SplevW.def");
  135. schoolBorders[3] = CDefHandler::giveDef("SplevE.def");
  136. statusBar = new CGStatusBar(7 + pos.x, 569 + pos.y, "Spelroll.bmp");
  137. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  138. exitBtn = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fexitb, this), 460, this);
  139. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  140. battleSpells = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this);
  141. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  142. adventureSpells = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fadvSpellsb, this), 452, this);
  143. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  144. manaPoints = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fmanaPtsb, this), 459, this);
  145. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  146. selectSpellsA = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 0), 454, this);
  147. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  148. selectSpellsE = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 3), 457, this);
  149. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  150. selectSpellsF = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 1), 455, this);
  151. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  152. selectSpellsW = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 2), 456, this);
  153. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  154. selectSpellsAll = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this);
  155. temp_rect = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y);
  156. lCorner = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), 450, this);
  157. temp_rect = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y);
  158. rCorner = new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fRcornerb, this), 451, this);
  159. //areas for spells
  160. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  161. for(int v=0; v<12; ++v)
  162. {
  163. temp_rect = genRect(65, 78, xpos, ypos);
  164. spellAreas[v] = new SpellArea(temp_rect, this);
  165. if(v == 5) //to right page
  166. {
  167. xpos = 336 + pos.x; ypos = 90 + pos.y;
  168. }
  169. else
  170. {
  171. if(v%2 == 0)
  172. {
  173. xpos+=85;
  174. }
  175. else
  176. {
  177. xpos -= 85; ypos+=97;
  178. }
  179. }
  180. }
  181. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  182. currentPage = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  183. // spellbook last page battle index is not reset after battle, so this needs to stay here
  184. vstd::abetween(currentPage, 0, std::max(0, pagesWithinCurrentTab() - 1));
  185. computeSpellsPerArea();
  186. addUsedEvents(KEYBOARD);
  187. }
  188. CSpellWindow::~CSpellWindow()
  189. {
  190. SDL_FreeSurface(leftCorner);
  191. SDL_FreeSurface(rightCorner);
  192. spells->unload();
  193. delete spells;
  194. delete spellTab;
  195. delete schools;
  196. for(auto & elem : schoolBorders)
  197. delete elem;
  198. delete exitBtn;
  199. delete battleSpells;
  200. delete adventureSpells;
  201. delete manaPoints;
  202. delete statusBar;
  203. delete selectSpellsA;
  204. delete selectSpellsE;
  205. delete selectSpellsF;
  206. delete selectSpellsW;
  207. delete selectSpellsAll;
  208. delete lCorner;
  209. delete rCorner;
  210. for(auto & elem : spellAreas)
  211. {
  212. delete elem;
  213. }
  214. }
  215. void CSpellWindow::fexitb()
  216. {
  217. (LOCPLINT->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  218. (LOCPLINT->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  219. GH.popIntTotally(this);
  220. }
  221. void CSpellWindow::fadvSpellsb()
  222. {
  223. if (battleSpellsOnly == true)
  224. {
  225. turnPageRight();
  226. battleSpellsOnly = false;
  227. currentPage = 0;
  228. }
  229. computeSpellsPerArea();
  230. }
  231. void CSpellWindow::fbattleSpellsb()
  232. {
  233. if (battleSpellsOnly == false)
  234. {
  235. turnPageLeft();
  236. battleSpellsOnly = true;
  237. currentPage = 0;
  238. }
  239. computeSpellsPerArea();
  240. }
  241. void CSpellWindow::fmanaPtsb()
  242. {
  243. }
  244. void CSpellWindow::selectSchool(int school)
  245. {
  246. if (selectedTab != school)
  247. {
  248. if (selectedTab < school)
  249. turnPageLeft();
  250. else
  251. turnPageRight();
  252. selectedTab = school;
  253. currentPage = 0;
  254. }
  255. computeSpellsPerArea();
  256. }
  257. void CSpellWindow::fLcornerb()
  258. {
  259. if(currentPage>0)
  260. {
  261. turnPageLeft();
  262. --currentPage;
  263. }
  264. computeSpellsPerArea();
  265. GH.breakEventHandling();
  266. }
  267. void CSpellWindow::fRcornerb()
  268. {
  269. if((currentPage + 1) < (pagesWithinCurrentTab()))
  270. {
  271. turnPageRight();
  272. ++currentPage;
  273. }
  274. computeSpellsPerArea();
  275. GH.breakEventHandling();
  276. }
  277. void CSpellWindow::showAll(SDL_Surface * to)
  278. {
  279. CWindowObject::showAll(to);
  280. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  281. std::ostringstream mana;
  282. mana<<myHero->mana;
  283. printAtMiddleLoc(mana.str(), 435, 426, FONT_SMALL, Colors::YELLOW, to);
  284. statusBar->showAll(to);
  285. //printing school images
  286. if(selectedTab!=4 && currentPage == 0)
  287. {
  288. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  289. }
  290. //printing corners
  291. if(currentPage!=0)
  292. {
  293. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  294. }
  295. if((currentPage+1) < (pagesWithinCurrentTab()) )
  296. {
  297. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  298. }
  299. //printing spell info
  300. for(auto & elem : spellAreas)
  301. {
  302. elem->showAll(to);
  303. }
  304. }
  305. void CSpellWindow::show(SDL_Surface * to)
  306. {
  307. statusBar->show(to);
  308. }
  309. class SpellbookSpellSorter
  310. {
  311. public:
  312. bool operator()(const SpellID & a, const SpellID & b)
  313. {
  314. const CSpell & A = *(a.toSpell());
  315. const CSpell & B = *(b.toSpell());
  316. if(A.level<B.level)
  317. return true;
  318. if(A.level>B.level)
  319. return false;
  320. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  321. {
  322. if(A.school.at((ESpellSchool)schoolId) && !B.school.at((ESpellSchool)schoolId))
  323. return true;
  324. if(!A.school.at((ESpellSchool)schoolId) && B.school.at((ESpellSchool)schoolId))
  325. return false;
  326. }
  327. return A.name < B.name;
  328. }
  329. } spellsorter;
  330. void CSpellWindow::computeSpellsPerArea()
  331. {
  332. std::vector<SpellID> spellsCurSite;
  333. for(const SpellID & spellID : mySpells)
  334. {
  335. const CSpell * s = spellID.toSpell();
  336. if(s->combatSpell ^ !battleSpellsOnly
  337. && ((selectedTab == 4) || s->school.at((ESpellSchool)selectedTab))
  338. )
  339. {
  340. spellsCurSite.push_back(spellID);
  341. }
  342. }
  343. std::sort(spellsCurSite.begin(), spellsCurSite.end(), spellsorter);
  344. if(selectedTab == 4)
  345. {
  346. if(spellsCurSite.size() > 12)
  347. {
  348. spellsCurSite = std::vector<SpellID>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  349. if(spellsCurSite.size() > 12)
  350. {
  351. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  352. }
  353. }
  354. }
  355. else //selectedTab == 0, 1, 2 or 3
  356. {
  357. if(spellsCurSite.size() > 10)
  358. {
  359. if(currentPage == 0)
  360. {
  361. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  362. }
  363. else
  364. {
  365. spellsCurSite = std::vector<SpellID>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  366. if(spellsCurSite.size() > 12)
  367. {
  368. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  369. }
  370. }
  371. }
  372. }
  373. //applying
  374. if(selectedTab == 4 || currentPage != 0)
  375. {
  376. for(size_t c=0; c<12; ++c)
  377. {
  378. if(c<spellsCurSite.size())
  379. {
  380. spellAreas[c]->setSpell(spellsCurSite[c]);
  381. }
  382. else
  383. {
  384. spellAreas[c]->setSpell(SpellID::NONE);
  385. }
  386. }
  387. }
  388. else
  389. {
  390. spellAreas[0]->setSpell(SpellID::NONE);
  391. spellAreas[1]->setSpell(SpellID::NONE);
  392. for(size_t c=0; c<10; ++c)
  393. {
  394. if(c<spellsCurSite.size())
  395. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  396. else
  397. spellAreas[c+2]->setSpell(SpellID::NONE);
  398. }
  399. }
  400. redraw();
  401. }
  402. void CSpellWindow::activate()
  403. {
  404. CIntObject::activate();
  405. exitBtn->activate();
  406. battleSpells->activate();
  407. adventureSpells->activate();
  408. manaPoints->activate();
  409. selectSpellsA->activate();
  410. selectSpellsE->activate();
  411. selectSpellsF->activate();
  412. selectSpellsW->activate();
  413. selectSpellsAll->activate();
  414. for(auto & elem : spellAreas)
  415. {
  416. elem->activate();
  417. }
  418. lCorner->activate();
  419. rCorner->activate();
  420. }
  421. void CSpellWindow::deactivate()
  422. {
  423. CIntObject::deactivate();
  424. exitBtn->deactivate();
  425. battleSpells->deactivate();
  426. adventureSpells->deactivate();
  427. manaPoints->deactivate();
  428. selectSpellsA->deactivate();
  429. selectSpellsE->deactivate();
  430. selectSpellsF->deactivate();
  431. selectSpellsW->deactivate();
  432. selectSpellsAll->deactivate();
  433. for(auto & elem : spellAreas)
  434. {
  435. elem->deactivate();
  436. }
  437. lCorner->deactivate();
  438. rCorner->deactivate();
  439. }
  440. void CSpellWindow::turnPageLeft()
  441. {
  442. if (settings["video"]["spellbookAnimation"].Bool())
  443. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  444. }
  445. void CSpellWindow::turnPageRight()
  446. {
  447. if (settings["video"]["spellbookAnimation"].Bool())
  448. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  449. }
  450. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  451. {
  452. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  453. {
  454. fexitb();
  455. return;
  456. }
  457. if(key.state == SDL_PRESSED)
  458. {
  459. switch(key.keysym.sym)
  460. {
  461. case SDLK_LEFT:
  462. fLcornerb();
  463. break;
  464. case SDLK_RIGHT:
  465. fRcornerb();
  466. break;
  467. case SDLK_UP:
  468. case SDLK_DOWN:
  469. {
  470. bool down = key.keysym.sym == SDLK_DOWN;
  471. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  472. int index = -1;
  473. while(schoolsOrder[++index] != selectedTab);
  474. index += (down ? 1 : -1);
  475. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  476. if(selectedTab != schoolsOrder[index])
  477. selectSchool(schoolsOrder[index]);
  478. break;
  479. }
  480. case SDLK_c:
  481. fbattleSpellsb();
  482. break;
  483. case SDLK_a:
  484. fadvSpellsb();
  485. break;
  486. default://to get rid of warnings
  487. break;
  488. }
  489. //alt + 1234567890-= casts spell from 1 - 12 slot
  490. if(LOCPLINT->altPressed())
  491. {
  492. SDL_Keycode hlpKey = key.keysym.sym;
  493. if(CGuiHandler::isNumKey(hlpKey, false))
  494. {
  495. if(hlpKey == SDLK_KP_PLUS)
  496. hlpKey = SDLK_EQUALS;
  497. else
  498. hlpKey = CGuiHandler::numToDigit(hlpKey);
  499. }
  500. 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};
  501. int index = -1;
  502. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  503. if(index >= ARRAY_COUNT(spellSelectors))
  504. return;
  505. //try casting spell
  506. spellAreas[index]->clickLeft(false, true);
  507. }
  508. }
  509. }
  510. int CSpellWindow::pagesWithinCurrentTab()
  511. {
  512. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  513. }
  514. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  515. {
  516. this->pos = pos;
  517. this->owner = owner;
  518. addUsedEvents(LCLICK | RCLICK | HOVER);
  519. spellCost = whichSchool = schoolLevel = -1;
  520. mySpell = SpellID::NONE;
  521. }
  522. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  523. {
  524. if(!down && mySpell != SpellID::NONE)
  525. {
  526. const CSpell * sp = mySpell.toSpell();
  527. int spellCost = owner->myInt->cb->getSpellCost(sp, owner->myHero);
  528. if(spellCost > owner->myHero->mana) //insufficient mana
  529. {
  530. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  531. return;
  532. }
  533. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  534. if((sp->isCombatSpell() && !owner->myInt->battleInt)
  535. || (sp->isAdventureSpell() && (owner->myInt->battleInt || owner->myInt->castleInt)))
  536. {
  537. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell, 0));
  538. LOCPLINT->showInfoDialog(sp->getLevelInfo(schoolLevel).description, hlp);
  539. return;
  540. }
  541. //we will cast a spell
  542. if(sp->combatSpell && owner->myInt->battleInt && owner->myInt->cb->battleCanCastSpell()) //if battle window is open
  543. {
  544. ESpellCastProblem::ESpellCastProblem problem = owner->myInt->cb->battleCanCastThisSpell(sp);
  545. switch (problem)
  546. {
  547. case ESpellCastProblem::OK:
  548. {
  549. owner->myInt->battleInt->castThisSpell(mySpell);
  550. owner->fexitb();
  551. return;
  552. }
  553. break;
  554. case ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED:
  555. {
  556. std::string text = CGI->generaltexth->allTexts[538], elemental, caster;
  557. const PlayerColor player = owner->myInt->playerID;
  558. const TStacks stacks = owner->myInt->cb->battleGetStacksIf([player](const CStack * s)
  559. {
  560. return s->owner == player
  561. && vstd::contains(s->state, EBattleStackState::SUMMONED)
  562. && !vstd::contains(s->state, EBattleStackState::CLONED);
  563. });
  564. for(const CStack * s : stacks)
  565. {
  566. elemental = s->getCreature()->namePl;
  567. }
  568. if (owner->myHero->type->sex)
  569. { //female
  570. caster = CGI->generaltexth->allTexts[540];
  571. }
  572. else
  573. { //male
  574. caster = CGI->generaltexth->allTexts[539];
  575. }
  576. std::string summoner = owner->myHero->name;
  577. text = boost::str(boost::format(text) % summoner % elemental % caster);
  578. owner->myInt->showInfoDialog(text);
  579. }
  580. break;
  581. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  582. {
  583. //Recanter's Cloak or similar effect. Try to retrieve bonus
  584. const auto b = owner->myHero->getBonusLocalFirst(Selector::type(Bonus::BLOCK_MAGIC_ABOVE));
  585. //TODO what about other values and non-artifact sources?
  586. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  587. {
  588. std::string artName = CGI->arth->artifacts[b->sid]->Name();
  589. //The %s prevents %s from casting 3rd level or higher spells.
  590. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[536])
  591. % artName % owner->myHero->name));
  592. }
  593. else if(b && b->source == Bonus::TERRAIN_OVERLAY && b->sid == BFieldType::CURSED_GROUND)
  594. {
  595. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[537]);
  596. }
  597. else
  598. {
  599. // General message:
  600. // %s recites the incantations but they seem to have no effect.
  601. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  602. text = boost::str(boost::format(text) % caster);
  603. owner->myInt->showInfoDialog(text);
  604. }
  605. }
  606. break;
  607. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  608. {
  609. owner->myInt->showInfoDialog(CGI->generaltexth->allTexts[185]);
  610. }
  611. break;
  612. default:
  613. {
  614. // General message:
  615. std::string text = CGI->generaltexth->allTexts[541], caster = owner->myHero->name;
  616. text = boost::str(boost::format(text) % caster);
  617. owner->myInt->showInfoDialog(text);
  618. }
  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. }