1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00

Aesthetic changes in BattleHex (#303)

This commit is contained in:
FeniksFire 2017-05-29 09:33:34 +02:00 committed by ArseniyShestakov
parent 8a494b7820
commit 039e3842fc
5 changed files with 52 additions and 74 deletions

View File

@ -76,7 +76,7 @@ std::pair<si16, si16> BattleHex::getXY() const
return std::make_pair(getX(), getY());
}
BattleHex& BattleHex::moveInDir(EDir dir, bool hasToBeValid)
BattleHex& BattleHex::moveInDirection(EDir dir, bool hasToBeValid)
{
si16 x(getX()), y(getY());
switch(dir)
@ -102,58 +102,41 @@ BattleHex& BattleHex::moveInDir(EDir dir, bool hasToBeValid)
default:
throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
break;
}
}
return *this;
}
BattleHex &BattleHex::operator+=(BattleHex::EDir dir)
{
return moveInDir(dir);
return moveInDirection(dir);
}
BattleHex BattleHex::movedInDir(BattleHex::EDir dir, bool hasToBeValid) const
BattleHex BattleHex::cloneInDirection(BattleHex::EDir dir, bool hasToBeValid) const
{
BattleHex result(*this);
result.moveInDir(dir, hasToBeValid);
BattleHex result(hex);
result.moveInDirection(dir, hasToBeValid);
return result;
}
BattleHex BattleHex::operator+(BattleHex::EDir dir) const
{
return movedInDir(dir);
return cloneInDirection(dir);
}
std::vector<BattleHex> BattleHex::neighbouringTiles() const
{
std::vector<BattleHex> ret;
const int WN = GameConstants::BFIELD_WIDTH;
// H3 order : TR, R, BR, BL, L, TL (T = top, B = bottom ...)
checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret); // 1
checkAndPush(hex + 1, ret); // 2
checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret); // 3
checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret); // 4
checkAndPush(hex - 1, ret); // 5
checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret); // 6
for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
checkAndPush(cloneInDirection(dir, false), ret);
return ret;
}
signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
{
if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
return 0;
if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
return 1;
if(hex2 == hex1 + 1 && hex1%17 != 16) //right
return 2;
if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
return 3;
if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
return 4;
if(hex2 == hex1 - 1 && hex1%17 != 0) //left
return 5;
return -1;
for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
if(hex2 == hex1.cloneInDirection(dir,false))
return dir;
return INVALID;
}
char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)

View File

@ -15,7 +15,7 @@ struct DLL_LINKAGE BattleHex //TODO: decide if this should be changed to class f
{
si16 hex;
static const si16 INVALID = -1;
enum EDir { RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT };
enum EDir { TOP_LEFT, TOP_RIGHT, RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT};
BattleHex();
BattleHex(si16 _hex);
@ -31,16 +31,12 @@ struct DLL_LINKAGE BattleHex //TODO: decide if this should be changed to class f
si16 getX() const;
si16 getY() const;
std::pair<si16, si16> getXY() const;
//moving to direction
BattleHex& moveInDir(EDir dir, bool hasToBeValid = true);
BattleHex& operator+=(EDir dir); //sugar for above
//generates new BattleHex moved by given dir
BattleHex movedInDir(EDir dir, bool hasToBeValid = true) const;
BattleHex& moveInDirection(EDir dir, bool hasToBeValid = true);
BattleHex& operator+=(EDir dir);
BattleHex cloneInDirection(EDir dir, bool hasToBeValid = true) const;
BattleHex operator+(EDir dir) const;
std::vector<BattleHex> neighbouringTiles() const;
//returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)
static signed char mutualPosition(BattleHex hex1, BattleHex hex2);
//returns distance between given hexes
static char getDistance(BattleHex hex1, BattleHex hex2);
static void checkAndPush(BattleHex tile, std::vector<BattleHex> & ret);
static BattleHex getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities); //TODO: vector or set? copying one to another is bad

View File

@ -566,9 +566,9 @@ std::vector<BattleHex> WallMechanics::rangeInHexes(BattleHex centralHex, ui8 sch
};
ret.push_back(centralHex);
addIfValid(centralHex.moveInDir(firstStep, false));
addIfValid(centralHex.moveInDirection(firstStep, false));
if(schoolLvl >= 2) //advanced versions of fire wall / force field cotnains of 3 hexes
addIfValid(centralHex.moveInDir(secondStep, false)); //moveInDir function modifies subject hex
addIfValid(centralHex.moveInDirection(secondStep, false)); //moveInDir function modifies subject hex
return ret;
}

View File

@ -144,31 +144,31 @@ static void summonGuardiansHelper(std::vector<BattleHex> & output, const BattleH
int y = targetPosition.getY();
if (targetIsAttacker) //handle front guardians, TODO: should we handle situation when units start battle near opposite side of the battlefield? Cannot happen in normal H3...
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false).movedInDir(BattleHex::EDir::RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false).cloneInDirection(BattleHex::EDir::RIGHT, false), output);
else
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false).movedInDir(BattleHex::EDir::LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false).cloneInDirection(BattleHex::EDir::LEFT, false), output);
//guardian spawn locations for four default position cases for attacker and defender, non-default starting location for att and def is handled in first two if's
if (targetIsAttacker && ((y % 2 == 0) || (x > 1)))
{
if (targetIsTwoHex && (y % 2 == 1) && (x == 2)) //handle exceptional case
{
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::BOTTOM_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::BOTTOM_RIGHT, false), output);
}
else
{ //add back-side guardians for two-hex target, side guardians for one-hex
BattleHex::checkAndPush(targetPosition.movedInDir(targetIsTwoHex ? BattleHex::EDir::TOP_LEFT : BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(targetIsTwoHex ? BattleHex::EDir::BOTTOM_LEFT : BattleHex::EDir::BOTTOM_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(targetIsTwoHex ? BattleHex::EDir::TOP_LEFT : BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(targetIsTwoHex ? BattleHex::EDir::BOTTOM_LEFT : BattleHex::EDir::BOTTOM_RIGHT, false), output);
if (!targetIsTwoHex && x > 2) //back guard for one-hex
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false), output);
else if (targetIsTwoHex)//front-side guardians for two-hex target
{
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false).movedInDir(BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false).movedInDir(BattleHex::EDir::BOTTOM_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false).cloneInDirection(BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false).cloneInDirection(BattleHex::EDir::BOTTOM_RIGHT, false), output);
if (x > 3) //back guard for two-hex
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false).movedInDir(BattleHex::EDir::LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false).cloneInDirection(BattleHex::EDir::LEFT, false), output);
}
}
@ -178,36 +178,36 @@ static void summonGuardiansHelper(std::vector<BattleHex> & output, const BattleH
{
if (targetIsTwoHex && (y % 2 == 0) && (x == GameConstants::BFIELD_WIDTH - 3)) //handle exceptional case... equivalent for above for defender side
{
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::BOTTOM_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), output);
}
else
{
BattleHex::checkAndPush(targetPosition.movedInDir(targetIsTwoHex ? BattleHex::EDir::TOP_RIGHT : BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(targetIsTwoHex ? BattleHex::EDir::BOTTOM_RIGHT : BattleHex::EDir::BOTTOM_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(targetIsTwoHex ? BattleHex::EDir::TOP_RIGHT : BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(targetIsTwoHex ? BattleHex::EDir::BOTTOM_RIGHT : BattleHex::EDir::BOTTOM_LEFT, false), output);
if (!targetIsTwoHex && x < GameConstants::BFIELD_WIDTH - 3)
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false), output);
else if (targetIsTwoHex)
{
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false).movedInDir(BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false).movedInDir(BattleHex::EDir::BOTTOM_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false).cloneInDirection(BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false).cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), output);
if (x < GameConstants::BFIELD_WIDTH - 4)
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false).movedInDir(BattleHex::EDir::RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false).cloneInDirection(BattleHex::EDir::RIGHT, false), output);
}
}
}
else if (!targetIsAttacker && y % 2 == 0)
{
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false).movedInDir(BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::LEFT, false).movedInDir(BattleHex::EDir::BOTTOM_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false).cloneInDirection(BattleHex::EDir::TOP_LEFT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::LEFT, false).cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), output);
}
else if (targetIsAttacker && y % 2 == 1)
{
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false).movedInDir(BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.movedInDir(BattleHex::EDir::RIGHT, false).movedInDir(BattleHex::EDir::BOTTOM_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false).cloneInDirection(BattleHex::EDir::TOP_RIGHT, false), output);
BattleHex::checkAndPush(targetPosition.cloneInDirection(BattleHex::EDir::RIGHT, false).cloneInDirection(BattleHex::EDir::BOTTOM_RIGHT, false), output);
}
}

View File

@ -10,7 +10,6 @@
#include "StdInc.h"
#include <boost/test/unit_test.hpp>
#include "../lib/BattleHex.h"
BOOST_AUTO_TEST_SUITE(BattlefieldHex_Suite)
BOOST_AUTO_TEST_CASE(getNeighbouringTiles)
@ -38,11 +37,11 @@ BOOST_AUTO_TEST_CASE(getNeighbouringTiles)
BOOST_REQUIRE(neighbouringTiles.size()==6 && mainHex==93);
BOOST_TEST(neighbouringTiles.at(0)==75);
BOOST_TEST(neighbouringTiles.at(1)==94);
BOOST_TEST(neighbouringTiles.at(2)==110);
BOOST_TEST(neighbouringTiles.at(3)==109);
BOOST_TEST(neighbouringTiles.at(4)==92);
BOOST_TEST(neighbouringTiles.at(5)==76);
BOOST_TEST(neighbouringTiles.at(1)==76);
BOOST_TEST(neighbouringTiles.at(2)==94);
BOOST_TEST(neighbouringTiles.at(3)==110);
BOOST_TEST(neighbouringTiles.at(4)==109);
BOOST_TEST(neighbouringTiles.at(5)==92);
}
BOOST_AUTO_TEST_CASE(getDistance)
@ -107,17 +106,17 @@ BOOST_AUTO_TEST_CASE(getClosestTile)
BOOST_AUTO_TEST_CASE(moveEDir)
{
BattleHex mainHex(20);
mainHex.moveInDir(BattleHex::EDir::BOTTOM_RIGHT);
mainHex.moveInDirection(BattleHex::EDir::BOTTOM_RIGHT);
BOOST_TEST(mainHex==37);
mainHex.moveInDir(BattleHex::EDir::RIGHT);
mainHex.moveInDirection(BattleHex::EDir::RIGHT);
BOOST_TEST(mainHex==38);
mainHex.moveInDir(BattleHex::EDir::TOP_RIGHT);
mainHex.moveInDirection(BattleHex::EDir::TOP_RIGHT);
BOOST_TEST(mainHex==22);
mainHex.moveInDir(BattleHex::EDir::TOP_LEFT);
mainHex.moveInDirection(BattleHex::EDir::TOP_LEFT);
BOOST_TEST(mainHex==4);
mainHex.moveInDir(BattleHex::EDir::LEFT);
mainHex.moveInDirection(BattleHex::EDir::LEFT);
BOOST_TEST(mainHex==3);
mainHex.moveInDir(BattleHex::EDir::BOTTOM_LEFT);
mainHex.moveInDirection(BattleHex::EDir::BOTTOM_LEFT);
BOOST_TEST(mainHex==20);
}