AdventureMapClasses.cpp 31 KB

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