CCastleInterface.cpp 53 KB

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