CCastleInterface.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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->getTownInfo(where, 1);
  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(14+55*i, 459, town, i));
  912. for (size_t i=0; i<4; i++)
  913. creainfo.push_back(new CCreaInfo(14+55*i, 507, town, i+4));
  914. }
  915. CCreaInfo::CCreaInfo(int posX, int posY, const CGTownInstance *Town, int Level):
  916. town(Town),
  917. level(Level)
  918. {
  919. OBJ_CONSTRUCTION_CAPTURING_ALL;
  920. pos.x += posX;
  921. pos.y += posY;
  922. pos.w = 48;
  923. pos.h = 48;
  924. if ( town->creatures.size() <= level || town->creatures[level].second.empty())
  925. {
  926. level = -1;
  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. label = new CLabel(24, 40, FONT_SMALL, CENTER, zwykly, boost::lexical_cast<std::string>(town->creatureGrowth(level)));
  933. picture = new CAnimImage("CPRSMALL", creatureID+2, 0, 8, 0);
  934. }
  935. void CCreaInfo::hover(bool on)
  936. {
  937. std::string message = CGI->generaltexth->allTexts[588];
  938. boost::algorithm::replace_first(message,"%s",creature->namePl);
  939. if(on)
  940. {
  941. GH.statusbar->print(message);
  942. }
  943. else if (message == GH.statusbar->getCurrent())
  944. GH.statusbar->clear();
  945. }
  946. void CCreaInfo::clickLeft(tribool down, bool previousState)
  947. {//FIXME: castleInt should be present - may be NULL if no castle window opened
  948. if(previousState && (!down) && LOCPLINT->castleInt)
  949. LOCPLINT->castleInt->builds->enterDwelling(level);
  950. }
  951. int CCreaInfo::AddToString(std::string from, std::string & to, int numb)
  952. {
  953. if (numb == 0)
  954. return 0;
  955. boost::algorithm::replace_first(from,"%+d", (numb > 0 ? "+" : "")+boost::lexical_cast<std::string>(numb)); //negative values don't need "+"
  956. to+="\n"+from;
  957. return numb;
  958. }
  959. void CCreaInfo::clickRight(tribool down, bool previousState)
  960. {
  961. if(down)
  962. {
  963. int summ=0;
  964. std::string descr=CGI->generaltexth->allTexts[589];//Growth of creature is number
  965. boost::algorithm::replace_first(descr,"%s", creature->nameSing);
  966. boost::algorithm::replace_first(descr,"%d", boost::lexical_cast<std::string>(town->creatureGrowth(level)));
  967. descr +="\n"+CGI->generaltexth->allTexts[590];
  968. summ = creature->growth;
  969. boost::algorithm::replace_first(descr,"%d", boost::lexical_cast<std::string>(summ));
  970. if ( level>=0 && level<CREATURES_PER_TOWN)
  971. {
  972. if ( vstd::contains(town->builtBuildings, Buildings::CASTLE))
  973. summ+=AddToString(CGI->buildh->buildings[town->subID][Buildings::CASTLE]->Name()+" %+d",descr,summ);
  974. else if ( vstd::contains(town->builtBuildings, Buildings::CITADEL))
  975. summ+=AddToString(CGI->buildh->buildings[town->subID][Buildings::CITADEL]->Name()+" %+d",descr,summ/2);
  976. summ+=AddToString(CGI->generaltexth->allTexts[63] + " %+d",descr, //double growth or plague
  977. summ * creature->valOfBonuses(Bonus::CREATURE_GROWTH_PERCENT)/100);
  978. summ+=AddToString(CGI->generaltexth->artifNames[133] + " %+d",descr,
  979. summ * town->valOfGlobalBonuses
  980. (Selector::type(Bonus::CREATURE_GROWTH_PERCENT) && Selector::sourceType(Bonus::ARTIFACT))/100); //Statue of Legion
  981. if(town->town->hordeLvl[0]==level)//horde, x to summ
  982. if( vstd::contains(town->builtBuildings, Buildings::HORDE_1) || vstd::contains(town->builtBuildings, Buildings::HORDE_1_UPGR))
  983. summ+=AddToString(CGI->buildh->buildings[town->subID][Buildings::HORDE_1]->Name()+" %+d",descr,
  984. creature->hordeGrowth);
  985. if(town->town->hordeLvl[1]==level)//horde, x to summ
  986. if( vstd::contains(town->builtBuildings, Buildings::HORDE_2) || vstd::contains(town->builtBuildings, Buildings::HORDE_2_UPGR))
  987. summ+=AddToString(CGI->buildh->buildings[town->subID][Buildings::HORDE_2]->Name()+" %+d",descr,
  988. creature->hordeGrowth);
  989. int cnt = 0;
  990. std::vector< const CGDwelling * > myDwellings = LOCPLINT->cb->getMyDwellings();
  991. for (std::vector<const CGDwelling*>::const_iterator it = myDwellings.begin(); it != myDwellings.end(); ++it)
  992. if (CGI->creh->creatures[town->town->basicCreatures[level]]->idNumber == (*it)->creatures[0].second[0])
  993. cnt++;//external dwellings count to summ
  994. summ+=AddToString(CGI->generaltexth->allTexts[591],descr,cnt);
  995. BonusList bl;
  996. const CGHeroInstance *hero = town->garrisonHero;
  997. if (hero)
  998. hero->getBonuses(bl, Selector::type(Bonus::CREATURE_GROWTH) && Selector::subtype(level)
  999. && Selector::sourceType(Bonus::ARTIFACT), hero);
  1000. hero = town->visitingHero;
  1001. if (hero)
  1002. hero->getBonuses(bl, Selector::type(Bonus::CREATURE_GROWTH) && Selector::subtype(level)
  1003. && Selector::sourceType(Bonus::ARTIFACT), hero);
  1004. if (bl.size())
  1005. summ+=AddToString (CGI->arth->artifacts[bl.front()->sid]->Name()+" %+d", descr, bl.totalValue());
  1006. //TODO: player bonuses
  1007. if(vstd::contains(town->builtBuildings, Buildings::GRAIL)) //grail - +50% to ALL growth
  1008. summ+=AddToString(CGI->buildh->buildings[town->subID][Buildings::GRAIL]->Name()+" %+d",descr,summ/2);
  1009. summ+=AddToString(CGI->generaltexth->allTexts[63] + " %+d",descr, creature->valOfBonuses(Bonus::CREATURE_GROWTH));
  1010. }
  1011. CInfoPopup *mess = new CInfoPopup();//creating popup
  1012. mess->free = true;
  1013. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1014. (LOCPLINT->playerID, descr,graphics->bigImgs[creature->idNumber],"");
  1015. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1016. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1017. GH.pushInt(mess);
  1018. }
  1019. }
  1020. CTownInfo::CTownInfo(int posX, int posY, const CGTownInstance* Town, bool townHall):
  1021. town(Town),
  1022. building(NULL)
  1023. {
  1024. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1025. used = RCLICK | HOVER;
  1026. pos.x += posX;
  1027. pos.y += posY;
  1028. int buildID;
  1029. if (townHall)
  1030. {
  1031. buildID = 10 + town->hallLevel();
  1032. picture = new CAnimImage("ITMTL.DEF", town->hallLevel());
  1033. }
  1034. else
  1035. {
  1036. buildID = 6 + town->fortLevel();
  1037. if (buildID == 6)
  1038. return;
  1039. picture = new CAnimImage("ITMCL.DEF", town->fortLevel()-1);
  1040. }
  1041. building = CGI->buildh->buildings[town->subID][buildID];
  1042. pos = picture->pos;
  1043. }
  1044. void CTownInfo::hover(bool on)
  1045. {
  1046. if(on)
  1047. {
  1048. if ( building )
  1049. GH.statusbar->print(building->Name());
  1050. }
  1051. else
  1052. GH.statusbar->clear();
  1053. }
  1054. void CTownInfo::clickRight(tribool down, bool previousState)
  1055. {//FIXME: castleInt may be NULL
  1056. if(down && building && LOCPLINT->castleInt)
  1057. {
  1058. CInfoPopup *mess = new CInfoPopup();
  1059. mess->free = true;
  1060. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1061. (LOCPLINT->playerID,
  1062. building->Description(),
  1063. LOCPLINT->castleInt->bicons->ourImages[building->bid].bitmap,
  1064. building->Name());
  1065. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1066. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1067. GH.pushInt(mess);
  1068. }
  1069. }
  1070. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  1071. {
  1072. if(key.state != SDL_PRESSED) return;
  1073. switch(key.keysym.sym)
  1074. {
  1075. case SDLK_UP:
  1076. if(townlist->selected)
  1077. {
  1078. townlist->selected--;
  1079. townlist->from--;
  1080. townChange();
  1081. }
  1082. break;
  1083. case SDLK_DOWN:
  1084. if(townlist->selected < LOCPLINT->towns.size() - 1)
  1085. {
  1086. townlist->selected++;
  1087. townlist->from++;
  1088. townChange();
  1089. }
  1090. break;
  1091. case SDLK_SPACE:
  1092. if(!!town->visitingHero && town->garrisonHero)
  1093. {
  1094. LOCPLINT->cb->swapGarrisonHero(town);
  1095. }
  1096. break;
  1097. default:
  1098. break;
  1099. }
  1100. }
  1101. HeroSlots::HeroSlots(const CGTownInstance * Town, Point garrPos, Point visitPos, CGarrisonInt *Garrison, bool ShowEmpty):
  1102. showEmpty(ShowEmpty),
  1103. town(Town),
  1104. garr(Garrison)
  1105. {
  1106. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1107. garrisonedHero = new CHeroGSlot(garrPos.x, garrPos.y, 0, town->garrisonHero, this);
  1108. visitingHero = new CHeroGSlot(visitPos.x, visitPos.y, 1, town->visitingHero, this);
  1109. }
  1110. void HeroSlots::update()
  1111. {
  1112. garrisonedHero->hero = town->garrisonHero;
  1113. visitingHero->hero = town->visitingHero;
  1114. }
  1115. void HeroSlots::splitClicked()
  1116. {
  1117. if(!!town->visitingHero && town->garrisonHero && (visitingHero->highlight || garrisonedHero->highlight))
  1118. {
  1119. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  1120. }
  1121. }
  1122. void CHallInterface::CBuildingBox::hover(bool on)
  1123. {
  1124. if(on)
  1125. {
  1126. std::string toPrint;
  1127. if(state==Buildings::PREREQUIRES)
  1128. toPrint = CGI->generaltexth->hcommands[5];
  1129. else if(state==Buildings::CANT_BUILD_TODAY)
  1130. toPrint = CGI->generaltexth->allTexts[223];
  1131. else
  1132. toPrint = CGI->generaltexth->hcommands[state];
  1133. boost::algorithm::replace_first(toPrint,"%s",building->Name());
  1134. GH.statusbar->print(toPrint);
  1135. }
  1136. else
  1137. GH.statusbar->clear();
  1138. }
  1139. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1140. {
  1141. if(previousState && (!down))
  1142. GH.pushInt(new CBuildWindow(town,building,state,0));
  1143. }
  1144. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1145. {
  1146. if(down)
  1147. GH.pushInt(new CBuildWindow(town,building,state,1));
  1148. }
  1149. CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance * Town, const CBuilding * Building):
  1150. town(Town),
  1151. building(Building)
  1152. {
  1153. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1154. used = LCLICK | RCLICK | HOVER;
  1155. pos.x += x;
  1156. pos.y += y;
  1157. pos.w = 154;
  1158. pos.h = 92;
  1159. state = LOCPLINT->cb->canBuildStructure(town,building->bid);
  1160. assert(state < Buildings::ERROR);
  1161. static int panelIndex[9] = { 3, 3, 3, 0, 0, 2, 2, 1, 2};
  1162. static int iconIndex[9] = {-1, -1, -1, 0, 0, 1, 2, -1, 1};
  1163. picture = new CAnimImage(graphics->buildingPics[town->subID], building->bid, 0, 2, 2);
  1164. panel = new CAnimImage("TPTHBAR", panelIndex[state], 0, 1, 73);
  1165. if ( iconIndex[state] >=0 )
  1166. icon = new CAnimImage("TPTHCHK", iconIndex[state], 0, 136, 56);
  1167. label = new CLabel(75, 81, FONT_SMALL, CENTER, zwykly, building->Name());
  1168. }
  1169. CHallInterface::CHallInterface(const CGTownInstance *Town):
  1170. town(Town)
  1171. {
  1172. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1173. background = new CPicture(CGI->buildh->hall[town->subID].first);
  1174. background->colorize(LOCPLINT->playerID);
  1175. pos = background->center();
  1176. resdatabar = new CMinorResDataBar;
  1177. resdatabar->pos.x += pos.x;
  1178. resdatabar->pos.y += pos.y;
  1179. Rect barRect(5, 556, 740, 18);
  1180. statusBar = new CGStatusBar(new CPicture(*background, barRect, 5, 556, false));
  1181. title = new CLabel(399, 12, FONT_MEDIUM, CENTER, zwykly, CGI->buildh->buildings[town->subID][town->hallLevel()+Buildings::VILLAGE_HALL]->Name());
  1182. exit = new AdventureMapButton(CGI->generaltexth->hcommands[8], "",
  1183. boost::bind(&CHallInterface::close,this), 748, 556, "TPMAGE1.DEF", SDLK_RETURN);
  1184. exit->assignedKeys.insert(SDLK_ESCAPE);
  1185. const std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[town->subID].second;
  1186. boxes.resize(boxList.size());
  1187. for(size_t row=0; row<boxList.size(); row++) //for each row
  1188. {
  1189. for(size_t col=0; col<boxList[row].size(); col++) //for each box
  1190. {
  1191. const CBuilding *building = NULL;
  1192. for(size_t item=0; item<boxList[row][col].size(); item++)//we are looking for the first not build structure
  1193. {
  1194. int buildingID = boxList[row][col][item];
  1195. building = CGI->buildh->buildings[town->subID][buildingID];
  1196. if (buildingID == 18 && vstd::contains(town->builtBuildings, town->town->hordeLvl[0]+37))
  1197. continue;
  1198. if (buildingID == 24 && vstd::contains(town->builtBuildings, town->town->hordeLvl[1]+37))
  1199. continue;
  1200. if(vstd::contains(town->builtBuildings,buildingID))
  1201. continue;
  1202. break;
  1203. }
  1204. int posX = pos.w/2 - boxList[row].size()*154/2 - (boxList[row].size()-1)*20 + 194*col,
  1205. posY = 35 + 104*row;
  1206. if (building)
  1207. boxes[row].push_back(new CBuildingBox(posX, posY, town, building));
  1208. }
  1209. }
  1210. }
  1211. void CHallInterface::close()
  1212. {
  1213. GH.popIntTotally(this);
  1214. }
  1215. void CBuildWindow::buyFunc()
  1216. {
  1217. LOCPLINT->cb->buildBuilding(town,building->bid);
  1218. GH.popInts(2); //we - build window and hall screen
  1219. }
  1220. void CBuildWindow::close()
  1221. {
  1222. GH.popIntTotally(this);
  1223. }
  1224. void CBuildWindow::clickRight(tribool down, bool previousState)
  1225. {
  1226. if((!down || indeterminate(down)) && mode)
  1227. close();
  1228. }
  1229. std::string CBuildWindow::getTextForState(int state)
  1230. {
  1231. std::string ret;
  1232. if(state<7)
  1233. ret = CGI->generaltexth->hcommands[state];
  1234. switch (state)
  1235. {
  1236. case 4: case 5: case 6:
  1237. ret.replace(ret.find_first_of("%s"),2,building->Name());
  1238. break;
  1239. case 7:
  1240. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1241. case 8:
  1242. {
  1243. ret = CGI->generaltexth->allTexts[52];
  1244. std::set<int> reqs= LOCPLINT->cb->getBuildingRequiments(town, building->bid);
  1245. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1246. {
  1247. if (vstd::contains(town->builtBuildings, *i))
  1248. continue;//skipping constructed buildings
  1249. ret+= CGI->buildh->buildings[town->subID][*i]->Name() + ", ";
  1250. }
  1251. ret.erase(ret.size()-2);
  1252. }
  1253. }
  1254. return ret;
  1255. }
  1256. CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, int State, bool Mode)
  1257. :town(Town), building(Building), state(State), mode(Mode)
  1258. {
  1259. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1260. used |= RCLICK;
  1261. background = new CPicture("TPUBUILD");
  1262. pos = background->center();
  1263. background->colorize(LOCPLINT->playerID);
  1264. buildingPic = new CAnimImage(graphics->buildingPics[town->subID], building->bid, 0, 125, 50);
  1265. Rect barRect(9, 494, 380, 18);
  1266. statusBar = new CGStatusBar(new CPicture(*background, barRect, 9, 494, false));
  1267. title = new CLabel(197, 30, FONT_MEDIUM, CENTER, zwykly,
  1268. boost::str(boost::format(CGI->generaltexth->hcommands[7]) % building->Name()));
  1269. buildingDescr = new CTextBox(building->Description(), Rect(33, 135, 329, 67), 0, FONT_MEDIUM, CENTER);
  1270. buildingState = new CTextBox(getTextForState(state), Rect(33, 216, 329, 67), 0, FONT_SMALL, CENTER);
  1271. //Create objects for all required resources
  1272. for(int i = 0; i<RESOURCE_QUANTITY; i++)
  1273. {
  1274. if(building->resources[i])
  1275. {
  1276. resPicture.push_back(new CPicture(graphics->resources32->ourImages[i].bitmap, 0, 0, false));
  1277. resAmount.push_back(new CLabel(0,0, FONT_SMALL, CENTER, zwykly,
  1278. boost::lexical_cast<std::string>(building->resources[i])));
  1279. }
  1280. }
  1281. unsigned int rowSize[2];
  1282. int posY;
  1283. if (resAmount.size() > 4)
  1284. {//Resources will be placed in multiple rows
  1285. rowSize[0] = (resAmount.size()+1)/2;
  1286. posY = 303;
  1287. }
  1288. else
  1289. {//one row
  1290. rowSize[0] = resAmount.size();
  1291. posY = 340;
  1292. }
  1293. rowSize[1] = resAmount.size() - rowSize[0];
  1294. unsigned int index=0;
  1295. for (size_t row=0; row<2; row++)
  1296. {
  1297. int posX = pos.w/2 - rowSize[row] * 40 + 24;
  1298. for (size_t i=0; i<rowSize[row]; i++)
  1299. {//Move current resource to correct position
  1300. resPicture[index]->moveBy(Point(posX, posY));
  1301. resAmount[index]->moveBy(Point(posX+16, posY+48));
  1302. posX += 80;
  1303. index++;
  1304. }
  1305. posY +=75;
  1306. }
  1307. if(!mode)
  1308. {
  1309. buy = new AdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[595]) % building->Name()),
  1310. "", boost::bind(&CBuildWindow::buyFunc,this), 45, 446,"IBUY30", SDLK_RETURN);
  1311. buy->borderColor = Colors::MetallicGold;
  1312. buy->borderEnabled = true;
  1313. cancel = new AdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[596]) % building->Name()),
  1314. "", boost::bind(&CBuildWindow::close,this), 290, 445, "ICANCEL", SDLK_ESCAPE);
  1315. cancel->borderColor = Colors::MetallicGold;
  1316. cancel->borderEnabled = true;
  1317. buy->block(state!=7);
  1318. }
  1319. }
  1320. CBuildWindow::~CBuildWindow()
  1321. {}
  1322. void CFortScreen::close()
  1323. {
  1324. GH.popIntTotally(this);
  1325. }
  1326. CFortScreen::CFortScreen(const CGTownInstance * town)
  1327. {
  1328. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1329. unsigned int fortSize = town->creatures.size();
  1330. if (fortSize > CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1331. fortSize--;
  1332. if (fortSize == CREATURES_PER_TOWN)
  1333. background = new CPicture("TPCASTL7");
  1334. else
  1335. background = new CPicture("TPCASTL8");
  1336. background->colorize(LOCPLINT->playerID);
  1337. pos = background->center();
  1338. const CBuilding *fortBuilding = CGI->buildh->buildings[town->subID][town->fortLevel()+6];
  1339. title = new CLabel(400, 12, FONT_BIG, CENTER, zwykly, fortBuilding->Name());
  1340. std::string text = boost::str(boost::format(CGI->generaltexth->fcommands[6]) % fortBuilding->Name());
  1341. exit = new AdventureMapButton(text, "", boost::bind(&CFortScreen::close,this) ,748, 556, "TPMAGE1", SDLK_RETURN);
  1342. std::vector<Point> positions;
  1343. positions += Point(10, 22), Point(404, 22),
  1344. Point(10, 155), Point(404,155),
  1345. Point(10, 288), Point(404,288);
  1346. if (fortSize == CREATURES_PER_TOWN)
  1347. positions += Point(206,421);
  1348. else
  1349. positions += Point(10, 421), Point(404,421);
  1350. for (unsigned int i=0; i<fortSize; i++)
  1351. {
  1352. int buildingID;
  1353. if (fortSize == CREATURES_PER_TOWN)
  1354. {
  1355. if (vstd::contains(town->builtBuildings, Buildings::DWELL_UP_FIRST+i))
  1356. buildingID = Buildings::DWELL_UP_FIRST+i;
  1357. else
  1358. buildingID = Buildings::DWELL_FIRST+i;
  1359. }
  1360. else
  1361. buildingID = 22;
  1362. recAreas.push_back(new RecruitArea(positions[i].x, positions[i].y, town, buildingID, i));
  1363. }
  1364. resdatabar = new CMinorResDataBar;
  1365. resdatabar->pos.x += pos.x;
  1366. resdatabar->pos.y += pos.y;
  1367. Rect barRect(4, 554, 740, 18);
  1368. statusBar = new CGStatusBar(new CPicture(*background, barRect, 4, 554, false));
  1369. }
  1370. void CFortScreen::creaturesChanged()
  1371. {
  1372. for (size_t i=0; i<recAreas.size(); i++)
  1373. recAreas[i]->creaturesChanged();
  1374. }
  1375. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int min, int max)
  1376. {
  1377. pos.x+=size.x;
  1378. pos.y+=size.y;
  1379. pos.w = size.w;
  1380. pos.h = size.h;
  1381. init(name, descr, min, max);
  1382. }
  1383. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int val)
  1384. {
  1385. pos.x+=size.x;
  1386. pos.y+=size.y;
  1387. pos.w = size.w;
  1388. pos.h = size.h;
  1389. init(name, descr, val, val);
  1390. }
  1391. void LabeledValue::init(std::string nameText, std::string descr, int min, int max)
  1392. {
  1393. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1394. used |= HOVER;
  1395. hoverText = descr;
  1396. std::string valueText;
  1397. if (min && max)
  1398. {
  1399. valueText = boost::lexical_cast<std::string>(min);
  1400. if (min != max)
  1401. valueText += '-' + boost::lexical_cast<std::string>(max);
  1402. }
  1403. name = new CLabel(3, 0, FONT_SMALL, TOPLEFT, zwykly, nameText);
  1404. value = new CLabel(pos.w-3, pos.h-2, FONT_SMALL, BOTTOMRIGHT, zwykly, valueText);
  1405. }
  1406. void LabeledValue::hover(bool on)
  1407. {
  1408. if(on)
  1409. GH.statusbar->print(hoverText);
  1410. else
  1411. {
  1412. GH.statusbar->clear();
  1413. parent->hovered = false;
  1414. }
  1415. }
  1416. CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance *Town, int buildingID, int Level):
  1417. town(Town),
  1418. level(Level),
  1419. availableCount(NULL)
  1420. {
  1421. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1422. pos.x +=posX;
  1423. pos.y +=posY;
  1424. pos.w = 386;
  1425. pos.h = 126;
  1426. if (!town->creatures[level].second.empty())
  1427. used |= LCLICK | RCLICK | HOVER;//Activate only if dwelling is present
  1428. icons = new CPicture("ZPCAINFO", 261, 3);
  1429. buildingPic = new CAnimImage(graphics->buildingPics[town->subID], buildingID, 0, 4, 21);
  1430. const CCreature* creature = NULL;
  1431. if (!town->creatures[level].second.empty())
  1432. creature = CGI->creh->creatures[town->creatures[level].second.back()];
  1433. else
  1434. creature = CGI->creh->creatures[town->town->basicCreatures[level]];
  1435. hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % creature->namePl);
  1436. creatureAnim = new CCreaturePic(159, 4, creature, false);
  1437. Rect sizes(287, 4, 96, 18);
  1438. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], creature->attack));
  1439. sizes.y+=20;
  1440. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[191], CGI->generaltexth->fcommands[1], creature->defence));
  1441. sizes.y+=21;
  1442. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[199], CGI->generaltexth->fcommands[2], creature->damageMin, creature->damageMax));
  1443. sizes.y+=20;
  1444. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[388], CGI->generaltexth->fcommands[3], creature->hitPoints));
  1445. sizes.y+=21;
  1446. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[193], CGI->generaltexth->fcommands[4], creature->speed));
  1447. sizes.y+=20;
  1448. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[194], CGI->generaltexth->fcommands[5], town->creatureGrowth(level)));
  1449. creatureName = new CLabel(78, 11, FONT_SMALL, CENTER, zwykly, creature->namePl);
  1450. dwellingName = new CLabel(78, 101, FONT_SMALL, CENTER, zwykly, CGI->buildh->buildings[town->subID][buildingID]->Name());
  1451. if (vstd::contains(town->builtBuildings, buildingID))
  1452. {
  1453. unsigned int available = town->creatures[level].first;
  1454. std::string availableText = CGI->generaltexth->allTexts[217]+ boost::lexical_cast<std::string>(available);
  1455. availableCount = new CLabel(78, 119, FONT_SMALL, CENTER, zwykly, availableText);
  1456. }
  1457. }
  1458. void CFortScreen::RecruitArea::hover(bool on)
  1459. {
  1460. if(on)
  1461. GH.statusbar->print(hoverText);
  1462. else
  1463. GH.statusbar->clear();
  1464. }
  1465. void CFortScreen::RecruitArea::creaturesChanged()
  1466. {
  1467. if (availableCount)
  1468. {
  1469. std::string availableText = CGI->generaltexth->allTexts[217] +
  1470. boost::lexical_cast<std::string>(town->creatures[level].first);
  1471. availableCount->setTxt(availableText);
  1472. }
  1473. }
  1474. void CFortScreen::RecruitArea::clickLeft(tribool down, bool previousState)
  1475. {
  1476. if(!down && previousState)
  1477. LOCPLINT->castleInt->builds->enterDwelling(level);
  1478. }
  1479. void CFortScreen::RecruitArea::clickRight(tribool down, bool previousState)
  1480. {
  1481. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1482. }
  1483. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1484. {
  1485. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1486. background = new CPicture("TPMAGE.bmp");
  1487. pos = background->center();
  1488. window = new CPicture(graphics->guildBgs[owner->town->subID], 332, 76);
  1489. resdatabar = new CMinorResDataBar;
  1490. resdatabar->pos.x += pos.x;
  1491. resdatabar->pos.y += pos.y;
  1492. Rect barRect(7, 556, 737, 18);
  1493. statusBar = new CGStatusBar(new CPicture(*background, barRect, 7, 556, false));
  1494. exit = new AdventureMapButton(CGI->generaltexth->allTexts[593],"",boost::bind(&CMageGuildScreen::close,this), 748, 556,"TPMAGE1.DEF",SDLK_RETURN);
  1495. exit->assignedKeys.insert(SDLK_ESCAPE);
  1496. std::vector<std::vector<Point> > positions;
  1497. positions.resize(5);
  1498. positions[0] += Point(222,445), Point(312,445), Point(402,445), Point(520,445), Point(610,445), Point(700,445);
  1499. positions[1] += Point(48,53), Point(48,147), Point(48,241), Point(48,335), Point(48,429);
  1500. positions[2] += Point(570,82), Point(672,82), Point(570,157), Point(672,157);
  1501. positions[3] += Point(183,42), Point(183,148), Point(183,253);
  1502. positions[4] += Point(491,325), Point(591,325);
  1503. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1504. {
  1505. size_t spellCount = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1506. for(size_t j=0; j<spellCount; j++)
  1507. {
  1508. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1509. spells.push_back( new Scroll(positions[i][j], CGI->spellh->spells[owner->town->spells[i][j]]));
  1510. else
  1511. new CAnimImage("TPMAGES.DEF", 1, 0, positions[i][j].x, positions[i][j].y);//closed scroll
  1512. }
  1513. }
  1514. }
  1515. void CMageGuildScreen::close()
  1516. {
  1517. GH.popIntTotally(this);
  1518. }
  1519. CMageGuildScreen::Scroll::Scroll(Point position, const CSpell *Spell)
  1520. :spell(Spell)
  1521. {
  1522. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1523. used = LCLICK | RCLICK | HOVER;
  1524. pos += position;
  1525. image = new CAnimImage("SPELLSCR", spell->id);
  1526. pos = image->pos;
  1527. }
  1528. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1529. {
  1530. if(down)
  1531. {
  1532. std::vector<SComponent*> comps(1,
  1533. new SComponent(SComponent::spell,spell->id,0)
  1534. );
  1535. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1536. }
  1537. }
  1538. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1539. {
  1540. if(down)
  1541. {
  1542. CInfoPopup *vinya = new CInfoPopup();
  1543. vinya->free = true;
  1544. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1545. (LOCPLINT->playerID,
  1546. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1547. spell->name,30,30);
  1548. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1549. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1550. GH.pushInt(vinya);
  1551. }
  1552. }
  1553. void CMageGuildScreen::Scroll::hover(bool on)
  1554. {
  1555. if(on)
  1556. GH.statusbar->print(spell->name);
  1557. else
  1558. GH.statusbar->clear();
  1559. }
  1560. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1561. {
  1562. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1563. background = new CPicture("TPSMITH");
  1564. pos = background->center();
  1565. background->colorize(LOCPLINT->playerID);
  1566. statusBar = new CGStatusBar(164, 370);
  1567. animBG = new CPicture("TPSMITBK", 64, 50);
  1568. animBG->needRefresh = true;
  1569. const CCreature *creature = CGI->creh->creatures[creMachineID];
  1570. anim = new CCreatureAnim(64, 50, creature->animDefName, Rect());
  1571. anim->clipRect(113,125,200,150);
  1572. title = new CLabel(165, 28, FONT_BIG, CENTER, tytulowy,
  1573. boost::str(boost::format(CGI->generaltexth->allTexts[274]) % creature->nameSing));
  1574. costText = new CLabel(165, 218, FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->jktexts[43]);
  1575. costValue = new CLabel(165, 290, FONT_MEDIUM, CENTER, zwykly,
  1576. boost::lexical_cast<std::string>(CGI->arth->artifacts[aid]->price));
  1577. std::string text = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % creature->nameSing);
  1578. buy = new AdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 42, 312,"IBUY30.DEF",SDLK_RETURN);
  1579. text = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % creature->nameSing);
  1580. cancel = new AdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 224, 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1581. if(possible)
  1582. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1583. else
  1584. buy->block(true);
  1585. gold = new CPicture(graphics->resources32->ourImages[6].bitmap,148,244, false);
  1586. }
  1587. void CBlacksmithDialog::close()
  1588. {
  1589. GH.popIntTotally(this);
  1590. }