AdventureMapClasses.cpp 31 KB

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