AdventureMapClasses.cpp 29 KB

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