AdventureMapClasses.cpp 31 KB

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