CHeroWindow.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #include "../stdafx.h"
  2. #include "AdventureMapButton.h"
  3. #include "CAdvmapInterface.h"
  4. #include "../CCallback.h"
  5. #include "CGameInfo.h"
  6. #include "CHeroWindow.h"
  7. #include "CMessage.h"
  8. #include "CKingdomInterface.h"
  9. #include "SDL.h"
  10. #include "SDL_Extensions.h"
  11. #include "CBitmapHandler.h"
  12. #include "Graphics.h"
  13. #include "CSpellWindow.h"
  14. #include "CConfigHandler.h"
  15. #include "../global.h"
  16. #include "../hch/CArtHandler.h"
  17. #include "../hch/CDefHandler.h"
  18. #include "../hch/CGeneralTextHandler.h"
  19. #include "../hch/CHeroHandler.h"
  20. #include "../hch/CLodHandler.h"
  21. #include "../hch/CObjectHandler.h"
  22. #include <boost/algorithm/string/replace.hpp>
  23. #include <boost/assign/list_of.hpp>
  24. #include <boost/assign/std/vector.hpp>
  25. #include <cstdlib>
  26. #include <sstream>
  27. #include <boost/lexical_cast.hpp>
  28. #undef min
  29. /*
  30. * CHeroWindow.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. extern SDL_Surface * screen;
  39. using namespace boost::assign;
  40. CHeroWindow::CHeroWindow(int playerColor):
  41. player(playerColor)
  42. {
  43. background = BitmapHandler::loadBitmap("HEROSCR4.bmp");
  44. graphics->blueToPlayersAdv(background, playerColor);
  45. pos.x = screen->w/2 - background->w/2 - 65;
  46. pos.y = screen->h/2 - background->h/2 - 8;
  47. pos.h = background->h;
  48. pos.w = background->w;
  49. curBack = NULL;
  50. curHero = NULL;
  51. char bufor[400];
  52. artifs = new CArtifactsOfHero(pos);
  53. artifs->commonInfo = new CArtifactsOfHero::SCommonPart;
  54. artifs->commonInfo->participants.insert(artifs);
  55. garr = NULL;
  56. ourBar = new CStatusBar(pos.x+72, pos.y+567, "ADROLLVR.bmp", 660);
  57. quitButton = new AdventureMapButton(CGI->generaltexth->heroscrn[17], std::string(), boost::function<void()>(), pos.x+674, pos.y+524, "hsbtns.def", SDLK_RETURN);
  58. dismissButton = new AdventureMapButton(std::string(), CGI->generaltexth->heroscrn[28], boost::bind(&CHeroWindow::dismissCurrent,this), pos.x+519, pos.y+437, "hsbtns2.def", SDLK_d);
  59. questlogButton = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CHeroWindow::questlog,this), pos.x+379, pos.y+437, "hsbtns4.def", SDLK_q);
  60. formations = new CHighlightableButtonsGroup(0);
  61. formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[23]),CGI->generaltexth->heroscrn[29], "hsbtns6.def", pos.x+546, pos.y+491, 0, 0, SDLK_t);
  62. formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[24]),CGI->generaltexth->heroscrn[30], "hsbtns7.def", pos.x+546, pos.y+527, 1, 0, SDLK_l);
  63. gar2button = new CHighlightableButton(0, 0, map_list_of(0,CGI->generaltexth->heroscrn[26])(3,CGI->generaltexth->heroscrn[25]), CGI->generaltexth->heroscrn[31], false, "hsbtns8.def", NULL, pos.x+604, pos.y+491, SDLK_b);
  64. //right list of heroes
  65. for(int g=0; g<8; ++g)
  66. {
  67. //heroList.push_back(new AdventureMapButton<CHeroWindow>(std::string(), std::string(), &CHeroWindow::switchHero, 677, 95+g*54, "hsbtns5.def", this));
  68. heroListMi.push_back(new LClickableAreaHero());
  69. heroListMi[g]->pos = genRect(32, 48, pos.x+677, pos.y + 95+g*54);
  70. heroListMi[g]->owner = this;
  71. heroListMi[g]->id = g;
  72. }
  73. flags = CDefHandler::giveDefEss("CREST58.DEF");
  74. //areas
  75. portraitArea = new LRClickableAreaWText();
  76. portraitArea->pos = genRect(64, 58, pos.x+83, pos.y + 26);
  77. for(int v=0; v<PRIMARY_SKILLS; ++v)
  78. {
  79. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  80. primSkillAreas[v]->pos = genRect(64, 42, pos.x+95 + 70*v, pos.y + 117);
  81. primSkillAreas[v]->text = CGI->generaltexth->arraytxt[2+v];
  82. primSkillAreas[v]->type = v;
  83. primSkillAreas[v]->bonus = -1; // to be initilized when hero is being set
  84. primSkillAreas[v]->baseType = 0;
  85. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[v].c_str());
  86. primSkillAreas[v]->hoverText = std::string(bufor);
  87. }
  88. specArea = new LRClickableAreaWText();
  89. specArea->pos = genRect(42, 136, pos.x+83, pos.y + 188);
  90. specArea->hoverText = CGI->generaltexth->heroscrn[27];
  91. expArea = new LRClickableAreaWText();
  92. expArea->pos = genRect(42, 136, pos.x+83, pos.y + 236);
  93. expArea->hoverText = CGI->generaltexth->heroscrn[9];
  94. morale = new MoraleLuckBox();
  95. morale->pos = genRect(45,53,pos.x+240,pos.y+187);
  96. luck = new MoraleLuckBox();
  97. luck->pos = genRect(45,53,pos.x+298,pos.y+187);
  98. spellPointsArea = new LRClickableAreaWText();
  99. spellPointsArea->pos = genRect(42, 136, pos.x+227, pos.y + 236);
  100. spellPointsArea->hoverText = CGI->generaltexth->heroscrn[22];
  101. for(int i=0; i<SKILL_PER_HERO; ++i)
  102. {
  103. secSkillAreas.push_back(new LRClickableAreaWTextComp());
  104. secSkillAreas[i]->pos = genRect(42, 136, pos.x + ((i%2==0) ? (83) : (227)), pos.y + (284 + 48 * (i/2)));
  105. secSkillAreas[i]->baseType = 1;
  106. }
  107. pos.x += 65;
  108. pos.y += 8;
  109. }
  110. CHeroWindow::~CHeroWindow()
  111. {
  112. SDL_FreeSurface(background);
  113. delete quitButton;
  114. delete dismissButton;
  115. delete questlogButton;
  116. delete formations;
  117. delete gar2button;
  118. //delete gar4button;
  119. for(size_t g=0; g<heroListMi.size(); ++g)
  120. {
  121. delete heroListMi[g];
  122. }
  123. if(curBack)
  124. {
  125. SDL_FreeSurface(curBack);
  126. }
  127. delete flags;
  128. delete garr;
  129. delete ourBar;
  130. artifs->rollback();
  131. delete artifs->commonInfo;
  132. artifs->commonInfo = NULL; //to prevent heap corruption
  133. delete artifs;
  134. delete portraitArea;
  135. delete expArea;
  136. delete luck;
  137. delete morale;
  138. delete specArea;
  139. delete spellPointsArea;
  140. for(size_t v=0; v<primSkillAreas.size(); ++v)
  141. {
  142. delete primSkillAreas[v];
  143. }
  144. for(size_t v=0; v<secSkillAreas.size(); ++v)
  145. {
  146. delete secSkillAreas[v];
  147. }
  148. }
  149. void CHeroWindow::show(SDL_Surface *to)
  150. {
  151. if(curBack)
  152. blitAt(curBack,pos.x,pos.y,to);
  153. quitButton->show(to);
  154. dismissButton->show(to);
  155. questlogButton->show(to);
  156. formations->show(to);
  157. gar2button->show(to);
  158. //gar4button->show(to);
  159. garr->show(to);
  160. ourBar->show(to);
  161. artifs->show(to);
  162. }
  163. void CHeroWindow::setHero(const CGHeroInstance *hero)
  164. {
  165. char bufor[400];
  166. //CGHeroInstance *hero = const_cast<CGHeroInstance*>(Hero); //but don't modify hero! - it's only for easy map reading
  167. if(!hero) //something strange... no hero? it shouldn't happen
  168. {
  169. return;
  170. }
  171. curHero = hero;
  172. //pos temporarily switched, restored later
  173. pos.x -= 65;
  174. pos.y -= 8;
  175. specArea->text = CGI->generaltexth->hTxts[hero->subID].longBonus;
  176. gar2button->callback.clear();
  177. gar2button->callback2.clear();
  178. sprintf(bufor, CGI->generaltexth->heroscrn[16].c_str(), curHero->name.c_str(), curHero->type->heroClass->name.c_str());
  179. dismissButton->hoverTexts[0] = std::string(bufor);
  180. sprintf(bufor, CGI->generaltexth->allTexts[15].c_str(), curHero->name.c_str(), curHero->type->heroClass->name.c_str());
  181. portraitArea->hoverText = std::string(bufor);
  182. portraitArea->text = hero->getBiography();
  183. delete garr;
  184. garr = new CGarrisonInt(pos.x+80, pos.y+493, 8, Point(), curBack, Point(16,486), curHero);
  185. garr->update = false;
  186. AdventureMapButton * split = new AdventureMapButton(CGI->generaltexth->allTexts[256], CGI->generaltexth->heroscrn[32], boost::bind(&CGarrisonInt::splitClick,garr), pos.x+604, pos.y+527, "hsbtns9.def", false, NULL, false); //deleted by garrison destructor
  187. boost::algorithm::replace_first(split->hoverTexts[0],"%s",CGI->generaltexth->allTexts[43]);
  188. garr->splitButtons.push_back(split);
  189. //primary skills support
  190. for(size_t g=0; g<primSkillAreas.size(); ++g)
  191. {
  192. primSkillAreas[g]->bonus = hero->getPrimSkillLevel(g);
  193. }
  194. //secondary skills support
  195. for(size_t g=0; g<std::min(secSkillAreas.size(),hero->secSkills.size()); ++g)
  196. {
  197. int skill = hero->secSkills[g].first,
  198. level = hero->secSkills[g].second;
  199. secSkillAreas[g]->type = skill;
  200. secSkillAreas[g]->bonus = level;
  201. secSkillAreas[g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  202. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level-1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  203. secSkillAreas[g]->hoverText = std::string(bufor);
  204. }
  205. //printing experience - original format does not support ui64
  206. expArea->text = CGI->generaltexth->allTexts[2].c_str();
  207. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(hero->level));
  208. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  209. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(hero->exp));
  210. //printing spell points
  211. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), hero->name.c_str(), hero->mana, hero->manaLimit());
  212. spellPointsArea->text = std::string(bufor);
  213. artifs->updateState = true;
  214. artifs->setHero(hero);
  215. artifs->updateState = false;
  216. //if we have exchange window with this hero open
  217. bool noDismiss=false;
  218. for(std::list<IShowActivable *>::iterator it=GH.listInt.begin() ; it != GH.listInt.end(); it++)
  219. {
  220. CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>((*it));
  221. if(cew)
  222. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  223. if(cew->heroInst[g] == hero)
  224. noDismiss = true;
  225. CKingdomInterface * cki = dynamic_cast<CKingdomInterface*>((*it));
  226. if (cki)
  227. noDismiss = true;
  228. }
  229. dismissButton->block(!!hero->visitedTown || noDismiss);
  230. if(hero->getSecSkillLevel(19)==0)
  231. gar2button->block(true);
  232. else
  233. {
  234. gar2button->block(false);
  235. gar2button->callback = vstd::assigno(hero->tacticFormationEnabled,true);
  236. gar2button->callback2 = vstd::assigno(hero->tacticFormationEnabled,false);
  237. }
  238. //setting formations
  239. formations->onChange = 0;
  240. formations->select(hero->army.formation,true);
  241. formations->onChange = boost::bind(&CCallback::setFormation, LOCPLINT->cb, hero, _1);
  242. morale->set(true, hero);
  243. luck->set(false, hero);
  244. //restoring pos
  245. pos.x += 65;
  246. pos.y += 8;
  247. redrawCurBack();
  248. }
  249. void CHeroWindow::quit()
  250. {
  251. GH.popInt(this);
  252. dispose();
  253. }
  254. void CHeroWindow::activate()
  255. {
  256. quitButton->activate();
  257. dismissButton->activate();
  258. questlogButton->activate();
  259. gar2button->activate();
  260. formations->activate();
  261. //gar4button->activate();
  262. portraitArea->activate();
  263. specArea->activate();
  264. expArea->activate();
  265. spellPointsArea->activate();
  266. morale->activate();
  267. luck->activate();
  268. garr->activate();
  269. GH.statusbar = ourBar;
  270. for(size_t v=0; v<primSkillAreas.size(); ++v)
  271. {
  272. primSkillAreas[v]->activate();
  273. }
  274. for(size_t v=0; v<std::min(secSkillAreas.size(), curHero->secSkills.size()); ++v)
  275. {
  276. secSkillAreas[v]->activate();
  277. }
  278. redrawCurBack();
  279. artifs->activate();
  280. for(size_t e=0; e<heroListMi.size(); ++e)
  281. {
  282. heroListMi[e]->activate();
  283. }
  284. }
  285. void CHeroWindow::deactivate()
  286. {
  287. quitButton->deactivate();
  288. dismissButton->deactivate();
  289. questlogButton->deactivate();
  290. gar2button->deactivate();
  291. formations->deactivate();
  292. specArea->deactivate();
  293. //gar4button->deactivate();
  294. portraitArea->deactivate();
  295. expArea->deactivate();
  296. spellPointsArea->deactivate();
  297. morale->deactivate();
  298. luck->deactivate();
  299. garr->deactivate();
  300. for(size_t v=0; v<primSkillAreas.size(); ++v)
  301. {
  302. primSkillAreas[v]->deactivate();
  303. }
  304. for(size_t v=0; v<std::min(secSkillAreas.size(), curHero->secSkills.size()); ++v)
  305. {
  306. secSkillAreas[v]->deactivate();
  307. }
  308. artifs->deactivate();
  309. for(size_t e=0; e<heroListMi.size(); ++e)
  310. {
  311. heroListMi[e]->deactivate();
  312. }
  313. }
  314. void CHeroWindow::dismissCurrent()
  315. {
  316. CFunctionList<void()> ony = boost::bind(&CHeroWindow::quit,this);
  317. ony += boost::bind(&CCallback::dismissHero,LOCPLINT->cb,curHero);
  318. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[22],std::vector<SComponent*>(), ony, 0, false);
  319. }
  320. void CHeroWindow::questlog()
  321. {
  322. }
  323. void CHeroWindow::redrawCurBack()
  324. {
  325. if(curBack)
  326. SDL_FreeSurface(curBack);
  327. curBack = SDL_DisplayFormat(background);
  328. //primary skills & exp and mana
  329. blitAt(graphics->pskillsm->ourImages[0].bitmap, 32, 111, curBack);
  330. blitAt(graphics->pskillsm->ourImages[1].bitmap, 102, 111, curBack);
  331. blitAt(graphics->pskillsm->ourImages[2].bitmap, 172, 111, curBack);
  332. blitAt(graphics->pskillsm->ourImages[5].bitmap, 242, 111, curBack);
  333. blitAt(graphics->pskillsm->ourImages[4].bitmap, 20, 230, curBack);
  334. blitAt(graphics->pskillsm->ourImages[3].bitmap, 162, 230, curBack);
  335. //blitting portrait
  336. blitAt(graphics->portraitLarge[curHero->portrait], 19, 19, curBack);
  337. //printing hero's name
  338. CSDL_Ext::printAtMiddle(curHero->name, 190, 38, FONT_BIG, tytulowy, curBack);
  339. //printing hero's level
  340. std::ostringstream secondLine;
  341. secondLine<<"Level "<<curHero->level<<" "<<curHero->type->heroClass->name;
  342. CSDL_Ext::printAtMiddle(secondLine.str(), 190, 65, FONT_MEDIUM, zwykly, curBack);
  343. //primary skills names
  344. CSDL_Ext::printAtMiddle(CGI->generaltexth->jktexts[1], 52, 99, FONT_SMALL, tytulowy, curBack);
  345. CSDL_Ext::printAtMiddle(CGI->generaltexth->jktexts[2], 123, 99, FONT_SMALL, tytulowy, curBack);
  346. CSDL_Ext::printAtMiddle(CGI->generaltexth->jktexts[3], 193, 99, FONT_SMALL, tytulowy, curBack);
  347. CSDL_Ext::printAtMiddle(CGI->generaltexth->jktexts[4], 262, 99, FONT_SMALL, tytulowy, curBack);
  348. //dismiss / quest log
  349. std::vector<std::string> * toPrin = CMessage::breakText(CGI->generaltexth->jktexts[8].substr(1, CGI->generaltexth->jktexts[8].size()-2));
  350. if(toPrin->size()==1)
  351. {
  352. CSDL_Ext::printAt((*toPrin)[0], 372, 439, FONT_SMALL, zwykly, curBack);
  353. }
  354. else
  355. {
  356. CSDL_Ext::printAt((*toPrin)[0], 372, 430, FONT_SMALL, zwykly, curBack);
  357. CSDL_Ext::printAt((*toPrin)[1], 372, 446, FONT_SMALL, zwykly, curBack);
  358. }
  359. delete toPrin;
  360. toPrin = CMessage::breakText(CGI->generaltexth->jktexts[9].substr(1, CGI->generaltexth->jktexts[9].size()-2));
  361. if(toPrin->size()==1)
  362. {
  363. CSDL_Ext::printAt((*toPrin)[0], 512, 439, FONT_SMALL, zwykly, curBack);
  364. }
  365. else
  366. {
  367. CSDL_Ext::printAt((*toPrin)[0], 512, 430, FONT_SMALL, zwykly, curBack);
  368. CSDL_Ext::printAt((*toPrin)[1], 512, 446, FONT_SMALL, zwykly, curBack);
  369. }
  370. delete toPrin;
  371. //printing primary skills' amounts
  372. for(int m=0; m<4; ++m)
  373. {
  374. std::ostringstream primarySkill;
  375. primarySkill<<curHero->getPrimSkillLevel(m);
  376. CSDL_Ext::printAtMiddle(primarySkill.str(), 53 + 70 * m, 166, FONT_SMALL, zwykly, curBack);
  377. }
  378. //morale and luck printing
  379. blitAt(graphics->luck42->ourImages[curHero->getCurrentLuck()+3].bitmap, 239, 182, curBack);
  380. blitAt(graphics->morale42->ourImages[curHero->getCurrentMorale()+3].bitmap, 181, 182, curBack);
  381. blitAt(flags->ourImages[player].bitmap, 606, 8, curBack);
  382. //hero list blitting
  383. for(int pos=0, g=0; g<LOCPLINT->wanderingHeroes.size(); ++g)
  384. {
  385. const CGHeroInstance * cur = LOCPLINT->wanderingHeroes[g];
  386. if (cur->inTownGarrison)
  387. // Only display heroes that are not in garrison
  388. continue;
  389. blitAt(graphics->portraitSmall[cur->portrait], 611, 87+pos*54, curBack);
  390. //printing yellow border
  391. if(cur->name == curHero->name)
  392. {
  393. for(int f=0; f<graphics->portraitSmall[cur->portrait]->w; ++f)
  394. {
  395. for(int h=0; h<graphics->portraitSmall[cur->portrait]->h; ++h)
  396. if(f==0 || h==0 || f==graphics->portraitSmall[cur->portrait]->w-1 || h==graphics->portraitSmall[cur->portrait]->h-1)
  397. {
  398. CSDL_Ext::SDL_PutPixelWithoutRefresh(curBack, 611+f, 87+pos*54+h, 240, 220, 120);
  399. }
  400. }
  401. }
  402. pos ++;
  403. }
  404. //secondary skills
  405. for(int g=1; g<=8; ++g)
  406. {
  407. if(curHero->secSkills.size()>=g)
  408. {
  409. blitAt(graphics->abils44->ourImages[curHero->secSkills[g-1].first*3+3+curHero->secSkills[g-1].second-1].bitmap, g%2 ? 18 : 161, 276 + 48 * ((g-1)/2), curBack);
  410. CSDL_Ext::printAt(CGI->generaltexth->levels[curHero->secSkills[g-1].second-1], g%2 ? 68 : 212, 280 + 48 * ((g-1)/2), FONT_SMALL, zwykly, curBack);
  411. CSDL_Ext::printAt(CGI->generaltexth->skillName[curHero->secSkills[g-1].first], g%2 ? 68 : 212, 300 + 48 * ((g-1)/2), FONT_SMALL, zwykly, curBack);
  412. }
  413. }
  414. //printing special ability
  415. blitAt(graphics->un44->ourImages[curHero->subID].bitmap, 18, 180, curBack);
  416. CSDL_Ext::printAt(CGI->generaltexth->jktexts[5].substr(1, CGI->generaltexth->jktexts[5].size()-2), 69, 183, FONT_SMALL, tytulowy, curBack);
  417. CSDL_Ext::printAt(CGI->generaltexth->hTxts[curHero->subID].bonusName, 69, 205, FONT_SMALL, zwykly, curBack);
  418. //printing necessery texts
  419. CSDL_Ext::printAt(CGI->generaltexth->jktexts[6].substr(1, CGI->generaltexth->jktexts[6].size()-2), 69, 232, FONT_SMALL, tytulowy, curBack);
  420. std::ostringstream expstr;
  421. expstr<<curHero->exp;
  422. CSDL_Ext::printAt(expstr.str(), 68, 252, FONT_SMALL, zwykly, curBack);
  423. CSDL_Ext::printAt(CGI->generaltexth->jktexts[7].substr(1, CGI->generaltexth->jktexts[7].size()-2), 213, 232, FONT_SMALL, tytulowy, curBack);
  424. std::ostringstream manastr;
  425. manastr << curHero->mana << '/' << curHero->manaLimit();
  426. CSDL_Ext::printAt(manastr.str(), 211, 252, FONT_SMALL, zwykly, curBack);
  427. }
  428. void CHeroWindow::dispose()
  429. {
  430. SDL_FreeSurface(curBack);
  431. curBack = NULL;
  432. curHero = NULL;
  433. artifs->rollback();
  434. artifs->dispose();
  435. }