CCastleInterface.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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. std::vector<CComponent*> comps(1, new CComponent(CComponent::building, bld->tid, bld->bid));
  100. CRClickPopup::createAndPush(bld->Description(), comps);
  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::Cornsilk, 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::Cornsilk, CGI->generaltexth->allTexts[217] + text);
  217. costPerTroop = new CLabel(80, 227, FONT_SMALL, CENTER, Colors::Cornsilk, 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::Cornsilk, 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->splitting = false;
  292. owner->garr->highlighted = NULL;
  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->highlighted = NULL;
  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+9);
  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::Cornsilk, 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::Cornsilk, 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::Cornsilk, 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. {
  924. std::string descr = genGrowthText();
  925. CInfoPopup *mess = new CInfoPopup();//creating popup
  926. mess->free = true;
  927. mess->bitmap = CMessage::drawBoxTextBitmapSub
  928. (LOCPLINT->playerID, descr,graphics->bigImgs[creature->idNumber],"");
  929. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  930. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  931. GH.pushInt(mess);
  932. }
  933. }
  934. }
  935. CTownInfo::CTownInfo(int posX, int posY, const CGTownInstance* Town, bool townHall):
  936. town(Town),
  937. building(NULL)
  938. {
  939. OBJ_CONSTRUCTION_CAPTURING_ALL;
  940. addUsedEvents(RCLICK | HOVER);
  941. pos.x += posX;
  942. pos.y += posY;
  943. int buildID;
  944. if (townHall)
  945. {
  946. buildID = 10 + town->hallLevel();
  947. picture = new CAnimImage("ITMTL.DEF", town->hallLevel());
  948. }
  949. else
  950. {
  951. buildID = 6 + town->fortLevel();
  952. if (buildID == 6)
  953. return;
  954. picture = new CAnimImage("ITMCL.DEF", town->fortLevel()-1);
  955. }
  956. building = town->town->buildings[buildID];
  957. pos = picture->pos;
  958. }
  959. void CTownInfo::hover(bool on)
  960. {
  961. if(on)
  962. {
  963. if ( building )
  964. GH.statusbar->print(building->Name());
  965. }
  966. else
  967. GH.statusbar->clear();
  968. }
  969. void CTownInfo::clickRight(tribool down, bool previousState)
  970. {//FIXME: castleInt may be NULL
  971. if(down && building && LOCPLINT->castleInt)
  972. {
  973. CInfoPopup *mess = new CInfoPopup();
  974. mess->free = true;
  975. mess->bitmap = CMessage::drawBoxTextBitmapSub
  976. (LOCPLINT->playerID,
  977. building->Description(),
  978. LOCPLINT->castleInt->bicons->ourImages[building->bid].bitmap,
  979. building->Name());
  980. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  981. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  982. GH.pushInt(mess);
  983. }
  984. }
  985. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  986. {
  987. if(key.state != SDL_PRESSED) return;
  988. switch(key.keysym.sym)
  989. {
  990. case SDLK_UP:
  991. townlist->selectPrev();
  992. break;
  993. case SDLK_DOWN:
  994. townlist->selectNext();
  995. break;
  996. case SDLK_SPACE:
  997. heroes->swapArmies();
  998. break;
  999. case SDLK_t:
  1000. if(town->hasBuilt(EBuilding::TAVERN))
  1001. LOCPLINT->showTavernWindow(town);
  1002. break;
  1003. default:
  1004. break;
  1005. }
  1006. }
  1007. HeroSlots::HeroSlots(const CGTownInstance * Town, Point garrPos, Point visitPos, CGarrisonInt *Garrison, bool ShowEmpty):
  1008. showEmpty(ShowEmpty),
  1009. town(Town),
  1010. garr(Garrison)
  1011. {
  1012. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1013. garrisonedHero = new CHeroGSlot(garrPos.x, garrPos.y, 0, town->garrisonHero, this);
  1014. visitingHero = new CHeroGSlot(visitPos.x, visitPos.y, 1, town->visitingHero, this);
  1015. }
  1016. void HeroSlots::update()
  1017. {
  1018. garrisonedHero->set(town->garrisonHero);
  1019. visitingHero->set(town->visitingHero);
  1020. }
  1021. void HeroSlots::splitClicked()
  1022. {
  1023. if(!!town->visitingHero && town->garrisonHero && (visitingHero->selection || garrisonedHero->selection))
  1024. {
  1025. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  1026. }
  1027. }
  1028. void HeroSlots::swapArmies()
  1029. {
  1030. if(!town->garrisonHero && town->visitingHero) //visiting => garrison, merge armies: town army => hero army
  1031. {
  1032. if(!town->visitingHero->canBeMergedWith(*town))
  1033. {
  1034. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[275], std::vector<CComponent*>(), soundBase::sound_todo);
  1035. return;
  1036. }
  1037. }
  1038. LOCPLINT->cb->swapGarrisonHero(town);
  1039. }
  1040. void CHallInterface::CBuildingBox::hover(bool on)
  1041. {
  1042. if(on)
  1043. {
  1044. std::string toPrint;
  1045. if(state==EBuildingState::PREREQUIRES)
  1046. toPrint = CGI->generaltexth->hcommands[5];
  1047. else if(state==EBuildingState::CANT_BUILD_TODAY)
  1048. toPrint = CGI->generaltexth->allTexts[223];
  1049. else
  1050. toPrint = CGI->generaltexth->hcommands[state];
  1051. boost::algorithm::replace_first(toPrint,"%s",building->Name());
  1052. GH.statusbar->print(toPrint);
  1053. }
  1054. else
  1055. GH.statusbar->clear();
  1056. }
  1057. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1058. {
  1059. if(previousState && (!down))
  1060. GH.pushInt(new CBuildWindow(town,building,state,0));
  1061. }
  1062. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1063. {
  1064. if(down)
  1065. GH.pushInt(new CBuildWindow(town,building,state,1));
  1066. }
  1067. CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance * Town, const CBuilding * Building):
  1068. town(Town),
  1069. building(Building)
  1070. {
  1071. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1072. addUsedEvents(LCLICK | RCLICK | HOVER);
  1073. pos.x += x;
  1074. pos.y += y;
  1075. pos.w = 154;
  1076. pos.h = 92;
  1077. state = LOCPLINT->cb->canBuildStructure(town,building->bid);
  1078. assert(state < EBuildingState::BUILDING_ERROR);
  1079. static int panelIndex[9] = { 3, 3, 3, 0, 0, 2, 2, 1, 2};
  1080. static int iconIndex[9] = {-1, -1, -1, 0, 0, 1, 2, -1, 1};
  1081. picture = new CAnimImage(town->town->clientInfo.buildingsIcons, building->bid, 0, 2, 2);
  1082. panel = new CAnimImage("TPTHBAR", panelIndex[state], 0, 1, 73);
  1083. if ( iconIndex[state] >=0 )
  1084. icon = new CAnimImage("TPTHCHK", iconIndex[state], 0, 136, 56);
  1085. label = new CLabel(75, 81, FONT_SMALL, CENTER, Colors::Cornsilk, building->Name());
  1086. }
  1087. CHallInterface::CHallInterface(const CGTownInstance *Town):
  1088. CWindowObject(PLAYER_COLORED | BORDERED, Town->town->clientInfo.hallBackground),
  1089. town(Town)
  1090. {
  1091. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1092. resdatabar = new CMinorResDataBar;
  1093. resdatabar->pos.x += pos.x;
  1094. resdatabar->pos.y += pos.y;
  1095. Rect barRect(5, 556, 740, 18);
  1096. statusBar = new CGStatusBar(new CPicture(*background, barRect, 5, 556, false));
  1097. title = new CLabel(399, 12, FONT_MEDIUM, CENTER, Colors::Cornsilk, town->town->buildings[town->hallLevel()+EBuilding::VILLAGE_HALL]->Name());
  1098. exit = new CAdventureMapButton(CGI->generaltexth->hcommands[8], "",
  1099. boost::bind(&CHallInterface::close,this), 748, 556, "TPMAGE1.DEF", SDLK_RETURN);
  1100. exit->assignedKeys.insert(SDLK_ESCAPE);
  1101. auto & boxList = town->town->clientInfo.hallSlots;
  1102. boxes.resize(boxList.size());
  1103. for(size_t row=0; row<boxList.size(); row++) //for each row
  1104. {
  1105. for(size_t col=0; col<boxList[row].size(); col++) //for each box
  1106. {
  1107. const CBuilding *building = NULL;
  1108. for(size_t item=0; item<boxList[row][col].size(); item++)//we are looking for the first not build structure
  1109. {
  1110. int buildingID = boxList[row][col][item];
  1111. building = town->town->buildings[buildingID];
  1112. if(!vstd::contains(town->builtBuildings,buildingID))
  1113. break;
  1114. }
  1115. int posX = pos.w/2 - boxList[row].size()*154/2 - (boxList[row].size()-1)*20 + 194*col,
  1116. posY = 35 + 104*row;
  1117. if (building)
  1118. boxes[row].push_back(new CBuildingBox(posX, posY, town, building));
  1119. }
  1120. }
  1121. }
  1122. void CBuildWindow::buyFunc()
  1123. {
  1124. LOCPLINT->cb->buildBuilding(town,building->bid);
  1125. GH.popInts(2); //we - build window and hall screen
  1126. }
  1127. std::string CBuildWindow::getTextForState(int state)
  1128. {
  1129. std::string ret;
  1130. if(state<7)
  1131. ret = CGI->generaltexth->hcommands[state];
  1132. switch (state)
  1133. {
  1134. case 4: case 5: case 6:
  1135. ret.replace(ret.find_first_of("%s"),2,building->Name());
  1136. break;
  1137. case 7:
  1138. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1139. case 8:
  1140. {
  1141. ret = CGI->generaltexth->allTexts[52];
  1142. std::set<int> reqs= LOCPLINT->cb->getBuildingRequiments(town, building->bid);
  1143. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1144. {
  1145. if (vstd::contains(town->builtBuildings, *i))
  1146. continue;//skipping constructed buildings
  1147. ret+= town->town->buildings[*i]->Name() + ", ";
  1148. }
  1149. ret.erase(ret.size()-2);
  1150. }
  1151. }
  1152. return ret;
  1153. }
  1154. CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, int state, bool rightClick):
  1155. CWindowObject(PLAYER_COLORED | (rightClick ? RCLICK_POPUP : 0), "TPUBUILD"),
  1156. town(Town),
  1157. building(Building)
  1158. {
  1159. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1160. new CAnimImage(town->town->clientInfo.buildingsIcons, building->bid, 0, 125, 50);
  1161. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1162. new CLabel(197, 30, FONT_MEDIUM, CENTER, Colors::Cornsilk,
  1163. boost::str(boost::format(CGI->generaltexth->hcommands[7]) % building->Name()));
  1164. new CTextBox(building->Description(), Rect(33, 135, 329, 67), 0, FONT_MEDIUM, CENTER);
  1165. new CTextBox(getTextForState(state), Rect(33, 216, 329, 67), 0, FONT_SMALL, CENTER);
  1166. //Create components for all required resources
  1167. std::vector<CComponent *> components;
  1168. for(int i = 0; i<GameConstants::RESOURCE_QUANTITY; i++)
  1169. {
  1170. if(building->resources[i])
  1171. {
  1172. components.push_back(new CComponent(CComponent::resource, i, building->resources[i], CComponent::small));
  1173. }
  1174. }
  1175. new CComponentBox(components, Rect(25, 300, pos.w - 50, 130));
  1176. if(!rightClick)
  1177. { //normal window
  1178. buy = new CAdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[595]) % building->Name()),
  1179. "", boost::bind(&CBuildWindow::buyFunc,this), 45, 446,"IBUY30", SDLK_RETURN);
  1180. buy->borderColor = Colors::MetallicGold;
  1181. buy->borderEnabled = true;
  1182. cancel = new CAdventureMapButton(boost::str(boost::format(CGI->generaltexth->allTexts[596]) % building->Name()),
  1183. "", boost::bind(&CBuildWindow::close,this), 290, 445, "ICANCEL", SDLK_ESCAPE);
  1184. cancel->borderColor = Colors::MetallicGold;
  1185. cancel->borderEnabled = true;
  1186. buy->block(state!=7 || LOCPLINT->playerID != town->tempOwner);
  1187. }
  1188. }
  1189. std::string CFortScreen::getBgName(const CGTownInstance *town)
  1190. {
  1191. ui32 fortSize = town->creatures.size();
  1192. if (fortSize > GameConstants::CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1193. fortSize--;
  1194. if (fortSize == GameConstants::CREATURES_PER_TOWN)
  1195. return "TPCASTL7";
  1196. else
  1197. return "TPCASTL8";
  1198. }
  1199. CFortScreen::CFortScreen(const CGTownInstance * town):
  1200. CWindowObject(PLAYER_COLORED | BORDERED, getBgName(town))
  1201. {
  1202. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1203. ui32 fortSize = town->creatures.size();
  1204. if (fortSize > GameConstants::CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1205. fortSize--;
  1206. const CBuilding *fortBuilding = town->town->buildings[town->fortLevel()+6];
  1207. title = new CLabel(400, 12, FONT_BIG, CENTER, Colors::Cornsilk, fortBuilding->Name());
  1208. std::string text = boost::str(boost::format(CGI->generaltexth->fcommands[6]) % fortBuilding->Name());
  1209. exit = new CAdventureMapButton(text, "", boost::bind(&CFortScreen::close,this) ,748, 556, "TPMAGE1", SDLK_RETURN);
  1210. std::vector<Point> positions;
  1211. positions += Point(10, 22), Point(404, 22),
  1212. Point(10, 155), Point(404,155),
  1213. Point(10, 288), Point(404,288);
  1214. if (fortSize == GameConstants::CREATURES_PER_TOWN)
  1215. positions += Point(206,421);
  1216. else
  1217. positions += Point(10, 421), Point(404,421);
  1218. for (ui32 i=0; i<fortSize; i++)
  1219. {
  1220. int buildingID;
  1221. if (fortSize == GameConstants::CREATURES_PER_TOWN)
  1222. {
  1223. if (vstd::contains(town->builtBuildings, EBuilding::DWELL_UP_FIRST+i))
  1224. buildingID = EBuilding::DWELL_UP_FIRST+i;
  1225. else
  1226. buildingID = EBuilding::DWELL_FIRST+i;
  1227. }
  1228. else
  1229. buildingID = 22;
  1230. recAreas.push_back(new RecruitArea(positions[i].x, positions[i].y, town, buildingID, i));
  1231. }
  1232. resdatabar = new CMinorResDataBar;
  1233. resdatabar->pos.x += pos.x;
  1234. resdatabar->pos.y += pos.y;
  1235. Rect barRect(4, 554, 740, 18);
  1236. statusBar = new CGStatusBar(new CPicture(*background, barRect, 4, 554, false));
  1237. }
  1238. void CFortScreen::creaturesChanged()
  1239. {
  1240. for (size_t i=0; i<recAreas.size(); i++)
  1241. recAreas[i]->creaturesChanged();
  1242. }
  1243. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int min, int max)
  1244. {
  1245. pos.x+=size.x;
  1246. pos.y+=size.y;
  1247. pos.w = size.w;
  1248. pos.h = size.h;
  1249. init(name, descr, min, max);
  1250. }
  1251. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int val)
  1252. {
  1253. pos.x+=size.x;
  1254. pos.y+=size.y;
  1255. pos.w = size.w;
  1256. pos.h = size.h;
  1257. init(name, descr, val, val);
  1258. }
  1259. void LabeledValue::init(std::string nameText, std::string descr, int min, int max)
  1260. {
  1261. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1262. addUsedEvents(HOVER);
  1263. hoverText = descr;
  1264. std::string valueText;
  1265. if (min && max)
  1266. {
  1267. valueText = boost::lexical_cast<std::string>(min);
  1268. if (min != max)
  1269. valueText += '-' + boost::lexical_cast<std::string>(max);
  1270. }
  1271. name = new CLabel(3, 0, FONT_SMALL, TOPLEFT, Colors::Cornsilk, nameText);
  1272. value = new CLabel(pos.w-3, pos.h-2, FONT_SMALL, BOTTOMRIGHT, Colors::Cornsilk, valueText);
  1273. }
  1274. void LabeledValue::hover(bool on)
  1275. {
  1276. if(on)
  1277. GH.statusbar->print(hoverText);
  1278. else
  1279. {
  1280. GH.statusbar->clear();
  1281. parent->hovered = false;
  1282. }
  1283. }
  1284. CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance *Town, int buildingID, int Level):
  1285. town(Town),
  1286. level(Level),
  1287. availableCount(NULL)
  1288. {
  1289. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1290. pos.x +=posX;
  1291. pos.y +=posY;
  1292. pos.w = 386;
  1293. pos.h = 126;
  1294. if (!town->creatures[level].second.empty())
  1295. addUsedEvents(LCLICK | RCLICK | HOVER);//Activate only if dwelling is present
  1296. icons = new CPicture("ZPCAINFO", 261, 3);
  1297. buildingPic = new CAnimImage(town->town->clientInfo.buildingsIcons, buildingID, 0, 4, 21);
  1298. const CCreature* creature = NULL;
  1299. if (!town->creatures[level].second.empty())
  1300. creature = CGI->creh->creatures[town->creatures[level].second.back()];
  1301. else
  1302. creature = CGI->creh->creatures[town->town->creatures[level][0]];
  1303. hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % creature->namePl);
  1304. creatureAnim = new CCreaturePic(159, 4, creature, false);
  1305. Rect sizes(287, 4, 96, 18);
  1306. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], creature->Attack()));
  1307. sizes.y+=20;
  1308. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[191], CGI->generaltexth->fcommands[1], creature->Defense()));
  1309. sizes.y+=21;
  1310. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[199], CGI->generaltexth->fcommands[2], creature->getMinDamage(), creature->getMaxDamage()));
  1311. sizes.y+=20;
  1312. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[388], CGI->generaltexth->fcommands[3], creature->MaxHealth()));
  1313. sizes.y+=21;
  1314. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[193], CGI->generaltexth->fcommands[4], creature->valOfBonuses(Bonus::STACKS_SPEED)));
  1315. sizes.y+=20;
  1316. values.push_back(new LabeledValue(sizes, CGI->generaltexth->allTexts[194], CGI->generaltexth->fcommands[5], town->creatureGrowth(level)));
  1317. creatureName = new CLabel(78, 11, FONT_SMALL, CENTER, Colors::Cornsilk, creature->namePl);
  1318. dwellingName = new CLabel(78, 101, FONT_SMALL, CENTER, Colors::Cornsilk, town->town->buildings[buildingID]->Name());
  1319. if (vstd::contains(town->builtBuildings, buildingID))
  1320. {
  1321. ui32 available = town->creatures[level].first;
  1322. std::string availableText = CGI->generaltexth->allTexts[217]+ boost::lexical_cast<std::string>(available);
  1323. availableCount = new CLabel(78, 119, FONT_SMALL, CENTER, Colors::Cornsilk, availableText);
  1324. }
  1325. }
  1326. void CFortScreen::RecruitArea::hover(bool on)
  1327. {
  1328. if(on)
  1329. GH.statusbar->print(hoverText);
  1330. else
  1331. GH.statusbar->clear();
  1332. }
  1333. void CFortScreen::RecruitArea::creaturesChanged()
  1334. {
  1335. if (availableCount)
  1336. {
  1337. std::string availableText = CGI->generaltexth->allTexts[217] +
  1338. boost::lexical_cast<std::string>(town->creatures[level].first);
  1339. availableCount->setTxt(availableText);
  1340. }
  1341. }
  1342. void CFortScreen::RecruitArea::clickLeft(tribool down, bool previousState)
  1343. {
  1344. if(!down && previousState)
  1345. LOCPLINT->castleInt->builds->enterDwelling(level);
  1346. }
  1347. void CFortScreen::RecruitArea::clickRight(tribool down, bool previousState)
  1348. {
  1349. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1350. }
  1351. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner):
  1352. CWindowObject(BORDERED, "TPMAGE")
  1353. {
  1354. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1355. window = new CPicture(owner->town->town->clientInfo.guildWindow , 332, 76);
  1356. resdatabar = new CMinorResDataBar;
  1357. resdatabar->pos.x += pos.x;
  1358. resdatabar->pos.y += pos.y;
  1359. Rect barRect(7, 556, 737, 18);
  1360. statusBar = new CGStatusBar(new CPicture(*background, barRect, 7, 556, false));
  1361. exit = new CAdventureMapButton(CGI->generaltexth->allTexts[593],"",boost::bind(&CMageGuildScreen::close,this), 748, 556,"TPMAGE1.DEF",SDLK_RETURN);
  1362. exit->assignedKeys.insert(SDLK_ESCAPE);
  1363. std::vector<std::vector<Point> > positions;
  1364. positions.resize(5);
  1365. positions[0] += Point(222,445), Point(312,445), Point(402,445), Point(520,445), Point(610,445), Point(700,445);
  1366. positions[1] += Point(48,53), Point(48,147), Point(48,241), Point(48,335), Point(48,429);
  1367. positions[2] += Point(570,82), Point(672,82), Point(570,157), Point(672,157);
  1368. positions[3] += Point(183,42), Point(183,148), Point(183,253);
  1369. positions[4] += Point(491,325), Point(591,325);
  1370. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1371. {
  1372. size_t spellCount = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1373. for(size_t j=0; j<spellCount; j++)
  1374. {
  1375. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1376. spells.push_back( new Scroll(positions[i][j], CGI->spellh->spells[owner->town->spells[i][j]]));
  1377. else
  1378. new CAnimImage("TPMAGES.DEF", 1, 0, positions[i][j].x, positions[i][j].y);//closed scroll
  1379. }
  1380. }
  1381. }
  1382. CMageGuildScreen::Scroll::Scroll(Point position, const CSpell *Spell)
  1383. :spell(Spell)
  1384. {
  1385. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1386. addUsedEvents(LCLICK | RCLICK | HOVER);
  1387. pos += position;
  1388. image = new CAnimImage("SPELLSCR", spell->id);
  1389. pos = image->pos;
  1390. }
  1391. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1392. {
  1393. if(down)
  1394. {
  1395. std::vector<CComponent*> comps(1,
  1396. new CComponent(CComponent::spell,spell->id,0)
  1397. );
  1398. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1399. }
  1400. }
  1401. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1402. {
  1403. if(down)
  1404. {
  1405. CInfoPopup *vinya = new CInfoPopup();
  1406. vinya->free = true;
  1407. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1408. (LOCPLINT->playerID,
  1409. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1410. spell->name,30,30);
  1411. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1412. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1413. GH.pushInt(vinya);
  1414. }
  1415. }
  1416. void CMageGuildScreen::Scroll::hover(bool on)
  1417. {
  1418. if(on)
  1419. GH.statusbar->print(spell->name);
  1420. else
  1421. GH.statusbar->clear();
  1422. }
  1423. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid):
  1424. CWindowObject(PLAYER_COLORED, "TPSMITH")
  1425. {
  1426. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1427. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1428. animBG = new CPicture("TPSMITBK", 64, 50);
  1429. animBG->needRefresh = true;
  1430. const CCreature *creature = CGI->creh->creatures[creMachineID];
  1431. anim = new CCreatureAnim(64, 50, creature->animDefName, Rect());
  1432. anim->clipRect(113,125,200,150);
  1433. title = new CLabel(165, 28, FONT_BIG, CENTER, Colors::Jasmine,
  1434. boost::str(boost::format(CGI->generaltexth->allTexts[274]) % creature->nameSing));
  1435. costText = new CLabel(165, 218, FONT_MEDIUM, CENTER, Colors::Cornsilk, CGI->generaltexth->jktexts[43]);
  1436. costValue = new CLabel(165, 290, FONT_MEDIUM, CENTER, Colors::Cornsilk,
  1437. boost::lexical_cast<std::string>(CGI->arth->artifacts[aid]->price));
  1438. std::string text = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % creature->nameSing);
  1439. buy = new CAdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 42, 312,"IBUY30.DEF",SDLK_RETURN);
  1440. text = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % creature->nameSing);
  1441. cancel = new CAdventureMapButton(text,"",boost::bind(&CBlacksmithDialog::close, this), 224, 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1442. if(possible)
  1443. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHero(hid),aid);
  1444. else
  1445. buy->block(true);
  1446. new CAnimImage("RESOURCE", 6, 0, 148, 244);
  1447. }