AdventureMapClasses.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. switch (obj->ID)
  315. {
  316. case Obj::HERO:
  317. case Obj::PRISON:
  318. continue;
  319. }
  320. PlayerColor player = obj->getOwner();
  321. if(player == PlayerColor::NEUTRAL)
  322. return *graphics->neutralColor;
  323. else
  324. if (player < PlayerColor::PLAYER_LIMIT)
  325. return graphics->playerColors[player.getNum()];
  326. }
  327. // else - use terrain color (blocked version or normal)
  328. if (tile->blocked && (!tile->visitable))
  329. return parent->colors.find(tile->terType)->second.second;
  330. else
  331. return parent->colors.find(tile->terType)->second.first;
  332. }
  333. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  334. {
  335. int3 mapSizes = LOCPLINT->cb->getMapSize();
  336. double stepX = double(pos.w) / mapSizes.x;
  337. double stepY = double(pos.h) / mapSizes.y;
  338. x = toX + stepX * tile.x;
  339. y = toY + stepY * tile.y;
  340. }
  341. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  342. {
  343. //coordinates of rectangle on minimap representing this tile
  344. // begin - first to blit, end - first NOT to blit
  345. int xBegin, yBegin, xEnd, yEnd;
  346. tileToPixels (tile, xBegin, yBegin, toX, toY);
  347. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  348. for (int y=yBegin; y<yEnd; y++)
  349. {
  350. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  351. for (int x=xBegin; x<xEnd; x++)
  352. ColorPutter<4, 1>::PutColor(ptr, color);
  353. }
  354. }
  355. void CMinimapInstance::refreshTile(const int3 &tile)
  356. {
  357. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  358. }
  359. void CMinimapInstance::drawScaled(int level)
  360. {
  361. int3 mapSizes = LOCPLINT->cb->getMapSize();
  362. //size of one map tile on our minimap
  363. double stepX = double(pos.w) / mapSizes.x;
  364. double stepY = double(pos.h) / mapSizes.y;
  365. double currY = 0;
  366. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  367. {
  368. double currX = 0;
  369. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  370. {
  371. const SDL_Color &color = getTileColor(int3(x,y,level));
  372. //coordinates of rectangle on minimap representing this tile
  373. // begin - first to blit, end - first NOT to blit
  374. int xBegin = currX;
  375. int yBegin = currY;
  376. int xEnd = currX + stepX;
  377. int yEnd = currY + stepY;
  378. for (int y=yBegin; y<yEnd; y++)
  379. {
  380. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  381. for (int x=xBegin; x<xEnd; x++)
  382. ColorPutter<4, 1>::PutColor(ptr, color);
  383. }
  384. }
  385. }
  386. }
  387. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  388. parent(Parent),
  389. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  390. level(Level)
  391. {
  392. pos.w = parent->pos.w;
  393. pos.h = parent->pos.h;
  394. drawScaled(level);
  395. }
  396. CMinimapInstance::~CMinimapInstance()
  397. {
  398. SDL_FreeSurface(minimap);
  399. }
  400. void CMinimapInstance::showAll(SDL_Surface *to)
  401. {
  402. blitAtLoc(minimap, 0, 0, to);
  403. //draw heroes
  404. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  405. for(auto & hero : heroes)
  406. {
  407. int3 position = hero->getPosition(false);
  408. if (position.z == level)
  409. {
  410. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  411. blitTileWithColor(color, position, to, pos.x, pos.y);
  412. }
  413. }
  414. }
  415. std::map<int, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors(std::string from)
  416. {
  417. std::map<int, std::pair<SDL_Color, SDL_Color> > ret;
  418. const JsonNode config(ResourceID(from, EResType::TEXT));
  419. for(auto &m : config.Struct())
  420. {
  421. auto index = boost::find(GameConstants::TERRAIN_NAMES, m.first);
  422. if (index == std::end(GameConstants::TERRAIN_NAMES))
  423. {
  424. logGlobal->errorStream() << "Error: unknown terrain in terrains.json: " << m.first;
  425. continue;
  426. }
  427. int terrainID = index - std::begin(GameConstants::TERRAIN_NAMES);
  428. const JsonVector &unblockedVec = m.second["minimapUnblocked"].Vector();
  429. SDL_Color normal =
  430. {
  431. ui8(unblockedVec[0].Float()),
  432. ui8(unblockedVec[1].Float()),
  433. ui8(unblockedVec[2].Float()),
  434. ui8(255)
  435. };
  436. const JsonVector &blockedVec = m.second["minimapBlocked"].Vector();
  437. SDL_Color blocked =
  438. {
  439. ui8(blockedVec[0].Float()),
  440. ui8(blockedVec[1].Float()),
  441. ui8(blockedVec[2].Float()),
  442. ui8(255)
  443. };
  444. ret.insert(std::make_pair(terrainID, std::make_pair(normal, blocked)));
  445. }
  446. return ret;
  447. }
  448. CMinimap::CMinimap(const Rect &position):
  449. CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  450. aiShield(nullptr),
  451. minimap(nullptr),
  452. level(0),
  453. colors(loadColors("config/terrains.json"))
  454. {
  455. pos.w = position.w;
  456. pos.h = position.h;
  457. }
  458. int3 CMinimap::translateMousePosition()
  459. {
  460. // 0 = top-left corner, 1 = bottom-right corner
  461. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  462. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  463. int3 mapSizes = LOCPLINT->cb->getMapSize();
  464. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  465. return tile;
  466. }
  467. void CMinimap::moveAdvMapSelection()
  468. {
  469. int3 newLocation = translateMousePosition();
  470. adventureInt->centerOn(newLocation);
  471. if (!(adventureInt->active & GENERAL))
  472. GH.totalRedraw(); //redraw this as well as inactive adventure map
  473. else
  474. redraw();//redraw only this
  475. }
  476. void CMinimap::clickLeft(tribool down, bool previousState)
  477. {
  478. if (down)
  479. moveAdvMapSelection();
  480. }
  481. void CMinimap::clickRight(tribool down, bool previousState)
  482. {
  483. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  484. }
  485. void CMinimap::hover(bool on)
  486. {
  487. if (on)
  488. GH.statusbar->setText(CGI->generaltexth->zelp[291].first);
  489. else
  490. GH.statusbar->clear();
  491. }
  492. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  493. {
  494. if (pressedL)
  495. moveAdvMapSelection();
  496. }
  497. void CMinimap::showAll(SDL_Surface * to)
  498. {
  499. CIntObject::showAll(to);
  500. if (minimap)
  501. {
  502. int3 mapSizes = LOCPLINT->cb->getMapSize();
  503. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  504. //draw radar
  505. SDL_Rect oldClip;
  506. SDL_Rect radar =
  507. {
  508. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  509. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  510. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  511. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  512. };
  513. SDL_GetClipRect(to, &oldClip);
  514. SDL_SetClipRect(to, &pos);
  515. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  516. SDL_SetClipRect(to, &oldClip);
  517. }
  518. }
  519. void CMinimap::update()
  520. {
  521. if (aiShield) //AI turn is going on. There is no need to update minimap
  522. return;
  523. OBJ_CONSTRUCTION_CAPTURING_ALL;
  524. vstd::clear_pointer(minimap);
  525. minimap = new CMinimapInstance(this, level);
  526. redraw();
  527. }
  528. void CMinimap::setLevel(int newLevel)
  529. {
  530. level = newLevel;
  531. update();
  532. }
  533. void CMinimap::setAIRadar(bool on)
  534. {
  535. if (on)
  536. {
  537. OBJ_CONSTRUCTION_CAPTURING_ALL;
  538. vstd::clear_pointer(minimap);
  539. if (!aiShield)
  540. aiShield = new CPicture("AIShield");
  541. }
  542. else
  543. {
  544. vstd::clear_pointer(aiShield);
  545. update();
  546. }
  547. // this my happen during AI turn when this interface is inactive
  548. // force redraw in order to properly update interface
  549. GH.totalRedraw();
  550. }
  551. void CMinimap::hideTile(const int3 &pos)
  552. {
  553. if (minimap)
  554. minimap->refreshTile(pos);
  555. }
  556. void CMinimap::showTile(const int3 &pos)
  557. {
  558. if (minimap)
  559. minimap->refreshTile(pos);
  560. }
  561. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  562. CIntObject(0, position),
  563. aiProgress(nullptr)
  564. {
  565. }
  566. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  567. {
  568. CIntObject::show(to);
  569. for(auto object : forceRefresh)
  570. object->showAll(to);
  571. }
  572. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  573. {
  574. assert(children.empty()); // visible info should be re-created to change type
  575. OBJ_CONSTRUCTION_CAPTURING_ALL;
  576. new CPicture("ADSTATHR");
  577. new CHeroTooltip(Point(0,0), hero);
  578. }
  579. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  580. {
  581. assert(children.empty()); // visible info should be re-created to change type
  582. OBJ_CONSTRUCTION_CAPTURING_ALL;
  583. new CPicture("ADSTATCS");
  584. new CTownTooltip(Point(0,0), town);
  585. }
  586. void CInfoBar::CVisibleInfo::playNewDaySound()
  587. {
  588. if (LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  589. CCS->soundh->playSound(soundBase::newDay);
  590. else
  591. if (LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  592. CCS->soundh->playSound(soundBase::newWeek);
  593. else
  594. if (LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  595. CCS->soundh->playSound(soundBase::newMonth);
  596. else
  597. CCS->soundh->playSound(soundBase::newDay);
  598. }
  599. std::string CInfoBar::CVisibleInfo::getNewDayName()
  600. {
  601. if (LOCPLINT->cb->getDate(Date::DAY) == 1)
  602. return "NEWDAY";
  603. if (LOCPLINT->cb->getDate(Date::DAY) != 1)
  604. return "NEWDAY";
  605. switch(LOCPLINT->cb->getDate(Date::WEEK))
  606. {
  607. case 1: return "NEWWEEK1";
  608. case 2: return "NEWWEEK2";
  609. case 3: return "NEWWEEK3";
  610. case 4: return "NEWWEEK4";
  611. default: assert(0); return "";
  612. }
  613. }
  614. void CInfoBar::CVisibleInfo::loadDay()
  615. {
  616. assert(children.empty()); // visible info should be re-created first to change type
  617. playNewDaySound();
  618. OBJ_CONSTRUCTION_CAPTURING_ALL;
  619. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  620. std::string labelText;
  621. 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
  622. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  623. else
  624. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  625. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText));
  626. }
  627. void CInfoBar::CVisibleInfo::loadEnemyTurn(PlayerColor player)
  628. {
  629. assert(children.empty()); // visible info should be re-created to change type
  630. OBJ_CONSTRUCTION_CAPTURING_ALL;
  631. new CPicture("ADSTATNX");
  632. new CAnimImage("CREST58", player.getNum(), 0, 20, 51);
  633. new CShowableAnim(99, 51, "HOURSAND");
  634. // FIXME: currently there is no way to get progress from VCAI
  635. // if this will change at some point switch this ifdef to enable correct code
  636. #if 0
  637. //prepare hourglass for updating AI turn
  638. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  639. forceRefresh.push_back(aiProgress);
  640. #else
  641. //create hourglass that will be always animated ignoring AI status
  642. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  643. #endif
  644. }
  645. void CInfoBar::CVisibleInfo::loadGameStatus()
  646. {
  647. assert(children.empty()); // visible info should be re-created to change type
  648. //get amount of halls of each level
  649. std::vector<int> halls(4, 0);
  650. for(auto town : LOCPLINT->towns)
  651. halls[town->hallLevel()]++;
  652. std::vector<PlayerColor> allies, enemies;
  653. //generate list of allies and enemies
  654. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  655. {
  656. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  657. {
  658. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  659. allies.push_back(PlayerColor(i));
  660. else
  661. enemies.push_back(PlayerColor(i));
  662. }
  663. }
  664. //generate component
  665. OBJ_CONSTRUCTION_CAPTURING_ALL;
  666. new CPicture("ADSTATIN");
  667. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  668. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  669. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  670. for(PlayerColor & player : allies)
  671. {
  672. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 102);
  673. posx += image->pos.w;
  674. }
  675. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  676. for(PlayerColor & player : enemies)
  677. {
  678. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 132);
  679. posx += image->pos.w;
  680. }
  681. for (size_t i=0; i<halls.size(); i++)
  682. {
  683. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  684. if (halls[i])
  685. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i]));
  686. }
  687. }
  688. void CInfoBar::CVisibleInfo::loadComponent(const Component & compToDisplay, std::string message)
  689. {
  690. assert(children.empty()); // visible info should be re-created to change type
  691. OBJ_CONSTRUCTION_CAPTURING_ALL;
  692. new CPicture("ADSTATOT", 1);
  693. auto comp = new CComponent(compToDisplay);
  694. comp->moveTo(Point(pos.x+47, pos.y+50));
  695. new CTextBox(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  696. }
  697. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  698. {
  699. if (aiProgress)
  700. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  701. }
  702. void CInfoBar::reset(EState newState = EMPTY)
  703. {
  704. OBJ_CONSTRUCTION_CAPTURING_ALL;
  705. vstd::clear_pointer(visibleInfo);
  706. currentObject = nullptr;
  707. state = newState;
  708. visibleInfo = new CVisibleInfo(Point(8, 12));
  709. }
  710. void CInfoBar::showSelection()
  711. {
  712. if (adventureInt->selection)
  713. {
  714. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  715. if (hero)
  716. {
  717. showHeroSelection(hero);
  718. return;
  719. }
  720. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  721. if (town)
  722. {
  723. showTownSelection(town);
  724. return;
  725. }
  726. }
  727. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  728. }
  729. void CInfoBar::tick()
  730. {
  731. removeUsedEvents(TIME);
  732. showSelection();
  733. }
  734. void CInfoBar::clickLeft(tribool down, bool previousState)
  735. {
  736. if (down)
  737. {
  738. if (state == HERO || state == TOWN)
  739. showGameStatus();
  740. else if (state == GAME)
  741. showDate();
  742. else
  743. showSelection();
  744. }
  745. }
  746. void CInfoBar::clickRight(tribool down, bool previousState)
  747. {
  748. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  749. }
  750. void CInfoBar::hover(bool on)
  751. {
  752. if (on)
  753. GH.statusbar->setText(CGI->generaltexth->zelp[292].first);
  754. else
  755. GH.statusbar->clear();
  756. }
  757. CInfoBar::CInfoBar(const Rect &position):
  758. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  759. visibleInfo(nullptr),
  760. state(EMPTY),
  761. currentObject(nullptr)
  762. {
  763. pos.w = position.w;
  764. pos.h = position.h;
  765. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  766. }
  767. void CInfoBar::showDate()
  768. {
  769. reset(DATE);
  770. visibleInfo->loadDay();
  771. setTimer(3000);
  772. redraw();
  773. }
  774. void CInfoBar::showComponent(const Component & comp, std::string message)
  775. {
  776. reset(COMPONENT);
  777. visibleInfo->loadComponent(comp, message);
  778. setTimer(3000);
  779. redraw();
  780. }
  781. void CInfoBar::startEnemyTurn(PlayerColor color)
  782. {
  783. reset(AITURN);
  784. visibleInfo->loadEnemyTurn(color);
  785. redraw();
  786. }
  787. void CInfoBar::updateEnemyTurn(double progress)
  788. {
  789. assert(state == AITURN);
  790. visibleInfo->updateEnemyTurn(progress);
  791. redraw();
  792. }
  793. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  794. {
  795. if (!hero)
  796. return;
  797. reset(HERO);
  798. currentObject = hero;
  799. visibleInfo->loadHero(hero);
  800. redraw();
  801. }
  802. void CInfoBar::showTownSelection(const CGTownInstance * town)
  803. {
  804. if (!town)
  805. return;
  806. reset(TOWN);
  807. currentObject = town;
  808. visibleInfo->loadTown(town);
  809. redraw();
  810. }
  811. void CInfoBar::showGameStatus()
  812. {
  813. reset(GAME);
  814. visibleInfo->loadGameStatus();
  815. setTimer(3000);
  816. redraw();
  817. }
  818. void CInGameConsole::show(SDL_Surface * to)
  819. {
  820. int number = 0;
  821. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  822. boost::unique_lock<boost::mutex> lock(texts_mx);
  823. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  824. {
  825. Point leftBottomCorner(0, screen->h);
  826. if(LOCPLINT->battleInt)
  827. {
  828. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  829. }
  830. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  831. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  832. if(SDL_GetTicks() - it->second > defaultTimeout)
  833. {
  834. toDel.push_back(it);
  835. }
  836. }
  837. for(auto & elem : toDel)
  838. {
  839. texts.erase(elem);
  840. }
  841. }
  842. void CInGameConsole::print(const std::string &txt)
  843. {
  844. boost::unique_lock<boost::mutex> lock(texts_mx);
  845. int lineLen = conf.go()->ac.outputLineLength;
  846. if(txt.size() < lineLen)
  847. {
  848. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  849. if(texts.size() > maxDisplayedTexts)
  850. {
  851. texts.pop_front();
  852. }
  853. }
  854. else
  855. {
  856. assert(lineLen);
  857. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  858. {
  859. std::string part = txt.substr(g * lineLen, lineLen);
  860. if(part.size() == 0)
  861. break;
  862. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  863. if(texts.size() > maxDisplayedTexts)
  864. {
  865. texts.pop_front();
  866. }
  867. }
  868. }
  869. }
  870. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  871. {
  872. if(key.type != SDL_KEYDOWN) return;
  873. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  874. switch(key.keysym.sym)
  875. {
  876. case SDLK_TAB:
  877. case SDLK_ESCAPE:
  878. {
  879. if(captureAllKeys)
  880. {
  881. captureAllKeys = false;
  882. endEnteringText(false);
  883. }
  884. else if(SDLK_TAB)
  885. {
  886. captureAllKeys = true;
  887. startEnteringText();
  888. }
  889. break;
  890. }
  891. case SDLK_RETURN: //enter key
  892. {
  893. if(enteredText.size() > 0 && captureAllKeys)
  894. {
  895. captureAllKeys = false;
  896. endEnteringText(true);
  897. CCS->soundh->playSound("CHAT");
  898. }
  899. break;
  900. }
  901. case SDLK_BACKSPACE:
  902. {
  903. if(enteredText.size() > 1)
  904. {
  905. Unicode::trimRight(enteredText,2);
  906. enteredText += '_';
  907. refreshEnteredText();
  908. }
  909. break;
  910. }
  911. case SDLK_UP: //up arrow
  912. {
  913. if(previouslyEntered.size() == 0)
  914. break;
  915. if(prevEntDisp == -1)
  916. {
  917. prevEntDisp = previouslyEntered.size() - 1;
  918. enteredText = previouslyEntered[prevEntDisp] + "_";
  919. refreshEnteredText();
  920. }
  921. else if( prevEntDisp > 0)
  922. {
  923. --prevEntDisp;
  924. enteredText = previouslyEntered[prevEntDisp] + "_";
  925. refreshEnteredText();
  926. }
  927. break;
  928. }
  929. case SDLK_DOWN: //down arrow
  930. {
  931. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  932. {
  933. ++prevEntDisp;
  934. enteredText = previouslyEntered[prevEntDisp] + "_";
  935. refreshEnteredText();
  936. }
  937. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  938. {
  939. prevEntDisp = -1;
  940. enteredText = "_";
  941. refreshEnteredText();
  942. }
  943. break;
  944. }
  945. default:
  946. {
  947. #ifdef VCMI_SDL1
  948. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  949. {
  950. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  951. {
  952. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  953. enteredText += "_";
  954. refreshEnteredText();
  955. }
  956. }
  957. #endif // VCMI_SDL1
  958. break;
  959. }
  960. }
  961. }
  962. #ifndef VCMI_SDL1
  963. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  964. {
  965. if(!captureAllKeys || enteredText.size() == 0)
  966. return;
  967. enteredText.resize(enteredText.size()-1);
  968. enteredText += event.text;
  969. enteredText += "_";
  970. refreshEnteredText();
  971. }
  972. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  973. {
  974. //do nothing here
  975. }
  976. #endif // VCMI_SDL1
  977. void CInGameConsole::startEnteringText()
  978. {
  979. CSDL_Ext::startTextInput(&pos);
  980. enteredText = "_";
  981. if(GH.topInt() == adventureInt)
  982. {
  983. GH.statusbar->alignment = TOPLEFT;
  984. GH.statusbar->setText(enteredText);
  985. //Prevent changes to the text from mouse interaction with the adventure map
  986. GH.statusbar->lock(true);
  987. }
  988. else if(LOCPLINT->battleInt)
  989. {
  990. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  991. }
  992. }
  993. void CInGameConsole::endEnteringText(bool printEnteredText)
  994. {
  995. CSDL_Ext::stopTextInput();
  996. prevEntDisp = -1;
  997. if(printEnteredText)
  998. {
  999. std::string txt = enteredText.substr(0, enteredText.size()-1);
  1000. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  1001. previouslyEntered.push_back(txt);
  1002. //print(txt);
  1003. }
  1004. enteredText = "";
  1005. if(GH.topInt() == adventureInt)
  1006. {
  1007. GH.statusbar->alignment = CENTER;
  1008. GH.statusbar->lock(false);
  1009. GH.statusbar->clear();
  1010. }
  1011. else if(LOCPLINT->battleInt)
  1012. {
  1013. LOCPLINT->battleInt->console->ingcAlter = "";
  1014. }
  1015. }
  1016. void CInGameConsole::refreshEnteredText()
  1017. {
  1018. if(GH.topInt() == adventureInt)
  1019. {
  1020. GH.statusbar->lock(false);
  1021. GH.statusbar->clear();
  1022. GH.statusbar->setText(enteredText);
  1023. GH.statusbar->lock(true);
  1024. }
  1025. else if(LOCPLINT->battleInt)
  1026. {
  1027. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1028. }
  1029. }
  1030. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  1031. {
  1032. #ifdef VCMI_SDL1
  1033. addUsedEvents(KEYBOARD);
  1034. #else
  1035. addUsedEvents(KEYBOARD | TEXTINPUT);
  1036. #endif
  1037. }