AdventureMapClasses.cpp 31 KB

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