Skip to content

Commit

Permalink
Bugfix: handle missing path for shark + grid store empty tiles
Browse files Browse the repository at this point in the history
The latter will be used for a follow-up optimization.
  • Loading branch information
JesseEmond committed Oct 5, 2024
1 parent b39ca9b commit 56696b9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
6 changes: 1 addition & 5 deletions bot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ impl GameState {
Game {
tick: self.tick as usize,
pos: self.position.to_pos(),
grid: Grid {
width: self.map.width as u8,
height: self.map.height as u8,
tiles: self.map.tiles.clone(),
},
grid: Grid::new(self.map.width as u8, self.map.height as u8, self.map.tiles.clone()),
threats: self.threats.iter().map(
|t| Threat::new(t.position.to_pos(), from_style_name(&t.style),
t.direction.to_move()))
Expand Down
58 changes: 41 additions & 17 deletions bot/src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,28 @@ pub struct Grid {
pub height: u8,
/// Dims: [x][y], true for walls
pub tiles: Vec<Vec<bool>>,
pub empty_tiles: Vec<Pos>,
/// At [x][y], index in 'empty_tiles' (only valid for empty tiles).
pub empty_tiles_lookup: Vec<Vec<usize>>,
}

impl Grid {
pub fn new(width: u8, height: u8, tiles: Vec<Vec<bool>>) -> Self {
let mut empty_tiles = Vec::new();
let mut empty_tiles_lookup = vec![vec![usize::MAX; height as usize];
width as usize];
for x in 0..(width as usize) {
for y in 0..(height as usize) {
if !tiles[x][y] {
let idx = empty_tiles.len();
empty_tiles.push(Pos { x: x as i16, y: y as i16 });
empty_tiles_lookup[x][y] = idx;
}
}
}
Self { width, height, tiles, empty_tiles, empty_tiles_lookup }
}

fn available_moves(&self, from: &Pos) -> Vec<Move> {
Move::iter()
.filter(|&m| self.is_empty(&from.moved(m)))
Expand Down Expand Up @@ -137,7 +156,7 @@ pub fn make_grid(rows: Vec<&str>) -> Grid {
tiles[x][y] = c == b'#';
}
}
Grid { tiles, width: width as u8, height: height as u8 }
Grid::new(width as u8, height as u8, tiles)
}


Expand Down Expand Up @@ -261,32 +280,37 @@ impl Threat {
Style::Shark => {
// See aggressive.js
let path = get_aggressive_path(&grid, &self.pos, &player);
assert!(!path.is_empty());
let next = path[0];
assert!(next != self.pos);
assert!(self.pos.manhattan_dist(&next) <= 1);
if next.x > self.pos.x {
Some(Move::Right)
} else if next.x < self.pos.x {
Some(Move::Left)
} else if next.y < self.pos.y {
Some(Move::Up)
if !path.is_empty() {
let next = path[0];
assert!(next != self.pos);
assert!(self.pos.manhattan_dist(&next) <= 1);
if next.x > self.pos.x {
Some(Move::Right)
} else if next.x < self.pos.x {
Some(Move::Left)
} else if next.y < self.pos.y {
Some(Move::Up)
} else {
assert!(next.y > self.pos.y);
Some(Move::Down)
}
} else {
assert!(next.y > self.pos.y);
Some(Move::Down)
None
}
},
_ => None, // TODO: implement other styles
_ => {
assert!(!Threat::can_predict(self.style));
None // TODO: implement other styles
},
};
if let Some(m) = next_move {
assert!(Threat::can_predict(self.style));
self.pos = self.pos.moved(m);
self.dir = m;
} else {
assert!(!Threat::can_predict(self.style));
}
// TODO: remove return once we have 100% predictions
!next_move.is_none()
// If we can predict it, we have enforced above that we have handled it.
Threat::can_predict(self.style)
}

fn move_every_n_ticks(tick: usize) -> usize {
Expand Down

0 comments on commit 56696b9

Please sign in to comment.