summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-20 20:03:48 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-20 20:03:48 +0200
commit52ade984643c6482ccf0deeea98f7f63fa74045d (patch)
tree7fdac25e3f5cae097d7053f2d8dd284d6d49b4ee
parent7f2d82e15459fc3d365674e3772aed6c3d443ca2 (diff)
Split oils and muds into their own lists
Now memory for one isn't affected by memory usage of the other!
-rw-r--r--src/global_json.rs14
-rw-r--r--src/json.rs15
-rw-r--r--src/lib.rs3
-rw-r--r--src/state.rs21
-rw-r--r--vroomba-analysis/src/main.rs4
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<BTreeSet<Position>>,
+ pub muds: Rc<BTreeSet<Position>>,
+ pub oil_spills: Rc<BTreeSet<Position>>,
pub powerup_oils: Rc<BTreeSet<Position>>,
pub powerup_boosts: Rc<BTreeSet<Position>>,
}
@@ -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<Position>,
+ muds: &BTreeSet<Position>,
+ oil_spills: &BTreeSet<Position>,
powerup_oils: &BTreeSet<Position>,
powerup_boosts: &BTreeSet<Position>,
) {
@@ -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 {
"-"