AdventureMapClasses.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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/CMap.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 == Obj::HERO)
  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(auto &m, config.Struct())
  408. {
  409. auto index = boost::find(GameConstants::TERRAIN_NAMES, m.first);
  410. if (index == boost::end(GameConstants::TERRAIN_NAMES))
  411. {
  412. tlog1 << "Error: unknown terrain in terrains.json: " << m.first << "\n";
  413. continue;
  414. }
  415. int terrainID = index - boost::begin(GameConstants::TERRAIN_NAMES);
  416. const JsonVector &unblockedVec = m.second["minimapUnblocked"].Vector();
  417. SDL_Color normal =
  418. {
  419. ui8(unblockedVec[0].Float()),
  420. ui8(unblockedVec[1].Float()),
  421. ui8(unblockedVec[2].Float()),
  422. ui8(255)
  423. };
  424. const JsonVector &blockedVec = m.second["minimapBlocked"].Vector();
  425. SDL_Color blocked =
  426. {
  427. ui8(blockedVec[0].Float()),
  428. ui8(blockedVec[1].Float()),
  429. ui8(blockedVec[2].Float()),
  430. ui8(255)
  431. };
  432. ret.insert(std::make_pair(terrainID, std::make_pair(normal, blocked)));
  433. }
  434. return ret;
  435. }
  436. CMinimap::CMinimap(const Rect &position):
  437. CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  438. aiShield(nullptr),
  439. minimap(nullptr),
  440. level(0),
  441. colors(loadColors("config/terrains.json"))
  442. {
  443. pos.w = position.w;
  444. pos.h = position.h;
  445. }
  446. int3 CMinimap::translateMousePosition()
  447. {
  448. // 0 = top-left corner, 1 = bottom-right corner
  449. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  450. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  451. int3 mapSizes = LOCPLINT->cb->getMapSize();
  452. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  453. return tile;
  454. }
  455. void CMinimap::moveAdvMapSelection()
  456. {
  457. int3 newLocation = translateMousePosition();
  458. adventureInt->centerOn(newLocation);
  459. if (!(adventureInt->active & GENERAL))
  460. GH.totalRedraw(); //redraw this as well as inactive adventure map
  461. else
  462. redraw();//redraw only this
  463. }
  464. void CMinimap::clickLeft(tribool down, bool previousState)
  465. {
  466. if (down)
  467. moveAdvMapSelection();
  468. }
  469. void CMinimap::clickRight(tribool down, bool previousState)
  470. {
  471. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  472. }
  473. void CMinimap::hover(bool on)
  474. {
  475. if (on)
  476. GH.statusbar->print(CGI->generaltexth->zelp[291].first);
  477. else
  478. GH.statusbar->clear();
  479. }
  480. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  481. {
  482. if (pressedL)
  483. moveAdvMapSelection();
  484. }
  485. void CMinimap::showAll(SDL_Surface * to)
  486. {
  487. CIntObject::showAll(to);
  488. if (minimap)
  489. {
  490. int3 mapSizes = LOCPLINT->cb->getMapSize();
  491. //draw radar
  492. SDL_Rect oldClip;
  493. SDL_Rect radar =
  494. {
  495. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  496. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  497. ui16(adventureInt->terrain.tilesw * pos.w / mapSizes.x),
  498. ui16(adventureInt->terrain.tilesh * pos.h / mapSizes.y)
  499. };
  500. SDL_GetClipRect(to, &oldClip);
  501. SDL_SetClipRect(to, &pos);
  502. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  503. SDL_SetClipRect(to, &oldClip);
  504. }
  505. }
  506. void CMinimap::update()
  507. {
  508. if (aiShield) //AI turn is going on. There is no need to update minimap
  509. return;
  510. OBJ_CONSTRUCTION_CAPTURING_ALL;
  511. vstd::clear_pointer(minimap);
  512. minimap = new CMinimapInstance(this, level);
  513. redraw();
  514. }
  515. void CMinimap::setLevel(int newLevel)
  516. {
  517. level = newLevel;
  518. update();
  519. }
  520. void CMinimap::setAIRadar(bool on)
  521. {
  522. if (on)
  523. {
  524. OBJ_CONSTRUCTION_CAPTURING_ALL;
  525. vstd::clear_pointer(minimap);
  526. if (!aiShield)
  527. aiShield = new CPicture("AIShield");
  528. }
  529. else
  530. {
  531. vstd::clear_pointer(aiShield);
  532. update();
  533. }
  534. }
  535. void CMinimap::hideTile(const int3 &pos)
  536. {
  537. if (minimap)
  538. minimap->refreshTile(pos);
  539. }
  540. void CMinimap::showTile(const int3 &pos)
  541. {
  542. if (minimap)
  543. minimap->refreshTile(pos);
  544. }
  545. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  546. CIntObject(0, position),
  547. aiProgress(nullptr)
  548. {
  549. }
  550. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  551. {
  552. CIntObject::show(to);
  553. BOOST_FOREACH(auto object, forceRefresh)
  554. object->showAll(to);
  555. }
  556. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  557. {
  558. assert(children.empty()); // visible info should be re-created to change type
  559. OBJ_CONSTRUCTION_CAPTURING_ALL;
  560. new CPicture("ADSTATHR");
  561. new CHeroTooltip(Point(0,0), hero);
  562. }
  563. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  564. {
  565. assert(children.empty()); // visible info should be re-created to change type
  566. OBJ_CONSTRUCTION_CAPTURING_ALL;
  567. new CPicture("ADSTATCS");
  568. new CTownTooltip(Point(0,0), town);
  569. }
  570. void CInfoBar::CVisibleInfo::playNewDaySound()
  571. {
  572. if (LOCPLINT->cb->getDate(1) != 1) // not first day of the week
  573. CCS->soundh->playSound(soundBase::newDay);
  574. else
  575. if (LOCPLINT->cb->getDate(2) != 1) // not first week in month
  576. CCS->soundh->playSound(soundBase::newWeek);
  577. else
  578. if (LOCPLINT->cb->getDate(3) != 1) // not first month
  579. CCS->soundh->playSound(soundBase::newMonth);
  580. else
  581. CCS->soundh->playSound(soundBase::newDay);
  582. }
  583. std::string CInfoBar::CVisibleInfo::getNewDayName()
  584. {
  585. if (LOCPLINT->cb->getDate(0) == 1)
  586. return "NEWDAY";
  587. if (LOCPLINT->cb->getDate(1) != 1)
  588. return "NEWDAY";
  589. switch(LOCPLINT->cb->getDate(2))
  590. {
  591. case 1: return "NEWWEEK1";
  592. case 2: return "NEWWEEK2";
  593. case 3: return "NEWWEEK3";
  594. case 4: return "NEWWEEK4";
  595. default: assert(0); return "";
  596. }
  597. }
  598. void CInfoBar::CVisibleInfo::loadDay()
  599. {
  600. assert(children.empty()); // visible info should be re-created first to change type
  601. playNewDaySound();
  602. OBJ_CONSTRUCTION_CAPTURING_ALL;
  603. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  604. std::string labelText;
  605. if (LOCPLINT->cb->getDate(1) == 1 && LOCPLINT->cb->getDate(0) != 1) // monday of any week but first - show new week info
  606. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(2));
  607. else
  608. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(1));
  609. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText));
  610. }
  611. void CInfoBar::CVisibleInfo::loadEnemyTurn(int player)
  612. {
  613. assert(children.empty()); // visible info should be re-created to change type
  614. OBJ_CONSTRUCTION_CAPTURING_ALL;
  615. new CPicture("ADSTATNX");
  616. new CAnimImage("CREST58", player, 0, 20, 51);
  617. new CShowableAnim(99, 51, "HOURSAND");
  618. // FIXME: currently there is no way to get progress from VCAI
  619. // if this will change at some point switch this ifdef to enable correct code
  620. #if 0
  621. //prepare hourglass for updating AI turn
  622. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  623. forceRefresh.push_back(aiProgress);
  624. #else
  625. //create hourglass that will be always animated ignoring AI status
  626. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  627. #endif
  628. }
  629. void CInfoBar::CVisibleInfo::loadGameStatus()
  630. {
  631. assert(children.empty()); // visible info should be re-created to change type
  632. //get amount of halls of each level
  633. std::vector<int> halls(4, 0);
  634. BOOST_FOREACH(auto town, LOCPLINT->towns)
  635. halls[town->hallLevel()]++;
  636. std::vector<int> allies, enemies;
  637. //generate list of allies and enemies
  638. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  639. {
  640. if(LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  641. {
  642. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, i) > 0)
  643. allies.push_back(i);
  644. else
  645. enemies.push_back(i);
  646. }
  647. }
  648. //generate component
  649. OBJ_CONSTRUCTION_CAPTURING_ALL;
  650. new CPicture("ADSTATIN");
  651. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  652. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  653. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  654. BOOST_FOREACH(int & player, allies)
  655. {
  656. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 102);
  657. posx += image->pos.w;
  658. }
  659. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  660. BOOST_FOREACH(int & player, enemies)
  661. {
  662. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 132);
  663. posx += image->pos.w;
  664. }
  665. for (size_t i=0; i<halls.size(); i++)
  666. {
  667. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  668. if (halls[i])
  669. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i]));
  670. }
  671. }
  672. void CInfoBar::CVisibleInfo::loadComponent(const Component & compToDisplay, std::string message)
  673. {
  674. assert(children.empty()); // visible info should be re-created to change type
  675. OBJ_CONSTRUCTION_CAPTURING_ALL;
  676. new CPicture("ADSTATOT", 1);
  677. auto comp = new CComponent(compToDisplay);
  678. comp->moveTo(Point(pos.x+47, pos.y+50));
  679. new CTextBox(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  680. }
  681. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  682. {
  683. if (aiProgress)
  684. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  685. }
  686. void CInfoBar::reset(EState newState = EMPTY)
  687. {
  688. OBJ_CONSTRUCTION_CAPTURING_ALL;
  689. vstd::clear_pointer(visibleInfo);
  690. currentObject = nullptr;
  691. state = newState;
  692. visibleInfo = new CVisibleInfo(Point(8, 12));
  693. }
  694. void CInfoBar::showSelection()
  695. {
  696. if (adventureInt->selection)
  697. {
  698. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  699. if (hero)
  700. {
  701. showHeroSelection(hero);
  702. return;
  703. }
  704. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  705. if (town)
  706. {
  707. showTownSelection(town);
  708. return;
  709. }
  710. }
  711. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  712. }
  713. void CInfoBar::tick()
  714. {
  715. removeUsedEvents(TIME);
  716. showSelection();
  717. }
  718. void CInfoBar::clickLeft(tribool down, bool previousState)
  719. {
  720. if (down)
  721. {
  722. if (state == HERO || state == TOWN)
  723. showGameStatus();
  724. else if (state == GAME)
  725. showDate();
  726. else
  727. showSelection();
  728. }
  729. }
  730. void CInfoBar::clickRight(tribool down, bool previousState)
  731. {
  732. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  733. }
  734. void CInfoBar::hover(bool on)
  735. {
  736. if (on)
  737. GH.statusbar->print(CGI->generaltexth->zelp[292].first);
  738. else
  739. GH.statusbar->clear();
  740. }
  741. CInfoBar::CInfoBar(const Rect &position):
  742. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  743. visibleInfo(nullptr),
  744. state(EMPTY),
  745. currentObject(nullptr)
  746. {
  747. pos.w = position.w;
  748. pos.h = position.h;
  749. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  750. }
  751. void CInfoBar::showDate()
  752. {
  753. reset(DATE);
  754. visibleInfo->loadDay();
  755. setTimer(3000);
  756. redraw();
  757. }
  758. void CInfoBar::showComponent(const Component & comp, std::string message)
  759. {
  760. reset(COMPONENT);
  761. visibleInfo->loadComponent(comp, message);
  762. setTimer(3000);
  763. redraw();
  764. }
  765. void CInfoBar::startEnemyTurn(ui8 color)
  766. {
  767. reset(AITURN);
  768. visibleInfo->loadEnemyTurn(color);
  769. redraw();
  770. }
  771. void CInfoBar::updateEnemyTurn(double progress)
  772. {
  773. assert(state == AITURN);
  774. visibleInfo->updateEnemyTurn(progress);
  775. redraw();
  776. }
  777. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  778. {
  779. if (!hero)
  780. return;
  781. reset(HERO);
  782. currentObject = hero;
  783. visibleInfo->loadHero(hero);
  784. redraw();
  785. }
  786. void CInfoBar::showTownSelection(const CGTownInstance * town)
  787. {
  788. if (!town)
  789. return;
  790. reset(TOWN);
  791. currentObject = town;
  792. visibleInfo->loadTown(town);
  793. redraw();
  794. }
  795. void CInfoBar::showGameStatus()
  796. {
  797. reset(GAME);
  798. visibleInfo->loadGameStatus();
  799. setTimer(3000);
  800. redraw();
  801. }