CBattleFieldController.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * CBattleFieldController.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 "CBattleFieldController.h"
  12. #include "CBattleInterface.h"
  13. #include "CBattleActionsController.h"
  14. #include "CBattleInterfaceClasses.h"
  15. #include "CBattleEffectsController.h"
  16. #include "CBattleSiegeController.h"
  17. #include "CBattleStacksController.h"
  18. #include "CBattleObstacleController.h"
  19. #include "CBattleProjectileController.h"
  20. #include "../CGameInfo.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../gui/CAnimation.h"
  23. #include "../gui/CCanvas.h"
  24. #include "../gui/CGuiHandler.h"
  25. #include "../gui/CCursorHandler.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/BattleFieldHandler.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../lib/CStack.h"
  30. #include "../../lib/spells/ISpellMechanics.h"
  31. CBattleFieldController::CBattleFieldController(CBattleInterface * owner):
  32. owner(owner),
  33. attackingHex(BattleHex::INVALID)
  34. {
  35. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  36. pos.w = owner->pos.w;
  37. pos.h = owner->pos.h;
  38. //preparing cells and hexes
  39. cellBorder = IImage::createFromFile("CCELLGRD.BMP");
  40. cellShade = IImage::createFromFile("CCELLSHD.BMP");
  41. if(!owner->siegeController)
  42. {
  43. auto bfieldType = owner->curInt->cb->battleGetBattlefieldType();
  44. if(bfieldType == BattleField::NONE)
  45. logGlobal->error("Invalid battlefield returned for current battle");
  46. else
  47. background = IImage::createFromFile(bfieldType.getInfo()->graphics);
  48. }
  49. else
  50. {
  51. std::string backgroundName = owner->siegeController->getBattleBackgroundName();
  52. background = IImage::createFromFile(backgroundName);
  53. }
  54. //preparing graphic with cell borders
  55. cellBorders = std::make_shared<CCanvas>(Point(background->width(), background->height()));
  56. for (int i=0; i<GameConstants::BFIELD_SIZE; ++i)
  57. {
  58. if ( i % GameConstants::BFIELD_WIDTH == 0)
  59. continue;
  60. if ( i % GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_WIDTH - 1)
  61. continue;
  62. cellBorders->draw(cellBorder, hexPositionLocal(i).topLeft());
  63. }
  64. backgroundWithHexes = std::make_shared<CCanvas>(Point(background->width(), background->height()));
  65. for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h)
  66. {
  67. auto hex = std::make_shared<CClickableHex>();
  68. hex->myNumber = h;
  69. hex->pos = hexPosition(h);
  70. hex->myInterface = owner;
  71. bfield.push_back(hex);
  72. }
  73. auto accessibility = owner->curInt->cb->getAccesibility();
  74. for(int i = 0; i < accessibility.size(); i++)
  75. stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE);
  76. }
  77. void CBattleFieldController::renderBattlefield(std::shared_ptr<CCanvas> canvas)
  78. {
  79. showBackground(canvas);
  80. CBattleFieldRenderer renderer(owner);
  81. renderer.execute(canvas);
  82. owner->projectilesController->showProjectiles(canvas);
  83. }
  84. void CBattleFieldController::showBackground(std::shared_ptr<CCanvas> canvas)
  85. {
  86. if (owner->stacksController->getActiveStack() != nullptr ) //&& creAnims[stacksController->getActiveStack()->ID]->isIdle() //show everything with range
  87. showBackgroundImageWithHexes(canvas);
  88. else
  89. showBackgroundImage(canvas);
  90. showHighlightedHexes(canvas);
  91. }
  92. void CBattleFieldController::showBackgroundImage(std::shared_ptr<CCanvas> canvas)
  93. {
  94. canvas->draw(background, owner->pos.topLeft());
  95. owner->obstacleController->showAbsoluteObstacles(canvas, pos.topLeft());
  96. if ( owner->siegeController )
  97. owner->siegeController->showAbsoluteObstacles(canvas, pos.topLeft());
  98. if (settings["battle"]["cellBorders"].Bool())
  99. canvas->draw(cellBorders, owner->pos.topLeft());
  100. }
  101. void CBattleFieldController::showBackgroundImageWithHexes(std::shared_ptr<CCanvas> canvas)
  102. {
  103. canvas->draw(backgroundWithHexes, owner->pos.topLeft());
  104. }
  105. void CBattleFieldController::redrawBackgroundWithHexes()
  106. {
  107. const CStack *activeStack = owner->stacksController->getActiveStack();
  108. std::vector<BattleHex> attackableHexes;
  109. if (activeStack)
  110. occupyableHexes = owner->curInt->cb->battleGetAvailableHexes(activeStack, true, &attackableHexes);
  111. auto accessibility = owner->curInt->cb->getAccesibility();
  112. for(int i = 0; i < accessibility.size(); i++)
  113. stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE);
  114. //prepare background graphic with hexes and shaded hexes
  115. backgroundWithHexes->draw(background, Point(0,0));
  116. owner->obstacleController->showAbsoluteObstacles(backgroundWithHexes, Point(0,0));
  117. if ( owner->siegeController )
  118. owner->siegeController->showAbsoluteObstacles(backgroundWithHexes, Point(0,0));
  119. if (settings["battle"]["stackRange"].Bool())
  120. {
  121. std::vector<BattleHex> hexesToShade = occupyableHexes;
  122. hexesToShade.insert(hexesToShade.end(), attackableHexes.begin(), attackableHexes.end());
  123. for (BattleHex hex : hexesToShade)
  124. {
  125. backgroundWithHexes->draw(cellShade, hexPositionLocal(hex).topLeft());
  126. }
  127. }
  128. if(settings["battle"]["cellBorders"].Bool())
  129. backgroundWithHexes->draw(cellBorders, Point(0, 0));
  130. }
  131. void CBattleFieldController::showHighlightedHex(std::shared_ptr<CCanvas> to, BattleHex hex, bool darkBorder)
  132. {
  133. Point hexPos = hexPosition(hex).topLeft();
  134. to->draw(cellShade, hexPos);
  135. if(!darkBorder && settings["battle"]["cellBorders"].Bool())
  136. to->draw(cellBorder, hexPos);
  137. }
  138. std::set<BattleHex> CBattleFieldController::getHighlightedHexesStackRange()
  139. {
  140. std::set<BattleHex> result;
  141. if ( !owner->stacksController->getActiveStack())
  142. return result;
  143. if ( !settings["battle"]["stackRange"].Bool())
  144. return result;
  145. auto hoveredHex = getHoveredHex();
  146. std::set<BattleHex> set = owner->curInt->cb->battleGetAttackedHexes(owner->stacksController->getActiveStack(), hoveredHex, attackingHex);
  147. for(BattleHex hex : set)
  148. result.insert(hex);
  149. // display the movement shadow of the stack at b (i.e. stack under mouse)
  150. const CStack * const shere = owner->curInt->cb->battleGetStackByPos(hoveredHex, false);
  151. if(shere && shere != owner->stacksController->getActiveStack() && shere->alive())
  152. {
  153. std::vector<BattleHex> v = owner->curInt->cb->battleGetAvailableHexes(shere, true, nullptr);
  154. for(BattleHex hex : v)
  155. result.insert(hex);
  156. }
  157. return result;
  158. }
  159. std::set<BattleHex> CBattleFieldController::getHighlightedHexesSpellRange()
  160. {
  161. std::set<BattleHex> result;
  162. auto hoveredHex = getHoveredHex();
  163. if(!settings["battle"]["mouseShadow"].Bool())
  164. return result;
  165. const spells::Caster *caster = nullptr;
  166. const CSpell *spell = nullptr;
  167. spells::Mode mode = spells::Mode::HERO;
  168. if(owner->actionsController->spellcastingModeActive())//hero casts spell
  169. {
  170. spell = owner->actionsController->selectedSpell().toSpell();
  171. caster = owner->getActiveHero();
  172. }
  173. else if(owner->stacksController->activeStackSpellToCast() != SpellID::NONE)//stack casts spell
  174. {
  175. spell = SpellID(owner->stacksController->activeStackSpellToCast()).toSpell();
  176. caster = owner->stacksController->getActiveStack();
  177. mode = spells::Mode::CREATURE_ACTIVE;
  178. }
  179. if(caster && spell) //when casting spell
  180. {
  181. // printing shaded hex(es)
  182. spells::BattleCast event(owner->curInt->cb.get(), caster, mode, spell);
  183. auto shaded = spell->battleMechanics(&event)->rangeInHexes(hoveredHex);
  184. for(BattleHex shadedHex : shaded)
  185. {
  186. if((shadedHex.getX() != 0) && (shadedHex.getX() != GameConstants::BFIELD_WIDTH - 1))
  187. result.insert(shadedHex);
  188. }
  189. }
  190. else if(owner->active) //always highlight pointed hex
  191. {
  192. if(hoveredHex.getX() != 0 && hoveredHex.getX() != GameConstants::BFIELD_WIDTH - 1)
  193. result.insert(hoveredHex);
  194. }
  195. return result;
  196. }
  197. void CBattleFieldController::showHighlightedHexes(std::shared_ptr<CCanvas> canvas)
  198. {
  199. std::set<BattleHex> hoveredStack = getHighlightedHexesStackRange();
  200. std::set<BattleHex> hoveredMouse = getHighlightedHexesSpellRange();
  201. for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
  202. {
  203. bool stack = hoveredStack.count(b);
  204. bool mouse = hoveredMouse.count(b);
  205. if ( stack && mouse )
  206. {
  207. // area where enemy stack can move AND affected by mouse cursor - create darker highlight by blitting twice
  208. showHighlightedHex(canvas, b, true);
  209. showHighlightedHex(canvas, b, true);
  210. }
  211. if ( !stack && mouse )
  212. {
  213. showHighlightedHex(canvas, b, true);
  214. }
  215. if ( stack && !mouse )
  216. {
  217. showHighlightedHex(canvas, b, false);
  218. }
  219. }
  220. }
  221. Rect CBattleFieldController::hexPositionLocal(BattleHex hex) const
  222. {
  223. int x = 14 + ((hex.getY())%2==0 ? 22 : 0) + 44*hex.getX();
  224. int y = 86 + 42 *hex.getY();
  225. int w = cellShade->width();
  226. int h = cellShade->height();
  227. return Rect(x, y, w, h);
  228. }
  229. Rect CBattleFieldController::hexPosition(BattleHex hex) const
  230. {
  231. return hexPositionLocal(hex) + owner->pos.topLeft();
  232. }
  233. bool CBattleFieldController::isPixelInHex(Point const & position)
  234. {
  235. return !cellShade->isTransparent(position);
  236. }
  237. BattleHex CBattleFieldController::getHoveredHex()
  238. {
  239. for ( auto const & hex : bfield)
  240. if (hex->hovered && hex->strictHovered)
  241. return hex->myNumber;
  242. return BattleHex::INVALID;
  243. }
  244. void CBattleFieldController::setBattleCursor(BattleHex myNumber)
  245. {
  246. Rect hoveredHexPos = hexPosition(myNumber);
  247. CCursorHandler *cursor = CCS->curh;
  248. const double subdividingAngle = 2.0*M_PI/6.0; // Divide a hex into six sectors.
  249. const double hexMidX = hoveredHexPos.x + hoveredHexPos.w/2.0;
  250. const double hexMidY = hoveredHexPos.y + hoveredHexPos.h/2.0;
  251. const double cursorHexAngle = M_PI - atan2(hexMidY - cursor->ypos, cursor->xpos - hexMidX) + subdividingAngle/2; //TODO: refactor this nightmare
  252. const double sector = fmod(cursorHexAngle/subdividingAngle, 6.0);
  253. const int zigzagCorrection = !((myNumber/GameConstants::BFIELD_WIDTH)%2); // Off-by-one correction needed to deal with the odd battlefield rows.
  254. std::vector<int> sectorCursor; // From left to bottom left.
  255. sectorCursor.push_back(8);
  256. sectorCursor.push_back(9);
  257. sectorCursor.push_back(10);
  258. sectorCursor.push_back(11);
  259. sectorCursor.push_back(12);
  260. sectorCursor.push_back(7);
  261. const bool doubleWide = owner->stacksController->getActiveStack()->doubleWide();
  262. bool aboveAttackable = true, belowAttackable = true;
  263. // Exclude directions which cannot be attacked from.
  264. // Check to the left.
  265. if (myNumber%GameConstants::BFIELD_WIDTH <= 1 || !vstd::contains(occupyableHexes, myNumber - 1))
  266. {
  267. sectorCursor[0] = -1;
  268. }
  269. // Check top left, top right as well as above for 2-hex creatures.
  270. if (myNumber/GameConstants::BFIELD_WIDTH == 0)
  271. {
  272. sectorCursor[1] = -1;
  273. sectorCursor[2] = -1;
  274. aboveAttackable = false;
  275. }
  276. else
  277. {
  278. if (doubleWide)
  279. {
  280. bool attackRow[4] = {true, true, true, true};
  281. if (myNumber%GameConstants::BFIELD_WIDTH <= 1 || !vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH - 2 + zigzagCorrection))
  282. attackRow[0] = false;
  283. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  284. attackRow[1] = false;
  285. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH + zigzagCorrection))
  286. attackRow[2] = false;
  287. if (myNumber%GameConstants::BFIELD_WIDTH >= GameConstants::BFIELD_WIDTH - 2 || !vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH + 1 + zigzagCorrection))
  288. attackRow[3] = false;
  289. if (!(attackRow[0] && attackRow[1]))
  290. sectorCursor[1] = -1;
  291. if (!(attackRow[1] && attackRow[2]))
  292. aboveAttackable = false;
  293. if (!(attackRow[2] && attackRow[3]))
  294. sectorCursor[2] = -1;
  295. }
  296. else
  297. {
  298. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  299. sectorCursor[1] = -1;
  300. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH + zigzagCorrection))
  301. sectorCursor[2] = -1;
  302. }
  303. }
  304. // Check to the right.
  305. if (myNumber%GameConstants::BFIELD_WIDTH >= GameConstants::BFIELD_WIDTH - 2 || !vstd::contains(occupyableHexes, myNumber + 1))
  306. {
  307. sectorCursor[3] = -1;
  308. }
  309. // Check bottom right, bottom left as well as below for 2-hex creatures.
  310. if (myNumber/GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_HEIGHT - 1)
  311. {
  312. sectorCursor[4] = -1;
  313. sectorCursor[5] = -1;
  314. belowAttackable = false;
  315. }
  316. else
  317. {
  318. if (doubleWide)
  319. {
  320. bool attackRow[4] = {true, true, true, true};
  321. if (myNumber%GameConstants::BFIELD_WIDTH <= 1 || !vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH - 2 + zigzagCorrection))
  322. attackRow[0] = false;
  323. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  324. attackRow[1] = false;
  325. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH + zigzagCorrection))
  326. attackRow[2] = false;
  327. if (myNumber%GameConstants::BFIELD_WIDTH >= GameConstants::BFIELD_WIDTH - 2 || !vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH + 1 + zigzagCorrection))
  328. attackRow[3] = false;
  329. if (!(attackRow[0] && attackRow[1]))
  330. sectorCursor[5] = -1;
  331. if (!(attackRow[1] && attackRow[2]))
  332. belowAttackable = false;
  333. if (!(attackRow[2] && attackRow[3]))
  334. sectorCursor[4] = -1;
  335. }
  336. else
  337. {
  338. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH + zigzagCorrection))
  339. sectorCursor[4] = -1;
  340. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  341. sectorCursor[5] = -1;
  342. }
  343. }
  344. // Determine index from sector.
  345. int cursorIndex;
  346. if (doubleWide)
  347. {
  348. sectorCursor.insert(sectorCursor.begin() + 5, belowAttackable ? 13 : -1);
  349. sectorCursor.insert(sectorCursor.begin() + 2, aboveAttackable ? 14 : -1);
  350. if (sector < 1.5)
  351. cursorIndex = static_cast<int>(sector);
  352. else if (sector >= 1.5 && sector < 2.5)
  353. cursorIndex = 2;
  354. else if (sector >= 2.5 && sector < 4.5)
  355. cursorIndex = (int) sector + 1;
  356. else if (sector >= 4.5 && sector < 5.5)
  357. cursorIndex = 6;
  358. else
  359. cursorIndex = (int) sector + 2;
  360. }
  361. else
  362. {
  363. cursorIndex = static_cast<int>(sector);
  364. }
  365. // Generally should NEVER happen, but to avoid the possibility of having endless loop below... [#1016]
  366. if (!vstd::contains_if (sectorCursor, [](int sc) { return sc != -1; }))
  367. {
  368. logGlobal->error("Error: for hex %d cannot find a hex to attack from!", myNumber);
  369. attackingHex = -1;
  370. return;
  371. }
  372. // Find the closest direction attackable, starting with the right one.
  373. // FIXME: Is this really how the original H3 client does it?
  374. int i = 0;
  375. while (sectorCursor[(cursorIndex + i)%sectorCursor.size()] == -1) //Why hast thou forsaken me?
  376. i = i <= 0 ? 1 - i : -i; // 0, 1, -1, 2, -2, 3, -3 etc..
  377. int index = (cursorIndex + i)%sectorCursor.size(); //hopefully we get elements from sectorCursor
  378. cursor->changeGraphic(ECursor::COMBAT, sectorCursor[index]);
  379. switch (index)
  380. {
  381. case 0:
  382. attackingHex = myNumber - 1; //left
  383. break;
  384. case 1:
  385. attackingHex = myNumber - GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection; //top left
  386. break;
  387. case 2:
  388. attackingHex = myNumber - GameConstants::BFIELD_WIDTH + zigzagCorrection; //top right
  389. break;
  390. case 3:
  391. attackingHex = myNumber + 1; //right
  392. break;
  393. case 4:
  394. attackingHex = myNumber + GameConstants::BFIELD_WIDTH + zigzagCorrection; //bottom right
  395. break;
  396. case 5:
  397. attackingHex = myNumber + GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection; //bottom left
  398. break;
  399. }
  400. BattleHex hex(attackingHex);
  401. if (!hex.isValid())
  402. attackingHex = -1;
  403. }
  404. BattleHex CBattleFieldController::fromWhichHexAttack(BattleHex myNumber)
  405. {
  406. //TODO far too much repeating code
  407. BattleHex destHex;
  408. switch(CCS->curh->frame)
  409. {
  410. case 12: //from bottom right
  411. {
  412. bool doubleWide = owner->stacksController->getActiveStack()->doubleWide();
  413. destHex = myNumber + ( (myNumber/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH+1 ) +
  414. (owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER && doubleWide ? 1 : 0);
  415. if(vstd::contains(occupyableHexes, destHex))
  416. return destHex;
  417. else if(owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  418. {
  419. if (vstd::contains(occupyableHexes, destHex+1))
  420. return destHex+1;
  421. }
  422. else //if we are defender
  423. {
  424. if(vstd::contains(occupyableHexes, destHex-1))
  425. return destHex-1;
  426. }
  427. break;
  428. }
  429. case 7: //from bottom left
  430. {
  431. destHex = myNumber + ( (myNumber/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH-1 : GameConstants::BFIELD_WIDTH );
  432. if (vstd::contains(occupyableHexes, destHex))
  433. return destHex;
  434. else if(owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  435. {
  436. if(vstd::contains(occupyableHexes, destHex+1))
  437. return destHex+1;
  438. }
  439. else //we are defender
  440. {
  441. if(vstd::contains(occupyableHexes, destHex-1))
  442. return destHex-1;
  443. }
  444. break;
  445. }
  446. case 8: //from left
  447. {
  448. if(owner->stacksController->getActiveStack()->doubleWide() && owner->stacksController->getActiveStack()->side == BattleSide::DEFENDER)
  449. {
  450. std::vector<BattleHex> acc = owner->curInt->cb->battleGetAvailableHexes(owner->stacksController->getActiveStack());
  451. if (vstd::contains(acc, myNumber))
  452. return myNumber - 1;
  453. else
  454. return myNumber - 2;
  455. }
  456. else
  457. {
  458. return myNumber - 1;
  459. }
  460. break;
  461. }
  462. case 9: //from top left
  463. {
  464. destHex = myNumber - ((myNumber/GameConstants::BFIELD_WIDTH) % 2 ? GameConstants::BFIELD_WIDTH + 1 : GameConstants::BFIELD_WIDTH);
  465. if(vstd::contains(occupyableHexes, destHex))
  466. return destHex;
  467. else if(owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  468. {
  469. if(vstd::contains(occupyableHexes, destHex+1))
  470. return destHex+1;
  471. }
  472. else //if we are defender
  473. {
  474. if(vstd::contains(occupyableHexes, destHex-1))
  475. return destHex-1;
  476. }
  477. break;
  478. }
  479. case 10: //from top right
  480. {
  481. bool doubleWide = owner->stacksController->getActiveStack()->doubleWide();
  482. destHex = myNumber - ( (myNumber/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH-1 ) +
  483. (owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER && doubleWide ? 1 : 0);
  484. if(vstd::contains(occupyableHexes, destHex))
  485. return destHex;
  486. else if(owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  487. {
  488. if(vstd::contains(occupyableHexes, destHex+1))
  489. return destHex+1;
  490. }
  491. else //if we are defender
  492. {
  493. if(vstd::contains(occupyableHexes, destHex-1))
  494. return destHex-1;
  495. }
  496. break;
  497. }
  498. case 11: //from right
  499. {
  500. if(owner->stacksController->getActiveStack()->doubleWide() && owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  501. {
  502. std::vector<BattleHex> acc = owner->curInt->cb->battleGetAvailableHexes(owner->stacksController->getActiveStack());
  503. if(vstd::contains(acc, myNumber))
  504. return myNumber + 1;
  505. else
  506. return myNumber + 2;
  507. }
  508. else
  509. {
  510. return myNumber + 1;
  511. }
  512. break;
  513. }
  514. case 13: //from bottom
  515. {
  516. destHex = myNumber + ( (myNumber/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH+1 );
  517. if(vstd::contains(occupyableHexes, destHex))
  518. return destHex;
  519. else if(owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  520. {
  521. if(vstd::contains(occupyableHexes, destHex+1))
  522. return destHex+1;
  523. }
  524. else //if we are defender
  525. {
  526. if(vstd::contains(occupyableHexes, destHex-1))
  527. return destHex-1;
  528. }
  529. break;
  530. }
  531. case 14: //from top
  532. {
  533. destHex = myNumber - ( (myNumber/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH-1 );
  534. if (vstd::contains(occupyableHexes, destHex))
  535. return destHex;
  536. else if(owner->stacksController->getActiveStack()->side == BattleSide::ATTACKER)
  537. {
  538. if(vstd::contains(occupyableHexes, destHex+1))
  539. return destHex+1;
  540. }
  541. else //if we are defender
  542. {
  543. if(vstd::contains(occupyableHexes, destHex-1))
  544. return destHex-1;
  545. }
  546. break;
  547. }
  548. }
  549. return -1;
  550. }
  551. bool CBattleFieldController::isTileAttackable(const BattleHex & number) const
  552. {
  553. for (auto & elem : occupyableHexes)
  554. {
  555. if (BattleHex::mutualPosition(elem, number) != -1 || elem == number)
  556. return true;
  557. }
  558. return false;
  559. }
  560. bool CBattleFieldController::stackCountOutsideHex(const BattleHex & number) const
  561. {
  562. return stackCountOutsideHexes[number];
  563. }
  564. void CBattleFieldRenderer::collectObjects()
  565. {
  566. owner->collectRenderableObjects(*this);
  567. owner->effectsController->collectRenderableObjects(*this);
  568. owner->obstacleController->collectRenderableObjects(*this);
  569. owner->siegeController->collectRenderableObjects(*this);
  570. owner->stacksController->collectRenderableObjects(*this);
  571. }
  572. void CBattleFieldRenderer::sortObjects()
  573. {
  574. auto getRow = [](const RenderableInstance & object) -> int
  575. {
  576. if (object.tile.isValid())
  577. return object.tile.getY();
  578. if ( object.tile == BattleHex::HEX_BEFORE_ALL )
  579. return -1;
  580. if ( object.tile == BattleHex::HEX_AFTER_ALL )
  581. return GameConstants::BFIELD_HEIGHT;
  582. if ( object.tile == BattleHex::INVALID )
  583. return GameConstants::BFIELD_HEIGHT;
  584. assert(0);
  585. return GameConstants::BFIELD_HEIGHT;
  586. };
  587. std::stable_sort(objects.begin(), objects.end(), [&](const RenderableInstance & left, const RenderableInstance & right){
  588. if ( getRow(left) != getRow(right))
  589. return getRow(left) < getRow(right);
  590. return left.layer < right.layer;
  591. });
  592. }
  593. void CBattleFieldRenderer::renderObjects(CBattleFieldRenderer::RendererPtr targetCanvas)
  594. {
  595. for (auto const & object : objects)
  596. object.functor(targetCanvas);
  597. }
  598. CBattleFieldRenderer::CBattleFieldRenderer(CBattleInterface * owner):
  599. owner(owner)
  600. {
  601. }
  602. void CBattleFieldRenderer::insert(EBattleFieldLayer layer, BattleHex tile, CBattleFieldRenderer::RenderFunctor functor)
  603. {
  604. objects.push_back({ functor, layer, tile });
  605. }
  606. void CBattleFieldRenderer::execute(CBattleFieldRenderer::RendererPtr targetCanvas)
  607. {
  608. collectObjects();
  609. sortObjects();
  610. renderObjects(targetCanvas);
  611. }