AdventureMapClasses.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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(nullptr)
  46. {
  47. }
  48. CList::CListItem::~CListItem()
  49. {
  50. // select() method in this was already destroyed so we can't safely call method in parent
  51. if (parent->selected == this)
  52. parent->selected = nullptr;
  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)
  65. open();
  66. else
  67. {
  68. //first click - switch selection
  69. parent->select(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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  83. vstd::clear_pointer(selection);
  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,
  90. int helpUp, int helpDown, CListBox::CreateFunc create, CListBox::DestroyFunc destroy):
  91. CIntObject(0, position),
  92. size(Size),
  93. selected(nullptr)
  94. {
  95. OBJ_CONSTRUCTION_CAPTURING_ALL;
  96. scrollUp = new CButton(Point(0, 0), btnUp, CGI->generaltexth->zelp[helpUp]);
  97. list = new CListBox(create, destroy, 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, list));
  100. scrollDown = new CButton(Point(0, scrollUp->pos.h + 32*size), btnDown, CGI->generaltexth->zelp[helpDown], std::bind(&CListBox::moveToNext, list));
  101. scrollDown->addCallback(std::bind(&CList::update, this));
  102. scrollUp->addCallback(std::bind(&CList::update, this));
  103. update();
  104. }
  105. void CList::update()
  106. {
  107. bool onTop = list->getPos() == 0;
  108. bool onBottom = list->getPos() + size >= list->size();
  109. scrollUp->block(onTop);
  110. scrollDown->block(onBottom);
  111. }
  112. void CList::select(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 list->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. list->scrollTo(which);
  139. update();
  140. select(dynamic_cast<CListItem*>(list->getItem(which)));
  141. }
  142. }
  143. void CList::selectNext()
  144. {
  145. int index = getSelectedIndex() + 1;
  146. if (index >= list->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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  161. auto move = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  162. auto img = new CPicture("HPSXXX", move->pos.w + 1);
  163. auto mana = new CAnimImage("IMANA", 0, 0, move->pos.w + img->pos.w + 2, 1 );
  164. pos.w = mana->pos.w + mana->pos.x - pos.x;
  165. pos.h = std::max(std::max<SDLX_Size>(move->pos.h + 1, mana->pos.h + 1), img->pos.h);
  166. }
  167. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero):
  168. CListItem(parent),
  169. hero(Hero)
  170. {
  171. OBJ_CONSTRUCTION_CAPTURING_ALL;
  172. movement = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  173. portrait = new CAnimImage("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  174. mana = new 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. CIntObject * CHeroList::CHeroItem::genSelection()
  186. {
  187. return new 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. CIntObject * CHeroList::createHeroItem(size_t index)
  207. {
  208. if (LOCPLINT->wanderingHeroes.size() > index)
  209. return new CHeroItem(this, LOCPLINT->wanderingHeroes[index]);
  210. return new 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 : list->getItems())
  224. {
  225. auto item = dynamic_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. list->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. CIntObject * CTownList::createTownItem(size_t index)
  243. {
  244. if (LOCPLINT->towns.size() > index)
  245. return new CTownItem(this, LOCPLINT->towns[index]);
  246. return new CAnimImage("ITPA", 0);
  247. }
  248. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  249. CListItem(parent),
  250. town(Town)
  251. {
  252. OBJ_CONSTRUCTION_CAPTURING_ALL;
  253. picture = new CAnimImage("ITPA", 0);
  254. pos = picture->pos;
  255. update();
  256. }
  257. CIntObject * CTownList::CTownItem::genSelection()
  258. {
  259. return new 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. list->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. aiShield(nullptr),
  452. minimap(nullptr),
  453. level(0),
  454. colors(loadColors("config/terrains.json"))
  455. {
  456. pos.w = position.w;
  457. pos.h = position.h;
  458. }
  459. int3 CMinimap::translateMousePosition()
  460. {
  461. // 0 = top-left corner, 1 = bottom-right corner
  462. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  463. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  464. int3 mapSizes = LOCPLINT->cb->getMapSize();
  465. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  466. return tile;
  467. }
  468. void CMinimap::moveAdvMapSelection()
  469. {
  470. int3 newLocation = translateMousePosition();
  471. adventureInt->centerOn(newLocation);
  472. if (!(adventureInt->active & GENERAL))
  473. GH.totalRedraw(); //redraw this as well as inactive adventure map
  474. else
  475. redraw();//redraw only this
  476. }
  477. void CMinimap::clickLeft(tribool down, bool previousState)
  478. {
  479. if (down)
  480. moveAdvMapSelection();
  481. }
  482. void CMinimap::clickRight(tribool down, bool previousState)
  483. {
  484. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  485. }
  486. void CMinimap::hover(bool on)
  487. {
  488. if (on)
  489. GH.statusbar->setText(CGI->generaltexth->zelp[291].first);
  490. else
  491. GH.statusbar->clear();
  492. }
  493. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  494. {
  495. if(mouseState(EIntObjMouseBtnType::LEFT))
  496. moveAdvMapSelection();
  497. }
  498. void CMinimap::showAll(SDL_Surface * to)
  499. {
  500. CIntObject::showAll(to);
  501. if (minimap)
  502. {
  503. int3 mapSizes = LOCPLINT->cb->getMapSize();
  504. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  505. //draw radar
  506. SDL_Rect oldClip;
  507. SDL_Rect radar =
  508. {
  509. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  510. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  511. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  512. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  513. };
  514. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  515. {
  516. // adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
  517. radar.x = std::min<int>(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
  518. radar.y = std::min<int>(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
  519. if (radar.x < pos.x && radar.y < pos.y)
  520. return; // whole map is visible at once, no point in redrawing border
  521. }
  522. SDL_GetClipRect(to, &oldClip);
  523. SDL_SetClipRect(to, &pos);
  524. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  525. SDL_SetClipRect(to, &oldClip);
  526. }
  527. }
  528. void CMinimap::update()
  529. {
  530. if (aiShield) //AI turn is going on. There is no need to update minimap
  531. return;
  532. OBJ_CONSTRUCTION_CAPTURING_ALL;
  533. vstd::clear_pointer(minimap);
  534. minimap = new 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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  547. vstd::clear_pointer(minimap);
  548. if (!aiShield)
  549. aiShield = new CPicture("AIShield");
  550. }
  551. else
  552. {
  553. vstd::clear_pointer(aiShield);
  554. update();
  555. }
  556. // this my happen during AI turn when this interface is inactive
  557. // force redraw in order to properly update interface
  558. GH.totalRedraw();
  559. }
  560. void CMinimap::hideTile(const int3 &pos)
  561. {
  562. if (minimap)
  563. minimap->refreshTile(pos);
  564. }
  565. void CMinimap::showTile(const int3 &pos)
  566. {
  567. if (minimap)
  568. minimap->refreshTile(pos);
  569. }
  570. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  571. CIntObject(0, position),
  572. aiProgress(nullptr)
  573. {
  574. }
  575. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  576. {
  577. CIntObject::show(to);
  578. for(auto object : forceRefresh)
  579. object->showAll(to);
  580. }
  581. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  582. {
  583. assert(children.empty()); // visible info should be re-created to change type
  584. OBJ_CONSTRUCTION_CAPTURING_ALL;
  585. new CPicture("ADSTATHR");
  586. new CHeroTooltip(Point(0,0), hero);
  587. }
  588. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  589. {
  590. assert(children.empty()); // visible info should be re-created to change type
  591. OBJ_CONSTRUCTION_CAPTURING_ALL;
  592. new CPicture("ADSTATCS");
  593. new CTownTooltip(Point(0,0), town);
  594. }
  595. void CInfoBar::CVisibleInfo::playNewDaySound()
  596. {
  597. if (LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  598. CCS->soundh->playSound(soundBase::newDay);
  599. else
  600. if (LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  601. CCS->soundh->playSound(soundBase::newWeek);
  602. else
  603. if (LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  604. CCS->soundh->playSound(soundBase::newMonth);
  605. else
  606. CCS->soundh->playSound(soundBase::newDay);
  607. }
  608. std::string CInfoBar::CVisibleInfo::getNewDayName()
  609. {
  610. if (LOCPLINT->cb->getDate(Date::DAY) == 1)
  611. return "NEWDAY";
  612. if (LOCPLINT->cb->getDate(Date::DAY) != 1)
  613. return "NEWDAY";
  614. switch(LOCPLINT->cb->getDate(Date::WEEK))
  615. {
  616. case 1: return "NEWWEEK1";
  617. case 2: return "NEWWEEK2";
  618. case 3: return "NEWWEEK3";
  619. case 4: return "NEWWEEK4";
  620. default: assert(0); return "";
  621. }
  622. }
  623. void CInfoBar::CVisibleInfo::loadDay()
  624. {
  625. assert(children.empty()); // visible info should be re-created first to change type
  626. playNewDaySound();
  627. OBJ_CONSTRUCTION_CAPTURING_ALL;
  628. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  629. std::string labelText;
  630. 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
  631. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  632. else
  633. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  634. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText));
  635. }
  636. void CInfoBar::CVisibleInfo::loadEnemyTurn(PlayerColor player)
  637. {
  638. assert(children.empty()); // visible info should be re-created to change type
  639. OBJ_CONSTRUCTION_CAPTURING_ALL;
  640. new CPicture("ADSTATNX");
  641. new CAnimImage("CREST58", player.getNum(), 0, 20, 51);
  642. new CShowableAnim(99, 51, "HOURSAND");
  643. // FIXME: currently there is no way to get progress from VCAI
  644. // if this will change at some point switch this ifdef to enable correct code
  645. #if 0
  646. //prepare hourglass for updating AI turn
  647. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  648. forceRefresh.push_back(aiProgress);
  649. #else
  650. //create hourglass that will be always animated ignoring AI status
  651. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  652. #endif
  653. }
  654. void CInfoBar::CVisibleInfo::loadGameStatus()
  655. {
  656. assert(children.empty()); // visible info should be re-created to change type
  657. //get amount of halls of each level
  658. std::vector<int> halls(4, 0);
  659. for(auto town : LOCPLINT->towns)
  660. {
  661. int hallLevel = town->hallLevel();
  662. //negative value means no village hall, unlikely but possible
  663. if(hallLevel >= 0)
  664. halls.at(hallLevel)++;
  665. }
  666. std::vector<PlayerColor> allies, enemies;
  667. //generate list of allies and enemies
  668. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  669. {
  670. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  671. {
  672. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  673. allies.push_back(PlayerColor(i));
  674. else
  675. enemies.push_back(PlayerColor(i));
  676. }
  677. }
  678. //generate component
  679. OBJ_CONSTRUCTION_CAPTURING_ALL;
  680. new CPicture("ADSTATIN");
  681. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  682. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  683. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  684. for(PlayerColor & player : allies)
  685. {
  686. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 102);
  687. posx += image->pos.w;
  688. }
  689. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  690. for(PlayerColor & player : enemies)
  691. {
  692. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 132);
  693. posx += image->pos.w;
  694. }
  695. for (size_t i=0; i<halls.size(); i++)
  696. {
  697. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  698. if (halls[i])
  699. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i]));
  700. }
  701. }
  702. void CInfoBar::CVisibleInfo::loadComponent(const Component & compToDisplay, std::string message)
  703. {
  704. assert(children.empty()); // visible info should be re-created to change type
  705. OBJ_CONSTRUCTION_CAPTURING_ALL;
  706. new CPicture("ADSTATOT", 1);
  707. auto comp = new CComponent(compToDisplay);
  708. comp->moveTo(Point(pos.x+47, pos.y+50));
  709. new CTextBox(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  710. }
  711. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  712. {
  713. if (aiProgress)
  714. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  715. }
  716. void CInfoBar::reset(EState newState = EMPTY)
  717. {
  718. OBJ_CONSTRUCTION_CAPTURING_ALL;
  719. vstd::clear_pointer(visibleInfo);
  720. currentObject = nullptr;
  721. state = newState;
  722. visibleInfo = new CVisibleInfo(Point(8, 12));
  723. }
  724. void CInfoBar::showSelection()
  725. {
  726. if (adventureInt->selection)
  727. {
  728. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  729. if (hero)
  730. {
  731. showHeroSelection(hero);
  732. return;
  733. }
  734. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  735. if (town)
  736. {
  737. showTownSelection(town);
  738. return;
  739. }
  740. }
  741. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  742. }
  743. void CInfoBar::tick()
  744. {
  745. removeUsedEvents(TIME);
  746. if(GH.topInt() == adventureInt)
  747. showSelection();
  748. }
  749. void CInfoBar::clickLeft(tribool down, bool previousState)
  750. {
  751. if (down)
  752. {
  753. if (state == HERO || state == TOWN)
  754. showGameStatus();
  755. else if (state == GAME)
  756. showDate();
  757. else
  758. showSelection();
  759. }
  760. }
  761. void CInfoBar::clickRight(tribool down, bool previousState)
  762. {
  763. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  764. }
  765. void CInfoBar::hover(bool on)
  766. {
  767. if (on)
  768. GH.statusbar->setText(CGI->generaltexth->zelp[292].first);
  769. else
  770. GH.statusbar->clear();
  771. }
  772. CInfoBar::CInfoBar(const Rect &position):
  773. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  774. visibleInfo(nullptr),
  775. state(EMPTY),
  776. currentObject(nullptr)
  777. {
  778. pos.w = position.w;
  779. pos.h = position.h;
  780. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  781. }
  782. void CInfoBar::showDate()
  783. {
  784. reset(DATE);
  785. visibleInfo->loadDay();
  786. setTimer(3000);
  787. redraw();
  788. }
  789. void CInfoBar::showComponent(const Component & comp, std::string message)
  790. {
  791. reset(COMPONENT);
  792. visibleInfo->loadComponent(comp, message);
  793. setTimer(3000);
  794. redraw();
  795. }
  796. void CInfoBar::startEnemyTurn(PlayerColor color)
  797. {
  798. reset(AITURN);
  799. visibleInfo->loadEnemyTurn(color);
  800. redraw();
  801. }
  802. void CInfoBar::updateEnemyTurn(double progress)
  803. {
  804. assert(state == AITURN);
  805. visibleInfo->updateEnemyTurn(progress);
  806. redraw();
  807. }
  808. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  809. {
  810. if (!hero)
  811. return;
  812. reset(HERO);
  813. currentObject = hero;
  814. visibleInfo->loadHero(hero);
  815. redraw();
  816. }
  817. void CInfoBar::showTownSelection(const CGTownInstance * town)
  818. {
  819. if (!town)
  820. return;
  821. reset(TOWN);
  822. currentObject = town;
  823. visibleInfo->loadTown(town);
  824. redraw();
  825. }
  826. void CInfoBar::showGameStatus()
  827. {
  828. reset(GAME);
  829. visibleInfo->loadGameStatus();
  830. setTimer(3000);
  831. redraw();
  832. }
  833. void CInGameConsole::show(SDL_Surface * to)
  834. {
  835. int number = 0;
  836. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  837. boost::unique_lock<boost::mutex> lock(texts_mx);
  838. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  839. {
  840. Point leftBottomCorner(0, screen->h);
  841. if(LOCPLINT->battleInt)
  842. {
  843. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  844. }
  845. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  846. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  847. if(SDL_GetTicks() - it->second > defaultTimeout)
  848. {
  849. toDel.push_back(it);
  850. }
  851. }
  852. for(auto & elem : toDel)
  853. {
  854. texts.erase(elem);
  855. }
  856. }
  857. void CInGameConsole::print(const std::string &txt)
  858. {
  859. boost::unique_lock<boost::mutex> lock(texts_mx);
  860. int lineLen = conf.go()->ac.outputLineLength;
  861. if(txt.size() < lineLen)
  862. {
  863. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  864. if(texts.size() > maxDisplayedTexts)
  865. {
  866. texts.pop_front();
  867. }
  868. }
  869. else
  870. {
  871. assert(lineLen);
  872. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  873. {
  874. std::string part = txt.substr(g * lineLen, lineLen);
  875. if(part.size() == 0)
  876. break;
  877. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  878. if(texts.size() > maxDisplayedTexts)
  879. {
  880. texts.pop_front();
  881. }
  882. }
  883. }
  884. }
  885. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  886. {
  887. if(key.type != SDL_KEYDOWN) return;
  888. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  889. switch(key.keysym.sym)
  890. {
  891. case SDLK_TAB:
  892. case SDLK_ESCAPE:
  893. {
  894. if(captureAllKeys)
  895. {
  896. captureAllKeys = false;
  897. endEnteringText(false);
  898. }
  899. else if(SDLK_TAB == key.keysym.sym)
  900. {
  901. captureAllKeys = true;
  902. startEnteringText();
  903. }
  904. break;
  905. }
  906. case SDLK_RETURN: //enter key
  907. {
  908. if(enteredText.size() > 0 && captureAllKeys)
  909. {
  910. captureAllKeys = false;
  911. endEnteringText(true);
  912. CCS->soundh->playSound("CHAT");
  913. }
  914. break;
  915. }
  916. case SDLK_BACKSPACE:
  917. {
  918. if(enteredText.size() > 1)
  919. {
  920. Unicode::trimRight(enteredText,2);
  921. enteredText += '_';
  922. refreshEnteredText();
  923. }
  924. break;
  925. }
  926. case SDLK_UP: //up arrow
  927. {
  928. if(previouslyEntered.size() == 0)
  929. break;
  930. if(prevEntDisp == -1)
  931. {
  932. prevEntDisp = previouslyEntered.size() - 1;
  933. enteredText = previouslyEntered[prevEntDisp] + "_";
  934. refreshEnteredText();
  935. }
  936. else if( prevEntDisp > 0)
  937. {
  938. --prevEntDisp;
  939. enteredText = previouslyEntered[prevEntDisp] + "_";
  940. refreshEnteredText();
  941. }
  942. break;
  943. }
  944. case SDLK_DOWN: //down arrow
  945. {
  946. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  947. {
  948. ++prevEntDisp;
  949. enteredText = previouslyEntered[prevEntDisp] + "_";
  950. refreshEnteredText();
  951. }
  952. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  953. {
  954. prevEntDisp = -1;
  955. enteredText = "_";
  956. refreshEnteredText();
  957. }
  958. break;
  959. }
  960. default:
  961. {
  962. break;
  963. }
  964. }
  965. }
  966. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  967. {
  968. if(!captureAllKeys || enteredText.size() == 0)
  969. return;
  970. enteredText.resize(enteredText.size()-1);
  971. enteredText += event.text;
  972. enteredText += "_";
  973. refreshEnteredText();
  974. }
  975. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  976. {
  977. //do nothing here
  978. }
  979. void CInGameConsole::startEnteringText()
  980. {
  981. CSDL_Ext::startTextInput(&pos);
  982. enteredText = "_";
  983. if(GH.topInt() == adventureInt)
  984. {
  985. GH.statusbar->alignment = TOPLEFT;
  986. GH.statusbar->setText(enteredText);
  987. //Prevent changes to the text from mouse interaction with the adventure map
  988. GH.statusbar->lock(true);
  989. }
  990. else if(LOCPLINT->battleInt)
  991. {
  992. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  993. }
  994. }
  995. void CInGameConsole::endEnteringText(bool printEnteredText)
  996. {
  997. CSDL_Ext::stopTextInput();
  998. prevEntDisp = -1;
  999. if(printEnteredText)
  1000. {
  1001. std::string txt = enteredText.substr(0, enteredText.size()-1);
  1002. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  1003. previouslyEntered.push_back(txt);
  1004. //print(txt);
  1005. }
  1006. enteredText = "";
  1007. if(GH.topInt() == adventureInt)
  1008. {
  1009. GH.statusbar->alignment = CENTER;
  1010. GH.statusbar->lock(false);
  1011. GH.statusbar->clear();
  1012. }
  1013. else if(LOCPLINT->battleInt)
  1014. {
  1015. LOCPLINT->battleInt->console->ingcAlter = "";
  1016. }
  1017. }
  1018. void CInGameConsole::refreshEnteredText()
  1019. {
  1020. if(GH.topInt() == adventureInt)
  1021. {
  1022. GH.statusbar->lock(false);
  1023. GH.statusbar->clear();
  1024. GH.statusbar->setText(enteredText);
  1025. GH.statusbar->lock(true);
  1026. }
  1027. else if(LOCPLINT->battleInt)
  1028. {
  1029. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1030. }
  1031. }
  1032. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  1033. {
  1034. addUsedEvents(KEYBOARD | TEXTINPUT);
  1035. }
  1036. CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position)
  1037. : CIntObject(),
  1038. background(bg)
  1039. {
  1040. defActions = 255;
  1041. recActions = 255;
  1042. pos.x += position.x;
  1043. pos.y += position.y;
  1044. if (bg)
  1045. {
  1046. pos.w = bg->w;
  1047. pos.h = bg->h;
  1048. }
  1049. }
  1050. CAdvMapPanel::~CAdvMapPanel()
  1051. {
  1052. if (background)
  1053. SDL_FreeSurface(background);
  1054. }
  1055. void CAdvMapPanel::addChildColorableButton(CButton * btn)
  1056. {
  1057. buttons.push_back(btn);
  1058. addChildToPanel(btn, ACTIVATE | DEACTIVATE);
  1059. }
  1060. void CAdvMapPanel::setPlayerColor(const PlayerColor & clr)
  1061. {
  1062. for (auto &btn : buttons)
  1063. {
  1064. btn->setPlayerColor(clr);
  1065. }
  1066. }
  1067. void CAdvMapPanel::showAll(SDL_Surface * to)
  1068. {
  1069. if (background)
  1070. blitAt(background, pos.x, pos.y, to);
  1071. CIntObject::showAll(to);
  1072. }
  1073. void CAdvMapPanel::addChildToPanel(CIntObject * obj, ui8 actions)
  1074. {
  1075. obj->recActions |= actions | SHOWALL;
  1076. addChild(obj, false);
  1077. }
  1078. CAdvMapWorldViewPanel::CAdvMapWorldViewPanel(std::shared_ptr<CAnimation> _icons, SDL_Surface * bg, Point position, int spaceBottom, const PlayerColor &color)
  1079. : CAdvMapPanel(bg, position), icons(_icons)
  1080. {
  1081. fillerHeight = bg ? spaceBottom - pos.y - pos.h : 0;
  1082. if (fillerHeight > 0)
  1083. {
  1084. tmpBackgroundFiller = CMessage::drawDialogBox(pos.w, fillerHeight, color);
  1085. }
  1086. else
  1087. tmpBackgroundFiller = nullptr;
  1088. }
  1089. CAdvMapWorldViewPanel::~CAdvMapWorldViewPanel()
  1090. {
  1091. if (tmpBackgroundFiller)
  1092. SDL_FreeSurface(tmpBackgroundFiller);
  1093. }
  1094. void CAdvMapWorldViewPanel::recolorIcons(const PlayerColor &color, int indexOffset)
  1095. {
  1096. assert(iconsData.size() == currentIcons.size());
  1097. for(size_t idx = 0; idx < iconsData.size(); idx++)
  1098. {
  1099. const auto & data = iconsData.at(idx);
  1100. currentIcons[idx]->setFrame(data.first + indexOffset);
  1101. }
  1102. if (fillerHeight > 0)
  1103. {
  1104. if (tmpBackgroundFiller)
  1105. SDL_FreeSurface(tmpBackgroundFiller);
  1106. tmpBackgroundFiller = CMessage::drawDialogBox(pos.w, fillerHeight, color);
  1107. }
  1108. }
  1109. void CAdvMapWorldViewPanel::addChildIcon(std::pair<int, Point> data, int indexOffset)
  1110. {
  1111. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1112. iconsData.push_back(data);
  1113. currentIcons.push_back(new CAnimImage(icons, data.first + indexOffset, 0, data.second.x, data.second.y));
  1114. }
  1115. void CAdvMapWorldViewPanel::showAll(SDL_Surface * to)
  1116. {
  1117. if (tmpBackgroundFiller)
  1118. {
  1119. blitAt(tmpBackgroundFiller, pos.x, pos.y + pos.h, to);
  1120. }
  1121. CAdvMapPanel::showAll(to);
  1122. }