CCastleInterface.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. #include "../stdafx.h"
  2. #include <boost/algorithm/string.hpp>
  3. #include <boost/algorithm/string/replace.hpp>
  4. #include <boost/assign/std/vector.hpp>
  5. #include <boost/format.hpp>
  6. #include <boost/lexical_cast.hpp>
  7. #include <boost/foreach.hpp>
  8. #include "CCastleInterface.h"
  9. #include "../CCallback.h"
  10. #include "../lib/CArtHandler.h"
  11. #include "../lib/CBuildingHandler.h"
  12. #include "../lib/CCreatureHandler.h"
  13. #include "../lib/CGeneralTextHandler.h"
  14. #include "../lib/CObjectHandler.h"
  15. #include "../lib/CSpellHandler.h"
  16. #include "../lib/CTownHandler.h"
  17. #include "AdventureMapButton.h"
  18. #include "CAdvmapInterface.h"
  19. #include "CAnimation.h"
  20. #include "CBitmapHandler.h"
  21. #include "CDefHandler.h"
  22. #include "CGameInfo.h"
  23. #include "CHeroWindow.h"
  24. #include "CMessage.h"
  25. #include "CMusicHandler.h"
  26. #include "CPlayerInterface.h"
  27. #include "Graphics.h"
  28. #include "SDL_Extensions.h"
  29. using namespace boost::assign;
  30. /*
  31. * CCastleInterface.cpp, part of VCMI engine
  32. *
  33. * Authors: listed in file AUTHORS in main folder
  34. *
  35. * License: GNU General Public License v2.0 or later
  36. * Full text of license available in license.txt file, in main folder
  37. *
  38. */
  39. int hordeToDwellingID(int bid)//helper, converts horde buiding ID into corresponding dwelling ID
  40. {
  41. const CGTownInstance * t = LOCPLINT->castleInt->town;
  42. switch (bid)
  43. {
  44. case 18: return t->town->hordeLvl[0] + 30;
  45. case 19: return t->town->hordeLvl[0] + 37;
  46. case 24: return t->town->hordeLvl[1] + 30;
  47. case 25: return t->town->hordeLvl[1] + 37;
  48. default: return bid;
  49. }
  50. }
  51. CBuildingRect::CBuildingRect(CCastleBuildings * Par, const CGTownInstance *Town, const Structure *Str)
  52. :CShowableAnim(0, 0, Str->defName, CShowableAnim::BASE | CShowableAnim::USE_RLE),
  53. parent(Par),
  54. town(Town),
  55. str(Str),
  56. stateCounter(80)
  57. {
  58. recActions = ACTIVATE | DEACTIVATE | DISPOSE | SHARE_POS;
  59. used |= LCLICK | RCLICK | HOVER;
  60. pos.x += str->pos.x;
  61. pos.y += str->pos.y;
  62. if (!str->borderName.empty())
  63. border = BitmapHandler::loadBitmap(str->borderName, true);
  64. else
  65. border = NULL;
  66. if (!str->areaName.empty())
  67. area = BitmapHandler::loadBitmap(str->areaName);
  68. else
  69. area = NULL;
  70. }
  71. CBuildingRect::~CBuildingRect()
  72. {
  73. SDL_FreeSurface(border);
  74. SDL_FreeSurface(area);
  75. }
  76. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  77. {
  78. if(str->pos.z != p2.str->pos.z)
  79. return (str->pos.z) < (p2.str->pos.z);
  80. else
  81. return (str->ID) < (p2.str->ID);
  82. }
  83. void CBuildingRect::hover(bool on)
  84. {
  85. if(on)
  86. {
  87. if(!(active & MOVE))
  88. changeUsedEvents(MOVE, true, true);
  89. }
  90. else
  91. {
  92. if(active & MOVE)
  93. changeUsedEvents(MOVE, false, true);
  94. if(parent->selectedBuilding == this)
  95. {
  96. parent->selectedBuilding = NULL;
  97. GH.statusbar->clear();
  98. }
  99. }
  100. }
  101. void CBuildingRect::clickLeft(tribool down, bool previousState)
  102. {
  103. if( previousState && !down && area && (parent->selectedBuilding==this) )
  104. if (!CSDL_Ext::isTransparent(area, GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) ) //inside building image
  105. parent->buildingClicked(str->ID);
  106. }
  107. void CBuildingRect::clickRight(tribool down, bool previousState)
  108. {
  109. if((!area) || (!((bool)down)) || (this!=parent->selectedBuilding))
  110. return;
  111. if( !CSDL_Ext::isTransparent(area, GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) ) //inside building image
  112. {
  113. int bid = hordeToDwellingID(str->ID);
  114. const CBuilding *bld = CGI->buildh->buildings[str->townID].find(bid)->second;
  115. if (bid < Buildings::DWELL_FIRST)
  116. {
  117. std::vector<SComponent*> comps(1,
  118. new SComponent(SComponent::building, bld->tid, bld->bid,
  119. LOCPLINT->castleInt->bicons->ourImages[bld->bid].bitmap, false));
  120. CRClickPopup::createAndPush(bld->Description(), comps);
  121. }
  122. else
  123. {
  124. int level = ( bid - Buildings::DWELL_FIRST ) % CREATURES_PER_TOWN;
  125. GH.pushInt(new CDwellingInfoBox(parent->pos.x+parent->pos.w/2, parent->pos.y+parent->pos.h/2, town, level));
  126. }
  127. }
  128. }
  129. SDL_Color multiplyColors (const SDL_Color &b, const SDL_Color &a, float f)
  130. {
  131. SDL_Color ret;
  132. ret.r = a.r*f + b.r*(1-f);
  133. ret.g = a.g*f + b.g*(1-f);
  134. ret.b = a.b*f + b.b*(1-f);
  135. return ret;
  136. }
  137. void CBuildingRect::show(SDL_Surface *to)
  138. {
  139. const unsigned int stageDelay = 16;
  140. const unsigned int S1_TRANSP = 16; //0.5 sec building appear 0->100 transparency
  141. const unsigned int S2_WHITE_B = 32; //0.5 sec border glows from white to yellow
  142. const unsigned int S3_YELLOW_B= 48; //0.5 sec border glows from yellow to normal
  143. const unsigned int BUILDED = 80; // 1 sec delay, nothing happens
  144. if (stateCounter < S1_TRANSP)
  145. {
  146. setAlpha(255*stateCounter/stageDelay);
  147. CShowableAnim::show(to);
  148. }
  149. else
  150. {
  151. setAlpha(255);
  152. CShowableAnim::show(to);
  153. }
  154. if (border && stateCounter > S1_TRANSP)
  155. {
  156. if (stateCounter == BUILDED)
  157. {
  158. if (parent->selectedBuilding == this)
  159. blitAtLoc(border,0,0,to);
  160. return;
  161. }
  162. // key colors in glowing border
  163. SDL_Color c1 = {200, 200, 200, 255};
  164. SDL_Color c2 = {120, 100, 60, 255};
  165. SDL_Color c3 = {200, 180, 110, 255};
  166. unsigned int colorID = SDL_MapRGB(border->format, c3.r, c3.g, c3.b);
  167. SDL_Color oldColor = border->format->palette->colors[colorID];
  168. SDL_Color newColor;
  169. if (stateCounter < S2_WHITE_B)
  170. newColor = multiplyColors(c1, c2, float(stateCounter%stageDelay)/stageDelay);
  171. else
  172. if (stateCounter < S3_YELLOW_B)
  173. newColor = multiplyColors(c2, c3, float(stateCounter%stageDelay)/stageDelay);
  174. else
  175. newColor = oldColor;
  176. SDL_SetColors(border, &newColor, colorID, 1);
  177. blitAtLoc(border,0,0,to);
  178. SDL_SetColors(border, &oldColor, colorID, 1);
  179. }
  180. if (stateCounter < BUILDED)
  181. stateCounter++;
  182. }
  183. void CBuildingRect::showAll(SDL_Surface *to)
  184. {
  185. if (stateCounter == 0)
  186. return;
  187. CShowableAnim::showAll(to);
  188. if(!active && parent->selectedBuilding == this && border)
  189. blitAtLoc(border,0,0,to);
  190. }
  191. std::string getBuildingSubtitle(int tid, int bid)//hover text for building
  192. {
  193. const CGTownInstance * t = LOCPLINT->castleInt->town;
  194. bid = hordeToDwellingID(bid);
  195. if (bid<30)//non-dwellings - only buiding name
  196. return CGI->buildh->buildings[tid].find(bid)->second->Name();
  197. else//dwellings - recruit %creature%
  198. {
  199. int creaID = t->creatures[(bid-30)%CREATURES_PER_TOWN].second.back();//taking last of available creatures
  200. return CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creaID]->namePl;
  201. }
  202. }
  203. void CBuildingRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  204. {
  205. if(area && isItIn(&pos,sEvent.x, sEvent.y))
  206. {
  207. if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
  208. {
  209. if(parent->selectedBuilding == this)
  210. {
  211. parent->selectedBuilding = NULL;
  212. GH.statusbar->clear();
  213. }
  214. }
  215. else //inside the area of this building
  216. {
  217. if(! parent->selectedBuilding //no building hovered
  218. || (*parent->selectedBuilding)<(*this)) //or we are on top
  219. {
  220. parent->selectedBuilding = this;
  221. GH.statusbar->print(getBuildingSubtitle(str->townID, str->ID));
  222. }
  223. }
  224. }
  225. }
  226. CDwellingInfoBox::CDwellingInfoBox(int centerX, int centerY, const CGTownInstance *Town, int level)
  227. {
  228. OBJ_CONSTRUCTION_CAPTURING_ALL;
  229. used |= RCLICK;
  230. background = new CPicture("CRTOINFO");
  231. background->colorize(LOCPLINT->playerID);
  232. pos.w = background->pos.w;
  233. pos.h = background->pos.h;
  234. moveTo(Point(centerX - pos.w/2, centerY - pos.h/2));
  235. const CCreature * creature = CGI->creh->creatures[Town->creatures[level].second.back()];
  236. title = new CLabel(80, 30, FONT_SMALL, CENTER, zwykly, creature->namePl);
  237. animation = new CCreaturePic(30, 44, creature, true, true);
  238. std::string text = boost::lexical_cast<std::string>(Town->creatures[level].first);
  239. available = new CLabel(80,190, FONT_SMALL, CENTER, zwykly, CGI->generaltexth->allTexts[217] + text);
  240. costPerTroop = new CLabel(80, 227, FONT_SMALL, CENTER, zwykly, CGI->generaltexth->allTexts[346]);
  241. for(int i = 0; i<RESOURCE_QUANTITY; i++)
  242. {
  243. if(creature->cost[i])
  244. {
  245. resPicture.push_back(new CPicture(graphics->resources32->ourImages[i].bitmap, 0, 0, false));
  246. resAmount.push_back(new CLabel(0,0, FONT_SMALL, CENTER, zwykly, boost::lexical_cast<std::string>(creature->cost[i])));
  247. }
  248. }
  249. int posY = 238;
  250. int posX = pos.w/2 - resAmount.size() * 25 + 5;
  251. for (size_t i=0; i<resAmount.size(); i++)
  252. {
  253. resPicture[i]->moveBy(Point(posX, posY));
  254. resAmount[i]->moveBy(Point(posX+16, posY+43));
  255. posX += 50;
  256. }
  257. }
  258. void CDwellingInfoBox::clickRight(tribool down, bool previousState)
  259. {
  260. if((!down || indeterminate(down)))
  261. GH.popIntTotally(this);
  262. }
  263. void CHeroGSlot::hover (bool on)
  264. {
  265. if(!on)
  266. {
  267. GH.statusbar->clear();
  268. return;
  269. }
  270. CHeroGSlot *other = upg ? owner->garrisonedHero : owner->visitingHero;
  271. std::string temp;
  272. if(hero)
  273. {
  274. if(highlight)//view NNN
  275. {
  276. temp = CGI->generaltexth->tcommands[4];
  277. boost::algorithm::replace_first(temp,"%s",hero->name);
  278. }
  279. else if(other->hero && other->highlight)//exchange
  280. {
  281. temp = CGI->generaltexth->tcommands[7];
  282. boost::algorithm::replace_first(temp,"%s",hero->name);
  283. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  284. }
  285. else// select NNN (in ZZZ)
  286. {
  287. if(upg)//down - visiting
  288. {
  289. temp = CGI->generaltexth->tcommands[32];
  290. boost::algorithm::replace_first(temp,"%s",hero->name);
  291. }
  292. else //up - garrison
  293. {
  294. temp = CGI->generaltexth->tcommands[12];
  295. boost::algorithm::replace_first(temp,"%s",hero->name);
  296. }
  297. }
  298. }
  299. else //we are empty slot
  300. {
  301. if(other->highlight && other->hero) //move NNNN
  302. {
  303. temp = CGI->generaltexth->tcommands[6];
  304. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  305. }
  306. else //empty
  307. {
  308. temp = CGI->generaltexth->allTexts[507];
  309. }
  310. }
  311. if(temp.size())
  312. GH.statusbar->print(temp);
  313. }
  314. void CHeroGSlot::clickLeft(tribool down, bool previousState)
  315. {
  316. CHeroGSlot *other = upg ? owner->garrisonedHero : owner->visitingHero;
  317. if(!down)
  318. {
  319. owner->garr->splitting = false;
  320. owner->garr->highlighted = NULL;
  321. if(hero && highlight)
  322. {
  323. setHighlight(false);
  324. LOCPLINT->openHeroWindow(hero);
  325. }
  326. else if(other->hero && other->highlight)
  327. {
  328. bool allow = true;
  329. if(upg) //moving hero out of town - check if it is allowed
  330. {
  331. if(!hero && LOCPLINT->cb->howManyHeroes(false) >= 8)
  332. {
  333. std::string tmp = CGI->generaltexth->allTexts[18]; //You already have %d adventuring heroes under your command.
  334. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  335. LOCPLINT->showInfoDialog(tmp,std::vector<SComponent*>(), soundBase::sound_todo);
  336. allow = false;
  337. }
  338. else if(!other->hero->stacksCount()) //hero has no creatures - strange, but if we have appropriate error message...
  339. {
  340. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[19],std::vector<SComponent*>(), soundBase::sound_todo); //This hero has no creatures. A hero must have creatures before he can brave the dangers of the countryside.
  341. allow = false;
  342. }
  343. }
  344. setHighlight(false);
  345. other->setHighlight(false);
  346. if(allow)
  347. LOCPLINT->cb->swapGarrisonHero(owner->town);
  348. }
  349. else if(hero)
  350. {
  351. setHighlight(true);
  352. owner->garr->highlighted = NULL;
  353. showAll(screen2);
  354. }
  355. hover(false);hover(true); //refresh statusbar
  356. }
  357. }
  358. void CHeroGSlot::deactivate()
  359. {
  360. highlight = false;
  361. CIntObject::deactivate();
  362. }
  363. void CHeroGSlot::showAll(SDL_Surface * to)
  364. {
  365. if(hero) //there is hero
  366. blitAt(graphics->portraitLarge[hero->portrait],pos,to);
  367. else if(!upg && owner->showEmpty) //up garrison
  368. blitAt(graphics->flags->ourImages[LOCPLINT->castleInt->town->getOwner()].bitmap,pos,to);
  369. if(highlight)
  370. blitAt(graphics->bigImgs[-1],pos,to);
  371. }
  372. CHeroGSlot::CHeroGSlot(int x, int y, int updown, const CGHeroInstance *h, HeroSlots * Owner)
  373. {
  374. used = LCLICK | HOVER;
  375. owner = Owner;
  376. pos.x += x;
  377. pos.y += y;
  378. pos.w = 58;
  379. pos.h = 64;
  380. hero = h;
  381. upg = updown;
  382. highlight = false;
  383. }
  384. CHeroGSlot::~CHeroGSlot()
  385. {
  386. }
  387. void CHeroGSlot::setHighlight( bool on )
  388. {
  389. highlight = on;
  390. if(owner->garrisonedHero->hero && owner->visitingHero->hero) //two heroes in town
  391. {
  392. for(size_t i = 0; i<owner->garr->splitButtons.size(); i++) //splitting enabled when slot higlighted
  393. owner->garr->splitButtons[i]->block(!on);
  394. }
  395. }
  396. template <class ptr>
  397. class SORTHELP
  398. {
  399. public:
  400. bool operator ()
  401. (const ptr *a ,
  402. const ptr *b)
  403. {
  404. return (*a)<(*b);
  405. }
  406. };
  407. SORTHELP<CBuildingRect> buildSorter;
  408. SORTHELP<Structure> structSorter;
  409. CCastleBuildings::CCastleBuildings(const CGTownInstance* Town):
  410. town(Town),
  411. selectedBuilding(NULL)
  412. {
  413. OBJ_CONSTRUCTION_CAPTURING_ALL;
  414. background = new CPicture(graphics->townBgs[town->subID]);
  415. pos.w = background->pos.w;
  416. pos.h = background->pos.h;
  417. //Generate buildings list
  418. for (std::set<si32>::const_iterator building=town->builtBuildings.begin(); building!=town->builtBuildings.end(); building++)
  419. {
  420. std::map<int, Structure*>::iterator structure;
  421. structure = CGI->townh->structures[town->subID].find(*building);
  422. if(structure != CGI->townh->structures[town->subID].end())
  423. {
  424. if(structure->second->group<0) // no group - just add it
  425. buildings.push_back(new CBuildingRect(this, town, structure->second));
  426. else // generate list for each group
  427. groups[structure->second->group].push_back(structure->second);
  428. }
  429. }
  430. Structure * shipyard = CGI->townh->structures[town->subID][6];
  431. //ship in shipyard
  432. if(shipyard && vstd::contains(groups, shipyard->group))
  433. {
  434. std::vector <const CGObjectInstance *> vobjs = LOCPLINT->cb->getVisitableObjs(town->bestLocation());
  435. if(!vobjs.empty() && (vobjs.front()->ID == 8 || vobjs.front()->ID == HEROI_TYPE)) //there is visitable obj at shipyard output tile and it's a boat or hero (on boat)
  436. {
  437. groups[shipyard->group].push_back(CGI->townh->structures[town->subID][20]);
  438. }
  439. }
  440. //Create building for each group
  441. for (std::map< int, std::vector<const Structure*> >::iterator group = groups.begin(); group != groups.end(); group++)
  442. {
  443. std::sort(group->second.begin(), group->second.end(), structSorter);
  444. buildings.push_back(new CBuildingRect(this, town, group->second.back()));
  445. }
  446. std::sort(buildings.begin(),buildings.end(),buildSorter);
  447. checkRules();
  448. }
  449. CCastleBuildings::~CCastleBuildings()
  450. {
  451. }
  452. void CCastleBuildings::checkRules()
  453. {
  454. static const AnimRule animRule[2] =
  455. {
  456. {5, 21, 4, 10, -1, 0, 9},//code for Mana Vortex
  457. {0, 6, 8, 1, -1, 0, 0},//code for the shipyard in the Castle
  458. };
  459. for (size_t i=0; i<2; i++)
  460. {
  461. if ( town->subID != animRule[i].townID ) //wrong town
  462. continue;
  463. int groupID = CGI->townh->structures[town->subID][animRule[i].buildID]->group;
  464. std::map< int, std::vector<const Structure*> >::const_iterator git= groups.find(groupID);
  465. if ( git == groups.end() || git->second.empty() ) //we have no buildings in this group
  466. continue;
  467. int buildID = git->second.back()->ID;
  468. for (std::vector< CBuildingRect* >::const_iterator bit=buildings.begin() ; bit !=buildings.end(); bit++ )
  469. {
  470. if ( (*bit)->str->ID == buildID ) //last building in group
  471. {
  472. if (vstd::contains(town->builtBuildings, animRule[i].toCheck))
  473. (*bit)->set(0,animRule[i].firstA, animRule[i].lastA);
  474. else
  475. (*bit)->set(0,animRule[i].firstB, animRule[i].lastB);
  476. }
  477. }
  478. }
  479. }
  480. void CCastleBuildings::addBuilding(int building)
  481. {
  482. OBJ_CONSTRUCTION_CAPTURING_ALL;
  483. std::map<int, Structure*>::const_iterator structure;
  484. structure = CGI->townh->structures[town->subID].find(building);
  485. if(structure != CGI->townh->structures[town->subID].end()) //we have info about that structure
  486. {
  487. if(structure->second->group<0) //no group - just add it
  488. {
  489. buildings.push_back(new CBuildingRect(this, town, structure->second));
  490. buildings.back()->stateCounter = 0;
  491. }
  492. else
  493. {
  494. //find last building in this group and replace it with new building if needed
  495. groups[structure->second->group].push_back(structure->second);
  496. int newBuilding = groups[structure->second->group].back()->ID;
  497. if (newBuilding == building)
  498. {
  499. for (std::vector< CBuildingRect* >::iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  500. {
  501. if ((*it)->str->ID == newBuilding)
  502. {
  503. delChild(*it);
  504. buildings.erase(it);
  505. break;
  506. }
  507. }
  508. buildings.push_back(new CBuildingRect(this, town, structure->second));
  509. buildings.back()->stateCounter = 0;
  510. }
  511. }
  512. }
  513. std::sort(buildings.begin(),buildings.end(),buildSorter);
  514. checkRules();
  515. }
  516. void CCastleBuildings::removeBuilding(int building)
  517. {
  518. OBJ_CONSTRUCTION_CAPTURING_ALL;
  519. std::map<int, Structure*>::const_iterator structure;
  520. structure = CGI->townh->structures[town->subID].find(building);
  521. if(structure != CGI->townh->structures[town->subID].end()) //we have info about that structure
  522. {
  523. if(structure->second->group<0) //no group - just add it
  524. {
  525. for (std::vector< CBuildingRect* >::iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  526. {
  527. if ((*it)->str->ID == building)
  528. {
  529. delChild(*it);
  530. buildings.erase(it);
  531. break;
  532. }
  533. }
  534. }
  535. else
  536. {
  537. groups[structure->second->group].pop_back();
  538. delChild(buildings[building]);
  539. if (!groups[structure->second->group].empty())
  540. buildings.push_back(new CBuildingRect(this, town, structure->second));
  541. }
  542. }
  543. std::sort(buildings.begin(),buildings.end(),buildSorter);
  544. checkRules();
  545. }
  546. void CCastleBuildings::show(SDL_Surface *to)
  547. {
  548. CIntObject::show(to);
  549. for (std::vector< CBuildingRect* >::const_iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  550. (*it)->show(to);
  551. }
  552. void CCastleBuildings::showAll(SDL_Surface *to)
  553. {
  554. CIntObject::showAll(to);
  555. for (std::vector< CBuildingRect* >::const_iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  556. (*it)->showAll(to);
  557. }
  558. const CGHeroInstance* CCastleBuildings::getHero()
  559. {
  560. if (town->visitingHero)
  561. return town->visitingHero;
  562. if (town->garrisonHero)
  563. return town->garrisonHero;
  564. return NULL;
  565. }
  566. void CCastleBuildings::buildingClicked(int building)
  567. {
  568. tlog5<<"You've clicked on "<<building<<std::endl;
  569. building = hordeToDwellingID(building);
  570. const CBuilding *b = CGI->buildh->buildings[town->subID].find(building)->second;
  571. if(building >= Buildings::DWELL_FIRST)
  572. {
  573. enterDwelling((building-Buildings::DWELL_FIRST)%CREATURES_PER_TOWN);
  574. }
  575. else
  576. {
  577. switch(building)
  578. {
  579. case Buildings::MAGES_GUILD_1:
  580. case Buildings::MAGES_GUILD_2:
  581. case Buildings::MAGES_GUILD_3:
  582. case Buildings::MAGES_GUILD_4:
  583. case Buildings::MAGES_GUILD_5:
  584. enterMagesGuild();
  585. break;
  586. case Buildings::TAVERN:
  587. LOCPLINT->showTavernWindow(town);
  588. break;
  589. case Buildings::SHIPYARD:
  590. LOCPLINT->showShipyardDialog(town);
  591. break;
  592. case Buildings::FORT:
  593. case Buildings::CITADEL:
  594. case Buildings::CASTLE:
  595. GH.pushInt(new CFortScreen(town));
  596. break;
  597. case Buildings::VILLAGE_HALL:
  598. case Buildings::CITY_HALL:
  599. case Buildings::TOWN_HALL:
  600. case Buildings::CAPITOL:
  601. enterTownHall();
  602. break;
  603. case Buildings::MARKETPLACE:
  604. GH.pushInt(new CMarketplaceWindow(town, town->visitingHero));
  605. break;
  606. case Buildings::BLACKSMITH:
  607. enterBlacksmith(town->town->warMachine);
  608. break;
  609. case Buildings::SPECIAL_1:
  610. switch(town->subID)
  611. {
  612. case 1://Mystic Pond
  613. enterFountain(building);
  614. break;
  615. case 2: case 5: case 8://Artifact Merchant
  616. if(town->visitingHero)
  617. GH.pushInt(new CMarketplaceWindow(town, town->visitingHero, RESOURCE_ARTIFACT));
  618. else
  619. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->Name())); //Only visiting heroes may use the %s.
  620. break;
  621. default:
  622. enterBuilding(building);
  623. break;
  624. }
  625. break;
  626. case Buildings::SHIP:
  627. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[51]); //Cannot build another boat
  628. break;
  629. case Buildings::SPECIAL_2:
  630. switch(town->subID)
  631. {
  632. case 1: //Fountain of Fortune
  633. enterFountain(building);
  634. break;
  635. case 6: //Freelancer's Guild
  636. if(getHero())
  637. GH.pushInt(new CMarketplaceWindow(town, getHero(), CREATURE_RESOURCE));
  638. else
  639. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->Name())); //Only visiting heroes may use the %s.
  640. break;
  641. case 8: //Magic University
  642. if (getHero())
  643. GH.pushInt(new CUniversityWindow(getHero(), town));
  644. else
  645. enterBuilding(building);
  646. break;
  647. default:
  648. enterBuilding(building);
  649. break;
  650. }
  651. break;
  652. case Buildings::SPECIAL_3:
  653. switch(town->subID)
  654. {
  655. case 0: //Brotherhood of sword
  656. LOCPLINT->showTavernWindow(town);
  657. break;
  658. case 3: //Castle Gate
  659. enterCastleGate();
  660. break;
  661. case 4: //Skeleton Transformer
  662. GH.pushInt( new CTransformerWindow(getHero(), town) );
  663. break;
  664. case 5: //Portal of Summoning
  665. if (town->creatures[CREATURES_PER_TOWN].second.empty())//No creatures
  666. LOCPLINT->showInfoDialog(CGI->generaltexth->tcommands[30]);
  667. else
  668. enterDwelling(CREATURES_PER_TOWN);
  669. break;
  670. case 6: //Ballista Yard
  671. enterBlacksmith(4);
  672. break;
  673. default:
  674. enterBuilding(building);
  675. break;
  676. }
  677. break;
  678. default:
  679. enterBuilding(building);
  680. break;
  681. }
  682. }
  683. }
  684. void CCastleBuildings::enterBlacksmith(int ArtifactID)
  685. {
  686. const CGHeroInstance *hero = town->visitingHero;
  687. if(!hero)
  688. {
  689. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % CGI->buildh->buildings[town->subID].find(16)->second->Name()));
  690. return;
  691. }
  692. int price = CGI->arth->artifacts[ArtifactID]->price;
  693. bool possible = LOCPLINT->cb->getResourceAmount(Res::GOLD) >= price && !hero->hasArt(ArtifactID+9);
  694. GH.pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(ArtifactID,false),ArtifactID,hero->id));
  695. }
  696. void CCastleBuildings::enterBuilding(int building)
  697. {
  698. std::vector<SComponent*> comps(1,
  699. new SComponent(SComponent::building, town->subID,building,
  700. LOCPLINT->castleInt->bicons->ourImages[building].bitmap, false));
  701. LOCPLINT->showInfoDialog(
  702. CGI->buildh->buildings[town->subID].find(building)->second->Description(),comps);
  703. }
  704. void CCastleBuildings::enterCastleGate()
  705. {
  706. if (!town->visitingHero)
  707. {
  708. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[126]);
  709. return;//only visiting hero can use castle gates
  710. }
  711. std::vector <int> availableTowns;
  712. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(false);
  713. for(size_t i=0;i<Towns.size();i++)
  714. {
  715. const CGTownInstance *t = Towns[i];
  716. if (t->id != this->town->id && t->visitingHero == NULL && //another town, empty and this is
  717. t->subID == 3 && vstd::contains(t->builtBuildings, 22))//inferno with castle gate
  718. {
  719. availableTowns.push_back(t->id);//add to the list
  720. }
  721. }
  722. CPicture *titlePic = new CPicture (LOCPLINT->castleInt->bicons->ourImages[22].bitmap, 0,0, false);//will be deleted by selection window
  723. GH.pushInt (new CObjectListWindow(availableTowns, titlePic, CGI->generaltexth->jktexts[40],
  724. CGI->generaltexth->jktexts[41], boost::bind (&CCastleInterface::castleTeleport, LOCPLINT->castleInt, _1)));
  725. }
  726. void CCastleBuildings::enterDwelling(int level)
  727. {
  728. assert(level >= 0 && level < town->creatures.size());
  729. GH.pushInt(new CRecruitmentWindow(town, level, town, boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2,level), -87));
  730. }
  731. void CCastleBuildings::enterFountain(int building)
  732. {
  733. std::vector<SComponent*> comps(1,
  734. new SComponent(SComponent::building,town->subID,building,
  735. LOCPLINT->castleInt->bicons->ourImages[building].bitmap,false));
  736. std::string descr = CGI->buildh->buildings[town->subID].find(building)->second->Description();
  737. if ( building == 21)//we need description for mystic pond as well
  738. descr += "\n\n"+CGI->buildh->buildings[town->subID].find(17)->second->Description();
  739. if (town->bonusValue.first == 0)//fountain was builded this week
  740. descr += "\n\n"+ CGI->generaltexth->allTexts[677];
  741. else//fountain produced something;
  742. {
  743. descr+= "\n\n"+ CGI->generaltexth->allTexts[678];
  744. boost::algorithm::replace_first(descr,"%s",CGI->generaltexth->restypes[town->bonusValue.first]);
  745. boost::algorithm::replace_first(descr,"%d",boost::lexical_cast<std::string>(town->bonusValue.second));
  746. }
  747. LOCPLINT->showInfoDialog(descr, comps);
  748. }
  749. void CCastleBuildings::enterMagesGuild()
  750. {
  751. const CGHeroInstance *hero = getHero();
  752. if(hero && !hero->hasSpellbook()) //hero doesn't have spellbok
  753. {
  754. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < 500) //not enough gold to buy spellbook
  755. {
  756. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213]);
  757. }
  758. else
  759. {
  760. CFunctionList<void()> functionList = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb, hero,0);
  761. functionList += boost::bind(&CCastleBuildings::openMagesGuild,this);
  762. std::vector<SComponent*> components(1, new SComponent(SComponent::artifact,0,0));
  763. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214], components, functionList, 0, true);
  764. }
  765. }
  766. else
  767. {
  768. openMagesGuild();
  769. }
  770. }
  771. void CCastleBuildings::enterTownHall()
  772. {
  773. if(town->visitingHero && town->visitingHero->hasArt(2) &&
  774. !vstd::contains(town->builtBuildings, Buildings::GRAIL)) //hero has grail, but town does not have it
  775. {
  776. if(!vstd::contains(town->forbiddenBuildings, Buildings::GRAIL))
  777. {
  778. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[597], //Do you wish this to be the permanent home of the Grail?
  779. std::vector<SComponent*>(),
  780. boost::bind(&CCallback::buildBuilding, LOCPLINT->cb, town, Buildings::GRAIL),
  781. boost::bind(&CCastleBuildings::openTownHall, this), true);
  782. }
  783. else
  784. {
  785. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[673]);
  786. (dynamic_cast<CInfoWindow*>(GH.topInt()))->buttons[0]->callback += boost::bind(&CCastleBuildings::openTownHall, this);
  787. }
  788. }
  789. else
  790. {
  791. openTownHall();
  792. }
  793. }
  794. void CCastleBuildings::openMagesGuild()
  795. {
  796. GH.pushInt(new CMageGuildScreen(LOCPLINT->castleInt));
  797. }
  798. void CCastleBuildings::openTownHall()
  799. {
  800. GH.pushInt(new CHallInterface(town));
  801. }
  802. CCastleInterface::CCastleInterface(const CGTownInstance * Town, int listPos):
  803. hall(NULL),
  804. fort(NULL),
  805. town(Town)
  806. {
  807. OBJ_CONSTRUCTION_CAPTURING_ALL;
  808. used |= KEYBOARD;
  809. builds = new CCastleBuildings(town);
  810. panel = new CPicture("TOWNSCRN", 0, builds->pos.h);
  811. panel->colorize(LOCPLINT->playerID);
  812. pos.w = panel->pos.w;
  813. pos.h = builds->pos.h + panel->pos.h;
  814. center();
  815. garr = new CGarrisonInt(305, 387, 4, Point(0,96), panel->bg, Point(62,374), town->getUpperArmy(), town->visitingHero);
  816. heroes = new HeroSlots(town, Point(241, 387), Point(241, 483), garr, true);
  817. title = new CLabel(85, 387, FONT_MEDIUM, TOPLEFT, zwykly, town->name);
  818. income = new CLabel(195, 443, FONT_SMALL, CENTER);
  819. icon = new CAnimImage("ITPT", 0, 0, 15, 387);
  820. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CCastleInterface::close,this), 744, 544, "TSBTNS", SDLK_RETURN);
  821. exit->assignedKeys.insert(SDLK_ESCAPE);
  822. exit->setOffset(4);
  823. split = new AdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick,garr), 744, 382, "TSBTNS.DEF");
  824. split->callback += boost::bind(&HeroSlots::splitClicked, heroes);
  825. removeChild(split);
  826. garr->addSplitBtn(split);
  827. Rect barRect(9, 182, 732, 18);
  828. statusbar = new CGStatusBar(new CPicture(*panel, barRect, 9, 555, false));
  829. resdatabar = new CResDataBar("ZRESBAR", 3, 575, 32, 2, 85, 85);
  830. townlist = new CTownList(3, 744, 414, "IAM014", "IAM015");
  831. townlist->fun = boost::bind(&CCastleInterface::townChange, this);
  832. townlist->selected = vstd::findPos(LOCPLINT->towns, Town);
  833. townlist->from = townlist->selected - listPos;
  834. amax(townlist->from, 0);
  835. amin(townlist->from, LOCPLINT->towns.size() - townlist->SIZE);
  836. LOCPLINT->castleInt = this;
  837. recreateIcons();
  838. CCS->musich->playMusic(CCS->musich->townMusics[town->subID], -1);
  839. bicons = CDefHandler::giveDefEss(graphics->buildingPics[town->subID]);
  840. }
  841. CCastleInterface::~CCastleInterface()
  842. {
  843. LOCPLINT->castleInt = NULL;
  844. delete bicons;
  845. }
  846. void CCastleInterface::close()
  847. {
  848. if(town->tempOwner == LOCPLINT->playerID) //we may have opened window for an allied town
  849. {
  850. if(town->visitingHero)
  851. adventureInt->select(town->visitingHero);
  852. else
  853. adventureInt->select(town);
  854. }
  855. GH.popIntTotally(this);
  856. }
  857. void CCastleInterface::showAll(SDL_Surface *to)
  858. {
  859. CIntObject::showAll(to);
  860. if(screen->w != 800 || screen->h !=600)
  861. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  862. }
  863. void CCastleInterface::castleTeleport(int where)
  864. {
  865. const CGTownInstance * dest = LOCPLINT->cb->getTown(where);
  866. LOCPLINT->cb->teleportHero(town->visitingHero, dest);
  867. }
  868. void CCastleInterface::townChange()
  869. {
  870. const CGTownInstance * nt = LOCPLINT->towns[townlist->selected];
  871. int tpos = townlist->selected - townlist->from;
  872. if ( nt == town )
  873. return;
  874. GH.popIntTotally(this);
  875. GH.pushInt(new CCastleInterface(nt, tpos));
  876. }
  877. void CCastleInterface::addBuilding(int bid)
  878. {
  879. deactivate();
  880. builds->addBuilding(bid);
  881. recreateIcons();
  882. activate();
  883. }
  884. void CCastleInterface::removeBuilding(int bid)
  885. {
  886. deactivate();
  887. builds->removeBuilding(bid);
  888. recreateIcons();
  889. activate();
  890. }
  891. void CCastleInterface::recreateIcons()
  892. {
  893. OBJ_CONSTRUCTION_CAPTURING_ALL;
  894. if (fort)
  895. delChild(fort);
  896. if (hall)
  897. delChild(hall);
  898. size_t iconIndex = town->subID*2;
  899. if (!town->hasFort())
  900. iconIndex += F_NUMBER*2;
  901. if(town->builded >= MAX_BUILDING_PER_TURN)
  902. iconIndex++;
  903. icon->setFrame(iconIndex);
  904. income->setTxt(boost::lexical_cast<std::string>(town->dailyIncome()));
  905. hall = new CTownInfo( 80, 413, town, true);
  906. fort = new CTownInfo(122, 413, town, false);
  907. for (size_t i=0; i<creainfo.size(); i++)
  908. delChild(creainfo[i]);
  909. creainfo.clear();
  910. for (size_t i=0; i<4; i++)
  911. creainfo.push_back(new CCreaInfo(Point(14+55*i, 459), town, i));
  912. for (size_t i=0; i<4; i++)
  913. creainfo.push_back(new CCreaInfo(Point(14+55*i, 507), town, i+4));
  914. }
  915. CCreaInfo::CCreaInfo(Point position, const CGTownInstance *Town, int Level, bool compact, bool ShowAvailable):
  916. town(Town),
  917. level(Level),
  918. showAvailable(ShowAvailable)
  919. {
  920. OBJ_CONSTRUCTION_CAPTURING_ALL;
  921. pos += position;
  922. if ( town->creatures.size() <= level || town->creatures[level].second.empty())
  923. {
  924. level = -1;
  925. label = NULL;
  926. picture = NULL;
  927. return;//No creature
  928. }
  929. used = LCLICK | RCLICK | HOVER;
  930. unsigned int creatureID = town->creatures[level].second.back();
  931. creature = CGI->creh->creatures[creatureID];
  932. picture = new CAnimImage("CPRSMALL", creatureID+2, 0, 8, 0);
  933. std::string value;
  934. if (showAvailable)
  935. value = boost::lexical_cast<std::string>(town->creatures[level].first);
  936. else
  937. value = boost::lexical_cast<std::string>(town->creatureGrowth(level));
  938. if (compact)
  939. {
  940. label = new CLabel(40, 32, FONT_TINY, BOTTOMRIGHT, zwykly, value);
  941. pos.x += 8;
  942. pos.w = 32;
  943. pos.h = 32;
  944. }
  945. else
  946. {
  947. label = new CLabel(24, 40, FONT_SMALL, CENTER, zwykly, value);
  948. pos.w = 48;
  949. pos.h = 48;
  950. }
  951. }
  952. void CCreaInfo::update()
  953. {
  954. if (label)
  955. {
  956. std::string value;
  957. if (showAvailable)
  958. value = boost::lexical_cast<std::string>(town->creatures[level].first);
  959. else
  960. value = boost::lexical_cast<std::string>(town->creatureGrowth(level));
  961. if (value != label->text)
  962. label->setTxt(value);
  963. }
  964. }
  965. void CCreaInfo::hover(bool on)
  966. {
  967. std::string message = CGI->generaltexth->allTexts[588];
  968. boost::algorithm::replace_first(message,"%s",creature->namePl);
  969. if(on)
  970. {
  971. GH.statusbar->print(message);
  972. }
  973. else if (message == GH.statusbar->getCurrent())
  974. GH.statusbar->clear();
  975. }
  976. void CCreaInfo::clickLeft(tribool down, bool previousState)
  977. {
  978. if(previousState && (!down))
  979. {
  980. int offset = LOCPLINT->castleInt? (-87) : 0;
  981. GH.pushInt(new CRecruitmentWindow(town, level, town,
  982. boost::bind(&CCallback::recruitCreatures, LOCPLINT->cb, town, _1, _2, level), offset));
  983. }
  984. }
  985. int CCreaInfo::AddToString(std::string from, std::string & to, int numb)
  986. {
  987. if (numb == 0)
  988. return 0;
  989. boost::algorithm::replace_first(from,"%+d", (numb > 0 ? "+" : "")+boost::lexical_cast<std::string>(numb)); //negative values don't need "+"
  990. to+="\n"+from;
  991. return numb;
  992. }
  993. std::string CCreaInfo::genGrowthText()
  994. {
  995. GrowthInfo gi = town->getGrowthInfo(level);
  996. std::string descr = boost::str(boost::format(CGI->generaltexth->allTexts[589]) % creature->nameSing % gi.totalGrowth());
  997. BOOST_FOREACH(const GrowthInfo::Entry &entry, gi.entries)
  998. {
  999. descr +="\n" + entry.description;
  1000. }
  1001. return descr;
  1002. }
  1003. void CCreaInfo::clickRight(tribool down, bool previousState)
  1004. {
  1005. if(down)
  1006. {
  1007. if (showAvailable)
  1008. GH.pushInt(new CDwellingInfoBox(screen->w/2, screen->h/2, town, level));
  1009. else
  1010. {
  1011. std::string descr = genGrowthText();
  1012. CInfoPopup *mess = new CInfoPopup();//creating popup
  1013. mess->free = true;
  1014. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1015. (LOCPLINT->playerID, descr,graphics->bigImgs[creature->idNumber],"");
  1016. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1017. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1018. GH.pushInt(mess);
  1019. }
  1020. }
  1021. }
  1022. CTownInfo::CTownInfo(int posX, int posY, const CGTownInstance* Town, bool townHall):
  1023. town(Town),
  1024. building(NULL)
  1025. {
  1026. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1027. used = RCLICK | HOVER;
  1028. pos.x += posX;
  1029. pos.y += posY;
  1030. int buildID;
  1031. if (townHall)
  1032. {
  1033. buildID = 10 + town->hallLevel();
  1034. picture = new CAnimImage("ITMTL.DEF", town->hallLevel());
  1035. }
  1036. else
  1037. {
  1038. buildID = 6 + town->fortLevel();
  1039. if (buildID == 6)
  1040. return;
  1041. picture = new CAnimImage("ITMCL.DEF", town->fortLevel()-1);
  1042. }
  1043. building = CGI->buildh->buildings[town->subID][buildID];
  1044. pos = picture->pos;
  1045. }
  1046. void CTownInfo::hover(bool on)
  1047. {
  1048. if(on)
  1049. {
  1050. if ( building )
  1051. GH.statusbar->print(building->Name());
  1052. }
  1053. else
  1054. GH.statusbar->clear();
  1055. }
  1056. void CTownInfo::clickRight(tribool down, bool previousState)
  1057. {//FIXME: castleInt may be NULL
  1058. if(down && building && LOCPLINT->castleInt)
  1059. {
  1060. CInfoPopup *mess = new CInfoPopup();
  1061. mess->free = true;
  1062. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1063. (LOCPLINT->playerID,
  1064. building->Description(),
  1065. LOCPLINT->castleInt->bicons->ourImages[building->bid].bitmap,
  1066. building->Name());
  1067. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1068. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1069. GH.pushInt(mess);
  1070. }
  1071. }
  1072. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  1073. {
  1074. if(key.state != SDL_PRESSED) return;
  1075. switch(key.keysym.sym)
  1076. {
  1077. case SDLK_UP:
  1078. if(townlist->selected)
  1079. {
  1080. townlist->selected--;
  1081. townlist->from--;
  1082. townChange();
  1083. }
  1084. break;
  1085. case SDLK_DOWN:
  1086. if(townlist->selected < LOCPLINT->towns.size() - 1)
  1087. {
  1088. townlist->selected++;
  1089. townlist->from++;
  1090. townChange();
  1091. }
  1092. break;
  1093. case SDLK_SPACE:
  1094. if(!!town->visitingHero && town->garrisonHero)
  1095. {
  1096. LOCPLINT->cb->swapGarrisonHero(town);
  1097. }
  1098. break;
  1099. default:
  1100. break;
  1101. }
  1102. }
  1103. HeroSlots::HeroSlots(const CGTownInstance * Town, Point garrPos, Point visitPos, CGarrisonInt *Garrison, bool ShowEmpty):
  1104. showEmpty(ShowEmpty),
  1105. town(Town),
  1106. garr(Garrison)
  1107. {
  1108. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1109. garrisonedHero = new CHeroGSlot(garrPos.x, garrPos.y, 0, town->garrisonHero, this);
  1110. visitingHero = new CHeroGSlot(visitPos.x, visitPos.y, 1, town->visitingHero, this);
  1111. }
  1112. void HeroSlots::update()
  1113. {
  1114. garrisonedHero->hero = town->garrisonHero;
  1115. visitingHero->hero = town->visitingHero;
  1116. }
  1117. void HeroSlots::splitClicked()
  1118. {
  1119. if(!!town->visitingHero && town->garrisonHero && (visitingHero->highlight || garrisonedHero->highlight))
  1120. {
  1121. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  1122. }
  1123. }
  1124. void CHallInterface::CBuildingBox::hover(bool on)
  1125. {
  1126. if(on)
  1127. {
  1128. std::string toPrint;
  1129. if(state==Buildings::PREREQUIRES)
  1130. toPrint = CGI->generaltexth->hcommands[5];
  1131. else if(state==Buildings::CANT_BUILD_TODAY)
  1132. toPrint = CGI->generaltexth->allTexts[223];
  1133. else
  1134. toPrint = CGI->generaltexth->hcommands[state];
  1135. boost::algorithm::replace_first(toPrint,"%s",building->Name());
  1136. GH.statusbar->print(toPrint);
  1137. }
  1138. else
  1139. GH.statusbar->clear();
  1140. }
  1141. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1142. {
  1143. if(previousState && (!down))
  1144. GH.pushInt(new CBuildWindow(town,building,state,0));
  1145. }
  1146. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1147. {
  1148. if(down)
  1149. GH.pushInt(new CBuildWindow(town,building,state,1));
  1150. }
  1151. CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance * Town, const CBuilding * Building):
  1152. town(Town),
  1153. building(Building)
  1154. {
  1155. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1156. used = LCLICK | RCLICK | HOVER;
  1157. pos.x += x;
  1158. pos.y += y;
  1159. pos.w = 154;
  1160. pos.h = 92;
  1161. state = LOCPLINT->cb->canBuildStructure(town,building->bid);
  1162. assert(state < Buildings::ERROR);
  1163. static int panelIndex[9] = { 3, 3, 3, 0, 0, 2, 2, 1, 2};
  1164. static int iconIndex[9] = {-1, -1, -1, 0, 0, 1, 2, -1, 1};
  1165. picture = new CAnimImage(graphics->buildingPics[town->subID], building->bid, 0, 2, 2);
  1166. panel = new CAnimImage("TPTHBAR", panelIndex[state], 0, 1, 73);
  1167. if ( iconIndex[state] >=0 )
  1168. icon = new CAnimImage("TPTHCHK", iconIndex[state], 0, 136, 56);
  1169. label = new CLabel(75, 81, FONT_SMALL, CENTER, zwykly, building->Name());
  1170. }
  1171. CHallInterface::CHallInterface(const CGTownInstance *Town):
  1172. town(Town)
  1173. {
  1174. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1175. background = new CPicture(CGI->buildh->hall[town->subID].first);
  1176. background->colorize(LOCPLINT->playerID);
  1177. pos = background->center();
  1178. resdatabar = new CMinorResDataBar;
  1179. resdatabar->pos.x += pos.x;
  1180. resdatabar->pos.y += pos.y;
  1181. Rect barRect(5, 556, 740, 18);
  1182. statusBar = new CGStatusBar(new CPicture(*background, barRect, 5, 556, false));
  1183. title = new CLabel(399, 12, FONT_MEDIUM, CENTER, zwykly, CGI->buildh->buildings[town->subID][town->hallLevel()+Buildings::VILLAGE_HALL]->Name());
  1184. exit = new AdventureMapButton(CGI->generaltexth->hcommands[8], "",
  1185. boost::bind(&CHallInterface::close,this), 748, 556, "TPMAGE1.DEF", SDLK_RETURN);
  1186. exit->assignedKeys.insert(SDLK_ESCAPE);
  1187. const std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[town->subID].second;
  1188. boxes.resize(boxList.size());
  1189. for(size_t row=0; row<boxList.size(); row++) //for each row
  1190. {
  1191. for(size_t col=0; col<boxList[row].size(); col++) //for each box
  1192. {
  1193. const CBuilding *building = NULL;
  1194. for(size_t item=0; item<boxList[row][col].size(); item++)//we are looking for the first not build structure
  1195. {
  1196. int buildingID = boxList[row][col][item];
  1197. building = CGI->buildh->buildings[town->subID][buildingID];
  1198. //Creature hordes - select unupgraded version if dwelling upgrade was not build yet
  1199. if (buildingID == 18 && !vstd::contains(town->builtBuildings, town->town->hordeLvl[0]+37))
  1200. break;
  1201. if (buildingID == 24 && !vstd::contains(town->builtBuildings, town->town->hordeLvl[1]+37))
  1202. break;
  1203. if(vstd::contains(town->builtBuildings,buildingID))
  1204. continue;
  1205. break;
  1206. }
  1207. int posX = pos.w/2 - boxList[row].size()*154/2 - (boxList[row].size()-1)*20 + 194*col,
  1208. posY = 35 + 104*row;
  1209. if (building)
  1210. boxes[row].push_back(new CBuildingBox(posX, posY, town, building));
  1211. }
  1212. }
  1213. }
  1214. void CHallInterface::close()
  1215. {
  1216. GH.popIntTotally(this);
  1217. }
  1218. void CBuildWindow::buyFunc()
  1219. {
  1220. LOCPLINT->cb->buildBuilding(town,building->bid);
  1221. GH.popInts(2); //we - build window and hall screen
  1222. }
  1223. void CBuildWindow::close()
  1224. {
  1225. GH.popIntTotally(this);
  1226. }
  1227. void CBuildWindow::clickRight(tribool down, bool previousState)
  1228. {
  1229. if((!down || indeterminate(down)) && mode)
  1230. close();
  1231. }
  1232. std::string CBuildWindow::getTextForState(int state)
  1233. {
  1234. std::string ret;
  1235. if(state<7)
  1236. ret = CGI->generaltexth->hcommands[state];
  1237. switch (state)
  1238. {
  1239. case 4: case 5: case 6:
  1240. ret.replace(ret.find_first_of("%s"),2,building->Name());
  1241. break;
  1242. case 7:
  1243. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1244. case 8:
  1245. {
  1246. ret = CGI->generaltexth->allTexts[52];
  1247. std::set<int> reqs= LOCPLINT->cb->getBuildingRequiments(town, building->bid);
  1248. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1249. {
  1250. if (vstd::contains(town->builtBuildings, *i))
  1251. continue;//skipping constructed buildings
  1252. ret+= CGI->buildh->buildings[town->subID][*i]->Name() + ", ";
  1253. }
  1254. ret.erase(ret.size()-2);
  1255. }
  1256. }
  1257. return ret;
  1258. }
  1259. CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, int State, bool Mode)
  1260. :town(Town), building(Building), state(State), mode(Mode)
  1261. {
  1262. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1263. used |= RCLICK;
  1264. background = new CPicture("TPUBUILD");
  1265. pos = background->center();
  1266. background->colorize(LOCPLINT->playerID);
  1267. buildingPic = new CAnimImage(graphics->buildingPics[town->subID], building->bid, 0, 125, 50);
  1268. Rect barRect(9, 494, 380, 18);
  1269. statusBar = new CGStatusBar(new CPicture(*background, barRect, 9, 494, false));
  1270. title = new CLabel(197, 30, FONT_MEDIUM, CENTER, zwykly,
  1271. boost::str(boost::format(CGI->generaltexth->hcommands[7]) % building->Name()));
  1272. buildingDescr = new CTextBox(building->Description(), Rect(33, 135, 329, 67), 0, FONT_MEDIUM, CENTER);
  1273. buildingState = new CTextBox(getTextForState(state), Rect(33, 216, 329, 67), 0, FONT_SMALL, CENTER);
  1274. //Create objects for all required resources
  1275. for(int i = 0; i<RESOURCE_QUANTITY; i++)
  1276. {
  1277. if(building->resources[i])
  1278. {
  1279. resPicture.push_back(new CPicture(graphics->resources32->ourImages[i].bitmap, 0, 0, false));
  1280. resAmount.push_back(new CLabel(0,0, FONT_SMALL, CENTER, zwykly,
  1281. boost::lexical_cast<std::string>(building->resources[i])));
  1282. }
  1283. }
  1284. unsigned int rowSize[2];
  1285. int posY;
  1286. if (resAmount.size() > 4)
  1287. {//Resources will be placed in multiple rows
  1288. rowSize[0] = (resAmount.size()+1)/2;
  1289. posY = 303;
  1290. }
  1291. else
  1292. {//one row
  1293. rowSize[0] = resAmount.size();
  1294. posY = 340;
  1295. }
  1296. rowSize[1] = resAmount.size() - rowSize[0];
  1297. unsigned int index=0;
  1298. for (size_t row=0; row<2; row++)
  1299. {
  1300. int posX = pos.w/2 - rowSize[row] * 40 + 24;
  1301. for (size_t i=0; i<rowSize[row]; i++)
  1302. {//Move current resource to correct position
  1303. resPicture[index]->moveBy(Point(posX, posY));
  1304. resAmount[index]->moveBy(Point(posX+16, posY+48));
  1305. posX += 80;
  1306. index++;
  1307. }
  1308. posY +=75;
  1309. }
  1310. if(!mode)
  1311. {
  1312. buy = new AdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[595]) % building->Name()),
  1313. "", boost::bind(&CBuildWindow::buyFunc,this), 45, 446,"IBUY30", SDLK_RETURN);
  1314. buy->borderColor = Colors::MetallicGold;
  1315. buy->borderEnabled = true;
  1316. cancel = new AdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[596]) % building->Name()),
  1317. "", boost::bind(&CBuildWindow::close,this), 290, 445, "ICANCEL", SDLK_ESCAPE);
  1318. cancel->borderColor = Colors::MetallicGold;
  1319. cancel->borderEnabled = true;
  1320. buy->block(state!=7);
  1321. }
  1322. }
  1323. CBuildWindow::~CBuildWindow()
  1324. {}
  1325. void CFortScreen::close()
  1326. {
  1327. GH.popIntTotally(this);
  1328. }
  1329. CFortScreen::CFortScreen(const CGTownInstance * town)
  1330. {
  1331. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1332. unsigned int fortSize = town->creatures.size();
  1333. if (fortSize > CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1334. fortSize--;
  1335. if (fortSize == CREATURES_PER_TOWN)
  1336. background = new CPicture("TPCASTL7");
  1337. else
  1338. background = new CPicture("TPCASTL8");
  1339. background->colorize(LOCPLINT->playerID);
  1340. pos = background->center();
  1341. const CBuilding *fortBuilding = CGI->buildh->buildings[town->subID][town->fortLevel()+6];
  1342. title = new CLabel(400, 12, FONT_BIG, CENTER, zwykly, fortBuilding->Name());
  1343. std::string text = boost::str(boost::format(CGI->generaltexth->fcommands[6]) % fortBuilding->Name());
  1344. exit = new AdventureMapButton(text, "", boost::bind(&CFortScreen::close,this) ,748, 556, "TPMAGE1", SDLK_RETURN);
  1345. std::vector<Point> positions;
  1346. positions += Point(10, 22), Point(404, 22),
  1347. Point(10, 155), Point(404,155),
  1348. Point(10, 288), Point(404,288);
  1349. if (fortSize == CREATURES_PER_TOWN)
  1350. positions += Point(206,421);
  1351. else
  1352. positions += Point(10, 421), Point(404,421);
  1353. for (unsigned int i=0; i<fortSize; i++)
  1354. {
  1355. int buildingID;
  1356. if (fortSize == CREATURES_PER_TOWN)
  1357. {
  1358. if (vstd::contains(town->builtBuildings, Buildings::DWELL_UP_FIRST+i))
  1359. buildingID = Buildings::DWELL_UP_FIRST+i;
  1360. else
  1361. buildingID = Buildings::DWELL_FIRST+i;
  1362. }
  1363. else
  1364. buildingID = 22;
  1365. recAreas.push_back(new RecruitArea(positions[i].x, positions[i].y, town, buildingID, i));
  1366. }
  1367. resdatabar = new CMinorResDataBar;
  1368. resdatabar->pos.x += pos.x;
  1369. resdatabar->pos.y += pos.y;
  1370. Rect barRect(4, 554, 740, 18);
  1371. statusBar = new CGStatusBar(new CPicture(*background, barRect, 4, 554, false));
  1372. }
  1373. void CFortScreen::creaturesChanged()
  1374. {
  1375. for (size_t i=0; i<recAreas.size(); i++)
  1376. recAreas[i]->creaturesChanged();
  1377. }
  1378. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int min, int max)
  1379. {
  1380. pos.x+=size.x;
  1381. pos.y+=size.y;
  1382. pos.w = size.w;
  1383. pos.h = size.h;
  1384. init(name, descr, min, max);
  1385. }
  1386. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int val)
  1387. {
  1388. pos.x+=size.x;
  1389. pos.y+=size.y;
  1390. pos.w = size.w;
  1391. pos.h = size.h;
  1392. init(name, descr, val, val);
  1393. }
  1394. void LabeledValue::init(std::string nameText, std::string descr, int min, int max)
  1395. {
  1396. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1397. used |= HOVER;
  1398. hoverText = descr;
  1399. std::string valueText;
  1400. if (min && max)
  1401. {
  1402. valueText = boost::lexical_cast<std::string>(min);
  1403. if (min != max)
  1404. valueText += '-' + boost::lexical_cast<std::string>(max);
  1405. }
  1406. name = new CLabel(3, 0, FONT_SMALL, TOPLEFT, zwykly, nameText);
  1407. value = new CLabel(pos.w-3, pos.h-2, FONT_SMALL, BOTTOMRIGHT, zwykly, valueText);
  1408. }
  1409. void LabeledValue::hover(bool on)
  1410. {
  1411. if(on)
  1412. GH.statusbar->print(hoverText);
  1413. else
  1414. {
  1415. GH.statusbar->clear();
  1416. parent->hovered = false;
  1417. }
  1418. }
  1419. CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance *Town, int buildingID, int Level):
  1420. town(Town),
  1421. level(Level),
  1422. availableCount(NULL)
  1423. {
  1424. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1425. pos.x +=posX;
  1426. pos.y +=posY;
  1427. pos.w = 386;
  1428. pos.h = 126;
  1429. if (!town->creatures[level].second.empty())
  1430. used |= LCLICK | RCLICK | HOVER;//Activate only if dwelling is present
  1431. icons = new CPicture("ZPCAINFO", 261, 3);
  1432. buildingPic = new CAnimImage(graphics->buildingPics[town->subID], buildingID, 0, 4, 21);
  1433. const CCreature* creature = NULL;
  1434. if (!town->creatures[level].second.empty())
  1435. creature = CGI->creh->creatures[town->creatures[level].second.back()];
  1436. else
  1437. creature = CGI->creh->creatures[town->town->basicCreatures[level]];
  1438. hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % creature->namePl);
  1439. creatureAnim = new CCreaturePic(159, 4, creature, false);
  1440. Rect sizes(287, 4, 96, 18);
  1441. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], creature->attack));
  1442. sizes.y+=20;
  1443. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[191], CGI->generaltexth->fcommands[1], creature->defence));
  1444. sizes.y+=21;
  1445. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[199], CGI->generaltexth->fcommands[2], creature->damageMin, creature->damageMax));
  1446. sizes.y+=20;
  1447. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[388], CGI->generaltexth->fcommands[3], creature->hitPoints));
  1448. sizes.y+=21;
  1449. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[193], CGI->generaltexth->fcommands[4], creature->speed));
  1450. sizes.y+=20;
  1451. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[194], CGI->generaltexth->fcommands[5], town->creatureGrowth(level)));
  1452. creatureName = new CLabel(78, 11, FONT_SMALL, CENTER, zwykly, creature->namePl);
  1453. dwellingName = new CLabel(78, 101, FONT_SMALL, CENTER, zwykly, CGI->buildh->buildings[town->subID][buildingID]->Name());
  1454. if (vstd::contains(town->builtBuildings, buildingID))
  1455. {
  1456. unsigned int available = town->creatures[level].first;
  1457. std::string availableText = CGI->generaltexth->allTexts[217]+ boost::lexical_cast<std::string>(available);
  1458. availableCount = new CLabel(78, 119, FONT_SMALL, CENTER, zwykly, availableText);
  1459. }
  1460. }
  1461. void CFortScreen::RecruitArea::hover(bool on)
  1462. {
  1463. if(on)
  1464. GH.statusbar->print(hoverText);
  1465. else
  1466. GH.statusbar->clear();
  1467. }
  1468. void CFortScreen::RecruitArea::creaturesChanged()
  1469. {
  1470. if (availableCount)
  1471. {
  1472. std::string availableText = CGI->generaltexth->allTexts[217] +
  1473. boost::lexical_cast<std::string>(town->creatures[level].first);
  1474. availableCount->setTxt(availableText);
  1475. }
  1476. }
  1477. void CFortScreen::RecruitArea::clickLeft(tribool down, bool previousState)
  1478. {
  1479. if(!down && previousState)
  1480. LOCPLINT->castleInt->builds->enterDwelling(level);
  1481. }
  1482. void CFortScreen::RecruitArea::clickRight(tribool down, bool previousState)
  1483. {
  1484. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1485. }
  1486. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1487. {
  1488. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1489. background = new CPicture("TPMAGE.bmp");
  1490. pos = background->center();
  1491. window = new CPicture(graphics->guildBgs[owner->town->subID], 332, 76);
  1492. resdatabar = new CMinorResDataBar;
  1493. resdatabar->pos.x += pos.x;
  1494. resdatabar->pos.y += pos.y;
  1495. Rect barRect(7, 556, 737, 18);
  1496. statusBar = new CGStatusBar(new CPicture(*background, barRect, 7, 556, false));
  1497. exit = new AdventureMapButton(CGI->generaltexth->allTexts[593],"",boost::bind(&CMageGuildScreen::close,this), 748, 556,"TPMAGE1.DEF",SDLK_RETURN);
  1498. exit->assignedKeys.insert(SDLK_ESCAPE);
  1499. std::vector<std::vector<Point> > positions;
  1500. positions.resize(5);
  1501. positions[0] += Point(222,445), Point(312,445), Point(402,445), Point(520,445), Point(610,445), Point(700,445);
  1502. positions[1] += Point(48,53), Point(48,147), Point(48,241), Point(48,335), Point(48,429);
  1503. positions[2] += Point(570,82), Point(672,82), Point(570,157), Point(672,157);
  1504. positions[3] += Point(183,42), Point(183,148), Point(183,253);
  1505. positions[4] += Point(491,325), Point(591,325);
  1506. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1507. {
  1508. size_t spellCount = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1509. for(size_t j=0; j<spellCount; j++)
  1510. {
  1511. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1512. spells.push_back( new Scroll(positions[i][j], CGI->spellh->spells[owner->town->spells[i][j]]));
  1513. else
  1514. new CAnimImage("TPMAGES.DEF", 1, 0, positions[i][j].x, positions[i][j].y);//closed scroll
  1515. }
  1516. }
  1517. }
  1518. void CMageGuildScreen::close()
  1519. {
  1520. GH.popIntTotally(this);
  1521. }
  1522. CMageGuildScreen::Scroll::Scroll(Point position, const CSpell *Spell)
  1523. :spell(Spell)
  1524. {
  1525. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1526. used = LCLICK | RCLICK | HOVER;
  1527. pos += position;
  1528. image = new CAnimImage("SPELLSCR", spell->id);
  1529. pos = image->pos;
  1530. }
  1531. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1532. {
  1533. if(down)
  1534. {
  1535. std::vector<SComponent*> comps(1,
  1536. new SComponent(SComponent::spell,spell->id,0)
  1537. );
  1538. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1539. }
  1540. }
  1541. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1542. {
  1543. if(down)
  1544. {
  1545. CInfoPopup *vinya = new CInfoPopup();
  1546. vinya->free = true;
  1547. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1548. (LOCPLINT->playerID,
  1549. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1550. spell->name,30,30);
  1551. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1552. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1553. GH.pushInt(vinya);
  1554. }
  1555. }
  1556. void CMageGuildScreen::Scroll::hover(bool on)
  1557. {
  1558. if(on)
  1559. GH.statusbar->print(spell->name);
  1560. else
  1561. GH.statusbar->clear();
  1562. }
  1563. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1564. {
  1565. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1566. background = new CPicture("TPSMITH");
  1567. pos = background->center();
  1568. background->colorize(LOCPLINT->playerID);
  1569. statusBar = new CGStatusBar(164, 370);
  1570. animBG = new CPicture("TPSMITBK", 64, 50);
  1571. animBG->needRefresh = true;
  1572. const CCreature *creature = CGI->creh->creatures[creMachineID];
  1573. anim = new CCreatureAnim(64, 50, creature->animDefName, Rect());
  1574. anim->clipRect(113,125,200,150);
  1575. title = new CLabel(165, 28, FONT_BIG, CENTER, tytulowy,
  1576. boost::str(boost::format(CGI->generaltexth->allTexts[274]) % creature->nameSing));
  1577. costText = new CLabel(165, 218, FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->jktexts[43]);
  1578. costValue = new CLabel(165, 290, FONT_MEDIUM, CENTER, zwykly,
  1579. boost::lexical_cast<std::string>(CGI->arth->artifacts[aid]->price));
  1580. std::string text = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % creature->nameSing);
  1581. buy = new AdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 42, 312,"IBUY30.DEF",SDLK_RETURN);
  1582. text = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % creature->nameSing);
  1583. cancel = new AdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 224, 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1584. if(possible)
  1585. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHero(hid),aid);
  1586. else
  1587. buy->block(true);
  1588. gold = new CPicture(graphics->resources32->ourImages[6].bitmap,148,244, false);
  1589. }
  1590. void CBlacksmithDialog::close()
  1591. {
  1592. GH.popIntTotally(this);
  1593. }