BattleFieldController.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. cellShade = IImage::createFromFile("CCELLSHD.BMP");
  42. cellUnitMovementHighlight = IImage::createFromFile("UnitMovementHighlight.PNG", EImageBlitMode::COLORKEY);
  43. cellUnitMaxMovementHighlight = IImage::createFromFile("UnitMaxMovementHighlight.PNG", EImageBlitMode::COLORKEY);
  44. if(!owner.siegeController)
  45. {
  46. auto bfieldType = owner.curInt->cb->battleGetBattlefieldType();
  47. if(bfieldType == BattleField::NONE)
  48. logGlobal->error("Invalid battlefield returned for current battle");
  49. else
  50. background = IImage::createFromFile(bfieldType.getInfo()->graphics, EImageBlitMode::OPAQUE);
  51. }
  52. else
  53. {
  54. std::string backgroundName = owner.siegeController->getBattleBackgroundName();
  55. background = IImage::createFromFile(backgroundName, EImageBlitMode::OPAQUE);
  56. }
  57. pos.w = background->width();
  58. pos.h = background->height();
  59. backgroundWithHexes = std::make_unique<Canvas>(Point(background->width(), background->height()));
  60. updateAccessibleHexes();
  61. addUsedEvents(LCLICK | RCLICK | MOVE);
  62. LOCPLINT->cingconsole->pos = this->pos;
  63. }
  64. void BattleFieldController::createHeroes()
  65. {
  66. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  67. // create heroes as part of our constructor for correct positioning inside battlefield
  68. if(owner.attackingHeroInstance)
  69. owner.attackingHero = std::make_shared<BattleHero>(owner, owner.attackingHeroInstance, false);
  70. if(owner.defendingHeroInstance)
  71. owner.defendingHero = std::make_shared<BattleHero>(owner, owner.defendingHeroInstance, true);
  72. }
  73. void BattleFieldController::mouseMoved(const Point & cursorPosition)
  74. {
  75. if (!pos.isInside(cursorPosition))
  76. {
  77. owner.actionsController->onHoverEnded();
  78. return;
  79. }
  80. BattleHex selectedHex = getHoveredHex();
  81. owner.actionsController->onHexHovered(selectedHex);
  82. }
  83. void BattleFieldController::clickLeft(tribool down, bool previousState)
  84. {
  85. if(!down)
  86. {
  87. BattleHex selectedHex = getHoveredHex();
  88. if (selectedHex != BattleHex::INVALID)
  89. owner.actionsController->onHexLeftClicked(selectedHex);
  90. }
  91. }
  92. void BattleFieldController::clickRight(tribool down, bool previousState)
  93. {
  94. if(down)
  95. {
  96. BattleHex selectedHex = getHoveredHex();
  97. if (selectedHex != BattleHex::INVALID)
  98. owner.actionsController->onHexRightClicked(selectedHex);
  99. }
  100. }
  101. void BattleFieldController::renderBattlefield(Canvas & canvas)
  102. {
  103. Canvas clippedCanvas(canvas, pos);
  104. showBackground(clippedCanvas);
  105. BattleRenderer renderer(owner);
  106. renderer.execute(clippedCanvas);
  107. owner.projectilesController->showProjectiles(clippedCanvas);
  108. }
  109. void BattleFieldController::showBackground(Canvas & canvas)
  110. {
  111. if (owner.stacksController->getActiveStack() != nullptr ) //&& creAnims[stacksController->getActiveStack()->ID]->isIdle() //show everything with range
  112. showBackgroundImageWithHexes(canvas);
  113. else
  114. showBackgroundImage(canvas);
  115. showHighlightedHexes(canvas);
  116. }
  117. void BattleFieldController::showBackgroundImage(Canvas & canvas)
  118. {
  119. canvas.draw(background, Point(0, 0));
  120. owner.obstacleController->showAbsoluteObstacles(canvas);
  121. if ( owner.siegeController )
  122. owner.siegeController->showAbsoluteObstacles(canvas);
  123. if (settings["battle"]["cellBorders"].Bool())
  124. {
  125. for (int i=0; i<GameConstants::BFIELD_SIZE; ++i)
  126. {
  127. if ( i % GameConstants::BFIELD_WIDTH == 0)
  128. continue;
  129. if ( i % GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_WIDTH - 1)
  130. continue;
  131. canvas.draw(cellBorder, hexPositionLocal(i).topLeft());
  132. }
  133. }
  134. }
  135. void BattleFieldController::showBackgroundImageWithHexes(Canvas & canvas)
  136. {
  137. canvas.draw(*backgroundWithHexes.get(), Point(0, 0));
  138. }
  139. void BattleFieldController::redrawBackgroundWithHexes()
  140. {
  141. const CStack *activeStack = owner.stacksController->getActiveStack();
  142. std::vector<BattleHex> attackableHexes;
  143. if(activeStack)
  144. occupyableHexes = owner.curInt->cb->battleGetAvailableHexes(activeStack, true, true, &attackableHexes);
  145. // prepare background graphic with hexes and shaded hexes
  146. backgroundWithHexes->draw(background, Point(0,0));
  147. owner.obstacleController->showAbsoluteObstacles(*backgroundWithHexes);
  148. if(owner.siegeController)
  149. owner.siegeController->showAbsoluteObstacles(*backgroundWithHexes);
  150. // show shaded hexes for active's stackMovement valid movement and the hexes that it can attack
  151. if(settings["battle"]["stackRange"].Bool())
  152. {
  153. std::vector<BattleHex> hexesToShade = occupyableHexes;
  154. hexesToShade.insert(hexesToShade.end(), attackableHexes.begin(), attackableHexes.end());
  155. for(BattleHex hex : hexesToShade)
  156. {
  157. backgroundWithHexes->draw(cellShade, hexPositionLocal(hex).topLeft());
  158. }
  159. }
  160. // draw cell borders
  161. if(settings["battle"]["cellBorders"].Bool())
  162. {
  163. for(int i=0; i<GameConstants::BFIELD_SIZE; ++i)
  164. {
  165. if(i % GameConstants::BFIELD_WIDTH == 0)
  166. continue;
  167. if(i % GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_WIDTH - 1)
  168. continue;
  169. backgroundWithHexes->draw(cellBorder, hexPositionLocal(i).topLeft());
  170. }
  171. }
  172. }
  173. void BattleFieldController::showHighlightedHex(Canvas & canvas, std::shared_ptr<IImage> highlight, BattleHex hex, bool darkBorder)
  174. {
  175. Point hexPos = hexPositionLocal(hex).topLeft();
  176. canvas.draw(highlight, hexPos);
  177. if(!darkBorder && settings["battle"]["cellBorders"].Bool())
  178. canvas.draw(cellBorder, hexPos);
  179. }
  180. std::set<BattleHex> BattleFieldController::getHighlightedHexesForActiveStack()
  181. {
  182. std::set<BattleHex> result;
  183. if(!owner.stacksController->getActiveStack())
  184. return result;
  185. if(!settings["battle"]["stackRange"].Bool())
  186. return result;
  187. auto hoveredHex = getHoveredHex();
  188. std::set<BattleHex> set = owner.curInt->cb->battleGetAttackedHexes(owner.stacksController->getActiveStack(), hoveredHex, attackingHex);
  189. for(BattleHex hex : set)
  190. result.insert(hex);
  191. return result;
  192. }
  193. std::set<BattleHex> BattleFieldController::getMovementRangeForHoveredStack()
  194. {
  195. std::set<BattleHex> result;
  196. if (!owner.stacksController->getActiveStack())
  197. return result;
  198. if (!settings["battle"]["stackRange"].Bool())
  199. return result;
  200. auto hoveredHex = getHoveredHex();
  201. // add possible movement hexes for stack under mouse
  202. const CStack * const hoveredStack = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  203. if(hoveredStack)
  204. {
  205. std::vector<BattleHex> v = owner.curInt->cb->battleGetAvailableHexes(hoveredStack, false, true, nullptr);
  206. for(BattleHex hex : v)
  207. result.insert(hex);
  208. }
  209. return result;
  210. }
  211. std::set<BattleHex> BattleFieldController::STUB_getMaxMovementRangeForHoveredStack()
  212. {
  213. std::set<BattleHex> result;
  214. if (!owner.stacksController->getActiveStack())
  215. return result;
  216. if (!settings["battle"]["stackRange"].Bool())
  217. return result;
  218. auto hoveredHex = getHoveredHex();
  219. // add max movement hexes for stack under mouse
  220. const CStack * const hoveredStack = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  221. if(hoveredStack)
  222. {
  223. auto hex = BattleHex(10, 5);
  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::getHighlightedHexesMovementTarget()
  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, true, 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> hoveredStackMaxMovementHexes = STUB_getMaxMovementRangeForHoveredStack();
  292. std::set<BattleHex> hoveredSpellHexes = getHighlightedHexesForSpellRange();
  293. std::set<BattleHex> hoveredMoveHexes = getHighlightedHexesMovementTarget();
  294. if(getHoveredHex() == BattleHex::INVALID)
  295. return;
  296. auto const & hoveredMouseHexes = owner.actionsController->currentActionSpellcasting(getHoveredHex()) ? hoveredSpellHexes : hoveredMoveHexes;
  297. for(int hex = 0; hex < GameConstants::BFIELD_SIZE; ++hex)
  298. {
  299. bool stackMovement = hoveredStackMovementRangeHexes.count(hex);
  300. bool stackMaxMovement = hoveredStackMaxMovementHexes.count(hex);
  301. bool mouse = hoveredMouseHexes.count(hex);
  302. if(stackMovement && mouse) // area where hovered stackMovement can move shown with highlight. Because also affected by mouse cursor, shade as well
  303. {
  304. showHighlightedHex(canvas, cellUnitMovementHighlight, hex, false);
  305. showHighlightedHex(canvas, cellShade, hex, true);
  306. }
  307. if(!stackMovement && mouse) // hexes affected only at mouse cursor shown as shaded
  308. {
  309. showHighlightedHex(canvas, cellShade, hex, true);
  310. }
  311. if(stackMovement && !mouse) // hexes where hovered stackMovement can move shown with highlight
  312. {
  313. showHighlightedHex(canvas, cellUnitMovementHighlight, hex, false);
  314. }
  315. if(stackMaxMovement)
  316. showHighlightedHex(canvas, cellUnitMaxMovementHighlight, hex, false);
  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. Point hoverPos = GH.getCursorPosition();
  338. if (owner.attackingHero)
  339. {
  340. if (owner.attackingHero->pos.isInside(hoverPos))
  341. return BattleHex::HERO_ATTACKER;
  342. }
  343. if (owner.defendingHero)
  344. {
  345. if (owner.attackingHero->pos.isInside(hoverPos))
  346. return BattleHex::HERO_DEFENDER;
  347. }
  348. for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h)
  349. {
  350. Rect hexPosition = hexPositionAbsolute(h);
  351. if (!hexPosition.isInside(hoverPos))
  352. continue;
  353. if (isPixelInHex(hoverPos - hexPosition.topLeft()))
  354. return h;
  355. }
  356. return BattleHex::INVALID;
  357. }
  358. void BattleFieldController::setBattleCursor(BattleHex myNumber)
  359. {
  360. Point cursorPos = CCS->curh->position();
  361. std::vector<Cursor::Combat> sectorCursor = {
  362. Cursor::Combat::HIT_SOUTHEAST,
  363. Cursor::Combat::HIT_SOUTHWEST,
  364. Cursor::Combat::HIT_WEST,
  365. Cursor::Combat::HIT_NORTHWEST,
  366. Cursor::Combat::HIT_NORTHEAST,
  367. Cursor::Combat::HIT_EAST,
  368. Cursor::Combat::HIT_SOUTH,
  369. Cursor::Combat::HIT_NORTH,
  370. };
  371. auto direction = static_cast<size_t>(selectAttackDirection(myNumber, cursorPos));
  372. assert(direction != -1);
  373. if (direction != -1)
  374. CCS->curh->set(sectorCursor[direction]);
  375. }
  376. BattleHex::EDir BattleFieldController::selectAttackDirection(BattleHex myNumber, const Point & cursorPos)
  377. {
  378. const bool doubleWide = owner.stacksController->getActiveStack()->doubleWide();
  379. auto neighbours = myNumber.allNeighbouringTiles();
  380. // 0 1
  381. // 5 x 2
  382. // 4 3
  383. // if true - our current stack can move into this hex (and attack)
  384. std::array<bool, 8> attackAvailability;
  385. if (doubleWide)
  386. {
  387. // For double-hexes we need to ensure that both hexes needed for this direction are occupyable:
  388. // | -0- | -1- | -2- | -3- | -4- | -5- | -6- | -7-
  389. // | o o - | - o o | - - | - - | - - | - - | o o | - -
  390. // | - x - | - x - | - x o o| - x - | - x - |o o x - | - x - | - x -
  391. // | - - | - - | - - | - o o | o o - | - - | - - | o o
  392. for (size_t i : { 1, 2, 3})
  393. attackAvailability[i] = vstd::contains(occupyableHexes, neighbours[i]) && vstd::contains(occupyableHexes, neighbours[i].cloneInDirection(BattleHex::RIGHT, false));
  394. for (size_t i : { 4, 5, 0})
  395. attackAvailability[i] = vstd::contains(occupyableHexes, neighbours[i]) && vstd::contains(occupyableHexes, neighbours[i].cloneInDirection(BattleHex::LEFT, false));
  396. attackAvailability[6] = vstd::contains(occupyableHexes, neighbours[0]) && vstd::contains(occupyableHexes, neighbours[1]);
  397. attackAvailability[7] = vstd::contains(occupyableHexes, neighbours[3]) && vstd::contains(occupyableHexes, neighbours[4]);
  398. }
  399. else
  400. {
  401. for (size_t i = 0; i < 6; ++i)
  402. attackAvailability[i] = vstd::contains(occupyableHexes, neighbours[i]);
  403. attackAvailability[6] = false;
  404. attackAvailability[7] = false;
  405. }
  406. // Zero available tiles to attack from
  407. if ( vstd::find(attackAvailability, true) == attackAvailability.end())
  408. {
  409. logGlobal->error("Error: cannot find a hex to attack hex %d from!", myNumber);
  410. return BattleHex::NONE;
  411. }
  412. // For each valid direction, select position to test against
  413. std::array<Point, 8> testPoint;
  414. for (size_t i = 0; i < 6; ++i)
  415. if (attackAvailability[i])
  416. testPoint[i] = hexPositionAbsolute(neighbours[i]).center();
  417. // For bottom/top directions select central point, but move it a bit away from true center to reduce zones allocated to them
  418. if (attackAvailability[6])
  419. testPoint[6] = (hexPositionAbsolute(neighbours[0]).center() + hexPositionAbsolute(neighbours[1]).center()) / 2 + Point(0, -5);
  420. if (attackAvailability[7])
  421. testPoint[7] = (hexPositionAbsolute(neighbours[3]).center() + hexPositionAbsolute(neighbours[4]).center()) / 2 + Point(0, 5);
  422. // Compute distance between tested position & cursor position and pick nearest
  423. std::array<int, 8> distance2;
  424. for (size_t i = 0; i < 8; ++i)
  425. if (attackAvailability[i])
  426. distance2[i] = (testPoint[i].y - cursorPos.y)*(testPoint[i].y - cursorPos.y) + (testPoint[i].x - cursorPos.x)*(testPoint[i].x - cursorPos.x);
  427. size_t nearest = -1;
  428. for (size_t i = 0; i < 8; ++i)
  429. if (attackAvailability[i] && (nearest == -1 || distance2[i] < distance2[nearest]) )
  430. nearest = i;
  431. assert(nearest != -1);
  432. return BattleHex::EDir(nearest);
  433. }
  434. BattleHex BattleFieldController::fromWhichHexAttack(BattleHex attackTarget)
  435. {
  436. BattleHex::EDir direction = selectAttackDirection(attackTarget, CCS->curh->position());
  437. const CStack * attacker = owner.stacksController->getActiveStack();
  438. assert(direction != BattleHex::NONE);
  439. assert(attacker);
  440. if (!attacker->doubleWide())
  441. {
  442. assert(direction != BattleHex::BOTTOM);
  443. assert(direction != BattleHex::TOP);
  444. return attackTarget.cloneInDirection(direction);
  445. }
  446. else
  447. {
  448. // We need to find position of right hex of double-hex creature (or left for defending side)
  449. // | TOP_LEFT |TOP_RIGHT | RIGHT |BOTTOM_RIGHT|BOTTOM_LEFT| LEFT | TOP |BOTTOM
  450. // | o o - | - o o | - - | - - | - - | - - | o o | - -
  451. // | - x - | - x - | - x o o| - x - | - x - |o o x - | - x - | - x -
  452. // | - - | - - | - - | - o o | o o - | - - | - - | o o
  453. switch (direction)
  454. {
  455. case BattleHex::TOP_LEFT:
  456. case BattleHex::LEFT:
  457. case BattleHex::BOTTOM_LEFT:
  458. {
  459. if ( attacker->side == BattleSide::ATTACKER )
  460. return attackTarget.cloneInDirection(direction);
  461. else
  462. return attackTarget.cloneInDirection(direction).cloneInDirection(BattleHex::LEFT);
  463. }
  464. case BattleHex::TOP_RIGHT:
  465. case BattleHex::RIGHT:
  466. case BattleHex::BOTTOM_RIGHT:
  467. {
  468. if ( attacker->side == BattleSide::ATTACKER )
  469. return attackTarget.cloneInDirection(direction).cloneInDirection(BattleHex::RIGHT);
  470. else
  471. return attackTarget.cloneInDirection(direction);
  472. }
  473. case BattleHex::TOP:
  474. {
  475. if ( attacker->side == BattleSide::ATTACKER )
  476. return attackTarget.cloneInDirection(BattleHex::TOP_RIGHT);
  477. else
  478. return attackTarget.cloneInDirection(BattleHex::TOP_LEFT);
  479. }
  480. case BattleHex::BOTTOM:
  481. {
  482. if ( attacker->side == BattleSide::ATTACKER )
  483. return attackTarget.cloneInDirection(BattleHex::BOTTOM_RIGHT);
  484. else
  485. return attackTarget.cloneInDirection(BattleHex::BOTTOM_LEFT);
  486. }
  487. default:
  488. assert(0);
  489. return BattleHex::INVALID;
  490. }
  491. }
  492. }
  493. bool BattleFieldController::isTileAttackable(const BattleHex & number) const
  494. {
  495. for (auto & elem : occupyableHexes)
  496. {
  497. if (BattleHex::mutualPosition(elem, number) != -1 || elem == number)
  498. return true;
  499. }
  500. return false;
  501. }
  502. void BattleFieldController::updateAccessibleHexes()
  503. {
  504. auto accessibility = owner.curInt->cb->getAccesibility();
  505. for(int i = 0; i < accessibility.size(); i++)
  506. stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE || (accessibility[i] == EAccessibility::SIDE_COLUMN));
  507. }
  508. bool BattleFieldController::stackCountOutsideHex(const BattleHex & number) const
  509. {
  510. return stackCountOutsideHexes[number];
  511. }
  512. void BattleFieldController::showAll(SDL_Surface * to)
  513. {
  514. show(to);
  515. }
  516. void BattleFieldController::show(SDL_Surface * to)
  517. {
  518. updateAccessibleHexes();
  519. owner.stacksController->update();
  520. owner.obstacleController->update();
  521. Canvas canvas(to);
  522. CSDL_Ext::CClipRectGuard guard(to, pos);
  523. renderBattlefield(canvas);
  524. }