AdventureMapClasses.cpp 22 KB

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