From af0baffde09a91f552224ed9c4d242cecc5365d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Mon, 30 Sep 2024 18:26:22 +0200 Subject: [PATCH] whatever I did there --- battlesnake/src/logic.rs | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/battlesnake/src/logic.rs b/battlesnake/src/logic.rs index f235be7..b28a137 100644 --- a/battlesnake/src/logic.rs +++ b/battlesnake/src/logic.rs @@ -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