CCastleInterface.cpp 49 KB

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