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