CSpellWindow.cpp 24 KB

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