2
0

CCastleInterface.cpp 55 KB

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