AdventureMapClasses.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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(), hero->movement / 100));
  170. mana->setFrame(std::min<size_t>(mana->size(), 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. if (vstd::contains(LOCPLINT->wanderingHeroes, 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)
  217. {
  218. item->update();
  219. return;
  220. }
  221. }
  222. return;
  223. }
  224. //simplest solution for now: reset list and restore selection
  225. list->resize(LOCPLINT->wanderingHeroes.size());
  226. if (adventureInt->selection)
  227. {
  228. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  229. if (hero)
  230. select(hero);
  231. }
  232. CList::update();
  233. }
  234. CIntObject * CTownList::createTownItem(size_t index)
  235. {
  236. if (LOCPLINT->towns.size() > index)
  237. return new CTownItem(this, LOCPLINT->towns[index]);
  238. return new CAnimImage("ITPA", 0);
  239. }
  240. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  241. CListItem(parent),
  242. town(Town)
  243. {
  244. OBJ_CONSTRUCTION_CAPTURING_ALL;
  245. picture = new CAnimImage("ITPA", 0);
  246. pos = picture->pos;
  247. update();
  248. }
  249. CIntObject * CTownList::CTownItem::genSelection()
  250. {
  251. return new CAnimImage("ITPA", 1);
  252. }
  253. void CTownList::CTownItem::update()
  254. {
  255. size_t iconIndex = town->subID*2;
  256. if (!town->hasFort())
  257. iconIndex += GameConstants::F_NUMBER*2;
  258. if(town->builded >= GameConstants::MAX_BUILDING_PER_TURN)
  259. iconIndex++;
  260. picture->setFrame(iconIndex + 2);
  261. redraw();
  262. }
  263. void CTownList::CTownItem::select(bool on)
  264. {
  265. if (on && adventureInt->selection != town)
  266. adventureInt->select(town);
  267. }
  268. void CTownList::CTownItem::open()
  269. {
  270. LOCPLINT->openTownWindow(town);
  271. }
  272. void CTownList::CTownItem::showTooltip()
  273. {
  274. CRClickPopup::createAndPush(town, GH.current->motion);
  275. }
  276. std::string CTownList::CTownItem::getHoverText()
  277. {
  278. return town->hoverName;
  279. }
  280. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  281. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, boost::bind(&CTownList::createTownItem, this, _1))
  282. {
  283. }
  284. void CTownList::select(const CGTownInstance * town)
  285. {
  286. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  287. }
  288. void CTownList::update(const CGTownInstance *)
  289. {
  290. //simplest solution for now: reset list and restore selection
  291. list->resize(LOCPLINT->towns.size());
  292. if (adventureInt->selection)
  293. {
  294. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  295. if (town)
  296. select(town);
  297. }
  298. CList::update();
  299. }
  300. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  301. {
  302. static const SDL_Color fogOfWar = {0, 0, 0, 255};
  303. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  304. // if tile is not visible it will be black on minimap
  305. if(!tile)
  306. return fogOfWar;
  307. // if object at tile is owned - it will be colored as its owner
  308. BOOST_FOREACH(const CGObjectInstance *obj, tile->blockingObjects)
  309. {
  310. //heroes will be blitted later
  311. if (obj->ID == GameConstants::HEROI_TYPE)
  312. continue;
  313. int player = obj->getOwner();
  314. if(player == GameConstants::NEUTRAL_PLAYER)
  315. return *graphics->neutralColor;
  316. else
  317. if (player < GameConstants::PLAYER_LIMIT)
  318. return graphics->playerColors[player];
  319. }
  320. // else - use terrain color (blocked version or normal)
  321. if (tile->blocked && (!tile->visitable))
  322. return parent->colors.find(tile->tertype)->second.second;
  323. else
  324. return parent->colors.find(tile->tertype)->second.first;
  325. }
  326. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  327. {
  328. //method is mostly copy-pasted from drawScaled()
  329. int3 mapSizes = LOCPLINT->cb->getMapSize();
  330. double stepX = double(pos.w) / mapSizes.x;
  331. double stepY = double(pos.h) / mapSizes.y;
  332. //coordinates of rectangle on minimap representing this tile
  333. // begin - first to blit, end - first NOT to blit
  334. int xBegin = toX + stepX * tile.x;
  335. int yBegin = toY + stepY * tile.y;
  336. int xEnd = toX + stepX * (tile.x + 1);
  337. int yEnd = toY + stepY * (tile.y + 1);
  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(GameConstants::DATA_DIR + from);
  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. void CMinimap::moveAdvMapSelection()
  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 newLocation (mapSizes.x * dx, mapSizes.y * dy, level);
  449. adventureInt->centerOn(newLocation);
  450. redraw();
  451. }
  452. void CMinimap::clickLeft(tribool down, bool previousState)
  453. {
  454. if (down)
  455. moveAdvMapSelection();
  456. }
  457. void CMinimap::clickRight(tribool down, bool previousState)
  458. {
  459. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  460. }
  461. void CMinimap::hover(bool on)
  462. {
  463. if (on)
  464. GH.statusbar->print(CGI->generaltexth->zelp[291].first);
  465. else
  466. GH.statusbar->clear();
  467. }
  468. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  469. {
  470. if (pressedL)
  471. moveAdvMapSelection();
  472. }
  473. void CMinimap::showAll(SDL_Surface * to)
  474. {
  475. CIntObject::showAll(to);
  476. if (minimap)
  477. {
  478. int3 mapSizes = LOCPLINT->cb->getMapSize();
  479. //draw radar
  480. SDL_Rect oldClip;
  481. SDL_Rect radar =
  482. {
  483. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  484. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  485. ui16(adventureInt->terrain.tilesw * pos.w / mapSizes.x),
  486. ui16(adventureInt->terrain.tilesh * pos.h / mapSizes.y)
  487. };
  488. SDL_GetClipRect(to, &oldClip);
  489. SDL_SetClipRect(to, &pos);
  490. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  491. SDL_SetClipRect(to, &oldClip);
  492. }
  493. }
  494. void CMinimap::update()
  495. {
  496. if (aiShield) //AI turn is going on. There is no need to update minimap
  497. return;
  498. OBJ_CONSTRUCTION_CAPTURING_ALL;
  499. vstd::clear_pointer(minimap);
  500. minimap = new CMinimapInstance(this, level);
  501. redraw();
  502. }
  503. void CMinimap::setLevel(int newLevel)
  504. {
  505. level = newLevel;
  506. update();
  507. }
  508. void CMinimap::setAIRadar(bool on)
  509. {
  510. if (on)
  511. {
  512. OBJ_CONSTRUCTION_CAPTURING_ALL;
  513. vstd::clear_pointer(minimap);
  514. if (!aiShield)
  515. aiShield = new CPicture("AIShield");
  516. }
  517. else
  518. {
  519. vstd::clear_pointer(aiShield);
  520. update();
  521. }
  522. }
  523. void CMinimap::hideTile(const int3 &pos)
  524. {
  525. if (minimap)
  526. minimap->refreshTile(pos);
  527. }
  528. void CMinimap::showTile(const int3 &pos)
  529. {
  530. if (minimap)
  531. minimap->refreshTile(pos);
  532. }
  533. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  534. CIntObject(0, position),
  535. aiProgress(nullptr)
  536. {
  537. }
  538. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  539. {
  540. CIntObject::show(to);
  541. BOOST_FOREACH(auto object, forceRefresh)
  542. object->showAll(to);
  543. }
  544. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  545. {
  546. assert(children.empty()); // visible info should be re-created to change type
  547. OBJ_CONSTRUCTION_CAPTURING_ALL;
  548. new CPicture("ADSTATHR");
  549. new CHeroTooltip(Point(0,0), hero);
  550. }
  551. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  552. {
  553. assert(children.empty()); // visible info should be re-created to change type
  554. OBJ_CONSTRUCTION_CAPTURING_ALL;
  555. new CPicture("ADSTATCS");
  556. new CTownTooltip(Point(0,0), town);
  557. }
  558. void CInfoBar::CVisibleInfo::playNewDaySound()
  559. {
  560. if (LOCPLINT->cb->getDate(1) != 1) // not first day of the week
  561. CCS->soundh->playSound(soundBase::newDay);
  562. else
  563. if (LOCPLINT->cb->getDate(2) != 1) // not first week in month
  564. CCS->soundh->playSound(soundBase::newWeek);
  565. else
  566. if (LOCPLINT->cb->getDate(3) != 1) // not first month
  567. CCS->soundh->playSound(soundBase::newMonth);
  568. else
  569. CCS->soundh->playSound(soundBase::newDay);
  570. }
  571. std::string CInfoBar::CVisibleInfo::getNewDayName()
  572. {
  573. if (LOCPLINT->cb->getDate(0) == 1)
  574. return "NEWDAY";
  575. if (LOCPLINT->cb->getDate(1) != 1)
  576. return "NEWDAY";
  577. switch(LOCPLINT->cb->getDate(2))
  578. {
  579. case 1: return "NEWWEEK1";
  580. case 2: return "NEWWEEK2";
  581. case 3: return "NEWWEEK3";
  582. case 4: return "NEWWEEK4";
  583. default: assert(0); return "";
  584. }
  585. }
  586. void CInfoBar::CVisibleInfo::loadDay()
  587. {
  588. assert(children.empty()); // visible info should be re-created first to change type
  589. playNewDaySound();
  590. OBJ_CONSTRUCTION_CAPTURING_ALL;
  591. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  592. std::string labelText;
  593. if (LOCPLINT->cb->getDate(1) == 1 && LOCPLINT->cb->getDate(0) != 1) // monday of any week but first - show new week info
  594. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(2));
  595. else
  596. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(1));
  597. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::Cornsilk, labelText));
  598. }
  599. void CInfoBar::CVisibleInfo::loadEnemyTurn(int player)
  600. {
  601. assert(children.empty()); // visible info should be re-created to change type
  602. OBJ_CONSTRUCTION_CAPTURING_ALL;
  603. new CPicture("ADSTATNX");
  604. new CAnimImage("CREST58", player, 0, 20, 51);
  605. new CShowableAnim(99, 51, "HOURSAND");
  606. // FIXME: currently there is no way to get progress from VCAI
  607. // if this will change at some point switch this ifdef to enable correct code
  608. #if 0
  609. //prepare hourglass for updating AI turn
  610. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  611. forceRefresh.push_back(aiProgress);
  612. #else
  613. //create hourglass that will be always animated ignoring AI status
  614. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  615. #endif
  616. }
  617. void CInfoBar::CVisibleInfo::loadGameStatus()
  618. {
  619. assert(children.empty()); // visible info should be re-created to change type
  620. //get amount of halls of each level
  621. std::vector<int> halls(4, 0);
  622. BOOST_FOREACH(auto town, LOCPLINT->towns)
  623. halls[town->hallLevel()]++;
  624. std::vector<int> allies, enemies;
  625. //generate list of allies and enemies
  626. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  627. {
  628. if(LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  629. {
  630. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, i) > 0)
  631. allies.push_back(i);
  632. else
  633. enemies.push_back(i);
  634. }
  635. }
  636. //generate component
  637. OBJ_CONSTRUCTION_CAPTURING_ALL;
  638. new CPicture("ADSTATIN");
  639. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[390] + ":");
  640. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[391] + ":");
  641. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  642. BOOST_FOREACH(int & player, allies)
  643. {
  644. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 102);
  645. posx += image->pos.w;
  646. }
  647. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  648. BOOST_FOREACH(int & player, enemies)
  649. {
  650. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 132);
  651. posx += image->pos.w;
  652. }
  653. for (size_t i=0; i<halls.size(); i++)
  654. {
  655. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  656. if (halls[i])
  657. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::Cornsilk, boost::lexical_cast<std::string>(halls[i]));
  658. }
  659. }
  660. void CInfoBar::CVisibleInfo::loadComponent(const Component compToDisplay, std::string message)
  661. {
  662. assert(children.empty()); // visible info should be re-created to change type
  663. OBJ_CONSTRUCTION_CAPTURING_ALL;
  664. new CPicture("ADSTATOT");
  665. auto comp = new CComponent(compToDisplay);
  666. comp->moveTo(Point(pos.x+52, pos.y+54));
  667. new CTextBox(message, Rect(8, 8, 164, 50), 0, FONT_SMALL, CENTER, Colors::Cornsilk);
  668. new CLabel(91, 158, FONT_SMALL, CENTER, Colors::Cornsilk, comp->subtitle);
  669. }
  670. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  671. {
  672. if (aiProgress)
  673. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  674. }
  675. void CInfoBar::reset(EState newState = EMPTY)
  676. {
  677. OBJ_CONSTRUCTION_CAPTURING_ALL;
  678. vstd::clear_pointer(visibleInfo);
  679. currentObject = nullptr;
  680. state = newState;
  681. visibleInfo = new CVisibleInfo(Point(8, 12));
  682. }
  683. void CInfoBar::showSelection()
  684. {
  685. if (adventureInt->selection)
  686. {
  687. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  688. if (hero)
  689. {
  690. showHeroSelection(hero, false);
  691. return;
  692. }
  693. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  694. if (town)
  695. {
  696. showTownSelection(town, false);
  697. return;
  698. }
  699. }
  700. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  701. }
  702. void CInfoBar::tick()
  703. {
  704. removeUsedEvents(TIME);
  705. showSelection();
  706. }
  707. void CInfoBar::clickLeft(tribool down, bool previousState)
  708. {
  709. if (down)
  710. {
  711. if (state == HERO || state == TOWN)
  712. showGameStatus();
  713. else if (state == GAME)
  714. showDate();
  715. else
  716. showSelection();
  717. }
  718. }
  719. void CInfoBar::clickRight(tribool down, bool previousState)
  720. {
  721. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  722. }
  723. void CInfoBar::hover(bool on)
  724. {
  725. if (on)
  726. GH.statusbar->print(CGI->generaltexth->zelp[292].first);
  727. else
  728. GH.statusbar->clear();
  729. }
  730. CInfoBar::CInfoBar(const Rect &position):
  731. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  732. visibleInfo(nullptr),
  733. state(EMPTY),
  734. currentObject(nullptr)
  735. {
  736. pos.w = position.w;
  737. pos.h = position.h;
  738. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  739. }
  740. void CInfoBar::showDate()
  741. {
  742. reset(DATE);
  743. visibleInfo->loadDay();
  744. setTimer(3000);
  745. redraw();
  746. }
  747. void CInfoBar::showComponent(const Component comp, std::string message)
  748. {
  749. reset(COMPONENT);
  750. visibleInfo->loadComponent(comp, message);
  751. setTimer(3000);
  752. redraw();
  753. }
  754. void CInfoBar::startEnemyTurn(ui8 color)
  755. {
  756. reset(AITURN);
  757. visibleInfo->loadEnemyTurn(color);
  758. redraw();
  759. }
  760. void CInfoBar::updateEnemyTurn(double progress)
  761. {
  762. assert(state == AITURN);
  763. visibleInfo->updateEnemyTurn(progress);
  764. redraw();
  765. }
  766. void CInfoBar::showHeroSelection(const CGHeroInstance * hero, bool onlyUpdate)
  767. {
  768. reset(HERO);
  769. currentObject = hero;
  770. visibleInfo->loadHero(hero);
  771. redraw();
  772. }
  773. void CInfoBar::showTownSelection(const CGTownInstance * town, bool onlyUpdate)
  774. {
  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. }