AdventureMapClasses.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  323. {
  324. int3 mapSizes = LOCPLINT->cb->getMapSize();
  325. double stepX = double(pos.w) / mapSizes.x;
  326. double stepY = double(pos.h) / mapSizes.y;
  327. x = toX + stepX * tile.x;
  328. y = toY + stepY * tile.y;
  329. }
  330. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  331. {
  332. //coordinates of rectangle on minimap representing this tile
  333. // begin - first to blit, end - first NOT to blit
  334. int xBegin, yBegin, xEnd, yEnd;
  335. tileToPixels (tile, xBegin, yBegin, toX, toY);
  336. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  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. int3 CMinimap::translateMousePosition()
  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 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  448. return tile;
  449. }
  450. void CMinimap::moveAdvMapSelection()
  451. {
  452. int3 newLocation = translateMousePosition();
  453. adventureInt->centerOn(newLocation);
  454. GH.totalRedraw(); //redraw this as well as adventure map (which may be inactive)
  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. redraw();
  506. }
  507. void CMinimap::setLevel(int newLevel)
  508. {
  509. level = newLevel;
  510. update();
  511. }
  512. void CMinimap::setAIRadar(bool on)
  513. {
  514. if (on)
  515. {
  516. OBJ_CONSTRUCTION_CAPTURING_ALL;
  517. vstd::clear_pointer(minimap);
  518. if (!aiShield)
  519. aiShield = new CPicture("AIShield");
  520. }
  521. else
  522. {
  523. vstd::clear_pointer(aiShield);
  524. update();
  525. }
  526. }
  527. void CMinimap::hideTile(const int3 &pos)
  528. {
  529. if (minimap)
  530. minimap->refreshTile(pos);
  531. }
  532. void CMinimap::showTile(const int3 &pos)
  533. {
  534. if (minimap)
  535. minimap->refreshTile(pos);
  536. }
  537. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  538. CIntObject(0, position),
  539. aiProgress(nullptr)
  540. {
  541. }
  542. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  543. {
  544. CIntObject::show(to);
  545. BOOST_FOREACH(auto object, forceRefresh)
  546. object->showAll(to);
  547. }
  548. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  549. {
  550. assert(children.empty()); // visible info should be re-created to change type
  551. OBJ_CONSTRUCTION_CAPTURING_ALL;
  552. new CPicture("ADSTATHR");
  553. new CHeroTooltip(Point(0,0), hero);
  554. }
  555. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  556. {
  557. assert(children.empty()); // visible info should be re-created to change type
  558. OBJ_CONSTRUCTION_CAPTURING_ALL;
  559. new CPicture("ADSTATCS");
  560. new CTownTooltip(Point(0,0), town);
  561. }
  562. void CInfoBar::CVisibleInfo::playNewDaySound()
  563. {
  564. if (LOCPLINT->cb->getDate(1) != 1) // not first day of the week
  565. CCS->soundh->playSound(soundBase::newDay);
  566. else
  567. if (LOCPLINT->cb->getDate(2) != 1) // not first week in month
  568. CCS->soundh->playSound(soundBase::newWeek);
  569. else
  570. if (LOCPLINT->cb->getDate(3) != 1) // not first month
  571. CCS->soundh->playSound(soundBase::newMonth);
  572. else
  573. CCS->soundh->playSound(soundBase::newDay);
  574. }
  575. std::string CInfoBar::CVisibleInfo::getNewDayName()
  576. {
  577. if (LOCPLINT->cb->getDate(0) == 1)
  578. return "NEWDAY";
  579. if (LOCPLINT->cb->getDate(1) != 1)
  580. return "NEWDAY";
  581. switch(LOCPLINT->cb->getDate(2))
  582. {
  583. case 1: return "NEWWEEK1";
  584. case 2: return "NEWWEEK2";
  585. case 3: return "NEWWEEK3";
  586. case 4: return "NEWWEEK4";
  587. default: assert(0); return "";
  588. }
  589. }
  590. void CInfoBar::CVisibleInfo::loadDay()
  591. {
  592. assert(children.empty()); // visible info should be re-created first to change type
  593. playNewDaySound();
  594. OBJ_CONSTRUCTION_CAPTURING_ALL;
  595. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  596. std::string labelText;
  597. if (LOCPLINT->cb->getDate(1) == 1 && LOCPLINT->cb->getDate(0) != 1) // monday of any week but first - show new week info
  598. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(2));
  599. else
  600. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(1));
  601. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::Cornsilk, labelText));
  602. }
  603. void CInfoBar::CVisibleInfo::loadEnemyTurn(int player)
  604. {
  605. assert(children.empty()); // visible info should be re-created to change type
  606. OBJ_CONSTRUCTION_CAPTURING_ALL;
  607. new CPicture("ADSTATNX");
  608. new CAnimImage("CREST58", player, 0, 20, 51);
  609. new CShowableAnim(99, 51, "HOURSAND");
  610. // FIXME: currently there is no way to get progress from VCAI
  611. // if this will change at some point switch this ifdef to enable correct code
  612. #if 0
  613. //prepare hourglass for updating AI turn
  614. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  615. forceRefresh.push_back(aiProgress);
  616. #else
  617. //create hourglass that will be always animated ignoring AI status
  618. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  619. #endif
  620. }
  621. void CInfoBar::CVisibleInfo::loadGameStatus()
  622. {
  623. assert(children.empty()); // visible info should be re-created to change type
  624. //get amount of halls of each level
  625. std::vector<int> halls(4, 0);
  626. BOOST_FOREACH(auto town, LOCPLINT->towns)
  627. halls[town->hallLevel()]++;
  628. std::vector<int> allies, enemies;
  629. //generate list of allies and enemies
  630. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  631. {
  632. if(LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  633. {
  634. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, i) > 0)
  635. allies.push_back(i);
  636. else
  637. enemies.push_back(i);
  638. }
  639. }
  640. //generate component
  641. OBJ_CONSTRUCTION_CAPTURING_ALL;
  642. new CPicture("ADSTATIN");
  643. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[390] + ":");
  644. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::Cornsilk, CGI->generaltexth->allTexts[391] + ":");
  645. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  646. BOOST_FOREACH(int & player, allies)
  647. {
  648. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 102);
  649. posx += image->pos.w;
  650. }
  651. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  652. BOOST_FOREACH(int & player, enemies)
  653. {
  654. auto image = new CAnimImage("ITGFLAGS", player, 0, posx, 132);
  655. posx += image->pos.w;
  656. }
  657. for (size_t i=0; i<halls.size(); i++)
  658. {
  659. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  660. if (halls[i])
  661. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::Cornsilk, boost::lexical_cast<std::string>(halls[i]));
  662. }
  663. }
  664. void CInfoBar::CVisibleInfo::loadComponent(const Component compToDisplay, std::string message)
  665. {
  666. assert(children.empty()); // visible info should be re-created to change type
  667. OBJ_CONSTRUCTION_CAPTURING_ALL;
  668. new CPicture("ADSTATOT");
  669. auto comp = new CComponent(compToDisplay);
  670. comp->moveTo(Point(pos.x+52, pos.y+54));
  671. new CTextBox(message, Rect(8, 8, 164, 50), 0, FONT_SMALL, CENTER, Colors::Cornsilk);
  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);
  694. return;
  695. }
  696. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  697. if (town)
  698. {
  699. showTownSelection(town);
  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)
  770. {
  771. if (!hero)
  772. return;
  773. reset(HERO);
  774. currentObject = hero;
  775. visibleInfo->loadHero(hero);
  776. redraw();
  777. }
  778. void CInfoBar::showTownSelection(const CGTownInstance * town)
  779. {
  780. if (!town)
  781. return;
  782. reset(TOWN);
  783. currentObject = town;
  784. visibleInfo->loadTown(town);
  785. redraw();
  786. }
  787. void CInfoBar::showGameStatus()
  788. {
  789. reset(GAME);
  790. visibleInfo->loadGameStatus();
  791. setTimer(3000);
  792. redraw();
  793. }