1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00
vcmi/lib/BattleHex.cpp
Michał W. Urbańczyk a9af0da0ab Rewritten handling mouse movement over hex and l-clicking hex into one procedure. That way the tooltip and cursor are always accurate, because they're set by the same routing that selects action. Having that logic duplicated in two methods was unmaintainable. [though the new one is still monstrous...] By the way fixed numerous issues, including:
* #785 and parts of #760
* first aid tent can heal only creatures that suffered damage
* war machines can't be healed by tent
* creatures casting spells won't try to cast them during tactic phase
* console tooltips for first aid tent
* console tooltips for teleport spell
* cursor is reset to pointer when action is requested
* fixed a few other missing or wrong tooltips/cursors

Implemented opening creature window by l-clicking on stack. Master Genie's spell is picked by server, not client.
Minor changes.
2012-03-30 21:36:07 +00:00

90 lines
2.3 KiB
C++

#include "StdInc.h"
#include "BattleHex.h"
void BattleHex::operator+=(EDir dir)
{
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:
throw std::string("Disaster: wrong direction in BattleHex::operator+=!\n");
break;
}
}
BattleHex BattleHex::operator+(EDir dir) const
{
BattleHex ret(*this);
ret += dir;
return ret;
}
std::vector<BattleHex> BattleHex::neighbouringTiles() const
{
std::vector<BattleHex> ret;
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;
}
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 != 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;
}
char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
{
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;
}
void BattleHex::checkAndPush(int tile, std::vector<BattleHex> & ret)
{
if( tile>=0 && tile<GameConstants::BFIELD_SIZE && (tile%GameConstants::BFIELD_WIDTH != (GameConstants::BFIELD_WIDTH - 1))
&& (tile%GameConstants::BFIELD_WIDTH != 0) )
ret.push_back(BattleHex(tile));
}
bool BattleHex::isAvailable() const
{
return isValid() && getX() > 0 && getX() < GameConstants::BFIELD_WIDTH-1;
}