mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-12 10:03:53 +02:00
515 lines
16 KiB
C++
515 lines
16 KiB
C++
/*
|
|
* BattleFieldController.cpp, part of VCMI engine
|
|
*
|
|
* Authors: listed in file AUTHORS in main folder
|
|
*
|
|
* License: GNU General Public License v2.0 or later
|
|
* Full text of license available in license.txt file, in main folder
|
|
*
|
|
*/
|
|
#include "StdInc.h"
|
|
#include "BattleFieldController.h"
|
|
|
|
#include "BattleInterface.h"
|
|
#include "BattleActionsController.h"
|
|
#include "BattleInterfaceClasses.h"
|
|
#include "BattleEffectsController.h"
|
|
#include "BattleSiegeController.h"
|
|
#include "BattleStacksController.h"
|
|
#include "BattleObstacleController.h"
|
|
#include "BattleProjectileController.h"
|
|
#include "BattleRenderer.h"
|
|
|
|
#include "../CGameInfo.h"
|
|
#include "../CPlayerInterface.h"
|
|
#include "../gui/CAnimation.h"
|
|
#include "../gui/Canvas.h"
|
|
#include "../gui/CGuiHandler.h"
|
|
#include "../gui/CCursorHandler.h"
|
|
|
|
#include "../../CCallback.h"
|
|
#include "../../lib/BattleFieldHandler.h"
|
|
#include "../../lib/CConfigHandler.h"
|
|
#include "../../lib/CStack.h"
|
|
#include "../../lib/spells/ISpellMechanics.h"
|
|
|
|
BattleFieldController::BattleFieldController(BattleInterface & owner):
|
|
owner(owner)
|
|
{
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
|
pos.w = owner.pos.w;
|
|
pos.h = owner.pos.h;
|
|
|
|
//preparing cells and hexes
|
|
cellBorder = IImage::createFromFile("CCELLGRD.BMP");
|
|
cellShade = IImage::createFromFile("CCELLSHD.BMP");
|
|
|
|
if(!owner.siegeController)
|
|
{
|
|
auto bfieldType = owner.curInt->cb->battleGetBattlefieldType();
|
|
|
|
if(bfieldType == BattleField::NONE)
|
|
logGlobal->error("Invalid battlefield returned for current battle");
|
|
else
|
|
background = IImage::createFromFile(bfieldType.getInfo()->graphics);
|
|
}
|
|
else
|
|
{
|
|
std::string backgroundName = owner.siegeController->getBattleBackgroundName();
|
|
background = IImage::createFromFile(backgroundName);
|
|
}
|
|
|
|
//preparing graphic with cell borders
|
|
cellBorders = std::make_unique<Canvas>(Point(background->width(), background->height()));
|
|
|
|
for (int i=0; i<GameConstants::BFIELD_SIZE; ++i)
|
|
{
|
|
if ( i % GameConstants::BFIELD_WIDTH == 0)
|
|
continue;
|
|
if ( i % GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_WIDTH - 1)
|
|
continue;
|
|
|
|
cellBorders->draw(cellBorder, hexPositionLocal(i).topLeft());
|
|
}
|
|
|
|
backgroundWithHexes = std::make_unique<Canvas>(Point(background->width(), background->height()));
|
|
|
|
for (int h = 0; h < GameConstants::BFIELD_SIZE; ++h)
|
|
{
|
|
auto hex = std::make_shared<ClickableHex>();
|
|
hex->myNumber = h;
|
|
hex->pos = hexPositionAbsolute(h);
|
|
hex->myInterface = &owner;
|
|
bfield.push_back(hex);
|
|
}
|
|
|
|
auto accessibility = owner.curInt->cb->getAccesibility();
|
|
for(int i = 0; i < accessibility.size(); i++)
|
|
stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE);
|
|
}
|
|
|
|
void BattleFieldController::renderBattlefield(Canvas & canvas)
|
|
{
|
|
showBackground(canvas);
|
|
|
|
BattleRenderer renderer(owner);
|
|
|
|
renderer.execute(canvas);
|
|
|
|
owner.projectilesController->showProjectiles(canvas);
|
|
}
|
|
|
|
void BattleFieldController::showBackground(Canvas & canvas)
|
|
{
|
|
if (owner.stacksController->getActiveStack() != nullptr ) //&& creAnims[stacksController->getActiveStack()->ID]->isIdle() //show everything with range
|
|
showBackgroundImageWithHexes(canvas);
|
|
else
|
|
showBackgroundImage(canvas);
|
|
|
|
showHighlightedHexes(canvas);
|
|
|
|
}
|
|
|
|
void BattleFieldController::showBackgroundImage(Canvas & canvas)
|
|
{
|
|
canvas.draw(background, owner.pos.topLeft());
|
|
|
|
owner.obstacleController->showAbsoluteObstacles(canvas, pos.topLeft());
|
|
if ( owner.siegeController )
|
|
owner.siegeController->showAbsoluteObstacles(canvas, pos.topLeft());
|
|
|
|
if (settings["battle"]["cellBorders"].Bool())
|
|
canvas.draw(*cellBorders, owner.pos.topLeft());
|
|
}
|
|
|
|
void BattleFieldController::showBackgroundImageWithHexes(Canvas & canvas)
|
|
{
|
|
canvas.draw(*backgroundWithHexes.get(), owner.pos.topLeft());
|
|
}
|
|
|
|
void BattleFieldController::redrawBackgroundWithHexes()
|
|
{
|
|
const CStack *activeStack = owner.stacksController->getActiveStack();
|
|
std::vector<BattleHex> attackableHexes;
|
|
if (activeStack)
|
|
occupyableHexes = owner.curInt->cb->battleGetAvailableHexes(activeStack, true, &attackableHexes);
|
|
|
|
auto accessibility = owner.curInt->cb->getAccesibility();
|
|
|
|
for(int i = 0; i < accessibility.size(); i++)
|
|
stackCountOutsideHexes[i] = (accessibility[i] == EAccessibility::ACCESSIBLE);
|
|
|
|
//prepare background graphic with hexes and shaded hexes
|
|
backgroundWithHexes->draw(background, Point(0,0));
|
|
owner.obstacleController->showAbsoluteObstacles(*backgroundWithHexes, Point(0,0));
|
|
if ( owner.siegeController )
|
|
owner.siegeController->showAbsoluteObstacles(*backgroundWithHexes, Point(0,0));
|
|
|
|
if (settings["battle"]["stackRange"].Bool())
|
|
{
|
|
std::vector<BattleHex> hexesToShade = occupyableHexes;
|
|
hexesToShade.insert(hexesToShade.end(), attackableHexes.begin(), attackableHexes.end());
|
|
for (BattleHex hex : hexesToShade)
|
|
{
|
|
backgroundWithHexes->draw(cellShade, hexPositionLocal(hex).topLeft());
|
|
}
|
|
}
|
|
|
|
if(settings["battle"]["cellBorders"].Bool())
|
|
backgroundWithHexes->draw(*cellBorders, Point(0, 0));
|
|
}
|
|
|
|
void BattleFieldController::showHighlightedHex(Canvas & canvas, BattleHex hex, bool darkBorder)
|
|
{
|
|
Point hexPos = hexPositionAbsolute(hex).topLeft();
|
|
|
|
canvas.draw(cellShade, hexPos);
|
|
if(!darkBorder && settings["battle"]["cellBorders"].Bool())
|
|
canvas.draw(cellBorder, hexPos);
|
|
}
|
|
|
|
std::set<BattleHex> BattleFieldController::getHighlightedHexesStackRange()
|
|
{
|
|
std::set<BattleHex> result;
|
|
|
|
if ( !owner.stacksController->getActiveStack())
|
|
return result;
|
|
|
|
if ( !settings["battle"]["stackRange"].Bool())
|
|
return result;
|
|
|
|
auto hoveredHex = getHoveredHex();
|
|
|
|
std::set<BattleHex> set = owner.curInt->cb->battleGetAttackedHexes(owner.stacksController->getActiveStack(), hoveredHex, attackingHex);
|
|
for(BattleHex hex : set)
|
|
result.insert(hex);
|
|
|
|
// display the movement shadow of the stack at b (i.e. stack under mouse)
|
|
const CStack * const shere = owner.curInt->cb->battleGetStackByPos(hoveredHex, false);
|
|
if(shere && shere != owner.stacksController->getActiveStack() && shere->alive())
|
|
{
|
|
std::vector<BattleHex> v = owner.curInt->cb->battleGetAvailableHexes(shere, true, nullptr);
|
|
for(BattleHex hex : v)
|
|
result.insert(hex);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::set<BattleHex> BattleFieldController::getHighlightedHexesSpellRange()
|
|
{
|
|
std::set<BattleHex> result;
|
|
auto hoveredHex = getHoveredHex();
|
|
|
|
if(!settings["battle"]["mouseShadow"].Bool())
|
|
return result;
|
|
|
|
const spells::Caster *caster = nullptr;
|
|
const CSpell *spell = nullptr;
|
|
|
|
spells::Mode mode = spells::Mode::HERO;
|
|
|
|
if(owner.actionsController->spellcastingModeActive())//hero casts spell
|
|
{
|
|
spell = owner.actionsController->selectedSpell().toSpell();
|
|
caster = owner.getActiveHero();
|
|
}
|
|
else if(owner.stacksController->activeStackSpellToCast() != SpellID::NONE)//stack casts spell
|
|
{
|
|
spell = SpellID(owner.stacksController->activeStackSpellToCast()).toSpell();
|
|
caster = owner.stacksController->getActiveStack();
|
|
mode = spells::Mode::CREATURE_ACTIVE;
|
|
}
|
|
|
|
if(caster && spell) //when casting spell
|
|
{
|
|
// printing shaded hex(es)
|
|
spells::BattleCast event(owner.curInt->cb.get(), caster, mode, spell);
|
|
auto shaded = spell->battleMechanics(&event)->rangeInHexes(hoveredHex);
|
|
|
|
for(BattleHex shadedHex : shaded)
|
|
{
|
|
if((shadedHex.getX() != 0) && (shadedHex.getX() != GameConstants::BFIELD_WIDTH - 1))
|
|
result.insert(shadedHex);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::set<BattleHex> BattleFieldController::getHighlightedHexesMovementTarget()
|
|
{
|
|
const CStack * stack = owner.stacksController->getActiveStack();
|
|
auto hoveredHex = getHoveredHex();
|
|
|
|
if (stack)
|
|
{
|
|
std::vector<BattleHex> v = owner.curInt->cb->battleGetAvailableHexes(stack, false, nullptr);
|
|
|
|
auto hoveredStack = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
|
|
if(owner.curInt->cb->battleCanAttack(stack, hoveredStack, hoveredHex))
|
|
{
|
|
if (isTileAttackable(hoveredHex))
|
|
{
|
|
BattleHex attackFromHex = fromWhichHexAttack(hoveredHex);
|
|
|
|
if (stack->doubleWide())
|
|
return {attackFromHex, stack->occupiedHex(attackFromHex)};
|
|
else
|
|
return {attackFromHex};
|
|
}
|
|
}
|
|
|
|
if (vstd::contains(v,hoveredHex))
|
|
{
|
|
if (stack->doubleWide())
|
|
return {hoveredHex, stack->occupiedHex(hoveredHex)};
|
|
else
|
|
return {hoveredHex};
|
|
}
|
|
if (stack->doubleWide())
|
|
{
|
|
for (auto const & hex : v)
|
|
{
|
|
if (stack->occupiedHex(hex) == hoveredHex)
|
|
return { hoveredHex, hex };
|
|
}
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void BattleFieldController::showHighlightedHexes(Canvas & canvas)
|
|
{
|
|
std::set<BattleHex> hoveredStack = getHighlightedHexesStackRange();
|
|
std::set<BattleHex> hoveredSpell = getHighlightedHexesSpellRange();
|
|
std::set<BattleHex> hoveredMove = getHighlightedHexesMovementTarget();
|
|
|
|
auto const & hoveredMouse = owner.actionsController->spellcastingModeActive() ? hoveredSpell : hoveredMove;
|
|
|
|
for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
|
|
{
|
|
bool stack = hoveredStack.count(b);
|
|
bool mouse = hoveredMouse.count(b);
|
|
|
|
if ( stack && mouse )
|
|
{
|
|
// area where enemy stack can move AND affected by mouse cursor - create darker highlight by blitting twice
|
|
showHighlightedHex(canvas, b, true);
|
|
showHighlightedHex(canvas, b, true);
|
|
}
|
|
if ( !stack && mouse )
|
|
{
|
|
showHighlightedHex(canvas, b, true);
|
|
}
|
|
if ( stack && !mouse )
|
|
{
|
|
showHighlightedHex(canvas, b, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
Rect BattleFieldController::hexPositionLocal(BattleHex hex) const
|
|
{
|
|
int x = 14 + ((hex.getY())%2==0 ? 22 : 0) + 44*hex.getX();
|
|
int y = 86 + 42 *hex.getY();
|
|
int w = cellShade->width();
|
|
int h = cellShade->height();
|
|
return Rect(x, y, w, h);
|
|
}
|
|
|
|
Rect BattleFieldController::hexPositionAbsolute(BattleHex hex) const
|
|
{
|
|
return hexPositionLocal(hex) + owner.pos.topLeft();
|
|
}
|
|
|
|
bool BattleFieldController::isPixelInHex(Point const & position)
|
|
{
|
|
return !cellShade->isTransparent(position);
|
|
}
|
|
|
|
BattleHex BattleFieldController::getHoveredHex()
|
|
{
|
|
for ( auto const & hex : bfield)
|
|
if (hex->hovered && hex->strictHovered)
|
|
return hex->myNumber;
|
|
|
|
return BattleHex::INVALID;
|
|
}
|
|
|
|
void BattleFieldController::setBattleCursor(BattleHex myNumber)
|
|
{
|
|
Point cursorPos = CCS->curh->position();
|
|
|
|
std::vector<Cursor::Combat> sectorCursor = {
|
|
Cursor::Combat::HIT_SOUTHEAST,
|
|
Cursor::Combat::HIT_SOUTHWEST,
|
|
Cursor::Combat::HIT_WEST,
|
|
Cursor::Combat::HIT_NORTHWEST,
|
|
Cursor::Combat::HIT_NORTHEAST,
|
|
Cursor::Combat::HIT_EAST,
|
|
Cursor::Combat::HIT_SOUTH,
|
|
Cursor::Combat::HIT_NORTH,
|
|
};
|
|
|
|
auto direction = static_cast<size_t>(selectAttackDirection(myNumber, cursorPos));
|
|
|
|
assert(direction != -1);
|
|
if (direction != -1)
|
|
CCS->curh->set(sectorCursor[direction]);
|
|
}
|
|
|
|
BattleHex::EDir BattleFieldController::selectAttackDirection(BattleHex myNumber, const Point & cursorPos)
|
|
{
|
|
const bool doubleWide = owner.stacksController->getActiveStack()->doubleWide();
|
|
auto neighbours = myNumber.allNeighbouringTiles();
|
|
// 0 1
|
|
// 5 x 2
|
|
// 4 3
|
|
|
|
// if true - our current stack can move into this hex (and attack)
|
|
std::array<bool, 8> attackAvailability;
|
|
|
|
if (doubleWide)
|
|
{
|
|
// For double-hexes we need to ensure that both hexes needed for this direction are occupyable:
|
|
// | -0- | -1- | -2- | -3- | -4- | -5- | -6- | -7-
|
|
// | o o - | - o o | - - | - - | - - | - - | o o | - -
|
|
// | - x - | - x - | - x o o| - x - | - x - |o o x - | - x - | - x -
|
|
// | - - | - - | - - | - o o | o o - | - - | - - | o o
|
|
|
|
for (size_t i : { 1, 2, 3})
|
|
attackAvailability[i] = vstd::contains(occupyableHexes, neighbours[i]) && vstd::contains(occupyableHexes, neighbours[i].cloneInDirection(BattleHex::RIGHT, false));
|
|
|
|
for (size_t i : { 4, 5, 0})
|
|
attackAvailability[i] = vstd::contains(occupyableHexes, neighbours[i]) && vstd::contains(occupyableHexes, neighbours[i].cloneInDirection(BattleHex::LEFT, false));
|
|
|
|
attackAvailability[6] = vstd::contains(occupyableHexes, neighbours[0]) && vstd::contains(occupyableHexes, neighbours[1]);
|
|
attackAvailability[7] = vstd::contains(occupyableHexes, neighbours[3]) && vstd::contains(occupyableHexes, neighbours[4]);
|
|
}
|
|
else
|
|
{
|
|
for (size_t i = 0; i < 6; ++i)
|
|
attackAvailability[i] = vstd::contains(occupyableHexes, neighbours[i]);
|
|
|
|
attackAvailability[6] = false;
|
|
attackAvailability[7] = false;
|
|
}
|
|
|
|
// Zero available tiles to attack from
|
|
if ( vstd::find(attackAvailability, true) == attackAvailability.end())
|
|
{
|
|
logGlobal->error("Error: cannot find a hex to attack hex %d from!", myNumber);
|
|
return BattleHex::NONE;
|
|
}
|
|
|
|
// For each valid direction, select position to test against
|
|
std::array<Point, 8> testPoint;
|
|
|
|
for (size_t i = 0; i < 6; ++i)
|
|
if (attackAvailability[i])
|
|
testPoint[i] = hexPositionAbsolute(neighbours[i]).center();
|
|
|
|
// For bottom/top directions select central point, but move it a bit away from true center to reduce zones allocated to them
|
|
if (attackAvailability[6])
|
|
testPoint[6] = (hexPositionAbsolute(neighbours[0]).center() + hexPositionAbsolute(neighbours[1]).center()) / 2 + Point(0, -5);
|
|
|
|
if (attackAvailability[7])
|
|
testPoint[7] = (hexPositionAbsolute(neighbours[3]).center() + hexPositionAbsolute(neighbours[4]).center()) / 2 + Point(0, 5);
|
|
|
|
// Compute distance between tested position & cursor position and pick nearest
|
|
std::array<int, 8> distance2;
|
|
|
|
for (size_t i = 0; i < 8; ++i)
|
|
if (attackAvailability[i])
|
|
distance2[i] = (testPoint[i].y - cursorPos.y)*(testPoint[i].y - cursorPos.y) + (testPoint[i].x - cursorPos.x)*(testPoint[i].x - cursorPos.x);
|
|
|
|
size_t nearest = -1;
|
|
for (size_t i = 0; i < 8; ++i)
|
|
if (attackAvailability[i] && (nearest == -1 || distance2[i] < distance2[nearest]) )
|
|
nearest = i;
|
|
|
|
assert(nearest != -1);
|
|
return BattleHex::EDir(nearest);
|
|
}
|
|
|
|
BattleHex BattleFieldController::fromWhichHexAttack(BattleHex attackTarget)
|
|
{
|
|
BattleHex::EDir direction = selectAttackDirection(attackTarget, CCS->curh->position());
|
|
|
|
const CStack * attacker = owner.stacksController->getActiveStack();
|
|
|
|
assert(direction != BattleHex::NONE);
|
|
assert(attacker);
|
|
|
|
if (!attacker->doubleWide())
|
|
{
|
|
assert(direction != BattleHex::BOTTOM);
|
|
assert(direction != BattleHex::TOP);
|
|
return attackTarget.cloneInDirection(direction);
|
|
}
|
|
else
|
|
{
|
|
// We need to find position of right hex of double-hex creature (or left for defending side)
|
|
// | TOP_LEFT |TOP_RIGHT | RIGHT |BOTTOM_RIGHT|BOTTOM_LEFT| LEFT | TOP |BOTTOM
|
|
// | o o - | - o o | - - | - - | - - | - - | o o | - -
|
|
// | - x - | - x - | - x o o| - x - | - x - |o o x - | - x - | - x -
|
|
// | - - | - - | - - | - o o | o o - | - - | - - | o o
|
|
|
|
switch (direction)
|
|
{
|
|
case BattleHex::TOP_LEFT:
|
|
case BattleHex::LEFT:
|
|
case BattleHex::BOTTOM_LEFT:
|
|
{
|
|
if ( attacker->side == BattleSide::ATTACKER )
|
|
return attackTarget.cloneInDirection(direction);
|
|
else
|
|
return attackTarget.cloneInDirection(direction).cloneInDirection(BattleHex::LEFT);
|
|
}
|
|
|
|
case BattleHex::TOP_RIGHT:
|
|
case BattleHex::RIGHT:
|
|
case BattleHex::BOTTOM_RIGHT:
|
|
{
|
|
if ( attacker->side == BattleSide::ATTACKER )
|
|
return attackTarget.cloneInDirection(direction).cloneInDirection(BattleHex::RIGHT);
|
|
else
|
|
return attackTarget.cloneInDirection(direction);
|
|
}
|
|
|
|
case BattleHex::TOP:
|
|
{
|
|
if ( attacker->side == BattleSide::ATTACKER )
|
|
return attackTarget.cloneInDirection(BattleHex::TOP_RIGHT);
|
|
else
|
|
return attackTarget.cloneInDirection(BattleHex::TOP_LEFT);
|
|
}
|
|
|
|
case BattleHex::BOTTOM:
|
|
{
|
|
if ( attacker->side == BattleSide::ATTACKER )
|
|
return attackTarget.cloneInDirection(BattleHex::BOTTOM_RIGHT);
|
|
else
|
|
return attackTarget.cloneInDirection(BattleHex::BOTTOM_LEFT);
|
|
}
|
|
default:
|
|
assert(0);
|
|
return attackTarget.cloneInDirection(BattleHex::LEFT);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool BattleFieldController::isTileAttackable(const BattleHex & number) const
|
|
{
|
|
for (auto & elem : occupyableHexes)
|
|
{
|
|
if (BattleHex::mutualPosition(elem, number) != -1 || elem == number)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool BattleFieldController::stackCountOutsideHex(const BattleHex & number) const
|
|
{
|
|
return stackCountOutsideHexes[number];
|
|
}
|