CHeroWindow.cpp 13 KB

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