AdventureMapClasses.cpp 31 KB

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