AdventureMapClasses.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. #include "StdInc.h"
  2. #include "AdventureMapClasses.h"
  3. #include <SDL.h>
  4. #include "MiscWidgets.h"
  5. #include "CComponent.h"
  6. #include "../CGameInfo.h"
  7. #include "../CMusicHandler.h"
  8. #include "../CDefHandler.h"
  9. #include "../CPlayerInterface.h"
  10. #include "../CPreGame.h"
  11. #include "../Graphics.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/SDL_Pixels.h"
  14. #include "../windows/InfoWindows.h"
  15. #include "../windows/CAdvmapInterface.h"
  16. #include "../windows/GUIClasses.h"
  17. #include "../battle/CBattleInterfaceClasses.h"
  18. #include "../battle/CBattleInterface.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/StartInfo.h"
  21. #include "../../lib/CGameState.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. #include "../../lib/CHeroHandler.h"
  24. #include "../../lib/CModHandler.h"
  25. #include "../../lib/CTownHandler.h"
  26. #include "../../lib/filesystem/Filesystem.h"
  27. #include "../../lib/JsonNode.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapping/CMap.h"
  30. #include "../../lib/NetPacksBase.h"
  31. #include "../../lib/StringConstants.h"
  32. /*
  33. * CAdventureMapClasses.cpp, part of VCMI engine
  34. *
  35. * Authors: listed in file AUTHORS in main folder
  36. *
  37. * License: GNU General Public License v2.0 or later
  38. * Full text of license available in license.txt file, in main folder
  39. *
  40. */
  41. CList::CListItem::CListItem(CList * Parent):
  42. CIntObject(LCLICK | RCLICK | HOVER),
  43. parent(Parent),
  44. selection(nullptr)
  45. {
  46. }
  47. CList::CListItem::~CListItem()
  48. {
  49. // select() method in this was already destroyed so we can't safely call method in parent
  50. if (parent->selected == this)
  51. parent->selected = nullptr;
  52. }
  53. void CList::CListItem::clickRight(tribool down, bool previousState)
  54. {
  55. if (down == true)
  56. showTooltip();
  57. }
  58. void CList::CListItem::clickLeft(tribool down, bool previousState)
  59. {
  60. if (down == true)
  61. {
  62. //second click on already selected item
  63. if (parent->selected == this)
  64. open();
  65. else
  66. {
  67. //first click - switch selection
  68. parent->select(this);
  69. }
  70. }
  71. }
  72. void CList::CListItem::hover(bool on)
  73. {
  74. if (on)
  75. GH.statusbar->setText(getHoverText());
  76. else
  77. GH.statusbar->clear();
  78. }
  79. void CList::CListItem::onSelect(bool on)
  80. {
  81. OBJ_CONSTRUCTION_CAPTURING_ALL;
  82. vstd::clear_pointer(selection);
  83. if (on)
  84. selection = genSelection();
  85. select(on);
  86. GH.totalRedraw();
  87. }
  88. CList::CList(int Size, Point position, std::string btnUp, std::string btnDown, size_t listAmount,
  89. int helpUp, int helpDown, CListBox::CreateFunc create, CListBox::DestroyFunc destroy):
  90. CIntObject(0, position),
  91. size(Size),
  92. selected(nullptr)
  93. {
  94. OBJ_CONSTRUCTION_CAPTURING_ALL;
  95. scrollUp = new CButton(Point(0, 0), btnUp, CGI->generaltexth->zelp[helpUp]);
  96. list = new CListBox(create, destroy, Point(1,scrollUp->pos.h), Point(0, 32), size, listAmount);
  97. //assign callback only after list was created
  98. scrollUp->addCallback(std::bind(&CListBox::moveToPrev, list));
  99. scrollDown = new CButton(Point(0, scrollUp->pos.h + 32*size), btnDown, CGI->generaltexth->zelp[helpDown], std::bind(&CListBox::moveToNext, list));
  100. scrollDown->addCallback(std::bind(&CList::update, this));
  101. scrollUp->addCallback(std::bind(&CList::update, this));
  102. update();
  103. }
  104. void CList::update()
  105. {
  106. bool onTop = list->getPos() == 0;
  107. bool onBottom = list->getPos() + size >= list->size();
  108. scrollUp->block(onTop);
  109. scrollDown->block(onBottom);
  110. }
  111. void CList::select(CListItem *which)
  112. {
  113. if (selected == which)
  114. return;
  115. if (selected)
  116. selected->onSelect(false);
  117. selected = which;
  118. if (which)
  119. {
  120. which->onSelect(true);
  121. onSelect();
  122. }
  123. }
  124. int CList::getSelectedIndex()
  125. {
  126. return list->getIndexOf(selected);
  127. }
  128. void CList::selectIndex(int which)
  129. {
  130. if (which < 0)
  131. {
  132. if (selected)
  133. select(nullptr);
  134. }
  135. else
  136. {
  137. list->scrollTo(which);
  138. update();
  139. select(dynamic_cast<CListItem*>(list->getItem(which)));
  140. }
  141. }
  142. void CList::selectNext()
  143. {
  144. int index = getSelectedIndex();
  145. if (index < 0)
  146. selectIndex(0);
  147. else if (index + 1 < list->size())
  148. selectIndex(index+1);
  149. }
  150. void CList::selectPrev()
  151. {
  152. int index = getSelectedIndex();
  153. if (index <= 0)
  154. selectIndex(0);
  155. else
  156. selectIndex(index-1);
  157. }
  158. CHeroList::CEmptyHeroItem::CEmptyHeroItem()
  159. {
  160. OBJ_CONSTRUCTION_CAPTURING_ALL;
  161. auto move = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  162. auto img = new CPicture("HPSXXX", move->pos.w + 1);
  163. auto mana = new CAnimImage("IMANA", 0, 0, move->pos.w + img->pos.w + 2, 1 );
  164. pos.w = mana->pos.w + mana->pos.x - pos.x;
  165. pos.h = std::max(std::max<SDLX_Size>(move->pos.h + 1, mana->pos.h + 1), img->pos.h);
  166. }
  167. CHeroList::CHeroItem::CHeroItem(CHeroList *parent, const CGHeroInstance * Hero):
  168. CListItem(parent),
  169. hero(Hero)
  170. {
  171. OBJ_CONSTRUCTION_CAPTURING_ALL;
  172. movement = new CAnimImage("IMOBIL", 0, 0, 0, 1);
  173. portrait = new CAnimImage("PortraitsSmall", hero->portrait, 0, movement->pos.w + 1);
  174. mana = new CAnimImage("IMANA", 0, 0, movement->pos.w + portrait->pos.w + 2, 1 );
  175. pos.w = mana->pos.w + mana->pos.x - pos.x;
  176. pos.h = std::max(std::max<SDLX_Size>(movement->pos.h + 1, mana->pos.h + 1), portrait->pos.h);
  177. update();
  178. }
  179. void CHeroList::CHeroItem::update()
  180. {
  181. movement->setFrame(std::min<size_t>(movement->size()-1, hero->movement / 100));
  182. mana->setFrame(std::min<size_t>(mana->size()-1, hero->mana / 5));
  183. redraw();
  184. }
  185. CIntObject * CHeroList::CHeroItem::genSelection()
  186. {
  187. return new CPicture("HPSYYY", movement->pos.w + 1);
  188. }
  189. void CHeroList::CHeroItem::select(bool on)
  190. {
  191. if (on && adventureInt->selection != hero)
  192. adventureInt->select(hero);
  193. }
  194. void CHeroList::CHeroItem::open()
  195. {
  196. LOCPLINT->openHeroWindow(hero);
  197. }
  198. void CHeroList::CHeroItem::showTooltip()
  199. {
  200. CRClickPopup::createAndPush(hero, GH.current->motion);
  201. }
  202. std::string CHeroList::CHeroItem::getHoverText()
  203. {
  204. return boost::str(boost::format(CGI->generaltexth->allTexts[15]) % hero->name % hero->type->heroClass->name);
  205. }
  206. CIntObject * CHeroList::createHeroItem(size_t index)
  207. {
  208. if (LOCPLINT->wanderingHeroes.size() > index)
  209. return new CHeroItem(this, LOCPLINT->wanderingHeroes[index]);
  210. return new CEmptyHeroItem();
  211. }
  212. CHeroList::CHeroList(int size, Point position, std::string btnUp, std::string btnDown):
  213. CList(size, position, btnUp, btnDown, LOCPLINT->wanderingHeroes.size(), 303, 304, std::bind(&CHeroList::createHeroItem, this, _1))
  214. {
  215. }
  216. void CHeroList::select(const CGHeroInstance * hero)
  217. {
  218. selectIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  219. }
  220. void CHeroList::update(const CGHeroInstance * hero)
  221. {
  222. //this hero is already present, update its status
  223. for (auto & elem : list->getItems())
  224. {
  225. auto item = dynamic_cast<CHeroItem*>(elem);
  226. if (item && item->hero == hero && vstd::contains(LOCPLINT->wanderingHeroes, hero))
  227. {
  228. item->update();
  229. return;
  230. }
  231. }
  232. //simplest solution for now: reset list and restore selection
  233. list->resize(LOCPLINT->wanderingHeroes.size());
  234. if (adventureInt->selection)
  235. {
  236. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  237. if (hero)
  238. select(hero);
  239. }
  240. CList::update();
  241. }
  242. CIntObject * CTownList::createTownItem(size_t index)
  243. {
  244. if (LOCPLINT->towns.size() > index)
  245. return new CTownItem(this, LOCPLINT->towns[index]);
  246. return new CAnimImage("ITPA", 0);
  247. }
  248. CTownList::CTownItem::CTownItem(CTownList *parent, const CGTownInstance *Town):
  249. CListItem(parent),
  250. town(Town)
  251. {
  252. OBJ_CONSTRUCTION_CAPTURING_ALL;
  253. picture = new CAnimImage("ITPA", 0);
  254. pos = picture->pos;
  255. update();
  256. }
  257. CIntObject * CTownList::CTownItem::genSelection()
  258. {
  259. return new CAnimImage("ITPA", 1);
  260. }
  261. void CTownList::CTownItem::update()
  262. {
  263. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  264. picture->setFrame(iconIndex + 2);
  265. redraw();
  266. }
  267. void CTownList::CTownItem::select(bool on)
  268. {
  269. if (on && adventureInt->selection != town)
  270. adventureInt->select(town);
  271. }
  272. void CTownList::CTownItem::open()
  273. {
  274. LOCPLINT->openTownWindow(town);
  275. }
  276. void CTownList::CTownItem::showTooltip()
  277. {
  278. CRClickPopup::createAndPush(town, GH.current->motion);
  279. }
  280. std::string CTownList::CTownItem::getHoverText()
  281. {
  282. return town->getObjectName();
  283. }
  284. CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
  285. CList(size, position, btnUp, btnDown, LOCPLINT->towns.size(), 306, 307, std::bind(&CTownList::createTownItem, this, _1))
  286. {
  287. }
  288. void CTownList::select(const CGTownInstance * town)
  289. {
  290. selectIndex(vstd::find_pos(LOCPLINT->towns, town));
  291. }
  292. void CTownList::update(const CGTownInstance *)
  293. {
  294. //simplest solution for now: reset list and restore selection
  295. list->resize(LOCPLINT->towns.size());
  296. if (adventureInt->selection)
  297. {
  298. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  299. if (town)
  300. select(town);
  301. }
  302. CList::update();
  303. }
  304. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  305. {
  306. static const SDL_Color fogOfWar = {0, 0, 0, 255};
  307. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  308. // if tile is not visible it will be black on minimap
  309. if(!tile)
  310. return fogOfWar;
  311. // if object at tile is owned - it will be colored as its owner
  312. for (const CGObjectInstance *obj : tile->blockingObjects)
  313. {
  314. //heroes will be blitted later
  315. switch (obj->ID)
  316. {
  317. case Obj::HERO:
  318. case Obj::PRISON:
  319. continue;
  320. }
  321. PlayerColor player = obj->getOwner();
  322. if(player == PlayerColor::NEUTRAL)
  323. return *graphics->neutralColor;
  324. else
  325. if (player < PlayerColor::PLAYER_LIMIT)
  326. return graphics->playerColors[player.getNum()];
  327. }
  328. // else - use terrain color (blocked version or normal)
  329. if (tile->blocked && (!tile->visitable))
  330. return parent->colors.find(tile->terType)->second.second;
  331. else
  332. return parent->colors.find(tile->terType)->second.first;
  333. }
  334. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  335. {
  336. int3 mapSizes = LOCPLINT->cb->getMapSize();
  337. double stepX = double(pos.w) / mapSizes.x;
  338. double stepY = double(pos.h) / mapSizes.y;
  339. x = toX + stepX * tile.x;
  340. y = toY + stepY * tile.y;
  341. }
  342. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  343. {
  344. //coordinates of rectangle on minimap representing this tile
  345. // begin - first to blit, end - first NOT to blit
  346. int xBegin, yBegin, xEnd, yEnd;
  347. tileToPixels (tile, xBegin, yBegin, toX, toY);
  348. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  349. for (int y=yBegin; y<yEnd; y++)
  350. {
  351. Uint8 *ptr = (Uint8*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  352. for (int x=xBegin; x<xEnd; x++)
  353. ColorPutter<4, 1>::PutColor(ptr, color);
  354. }
  355. }
  356. void CMinimapInstance::refreshTile(const int3 &tile)
  357. {
  358. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  359. }
  360. void CMinimapInstance::drawScaled(int level)
  361. {
  362. int3 mapSizes = LOCPLINT->cb->getMapSize();
  363. //size of one map tile on our minimap
  364. double stepX = double(pos.w) / mapSizes.x;
  365. double stepY = double(pos.h) / mapSizes.y;
  366. double currY = 0;
  367. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  368. {
  369. double currX = 0;
  370. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  371. {
  372. const SDL_Color &color = getTileColor(int3(x,y,level));
  373. //coordinates of rectangle on minimap representing this tile
  374. // begin - first to blit, end - first NOT to blit
  375. int xBegin = currX;
  376. int yBegin = currY;
  377. int xEnd = currX + stepX;
  378. int yEnd = currY + stepY;
  379. for (int y=yBegin; y<yEnd; y++)
  380. {
  381. Uint8 *ptr = (Uint8*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  382. for (int x=xBegin; x<xEnd; x++)
  383. ColorPutter<4, 1>::PutColor(ptr, color);
  384. }
  385. }
  386. }
  387. }
  388. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  389. parent(Parent),
  390. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  391. level(Level)
  392. {
  393. pos.w = parent->pos.w;
  394. pos.h = parent->pos.h;
  395. drawScaled(level);
  396. }
  397. CMinimapInstance::~CMinimapInstance()
  398. {
  399. SDL_FreeSurface(minimap);
  400. }
  401. void CMinimapInstance::showAll(SDL_Surface *to)
  402. {
  403. blitAtLoc(minimap, 0, 0, to);
  404. //draw heroes
  405. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  406. for(auto & hero : heroes)
  407. {
  408. int3 position = hero->getPosition(false);
  409. if (position.z == level)
  410. {
  411. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  412. blitTileWithColor(color, position, to, pos.x, pos.y);
  413. }
  414. }
  415. }
  416. std::map<int, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors(std::string from)
  417. {
  418. std::map<int, std::pair<SDL_Color, SDL_Color> > ret;
  419. const JsonNode config(ResourceID(from, EResType::TEXT));
  420. for(auto &m : config.Struct())
  421. {
  422. auto index = boost::find(GameConstants::TERRAIN_NAMES, m.first);
  423. if (index == std::end(GameConstants::TERRAIN_NAMES))
  424. {
  425. logGlobal->errorStream() << "Error: unknown terrain in terrains.json: " << m.first;
  426. continue;
  427. }
  428. int terrainID = index - std::begin(GameConstants::TERRAIN_NAMES);
  429. const JsonVector &unblockedVec = m.second["minimapUnblocked"].Vector();
  430. SDL_Color normal =
  431. {
  432. ui8(unblockedVec[0].Float()),
  433. ui8(unblockedVec[1].Float()),
  434. ui8(unblockedVec[2].Float()),
  435. ui8(255)
  436. };
  437. const JsonVector &blockedVec = m.second["minimapBlocked"].Vector();
  438. SDL_Color blocked =
  439. {
  440. ui8(blockedVec[0].Float()),
  441. ui8(blockedVec[1].Float()),
  442. ui8(blockedVec[2].Float()),
  443. ui8(255)
  444. };
  445. ret.insert(std::make_pair(terrainID, std::make_pair(normal, blocked)));
  446. }
  447. return ret;
  448. }
  449. CMinimap::CMinimap(const Rect &position):
  450. CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  451. aiShield(nullptr),
  452. minimap(nullptr),
  453. level(0),
  454. colors(loadColors("config/terrains.json"))
  455. {
  456. pos.w = position.w;
  457. pos.h = position.h;
  458. }
  459. int3 CMinimap::translateMousePosition()
  460. {
  461. // 0 = top-left corner, 1 = bottom-right corner
  462. double dx = double(GH.current->motion.x - pos.x) / pos.w;
  463. double dy = double(GH.current->motion.y - pos.y) / pos.h;
  464. int3 mapSizes = LOCPLINT->cb->getMapSize();
  465. int3 tile (mapSizes.x * dx, mapSizes.y * dy, level);
  466. return tile;
  467. }
  468. void CMinimap::moveAdvMapSelection()
  469. {
  470. int3 newLocation = translateMousePosition();
  471. adventureInt->centerOn(newLocation);
  472. if (!(adventureInt->active & GENERAL))
  473. GH.totalRedraw(); //redraw this as well as inactive adventure map
  474. else
  475. redraw();//redraw only this
  476. }
  477. void CMinimap::clickLeft(tribool down, bool previousState)
  478. {
  479. if (down)
  480. moveAdvMapSelection();
  481. }
  482. void CMinimap::clickRight(tribool down, bool previousState)
  483. {
  484. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  485. }
  486. void CMinimap::hover(bool on)
  487. {
  488. if (on)
  489. GH.statusbar->setText(CGI->generaltexth->zelp[291].first);
  490. else
  491. GH.statusbar->clear();
  492. }
  493. void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  494. {
  495. if (pressedL)
  496. moveAdvMapSelection();
  497. }
  498. void CMinimap::showAll(SDL_Surface * to)
  499. {
  500. CIntObject::showAll(to);
  501. if (minimap)
  502. {
  503. int3 mapSizes = LOCPLINT->cb->getMapSize();
  504. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  505. //draw radar
  506. SDL_Rect oldClip;
  507. SDL_Rect radar =
  508. {
  509. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  510. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  511. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  512. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  513. };
  514. SDL_GetClipRect(to, &oldClip);
  515. SDL_SetClipRect(to, &pos);
  516. CSDL_Ext::drawDashedBorder(to, radar, int3(255,75,125));
  517. SDL_SetClipRect(to, &oldClip);
  518. }
  519. }
  520. void CMinimap::update()
  521. {
  522. if (aiShield) //AI turn is going on. There is no need to update minimap
  523. return;
  524. OBJ_CONSTRUCTION_CAPTURING_ALL;
  525. vstd::clear_pointer(minimap);
  526. minimap = new CMinimapInstance(this, level);
  527. redraw();
  528. }
  529. void CMinimap::setLevel(int newLevel)
  530. {
  531. level = newLevel;
  532. update();
  533. }
  534. void CMinimap::setAIRadar(bool on)
  535. {
  536. if (on)
  537. {
  538. OBJ_CONSTRUCTION_CAPTURING_ALL;
  539. vstd::clear_pointer(minimap);
  540. if (!aiShield)
  541. aiShield = new CPicture("AIShield");
  542. }
  543. else
  544. {
  545. vstd::clear_pointer(aiShield);
  546. update();
  547. }
  548. // this my happen during AI turn when this interface is inactive
  549. // force redraw in order to properly update interface
  550. GH.totalRedraw();
  551. }
  552. void CMinimap::hideTile(const int3 &pos)
  553. {
  554. if (minimap)
  555. minimap->refreshTile(pos);
  556. }
  557. void CMinimap::showTile(const int3 &pos)
  558. {
  559. if (minimap)
  560. minimap->refreshTile(pos);
  561. }
  562. CInfoBar::CVisibleInfo::CVisibleInfo(Point position):
  563. CIntObject(0, position),
  564. aiProgress(nullptr)
  565. {
  566. }
  567. void CInfoBar::CVisibleInfo::show(SDL_Surface *to)
  568. {
  569. CIntObject::show(to);
  570. for(auto object : forceRefresh)
  571. object->showAll(to);
  572. }
  573. void CInfoBar::CVisibleInfo::loadHero(const CGHeroInstance * hero)
  574. {
  575. assert(children.empty()); // visible info should be re-created to change type
  576. OBJ_CONSTRUCTION_CAPTURING_ALL;
  577. new CPicture("ADSTATHR");
  578. new CHeroTooltip(Point(0,0), hero);
  579. }
  580. void CInfoBar::CVisibleInfo::loadTown(const CGTownInstance *town)
  581. {
  582. assert(children.empty()); // visible info should be re-created to change type
  583. OBJ_CONSTRUCTION_CAPTURING_ALL;
  584. new CPicture("ADSTATCS");
  585. new CTownTooltip(Point(0,0), town);
  586. }
  587. void CInfoBar::CVisibleInfo::playNewDaySound()
  588. {
  589. if (LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
  590. CCS->soundh->playSound(soundBase::newDay);
  591. else
  592. if (LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
  593. CCS->soundh->playSound(soundBase::newWeek);
  594. else
  595. if (LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
  596. CCS->soundh->playSound(soundBase::newMonth);
  597. else
  598. CCS->soundh->playSound(soundBase::newDay);
  599. }
  600. std::string CInfoBar::CVisibleInfo::getNewDayName()
  601. {
  602. if (LOCPLINT->cb->getDate(Date::DAY) == 1)
  603. return "NEWDAY";
  604. if (LOCPLINT->cb->getDate(Date::DAY) != 1)
  605. return "NEWDAY";
  606. switch(LOCPLINT->cb->getDate(Date::WEEK))
  607. {
  608. case 1: return "NEWWEEK1";
  609. case 2: return "NEWWEEK2";
  610. case 3: return "NEWWEEK3";
  611. case 4: return "NEWWEEK4";
  612. default: assert(0); return "";
  613. }
  614. }
  615. void CInfoBar::CVisibleInfo::loadDay()
  616. {
  617. assert(children.empty()); // visible info should be re-created first to change type
  618. playNewDaySound();
  619. OBJ_CONSTRUCTION_CAPTURING_ALL;
  620. new CShowableAnim(1, 0, getNewDayName(), CShowableAnim::PLAY_ONCE);
  621. std::string labelText;
  622. if (LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) == 1 && LOCPLINT->cb->getDate(Date::DAY) != 1) // monday of any week but first - show new week info
  623. labelText = CGI->generaltexth->allTexts[63] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK));
  624. else
  625. labelText = CGI->generaltexth->allTexts[64] + " " + boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK));
  626. forceRefresh.push_back(new CLabel(95, 31, FONT_MEDIUM, CENTER, Colors::WHITE, labelText));
  627. }
  628. void CInfoBar::CVisibleInfo::loadEnemyTurn(PlayerColor player)
  629. {
  630. assert(children.empty()); // visible info should be re-created to change type
  631. OBJ_CONSTRUCTION_CAPTURING_ALL;
  632. new CPicture("ADSTATNX");
  633. new CAnimImage("CREST58", player.getNum(), 0, 20, 51);
  634. new CShowableAnim(99, 51, "HOURSAND");
  635. // FIXME: currently there is no way to get progress from VCAI
  636. // if this will change at some point switch this ifdef to enable correct code
  637. #if 0
  638. //prepare hourglass for updating AI turn
  639. aiProgress = new CAnimImage("HOURGLAS", 0, 0, 99, 51);
  640. forceRefresh.push_back(aiProgress);
  641. #else
  642. //create hourglass that will be always animated ignoring AI status
  643. new CShowableAnim(99, 51, "HOURGLAS", CShowableAnim::PLAY_ONCE, 40);
  644. #endif
  645. }
  646. void CInfoBar::CVisibleInfo::loadGameStatus()
  647. {
  648. assert(children.empty()); // visible info should be re-created to change type
  649. //get amount of halls of each level
  650. std::vector<int> halls(4, 0);
  651. for(auto town : LOCPLINT->towns)
  652. halls[town->hallLevel()]++;
  653. std::vector<PlayerColor> allies, enemies;
  654. //generate list of allies and enemies
  655. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  656. {
  657. if(LOCPLINT->cb->getPlayerStatus(PlayerColor(i), false) == EPlayerStatus::INGAME)
  658. {
  659. if (LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, PlayerColor(i)) != PlayerRelations::ENEMIES)
  660. allies.push_back(PlayerColor(i));
  661. else
  662. enemies.push_back(PlayerColor(i));
  663. }
  664. }
  665. //generate component
  666. OBJ_CONSTRUCTION_CAPTURING_ALL;
  667. new CPicture("ADSTATIN");
  668. auto allyLabel = new CLabel(10, 106, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  669. auto enemyLabel = new CLabel(10, 136, FONT_SMALL, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  670. int posx = allyLabel->pos.w + allyLabel->pos.x - pos.x + 4;
  671. for(PlayerColor & player : allies)
  672. {
  673. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 102);
  674. posx += image->pos.w;
  675. }
  676. posx = enemyLabel->pos.w + enemyLabel->pos.x - pos.x + 4;
  677. for(PlayerColor & player : enemies)
  678. {
  679. auto image = new CAnimImage("ITGFLAGS", player.getNum(), 0, posx, 132);
  680. posx += image->pos.w;
  681. }
  682. for (size_t i=0; i<halls.size(); i++)
  683. {
  684. new CAnimImage("itmtl", i, 0, 6 + 42 * i , 11);
  685. if (halls[i])
  686. new CLabel( 26 + 42 * i, 64, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(halls[i]));
  687. }
  688. }
  689. void CInfoBar::CVisibleInfo::loadComponent(const Component & compToDisplay, std::string message)
  690. {
  691. assert(children.empty()); // visible info should be re-created to change type
  692. OBJ_CONSTRUCTION_CAPTURING_ALL;
  693. new CPicture("ADSTATOT", 1);
  694. auto comp = new CComponent(compToDisplay);
  695. comp->moveTo(Point(pos.x+47, pos.y+50));
  696. new CTextBox(message, Rect(10, 4, 160, 50), 0, FONT_SMALL, CENTER, Colors::WHITE);
  697. }
  698. void CInfoBar::CVisibleInfo::updateEnemyTurn(double progress)
  699. {
  700. if (aiProgress)
  701. aiProgress->setFrame((aiProgress->size() - 1) * progress);
  702. }
  703. void CInfoBar::reset(EState newState = EMPTY)
  704. {
  705. OBJ_CONSTRUCTION_CAPTURING_ALL;
  706. vstd::clear_pointer(visibleInfo);
  707. currentObject = nullptr;
  708. state = newState;
  709. visibleInfo = new CVisibleInfo(Point(8, 12));
  710. }
  711. void CInfoBar::showSelection()
  712. {
  713. if (adventureInt->selection)
  714. {
  715. auto hero = dynamic_cast<const CGHeroInstance *>(adventureInt->selection);
  716. if (hero)
  717. {
  718. showHeroSelection(hero);
  719. return;
  720. }
  721. auto town = dynamic_cast<const CGTownInstance *>(adventureInt->selection);
  722. if (town)
  723. {
  724. showTownSelection(town);
  725. return;
  726. }
  727. }
  728. showGameStatus();//FIXME: may be incorrect but shouldn't happen in general
  729. }
  730. void CInfoBar::tick()
  731. {
  732. removeUsedEvents(TIME);
  733. showSelection();
  734. }
  735. void CInfoBar::clickLeft(tribool down, bool previousState)
  736. {
  737. if (down)
  738. {
  739. if (state == HERO || state == TOWN)
  740. showGameStatus();
  741. else if (state == GAME)
  742. showDate();
  743. else
  744. showSelection();
  745. }
  746. }
  747. void CInfoBar::clickRight(tribool down, bool previousState)
  748. {
  749. adventureInt->handleRightClick(CGI->generaltexth->allTexts[109], down);
  750. }
  751. void CInfoBar::hover(bool on)
  752. {
  753. if (on)
  754. GH.statusbar->setText(CGI->generaltexth->zelp[292].first);
  755. else
  756. GH.statusbar->clear();
  757. }
  758. CInfoBar::CInfoBar(const Rect &position):
  759. CIntObject(LCLICK | RCLICK | HOVER, position.topLeft()),
  760. visibleInfo(nullptr),
  761. state(EMPTY),
  762. currentObject(nullptr)
  763. {
  764. pos.w = position.w;
  765. pos.h = position.h;
  766. //FIXME: enable some mode? Should be done by advMap::select() when game starts but just in case?
  767. }
  768. void CInfoBar::showDate()
  769. {
  770. reset(DATE);
  771. visibleInfo->loadDay();
  772. setTimer(3000);
  773. redraw();
  774. }
  775. void CInfoBar::showComponent(const Component & comp, std::string message)
  776. {
  777. reset(COMPONENT);
  778. visibleInfo->loadComponent(comp, message);
  779. setTimer(3000);
  780. redraw();
  781. }
  782. void CInfoBar::startEnemyTurn(PlayerColor color)
  783. {
  784. reset(AITURN);
  785. visibleInfo->loadEnemyTurn(color);
  786. redraw();
  787. }
  788. void CInfoBar::updateEnemyTurn(double progress)
  789. {
  790. assert(state == AITURN);
  791. visibleInfo->updateEnemyTurn(progress);
  792. redraw();
  793. }
  794. void CInfoBar::showHeroSelection(const CGHeroInstance * hero)
  795. {
  796. if (!hero)
  797. return;
  798. reset(HERO);
  799. currentObject = hero;
  800. visibleInfo->loadHero(hero);
  801. redraw();
  802. }
  803. void CInfoBar::showTownSelection(const CGTownInstance * town)
  804. {
  805. if (!town)
  806. return;
  807. reset(TOWN);
  808. currentObject = town;
  809. visibleInfo->loadTown(town);
  810. redraw();
  811. }
  812. void CInfoBar::showGameStatus()
  813. {
  814. reset(GAME);
  815. visibleInfo->loadGameStatus();
  816. setTimer(3000);
  817. redraw();
  818. }
  819. void CInGameConsole::show(SDL_Surface * to)
  820. {
  821. int number = 0;
  822. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  823. boost::unique_lock<boost::mutex> lock(texts_mx);
  824. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  825. {
  826. Point leftBottomCorner(0, screen->h);
  827. if(LOCPLINT->battleInt)
  828. {
  829. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  830. }
  831. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  832. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  833. if(SDL_GetTicks() - it->second > defaultTimeout)
  834. {
  835. toDel.push_back(it);
  836. }
  837. }
  838. for(auto & elem : toDel)
  839. {
  840. texts.erase(elem);
  841. }
  842. }
  843. void CInGameConsole::print(const std::string &txt)
  844. {
  845. boost::unique_lock<boost::mutex> lock(texts_mx);
  846. int lineLen = conf.go()->ac.outputLineLength;
  847. if(txt.size() < lineLen)
  848. {
  849. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  850. if(texts.size() > maxDisplayedTexts)
  851. {
  852. texts.pop_front();
  853. }
  854. }
  855. else
  856. {
  857. assert(lineLen);
  858. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  859. {
  860. std::string part = txt.substr(g * lineLen, lineLen);
  861. if(part.size() == 0)
  862. break;
  863. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  864. if(texts.size() > maxDisplayedTexts)
  865. {
  866. texts.pop_front();
  867. }
  868. }
  869. }
  870. }
  871. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  872. {
  873. if(key.type != SDL_KEYDOWN) return;
  874. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  875. switch(key.keysym.sym)
  876. {
  877. case SDLK_TAB:
  878. case SDLK_ESCAPE:
  879. {
  880. if(captureAllKeys)
  881. {
  882. captureAllKeys = false;
  883. endEnteringText(false);
  884. }
  885. else if(SDLK_TAB)
  886. {
  887. captureAllKeys = true;
  888. startEnteringText();
  889. }
  890. break;
  891. }
  892. case SDLK_RETURN: //enter key
  893. {
  894. if(enteredText.size() > 0 && captureAllKeys)
  895. {
  896. captureAllKeys = false;
  897. endEnteringText(true);
  898. CCS->soundh->playSound("CHAT");
  899. }
  900. break;
  901. }
  902. case SDLK_BACKSPACE:
  903. {
  904. if(enteredText.size() > 1)
  905. {
  906. Unicode::trimRight(enteredText,2);
  907. enteredText += '_';
  908. refreshEnteredText();
  909. }
  910. break;
  911. }
  912. case SDLK_UP: //up arrow
  913. {
  914. if(previouslyEntered.size() == 0)
  915. break;
  916. if(prevEntDisp == -1)
  917. {
  918. prevEntDisp = previouslyEntered.size() - 1;
  919. enteredText = previouslyEntered[prevEntDisp] + "_";
  920. refreshEnteredText();
  921. }
  922. else if( prevEntDisp > 0)
  923. {
  924. --prevEntDisp;
  925. enteredText = previouslyEntered[prevEntDisp] + "_";
  926. refreshEnteredText();
  927. }
  928. break;
  929. }
  930. case SDLK_DOWN: //down arrow
  931. {
  932. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  933. {
  934. ++prevEntDisp;
  935. enteredText = previouslyEntered[prevEntDisp] + "_";
  936. refreshEnteredText();
  937. }
  938. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  939. {
  940. prevEntDisp = -1;
  941. enteredText = "_";
  942. refreshEnteredText();
  943. }
  944. break;
  945. }
  946. default:
  947. {
  948. #ifdef VCMI_SDL1
  949. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  950. {
  951. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  952. {
  953. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  954. enteredText += "_";
  955. refreshEnteredText();
  956. }
  957. }
  958. #endif // VCMI_SDL1
  959. break;
  960. }
  961. }
  962. }
  963. #ifndef VCMI_SDL1
  964. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  965. {
  966. if(!captureAllKeys || enteredText.size() == 0)
  967. return;
  968. enteredText.resize(enteredText.size()-1);
  969. enteredText += event.text;
  970. enteredText += "_";
  971. refreshEnteredText();
  972. }
  973. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  974. {
  975. //do nothing here
  976. }
  977. #endif // VCMI_SDL1
  978. void CInGameConsole::startEnteringText()
  979. {
  980. CSDL_Ext::startTextInput(&pos);
  981. enteredText = "_";
  982. if(GH.topInt() == adventureInt)
  983. {
  984. GH.statusbar->alignment = TOPLEFT;
  985. GH.statusbar->setText(enteredText);
  986. //Prevent changes to the text from mouse interaction with the adventure map
  987. GH.statusbar->lock(true);
  988. }
  989. else if(LOCPLINT->battleInt)
  990. {
  991. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  992. }
  993. }
  994. void CInGameConsole::endEnteringText(bool printEnteredText)
  995. {
  996. CSDL_Ext::stopTextInput();
  997. prevEntDisp = -1;
  998. if(printEnteredText)
  999. {
  1000. std::string txt = enteredText.substr(0, enteredText.size()-1);
  1001. LOCPLINT->cb->sendMessage(txt, LOCPLINT->getSelection());
  1002. previouslyEntered.push_back(txt);
  1003. //print(txt);
  1004. }
  1005. enteredText = "";
  1006. if(GH.topInt() == adventureInt)
  1007. {
  1008. GH.statusbar->alignment = CENTER;
  1009. GH.statusbar->lock(false);
  1010. GH.statusbar->clear();
  1011. }
  1012. else if(LOCPLINT->battleInt)
  1013. {
  1014. LOCPLINT->battleInt->console->ingcAlter = "";
  1015. }
  1016. }
  1017. void CInGameConsole::refreshEnteredText()
  1018. {
  1019. if(GH.topInt() == adventureInt)
  1020. {
  1021. GH.statusbar->lock(false);
  1022. GH.statusbar->clear();
  1023. GH.statusbar->setText(enteredText);
  1024. GH.statusbar->lock(true);
  1025. }
  1026. else if(LOCPLINT->battleInt)
  1027. {
  1028. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  1029. }
  1030. }
  1031. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  1032. {
  1033. #ifdef VCMI_SDL1
  1034. addUsedEvents(KEYBOARD);
  1035. #else
  1036. addUsedEvents(KEYBOARD | TEXTINPUT);
  1037. #endif
  1038. }
  1039. CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position)
  1040. : CIntObject(),
  1041. background(bg)
  1042. {
  1043. defActions = 255;
  1044. recActions = 255;
  1045. pos.x += position.x;
  1046. pos.y += position.y;
  1047. }
  1048. CAdvMapPanel::~CAdvMapPanel()
  1049. {
  1050. if (background)
  1051. SDL_FreeSurface(background);
  1052. }
  1053. void CAdvMapPanel::addChildColorableButton(CButton * btn)
  1054. {
  1055. buttons.push_back(btn);
  1056. addChildToPanel(btn, ACTIVATE | DEACTIVATE);
  1057. }
  1058. void CAdvMapPanel::setPlayerColor(const PlayerColor & clr)
  1059. {
  1060. for (auto &btn : buttons)
  1061. {
  1062. btn->setPlayerColor(clr);
  1063. }
  1064. }
  1065. void CAdvMapPanel::showAll(SDL_Surface * to)
  1066. {
  1067. if (background)
  1068. blitAt(background, pos.x, pos.y, to);
  1069. CIntObject::showAll(to);
  1070. }
  1071. void CAdvMapPanel::addChildToPanel(CIntObject * obj, ui8 actions /* = 0 */)
  1072. {
  1073. obj->recActions |= actions | SHOWALL;
  1074. addChild(obj, false);
  1075. }
  1076. CAdvMapWorldViewPanel::CAdvMapWorldViewPanel(SDL_Surface * bg, Point position)
  1077. : CAdvMapPanel(bg, position)
  1078. {
  1079. }
  1080. void CAdvMapWorldViewPanel::recolorIcons(const CDefHandler *def, int indexOffset)
  1081. {
  1082. for (auto &pic : currentIcons)
  1083. {
  1084. removeChild(pic);
  1085. delete pic;
  1086. }
  1087. currentIcons.clear();
  1088. for (auto &data : iconsData)
  1089. {
  1090. auto pic = new CPicture(def->ourImages[data.first + indexOffset].bitmap, data.second.x, data.second.y, false);
  1091. pic->recActions |= SHOWALL;
  1092. currentIcons.push_back(pic);
  1093. addChildToPanel(pic);
  1094. }
  1095. }
  1096. void CAdvMapWorldViewPanel::addChildIcon(std::pair<int, Point> data, const CDefHandler *def, int indexOffset)
  1097. {
  1098. iconsData.push_back(data);
  1099. auto pic = new CPicture(def->ourImages[data.first + indexOffset].bitmap, data.second.x, data.second.y, false);
  1100. currentIcons.push_back(pic);
  1101. addChildToPanel(pic);
  1102. }