2011-12-14 00:35:28 +03:00
|
|
|
#include "StdInc.h"
|
2011-12-22 16:05:19 +03:00
|
|
|
#include "BattleHex.h"
|
2011-12-14 00:35:28 +03:00
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void BattleHex::operator+=(EDir dir)
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
|
|
|
si16 x = getX(),
|
|
|
|
y = getY();
|
|
|
|
|
|
|
|
switch(dir)
|
|
|
|
{
|
|
|
|
case TOP_LEFT:
|
|
|
|
setXY(y%2 ? x-1 : x, y-1);
|
|
|
|
break;
|
|
|
|
case TOP_RIGHT:
|
|
|
|
setXY(y%2 ? x : x+1, y-1);
|
|
|
|
break;
|
|
|
|
case RIGHT:
|
|
|
|
setXY(x+1, y);
|
|
|
|
break;
|
|
|
|
case BOTTOM_RIGHT:
|
|
|
|
setXY(y%2 ? x : x+1, y+1);
|
|
|
|
break;
|
|
|
|
case BOTTOM_LEFT:
|
|
|
|
setXY(y%2 ? x-1 : x, y+1);
|
|
|
|
break;
|
|
|
|
case LEFT:
|
|
|
|
setXY(x-1, y);
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-22 10:32:45 +03:00
|
|
|
throw std::runtime_error("Disaster: wrong direction in BattleHex::operator+=!\n");
|
2011-12-14 00:35:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
BattleHex BattleHex::operator+(EDir dir) const
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
BattleHex ret(*this);
|
2011-12-14 00:35:28 +03:00
|
|
|
ret += dir;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> BattleHex::neighbouringTiles() const
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> ret;
|
2011-12-14 00:35:28 +03:00
|
|
|
const int WN = GameConstants::BFIELD_WIDTH;
|
|
|
|
checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret);
|
|
|
|
checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret);
|
|
|
|
checkAndPush(hex - 1, ret);
|
|
|
|
checkAndPush(hex + 1, ret);
|
|
|
|
checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret);
|
|
|
|
checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
|
|
|
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 != 0) //left
|
|
|
|
return 5;
|
|
|
|
if(hex2 == hex1 + 1 && hex1%17 != 16) //right
|
|
|
|
return 2;
|
|
|
|
if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
|
|
|
|
return 4;
|
|
|
|
if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
|
|
|
|
return 3;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
|
|
|
int xDst = std::abs(hex1 % GameConstants::BFIELD_WIDTH - hex2 % GameConstants::BFIELD_WIDTH),
|
|
|
|
yDst = std::abs(hex1 / GameConstants::BFIELD_WIDTH - hex2 / GameConstants::BFIELD_WIDTH);
|
|
|
|
return std::max(xDst, yDst) + std::min(xDst, yDst) - (yDst + 1)/2;
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void BattleHex::checkAndPush(int tile, std::vector<BattleHex> & ret)
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
|
|
|
if( tile>=0 && tile<GameConstants::BFIELD_SIZE && (tile%GameConstants::BFIELD_WIDTH != (GameConstants::BFIELD_WIDTH - 1))
|
|
|
|
&& (tile%GameConstants::BFIELD_WIDTH != 0) )
|
2011-12-22 16:05:19 +03:00
|
|
|
ret.push_back(BattleHex(tile));
|
2012-03-31 00:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BattleHex::isAvailable() const
|
|
|
|
{
|
|
|
|
return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
|
2011-12-14 00:35:28 +03:00
|
|
|
}
|