AdventureMapClasses.cpp 28 KB

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