BattleFieldController.cpp 20 KB

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