AdventureMapClasses.cpp 22 KB

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