whatever I did there

This commit is contained in:
Max Känner 2024-09-30 18:26:22 +02:00
parent 157ea2aaad
commit af0baffde0

View File

@ -14,7 +14,7 @@ use log::info;
use rand::seq::SliceRandom;
use serde_json::{json, Value};
use crate::{Battlesnake, Board, Direction, Game, Move};
use crate::{Battlesnake, Board, Coord, Direction, Game, Move};
impl Battlesnake {
/// Check if moving the snake in `direction` is safe.
@ -68,6 +68,46 @@ impl Battlesnake {
}
false
}
#[must_use]
pub fn health_cost(&self, tile: Coord, game: &Game, board: &Board) -> i32 {
// we can't leave the board
if !((0..board.width).contains(&tile.x) && (0..board.height).contains(&tile.y)) {
return self.health;
}
// we can't move into other snakes
if board
.snakes
.iter()
.filter(|snake| {
!(game.ruleset.settings.squad.allow_body_collisions && self.squad == snake.squad)
})
.flat_map(|snake| snake.body.iter())
.any(|&coord| tile == coord)
{
return self.health;
}
// every step costs us one health point
let mut cost = 1;
// hazards increase the health loss
if board.hazards.iter().any(|&hazard| tile == hazard) {
cost += game.ruleset.settings.hazard_damage_per_turn;
}
// check for bigger snakes that can move into this square
for snake in board
.snakes
.iter()
.filter(|snake| snake.id != self.id && self.length <= snake.length)
{
let moves = enum_iterator::all().map(|dir| snake.head.move_to(dir));
}
cost
}
}
// info is called when you create your Battlesnake on play.battlesnake.com