AdventureMapClasses.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * AdventureMapClasses.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "AdventureMapClasses.h"
  12. #include <SDL.h>
  13. #include "MiscWidgets.h"
  14. #include "CComponent.h"
  15. #include "Images.h"
  16. #include "../CGameInfo.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../mainmenu/CMainMenu.h"
  20. #include "../Graphics.h"
  21. #include "../CMessage.h"
  22. #include "../gui/CGuiHandler.h"
  23. #include "../gui/SDL_Pixels.h"
  24. #include "../gui/SDL_Compat.h"
  25. #include "../windows/InfoWindows.h"
  26. #include "../windows/CAdvmapInterface.h"
  27. #include "../windows/GUIClasses.h"
  28. #include "../battle/CBattleInterfaceClasses.h"
  29. #include "../battle/CBattleInterface.h"
  30. #include "../../CCallback.h"
  31. #include "../../lib/StartInfo.h"
  32. #include "../../lib/CGameState.h"
  33. #include "../../lib/CGeneralTextHandler.h"
  34. #include "../../lib/CHeroHandler.h"
  35. #include "../../lib/CModHandler.h"
  36. #include "../../lib/CTownHandler.h"
  37. #include "../../lib/Terrain.h"
  38. #include "../../lib/filesystem/Filesystem.h"
  39. #include "../../lib/JsonNode.h"
  40. #include "../../lib/mapObjects/CGHeroInstance.h"
  41. #include "../../lib/mapping/CMap.h"
  42. #include "../../lib/NetPacksBase.h"
  43. #include "../../lib/StringConstants.h"
  44. CList::CListItem::CListItem(CList * Parent)
  45. : CIntObject(LCLICK | RCLICK | HOVER),
  46. parent(Parent),
  47. selection()
  48. {
  49. defActions = 255-DISPOSE;
  50. }
  51. CList::CListItem::~CListItem()
  52. {
  53. }
  54. void CList::CListItem::clickRight(tribool down, bool previousState)
  55. {
  56. if (down == true)
  57. showTooltip();
  58. }
  59. void CList::CListItem::clickLeft(tribool down, bool previousState)
  60. {
  61. if(down == true)
  62. {
  63. //second click on already selected item
  64. if(parent->selected == this->shared_from_this())
  65. {
  66. open();
  67. }
  68. else
  69. {
  70. //first click - switch selection
  71. parent->select(this->shared_from_this());
  72. }
  73. }
  74. }
  75. void CList::CListItem::hover(bool on)
  76. {
  77. if (on)
  78. GH.statusbar->setText(getHoverText());
  79. else
  80. GH.statusbar->clear();
  81. }
  82. void CList::CListItem::onSelect(bool on)
  83. {
  84. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  85. selection.reset();
  86. if(on)
  87. selection = genSelection();
  88. select(on);
  89. GH.totalRedraw();
  90. }
  91. CList::CList(int Size, Point position, std::string btnUp, std::string btnDown, size_t listAmount, int helpUp, int helpDown, CListBox::CreateFunc create)
  92. : CIntObject(0, position),
  93. size(Size),
  94. selected(nullptr)
  95. {
  96. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  97. scrollUp = std::make_shared<CButton>(Point(0, 0), btnUp, CGI->generaltexth->zelp[helpUp]);
  98. scrollDown = std::make_shared<CButton>(Point(0, scrollUp->pos.h + 32*(int)size), btnDown, CGI->generaltexth->zelp[helpDown]);
  99. listBox = std::make_shared<CListBox>(create, Point(1,scrollUp->pos.h), Point(0, 32), size, listAmount);
  100. //assign callback only after list was created
  101. scrollUp->addCallback(std::bind(&CListBox::moveToPrev, listBox));
  102. scrollDown->addCallback(std::bind(&CListBox::moveToNext, listBox));
  103. scrollUp->addCallback(std::bind(&CList::update, this));
  104. scrollDown->addCallback(std::bind(&CList::update, this));
  105. update();
  106. }
  107. void CList::update()
  108. {
  109. bool onTop = listBox->getPos() == 0;
  110. bool onBottom = listBox->getPos() + size >= listBox->size();
  111. scrollUp->block(onTop);
  112. scrollDown->block(onBottom);
  113. }
  114. void CList::select(std::shared_ptr<CListItem> which)
  115. {
  116. if(selected == which)
  117. return;
  118. if(selected)
  119. selected->onSelect(false);
  120. selected = which;
  121. if(which)
  122. {
  123. which->onSelect(true);
  124. onSelect();
  125. }
  126. }
  127. int CList::getSelectedIndex()
  128. {
  129. return static_cast<int>(listBox->getIndexOf(selected));
  130. }
  131. void CList::selectIndex(int which)
  132. {
  133. if(which < 0)
  134. {
  135. if(selected)
  136. select(nullptr);
  137. }
  138. else
  139. {
  140. listBox->scrollTo(which);
  141. update();
  142. select(std::dynamic_pointer_cast<CListItem>(listBox->getItem(which)));
  143. }
  144. }
  145. void CList::selectNext()
  146. {
  147. int index = getSelectedIndex() + 1;
  148. if(index >= listBox->size())
  149. index = 0;
  150. selectIndex(index);
  151. }
  152. void CList::selectPrev()
  153. {
  154. int index = getSelectedIndex();
  155. if(index <= 0)
  156. selectIndex(0);
  157. else
  158. selectIndex(index-1);
  159. }
  160. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  161. {
  162. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  163. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  164. portrait = std::make_shared<CPicture>("HPSXXX", movement->pos.w + 1);
  165. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  166. pos.w = mana->pos.w + mana->pos.x - pos.x;
  167. pos.h = std::max(std::max<SDLX_Size>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  168. }
  169. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero)
  170. : CListItem(parent),
  171. hero(Hero)
  172. {
  173. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  174. movement = std::make_shared<CAnimImage>("IMOBIL", 0, 0, 0, 1);
  175. portrait = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  176. mana = std::make_shared<CAnimImage>("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1);
  177. pos.w = mana->pos.w + mana->pos.x - pos.x;
  178. pos.h = std::max(std::max<SDLX_Size>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  179. update();
  180. }
  181. void CHeroList::CHeroItem::update()
  182. {
  183. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  184. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  185. redraw();
  186. }
  187. std::shared_ptr<CIntObject> CHeroList::CHeroItem::genSelection()
  188. {
  189. return std::make_shared<CPicture>("HPSYYY", movement->pos.w + 1);
  190. }
  191. void CHeroList::CHeroItem::select(bool on)
  192. {
  193. if(on && adventureInt->selection != hero)
  194. adventureInt->select(hero);
  195. }
  196. void CHeroList::CHeroItem::open()
  197. {
  198. LOCPLINT->openHeroWindow(hero);
  199. }
  200. void CHeroList::CHeroItem::showTooltip()
  201. {
  202. CRClickPopup::createAndPush(hero, GH.current->motion);
  203. }
  204. std::string CHeroList::CHeroItem::getHoverText()
  205. {
  206. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->name % hero->type->heroClass->name);
  207. }
  208. std::shared_ptr<CIntObject> CHeroList::createHeroItem(size_t index)
  209. {
  210. if (LOCPLINT->wanderingHeroes.size() > index)
  211. return std::make_shared<CHeroItem>(this, LOCPLINT->wanderingHeroes[index]);
  212. return std::make_shared<CEmptyHeroItem>();
  213. }
  214. CHeroList::CHeroList(int size, Point position, std::string btnUp, std::string btnDown):
  215. CList(size, position, btnUp, btnDown, LOCPLINT->wanderingHeroes.size(), 303, 304, std::bind(&CHeroList::createHeroItem, this, _1))
  216. {
  217. }
  218. void CHeroList::select(const CGHeroInstance * hero)
  219. {
  220. selectIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  221. }
  222. void CHeroList::update(const CGHeroInstance * hero)
  223. {
  224. //this hero is already present, update its status
  225. for(auto & elem : listBox->getItems())
  226. {
  227. auto item = std::dynamic_pointer_cast<CHeroItem>(elem);
  228. if(item && item->hero == hero && vstd::contains(LOCPLINT->wanderingHeroes, hero))
  229. {
  230. item->update();
  231. return;
  232. }
  233. }
  234. //simplest solution for now: reset list and restore selection
  235. listBox->resize(LOCPLINT->wanderingHeroes.size());
  236. if (adventureInt->selection)
  237. {
  238. auto selectedHero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  239. if (selectedHero)
  240. select(selectedHero);
  241. }
  242. CList::update();
  243. }
  244. std::shared_ptr<CIntObject> CTownList::createTownItem(size_t index)
  245. {
  246. if (LOCPLINT->towns.size() > index)
  247. return std::make_shared<CTownItem>(this, LOCPLINT->towns[index]);
  248. return std::make_shared<CAnimImage>("ITPA", 0);
  249. }
  250. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  251. CListItem(parent),
  252. town(Town)
  253. {
  254. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  255. picture = std::make_shared<CAnimImage>("ITPA", 0);
  256. pos = picture->pos;
  257. update();
  258. }
  259. std::shared_ptr<CIntObject> CTownList::CTownItem::genSelection()
  260. {
  261. return std::make_shared<CAnimImage>("ITPA", 1);
  262. }
  263. void CTownList::CTownItem::update()
  264. {
  265. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  266. picture->setFrame(iconIndex + 2);
  267. redraw();
  268. }
  269. void CTownList::CTownItem::select(bool on)
  270. {
  271. if (on && adventureInt->selection != town)
  272. adventureInt->select(town);
  273. }
  274. void CTownList::CTownItem::open()
  275. {
  276. LOCPLINT->openTownWindow(town);
  277. }
  278. void CTownList::CTownItem::showTooltip()
  279. {
  280. CRClickPopup::createAndPush(town, GH.current->motion);
  281. }
  282. std::string CTownList::CTownItem::getHoverText()
  283. {
  284. return town->getObjectName();
  285. }
  286. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  287. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, std::bind(&CTownList::createTownItem, this, _1))
  288. {
  289. }
  290. void CTownList::select(const CGTownInstance * town)
  291. {
  292. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  293. }
  294. void CTownList::update(const CGTownInstance *)
  295. {
  296. //simplest solution for now: reset list and restore selection
  297. listBox->resize(LOCPLINT->towns.size());
  298. if (adventureInt->selection)
  299. {
  300. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  301. if (town)
  302. select(town);
  303. }
  304. CList::update();
  305. }
  306. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  307. {
  308. static const SDL_Color fogOfWar = {0, 0, 0, 255};
  309. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  310. // if tile is not visible it will be black on minimap
  311. if(!tile)
  312. return fogOfWar;
  313. // if object at tile is owned - it will be colored as its owner
  314. for (const CGObjectInstance *obj : tile->blockingObjects)
  315. {
  316. //heroes will be blitted later
  317. switch (obj->ID)
  318. {
  319. case Obj::HERO:
  320. case Obj::PRISON:
  321. continue;
  322. }
  323. PlayerColor player = obj->getOwner();
  324. if(player == PlayerColor::NEUTRAL)
  325. return *graphics->neutralColor;
  326. else
  327. if (player < PlayerColor::PLAYER_LIMIT)
  328. return graphics->playerColors[player.getNum()];
  329. }
  330. // else - use terrain color (blocked version or normal)
  331. const auto & colorPair = parent->colors.find(tile->terType->id)->second;
  332. if (tile->blocked && (!tile->visitable))
  333. return colorPair.second;
  334. else
  335. return colorPair.first;
  336. }
  337. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  338. {
  339. int3 mapSizes = LOCPLINT->cb->getMapSize();
  340. double stepX = double(pos.w) / mapSizes.x;
  341. double stepY = double(pos.h) / mapSizes.y;
  342. x = static_cast<int>(toX + stepX * tile.x);
  343. y = static_cast<int>(toY + stepY * tile.y);
  344. }
  345. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  346. {
  347. //coordinates of rectangle on minimap representing this tile
  348. // begin - first to blit, end - first NOT to blit
  349. int xBegin, yBegin, xEnd, yEnd;
  350. tileToPixels (tile, xBegin, yBegin, toX, toY);
  351. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  352. for (int y=yBegin; y<yEnd; y++)
  353. {
  354. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  355. for (int x=xBegin; x<xEnd; x++)
  356. ColorPutter<4, 1>::PutColor(ptr, color);
  357. }
  358. }
  359. void CMinimapInstance::refreshTile(const int3 &tile)
  360. {
  361. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  362. }
  363. void CMinimapInstance::drawScaled(int level)
  364. {
  365. int3 mapSizes = LOCPLINT->cb->getMapSize();
  366. //size of one map tile on our minimap
  367. double stepX = double(pos.w) / mapSizes.x;
  368. double stepY = double(pos.h) / mapSizes.y;
  369. double currY = 0;
  370. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  371. {
  372. double currX = 0;
  373. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  374. {
  375. const SDL_Color &color = getTileColor(int3(x,y,level));
  376. //coordinates of rectangle on minimap representing this tile
  377. // begin - first to blit, end - first NOT to blit
  378. int xBegin = static_cast<int>(currX);
  379. int yBegin = static_cast<int>(currY);
  380. int xEnd = static_cast<int>(currX + stepX);
  381. int yEnd = static_cast<int>(currY + stepY);
  382. for (int y=yBegin; y<yEnd; y++)
  383. {
  384. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  385. for (int x=xBegin; x<xEnd; x++)
  386. ColorPutter<4, 1>::PutColor(ptr, color);
  387. }
  388. }
  389. }
  390. }
  391. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  392. parent(Parent),
  393. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  394. level(Level)
  395. {
  396. pos.w = parent->pos.w;
  397. pos.h = parent->pos.h;
  398. drawScaled(level);
  399. }
  400. CMinimapInstance::~CMinimapInstance()
  401. {
  402. SDL_FreeSurface(minimap);
  403. }
  404. void CMinimapInstance::showAll(SDL_Surface * to)
  405. {
  406. blitAtLoc(minimap, 0, 0, to);
  407. //draw heroes
  408. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  409. for(auto & hero : heroes)
  410. {
  411. int3 position = hero->getPosition(false);
  412. if(position.z == level)
  413. {
  414. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  415. blitTileWithColor(color, position, to, pos.x, pos.y);
  416. }
  417. }
  418. }
  419. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
  420. {
  421. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > ret;
  422. for(const auto & terrain : CGI->terrainTypeHandler->terrains())
  423. {
  424. SDL_Color normal =
  425. {
  426. ui8(terrain.minimapUnblocked[0]),
  427. ui8(terrain.minimapUnblocked[1]),
  428. ui8(terrain.minimapUnblocked[2]),
  429. ui8(255)
  430. };
  431. SDL_Color blocked =
  432. {
  433. ui8(terrain.minimapBlocked[0]),
  434. ui8(terrain.minimapBlocked[1]),
  435. ui8(terrain.minimapBlocked[2]),
  436. ui8(255)
  437. };
  438. ret[terrain.id] = std::make_pair(normal, blocked);
  439. }
  440. return ret;
  441. }
  442. CMinimap::CMinimap(const Rect & position)
  443. : CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  444. level(0),
  445. colors(loadColors())
  446. {
  447. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  448. pos.w = position.w;
  449. pos.h = position.h;
  450. aiShield = std::make_shared<CPicture>("AIShield");
  451. aiShield->disable();
  452. }
  453. int3 CMinimap::translateMousePosition()
  454. {
  455. // 0 = top-left corner, 1 = bottom-right corner
  456. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  457. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  458. int3 mapSizes = LOCPLINT->cb->getMapSize();
  459. int3 tile ((si32)(mapSizes.x * dx), (si32)(mapSizes.y * dy), level);
  460. return tile;
  461. }
  462. void CMinimap::moveAdvMapSelection()
  463. {
  464. int3 newLocation = translateMousePosition();
  465. adventureInt->centerOn(newLocation);
  466. if (!(adventureInt->active & GENERAL))
  467. GH.totalRedraw(); //redraw this as well as inactive adventure map
  468. else
  469. redraw();//redraw only this
  470. }
  471. void CMinimap::clickLeft(tribool down, bool previousState)
  472. {
  473. if(down)
  474. moveAdvMapSelection();
  475. }
  476. void CMinimap::clickRight(tribool down, bool previousState)
  477. {
  478. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  479. }
  480. void CMinimap::hover(bool on)
  481. {
  482. if(on)
  483. GH.statusbar->setText(CGI->generaltexth->zelp[291].first);
  484. else
  485. GH.statusbar->clear();
  486. }
  487. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  488. {
  489. if(mouseState(EIntObjMouseBtnType::LEFT))
  490. moveAdvMapSelection();
  491. }
  492. void CMinimap::showAll(SDL_Surface * to)
  493. {
  494. CIntObject::showAll(to);
  495. if(minimap)
  496. {
  497. int3 mapSizes = LOCPLINT->cb->getMapSize();
  498. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  499. //draw radar
  500. SDL_Rect oldClip;
  501. SDL_Rect radar =
  502. {
  503. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  504. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  505. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  506. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  507. };
  508. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  509. {
  510. // adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
  511. radar.x = std::min<int>(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
  512. radar.y = std::min<int>(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
  513. if(radar.x < pos.x && radar.y < pos.y)
  514. return; // whole map is visible at once, no point in redrawing border
  515. }
  516. SDL_GetClipRect(to, &oldClip);
  517. SDL_SetClipRect(to, &pos);
  518. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  519. SDL_SetClipRect(to, &oldClip);
  520. }
  521. }
  522. void CMinimap::update()
  523. {
  524. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  525. return;
  526. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  527. minimap = std::make_shared<CMinimapInstance>(this, level);
  528. redraw();
  529. }
  530. void CMinimap::setLevel(int newLevel)
  531. {
  532. level = newLevel;
  533. update();
  534. }
  535. void CMinimap::setAIRadar(bool on)
  536. {
  537. if(on)
  538. {
  539. aiShield->enable();
  540. minimap.reset();
  541. }
  542. else
  543. {
  544. aiShield->disable();
  545. update();
  546. }
  547. // this my happen during AI turn when this interface is inactive
  548. // force redraw in order to properly update interface
  549. GH.totalRedraw();
  550. }
  551. void CMinimap::hideTile(const int3 &pos)
  552. {
  553. if(minimap)
  554. minimap->refreshTile(pos);
  555. }
  556. void CMinimap::showTile(const int3 &pos)
  557. {
  558. if(minimap)
  559. minimap->refreshTile(pos);
  560. }
  561. CInfoBar::CVisibleInfo::CVisibleInfo()
  562. : CIntObject(0, Point(8, 12))
  563. {
  564. }
  565. void CInfoBar::CVisibleInfo::show(SDL_Surface * to)
  566. {
  567. CIntObject::show(to);
  568. for(auto object : forceRefresh)
  569. object->showAll(to);
  570. }
  571. CInfoBar::EmptyVisibleInfo::EmptyVisibleInfo()
  572. {
  573. }
  574. CInfoBar::VisibleHeroInfo::VisibleHeroInfo(const CGHeroInstance * hero)
  575. {
  576. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  577. background = std::make_shared<CPicture>("ADSTATHR");
  578. heroTooltip = std::make_shared<CHeroTooltip>(Point(0,0), hero);
  579. }
  580. CInfoBar::VisibleTownInfo::VisibleTownInfo(const CGTownInstance * town)
  581. {
  582. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  583. background = std::make_shared<CPicture>("ADSTATCS");
  584. townTooltip = std::make_shared<CTownTooltip>(Point(0,0), town);
  585. }
  586. CInfoBar::VisibleDateInfo::VisibleDateInfo()
  587. {
  588. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  589. animation = std::make_shared<CShowableAnim>(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  590. std::string labelText;
  591. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) == 1 && LOCPLINT->cb->getDate(Date::DAY) != 1) // monday of any week but first - show new week info
  592. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  593. else
  594. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  595. label = std::make_shared<CLabel>(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText);
  596. forceRefresh.push_back(label);
  597. }
  598. std::string CInfoBar::VisibleDateInfo::getNewDayName()
  599. {
  600. if(LOCPLINT->cb->getDate(Date::DAY) == 1)
  601. return "NEWDAY";
  602. if(LOCPLINT->cb->getDate(Date::DAY) != 1)
  603. return "NEWDAY";
  604. switch(LOCPLINT->cb->getDate(Date::WEEK))
  605. {
  606. case 1:
  607. return "NEWWEEK1";
  608. case 2:
  609. return "NEWWEEK2";
  610. case 3:
  611. return "NEWWEEK3";
  612. case 4:
  613. return "NEWWEEK4";
  614. default:
  615. return "";
  616. }
  617. }
  618. CInfoBar::VisibleEnemyTurnInfo::VisibleEnemyTurnInfo(PlayerColor player)
  619. {
  620. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  621. background = std::make_shared<CPicture>("ADSTATNX");
  622. banner = std::make_shared<CAnimImage>("CREST58", player.getNum(), 0, 20, 51);
  623. sand = std::make_shared<CShowableAnim>(99, 51, "HOURSAND");
  624. glass = std::make_shared<CShowableAnim>(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  625. }
  626. CInfoBar::VisibleGameStatusInfo::VisibleGameStatusInfo()
  627. {
  628. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  629. //get amount of halls of each level
  630. std::vector<int> halls(4, 0);
  631. for(auto town : LOCPLINT->towns)
  632. {
  633. int hallLevel = town->hallLevel();
  634. //negative value means no village hall, unlikely but possible
  635. if(hallLevel >= 0)
  636. halls.at(hallLevel)++;
  637. }
  638. std::vector<PlayerColor> allies, enemies;
  639. //generate list of allies and enemies
  640. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  641. {
  642. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  643. {
  644. if(LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  645. allies.push_back(PlayerColor(i));
  646. else
  647. enemies.push_back(PlayerColor(i));
  648. }
  649. }
  650. //generate widgets
  651. background = std::make_shared<CPicture>("ADSTATIN");
  652. allyLabel = std::make_shared<CLabel>(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  653. enemyLabel = std::make_shared<CLabel>(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  654. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  655. for(PlayerColor & player : allies)
  656. {
  657. auto image = std::make_shared<CAnimImage>("ITGFLAGS", player.getNum(), 0, posx, 102);
  658. posx += image->pos.w;
  659. flags.push_back(image);
  660. }
  661. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  662. for(PlayerColor & player : enemies)
  663. {
  664. auto image = std::make_shared<CAnimImage>("ITGFLAGS", player.getNum(), 0, posx, 132);
  665. posx += image->pos.w;
  666. flags.push_back(image);
  667. }
  668. for(size_t i=0; i<halls.size(); i++)
  669. {
  670. hallIcons.push_back(std::make_shared<CAnimImage>("itmtl", i, 0, 6 + 42 * (int)i , 11));
  671. if(halls[i])
  672. hallLabels.push_back(std::make_shared<CLabel>( 26 + 42 * (int)i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i])));
  673. }
  674. }
  675. CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const Component & compToDisplay, std::string message)
  676. {
  677. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  678. background = std::make_shared<CPicture>("ADSTATOT", 1);
  679. comp = std::make_shared<CComponent>(compToDisplay);
  680. comp->moveTo(Point(pos.x+47, pos.y+50));
  681. text = std::make_shared<CTextBox>(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  682. }
  683. void CInfoBar::playNewDaySound()
  684. {
  685. if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  686. CCS->soundh->playSound(soundBase::newDay);
  687. else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  688. CCS->soundh->playSound(soundBase::newWeek);
  689. else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  690. CCS->soundh->playSound(soundBase::newMonth);
  691. else
  692. CCS->soundh->playSound(soundBase::newDay);
  693. }
  694. void CInfoBar::reset()
  695. {
  696. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  697. state = EMPTY;
  698. visibleInfo = std::make_shared<EmptyVisibleInfo>();
  699. }
  700. void CInfoBar::showSelection()
  701. {
  702. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  703. if(adventureInt->selection)
  704. {
  705. if(auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection))
  706. {
  707. showHeroSelection(hero);
  708. return;
  709. }
  710. else if(auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection))
  711. {
  712. showTownSelection(town);
  713. return;
  714. }
  715. }
  716. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  717. }
  718. void CInfoBar::tick()
  719. {
  720. removeUsedEvents(TIME);
  721. if(GH.topInt() == adventureInt)
  722. showSelection();
  723. }
  724. void CInfoBar::clickLeft(tribool down, bool previousState)
  725. {
  726. if(down)
  727. {
  728. if(state == HERO || state == TOWN)
  729. showGameStatus();
  730. else if(state == GAME)
  731. showDate();
  732. else
  733. showSelection();
  734. }
  735. }
  736. void CInfoBar::clickRight(tribool down, bool previousState)
  737. {
  738. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  739. }
  740. void CInfoBar::hover(bool on)
  741. {
  742. if(on)
  743. GH.statusbar->setText(CGI->generaltexth->zelp[292].first);
  744. else
  745. GH.statusbar->clear();
  746. }
  747. CInfoBar::CInfoBar(const Rect & position)
  748. : CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  749. state(EMPTY)
  750. {
  751. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  752. pos.w = position.w;
  753. pos.h = position.h;
  754. reset();
  755. }
  756. void CInfoBar::showDate()
  757. {
  758. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  759. playNewDaySound();
  760. state = DATE;
  761. visibleInfo = std::make_shared<VisibleDateInfo>();
  762. setTimer(3000);
  763. redraw();
  764. }
  765. void CInfoBar::showComponent(const Component & comp, std::string message)
  766. {
  767. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  768. state = COMPONENT;
  769. visibleInfo = std::make_shared<VisibleComponentInfo>(comp, message);
  770. setTimer(3000);
  771. redraw();
  772. }
  773. void CInfoBar::startEnemyTurn(PlayerColor color)
  774. {
  775. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  776. state = AITURN;
  777. visibleInfo = std::make_shared<VisibleEnemyTurnInfo>(color);
  778. redraw();
  779. }
  780. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  781. {
  782. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  783. if(!hero)
  784. {
  785. reset();
  786. }
  787. else
  788. {
  789. state = HERO;
  790. visibleInfo = std::make_shared<VisibleHeroInfo>(hero);
  791. }
  792. redraw();
  793. }
  794. void CInfoBar::showTownSelection(const CGTownInstance * town)
  795. {
  796. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  797. if(!town)
  798. {
  799. reset();
  800. }
  801. else
  802. {
  803. state = TOWN;
  804. visibleInfo = std::make_shared<VisibleTownInfo>(town);
  805. }
  806. redraw();
  807. }
  808. void CInfoBar::showGameStatus()
  809. {
  810. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  811. state = GAME;
  812. visibleInfo = std::make_shared<VisibleGameStatusInfo>();
  813. setTimer(3000);
  814. redraw();
  815. }
  816. CInGameConsole::CInGameConsole()
  817. : CIntObject(KEYBOARD | TEXTINPUT),
  818. prevEntDisp(-1),
  819. defaultTimeout(10000),
  820. maxDisplayedTexts(10)
  821. {
  822. }
  823. void CInGameConsole::show(SDL_Surface * to)
  824. {
  825. int number = 0;
  826. std::vector<std::list< std::pair< std::string, Uint32 > >::iterator> toDel;
  827. boost::unique_lock<boost::mutex> lock(texts_mx);
  828. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  829. {
  830. Point leftBottomCorner(0, screen->h);
  831. if(LOCPLINT->battleInt)
  832. {
  833. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  834. }
  835. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  836. Point(leftBottomCorner.x + 50, leftBottomCorner.y - (int)texts.size() * 20 - 80 + number*20));
  837. if((int)(SDL_GetTicks() - it->second) > defaultTimeout)
  838. {
  839. toDel.push_back(it);
  840. }
  841. }
  842. for(auto & elem : toDel)
  843. {
  844. texts.erase(elem);
  845. }
  846. }
  847. void CInGameConsole::print(const std::string &txt)
  848. {
  849. boost::unique_lock<boost::mutex> lock(texts_mx);
  850. int lineLen = conf.go()->ac.outputLineLength;
  851. if(txt.size() < lineLen)
  852. {
  853. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  854. if(texts.size() > maxDisplayedTexts)
  855. {
  856. texts.pop_front();
  857. }
  858. }
  859. else
  860. {
  861. assert(lineLen);
  862. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  863. {
  864. std::string part = txt.substr(g * lineLen, lineLen);
  865. if(part.size() == 0)
  866. break;
  867. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  868. if(texts.size() > maxDisplayedTexts)
  869. {
  870. texts.pop_front();
  871. }
  872. }
  873. }
  874. }
  875. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  876. {
  877. if(key.type != SDL_KEYDOWN) return;
  878. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  879. switch(key.keysym.sym)
  880. {
  881. case SDLK_TAB:
  882. case SDLK_ESCAPE:
  883. {
  884. if(captureAllKeys)
  885. {
  886. endEnteringText(false);
  887. }
  888. else if(SDLK_TAB == key.keysym.sym)
  889. {
  890. startEnteringText();
  891. }
  892. break;
  893. }
  894. case SDLK_RETURN: //enter key
  895. {
  896. if(enteredText.size() > 0 && captureAllKeys)
  897. {
  898. endEnteringText(true);
  899. CCS->soundh->playSound("CHAT");
  900. }
  901. break;
  902. }
  903. case SDLK_BACKSPACE:
  904. {
  905. if(enteredText.size() > 1)
  906. {
  907. Unicode::trimRight(enteredText,2);
  908. enteredText += '_';
  909. refreshEnteredText();
  910. }
  911. break;
  912. }
  913. case SDLK_UP: //up arrow
  914. {
  915. if(previouslyEntered.size() == 0)
  916. break;
  917. if(prevEntDisp == -1)
  918. {
  919. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  920. enteredText = previouslyEntered[prevEntDisp] + "_";
  921. refreshEnteredText();
  922. }
  923. else if( prevEntDisp > 0)
  924. {
  925. --prevEntDisp;
  926. enteredText = previouslyEntered[prevEntDisp] + "_";
  927. refreshEnteredText();
  928. }
  929. break;
  930. }
  931. case SDLK_DOWN: //down arrow
  932. {
  933. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  934. {
  935. ++prevEntDisp;
  936. enteredText = previouslyEntered[prevEntDisp] + "_";
  937. refreshEnteredText();
  938. }
  939. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  940. {
  941. prevEntDisp = -1;
  942. enteredText = "_";
  943. refreshEnteredText();
  944. }
  945. break;
  946. }
  947. default:
  948. {
  949. break;
  950. }
  951. }
  952. }
  953. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  954. {
  955. if(!captureAllKeys || enteredText.size() == 0)
  956. return;
  957. enteredText.resize(enteredText.size()-1);
  958. enteredText += event.text;
  959. enteredText += "_";
  960. refreshEnteredText();
  961. }
  962. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  963. {
  964. //do nothing here
  965. }
  966. void CInGameConsole::startEnteringText()
  967. {
  968. captureAllKeys = true;
  969. CSDL_Ext::startTextInput(&GH.statusbar->pos);
  970. enteredText = "_";
  971. if(GH.topInt() == adventureInt)
  972. {
  973. GH.statusbar->alignment = TOPLEFT;
  974. GH.statusbar->setText(enteredText);
  975. //Prevent changes to the text from mouse interaction with the adventure map
  976. GH.statusbar->lock(true);
  977. }
  978. else if(LOCPLINT->battleInt)
  979. {
  980. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  981. }
  982. }
  983. void CInGameConsole::endEnteringText(bool printEnteredText)
  984. {
  985. captureAllKeys = false;
  986. CSDL_Ext::stopTextInput();
  987. prevEntDisp = -1;
  988. if(printEnteredText)
  989. {
  990. std::string txt = enteredText.substr(0, enteredText.size()-1);
  991. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  992. previouslyEntered.push_back(txt);
  993. //print(txt);
  994. }
  995. enteredText.clear();
  996. if(GH.topInt() == adventureInt)
  997. {
  998. GH.statusbar->alignment = CENTER;
  999. GH.statusbar->lock(false);
  1000. GH.statusbar->clear();
  1001. }
  1002. else if(LOCPLINT->battleInt)
  1003. {
  1004. LOCPLINT->battleInt->console->ingcAlter = "";
  1005. }
  1006. }
  1007. void CInGameConsole::refreshEnteredText()
  1008. {
  1009. if(GH.topInt() == adventureInt)
  1010. {
  1011. GH.statusbar->lock(false);
  1012. GH.statusbar->clear();
  1013. GH.statusbar->setText(enteredText);
  1014. GH.statusbar->lock(true);
  1015. }
  1016. else if(LOCPLINT->battleInt)
  1017. {
  1018. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1019. }
  1020. }
  1021. CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position)
  1022. : CIntObject(),
  1023. background(bg)
  1024. {
  1025. defActions = 255;
  1026. recActions = 255;
  1027. pos.x += position.x;
  1028. pos.y += position.y;
  1029. if (bg)
  1030. {
  1031. pos.w = bg->w;
  1032. pos.h = bg->h;
  1033. }
  1034. }
  1035. CAdvMapPanel::~CAdvMapPanel()
  1036. {
  1037. if (background)
  1038. SDL_FreeSurface(background);
  1039. }
  1040. void CAdvMapPanel::addChildColorableButton(std::shared_ptr<CButton> button)
  1041. {
  1042. colorableButtons.push_back(button);
  1043. addChildToPanel(button, ACTIVATE | DEACTIVATE);
  1044. }
  1045. void CAdvMapPanel::setPlayerColor(const PlayerColor & clr)
  1046. {
  1047. for(auto & button : colorableButtons)
  1048. {
  1049. button->setPlayerColor(clr);
  1050. }
  1051. }
  1052. void CAdvMapPanel::showAll(SDL_Surface * to)
  1053. {
  1054. if(background)
  1055. blitAt(background, pos.x, pos.y, to);
  1056. CIntObject::showAll(to);
  1057. }
  1058. void CAdvMapPanel::addChildToPanel(std::shared_ptr<CIntObject> obj, ui8 actions)
  1059. {
  1060. otherObjects.push_back(obj);
  1061. obj->recActions |= actions | SHOWALL;
  1062. obj->recActions &= ~DISPOSE;
  1063. addChild(obj.get(), false);
  1064. }
  1065. CAdvMapWorldViewPanel::CAdvMapWorldViewPanel(std::shared_ptr<CAnimation> _icons, SDL_Surface * bg, Point position, int spaceBottom, const PlayerColor &color)
  1066. : CAdvMapPanel(bg, position), icons(_icons)
  1067. {
  1068. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1069. int fillerHeight = bg ? spaceBottom - pos.y - pos.h : 0;
  1070. if(fillerHeight > 0)
  1071. {
  1072. backgroundFiller = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, pos.h, pos.w, fillerHeight));
  1073. }
  1074. }
  1075. CAdvMapWorldViewPanel::~CAdvMapWorldViewPanel() = default;
  1076. void CAdvMapWorldViewPanel::recolorIcons(const PlayerColor & color, int indexOffset)
  1077. {
  1078. assert(iconsData.size() == currentIcons.size());
  1079. for(size_t idx = 0; idx < iconsData.size(); idx++)
  1080. {
  1081. const auto & data = iconsData.at(idx);
  1082. currentIcons[idx]->setFrame(data.first + indexOffset);
  1083. }
  1084. }
  1085. void CAdvMapWorldViewPanel::addChildIcon(std::pair<int, Point> data, int indexOffset)
  1086. {
  1087. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1088. iconsData.push_back(data);
  1089. currentIcons.push_back(std::make_shared<CAnimImage>(icons, data.first + indexOffset, 0, data.second.x, data.second.y));
  1090. }