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/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. captureAllKeys = false;
  887. endEnteringText(false);
  888. }
  889. else if(SDLK_TAB == key.keysym.sym)
  890. {
  891. captureAllKeys = true;
  892. startEnteringText();
  893. }
  894. break;
  895. }
  896. case SDLK_RETURN: //enter key
  897. {
  898. if(enteredText.size() > 0 && captureAllKeys)
  899. {
  900. captureAllKeys = false;
  901. endEnteringText(true);
  902. CCS->soundh->playSound("CHAT");
  903. }
  904. break;
  905. }
  906. case SDLK_BACKSPACE:
  907. {
  908. if(enteredText.size() > 1)
  909. {
  910. Unicode::trimRight(enteredText,2);
  911. enteredText += '_';
  912. refreshEnteredText();
  913. }
  914. break;
  915. }
  916. case SDLK_UP: //up arrow
  917. {
  918. if(previouslyEntered.size() == 0)
  919. break;
  920. if(prevEntDisp == -1)
  921. {
  922. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  923. enteredText = previouslyEntered[prevEntDisp] + "_";
  924. refreshEnteredText();
  925. }
  926. else if( prevEntDisp > 0)
  927. {
  928. --prevEntDisp;
  929. enteredText = previouslyEntered[prevEntDisp] + "_";
  930. refreshEnteredText();
  931. }
  932. break;
  933. }
  934. case SDLK_DOWN: //down arrow
  935. {
  936. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  937. {
  938. ++prevEntDisp;
  939. enteredText = previouslyEntered[prevEntDisp] + "_";
  940. refreshEnteredText();
  941. }
  942. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  943. {
  944. prevEntDisp = -1;
  945. enteredText = "_";
  946. refreshEnteredText();
  947. }
  948. break;
  949. }
  950. default:
  951. {
  952. break;
  953. }
  954. }
  955. }
  956. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  957. {
  958. if(!captureAllKeys || enteredText.size() == 0)
  959. return;
  960. enteredText.resize(enteredText.size()-1);
  961. enteredText += event.text;
  962. enteredText += "_";
  963. refreshEnteredText();
  964. }
  965. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  966. {
  967. //do nothing here
  968. }
  969. void CInGameConsole::startEnteringText()
  970. {
  971. CSDL_Ext::startTextInput(&pos);
  972. enteredText = "_";
  973. if(GH.topInt() == adventureInt)
  974. {
  975. GH.statusbar->alignment = TOPLEFT;
  976. GH.statusbar->setText(enteredText);
  977. //Prevent changes to the text from mouse interaction with the adventure map
  978. GH.statusbar->lock(true);
  979. }
  980. else if(LOCPLINT->battleInt)
  981. {
  982. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  983. }
  984. }
  985. void CInGameConsole::endEnteringText(bool printEnteredText)
  986. {
  987. CSDL_Ext::stopTextInput();
  988. prevEntDisp = -1;
  989. if(printEnteredText)
  990. {
  991. std::string txt = enteredText.substr(0, enteredText.size()-1);
  992. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  993. previouslyEntered.push_back(txt);
  994. //print(txt);
  995. }
  996. enteredText = "";
  997. if(GH.topInt() == adventureInt)
  998. {
  999. GH.statusbar->alignment = CENTER;
  1000. GH.statusbar->lock(false);
  1001. GH.statusbar->clear();
  1002. }
  1003. else if(LOCPLINT->battleInt)
  1004. {
  1005. LOCPLINT->battleInt->console->ingcAlter = "";
  1006. }
  1007. }
  1008. void CInGameConsole::refreshEnteredText()
  1009. {
  1010. if(GH.topInt() == adventureInt)
  1011. {
  1012. GH.statusbar->lock(false);
  1013. GH.statusbar->clear();
  1014. GH.statusbar->setText(enteredText);
  1015. GH.statusbar->lock(true);
  1016. }
  1017. else if(LOCPLINT->battleInt)
  1018. {
  1019. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1020. }
  1021. }
  1022. CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position)
  1023. : CIntObject(),
  1024. background(bg)
  1025. {
  1026. defActions = 255;
  1027. recActions = 255;
  1028. pos.x += position.x;
  1029. pos.y += position.y;
  1030. if (bg)
  1031. {
  1032. pos.w = bg->w;
  1033. pos.h = bg->h;
  1034. }
  1035. }
  1036. CAdvMapPanel::~CAdvMapPanel()
  1037. {
  1038. if (background)
  1039. SDL_FreeSurface(background);
  1040. }
  1041. void CAdvMapPanel::addChildColorableButton(std::shared_ptr<CButton> button)
  1042. {
  1043. colorableButtons.push_back(button);
  1044. addChildToPanel(button, ACTIVATE | DEACTIVATE);
  1045. }
  1046. void CAdvMapPanel::setPlayerColor(const PlayerColor & clr)
  1047. {
  1048. for(auto & button : colorableButtons)
  1049. {
  1050. button->setPlayerColor(clr);
  1051. }
  1052. }
  1053. void CAdvMapPanel::showAll(SDL_Surface * to)
  1054. {
  1055. if(background)
  1056. blitAt(background, pos.x, pos.y, to);
  1057. CIntObject::showAll(to);
  1058. }
  1059. void CAdvMapPanel::addChildToPanel(std::shared_ptr<CIntObject> obj, ui8 actions)
  1060. {
  1061. otherObjects.push_back(obj);
  1062. obj->recActions |= actions | SHOWALL;
  1063. obj->recActions &= ~DISPOSE;
  1064. addChild(obj.get(), false);
  1065. }
  1066. CAdvMapWorldViewPanel::CAdvMapWorldViewPanel(std::shared_ptr<CAnimation> _icons, SDL_Surface * bg, Point position, int spaceBottom, const PlayerColor &color)
  1067. : CAdvMapPanel(bg, position), icons(_icons)
  1068. {
  1069. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1070. int fillerHeight = bg ? spaceBottom - pos.y - pos.h : 0;
  1071. if(fillerHeight > 0)
  1072. {
  1073. backgroundFiller = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, pos.h, pos.w, fillerHeight));
  1074. }
  1075. }
  1076. CAdvMapWorldViewPanel::~CAdvMapWorldViewPanel() = default;
  1077. void CAdvMapWorldViewPanel::recolorIcons(const PlayerColor & color, int indexOffset)
  1078. {
  1079. assert(iconsData.size() == currentIcons.size());
  1080. for(size_t idx = 0; idx < iconsData.size(); idx++)
  1081. {
  1082. const auto & data = iconsData.at(idx);
  1083. currentIcons[idx]->setFrame(data.first + indexOffset);
  1084. }
  1085. }
  1086. void CAdvMapWorldViewPanel::addChildIcon(std::pair<int, Point> data, int indexOffset)
  1087. {
  1088. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1089. iconsData.push_back(data);
  1090. currentIcons.push_back(std::make_shared<CAnimImage>(icons, data.first + indexOffset, 0, data.second.x, data.second.y));
  1091. }