BattleFieldController.cpp 19 KB

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