AdventureMapClasses.cpp 22 KB

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