CCastleInterface.cpp 56 KB

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