From 52ade984643c6482ccf0deeea98f7f63fa74045d Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Mon, 20 Apr 2020 20:03:48 +0200 Subject: Split oils and muds into their own lists Now memory for one isn't affected by memory usage of the other! --- src/global_json.rs | 14 +++++++++----- src/json.rs | 15 ++++++++++----- src/lib.rs | 3 --- src/state.rs | 21 ++++++++++++++------- vroomba-analysis/src/main.rs | 4 ++-- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/global_json.rs b/src/global_json.rs index af69157..7ac109a 100644 --- a/src/global_json.rs +++ b/src/global_json.rs @@ -101,13 +101,17 @@ impl JsonState { Ok(GameState { status: GameStatus::Continue, players: [self.players[0].to_player()?, self.players[1].to_player()?], - obstacles: Rc::new( + muds: Rc::new( self.blocks .iter() - .filter(|cell| { - cell.surface_object == JsonSurfaceObject::Mud - || cell.surface_object == JsonSurfaceObject::OilSpill - }) + .filter(|cell| cell.surface_object == JsonSurfaceObject::Mud) + .map(|cell| cell.position.to_position()) + .collect(), + ), + oil_spills: Rc::new( + self.blocks + .iter() + .filter(|cell| cell.surface_object == JsonSurfaceObject::OilSpill) .map(|cell| cell.position.to_position()) .collect(), ), diff --git a/src/json.rs b/src/json.rs index a7d7759..82fc9fc 100644 --- a/src/json.rs +++ b/src/json.rs @@ -104,14 +104,19 @@ impl JsonState { Ok(GameState { status: GameStatus::Continue, players: [self.player.to_player()?, self.opponent.to_player()], - obstacles: Rc::new( + muds: Rc::new( self.world_map .iter() .flatten() - .filter(|cell| { - cell.surface_object == JsonSurfaceObject::Mud - || cell.surface_object == JsonSurfaceObject::OilSpill - }) + .filter(|cell| cell.surface_object == JsonSurfaceObject::Mud) + .map(|cell| cell.position.to_position()) + .collect(), + ), + oil_spills: Rc::new( + self.world_map + .iter() + .flatten() + .filter(|cell| cell.surface_object == JsonSurfaceObject::OilSpill) .map(|cell| cell.position.to_position()) .collect(), ), diff --git a/src/lib.rs b/src/lib.rs index ac73f72..446a494 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,9 +9,6 @@ use state::*; use std::cmp::Ordering; pub fn choose_command(state: &GameState) -> Command { - /* TODO: - * - Simulate both player and opponent moves - */ let player_moves = state.valid_moves(0); let naive_result = player_moves .into_iter() diff --git a/src/state.rs b/src/state.rs index 87f73f5..25594a0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -16,7 +16,8 @@ pub enum GameStatus { pub struct GameState { pub status: GameStatus, pub players: [Player; 2], - pub obstacles: Rc>, + pub muds: Rc>, + pub oil_spills: Rc>, pub powerup_oils: Rc>, pub powerup_boosts: Rc>, } @@ -37,6 +38,7 @@ pub struct Position { } impl GameState { + // TODO: Return metadata on what each bot did from this pub fn update(&mut self, commands: [Command; 2]) { if self.status != GameStatus::Continue { return; @@ -93,12 +95,12 @@ impl GameState { debug_assert!(self.players[player_index].oils > 0); self.players[player_index].oils = self.players[player_index].oils.saturating_sub(1); let player_position = self.players[player_index].position; - let mut obstacles = (*self.obstacles).clone(); - obstacles.insert(Position { + let mut oil_spills = (*self.oil_spills).clone(); + oil_spills.insert(Position { x: player_position.x.saturating_sub(1), y: player_position.y, }); - self.obstacles = Rc::new(obstacles); + self.oil_spills = Rc::new(oil_spills); } } @@ -159,7 +161,8 @@ impl GameState { for (player, next_position) in self.players.iter_mut().zip(next_positions.iter()) { player.move_along( *next_position, - &self.obstacles, + &self.muds, + &self.oil_spills, &self.powerup_oils, &self.powerup_boosts, ); @@ -247,7 +250,8 @@ impl Player { fn move_along( &mut self, next_position: Position, - obstacles: &BTreeSet, + muds: &BTreeSet, + oil_spills: &BTreeSet, powerup_oils: &BTreeSet, powerup_boosts: &BTreeSet, ) { @@ -262,7 +266,10 @@ impl Player { }), ); - for _ in obstacles.range(range) { + for _ in muds.range(range) { + self.decelerate_from_obstacle(); + } + for _ in oil_spills.range(range) { self.decelerate_from_obstacle(); } self.oils = self diff --git a/vroomba-analysis/src/main.rs b/vroomba-analysis/src/main.rs index 721064a..994e021 100644 --- a/vroomba-analysis/src/main.rs +++ b/vroomba-analysis/src/main.rs @@ -126,11 +126,11 @@ fn log_shortest_path( for x in start_x..start_x + chunk_size { let pos = Position { y, x }; - let c = if initial_state.obstacles.contains(&pos) { + let c = if initial_state.muds.contains(&pos) { "O" } else if initial_state.powerup_boosts.contains(&pos) { ">" - } else if initial_state.finish_lines.contains(&pos) { + } else if x == WIDTH { "|" } else { "-" -- cgit v1.2.3