CCastleInterface.cpp 50 KB

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