2011-12-14 00:35:28 +03:00
# pragma once
2013-02-05 22:56:28 +03:00
2011-12-14 00:35:28 +03:00
# include "GameConstants.h"
/*
2011-12-22 16:05:19 +03:00
* BattleHex . h , part of VCMI engine
2011-12-14 00:35:28 +03:00
*
* 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
*
*/
// for battle stacks' positions
2011-12-22 16:05:19 +03:00
struct DLL_LINKAGE BattleHex
2011-12-14 00:35:28 +03:00
{
static const si16 INVALID = - 1 ;
enum EDir { RIGHT , BOTTOM_RIGHT , BOTTOM_LEFT , LEFT , TOP_LEFT , TOP_RIGHT } ;
si16 hex ;
2011-12-22 16:05:19 +03:00
BattleHex ( ) : hex ( INVALID ) { }
BattleHex ( si16 _hex ) : hex ( _hex ) { }
2011-12-14 00:35:28 +03:00
operator si16 ( ) const
{
return hex ;
}
bool isValid ( ) const
{
return hex > = 0 & & hex < GameConstants : : BFIELD_SIZE ;
}
template < typename inttype >
2011-12-22 16:05:19 +03:00
BattleHex ( inttype x , inttype y )
2011-12-14 00:35:28 +03:00
{
setXY ( x , y ) ;
}
template < typename inttype >
2011-12-22 16:05:19 +03:00
BattleHex ( std : : pair < inttype , inttype > xy )
2011-12-14 00:35:28 +03:00
{
setXY ( xy ) ;
}
template < typename inttype >
void setX ( inttype x )
{
setXY ( x , getY ( ) ) ;
}
template < typename inttype >
void setY ( inttype y )
{
setXY ( getX ( ) , y ) ;
}
2012-05-18 23:50:16 +03:00
void setXY ( si16 x , si16 y , bool hasToBeValid = true )
2011-12-14 00:35:28 +03:00
{
2012-05-18 23:50:16 +03:00
if ( hasToBeValid )
{
assert ( x > = 0 & & x < GameConstants : : BFIELD_WIDTH & & y > = 0 & & y < GameConstants : : BFIELD_HEIGHT ) ;
}
2011-12-14 00:35:28 +03:00
hex = x + y * GameConstants : : BFIELD_WIDTH ;
}
template < typename inttype >
void setXY ( std : : pair < inttype , inttype > xy )
{
setXY ( xy . first , xy . second ) ;
}
si16 getY ( ) const
{
return hex / GameConstants : : BFIELD_WIDTH ;
}
si16 getX ( ) const
{
int pos = hex - getY ( ) * GameConstants : : BFIELD_WIDTH ;
return pos ;
}
std : : pair < si16 , si16 > getXY ( ) const
{
return std : : make_pair ( getX ( ) , getY ( ) ) ;
}
//moving to direction
2012-05-18 23:50:16 +03:00
BattleHex & moveInDir ( EDir dir , bool hasToBeValid = true ) ;
void operator + = ( EDir dir ) ; //sugar for above
2011-12-14 00:35:28 +03:00
2011-12-22 16:05:19 +03:00
//generates new BattleHex moved by given dir
BattleHex operator + ( EDir dir ) const ;
2011-12-14 00:35:28 +03:00
2011-12-22 16:05:19 +03:00
std : : vector < BattleHex > neighbouringTiles ( ) const ;
2011-12-14 00:35:28 +03:00
//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)
2011-12-22 16:05:19 +03:00
static signed char mutualPosition ( BattleHex hex1 , BattleHex hex2 ) ;
2011-12-14 00:35:28 +03:00
//returns distance between given hexes
2011-12-22 16:05:19 +03:00
static char getDistance ( BattleHex hex1 , BattleHex hex2 ) ;
2011-12-14 00:35:28 +03:00
template < typename Handler > void serialize ( Handler & h , const int version )
{
h & hex ;
}
2012-08-26 12:07:48 +03:00
static void checkAndPush ( BattleHex tile , std : : vector < BattleHex > & ret ) ;
2011-12-14 00:35:28 +03:00
2012-03-31 00:36:07 +03:00
bool isAvailable ( ) const ; //valid position not in first or last column
2012-09-24 02:10:56 +03:00
static BattleHex getClosestTile ( bool attackerOwned , BattleHex initialPos , std : : set < BattleHex > & possibilities ) ; //TODO: vector or set? copying one to another is bad
2013-11-09 19:25:20 +03:00
} ;
2013-11-10 00:29:46 +03:00
DLL_EXPORT std : : ostream & operator < < ( std : : ostream & os , const BattleHex & hex ) ;