BattleFieldController.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * BattleFieldController.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleFieldController.h"
  12. #include "BattleInterface.h"
  13. #include "BattleActionsController.h"
  14. #include "BattleInterfaceClasses.h"
  15. #include "BattleEffectsController.h"
  16. #include "BattleSiegeController.h"
  17. #include "BattleStacksController.h"
  18. #include "BattleObstacleController.h"
  19. #include "BattleProjectileController.h"
  20. #include "BattleRenderer.h"
  21. #include "../CGameInfo.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../render/CAnimation.h"
  24. #include "../render/Canvas.h"
  25. #include "../render/IImage.h"
  26. #include "../renderSDL/SDL_Extensions.h"
  27. #include "../gui/CGuiHandler.h"
  28. #include "../gui/CursorHandler.h"
  29. #include "../adventureMap/CInGameConsole.h"
  30. #include "../../CCallback.h"
  31. #include "../../lib/BattleFieldHandler.h"
  32. #include "../../lib/CConfigHandler.h"
  33. #include "../../lib/CStack.h"
  34. #include "../../lib/spells/ISpellMechanics.h"
  35. BattleFieldController::BattleFieldController(BattleInterface & owner):
  36. owner(owner)
  37. {
  38. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  39. //preparing cells and hexes
  40. cellBorder = IImage::createFromFile("CCELLGRD.BMP", EImageBlitMode::COLORKEY);
  41. cellShade = IImage::createFromFile("CCELLSHD.BMP");
  42. cellUnitMovementHighlight = IImage::createFromFile("UnitMovementHighlight.PNG", EImageBlitMode::COLORKEY);
  43. cellUnitMaxMovementHighlight = IImage::createFromFile("UnitMaxMovementHighlight.PNG", EImageBlitMode::COLORKEY);
  44. attackCursors = std::make_shared<CAnimation>("CRCOMBAT");
  45. attackCursors->preload();
  46. if(!owner.siegeController)
  47. {
  48. auto bfieldType = owner.curInt->cb->battleGetBattlefieldType();
  49. if(bfieldType == BattleField::NONE)
  50. logGlobal->error("Invalid battlefield returned for current battle");
  51. else
  52. background = IImage::createFromFile(bfieldType.getInfo()->graphics, EImageBlitMode::OPAQUE);
  53. }
  54. else
  55. {
  56. std::string backgroundName = owner.siegeController->getBattleBackgroundName();
  57. background = IImage::createFromFile(backgroundName, EImageBlitMode::OPAQUE);
  58. }
  59. pos.w = background->width();
  60. pos.h = background->height();
  61. backgroundWithHexes = std::make_unique<Canvas>(Point(background->width(), background->height()));
  62. updateAccessibleHexes();
  63. addUsedEvents(LCLICK | RCLICK | MOVE | TIME | GESTURE_PANNING);
  64. }
  65. void BattleFieldController::activate()
  66. {
  67. LOCPLINT->cingconsole->pos = this->pos;
  68. CIntObject::activate();
  69. }
  70. void BattleFieldController::createHeroes()
  71. {
  72. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  73. // create heroes as part of our constructor for correct positioning inside battlefield
  74. if(owner.attackingHeroInstance)
  75. owner.attackingHero = std::make_shared<BattleHero>(owner, owner.attackingHeroInstance, false);
  76. if(owner.defendingHeroInstance)
  77. owner.defendingHero = std::make_shared<BattleHero>(owner, owner.defendingHeroInstance, true);
  78. }
  79. void BattleFieldController::panning(bool on, const Point & initialPosition, const Point & finalPosition)
  80. {
  81. if (!on && pos.isInside(finalPosition))
  82. clickLeft(false, false);
  83. }
  84. void BattleFieldController::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  85. {
  86. hoveredHex = getHexAtPosition(initialPosition);
  87. currentAttackOriginPoint = currentPosition;
  88. if (pos.isInside(initialPosition))
  89. owner.actionsController->onHexHovered(getHoveredHex());
  90. }
  91. void BattleFieldController::mouseMoved(const Point & cursorPosition)
  92. {
  93. hoveredHex = getHexAtPosition(cursorPosition);
  94. currentAttackOriginPoint = cursorPosition;
  95. if (pos.isInside(cursorPosition))
  96. owner.actionsController->onHexHovered(getHoveredHex());
  97. else
  98. owner.actionsController->onHoverEnded();
  99. }
  100. void BattleFieldController::clickLeft(tribool down, bool previousState)
  101. {
  102. if(!down)
  103. {
  104. BattleHex selectedHex = getHoveredHex();
  105. if (selectedHex != BattleHex::INVALID)
  106. owner.actionsController->onHexLeftClicked(selectedHex);
  107. }
  108. }
  109. void BattleFieldController::clickRight(tribool down, bool previousState)
  110. {
  111. if(down)
  112. {
  113. BattleHex selectedHex = getHoveredHex();
  114. if (selectedHex != BattleHex::INVALID)
  115. owner.actionsController->onHexRightClicked(selectedHex);
  116. }
  117. }
  118. void BattleFieldController::renderBattlefield(Canvas & canvas)
  119. {
  120. Canvas clippedCanvas(canvas, pos);
  121. showBackground(clippedCanvas);
  122. BattleRenderer renderer(owner);
  123. renderer.execute(clippedCanvas);
  124. owner.projectilesController->render(clippedCanvas);
  125. }
  126. void BattleFieldController::showBackground(Canvas & canvas)
  127. {
  128. if (owner.stacksController->getActiveStack() != nullptr )
  129. showBackgroundImageWithHexes(canvas);
  130. else
  131. showBackgroundImage(canvas);
  132. showHighlightedHexes(canvas);
  133. }
  134. void BattleFieldController::showBackgroundImage(Canvas & canvas)
  135. {
  136. canvas.draw(background, Point(0, 0));
  137. owner.obstacleController->showAbsoluteObstacles(canvas);
  138. if ( owner.siegeController )
  139. owner.siegeController->showAbsoluteObstacles(canvas);
  140. if (settings["battle"]["cellBorders"].Bool())
  141. {
  142. for (int i=0; i<GameConstants::BFIELD_SIZE; ++i)
  143. {
  144. if ( i % GameConstants::BFIELD_WIDTH == 0)
  145. continue;
  146. if ( i % GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_WIDTH - 1)
  147. continue;
  148. canvas.draw(cellBorder, hexPositionLocal(i).topLeft());
  149. }
  150. }
  151. }
  152. void BattleFieldController::showBackgroundImageWithHexes(Canvas & canvas)
  153. {
  154. canvas.draw(*backgroundWithHexes, Point(0, 0));
  155. }
  156. void BattleFieldController::redrawBackgroundWithHexes()
  157. {
  158. const CStack *activeStack = owner.stacksController->getActiveStack();
  159. std::vector<BattleHex> attackableHexes;
  160. if(activeStack)
  161. occupiableHexes = owner.curInt->cb->battleGetAvailableHexes(activeStack, false, true, &attackableHexes);
  162. // prepare background graphic with hexes and shaded hexes
  163. backgroundWithHexes->draw(background, Point(0,0));
  164. owner.obstacleController->showAbsoluteObstacles(*backgroundWithHexes);
  165. if(owner.siegeController)
  166. owner.siegeController->showAbsoluteObstacles(*backgroundWithHexes);
  167. // show shaded hexes for active's stack valid movement and the hexes that it can attack
  168. if(settings["battle"]["stackRange"].Bool())
  169. {
  170. std::vector<BattleHex> hexesToShade = occupiableHexes;
  171. hexesToShade.insert(hexesToShade.end(), attackableHexes.begin(), attackableHexes.end());
  172. for(BattleHex hex : hexesToShade)
  173. {
  174. showHighlightedHex(*backgroundWithHexes, cellShade, hex, false);
  175. }
  176. }
  177. // draw cell borders
  178. if(settings["battle"]["cellBorders"].Bool())
  179. {
  180. for(int i=0; i<GameConstants::BFIELD_SIZE; ++i)
  181. {
  182. if(i % GameConstants::BFIELD_WIDTH == 0)
  183. continue;
  184. if(i % GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_WIDTH - 1)
  185. continue;
  186. backgroundWithHexes->draw(cellBorder, hexPositionLocal(i).topLeft());
  187. }
  188. }
  189. }
  190. void BattleFieldController::showHighlightedHex(Canvas & canvas, std::shared_ptr<IImage> highlight, BattleHex hex, bool darkBorder)
  191. {
  192. Point hexPos = hexPositionLocal(hex).topLeft();
  193. canvas.draw(highlight, hexPos);
  194. if(!darkBorder && settings["battle"]["cellBorders"].Bool())
  195. canvas.draw(cellBorder, hexPos);
  196. }
  197. std::set<BattleHex> BattleFieldController::getHighlightedHexesForActiveStack()
  198. {
  199. std::set<BattleHex> result;
  200. if(!owner.stacksController->getActiveStack())
  201. return result;
  202. if(!settings["battle"]["stackRange"].Bool())
  203. return result;
  204. auto hoveredHex = getHoveredHex();
  205. std::set<BattleHex> set = owner.curInt->cb->battleGetAttackedHexes(owner.stacksController->getActiveStack(), hoveredHex);
  206. for(BattleHex hex : set)
  207. result.insert(hex);
  208. return result;
  209. }
  210. std::set<BattleHex> BattleFieldController::getMovementRangeForHoveredStack()
  211. {
  212. std::set<BattleHex> result;
  213. if (!owner.stacksController->getActiveStack())
  214. return result;
  215. if (!settings["battle"]["movementHighlightOnHover"].Bool() && !GH.isKeyboardShiftDown())
  216. return result;
  217. auto hoveredHex = getHoveredHex();
  218. // add possible movement hexes for stack under mouse
  219. const CStack * const hoveredStack = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  220. if(hoveredStack)
  221. {
  222. std::vector<BattleHex> v = owner.curInt->cb->battleGetAvailableHexes(hoveredStack, true, true, nullptr);
  223. for(BattleHex hex : v)
  224. result.insert(hex);
  225. }
  226. return result;
  227. }
  228. std::set<BattleHex> BattleFieldController::getHighlightedHexesForSpellRange()
  229. {
  230. std::set<BattleHex> result;
  231. auto hoveredHex = getHoveredHex();
  232. if(!settings["battle"]["mouseShadow"].Bool())
  233. return result;
  234. const spells::Caster *caster = nullptr;
  235. const CSpell *spell = nullptr;
  236. spells::Mode mode = owner.actionsController->getCurrentCastMode();
  237. spell = owner.actionsController->getCurrentSpell(hoveredHex);
  238. caster = owner.actionsController->getCurrentSpellcaster();
  239. if(caster && spell) //when casting spell
  240. {
  241. // printing shaded hex(es)
  242. spells::BattleCast event(owner.curInt->cb.get(), caster, mode, spell);
  243. auto shadedHexes = spell->battleMechanics(&event)->rangeInHexes(hoveredHex);
  244. for(BattleHex shadedHex : shadedHexes)
  245. {
  246. if((shadedHex.getX() != 0) && (shadedHex.getX() != GameConstants::BFIELD_WIDTH - 1))
  247. result.insert(shadedHex);
  248. }
  249. }
  250. return result;
  251. }
  252. std::set<BattleHex> BattleFieldController::getHighlightedHexesForMovementTarget()
  253. {
  254. const CStack * stack = owner.stacksController->getActiveStack();
  255. auto hoveredHex = getHoveredHex();
  256. if(!stack)
  257. return {};
  258. std::vector<BattleHex> availableHexes = owner.curInt->cb->battleGetAvailableHexes(stack, false, false, nullptr);
  259. auto hoveredStack = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  260. if(owner.curInt->cb->battleCanAttack(stack, hoveredStack, hoveredHex))
  261. {
  262. if(isTileAttackable(hoveredHex))
  263. {
  264. BattleHex attackFromHex = fromWhichHexAttack(hoveredHex);
  265. if(stack->doubleWide())
  266. return {attackFromHex, stack->occupiedHex(attackFromHex)};
  267. else
  268. return {attackFromHex};
  269. }
  270. }
  271. if(vstd::contains(availableHexes, hoveredHex))
  272. {
  273. if(stack->doubleWide())
  274. return {hoveredHex, stack->occupiedHex(hoveredHex)};
  275. else
  276. return {hoveredHex};
  277. }
  278. if(stack->doubleWide())
  279. {
  280. for(auto const & hex : availableHexes)
  281. {
  282. if(stack->occupiedHex(hex) == hoveredHex)
  283. return {hoveredHex, hex};
  284. }
  285. }
  286. return {};
  287. }
  288. void BattleFieldController::showHighlightedHexes(Canvas & canvas)
  289. {
  290. std::set<BattleHex> hoveredStackMovementRangeHexes = getMovementRangeForHoveredStack();
  291. std::set<BattleHex> hoveredSpellHexes = getHighlightedHexesForSpellRange();
  292. std::set<BattleHex> hoveredMoveHexes = getHighlightedHexesForMovementTarget();
  293. if(getHoveredHex() == BattleHex::INVALID)
  294. return;
  295. auto const & hoveredMouseHexes = owner.actionsController->currentActionSpellcasting(getHoveredHex()) ? hoveredSpellHexes : hoveredMoveHexes;
  296. for(int hex = 0; hex < GameConstants::BFIELD_SIZE; ++hex)
  297. {
  298. bool stackMovement = hoveredStackMovementRangeHexes.count(hex);
  299. bool mouse = hoveredMouseHexes.count(hex);
  300. if(stackMovement && mouse) // area where hovered stackMovement can move shown with highlight. Because also affected by mouse cursor, shade as well
  301. {
  302. showHighlightedHex(canvas, cellUnitMovementHighlight, hex, false);
  303. showHighlightedHex(canvas, cellShade, hex, true);
  304. }
  305. if(!stackMovement && mouse) // hexes affected only at mouse cursor shown as shaded
  306. {
  307. showHighlightedHex(canvas, cellShade, hex, true);
  308. }
  309. if(stackMovement && !mouse) // hexes where hovered stackMovement can move shown with highlight
  310. {
  311. showHighlightedHex(canvas, cellUnitMovementHighlight, hex, false);
  312. }
  313. }
  314. }
  315. Rect BattleFieldController::hexPositionLocal(BattleHex hex) const
  316. {
  317. int x = 14 + ((hex.getY())%2==0 ? 22 : 0) + 44*hex.getX();
  318. int y = 86 + 42 *hex.getY();
  319. int w = cellShade->width();
  320. int h = cellShade->height();
  321. return Rect(x, y, w, h);
  322. }
  323. Rect BattleFieldController::hexPositionAbsolute(BattleHex hex) const
  324. {
  325. return hexPositionLocal(hex) + pos.topLeft();
  326. }
  327. bool BattleFieldController::isPixelInHex(Point const & position)
  328. {
  329. return !cellShade->isTransparent(position);
  330. }
  331. BattleHex BattleFieldController::getHoveredHex()
  332. {
  333. return hoveredHex;
  334. }
  335. BattleHex BattleFieldController::getHexAtPosition(Point hoverPos)
  336. {
  337. if (owner.attackingHero)
  338. {
  339. if (owner.attackingHero->pos.isInside(hoverPos))
  340. return BattleHex::HERO_ATTACKER;
  341. }
  342. if (owner.defendingHero)
  343. {
  344. if (owner.attackingHero->pos.isInside(hoverPos))
  345. return BattleHex::HERO_DEFENDER;
  346. }
  347. for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h)
  348. {
  349. Rect hexPosition = hexPositionAbsolute(h);
  350. if (!hexPosition.isInside(hoverPos))
  351. continue;
  352. if (isPixelInHex(hoverPos - hexPosition.topLeft()))
  353. return h;
  354. }
  355. return BattleHex::INVALID;
  356. }
  357. BattleHex::EDir BattleFieldController::selectAttackDirection(BattleHex myNumber)
  358. {
  359. const bool doubleWide = owner.stacksController->getActiveStack()->doubleWide();
  360. auto neighbours = myNumber.allNeighbouringTiles();
  361. // 0 1
  362. // 5 x 2
  363. // 4 3
  364. // if true - our current stack can move into this hex (and attack)
  365. std::array<bool, 8> attackAvailability;
  366. if (doubleWide)
  367. {
  368. // For double-hexes we need to ensure that both hexes needed for this direction are occupyable:
  369. // | -0- | -1- | -2- | -3- | -4- | -5- | -6- | -7-
  370. // | o o - | - o o | - - | - - | - - | - - | o o | - -
  371. // | - x - | - x - | - x o o| - x - | - x - |o o x - | - x - | - x -
  372. // | - - | - - | - - | - o o | o o - | - - | - - | o o
  373. for (size_t i : { 1, 2, 3})
  374. attackAvailability[i] = vstd::contains(occupiableHexes, neighbours[i]) && vstd::contains(occupiableHexes, neighbours[i].cloneInDirection(BattleHex::RIGHT, false));
  375. for (size_t i : { 4, 5, 0})
  376. attackAvailability[i] = vstd::contains(occupiableHexes, neighbours[i]) && vstd::contains(occupiableHexes, neighbours[i].cloneInDirection(BattleHex::LEFT, false));
  377. attackAvailability[6] = vstd::contains(occupiableHexes, neighbours[0]) && vstd::contains(occupiableHexes, neighbours[1]);
  378. attackAvailability[7] = vstd::contains(occupiableHexes, neighbours[3]) && vstd::contains(occupiableHexes, neighbours[4]);
  379. }
  380. else
  381. {
  382. for (size_t i = 0; i < 6; ++i)
  383. attackAvailability[i] = vstd::contains(occupiableHexes, neighbours[i]);
  384. attackAvailability[6] = false;
  385. attackAvailability[7] = false;
  386. }
  387. // Zero available tiles to attack from
  388. if ( vstd::find(attackAvailability, true) == attackAvailability.end())
  389. {
  390. logGlobal->error("Error: cannot find a hex to attack hex %d from!", myNumber);
  391. return BattleHex::NONE;
  392. }
  393. // For each valid direction, select position to test against
  394. std::array<Point, 8> testPoint;
  395. for (size_t i = 0; i < 6; ++i)
  396. if (attackAvailability[i])
  397. testPoint[i] = hexPositionAbsolute(neighbours[i]).center();
  398. // For bottom/top directions select central point, but move it a bit away from true center to reduce zones allocated to them
  399. if (attackAvailability[6])
  400. testPoint[6] = (hexPositionAbsolute(neighbours[0]).center() + hexPositionAbsolute(neighbours[1]).center()) / 2 + Point(0, -5);
  401. if (attackAvailability[7])
  402. testPoint[7] = (hexPositionAbsolute(neighbours[3]).center() + hexPositionAbsolute(neighbours[4]).center()) / 2 + Point(0, 5);
  403. // Compute distance between tested position & cursor position and pick nearest
  404. std::array<int, 8> distance2;
  405. for (size_t i = 0; i < 8; ++i)
  406. if (attackAvailability[i])
  407. distance2[i] = (testPoint[i].y - currentAttackOriginPoint.y)*(testPoint[i].y - currentAttackOriginPoint.y) + (testPoint[i].x - currentAttackOriginPoint.x)*(testPoint[i].x - currentAttackOriginPoint.x);
  408. size_t nearest = -1;
  409. for (size_t i = 0; i < 8; ++i)
  410. if (attackAvailability[i] && (nearest == -1 || distance2[i] < distance2[nearest]) )
  411. nearest = i;
  412. assert(nearest != -1);
  413. return BattleHex::EDir(nearest);
  414. }
  415. BattleHex BattleFieldController::fromWhichHexAttack(BattleHex attackTarget)
  416. {
  417. BattleHex::EDir direction = selectAttackDirection(getHoveredHex());
  418. const CStack * attacker = owner.stacksController->getActiveStack();
  419. assert(direction != BattleHex::NONE);
  420. assert(attacker);
  421. if (!attacker->doubleWide())
  422. {
  423. assert(direction != BattleHex::BOTTOM);
  424. assert(direction != BattleHex::TOP);
  425. return attackTarget.cloneInDirection(direction);
  426. }
  427. else
  428. {
  429. // We need to find position of right hex of double-hex creature (or left for defending side)
  430. // | TOP_LEFT |TOP_RIGHT | RIGHT |BOTTOM_RIGHT|BOTTOM_LEFT| LEFT | TOP |BOTTOM
  431. // | o o - | - o o | - - | - - | - - | - - | o o | - -
  432. // | - x - | - x - | - x o o| - x - | - x - |o o x - | - x - | - x -
  433. // | - - | - - | - - | - o o | o o - | - - | - - | o o
  434. switch (direction)
  435. {
  436. case BattleHex::TOP_LEFT:
  437. case BattleHex::LEFT:
  438. case BattleHex::BOTTOM_LEFT:
  439. {
  440. if ( attacker->unitSide() == BattleSide::ATTACKER )
  441. return attackTarget.cloneInDirection(direction);
  442. else
  443. return attackTarget.cloneInDirection(direction).cloneInDirection(BattleHex::LEFT);
  444. }
  445. case BattleHex::TOP_RIGHT:
  446. case BattleHex::RIGHT:
  447. case BattleHex::BOTTOM_RIGHT:
  448. {
  449. if ( attacker->unitSide() == BattleSide::ATTACKER )
  450. return attackTarget.cloneInDirection(direction).cloneInDirection(BattleHex::RIGHT);
  451. else
  452. return attackTarget.cloneInDirection(direction);
  453. }
  454. case BattleHex::TOP:
  455. {
  456. if ( attacker->unitSide() == BattleSide::ATTACKER )
  457. return attackTarget.cloneInDirection(BattleHex::TOP_RIGHT);
  458. else
  459. return attackTarget.cloneInDirection(BattleHex::TOP_LEFT);
  460. }
  461. case BattleHex::BOTTOM:
  462. {
  463. if ( attacker->unitSide() == BattleSide::ATTACKER )
  464. return attackTarget.cloneInDirection(BattleHex::BOTTOM_RIGHT);
  465. else
  466. return attackTarget.cloneInDirection(BattleHex::BOTTOM_LEFT);
  467. }
  468. default:
  469. assert(0);
  470. return BattleHex::INVALID;
  471. }
  472. }
  473. }
  474. bool BattleFieldController::isTileAttackable(const BattleHex & number) const
  475. {
  476. for (auto & elem : occupiableHexes)
  477. {
  478. if (BattleHex::mutualPosition(elem, number) != -1 || elem == number)
  479. return true;
  480. }
  481. return false;
  482. }
  483. void BattleFieldController::updateAccessibleHexes()
  484. {
  485. auto accessibility = owner.curInt->cb->getAccesibility();
  486. for(int i = 0; i < accessibility.size(); i++)
  487. stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE || (accessibility[i] == EAccessibility::SIDE_COLUMN));
  488. }
  489. bool BattleFieldController::stackCountOutsideHex(const BattleHex & number) const
  490. {
  491. return stackCountOutsideHexes[number];
  492. }
  493. void BattleFieldController::showAll(Canvas & to)
  494. {
  495. show(to);
  496. }
  497. void BattleFieldController::tick(uint32_t msPassed)
  498. {
  499. updateAccessibleHexes();
  500. owner.stacksController->tick(msPassed);
  501. owner.obstacleController->tick(msPassed);
  502. owner.projectilesController->tick(msPassed);
  503. }
  504. void BattleFieldController::show(Canvas & to)
  505. {
  506. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
  507. renderBattlefield(to);
  508. if (isActive() && isPanning() && getHoveredHex() != BattleHex::INVALID)
  509. {
  510. auto cursorIndex = CCS->curh->get<Cursor::Combat>();
  511. auto imageIndex = static_cast<size_t>(cursorIndex);
  512. canvas.draw(attackCursors->getImage(imageIndex), hexPositionAbsolute(getHoveredHex()).center() - CCS->curh->getPivotOffsetCombat(imageIndex));
  513. }
  514. }
  515. bool BattleFieldController::receiveEvent(const Point & position, int eventType) const
  516. {
  517. if (eventType == HOVER)
  518. return true;
  519. return CIntObject::receiveEvent(position, eventType);
  520. }