CCastleInterface.cpp 53 KB

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