boolean tileHasAllowMovement = false;
Move moveAbility = (Move) unit.getType().getAbility("Move");
if (moveAbility == null) {
return new CommandResult(CommandResult.RESULT_ERROR, "");
}
Tile toTile = realm.getTile(coordinate);
Iterator<TileImprovementType> iterator = toTile.getImprovementsIterator();
while (iterator.hasNext()) {
TileImprovementType tileImprovement = iterator.next();
if (tileImprovement.hasPropertyNamed("AllowMovement")) {
tileHasAllowMovement = true;
break;
}
}
if (!moveAbility.hasTileType(toTile.getType()) && !tileHasAllowMovement) {
return new CommandResult(CommandResult.RESULT_ERROR, "");
}
Vector<Coordinate> adjacentTiles = Utility.getNeighborCoordinates(realm, unit.getCoordinate());
if (!adjacentTiles.contains(coordinate)) {
return new CommandResult(CommandResult.RESULT_ERROR, "");
}
if (unit.getMovementPoints() <= 0) {
return new CommandResult(CommandResult.RESULT_ERROR, "");
}
if (unit.getMovementPoints() > 0 && unit.getMovementPoints() < 1 && toTile.getMovementCost() >= 1) {
return new CommandResult(CommandResult.RESULT_ERROR, "");
}
if (toTile.getNumberOfUnits() > 0) {
Unit defendingUnit = toTile.getUnits().get(toTile.getUnits().firstKey());
if (!unit.getPlayer().equals(defendingUnit.getPlayer())) {
return new CommandResult(CommandResult.RESULT_ERROR, "Tile contains a unit that does not belong to player. Use AttackTileCommand.");
}
}
if ((toTile.getSettlement() != null) && (!unit.getPlayer().equals((toTile.getSettlement().getPlayer())))) {
return new CommandResult(CommandResult.RESULT_ERROR, "Tile contains a settlement that does not belong to player. Use InvadeCityCommand.");
}
Coordinate oldCoordinate = unit.getCoordinate();
realm.getWorldMap().removeUnit(unit);
unit.setCoordinate(coordinate);
realm.getWorldMap().addUnit(unit);
float movementCost = toTile.getMovementCost();
float newMovementPoints = (unit.getMovementPoints() >= movementCost ? unit.getMovementPoints() - movementCost : 0);
unit.setMovementPoints(newMovementPoints);
manageExploration(realm);
manageDiplomacy(realm);
CommandResult commandResult = new CommandResult(CommandResult.RESULT_OK, "", CommandResult.UNIT_MOVEMENT_UPDATE);