CCastleInterface.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. //if town tonwID have building toCheck
  455. //then set animation of building buildID to firstA..lastA
  456. //else set to firstB..lastB
  457. struct AnimRule
  458. {
  459. int townID, buildID;
  460. int toCheck;
  461. size_t firstA, lastA;
  462. size_t firstB, lastB;
  463. };
  464. static const AnimRule animRule[2] =
  465. {
  466. {5, 21, 4, 10, -1, 0, 10}, //Mana Vortex, Dungeon
  467. {0, 6, 8, 1, -1, 0, 1} //Shipyard, Castle
  468. };
  469. for (size_t i=0; i<2; i++)
  470. {
  471. if ( town->subID != animRule[i].townID ) //wrong town
  472. continue;
  473. int buildingID = animRule[i].buildID;
  474. //check if this building have been upgraded (Ship is upgrade of Shipyard)
  475. int groupID = CGI->townh->structures[town->subID][animRule[i].buildID]->group;
  476. if (groupID != -1)
  477. {
  478. std::map< int, std::vector<const Structure*> >::const_iterator git= groups.find(groupID);
  479. if ( git == groups.end() || git->second.empty() )
  480. continue;
  481. buildingID = git->second.back()->ID;
  482. }
  483. BOOST_FOREACH(CBuildingRect* rect, buildings)
  484. {
  485. if ( rect->str->ID == buildingID )
  486. {
  487. if (vstd::contains(town->builtBuildings, animRule[i].toCheck))
  488. rect->set(0,animRule[i].firstA, animRule[i].lastA);
  489. else
  490. rect->set(0,animRule[i].firstB, animRule[i].lastB);
  491. break;
  492. }
  493. }
  494. }
  495. }
  496. void CCastleBuildings::addBuilding(int building)
  497. {
  498. OBJ_CONSTRUCTION_CAPTURING_ALL;
  499. std::map<int, Structure*>::const_iterator structure;
  500. structure = CGI->townh->structures[town->subID].find(building);
  501. if(structure != CGI->townh->structures[town->subID].end()) //we have info about that structure
  502. {
  503. if(structure->second->group<0) //no group - just add it
  504. {
  505. buildings.push_back(new CBuildingRect(this, town, structure->second));
  506. buildings.back()->stateCounter = 0;
  507. }
  508. else
  509. {
  510. //find last building in this group and replace it with new building if needed
  511. groups[structure->second->group].push_back(structure->second);
  512. int newBuilding = groups[structure->second->group].back()->ID;
  513. if (newBuilding == building)
  514. {
  515. for (std::vector< CBuildingRect* >::iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  516. {
  517. if ((*it)->str->ID == newBuilding)
  518. {
  519. delChild(*it);
  520. buildings.erase(it);
  521. break;
  522. }
  523. }
  524. buildings.push_back(new CBuildingRect(this, town, structure->second));
  525. buildings.back()->stateCounter = 0;
  526. }
  527. }
  528. }
  529. std::sort(buildings.begin(),buildings.end(),buildSorter);
  530. checkRules();
  531. }
  532. void CCastleBuildings::removeBuilding(int building)
  533. {
  534. OBJ_CONSTRUCTION_CAPTURING_ALL;
  535. std::map<int, Structure*>::const_iterator structure;
  536. structure = CGI->townh->structures[town->subID].find(building);
  537. if(structure != CGI->townh->structures[town->subID].end()) //we have info about that structure
  538. {
  539. if(structure->second->group<0) //no group - just add it
  540. {
  541. for (std::vector< CBuildingRect* >::iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  542. {
  543. if ((*it)->str->ID == building)
  544. {
  545. delChild(*it);
  546. buildings.erase(it);
  547. break;
  548. }
  549. }
  550. }
  551. else
  552. {
  553. groups[structure->second->group].pop_back();
  554. delChild(buildings[building]);
  555. if (!groups[structure->second->group].empty())
  556. buildings.push_back(new CBuildingRect(this, town, structure->second));
  557. }
  558. }
  559. std::sort(buildings.begin(),buildings.end(),buildSorter);
  560. checkRules();
  561. }
  562. void CCastleBuildings::show(SDL_Surface *to)
  563. {
  564. CIntObject::show(to);
  565. for (std::vector< CBuildingRect* >::const_iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  566. (*it)->show(to);
  567. }
  568. void CCastleBuildings::showAll(SDL_Surface *to)
  569. {
  570. CIntObject::showAll(to);
  571. for (std::vector< CBuildingRect* >::const_iterator it=buildings.begin() ; it !=buildings.end(); it++ )
  572. (*it)->showAll(to);
  573. }
  574. const CGHeroInstance* CCastleBuildings::getHero()
  575. {
  576. if (town->visitingHero)
  577. return town->visitingHero;
  578. if (town->garrisonHero)
  579. return town->garrisonHero;
  580. return NULL;
  581. }
  582. void CCastleBuildings::buildingClicked(int building)
  583. {
  584. tlog5<<"You've clicked on "<<building<<std::endl;
  585. building = hordeToDwellingID(building);
  586. const CBuilding *b = CGI->buildh->buildings[town->subID].find(building)->second;
  587. if(building >= Buildings::DWELL_FIRST)
  588. {
  589. enterDwelling((building-Buildings::DWELL_FIRST)%CREATURES_PER_TOWN);
  590. }
  591. else
  592. {
  593. switch(building)
  594. {
  595. case Buildings::MAGES_GUILD_1:
  596. case Buildings::MAGES_GUILD_2:
  597. case Buildings::MAGES_GUILD_3:
  598. case Buildings::MAGES_GUILD_4:
  599. case Buildings::MAGES_GUILD_5:
  600. enterMagesGuild();
  601. break;
  602. case Buildings::TAVERN:
  603. LOCPLINT->showTavernWindow(town);
  604. break;
  605. case Buildings::SHIPYARD:
  606. LOCPLINT->showShipyardDialog(town);
  607. break;
  608. case Buildings::FORT:
  609. case Buildings::CITADEL:
  610. case Buildings::CASTLE:
  611. GH.pushInt(new CFortScreen(town));
  612. break;
  613. case Buildings::VILLAGE_HALL:
  614. case Buildings::CITY_HALL:
  615. case Buildings::TOWN_HALL:
  616. case Buildings::CAPITOL:
  617. enterTownHall();
  618. break;
  619. case Buildings::MARKETPLACE:
  620. GH.pushInt(new CMarketplaceWindow(town, town->visitingHero));
  621. break;
  622. case Buildings::BLACKSMITH:
  623. enterBlacksmith(town->town->warMachine);
  624. break;
  625. case Buildings::SPECIAL_1:
  626. switch(town->subID)
  627. {
  628. case 1://Mystic Pond
  629. enterFountain(building);
  630. break;
  631. case 2: case 5: case 8://Artifact Merchant
  632. if(town->visitingHero)
  633. GH.pushInt(new CMarketplaceWindow(town, town->visitingHero, RESOURCE_ARTIFACT));
  634. else
  635. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->Name())); //Only visiting heroes may use the %s.
  636. break;
  637. default:
  638. enterBuilding(building);
  639. break;
  640. }
  641. break;
  642. case Buildings::SHIP:
  643. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[51]); //Cannot build another boat
  644. break;
  645. case Buildings::SPECIAL_2:
  646. switch(town->subID)
  647. {
  648. case 1: //Fountain of Fortune
  649. enterFountain(building);
  650. break;
  651. case 6: //Freelancer's Guild
  652. if(getHero())
  653. GH.pushInt(new CMarketplaceWindow(town, getHero(), CREATURE_RESOURCE));
  654. else
  655. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->Name())); //Only visiting heroes may use the %s.
  656. break;
  657. case 8: //Magic University
  658. if (getHero())
  659. GH.pushInt(new CUniversityWindow(getHero(), town));
  660. else
  661. enterBuilding(building);
  662. break;
  663. default:
  664. enterBuilding(building);
  665. break;
  666. }
  667. break;
  668. case Buildings::SPECIAL_3:
  669. switch(town->subID)
  670. {
  671. case 0: //Brotherhood of sword
  672. LOCPLINT->showTavernWindow(town);
  673. break;
  674. case 3: //Castle Gate
  675. enterCastleGate();
  676. break;
  677. case 4: //Skeleton Transformer
  678. GH.pushInt( new CTransformerWindow(getHero(), town) );
  679. break;
  680. case 5: //Portal of Summoning
  681. if (town->creatures[CREATURES_PER_TOWN].second.empty())//No creatures
  682. LOCPLINT->showInfoDialog(CGI->generaltexth->tcommands[30]);
  683. else
  684. enterDwelling(CREATURES_PER_TOWN);
  685. break;
  686. case 6: //Ballista Yard
  687. enterBlacksmith(4);
  688. break;
  689. default:
  690. enterBuilding(building);
  691. break;
  692. }
  693. break;
  694. default:
  695. enterBuilding(building);
  696. break;
  697. }
  698. }
  699. }
  700. void CCastleBuildings::enterBlacksmith(int ArtifactID)
  701. {
  702. const CGHeroInstance *hero = town->visitingHero;
  703. if(!hero)
  704. {
  705. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % CGI->buildh->buildings[town->subID].find(16)->second->Name()));
  706. return;
  707. }
  708. int price = CGI->arth->artifacts[ArtifactID]->price;
  709. bool possible = LOCPLINT->cb->getResourceAmount(Res::GOLD) >= price && !hero->hasArt(ArtifactID+9);
  710. GH.pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(ArtifactID,false),ArtifactID,hero->id));
  711. }
  712. void CCastleBuildings::enterBuilding(int building)
  713. {
  714. std::vector<SComponent*> comps(1,
  715. new SComponent(SComponent::building, town->subID,building,
  716. LOCPLINT->castleInt->bicons->ourImages[building].bitmap, false));
  717. LOCPLINT->showInfoDialog(
  718. CGI->buildh->buildings[town->subID].find(building)->second->Description(),comps);
  719. }
  720. void CCastleBuildings::enterCastleGate()
  721. {
  722. if (!town->visitingHero)
  723. {
  724. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[126]);
  725. return;//only visiting hero can use castle gates
  726. }
  727. std::vector <int> availableTowns;
  728. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(false);
  729. for(size_t i=0;i<Towns.size();i++)
  730. {
  731. const CGTownInstance *t = Towns[i];
  732. if (t->id != this->town->id && t->visitingHero == NULL && //another town, empty and this is
  733. t->subID == 3 && vstd::contains(t->builtBuildings, 22))//inferno with castle gate
  734. {
  735. availableTowns.push_back(t->id);//add to the list
  736. }
  737. }
  738. CPicture *titlePic = new CPicture (LOCPLINT->castleInt->bicons->ourImages[22].bitmap, 0,0, false);//will be deleted by selection window
  739. GH.pushInt (new CObjectListWindow(availableTowns, titlePic, CGI->generaltexth->jktexts[40],
  740. CGI->generaltexth->jktexts[41], boost::bind (&CCastleInterface::castleTeleport, LOCPLINT->castleInt, _1)));
  741. }
  742. void CCastleBuildings::enterDwelling(int level)
  743. {
  744. assert(level >= 0 && level < town->creatures.size());
  745. GH.pushInt(new CRecruitmentWindow(town, level, town, boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2,level), -87));
  746. }
  747. void CCastleBuildings::enterFountain(int building)
  748. {
  749. std::vector<SComponent*> comps(1,
  750. new SComponent(SComponent::building,town->subID,building,
  751. LOCPLINT->castleInt->bicons->ourImages[building].bitmap,false));
  752. std::string descr = CGI->buildh->buildings[town->subID].find(building)->second->Description();
  753. if ( building == 21)//we need description for mystic pond as well
  754. descr += "\n\n"+CGI->buildh->buildings[town->subID].find(17)->second->Description();
  755. if (town->bonusValue.first == 0)//fountain was builded this week
  756. descr += "\n\n"+ CGI->generaltexth->allTexts[677];
  757. else//fountain produced something;
  758. {
  759. descr+= "\n\n"+ CGI->generaltexth->allTexts[678];
  760. boost::algorithm::replace_first(descr,"%s",CGI->generaltexth->restypes[town->bonusValue.first]);
  761. boost::algorithm::replace_first(descr,"%d",boost::lexical_cast<std::string>(town->bonusValue.second));
  762. }
  763. LOCPLINT->showInfoDialog(descr, comps);
  764. }
  765. void CCastleBuildings::enterMagesGuild()
  766. {
  767. const CGHeroInstance *hero = getHero();
  768. if(hero && !hero->hasSpellbook()) //hero doesn't have spellbok
  769. {
  770. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < 500) //not enough gold to buy spellbook
  771. {
  772. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213]);
  773. }
  774. else
  775. {
  776. CFunctionList<void()> functionList = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb, hero,0);
  777. functionList += boost::bind(&CCastleBuildings::openMagesGuild,this);
  778. std::vector<SComponent*> components(1, new SComponent(SComponent::artifact,0,0));
  779. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214], components, functionList, 0, true);
  780. }
  781. }
  782. else
  783. {
  784. openMagesGuild();
  785. }
  786. }
  787. void CCastleBuildings::enterTownHall()
  788. {
  789. if(town->visitingHero && town->visitingHero->hasArt(2) &&
  790. !vstd::contains(town->builtBuildings, Buildings::GRAIL)) //hero has grail, but town does not have it
  791. {
  792. if(!vstd::contains(town->forbiddenBuildings, Buildings::GRAIL))
  793. {
  794. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[597], //Do you wish this to be the permanent home of the Grail?
  795. std::vector<SComponent*>(),
  796. boost::bind(&CCallback::buildBuilding, LOCPLINT->cb, town, Buildings::GRAIL),
  797. boost::bind(&CCastleBuildings::openTownHall, this), true);
  798. }
  799. else
  800. {
  801. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[673]);
  802. (dynamic_cast<CInfoWindow*>(GH.topInt()))->buttons[0]->callback += boost::bind(&CCastleBuildings::openTownHall, this);
  803. }
  804. }
  805. else
  806. {
  807. openTownHall();
  808. }
  809. }
  810. void CCastleBuildings::openMagesGuild()
  811. {
  812. GH.pushInt(new CMageGuildScreen(LOCPLINT->castleInt));
  813. }
  814. void CCastleBuildings::openTownHall()
  815. {
  816. GH.pushInt(new CHallInterface(town));
  817. }
  818. CCastleInterface::CCastleInterface(const CGTownInstance * Town, int listPos):
  819. hall(NULL),
  820. fort(NULL),
  821. town(Town)
  822. {
  823. OBJ_CONSTRUCTION_CAPTURING_ALL;
  824. used |= KEYBOARD;
  825. builds = new CCastleBuildings(town);
  826. panel = new CPicture("TOWNSCRN", 0, builds->pos.h);
  827. panel->colorize(LOCPLINT->playerID);
  828. pos.w = panel->pos.w;
  829. pos.h = builds->pos.h + panel->pos.h;
  830. center();
  831. garr = new CGarrisonInt(305, 387, 4, Point(0,96), panel->bg, Point(62,374), town->getUpperArmy(), town->visitingHero);
  832. heroes = new HeroSlots(town, Point(241, 387), Point(241, 483), garr, true);
  833. title = new CLabel(85, 387, FONT_MEDIUM, TOPLEFT, zwykly, town->name);
  834. income = new CLabel(195, 443, FONT_SMALL, CENTER);
  835. icon = new CAnimImage("ITPT", 0, 0, 15, 387);
  836. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CCastleInterface::close,this), 744, 544, "TSBTNS", SDLK_RETURN);
  837. exit->assignedKeys.insert(SDLK_ESCAPE);
  838. exit->setOffset(4);
  839. split = new AdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick,garr), 744, 382, "TSBTNS.DEF");
  840. split->callback += boost::bind(&HeroSlots::splitClicked, heroes);
  841. removeChild(split);
  842. garr->addSplitBtn(split);
  843. Rect barRect(9, 182, 732, 18);
  844. statusbar = new CGStatusBar(new CPicture(*panel, barRect, 9, 555, false));
  845. resdatabar = new CResDataBar("ZRESBAR", 3, 575, 32, 2, 85, 85);
  846. townlist = new CTownList(3, 744, 414, "IAM014", "IAM015");
  847. townlist->fun = boost::bind(&CCastleInterface::townChange, this);
  848. townlist->selected = vstd::findPos(LOCPLINT->towns, Town);
  849. townlist->from = townlist->selected - listPos;
  850. amax(townlist->from, 0);
  851. amin(townlist->from, LOCPLINT->towns.size() - townlist->SIZE);
  852. LOCPLINT->castleInt = this;
  853. recreateIcons();
  854. CCS->musich->playMusic(CCS->musich->townMusics[town->subID], -1);
  855. bicons = CDefHandler::giveDefEss(graphics->buildingPics[town->subID]);
  856. }
  857. CCastleInterface::~CCastleInterface()
  858. {
  859. LOCPLINT->castleInt = NULL;
  860. delete bicons;
  861. }
  862. void CCastleInterface::close()
  863. {
  864. if(town->tempOwner == LOCPLINT->playerID) //we may have opened window for an allied town
  865. {
  866. if(town->visitingHero)
  867. adventureInt->select(town->visitingHero);
  868. else
  869. adventureInt->select(town);
  870. }
  871. GH.popIntTotally(this);
  872. }
  873. void CCastleInterface::showAll(SDL_Surface *to)
  874. {
  875. CIntObject::showAll(to);
  876. if(screen->w != 800 || screen->h !=600)
  877. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  878. }
  879. void CCastleInterface::castleTeleport(int where)
  880. {
  881. const CGTownInstance * dest = LOCPLINT->cb->getTown(where);
  882. LOCPLINT->cb->teleportHero(town->visitingHero, dest);
  883. }
  884. void CCastleInterface::townChange()
  885. {
  886. const CGTownInstance * nt = LOCPLINT->towns[townlist->selected];
  887. int tpos = townlist->selected - townlist->from;
  888. if ( nt == town )
  889. return;
  890. GH.popIntTotally(this);
  891. GH.pushInt(new CCastleInterface(nt, tpos));
  892. }
  893. void CCastleInterface::addBuilding(int bid)
  894. {
  895. deactivate();
  896. builds->addBuilding(bid);
  897. recreateIcons();
  898. activate();
  899. }
  900. void CCastleInterface::removeBuilding(int bid)
  901. {
  902. deactivate();
  903. builds->removeBuilding(bid);
  904. recreateIcons();
  905. activate();
  906. }
  907. void CCastleInterface::recreateIcons()
  908. {
  909. OBJ_CONSTRUCTION_CAPTURING_ALL;
  910. if (fort)
  911. delChild(fort);
  912. if (hall)
  913. delChild(hall);
  914. size_t iconIndex = town->subID*2;
  915. if (!town->hasFort())
  916. iconIndex += F_NUMBER*2;
  917. if(town->builded >= MAX_BUILDING_PER_TURN)
  918. iconIndex++;
  919. icon->setFrame(iconIndex);
  920. income->setTxt(boost::lexical_cast<std::string>(town->dailyIncome()));
  921. hall = new CTownInfo( 80, 413, town, true);
  922. fort = new CTownInfo(122, 413, town, false);
  923. for (size_t i=0; i<creainfo.size(); i++)
  924. delChild(creainfo[i]);
  925. creainfo.clear();
  926. for (size_t i=0; i<4; i++)
  927. creainfo.push_back(new CCreaInfo(Point(14+55*i, 459), town, i));
  928. for (size_t i=0; i<4; i++)
  929. creainfo.push_back(new CCreaInfo(Point(14+55*i, 507), town, i+4));
  930. }
  931. CCreaInfo::CCreaInfo(Point position, const CGTownInstance *Town, int Level, bool compact, bool ShowAvailable):
  932. town(Town),
  933. level(Level),
  934. showAvailable(ShowAvailable)
  935. {
  936. OBJ_CONSTRUCTION_CAPTURING_ALL;
  937. pos += position;
  938. if ( town->creatures.size() <= level || town->creatures[level].second.empty())
  939. {
  940. level = -1;
  941. label = NULL;
  942. picture = NULL;
  943. return;//No creature
  944. }
  945. used = LCLICK | RCLICK | HOVER;
  946. unsigned int creatureID = town->creatures[level].second.back();
  947. creature = CGI->creh->creatures[creatureID];
  948. picture = new CAnimImage("CPRSMALL", creatureID+2, 0, 8, 0);
  949. std::string value;
  950. if (showAvailable)
  951. value = boost::lexical_cast<std::string>(town->creatures[level].first);
  952. else
  953. value = boost::lexical_cast<std::string>(town->creatureGrowth(level));
  954. if (compact)
  955. {
  956. label = new CLabel(40, 32, FONT_TINY, BOTTOMRIGHT, zwykly, value);
  957. pos.x += 8;
  958. pos.w = 32;
  959. pos.h = 32;
  960. }
  961. else
  962. {
  963. label = new CLabel(24, 40, FONT_SMALL, CENTER, zwykly, value);
  964. pos.w = 48;
  965. pos.h = 48;
  966. }
  967. }
  968. void CCreaInfo::update()
  969. {
  970. if (label)
  971. {
  972. std::string value;
  973. if (showAvailable)
  974. value = boost::lexical_cast<std::string>(town->creatures[level].first);
  975. else
  976. value = boost::lexical_cast<std::string>(town->creatureGrowth(level));
  977. if (value != label->text)
  978. label->setTxt(value);
  979. }
  980. }
  981. void CCreaInfo::hover(bool on)
  982. {
  983. std::string message = CGI->generaltexth->allTexts[588];
  984. boost::algorithm::replace_first(message,"%s",creature->namePl);
  985. if(on)
  986. {
  987. GH.statusbar->print(message);
  988. }
  989. else if (message == GH.statusbar->getCurrent())
  990. GH.statusbar->clear();
  991. }
  992. void CCreaInfo::clickLeft(tribool down, bool previousState)
  993. {
  994. if(previousState && (!down))
  995. {
  996. int offset = LOCPLINT->castleInt? (-87) : 0;
  997. GH.pushInt(new CRecruitmentWindow(town, level, town,
  998. boost::bind(&CCallback::recruitCreatures, LOCPLINT->cb, town, _1, _2, level), offset));
  999. }
  1000. }
  1001. int CCreaInfo::AddToString(std::string from, std::string & to, int numb)
  1002. {
  1003. if (numb == 0)
  1004. return 0;
  1005. boost::algorithm::replace_first(from,"%+d", (numb > 0 ? "+" : "")+boost::lexical_cast<std::string>(numb)); //negative values don't need "+"
  1006. to+="\n"+from;
  1007. return numb;
  1008. }
  1009. std::string CCreaInfo::genGrowthText()
  1010. {
  1011. GrowthInfo gi = town->getGrowthInfo(level);
  1012. std::string descr = boost::str(boost::format(CGI->generaltexth->allTexts[589]) % creature->nameSing % gi.totalGrowth());
  1013. BOOST_FOREACH(const GrowthInfo::Entry &entry, gi.entries)
  1014. {
  1015. descr +="\n" + entry.description;
  1016. }
  1017. return descr;
  1018. }
  1019. void CCreaInfo::clickRight(tribool down, bool previousState)
  1020. {
  1021. if(down)
  1022. {
  1023. if (showAvailable)
  1024. GH.pushInt(new CDwellingInfoBox(screen->w/2, screen->h/2, town, level));
  1025. else
  1026. {
  1027. std::string descr = genGrowthText();
  1028. CInfoPopup *mess = new CInfoPopup();//creating popup
  1029. mess->free = true;
  1030. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1031. (LOCPLINT->playerID, descr,graphics->bigImgs[creature->idNumber],"");
  1032. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1033. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1034. GH.pushInt(mess);
  1035. }
  1036. }
  1037. }
  1038. CTownInfo::CTownInfo(int posX, int posY, const CGTownInstance* Town, bool townHall):
  1039. town(Town),
  1040. building(NULL)
  1041. {
  1042. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1043. used = RCLICK | HOVER;
  1044. pos.x += posX;
  1045. pos.y += posY;
  1046. int buildID;
  1047. if (townHall)
  1048. {
  1049. buildID = 10 + town->hallLevel();
  1050. picture = new CAnimImage("ITMTL.DEF", town->hallLevel());
  1051. }
  1052. else
  1053. {
  1054. buildID = 6 + town->fortLevel();
  1055. if (buildID == 6)
  1056. return;
  1057. picture = new CAnimImage("ITMCL.DEF", town->fortLevel()-1);
  1058. }
  1059. building = CGI->buildh->buildings[town->subID][buildID];
  1060. pos = picture->pos;
  1061. }
  1062. void CTownInfo::hover(bool on)
  1063. {
  1064. if(on)
  1065. {
  1066. if ( building )
  1067. GH.statusbar->print(building->Name());
  1068. }
  1069. else
  1070. GH.statusbar->clear();
  1071. }
  1072. void CTownInfo::clickRight(tribool down, bool previousState)
  1073. {//FIXME: castleInt may be NULL
  1074. if(down && building && LOCPLINT->castleInt)
  1075. {
  1076. CInfoPopup *mess = new CInfoPopup();
  1077. mess->free = true;
  1078. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1079. (LOCPLINT->playerID,
  1080. building->Description(),
  1081. LOCPLINT->castleInt->bicons->ourImages[building->bid].bitmap,
  1082. building->Name());
  1083. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1084. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1085. GH.pushInt(mess);
  1086. }
  1087. }
  1088. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  1089. {
  1090. if(key.state != SDL_PRESSED) return;
  1091. switch(key.keysym.sym)
  1092. {
  1093. case SDLK_UP:
  1094. if(townlist->selected)
  1095. {
  1096. townlist->selected--;
  1097. townlist->from--;
  1098. townChange();
  1099. }
  1100. break;
  1101. case SDLK_DOWN:
  1102. if(townlist->selected < LOCPLINT->towns.size() - 1)
  1103. {
  1104. townlist->selected++;
  1105. townlist->from++;
  1106. townChange();
  1107. }
  1108. break;
  1109. case SDLK_SPACE:
  1110. if(!!town->visitingHero && town->garrisonHero)
  1111. {
  1112. LOCPLINT->cb->swapGarrisonHero(town);
  1113. }
  1114. break;
  1115. default:
  1116. break;
  1117. }
  1118. }
  1119. HeroSlots::HeroSlots(const CGTownInstance * Town, Point garrPos, Point visitPos, CGarrisonInt *Garrison, bool ShowEmpty):
  1120. showEmpty(ShowEmpty),
  1121. town(Town),
  1122. garr(Garrison)
  1123. {
  1124. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1125. garrisonedHero = new CHeroGSlot(garrPos.x, garrPos.y, 0, town->garrisonHero, this);
  1126. visitingHero = new CHeroGSlot(visitPos.x, visitPos.y, 1, town->visitingHero, this);
  1127. }
  1128. void HeroSlots::update()
  1129. {
  1130. garrisonedHero->hero = town->garrisonHero;
  1131. visitingHero->hero = town->visitingHero;
  1132. }
  1133. void HeroSlots::splitClicked()
  1134. {
  1135. if(!!town->visitingHero && town->garrisonHero && (visitingHero->highlight || garrisonedHero->highlight))
  1136. {
  1137. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  1138. }
  1139. }
  1140. void CHallInterface::CBuildingBox::hover(bool on)
  1141. {
  1142. if(on)
  1143. {
  1144. std::string toPrint;
  1145. if(state==Buildings::PREREQUIRES)
  1146. toPrint = CGI->generaltexth->hcommands[5];
  1147. else if(state==Buildings::CANT_BUILD_TODAY)
  1148. toPrint = CGI->generaltexth->allTexts[223];
  1149. else
  1150. toPrint = CGI->generaltexth->hcommands[state];
  1151. boost::algorithm::replace_first(toPrint,"%s",building->Name());
  1152. GH.statusbar->print(toPrint);
  1153. }
  1154. else
  1155. GH.statusbar->clear();
  1156. }
  1157. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1158. {
  1159. if(previousState && (!down))
  1160. GH.pushInt(new CBuildWindow(town,building,state,0));
  1161. }
  1162. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1163. {
  1164. if(down)
  1165. GH.pushInt(new CBuildWindow(town,building,state,1));
  1166. }
  1167. CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance * Town, const CBuilding * Building):
  1168. town(Town),
  1169. building(Building)
  1170. {
  1171. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1172. used = LCLICK | RCLICK | HOVER;
  1173. pos.x += x;
  1174. pos.y += y;
  1175. pos.w = 154;
  1176. pos.h = 92;
  1177. state = LOCPLINT->cb->canBuildStructure(town,building->bid);
  1178. assert(state < Buildings::ERROR);
  1179. static int panelIndex[9] = { 3, 3, 3, 0, 0, 2, 2, 1, 2};
  1180. static int iconIndex[9] = {-1, -1, -1, 0, 0, 1, 2, -1, 1};
  1181. picture = new CAnimImage(graphics->buildingPics[town->subID], building->bid, 0, 2, 2);
  1182. panel = new CAnimImage("TPTHBAR", panelIndex[state], 0, 1, 73);
  1183. if ( iconIndex[state] >=0 )
  1184. icon = new CAnimImage("TPTHCHK", iconIndex[state], 0, 136, 56);
  1185. label = new CLabel(75, 81, FONT_SMALL, CENTER, zwykly, building->Name());
  1186. }
  1187. CHallInterface::CHallInterface(const CGTownInstance *Town):
  1188. town(Town)
  1189. {
  1190. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1191. background = new CPicture(CGI->buildh->hall[town->subID].first);
  1192. background->colorize(LOCPLINT->playerID);
  1193. pos = background->center();
  1194. resdatabar = new CMinorResDataBar;
  1195. resdatabar->pos.x += pos.x;
  1196. resdatabar->pos.y += pos.y;
  1197. Rect barRect(5, 556, 740, 18);
  1198. statusBar = new CGStatusBar(new CPicture(*background, barRect, 5, 556, false));
  1199. title = new CLabel(399, 12, FONT_MEDIUM, CENTER, zwykly, CGI->buildh->buildings[town->subID][town->hallLevel()+Buildings::VILLAGE_HALL]->Name());
  1200. exit = new AdventureMapButton(CGI->generaltexth->hcommands[8], "",
  1201. boost::bind(&CHallInterface::close,this), 748, 556, "TPMAGE1.DEF", SDLK_RETURN);
  1202. exit->assignedKeys.insert(SDLK_ESCAPE);
  1203. const std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[town->subID].second;
  1204. boxes.resize(boxList.size());
  1205. for(size_t row=0; row<boxList.size(); row++) //for each row
  1206. {
  1207. for(size_t col=0; col<boxList[row].size(); col++) //for each box
  1208. {
  1209. const CBuilding *building = NULL;
  1210. for(size_t item=0; item<boxList[row][col].size(); item++)//we are looking for the first not build structure
  1211. {
  1212. int buildingID = boxList[row][col][item];
  1213. building = CGI->buildh->buildings[town->subID][buildingID];
  1214. if (buildingID == 18 || buildingID == 24)
  1215. {
  1216. if ( (buildingID == 18 && !vstd::contains(town->builtBuildings, town->town->hordeLvl[0]+37))
  1217. || (buildingID == 24 && !vstd::contains(town->builtBuildings, town->town->hordeLvl[1]+37)) )
  1218. break; // horde present, no upgraded dwelling -> select 18 or 24
  1219. else
  1220. continue; //upgraded dwelling, no horde -> select 19 or 25
  1221. }
  1222. if(vstd::contains(town->builtBuildings,buildingID))
  1223. continue;
  1224. break;
  1225. }
  1226. int posX = pos.w/2 - boxList[row].size()*154/2 - (boxList[row].size()-1)*20 + 194*col,
  1227. posY = 35 + 104*row;
  1228. if (building)
  1229. boxes[row].push_back(new CBuildingBox(posX, posY, town, building));
  1230. }
  1231. }
  1232. }
  1233. void CHallInterface::close()
  1234. {
  1235. GH.popIntTotally(this);
  1236. }
  1237. void CBuildWindow::buyFunc()
  1238. {
  1239. LOCPLINT->cb->buildBuilding(town,building->bid);
  1240. GH.popInts(2); //we - build window and hall screen
  1241. }
  1242. void CBuildWindow::close()
  1243. {
  1244. GH.popIntTotally(this);
  1245. }
  1246. void CBuildWindow::clickRight(tribool down, bool previousState)
  1247. {
  1248. if((!down || indeterminate(down)) && mode)
  1249. close();
  1250. }
  1251. std::string CBuildWindow::getTextForState(int state)
  1252. {
  1253. std::string ret;
  1254. if(state<7)
  1255. ret = CGI->generaltexth->hcommands[state];
  1256. switch (state)
  1257. {
  1258. case 4: case 5: case 6:
  1259. ret.replace(ret.find_first_of("%s"),2,building->Name());
  1260. break;
  1261. case 7:
  1262. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1263. case 8:
  1264. {
  1265. ret = CGI->generaltexth->allTexts[52];
  1266. std::set<int> reqs= LOCPLINT->cb->getBuildingRequiments(town, building->bid);
  1267. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1268. {
  1269. if (vstd::contains(town->builtBuildings, *i))
  1270. continue;//skipping constructed buildings
  1271. ret+= CGI->buildh->buildings[town->subID][*i]->Name() + ", ";
  1272. }
  1273. ret.erase(ret.size()-2);
  1274. }
  1275. }
  1276. return ret;
  1277. }
  1278. CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, int State, bool Mode)
  1279. :town(Town), building(Building), state(State), mode(Mode)
  1280. {
  1281. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1282. used |= RCLICK;
  1283. background = new CPicture("TPUBUILD");
  1284. pos = background->center();
  1285. background->colorize(LOCPLINT->playerID);
  1286. buildingPic = new CAnimImage(graphics->buildingPics[town->subID], building->bid, 0, 125, 50);
  1287. Rect barRect(9, 494, 380, 18);
  1288. statusBar = new CGStatusBar(new CPicture(*background, barRect, 9, 494, false));
  1289. title = new CLabel(197, 30, FONT_MEDIUM, CENTER, zwykly,
  1290. boost::str(boost::format(CGI->generaltexth->hcommands[7]) % building->Name()));
  1291. buildingDescr = new CTextBox(building->Description(), Rect(33, 135, 329, 67), 0, FONT_MEDIUM, CENTER);
  1292. buildingState = new CTextBox(getTextForState(state), Rect(33, 216, 329, 67), 0, FONT_SMALL, CENTER);
  1293. //Create objects for all required resources
  1294. for(int i = 0; i<RESOURCE_QUANTITY; i++)
  1295. {
  1296. if(building->resources[i])
  1297. {
  1298. resPicture.push_back(new CPicture(graphics->resources32->ourImages[i].bitmap, 0, 0, false));
  1299. resAmount.push_back(new CLabel(0,0, FONT_SMALL, CENTER, zwykly,
  1300. boost::lexical_cast<std::string>(building->resources[i])));
  1301. }
  1302. }
  1303. unsigned int rowSize[2];
  1304. int posY;
  1305. if (resAmount.size() > 4)
  1306. {//Resources will be placed in multiple rows
  1307. rowSize[0] = (resAmount.size()+1)/2;
  1308. posY = 303;
  1309. }
  1310. else
  1311. {//one row
  1312. rowSize[0] = resAmount.size();
  1313. posY = 340;
  1314. }
  1315. rowSize[1] = resAmount.size() - rowSize[0];
  1316. unsigned int index=0;
  1317. for (size_t row=0; row<2; row++)
  1318. {
  1319. int posX = pos.w/2 - rowSize[row] * 40 + 24;
  1320. for (size_t i=0; i<rowSize[row]; i++)
  1321. {//Move current resource to correct position
  1322. resPicture[index]->moveBy(Point(posX, posY));
  1323. resAmount[index]->moveBy(Point(posX+16, posY+48));
  1324. posX += 80;
  1325. index++;
  1326. }
  1327. posY +=75;
  1328. }
  1329. if(!mode)
  1330. {
  1331. buy = new AdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[595]) % building->Name()),
  1332. "", boost::bind(&CBuildWindow::buyFunc,this), 45, 446,"IBUY30", SDLK_RETURN);
  1333. buy->borderColor = Colors::MetallicGold;
  1334. buy->borderEnabled = true;
  1335. cancel = new AdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[596]) % building->Name()),
  1336. "", boost::bind(&CBuildWindow::close,this), 290, 445, "ICANCEL", SDLK_ESCAPE);
  1337. cancel->borderColor = Colors::MetallicGold;
  1338. cancel->borderEnabled = true;
  1339. buy->block(state!=7);
  1340. }
  1341. }
  1342. CBuildWindow::~CBuildWindow()
  1343. {}
  1344. void CFortScreen::close()
  1345. {
  1346. GH.popIntTotally(this);
  1347. }
  1348. CFortScreen::CFortScreen(const CGTownInstance * town)
  1349. {
  1350. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1351. unsigned int fortSize = town->creatures.size();
  1352. if (fortSize > CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1353. fortSize--;
  1354. if (fortSize == CREATURES_PER_TOWN)
  1355. background = new CPicture("TPCASTL7");
  1356. else
  1357. background = new CPicture("TPCASTL8");
  1358. background->colorize(LOCPLINT->playerID);
  1359. pos = background->center();
  1360. const CBuilding *fortBuilding = CGI->buildh->buildings[town->subID][town->fortLevel()+6];
  1361. title = new CLabel(400, 12, FONT_BIG, CENTER, zwykly, fortBuilding->Name());
  1362. std::string text = boost::str(boost::format(CGI->generaltexth->fcommands[6]) % fortBuilding->Name());
  1363. exit = new AdventureMapButton(text, "", boost::bind(&CFortScreen::close,this) ,748, 556, "TPMAGE1", SDLK_RETURN);
  1364. std::vector<Point> positions;
  1365. positions += Point(10, 22), Point(404, 22),
  1366. Point(10, 155), Point(404,155),
  1367. Point(10, 288), Point(404,288);
  1368. if (fortSize == CREATURES_PER_TOWN)
  1369. positions += Point(206,421);
  1370. else
  1371. positions += Point(10, 421), Point(404,421);
  1372. for (unsigned int i=0; i<fortSize; i++)
  1373. {
  1374. int buildingID;
  1375. if (fortSize == CREATURES_PER_TOWN)
  1376. {
  1377. if (vstd::contains(town->builtBuildings, Buildings::DWELL_UP_FIRST+i))
  1378. buildingID = Buildings::DWELL_UP_FIRST+i;
  1379. else
  1380. buildingID = Buildings::DWELL_FIRST+i;
  1381. }
  1382. else
  1383. buildingID = 22;
  1384. recAreas.push_back(new RecruitArea(positions[i].x, positions[i].y, town, buildingID, i));
  1385. }
  1386. resdatabar = new CMinorResDataBar;
  1387. resdatabar->pos.x += pos.x;
  1388. resdatabar->pos.y += pos.y;
  1389. Rect barRect(4, 554, 740, 18);
  1390. statusBar = new CGStatusBar(new CPicture(*background, barRect, 4, 554, false));
  1391. }
  1392. void CFortScreen::creaturesChanged()
  1393. {
  1394. for (size_t i=0; i<recAreas.size(); i++)
  1395. recAreas[i]->creaturesChanged();
  1396. }
  1397. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int min, int max)
  1398. {
  1399. pos.x+=size.x;
  1400. pos.y+=size.y;
  1401. pos.w = size.w;
  1402. pos.h = size.h;
  1403. init(name, descr, min, max);
  1404. }
  1405. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int val)
  1406. {
  1407. pos.x+=size.x;
  1408. pos.y+=size.y;
  1409. pos.w = size.w;
  1410. pos.h = size.h;
  1411. init(name, descr, val, val);
  1412. }
  1413. void LabeledValue::init(std::string nameText, std::string descr, int min, int max)
  1414. {
  1415. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1416. used |= HOVER;
  1417. hoverText = descr;
  1418. std::string valueText;
  1419. if (min && max)
  1420. {
  1421. valueText = boost::lexical_cast<std::string>(min);
  1422. if (min != max)
  1423. valueText += '-' + boost::lexical_cast<std::string>(max);
  1424. }
  1425. name = new CLabel(3, 0, FONT_SMALL, TOPLEFT, zwykly, nameText);
  1426. value = new CLabel(pos.w-3, pos.h-2, FONT_SMALL, BOTTOMRIGHT, zwykly, valueText);
  1427. }
  1428. void LabeledValue::hover(bool on)
  1429. {
  1430. if(on)
  1431. GH.statusbar->print(hoverText);
  1432. else
  1433. {
  1434. GH.statusbar->clear();
  1435. parent->hovered = false;
  1436. }
  1437. }
  1438. CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance *Town, int buildingID, int Level):
  1439. town(Town),
  1440. level(Level),
  1441. availableCount(NULL)
  1442. {
  1443. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1444. pos.x +=posX;
  1445. pos.y +=posY;
  1446. pos.w = 386;
  1447. pos.h = 126;
  1448. if (!town->creatures[level].second.empty())
  1449. used |= LCLICK | RCLICK | HOVER;//Activate only if dwelling is present
  1450. icons = new CPicture("ZPCAINFO", 261, 3);
  1451. buildingPic = new CAnimImage(graphics->buildingPics[town->subID], buildingID, 0, 4, 21);
  1452. const CCreature* creature = NULL;
  1453. if (!town->creatures[level].second.empty())
  1454. creature = CGI->creh->creatures[town->creatures[level].second.back()];
  1455. else
  1456. creature = CGI->creh->creatures[town->town->basicCreatures[level]];
  1457. hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % creature->namePl);
  1458. creatureAnim = new CCreaturePic(159, 4, creature, false);
  1459. Rect sizes(287, 4, 96, 18);
  1460. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], creature->attack));
  1461. sizes.y+=20;
  1462. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[191], CGI->generaltexth->fcommands[1], creature->defence));
  1463. sizes.y+=21;
  1464. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[199], CGI->generaltexth->fcommands[2], creature->damageMin, creature->damageMax));
  1465. sizes.y+=20;
  1466. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[388], CGI->generaltexth->fcommands[3], creature->hitPoints));
  1467. sizes.y+=21;
  1468. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[193], CGI->generaltexth->fcommands[4], creature->speed));
  1469. sizes.y+=20;
  1470. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[194], CGI->generaltexth->fcommands[5], town->creatureGrowth(level)));
  1471. creatureName = new CLabel(78, 11, FONT_SMALL, CENTER, zwykly, creature->namePl);
  1472. dwellingName = new CLabel(78, 101, FONT_SMALL, CENTER, zwykly, CGI->buildh->buildings[town->subID][buildingID]->Name());
  1473. if (vstd::contains(town->builtBuildings, buildingID))
  1474. {
  1475. unsigned int available = town->creatures[level].first;
  1476. std::string availableText = CGI->generaltexth->allTexts[217]+ boost::lexical_cast<std::string>(available);
  1477. availableCount = new CLabel(78, 119, FONT_SMALL, CENTER, zwykly, availableText);
  1478. }
  1479. }
  1480. void CFortScreen::RecruitArea::hover(bool on)
  1481. {
  1482. if(on)
  1483. GH.statusbar->print(hoverText);
  1484. else
  1485. GH.statusbar->clear();
  1486. }
  1487. void CFortScreen::RecruitArea::creaturesChanged()
  1488. {
  1489. if (availableCount)
  1490. {
  1491. std::string availableText = CGI->generaltexth->allTexts[217] +
  1492. boost::lexical_cast<std::string>(town->creatures[level].first);
  1493. availableCount->setTxt(availableText);
  1494. }
  1495. }
  1496. void CFortScreen::RecruitArea::clickLeft(tribool down, bool previousState)
  1497. {
  1498. if(!down && previousState)
  1499. LOCPLINT->castleInt->builds->enterDwelling(level);
  1500. }
  1501. void CFortScreen::RecruitArea::clickRight(tribool down, bool previousState)
  1502. {
  1503. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1504. }
  1505. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1506. {
  1507. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1508. background = new CPicture("TPMAGE.bmp");
  1509. pos = background->center();
  1510. window = new CPicture(graphics->guildBgs[owner->town->subID], 332, 76);
  1511. resdatabar = new CMinorResDataBar;
  1512. resdatabar->pos.x += pos.x;
  1513. resdatabar->pos.y += pos.y;
  1514. Rect barRect(7, 556, 737, 18);
  1515. statusBar = new CGStatusBar(new CPicture(*background, barRect, 7, 556, false));
  1516. exit = new AdventureMapButton(CGI->generaltexth->allTexts[593],"",boost::bind(&CMageGuildScreen::close,this), 748, 556,"TPMAGE1.DEF",SDLK_RETURN);
  1517. exit->assignedKeys.insert(SDLK_ESCAPE);
  1518. std::vector<std::vector<Point> > positions;
  1519. positions.resize(5);
  1520. positions[0] += Point(222,445), Point(312,445), Point(402,445), Point(520,445), Point(610,445), Point(700,445);
  1521. positions[1] += Point(48,53), Point(48,147), Point(48,241), Point(48,335), Point(48,429);
  1522. positions[2] += Point(570,82), Point(672,82), Point(570,157), Point(672,157);
  1523. positions[3] += Point(183,42), Point(183,148), Point(183,253);
  1524. positions[4] += Point(491,325), Point(591,325);
  1525. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1526. {
  1527. size_t spellCount = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1528. for(size_t j=0; j<spellCount; j++)
  1529. {
  1530. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1531. spells.push_back( new Scroll(positions[i][j], CGI->spellh->spells[owner->town->spells[i][j]]));
  1532. else
  1533. new CAnimImage("TPMAGES.DEF", 1, 0, positions[i][j].x, positions[i][j].y);//closed scroll
  1534. }
  1535. }
  1536. }
  1537. void CMageGuildScreen::close()
  1538. {
  1539. GH.popIntTotally(this);
  1540. }
  1541. CMageGuildScreen::Scroll::Scroll(Point position, const CSpell *Spell)
  1542. :spell(Spell)
  1543. {
  1544. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1545. used = LCLICK | RCLICK | HOVER;
  1546. pos += position;
  1547. image = new CAnimImage("SPELLSCR", spell->id);
  1548. pos = image->pos;
  1549. }
  1550. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1551. {
  1552. if(down)
  1553. {
  1554. std::vector<SComponent*> comps(1,
  1555. new SComponent(SComponent::spell,spell->id,0)
  1556. );
  1557. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1558. }
  1559. }
  1560. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1561. {
  1562. if(down)
  1563. {
  1564. CInfoPopup *vinya = new CInfoPopup();
  1565. vinya->free = true;
  1566. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1567. (LOCPLINT->playerID,
  1568. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1569. spell->name,30,30);
  1570. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1571. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1572. GH.pushInt(vinya);
  1573. }
  1574. }
  1575. void CMageGuildScreen::Scroll::hover(bool on)
  1576. {
  1577. if(on)
  1578. GH.statusbar->print(spell->name);
  1579. else
  1580. GH.statusbar->clear();
  1581. }
  1582. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1583. {
  1584. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1585. background = new CPicture("TPSMITH");
  1586. pos = background->center();
  1587. background->colorize(LOCPLINT->playerID);
  1588. statusBar = new CGStatusBar(164, 370);
  1589. animBG = new CPicture("TPSMITBK", 64, 50);
  1590. animBG->needRefresh = true;
  1591. const CCreature *creature = CGI->creh->creatures[creMachineID];
  1592. anim = new CCreatureAnim(64, 50, creature->animDefName, Rect());
  1593. anim->clipRect(113,125,200,150);
  1594. title = new CLabel(165, 28, FONT_BIG, CENTER, tytulowy,
  1595. boost::str(boost::format(CGI->generaltexth->allTexts[274]) % creature->nameSing));
  1596. costText = new CLabel(165, 218, FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->jktexts[43]);
  1597. costValue = new CLabel(165, 290, FONT_MEDIUM, CENTER, zwykly,
  1598. boost::lexical_cast<std::string>(CGI->arth->artifacts[aid]->price));
  1599. std::string text = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % creature->nameSing);
  1600. buy = new AdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 42, 312,"IBUY30.DEF",SDLK_RETURN);
  1601. text = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % creature->nameSing);
  1602. cancel = new AdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 224, 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1603. if(possible)
  1604. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHero(hid),aid);
  1605. else
  1606. buy->block(true);
  1607. gold = new CPicture(graphics->resources32->ourImages[6].bitmap,148,244, false);
  1608. }
  1609. void CBlacksmithDialog::close()
  1610. {
  1611. GH.popIntTotally(this);
  1612. }