CCastleInterface.cpp 55 KB

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