2
0

AdventureMapClasses.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. #include "StdInc.h"
  2. #include "AdventureMapClasses.h"
  3. #include "../CCallback.h"
  4. #include "../lib/JsonNode.h"
  5. #include "../lib/Filesystem/CResourceLoader.h"
  6. #include "../lib/map.h"
  7. #include "../lib/CModHandler.h"
  8. #include "../lib/CObjectHandler.h"
  9. #include "../lib/CGameState.h"
  10. #include "../lib/CGeneralTextHandler.h"
  11. #include "../lib/CTownHandler.h"
  12. #include "../lib/NetPacks.h"
  13. #include "../lib/CHeroHandler.h"
  14. #include "CAdvmapInterface.h"
  15. #include "CAnimation.h"
  16. #include "CGameInfo.h"
  17. #include "CPlayerInterface.h"
  18. #include "CMusicHandler.h"
  19. #include "Graphics.h"
  20. #include "GUIClasses.h"
  21. #include "UIFramework/CGuiHandler.h"
  22. #include "UIFramework/SDL_Pixels.h"
  23. /*
  24. * CAdventureMapClasses.h, part of VCMI engine
  25. *
  26. * Authors: listed in file AUTHORS in main folder
  27. *
  28. * License: GNU General Public License v2.0 or later
  29. * Full text of license available in license.txt file, in main folder
  30. *
  31. */
  32. CList::CListItem::CListItem(CList * Parent):
  33. CIntObject(LCLICK | RCLICK | HOVER),
  34. parent(Parent),
  35. selection(nullptr)
  36. {
  37. }
  38. CList::CListItem::~CListItem()
  39. {
  40. // select() method in this was already destroyed so we can't safely call method in parent
  41. if (parent->selected == this)
  42. parent->selected = nullptr;
  43. }
  44. void CList::CListItem::clickRight(tribool down, bool previousState)
  45. {
  46. if (down == true)
  47. showTooltip();
  48. }
  49. void CList::CListItem::clickLeft(tribool down, bool previousState)
  50. {
  51. if (down == true)
  52. {
  53. //second click on already selected item
  54. if (parent->selected == this)
  55. open();
  56. else
  57. {
  58. //first click - switch selection
  59. parent->select(this);
  60. }
  61. }
  62. }
  63. void CList::CListItem::hover(bool on)
  64. {
  65. if (on)
  66. GH.statusbar->print(getHoverText());
  67. else
  68. GH.statusbar->clear();
  69. }
  70. void CList::CListItem::onSelect(bool on)
  71. {
  72. OBJ_CONSTRUCTION_CAPTURING_ALL;
  73. vstd::clear_pointer(selection);
  74. if (on)
  75. selection = genSelection();
  76. select(on);
  77. GH.totalRedraw();
  78. }
  79. CList::CList(int Size, Point position, std::string btnUp, std::string btnDown, size_t listAmount,
  80. int helpUp, int helpDown, CListBox::CreateFunc create, CListBox::DestroyFunc destroy):
  81. CIntObject(0, position),
  82. size(Size),
  83. selected(nullptr)
  84. {
  85. OBJ_CONSTRUCTION_CAPTURING_ALL;
  86. scrollUp = new CAdventureMapButton(CGI->generaltexth->zelp[helpUp], 0, 0, 0, btnUp);
  87. list = new CListBox(create, destroy, Point(1,scrollUp->pos.h), Point(0, 32), size, listAmount);
  88. //assign callback only after list was created
  89. scrollUp->callback = boost::bind(&CListBox::moveToPrev, list);
  90. scrollDown = new CAdventureMapButton(CGI->generaltexth->zelp[helpDown], boost::bind(&CListBox::moveToNext, list), 0, scrollUp->pos.h + 32*size, btnDown);
  91. scrollDown->callback += boost::bind(&CList::update, this);
  92. scrollUp->callback += boost::bind(&CList::update, this);
  93. update();
  94. }
  95. void CList::update()
  96. {
  97. bool onTop = list->getPos() == 0;
  98. bool onBottom = list->getPos() + size >= list->size();
  99. scrollUp->block(onTop);
  100. scrollDown->block(onBottom);
  101. }
  102. void CList::select(CListItem *which)
  103. {
  104. if (selected == which)
  105. return;
  106. if (selected)
  107. selected->onSelect(false);
  108. selected = which;
  109. if (which)
  110. {
  111. which->onSelect(true);
  112. onSelect();
  113. }
  114. }
  115. int CList::getSelectedIndex()
  116. {
  117. return list->getIndexOf(selected);
  118. }
  119. void CList::selectIndex(int which)
  120. {
  121. if (which < 0)
  122. {
  123. if (selected)
  124. select(nullptr);
  125. }
  126. else
  127. {
  128. list->scrollTo(which);
  129. update();
  130. select(dynamic_cast<CListItem*>(list->getItem(which)));
  131. }
  132. }
  133. void CList::selectNext()
  134. {
  135. int index = getSelectedIndex();
  136. if (index < 0)
  137. selectIndex(0);
  138. else if (index + 1 < list->size())
  139. selectIndex(index+1);
  140. }
  141. void CList::selectPrev()
  142. {
  143. int index = getSelectedIndex();
  144. if (index <= 0)
  145. selectIndex(0);
  146. else
  147. selectIndex(index-1);
  148. }
  149. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  150. {
  151. OBJ_CONSTRUCTION_CAPTURING_ALL;
  152. auto move = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  153. auto img = new CPicture("HPSXXX", move->pos.w + 1);
  154. auto mana = new CAnimImage("IMANA", 0, 0, move->pos.w + img->pos.w + 2, 1 );
  155. pos.w = mana->pos.w + mana->pos.x - pos.x;
  156. pos.h = std::max(std::max<ui16>(move->pos.h + 1, mana->pos.h + 1), img->pos.h);
  157. }
  158. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero):
  159. CListItem(parent),
  160. hero(Hero)
  161. {
  162. OBJ_CONSTRUCTION_CAPTURING_ALL;
  163. movement = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  164. portrait = new CAnimImage("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  165. mana = new CAnimImage("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  166. pos.w = mana->pos.w + mana->pos.x - pos.x;
  167. pos.h = std::max(std::max<ui16>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  168. update();
  169. }
  170. void CHeroList::CHeroItem::update()
  171. {
  172. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  173. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  174. redraw();
  175. }
  176. CIntObject * CHeroList::CHeroItem::genSelection()
  177. {
  178. return new CPicture("HPSYYY", movement->pos.w + 1);
  179. }
  180. void CHeroList::CHeroItem::select(bool on)
  181. {
  182. if (on && adventureInt->selection != hero)
  183. adventureInt->select(hero);
  184. }
  185. void CHeroList::CHeroItem::open()
  186. {
  187. LOCPLINT->openHeroWindow(hero);
  188. }
  189. void CHeroList::CHeroItem::showTooltip()
  190. {
  191. CRClickPopup::createAndPush(hero, GH.current->motion);
  192. }
  193. std::string CHeroList::CHeroItem::getHoverText()
  194. {
  195. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->name % hero->type->heroClass->name);
  196. }
  197. CIntObject * CHeroList::createHeroItem(size_t index)
  198. {
  199. if (LOCPLINT->wanderingHeroes.size() > index)
  200. return new CHeroItem(this, LOCPLINT->wanderingHeroes[index]);
  201. return new CEmptyHeroItem();
  202. }
  203. CHeroList::CHeroList(int size, Point position, std::string btnUp, std::string btnDown):
  204. CList(size, position, btnUp, btnDown, LOCPLINT->wanderingHeroes.size(), 303, 304, boost::bind(&CHeroList::createHeroItem, this, _1))
  205. {
  206. }
  207. void CHeroList::select(const CGHeroInstance * hero)
  208. {
  209. selectIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  210. }
  211. void CHeroList::update(const CGHeroInstance * hero)
  212. {
  213. //this hero is already present, update its status
  214. for (auto iter = list->getItems().begin(); iter != list->getItems().end(); iter++)
  215. {
  216. auto item = dynamic_cast<CHeroItem*>(*iter);
  217. if (item && item->hero == hero && vstd::contains(LOCPLINT->wanderingHeroes, hero))
  218. {
  219. item->update();
  220. return;
  221. }
  222. }
  223. //simplest solution for now: reset list and restore selection
  224. list->resize(LOCPLINT->wanderingHeroes.size());
  225. if (adventureInt->selection)
  226. {
  227. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  228. if (hero)
  229. select(hero);
  230. }
  231. CList::update();
  232. }
  233. CIntObject * CTownList::createTownItem(size_t index)
  234. {
  235. if (LOCPLINT->towns.size() > index)
  236. return new CTownItem(this, LOCPLINT->towns[index]);
  237. return new CAnimImage("ITPA", 0);
  238. }
  239. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  240. CListItem(parent),
  241. town(Town)
  242. {
  243. OBJ_CONSTRUCTION_CAPTURING_ALL;
  244. picture = new CAnimImage("ITPA", 0);
  245. pos = picture->pos;
  246. update();
  247. }
  248. CIntObject * CTownList::CTownItem::genSelection()
  249. {
  250. return new CAnimImage("ITPA", 1);
  251. }
  252. void CTownList::CTownItem::update()
  253. {
  254. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  255. picture->setFrame(iconIndex + 2);
  256. redraw();
  257. }
  258. void CTownList::CTownItem::select(bool on)
  259. {
  260. if (on && adventureInt->selection != town)
  261. adventureInt->select(town);
  262. }
  263. void CTownList::CTownItem::open()
  264. {
  265. LOCPLINT->openTownWindow(town);
  266. }
  267. void CTownList::CTownItem::showTooltip()
  268. {
  269. CRClickPopup::createAndPush(town, GH.current->motion);
  270. }
  271. std::string CTownList::CTownItem::getHoverText()
  272. {
  273. return town->hoverName;
  274. }
  275. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  276. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, boost::bind(&CTownList::createTownItem, this, _1))
  277. {
  278. }
  279. void CTownList::select(const CGTownInstance * town)
  280. {
  281. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  282. }
  283. void CTownList::update(const CGTownInstance *)
  284. {
  285. //simplest solution for now: reset list and restore selection
  286. list->resize(LOCPLINT->towns.size());
  287. if (adventureInt->selection)
  288. {
  289. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  290. if (town)
  291. select(town);
  292. }
  293. CList::update();
  294. }
  295. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  296. {
  297. static const SDL_Color fogOfWar = {0, 0, 0, 255};
  298. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  299. // if tile is not visible it will be black on minimap
  300. if(!tile)
  301. return fogOfWar;
  302. // if object at tile is owned - it will be colored as its owner
  303. BOOST_FOREACH(const CGObjectInstance *obj, tile->blockingObjects)
  304. {
  305. //heroes will be blitted later
  306. if (obj->ID == GameConstants::HEROI_TYPE)
  307. continue;
  308. int player = obj->getOwner();
  309. if(player == GameConstants::NEUTRAL_PLAYER)
  310. return *graphics->neutralColor;
  311. else
  312. if (player < GameConstants::PLAYER_LIMIT)
  313. return graphics->playerColors[player];
  314. }
  315. // else - use terrain color (blocked version or normal)
  316. if (tile->blocked && (!tile->visitable))
  317. return parent->colors.find(tile->tertype)->second.second;
  318. else
  319. return parent->colors.find(tile->tertype)->second.first;
  320. }
  321. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  322. {
  323. int3 mapSizes = LOCPLINT->cb->getMapSize();
  324. double stepX = double(pos.w) / mapSizes.x;
  325. double stepY = double(pos.h) / mapSizes.y;
  326. x = toX + stepX * tile.x;
  327. y = toY + stepY * tile.y;
  328. }
  329. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  330. {
  331. //coordinates of rectangle on minimap representing this tile
  332. // begin - first to blit, end - first NOT to blit
  333. int xBegin, yBegin, xEnd, yEnd;
  334. tileToPixels (tile, xBegin, yBegin, toX, toY);
  335. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  336. for (int y=yBegin; y<yEnd; y++)
  337. {
  338. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  339. for (int x=xBegin; x<xEnd; x++)
  340. ColorPutter<4, 1>::PutColor(ptr, color);
  341. }
  342. }
  343. void CMinimapInstance::refreshTile(const int3 &tile)
  344. {
  345. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  346. }
  347. void CMinimapInstance::drawScaled(int level)
  348. {
  349. int3 mapSizes = LOCPLINT->cb->getMapSize();
  350. //size of one map tile on our minimap
  351. double stepX = double(pos.w) / mapSizes.x;
  352. double stepY = double(pos.h) / mapSizes.y;
  353. double currY = 0;
  354. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  355. {
  356. double currX = 0;
  357. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  358. {
  359. const SDL_Color &color = getTileColor(int3(x,y,level));
  360. //coordinates of rectangle on minimap representing this tile
  361. // begin - first to blit, end - first NOT to blit
  362. int xBegin = currX;
  363. int yBegin = currY;
  364. int xEnd = currX + stepX;
  365. int yEnd = currY + stepY;
  366. for (int y=yBegin; y<yEnd; y++)
  367. {
  368. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  369. for (int x=xBegin; x<xEnd; x++)
  370. ColorPutter<4, 1>::PutColor(ptr, color);
  371. }
  372. }
  373. }
  374. }
  375. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  376. parent(Parent),
  377. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  378. level(Level)
  379. {
  380. pos.w = parent->pos.w;
  381. pos.h = parent->pos.h;
  382. drawScaled(level);
  383. }
  384. CMinimapInstance::~CMinimapInstance()
  385. {
  386. SDL_FreeSurface(minimap);
  387. }
  388. void CMinimapInstance::showAll(SDL_Surface *to)
  389. {
  390. blitAtLoc(minimap, 0, 0, to);
  391. //draw heroes
  392. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false);
  393. BOOST_FOREACH(auto & hero, heroes)
  394. {
  395. int3 position = hero->getPosition(false);
  396. if (position.z == level)
  397. {
  398. const SDL_Color & color = graphics->playerColors[hero->getOwner()];
  399. blitTileWithColor(color, position, to, pos.x, pos.y);
  400. }
  401. }
  402. }
  403. std::map<int, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors(std::string from)
  404. {
  405. std::map<int, std::pair<SDL_Color, SDL_Color> > ret;
  406. const JsonNode config(ResourceID(from, EResType::TEXT));
  407. BOOST_FOREACH(const JsonNode &m, config["MinimapColors"].Vector())
  408. {
  409. int id = m["terrain_id"].Float();
  410. const JsonVector &unblockedVec = m["unblocked"].Vector();
  411. SDL_Color normal =
  412. {
  413. ui8(unblockedVec[0].Float()),
  414. ui8(unblockedVec[1].Float()),
  415. ui8(unblockedVec[2].Float()),
  416. ui8(255)
  417. };
  418. const JsonVector &blockedVec = m["blocked"].Vector();
  419. SDL_Color blocked =
  420. {
  421. ui8(blockedVec[0].Float()),
  422. ui8(blockedVec[1].Float()),
  423. ui8(blockedVec[2].Float()),
  424. ui8(255)
  425. };
  426. ret.insert(std::make_pair(id, std::make_pair(normal, blocked)));
  427. }
  428. return ret;
  429. }
  430. CMinimap::CMinimap(const Rect &position):
  431. CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  432. aiShield(nullptr),
  433. minimap(nullptr),
  434. level(0),
  435. colors(loadColors("config/minimap.json"))
  436. {
  437. pos.w = position.w;
  438. pos.h = position.h;
  439. }
  440. int3 CMinimap::translateMousePosition()
  441. {
  442. // 0 = top-left corner, 1 = bottom-right corner
  443. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  444. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  445. int3 mapSizes = LOCPLINT->cb->getMapSize();
  446. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  447. return tile;
  448. }
  449. void CMinimap::moveAdvMapSelection()
  450. {
  451. int3 newLocation = translateMousePosition();
  452. adventureInt->centerOn(newLocation);
  453. if (!(adventureInt->active & GENERAL))
  454. GH.totalRedraw(); //redraw this as well as inactive adventure map
  455. else
  456. redraw();//redraw only this
  457. }
  458. void CMinimap::clickLeft(tribool down, bool previousState)
  459. {
  460. if (down)
  461. moveAdvMapSelection();
  462. }
  463. void CMinimap::clickRight(tribool down, bool previousState)
  464. {
  465. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  466. }
  467. void CMinimap::hover(bool on)
  468. {
  469. if (on)
  470. GH.statusbar->print(CGI->generaltexth->zelp[291].first);
  471. else
  472. GH.statusbar->clear();
  473. }
  474. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  475. {
  476. if (pressedL)
  477. moveAdvMapSelection();
  478. }
  479. void CMinimap::showAll(SDL_Surface * to)
  480. {
  481. CIntObject::showAll(to);
  482. if (minimap)
  483. {
  484. int3 mapSizes = LOCPLINT->cb->getMapSize();
  485. //draw radar
  486. SDL_Rect oldClip;
  487. SDL_Rect radar =
  488. {
  489. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  490. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  491. ui16(adventureInt->terrain.tilesw * pos.w / mapSizes.x),
  492. ui16(adventureInt->terrain.tilesh * pos.h / mapSizes.y)
  493. };
  494. SDL_GetClipRect(to, &oldClip);
  495. SDL_SetClipRect(to, &pos);
  496. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  497. SDL_SetClipRect(to, &oldClip);
  498. }
  499. }
  500. void CMinimap::update()
  501. {
  502. if (aiShield) //AI turn is going on. There is no need to update minimap
  503. return;
  504. OBJ_CONSTRUCTION_CAPTURING_ALL;
  505. vstd::clear_pointer(minimap);
  506. minimap = new CMinimapInstance(this, level);
  507. redraw();
  508. }
  509. void CMinimap::setLevel(int newLevel)
  510. {
  511. level = newLevel;
  512. update();
  513. }
  514. void CMinimap::setAIRadar(bool on)
  515. {
  516. if (on)
  517. {
  518. OBJ_CONSTRUCTION_CAPTURING_ALL;
  519. vstd::clear_pointer(minimap);
  520. if (!aiShield)
  521. aiShield = new CPicture("AIShield");
  522. }
  523. else
  524. {
  525. vstd::clear_pointer(aiShield);
  526. update();
  527. }
  528. }
  529. void CMinimap::hideTile(const int3 &pos)
  530. {
  531. if (minimap)
  532. minimap->refreshTile(pos);
  533. }
  534. void CMinimap::showTile(const int3 &pos)
  535. {
  536. if (minimap)
  537. minimap->refreshTile(pos);
  538. }
  539. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  540. CIntObject(0, position),
  541. aiProgress(nullptr)
  542. {
  543. }
  544. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  545. {
  546. CIntObject::show(to);
  547. BOOST_FOREACH(auto object, forceRefresh)
  548. object->showAll(to);
  549. }
  550. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  551. {
  552. assert(children.empty()); // visible info should be re-created to change type
  553. OBJ_CONSTRUCTION_CAPTURING_ALL;
  554. new CPicture("ADSTATHR");
  555. new CHeroTooltip(Point(0,0), hero);
  556. }
  557. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  558. {
  559. assert(children.empty()); // visible info should be re-created to change type
  560. OBJ_CONSTRUCTION_CAPTURING_ALL;
  561. new CPicture("ADSTATCS");
  562. new CTownTooltip(Point(0,0), town);
  563. }
  564. void CInfoBar::CVisibleInfo::playNewDaySound()
  565. {
  566. if (LOCPLINT->cb->getDate(1) != 1) // not first day of the week
  567. CCS->soundh->playSound(soundBase::newDay);
  568. else
  569. if (LOCPLINT->cb->getDate(2) != 1) // not first week in month
  570. CCS->soundh->playSound(soundBase::newWeek);
  571. else
  572. if (LOCPLINT->cb->getDate(3) != 1) // not first month
  573. CCS->soundh->playSound(soundBase::newMonth);
  574. else
  575. CCS->soundh->playSound(soundBase::newDay);
  576. }
  577. std::string CInfoBar::CVisibleInfo::getNewDayName()
  578. {
  579. if (LOCPLINT->cb->getDate(0) == 1)
  580. return "NEWDAY";
  581. if (LOCPLINT->cb->getDate(1) != 1)
  582. return "NEWDAY";
  583. switch(LOCPLINT->cb->getDate(2))
  584. {
  585. case 1: return "NEWWEEK1";
  586. case 2: return "NEWWEEK2";
  587. case 3: return "NEWWEEK3";
  588. case 4: return "NEWWEEK4";
  589. default: assert(0); return "";
  590. }
  591. }
  592. void CInfoBar::CVisibleInfo::loadDay()
  593. {
  594. assert(children.empty()); // visible info should be re-created first to change type
  595. playNewDaySound();
  596. OBJ_CONSTRUCTION_CAPTURING_ALL;
  597. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  598. std::string labelText;
  599. if (LOCPLINT->cb->getDate(1) == 1 && LOCPLINT->cb->getDate(0) != 1) // monday of any week but first - show new week info
  600. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(2));
  601. else
  602. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(1));
  603. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::Cornsilk, labelText));
  604. }
  605. void CInfoBar::CVisibleInfo::loadEnemyTurn(int player)
  606. {
  607. assert(children.empty()); // visible info should be re-created to change type
  608. OBJ_CONSTRUCTION_CAPTURING_ALL;
  609. new CPicture("ADSTATNX");
  610. new CAnimImage("CREST58", player, 0, 20, 51);
  611. new CShowableAnim(99, 51, "HOURSAND");
  612. // FIXME: currently there is no way to get progress from VCAI
  613. // if this will change at some point switch this ifdef to enable correct code
  614. #if 0
  615. //prepare hourglass for updating AI turn
  616. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  617. forceRefresh.push_back(aiProgress);
  618. #else
  619. //create hourglass that will be always animated ignoring AI status
  620. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  621. #endif
  622. }
  623. void CInfoBar::CVisibleInfo::loadGameStatus()
  624. {
  625. assert(children.empty()); // visible info should be re-created to change type
  626. //get amount of halls of each level
  627. std::vector<int> halls(4, 0);
  628. BOOST_FOREACH(auto town, LOCPLINT->towns)
  629. halls[town->hallLevel()]++;
  630. std::vector<int> allies, enemies;
  631. //generate list of allies and enemies
  632. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  633. {
  634. if(LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  635. {
  636. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, i) > 0)
  637. allies.push_back(i);
  638. else
  639. enemies.push_back(i);
  640. }
  641. }
  642. //generate component
  643. OBJ_CONSTRUCTION_CAPTURING_ALL;
  644. new CPicture("ADSTATIN");
  645. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[390] + ":");
  646. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[391] + ":");
  647. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  648. BOOST_FOREACH(int & player, allies)
  649. {
  650. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 102);
  651. posx += image->pos.w;
  652. }
  653. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  654. BOOST_FOREACH(int & player, enemies)
  655. {
  656. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 132);
  657. posx += image->pos.w;
  658. }
  659. for (size_t i=0; i<halls.size(); i++)
  660. {
  661. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  662. if (halls[i])
  663. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::Cornsilk, boost::lexical_cast<std::string>(halls[i]));
  664. }
  665. }
  666. void CInfoBar::CVisibleInfo::loadComponent(const Component compToDisplay, std::string message)
  667. {
  668. assert(children.empty()); // visible info should be re-created to change type
  669. OBJ_CONSTRUCTION_CAPTURING_ALL;
  670. new CPicture("ADSTATOT");
  671. auto comp = new CComponent(compToDisplay);
  672. comp->moveTo(Point(pos.x+52, pos.y+54));
  673. new CTextBox(message, Rect(8, 8, 164, 50), 0, FONT_SMALL, CENTER, Colors::Cornsilk);
  674. }
  675. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  676. {
  677. if (aiProgress)
  678. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  679. }
  680. void CInfoBar::reset(EState newState = EMPTY)
  681. {
  682. OBJ_CONSTRUCTION_CAPTURING_ALL;
  683. vstd::clear_pointer(visibleInfo);
  684. currentObject = nullptr;
  685. state = newState;
  686. visibleInfo = new CVisibleInfo(Point(8, 12));
  687. }
  688. void CInfoBar::showSelection()
  689. {
  690. if (adventureInt->selection)
  691. {
  692. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  693. if (hero)
  694. {
  695. showHeroSelection(hero);
  696. return;
  697. }
  698. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  699. if (town)
  700. {
  701. showTownSelection(town);
  702. return;
  703. }
  704. }
  705. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  706. }
  707. void CInfoBar::tick()
  708. {
  709. removeUsedEvents(TIME);
  710. showSelection();
  711. }
  712. void CInfoBar::clickLeft(tribool down, bool previousState)
  713. {
  714. if (down)
  715. {
  716. if (state == HERO || state == TOWN)
  717. showGameStatus();
  718. else if (state == GAME)
  719. showDate();
  720. else
  721. showSelection();
  722. }
  723. }
  724. void CInfoBar::clickRight(tribool down, bool previousState)
  725. {
  726. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  727. }
  728. void CInfoBar::hover(bool on)
  729. {
  730. if (on)
  731. GH.statusbar->print(CGI->generaltexth->zelp[292].first);
  732. else
  733. GH.statusbar->clear();
  734. }
  735. CInfoBar::CInfoBar(const Rect &position):
  736. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  737. visibleInfo(nullptr),
  738. state(EMPTY),
  739. currentObject(nullptr)
  740. {
  741. pos.w = position.w;
  742. pos.h = position.h;
  743. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  744. }
  745. void CInfoBar::showDate()
  746. {
  747. reset(DATE);
  748. visibleInfo->loadDay();
  749. setTimer(3000);
  750. redraw();
  751. }
  752. void CInfoBar::showComponent(const Component comp, std::string message)
  753. {
  754. reset(COMPONENT);
  755. visibleInfo->loadComponent(comp, message);
  756. setTimer(3000);
  757. redraw();
  758. }
  759. void CInfoBar::startEnemyTurn(ui8 color)
  760. {
  761. reset(AITURN);
  762. visibleInfo->loadEnemyTurn(color);
  763. redraw();
  764. }
  765. void CInfoBar::updateEnemyTurn(double progress)
  766. {
  767. assert(state == AITURN);
  768. visibleInfo->updateEnemyTurn(progress);
  769. redraw();
  770. }
  771. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  772. {
  773. if (!hero)
  774. return;
  775. reset(HERO);
  776. currentObject = hero;
  777. visibleInfo->loadHero(hero);
  778. redraw();
  779. }
  780. void CInfoBar::showTownSelection(const CGTownInstance * town)
  781. {
  782. if (!town)
  783. return;
  784. reset(TOWN);
  785. currentObject = town;
  786. visibleInfo->loadTown(town);
  787. redraw();
  788. }
  789. void CInfoBar::showGameStatus()
  790. {
  791. reset(GAME);
  792. visibleInfo->loadGameStatus();
  793. setTimer(3000);
  794. redraw();
  795. }