CHeroWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "CPlayerInterface.h"
  16. #include "../global.h"
  17. #include "../lib/CArtHandler.h"
  18. #include "CDefHandler.h"
  19. #include "../lib/CGeneralTextHandler.h"
  20. #include "../lib/CHeroHandler.h"
  21. #include "../lib/CLodHandler.h"
  22. #include "../lib/CObjectHandler.h"
  23. #include <boost/algorithm/string/replace.hpp>
  24. #include <boost/assign/list_of.hpp>
  25. #include <boost/assign/std/vector.hpp>
  26. #include <cstdlib>
  27. #include <sstream>
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/format.hpp>
  30. #include <boost/foreach.hpp>
  31. #undef min
  32. /*
  33. * CHeroWindow.cpp, part of VCMI engine
  34. *
  35. * Authors: listed in file AUTHORS in main folder
  36. *
  37. * License: GNU General Public License v2.0 or later
  38. * Full text of license available in license.txt file, in main folder
  39. *
  40. */
  41. extern SDL_Surface * screen;
  42. using namespace boost::assign;
  43. void CHeroSwitcher::clickLeft(tribool down, bool previousState)
  44. {
  45. if(!down)
  46. {
  47. const CGHeroInstance * buf = LOCPLINT->getWHero(id);
  48. if(!buf)
  49. return;
  50. GH.popIntTotally(getOwner());
  51. GH.pushInt(new CHeroWindow(buf));
  52. }
  53. }
  54. CHeroWindow * CHeroSwitcher::getOwner()
  55. {
  56. return dynamic_cast<CHeroWindow*>(parent);
  57. }
  58. CHeroSwitcher::CHeroSwitcher(int serial)
  59. {
  60. pos = Rect(612, 87 + serial * 54, 48, 32) + pos;
  61. id = serial;
  62. used = LCLICK;
  63. }
  64. CHeroWindow::CHeroWindow(const CGHeroInstance *hero)
  65. {
  66. OBJ_CONSTRUCTION_CAPTURING_ALL;
  67. garr = NULL;
  68. curHero = hero;
  69. player = LOCPLINT->playerID;//hero->tempOwner;
  70. background = new CPicture("HeroScr4.BMP");
  71. background->colorizeAndConvert(player);
  72. pos = background->center();
  73. //artifs = new CArtifactsOfHero(pos.topLeft(), true);
  74. ourBar = new CGStatusBar(7, 559, "ADROLLVR.bmp", 660); // new CStatusBar(pos.x+72, pos.y+567, "ADROLLVR.bmp", 660);
  75. quitButton = new AdventureMapButton(CGI->generaltexth->heroscrn[17], std::string(),boost::bind(&CHeroWindow::quit,this), 609, 516, "hsbtns.def", SDLK_RETURN);
  76. dismissButton = new AdventureMapButton(std::string(), CGI->generaltexth->heroscrn[28], boost::bind(&CHeroWindow::dismissCurrent,this), 454, 429, "hsbtns2.def", SDLK_d);
  77. questlogButton = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CHeroWindow::questlog,this), 314, 429, "hsbtns4.def", SDLK_q);
  78. formations = new CHighlightableButtonsGroup(0);
  79. formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[23]),CGI->generaltexth->heroscrn[29], "hsbtns6.def", 481, 483, 0, 0, SDLK_t);
  80. formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[24]),CGI->generaltexth->heroscrn[30], "hsbtns7.def", 481, 519, 1, 0, SDLK_l);
  81. tacticsButton = 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, 539, 483, SDLK_b);
  82. //right list of heroes
  83. for(int g=0; g<8; ++g)
  84. heroListMi.push_back(new CHeroSwitcher(g));
  85. flags = CDefHandler::giveDefEss("CREST58.DEF");
  86. //areas
  87. portraitArea = new LRClickableAreaWText(Rect(18, 18, 58, 64));
  88. for(int v=0; v<PRIMARY_SKILLS; ++v)
  89. {
  90. LRClickableAreaWTextComp *area = new LRClickableAreaWTextComp(Rect(30 + 70*v, 109, 42, 64), SComponent::primskill);
  91. area->text = CGI->generaltexth->arraytxt[2+v];
  92. area->type = v;
  93. area->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[v]);
  94. primSkillAreas.push_back(area);
  95. }
  96. specArea = new LRClickableAreaWText(Rect(18, 180, 136, 42), CGI->generaltexth->heroscrn[27]);
  97. expArea = new LRClickableAreaWText(Rect(18, 228, 136, 42), CGI->generaltexth->heroscrn[9]);
  98. morale = new MoraleLuckBox(true, Rect(175,179,53,45));
  99. luck = new MoraleLuckBox(false, Rect(233,179,53,45));
  100. spellPointsArea = new LRClickableAreaWText(Rect(162,228, 136, 42), CGI->generaltexth->heroscrn[22]);
  101. for(int i = 0; i < std::min<size_t>(hero->secSkills.size(), 8u); ++i)
  102. {
  103. Rect r = Rect(i%2 == 0 ? 18 : 162, 276 + 48 * (i/2), 136, 42);
  104. secSkillAreas.push_back(new LRClickableAreaWTextComp(r, SComponent::secskill));
  105. }
  106. //////////////////////////////////////////////////////////////////////////???????????????
  107. // pos.x += 65;
  108. // pos.y += 8;
  109. //
  110. //primary skills & exp and mana
  111. new CPicture(graphics->pskillsm->ourImages[0].bitmap, 32, 111, false);
  112. new CPicture(graphics->pskillsm->ourImages[1].bitmap, 102, 111, false);
  113. new CPicture(graphics->pskillsm->ourImages[2].bitmap, 172, 111, false);
  114. new CPicture(graphics->pskillsm->ourImages[5].bitmap, 242, 111, false);
  115. new CPicture(graphics->pskillsm->ourImages[4].bitmap, 20, 230, false);
  116. new CPicture(graphics->pskillsm->ourImages[3].bitmap, 162, 230, false);
  117. update(hero);
  118. }
  119. CHeroWindow::~CHeroWindow()
  120. {
  121. delete flags;
  122. //SDL_FreeSurface(curBack);
  123. //curBack = NULL;
  124. curHero = NULL;
  125. //artifs->dispose();
  126. }
  127. void CHeroWindow::update(const CGHeroInstance * hero, bool redrawNeeded /*= false*/)
  128. {
  129. if(!hero) //something strange... no hero? it shouldn't happen
  130. {
  131. tlog1 << "Set NULL hero? no way...\n";
  132. return;
  133. }
  134. assert(hero == curHero);
  135. //assert(hero->tempOwner == LOCPLINT->playerID || hero->tempOwner == NEUTRAL_PLAYER); //for now we won't show hero windows for non-our heroes
  136. specArea->text = CGI->generaltexth->hTxts[hero->subID].longBonus;
  137. tacticsButton->callback.clear();
  138. tacticsButton->callback2.clear();
  139. dismissButton->hoverTexts[0] = boost::str(boost::format(CGI->generaltexth->heroscrn[16]) % curHero->name % curHero->type->heroClass->name);
  140. portraitArea->hoverText = boost::str(boost::format(CGI->generaltexth->allTexts[15]) % curHero->name % curHero->type->heroClass->name);
  141. portraitArea->text = hero->getBiography();
  142. {
  143. AdventureMapButton * split = NULL;
  144. {
  145. BLOCK_CAPTURING;
  146. split = new AdventureMapButton(CGI->generaltexth->allTexts[256], CGI->generaltexth->heroscrn[32], boost::bind(&CGarrisonInt::splitClick,garr), pos.x + 539, pos.y + 519, "hsbtns9.def", false, NULL, false); //deleted by garrison destructor
  147. boost::algorithm::replace_first(split->hoverTexts[0],"%s",CGI->generaltexth->allTexts[43]);
  148. }
  149. //delete garr;
  150. OBJ_CONSTRUCTION_CAPTURING_ALL;
  151. if(!garr)
  152. {
  153. garr = new CGarrisonInt(15, 485, 8, Point(), background->bg, Point(15,485), curHero);
  154. garr->addSplitBtn(split);
  155. }
  156. if(!artSets.size())
  157. {
  158. CArtifactsOfHero *arts = new CArtifactsOfHero(Point(-65, -8), true);
  159. arts->setHero(hero);
  160. artSets.push_back(arts);
  161. }
  162. }
  163. //primary skills support
  164. for(size_t g=0; g<primSkillAreas.size(); ++g)
  165. {
  166. primSkillAreas[g]->bonusValue = hero->getPrimSkillLevel(g);
  167. }
  168. //secondary skills support
  169. for(size_t g=0; g< secSkillAreas.size(); ++g)
  170. {
  171. int skill = hero->secSkills[g].first,
  172. level = hero->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(hero->secSkills[g].first));
  173. secSkillAreas[g]->type = skill;
  174. secSkillAreas[g]->bonusValue = level;
  175. secSkillAreas[g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  176. secSkillAreas[g]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % CGI->generaltexth->levels[level-1] % CGI->generaltexth->skillName[skill]);
  177. }
  178. //printing experience - original format does not support ui64
  179. expArea->text = CGI->generaltexth->allTexts[2];
  180. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(hero->level));
  181. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  182. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(hero->exp));
  183. //printing spell points
  184. spellPointsArea->text = boost::str(boost::format(CGI->generaltexth->allTexts[205]) % hero->name % hero->mana % hero->manaLimit());
  185. //if we have exchange window with this hero open
  186. bool noDismiss=false;
  187. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  188. {
  189. if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(isa))
  190. for(int g=0; g < ARRAY_COUNT(cew->heroInst); ++g)
  191. if(cew->heroInst[g] == hero)
  192. noDismiss = true;
  193. if (dynamic_cast<CKingdomInterface*>(isa))
  194. noDismiss = true;
  195. }
  196. dismissButton->block(!!hero->visitedTown || noDismiss);
  197. if(hero->getSecSkillLevel(CGHeroInstance::TACTICS) == 0)
  198. tacticsButton->block(true);
  199. else
  200. {
  201. tacticsButton->block(false);
  202. tacticsButton->callback = vstd::assigno(hero->tacticFormationEnabled,true);
  203. tacticsButton->callback2 = vstd::assigno(hero->tacticFormationEnabled,false);
  204. }
  205. //setting formations
  206. formations->onChange = 0;
  207. formations->select(hero->formation,true);
  208. formations->onChange = boost::bind(&CCallback::setFormation, LOCPLINT->cb, hero, _1);
  209. morale->set(hero);
  210. luck->set(hero);
  211. if(redrawNeeded)
  212. redraw();
  213. }
  214. void CHeroWindow::quit()
  215. {
  216. GH.popIntTotally(this);
  217. }
  218. void CHeroWindow::dismissCurrent()
  219. {
  220. CFunctionList<void()> ony = boost::bind(&CHeroWindow::quit,this);
  221. ony += boost::bind(&CCallback::dismissHero,LOCPLINT->cb,curHero);
  222. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[22],std::vector<SComponent*>(), ony, 0, false);
  223. }
  224. void CHeroWindow::questlog()
  225. {
  226. }
  227. void CHeroWindow::showAll(SDL_Surface * to)
  228. {
  229. CIntObject::showAll(to);
  230. //blitting portrait
  231. blitAtLoc(graphics->portraitLarge[curHero->portrait], 19, 19, to);
  232. //printing hero's name
  233. printAtMiddleLoc(curHero->name, 190, 38, FONT_BIG, tytulowy, to);
  234. //printing hero's level
  235. std::string secondLine= CGI->generaltexth->allTexts[342];
  236. boost::algorithm::replace_first(secondLine,"%d",boost::lexical_cast<std::string>(curHero->level));
  237. boost::algorithm::replace_first(secondLine,"%s",curHero->type->heroClass->name);
  238. printAtMiddleLoc(secondLine, 190, 65, FONT_MEDIUM, zwykly, to);
  239. //primary skills names
  240. printAtMiddleLoc(CGI->generaltexth->jktexts[1], 52, 99, FONT_SMALL, tytulowy, to);
  241. printAtMiddleLoc(CGI->generaltexth->jktexts[2], 123, 99, FONT_SMALL, tytulowy, to);
  242. printAtMiddleLoc(CGI->generaltexth->jktexts[3], 193, 99, FONT_SMALL, tytulowy, to);
  243. printAtMiddleLoc(CGI->generaltexth->jktexts[4], 262, 99, FONT_SMALL, tytulowy, to);
  244. //dismiss / quest log
  245. std::vector<std::string> toPrin = CMessage::breakText(CGI->generaltexth->jktexts[8]);
  246. if(toPrin.size()==1)
  247. {
  248. printAtLoc(toPrin[0], 372, 439, FONT_SMALL, zwykly, to);
  249. }
  250. else
  251. {
  252. printAtLoc(toPrin[0], 372, 430, FONT_SMALL, zwykly, to);
  253. printAtLoc(toPrin[1], 372, 446, FONT_SMALL, zwykly, to);
  254. }
  255. toPrin = CMessage::breakText(CGI->generaltexth->jktexts[9]);
  256. if(toPrin.size()==1)
  257. {
  258. printAtLoc(toPrin[0], 512, 439, FONT_SMALL, zwykly, to);
  259. }
  260. else
  261. {
  262. printAtLoc(toPrin[0], 512, 430, FONT_SMALL, zwykly, to);
  263. printAtLoc(toPrin[1], 512, 446, FONT_SMALL, zwykly, to);
  264. }
  265. //printing primary skills' amounts
  266. for(int m=0; m<4; ++m)
  267. {
  268. std::ostringstream primarySkill;
  269. primarySkill<<curHero->getPrimSkillLevel(m);
  270. printAtMiddleLoc(primarySkill.str(), 53 + 70 * m, 166, FONT_SMALL, zwykly, to);
  271. }
  272. blitAtLoc(flags->ourImages[player].bitmap, 606, 8, to);
  273. //hero list blitting
  274. for(int slotPos=0, g=0; g<LOCPLINT->wanderingHeroes.size(); ++g)
  275. {
  276. const CGHeroInstance * cur = LOCPLINT->wanderingHeroes[g];
  277. if (cur->inTownGarrison)
  278. // Only display heroes that are not in garrison
  279. continue;
  280. blitAtLoc(graphics->portraitSmall[cur->portrait], 611, 87+slotPos*54, to);
  281. //printing yellow border
  282. if(cur->name == curHero->name)
  283. {
  284. for(int f=0; f<graphics->portraitSmall[cur->portrait]->w; ++f)
  285. {
  286. for(int h=0; h<graphics->portraitSmall[cur->portrait]->h; ++h)
  287. if(f==0 || h==0 || f==graphics->portraitSmall[cur->portrait]->w-1 || h==graphics->portraitSmall[cur->portrait]->h-1)
  288. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + 611+f, pos.y + 87+slotPos*54+h, 240, 220, 120);
  289. }
  290. }
  291. slotPos ++;
  292. }
  293. //secondary skills
  294. for(size_t v=0; v<std::min(secSkillAreas.size(), curHero->secSkills.size()); ++v)
  295. {
  296. blitAtLoc(graphics->abils44->ourImages[curHero->secSkills[v].first*3+3+curHero->secSkills[v].second-1].bitmap, v%2 ? 161 : 18, 276 + 48 * (v/2), to);
  297. printAtLoc(CGI->generaltexth->levels[curHero->secSkills[v].second-1], v%2 ? 212 : 68, 280 + 48 * (v/2), FONT_SMALL, zwykly, to);
  298. printAtLoc(CGI->generaltexth->skillName[curHero->secSkills[v].first], v%2 ? 212 : 68, 300 + 48 * (v/2), FONT_SMALL, zwykly, to);
  299. }
  300. //printing special ability
  301. blitAtLoc(graphics->un44->ourImages[curHero->subID].bitmap, 18, 180, to);
  302. printAtLoc(CGI->generaltexth->jktexts[5].substr(1, CGI->generaltexth->jktexts[5].size()-2), 69, 183, FONT_SMALL, tytulowy, to);
  303. printAtLoc(CGI->generaltexth->hTxts[curHero->subID].bonusName, 69, 205, FONT_SMALL, zwykly, to);
  304. //printing necessery texts
  305. printAtLoc(CGI->generaltexth->jktexts[6].substr(1, CGI->generaltexth->jktexts[6].size()-2), 69, 232, FONT_SMALL, tytulowy, to);
  306. std::ostringstream expstr;
  307. expstr<<curHero->exp;
  308. printAtLoc(expstr.str(), 68, 252, FONT_SMALL, zwykly, to);
  309. printAtLoc(CGI->generaltexth->jktexts[7].substr(1, CGI->generaltexth->jktexts[7].size()-2), 213, 232, FONT_SMALL, tytulowy, to);
  310. std::ostringstream manastr;
  311. manastr << curHero->mana << '/' << curHero->manaLimit();
  312. printAtLoc(manastr.str(), 211, 252, FONT_SMALL, zwykly, to);
  313. }