CCastleInterface.cpp 50 KB

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