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