CCastleInterface.cpp 55 KB

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