Skip to content

Commit

Permalink
fix for some melee and medkit related stuff
Browse files Browse the repository at this point in the history
make them both use validMeleeRange, and make validMeleeRange store the
position of the tile a valid melee target was found in. this avoids the
need for deriving the location later, removing the need for copy/pasted
defective code and making everything look all neat and shiny
  • Loading branch information
Warboy1982 committed Dec 17, 2013
1 parent 9066047 commit 4c7c0d8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
20 changes: 13 additions & 7 deletions src/Battlescape/ActionMenuState.cpp
Expand Up @@ -234,11 +234,18 @@ void ActionMenuState::btnActionMenuItemClick(Action *action)
}
if (!targetUnit)
{
Position p;
Pathfinding::directionToVector(_action->actor->getDirection(), &p);
Tile * tile (_game->getSavedGame()->getSavedBattle()->getTile(_action->actor->getPosition() + p));
if (tile != 0 && tile->getUnit() && tile->getUnit()->isWoundable())
targetUnit = tile->getUnit();
if (_game->getSavedGame()->getSavedBattle()->getTileEngine()->validMeleeRange(
_action->actor->getPosition(),
_action->actor->getDirection(),
_action->actor,
0, &_action->target))
{
Tile * tile (_game->getSavedGame()->getSavedBattle()->getTile(_action->target));
if (tile != 0 && tile->getUnit() && tile->getUnit()->isWoundable())
{
targetUnit = tile->getUnit();
}
}
}
if (targetUnit)
{
Expand Down Expand Up @@ -284,12 +291,11 @@ void ActionMenuState::btnActionMenuItemClick(Action *action)
}
else if ((_action->type == BA_STUN || _action->type == BA_HIT) && weapon->getBattleType() == BT_MELEE)
{

if (!_game->getSavedGame()->getSavedBattle()->getTileEngine()->validMeleeRange(
_action->actor->getPosition(),
_action->actor->getDirection(),
_action->actor,
0))
0, &_action->target))
{
_action->result = "STR_THERE_IS_NO_ONE_THERE";
}
Expand Down
4 changes: 2 additions & 2 deletions src/Battlescape/AlienBAIState.cpp
Expand Up @@ -1007,7 +1007,7 @@ int AlienBAIState::selectNearestTarget()
if (selectPointNearTarget(*i, _unit->getTimeUnits()))
{
int dir = _save->getTileEngine()->getDirectionTo(_attackAction->target, (*i)->getPosition());
validTarget = _save->getTileEngine()->validMeleeRange(_attackAction->target, dir, _unit, *i);
validTarget = _save->getTileEngine()->validMeleeRange(_attackAction->target, dir, _unit, *i, 0);
}
}
if (validTarget)
Expand Down Expand Up @@ -1094,7 +1094,7 @@ bool AlienBAIState::selectPointNearTarget(BattleUnit *target, int maxTUs) const
{
Position checkPath = target->getPosition() + Position (x, y, 0);
int dir = _save->getTileEngine()->getDirectionTo(checkPath, target->getPosition());
bool valid = _save->getTileEngine()->validMeleeRange(checkPath, dir, _unit, target);
bool valid = _save->getTileEngine()->validMeleeRange(checkPath, dir, _unit, target, 0);
bool fitHere = _save->setUnitPosition(_unit, checkPath, true);

if (valid && fitHere && !_save->getTile(checkPath)->getDangerous())
Expand Down
21 changes: 2 additions & 19 deletions src/Battlescape/BattlescapeGame.cpp
Expand Up @@ -651,25 +651,8 @@ void BattlescapeGame::handleNonTargetAction()
{
if (_currentAction.actor->spendTimeUnits(_currentAction.TU))
{
Position p;
Pathfinding::directionToVector(_currentAction.actor->getDirection(), &p);
Tile * tile (_save->getTile(_currentAction.actor->getPosition() + p));
for (int x = 0; x != _currentAction.actor->getArmor()->getSize(); ++x)
{
for (int y = 0; y != _currentAction.actor->getArmor()->getSize(); ++y)
{
tile = _save->getTile(Position(_currentAction.actor->getPosition().x + x, _currentAction.actor->getPosition().y + y, _currentAction.actor->getPosition().z) + p);
if (tile->getUnit() && tile->getUnit() != _currentAction.actor)
{
Position voxel = Position(tile->getPosition().x*16,tile->getPosition().y*16,tile->getPosition().z*24);
voxel.x += 8;voxel.y += 8;voxel.z += 8;
statePushNext(new ExplosionBState(this, voxel, _currentAction.weapon, _currentAction.actor));
break;
}
}
if (tile->getUnit() && tile->getUnit() != _currentAction.actor)
break;
}
Position voxel = _currentAction.target * Position(16, 16, 24) + Position(8,8,8 - _save->getTile(_currentAction.target)->getTerrainLevel());
statePushNext(new ExplosionBState(this, voxel, _currentAction.weapon, _currentAction.actor));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Battlescape/ProjectileFlyBState.cpp
Expand Up @@ -165,7 +165,7 @@ void ProjectileFlyBState::init()
_projectileItem = weapon;
break;
case BA_HIT:
if (!_parent->getTileEngine()->validMeleeRange(_action.actor->getPosition(), _action.actor->getDirection(), _action.actor, 0))
if (!_parent->getTileEngine()->validMeleeRange(_action.actor->getPosition(), _action.actor->getDirection(), _action.actor, 0, &_action.target))
{
_action.result = "STR_THERE_IS_NO_ONE_THERE";
_parent->popState();
Expand Down
17 changes: 13 additions & 4 deletions src/Battlescape/TileEngine.cpp
Expand Up @@ -2186,19 +2186,19 @@ bool TileEngine::isVoxelVisible(const Position& voxel)
int TileEngine::voxelCheck(const Position& voxel, BattleUnit *excludeUnit, bool excludeAllUnits, bool onlyVisible, BattleUnit *excludeAllBut)
{
Tile *tile = _save->getTile(voxel / Position(16, 16, 24));
Tile *tileBelow = _save->getTile(tile->getPosition() + Position(0,0,-1));
// check if we are not out of the map
if (tile == 0 || voxel.x < 0 || voxel.y < 0 || voxel.z < 0)
{
return V_OUTOFBOUNDS;
}
if (tile->isVoid() && tile->getUnit() == 0)
if (tile->isVoid() && tile->getUnit() == 0 && (!tileBelow || tileBelow->getUnit() == 0))
{
return V_EMPTY;
}

if (voxel.z % 24 == 0 && tile->getMapData(MapData::O_FLOOR) && tile->getMapData(MapData::O_FLOOR)->isGravLift())
{
Tile *tileBelow = _save->getTile(tile->getPosition() + Position(0,0,-1));
if (tileBelow && tileBelow->getMapData(MapData::O_FLOOR) && !tileBelow->getMapData(MapData::O_FLOOR)->isGravLift())
return V_FLOOR;
}
Expand Down Expand Up @@ -2457,7 +2457,7 @@ Tile *TileEngine::applyGravity(Tile *t)
*/
bool TileEngine::validMeleeRange(BattleUnit *attacker, BattleUnit *target, int dir)
{
return validMeleeRange(attacker->getPosition(), dir, attacker, target);
return validMeleeRange(attacker->getPosition(), dir, attacker, target, 0);
}

/**
Expand All @@ -2468,7 +2468,7 @@ bool TileEngine::validMeleeRange(BattleUnit *attacker, BattleUnit *target, int d
* @param target The unit we want to attack, 0 for any unit.
* @return True when the range is valid.
*/
bool TileEngine::validMeleeRange(Position pos, int direction, BattleUnit *attacker, BattleUnit *target)
bool TileEngine::validMeleeRange(Position pos, int direction, BattleUnit *attacker, BattleUnit *target, Position *dest)
{
if (direction < 0 || direction > 7)
{
Expand All @@ -2484,13 +2484,18 @@ bool TileEngine::validMeleeRange(Position pos, int direction, BattleUnit *attack
Tile *origin (_save->getTile(Position(pos + Position(x, y, 0))));
Tile *targetTile (_save->getTile(Position(pos + Position(x, y, 0) + p)));
Tile *aboveTargetTile (_save->getTile(Position(pos + Position(x, y, 1) + p)));
Tile *belowTargetTile (_save->getTile(Position(pos + Position(x, y, -1) + p)));

if (targetTile && origin)
{
if (origin->getTerrainLevel() <= -16 && aboveTargetTile && !aboveTargetTile->hasNoFloor(targetTile))
{
targetTile = aboveTargetTile;
}
else if (belowTargetTile && targetTile->hasNoFloor(belowTargetTile) && !targetTile->getUnit() && belowTargetTile->getTerrainLevel() <= -16)
{
targetTile = belowTargetTile;
}
if (targetTile->getUnit())
{
if (target == 0 || targetTile->getUnit() == target)
Expand All @@ -2500,6 +2505,10 @@ bool TileEngine::validMeleeRange(Position pos, int direction, BattleUnit *attack
Position targetVoxel;
if (canTargetUnit(&originVoxel, targetTile, &targetVoxel, attacker))
{
if (dest)
{
*dest = targetTile->getPosition();
}
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Battlescape/TileEngine.h
Expand Up @@ -104,7 +104,7 @@ class TileEngine
/// Returns melee validity between two units.
bool validMeleeRange(BattleUnit *attacker, BattleUnit *target, int dir);
/// Returns validity of a melee attack from a given position.
bool validMeleeRange(Position pos, int direction, BattleUnit *attacker, BattleUnit *target);
bool validMeleeRange(Position pos, int direction, BattleUnit *attacker, BattleUnit *target, Position *dest);
/// Gets the AI to look through a window.
int faceWindow(const Position &position);
/// Checks a unit's % exposure on a tile.
Expand Down

0 comments on commit 4c7c0d8

Please sign in to comment.