CCastleInterface.cpp 57 KB

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