2012-12-19 17:54:10 +03:00
|
|
|
/*
|
|
|
|
* BattleHex.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
|
|
|
|
*
|
|
|
|
*/
|
2017-06-29 01:02:05 +02:00
|
|
|
#include "../StdInc.h"
|
2017-01-29 17:38:13 +02:00
|
|
|
#include "BattleHex.h"
|
2012-12-19 17:54:10 +03:00
|
|
|
|
2017-01-29 17:38:13 +02:00
|
|
|
BattleHex::BattleHex() : hex(INVALID) {}
|
|
|
|
|
|
|
|
BattleHex::BattleHex(si16 _hex) : hex(_hex) {}
|
|
|
|
|
|
|
|
BattleHex::BattleHex(si16 x, si16 y)
|
|
|
|
{
|
|
|
|
setXY(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleHex::BattleHex(std::pair<si16, si16> xy)
|
|
|
|
{
|
|
|
|
setXY(xy);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleHex::operator si16() const
|
|
|
|
{
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BattleHex::isValid() const
|
|
|
|
{
|
|
|
|
return hex >= 0 && hex < GameConstants::BFIELD_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BattleHex::isAvailable() const
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
2017-01-29 17:38:13 +02:00
|
|
|
return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleHex::setX(si16 x)
|
|
|
|
{
|
|
|
|
setXY(x, getY());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleHex::setY(si16 y)
|
|
|
|
{
|
|
|
|
setXY(getX(), y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleHex::setXY(si16 x, si16 y, bool hasToBeValid)
|
|
|
|
{
|
|
|
|
if(hasToBeValid)
|
|
|
|
assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT);
|
|
|
|
hex = x + y * GameConstants::BFIELD_WIDTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleHex::setXY(std::pair<si16, si16> xy)
|
|
|
|
{
|
|
|
|
setXY(xy.first, xy.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
si16 BattleHex::getX() const
|
|
|
|
{
|
|
|
|
return hex % GameConstants::BFIELD_WIDTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
si16 BattleHex::getY() const
|
|
|
|
{
|
|
|
|
return hex / GameConstants::BFIELD_WIDTH;
|
|
|
|
}
|
2011-12-14 00:35:28 +03:00
|
|
|
|
2017-01-29 17:38:13 +02:00
|
|
|
std::pair<si16, si16> BattleHex::getXY() const
|
|
|
|
{
|
|
|
|
return std::make_pair(getX(), getY());
|
|
|
|
}
|
|
|
|
|
2017-05-29 09:33:34 +02:00
|
|
|
BattleHex& BattleHex::moveInDirection(EDir dir, bool hasToBeValid)
|
2017-01-29 17:38:13 +02:00
|
|
|
{
|
|
|
|
si16 x(getX()), y(getY());
|
2011-12-14 00:35:28 +03:00
|
|
|
switch(dir)
|
|
|
|
{
|
|
|
|
case TOP_LEFT:
|
2013-11-06 16:42:58 +03:00
|
|
|
setXY((y%2) ? x-1 : x, y-1, hasToBeValid);
|
2011-12-14 00:35:28 +03:00
|
|
|
break;
|
|
|
|
case TOP_RIGHT:
|
2013-11-06 16:42:58 +03:00
|
|
|
setXY((y%2) ? x : x+1, y-1, hasToBeValid);
|
2011-12-14 00:35:28 +03:00
|
|
|
break;
|
|
|
|
case RIGHT:
|
2012-05-18 23:50:16 +03:00
|
|
|
setXY(x+1, y, hasToBeValid);
|
2011-12-14 00:35:28 +03:00
|
|
|
break;
|
|
|
|
case BOTTOM_RIGHT:
|
2013-11-06 16:42:58 +03:00
|
|
|
setXY((y%2) ? x : x+1, y+1, hasToBeValid);
|
2011-12-14 00:35:28 +03:00
|
|
|
break;
|
|
|
|
case BOTTOM_LEFT:
|
2013-11-06 16:42:58 +03:00
|
|
|
setXY((y%2) ? x-1 : x, y+1, hasToBeValid);
|
2011-12-14 00:35:28 +03:00
|
|
|
break;
|
|
|
|
case LEFT:
|
2012-05-18 23:50:16 +03:00
|
|
|
setXY(x-1, y, hasToBeValid);
|
2011-12-14 00:35:28 +03:00
|
|
|
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;
|
2017-05-29 09:33:34 +02:00
|
|
|
}
|
2012-05-18 23:50:16 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-01-29 17:38:13 +02:00
|
|
|
BattleHex &BattleHex::operator+=(BattleHex::EDir dir)
|
|
|
|
{
|
2017-05-29 09:33:34 +02:00
|
|
|
return moveInDirection(dir);
|
2017-01-29 17:38:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 09:33:34 +02:00
|
|
|
BattleHex BattleHex::cloneInDirection(BattleHex::EDir dir, bool hasToBeValid) const
|
2017-01-29 17:38:13 +02:00
|
|
|
{
|
2017-05-29 09:33:34 +02:00
|
|
|
BattleHex result(hex);
|
|
|
|
result.moveInDirection(dir, hasToBeValid);
|
2017-01-29 17:38:13 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleHex BattleHex::operator+(BattleHex::EDir dir) const
|
|
|
|
{
|
2017-05-29 09:33:34 +02:00
|
|
|
return cloneInDirection(dir);
|
2017-01-29 17:38:13 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2017-05-29 09:33:34 +02:00
|
|
|
for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
|
|
|
|
checkAndPush(cloneInDirection(dir, false), ret);
|
2011-12-14 00:35:28 +03:00
|
|
|
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
|
|
|
{
|
2017-05-29 09:33:34 +02:00
|
|
|
for(EDir dir = EDir(0); dir <= EDir(5); dir = EDir(dir+1))
|
|
|
|
if(hex2 == hex1.cloneInDirection(dir,false))
|
|
|
|
return dir;
|
|
|
|
return INVALID;
|
2011-12-14 00:35:28 +03:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
|
2017-01-29 17:38:13 +02:00
|
|
|
{
|
|
|
|
int y1 = hex1.getY(), y2 = hex2.getY();
|
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
// FIXME: Omit floating point arithmetics
|
2017-01-29 17:38:13 +02:00
|
|
|
int x1 = (hex1.getX() + y1 * 0.5), x2 = (hex2.getX() + y2 * 0.5);
|
2012-08-28 15:28:13 +03:00
|
|
|
|
2017-01-29 17:38:13 +02:00
|
|
|
int xDst = x2 - x1, yDst = y2 - y1;
|
2012-08-28 15:28:13 +03:00
|
|
|
|
2017-01-29 17:38:13 +02:00
|
|
|
if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
|
2012-08-28 15:28:13 +03:00
|
|
|
return std::max(std::abs(xDst), std::abs(yDst));
|
2017-01-29 17:38:13 +02:00
|
|
|
|
2014-08-30 19:45:11 +03:00
|
|
|
return std::abs(xDst) + std::abs(yDst);
|
2011-12-14 00:35:28 +03:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)
|
2011-12-14 00:35:28 +03:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
if(tile.isAvailable())
|
|
|
|
ret.push_back(tile);
|
2012-03-31 00:36:07 +03:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:10:56 +03:00
|
|
|
BattleHex BattleHex::getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities)
|
|
|
|
{
|
|
|
|
std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
|
|
|
|
BattleHex initialHex = BattleHex(initialPos);
|
|
|
|
auto compareDistance = [initialHex](const BattleHex left, const BattleHex right) -> bool
|
|
|
|
{
|
|
|
|
return initialHex.getDistance (initialHex, left) < initialHex.getDistance (initialHex, right);
|
|
|
|
};
|
|
|
|
boost::sort (sortedTiles, compareDistance); //closest tiles at front
|
|
|
|
int closestDistance = initialHex.getDistance(initialPos, sortedTiles.front()); //sometimes closest tiles can be many hexes away
|
|
|
|
auto notClosest = [closestDistance, initialPos](const BattleHex here) -> bool
|
|
|
|
{
|
|
|
|
return closestDistance < here.getDistance (initialPos, here);
|
|
|
|
};
|
|
|
|
vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
|
|
|
|
auto compareHorizontal = [attackerOwned, initialPos](const BattleHex left, const BattleHex right) -> bool
|
|
|
|
{
|
|
|
|
if(left.getX() != right.getX())
|
|
|
|
{
|
|
|
|
if (attackerOwned)
|
|
|
|
return left.getX() > right.getX(); //find furthest right
|
|
|
|
else
|
|
|
|
return left.getX() < right.getX(); //find furthest left
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Prefer tiles in the same row.
|
|
|
|
return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
boost::sort (sortedTiles, compareHorizontal);
|
|
|
|
return sortedTiles.front();
|
2013-11-06 16:42:58 +03:00
|
|
|
}
|
2013-11-09 19:25:20 +03:00
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
|
|
|
|
{
|
2013-12-08 20:54:13 +03:00
|
|
|
return os << boost::str(boost::format("{BattleHex: x '%d', y '%d', hex '%d'}") % hex.getX() % hex.getY() % hex.hex);
|
2013-11-09 19:25:20 +03:00
|
|
|
}
|