CCastleInterface.cpp 50 KB

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