CCastleInterface.cpp 54 KB

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