AdventureMapClasses.cpp 30 KB

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