CCastleInterface.cpp 54 KB

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