wip: current project state
This commit is contained in:
+1
-1
@@ -1,5 +1,5 @@
|
||||
use verbatim::world::cell::{Cell, MaterialId};
|
||||
use verbatim::world::chunk::{world_to_chunk, Chunk, CHUNK_SIZE};
|
||||
use verbatim::world::chunk::{CHUNK_SIZE, Chunk, world_to_chunk};
|
||||
use verbatim::world::grid::Grid;
|
||||
|
||||
#[test]
|
||||
|
||||
+158
-26
@@ -1,11 +1,17 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
s.init_empty();
|
||||
s.clear_area(90, 90, 50, 50);
|
||||
s.perform_action(&AiAction::FillRect { x: 80, y: 130, w: 80, h: 15, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 80,
|
||||
y: 130,
|
||||
w: 80,
|
||||
h: 15,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s
|
||||
}
|
||||
|
||||
@@ -15,11 +21,22 @@ fn player_blocked_by_left_wall() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let wall_x = (p.pos[0] as i32) - 6;
|
||||
s.perform_action(&AiAction::FillRect { x: wall_x, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: wall_x,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::MoveLeft);
|
||||
s.step(20);
|
||||
let p2 = s.get_player().unwrap();
|
||||
assert!(p2.pos[0] > wall_x as f32, "player should not pass through left wall: wall={} player={}", wall_x, p2.pos[0]);
|
||||
assert!(
|
||||
p2.pos[0] > wall_x as f32,
|
||||
"player should not pass through left wall: wall={} player={}",
|
||||
wall_x,
|
||||
p2.pos[0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -28,11 +45,22 @@ fn player_blocked_by_right_wall() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let wall_x = (p.pos[0] as i32) + 6;
|
||||
s.perform_action(&AiAction::FillRect { x: wall_x, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: wall_x,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(20);
|
||||
let p2 = s.get_player().unwrap();
|
||||
assert!(p2.pos[0] < wall_x as f32, "player should not pass through right wall: wall={} player={}", wall_x, p2.pos[0]);
|
||||
assert!(
|
||||
p2.pos[0] < wall_x as f32,
|
||||
"player should not pass through right wall: wall={} player={}",
|
||||
wall_x,
|
||||
p2.pos[0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -41,14 +69,24 @@ fn player_slides_along_wall() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let wall_x = (p.pos[0] as i32) + 6;
|
||||
s.perform_action(&AiAction::FillRect { x: wall_x, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: wall_x,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(30);
|
||||
let p2 = s.get_player().unwrap();
|
||||
assert!(p2.pos[0] < wall_x as f32, "player should stay left of wall");
|
||||
assert!(p2.alive, "player should be alive");
|
||||
let y_diff = (p2.pos[1] - p.pos[1]).abs();
|
||||
assert!(y_diff < 5.0, "player should not fall through floor while sliding: dy={}", y_diff);
|
||||
assert!(
|
||||
y_diff < 5.0,
|
||||
"player should not fall through floor while sliding: dy={}",
|
||||
y_diff
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -57,11 +95,22 @@ fn player_blocked_by_ceiling() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let ceiling_y = (p.pos[1] as i32) - 8;
|
||||
s.perform_action(&AiAction::FillRect { x: p.pos[0] as i32 - 5, y: ceiling_y, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: p.pos[0] as i32 - 5,
|
||||
y: ceiling_y,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::Jump);
|
||||
s.step(10);
|
||||
let p2 = s.get_player().unwrap();
|
||||
assert!(p2.pos[1] > ceiling_y as f32, "player should not pass through ceiling: ceiling={} player={}", ceiling_y, p2.pos[1]);
|
||||
assert!(
|
||||
p2.pos[1] > ceiling_y as f32,
|
||||
"player should not pass through ceiling: ceiling={} player={}",
|
||||
ceiling_y,
|
||||
p2.pos[1]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -69,13 +118,40 @@ fn player_navigates_corridor() {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
s.init_empty();
|
||||
s.clear_area(100, 100, 40, 30);
|
||||
s.perform_action(&AiAction::FillRect { x: 95, y: 120, w: 50, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 115, y: 110, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 125, y: 110, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 115, y: 108, w: 11, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 95,
|
||||
y: 120,
|
||||
w: 50,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 115,
|
||||
y: 110,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 125,
|
||||
y: 110,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 115,
|
||||
y: 108,
|
||||
w: 11,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
assert!(p.pos[0] < 115.0 || p.pos[0] > 125.0, "player should be outside corridor initially");
|
||||
assert!(
|
||||
p.pos[0] < 115.0 || p.pos[0] > 125.0,
|
||||
"player should be outside corridor initially"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -84,7 +160,13 @@ fn player_does_not_stick_to_wall() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let wall_x = (p.pos[0] as i32) + 8;
|
||||
s.perform_action(&AiAction::FillRect { x: wall_x, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: wall_x,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
for _ in 0..10 {
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(2);
|
||||
@@ -96,7 +178,12 @@ fn player_does_not_stick_to_wall() {
|
||||
s.step(2);
|
||||
}
|
||||
let p_away = s.get_player().unwrap();
|
||||
assert!(p_away.pos[0] < x_at_wall - 1.0, "player should move away from wall: {} -> {}", x_at_wall, p_away.pos[0]);
|
||||
assert!(
|
||||
p_away.pos[0] < x_at_wall - 1.0,
|
||||
"player should move away from wall: {} -> {}",
|
||||
x_at_wall,
|
||||
p_away.pos[0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -105,8 +192,20 @@ fn player_squeezes_through_gap() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let px = p.pos[0] as i32;
|
||||
s.perform_action(&AiAction::FillRect { x: px + 8, y: 128, w: 1, h: 2, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: px + 8, y: 122, w: 1, h: 2, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: px + 8,
|
||||
y: 128,
|
||||
w: 1,
|
||||
h: 2,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: px + 8,
|
||||
y: 122,
|
||||
w: 1,
|
||||
h: 2,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(20);
|
||||
let p2 = s.get_player().unwrap();
|
||||
@@ -119,8 +218,20 @@ fn player_blocked_by_two_walls_both_sides() {
|
||||
s.step(30);
|
||||
let p = s.get_player().unwrap();
|
||||
let px = p.pos[0] as i32;
|
||||
s.perform_action(&AiAction::FillRect { x: px - 6, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: px + 6, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: px - 6,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: px + 6,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(10);
|
||||
let p_right = s.get_player().unwrap();
|
||||
@@ -129,7 +240,10 @@ fn player_blocked_by_two_walls_both_sides() {
|
||||
let p_left = s.get_player().unwrap();
|
||||
assert!(p_right.pos[0] < (px + 6) as f32, "blocked right");
|
||||
assert!(p_left.pos[0] > (px - 6) as f32, "blocked left");
|
||||
assert!((p_left.pos[0] - p_right.pos[0]).abs() < 12.0, "player should stay between walls");
|
||||
assert!(
|
||||
(p_left.pos[0] - p_right.pos[0]).abs() < 12.0,
|
||||
"player should stay between walls"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -140,7 +254,11 @@ fn player_walks_up_slope() {
|
||||
for x in 100..130 {
|
||||
let h = ((x - 100) / 3).min(15);
|
||||
for y in 0..h {
|
||||
s.perform_action(&AiAction::SetCell { x, y: 135 - 1 - y, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x,
|
||||
y: 135 - 1 - y,
|
||||
material: "stone".into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
s.step(40);
|
||||
@@ -152,11 +270,25 @@ fn player_walks_up_slope() {
|
||||
#[test]
|
||||
fn entity_collision_with_dirt_wall() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 140, y: 125, w: 1, h: 5, material: "dirt".into() });
|
||||
s.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 135.0, y: 120.0 });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 140,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 5,
|
||||
material: "dirt".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 135.0,
|
||||
y: 120.0,
|
||||
});
|
||||
s.step(40);
|
||||
let entities = s.get_entities();
|
||||
if let Some(g) = entities.into_iter().find(|e| e.kind == "Goblin" && e.alive) {
|
||||
assert!(g.pos[0] < 140.0, "goblin should be blocked by dirt wall: x={}", g.pos[0]);
|
||||
assert!(
|
||||
g.pos[0] < 140.0,
|
||||
"goblin should be blocked by dirt wall: x={}",
|
||||
g.pos[0]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+83
-18
@@ -1,12 +1,18 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::ReplayPlayer;
|
||||
|
||||
fn setup() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
s.init_empty();
|
||||
s.clear_area(90, 90, 50, 50);
|
||||
s.perform_action(&AiAction::FillRect { x: 80, y: 130, w: 80, h: 15, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 80,
|
||||
y: 130,
|
||||
w: 80,
|
||||
h: 15,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s
|
||||
}
|
||||
|
||||
@@ -32,7 +38,11 @@ fn same_seed_same_entity_count() {
|
||||
let mut s2 = GameSession::new_seeded(555);
|
||||
s2.init();
|
||||
s2.step(60);
|
||||
assert_eq!(s1.get_entities().len(), s2.get_entities().len(), "entity count should match");
|
||||
assert_eq!(
|
||||
s1.get_entities().len(),
|
||||
s2.get_entities().len(),
|
||||
"entity count should match"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -47,7 +57,11 @@ fn same_seed_same_grid_state() {
|
||||
for x in 100..150 {
|
||||
let c1 = s1.get_cell(x, y);
|
||||
let c2 = s2.get_cell(x, y);
|
||||
assert_eq!(c1.material, c2.material, "material mismatch at ({},{})", x, y);
|
||||
assert_eq!(
|
||||
c1.material, c2.material,
|
||||
"material mismatch at ({},{})",
|
||||
x, y
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +83,8 @@ fn replay_exact_match() {
|
||||
s.perform_action(&AiAction::MoveLeft);
|
||||
s.step(10);
|
||||
let state_orig = s.get_state();
|
||||
s.save_replay("/tmp/verbatim_replay_exact.json").expect("save");
|
||||
s.save_replay("/tmp/verbatim_replay_exact.json")
|
||||
.expect("save");
|
||||
|
||||
let player = ReplayPlayer::load("/tmp/verbatim_replay_exact.json").expect("load");
|
||||
let s2 = player.play();
|
||||
@@ -77,8 +92,18 @@ fn replay_exact_match() {
|
||||
|
||||
assert_eq!(state_orig.tick, state_replay.tick, "tick mismatch");
|
||||
if let (Some(p1), Some(p2)) = (&state_orig.player, &state_replay.player) {
|
||||
assert!((p1.pos[0] - p2.pos[0]).abs() < 0.1, "x mismatch: {} vs {}", p1.pos[0], p2.pos[0]);
|
||||
assert!((p1.pos[1] - p2.pos[1]).abs() < 0.1, "y mismatch: {} vs {}", p1.pos[1], p2.pos[1]);
|
||||
assert!(
|
||||
(p1.pos[0] - p2.pos[0]).abs() < 0.1,
|
||||
"x mismatch: {} vs {}",
|
||||
p1.pos[0],
|
||||
p2.pos[0]
|
||||
);
|
||||
assert!(
|
||||
(p1.pos[1] - p2.pos[1]).abs() < 0.1,
|
||||
"y mismatch: {} vs {}",
|
||||
p1.pos[1],
|
||||
p2.pos[1]
|
||||
);
|
||||
assert!((p1.health - p2.health).abs() < 1.0, "health mismatch");
|
||||
}
|
||||
}
|
||||
@@ -91,14 +116,25 @@ fn replay_stop_at_tick() {
|
||||
s.step(20);
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(20);
|
||||
s.save_replay("/tmp/verbatim_replay_partial.json").expect("save");
|
||||
s.save_replay("/tmp/verbatim_replay_partial.json")
|
||||
.expect("save");
|
||||
|
||||
let player = ReplayPlayer::load("/tmp/verbatim_replay_partial.json").expect("load");
|
||||
let s_half = player.play_until_tick(10);
|
||||
assert_eq!(s_half.tick(), 10, "should stop at tick 10, got {}", s_half.tick());
|
||||
assert_eq!(
|
||||
s_half.tick(),
|
||||
10,
|
||||
"should stop at tick 10, got {}",
|
||||
s_half.tick()
|
||||
);
|
||||
|
||||
let s_full = player.play();
|
||||
assert_eq!(s_full.tick(), 40, "full replay should reach tick 40, got {}", s_full.tick());
|
||||
assert_eq!(
|
||||
s_full.tick(),
|
||||
40,
|
||||
"full replay should reach tick 40, got {}",
|
||||
s_full.tick()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -110,28 +146,52 @@ fn recording_captures_all_actions() {
|
||||
s.perform_action(&AiAction::Jump);
|
||||
s.perform_action(&AiAction::MoveLeft);
|
||||
s.step(5);
|
||||
s.save_replay("/tmp/verbatim_replay_capture.json").expect("save");
|
||||
s.save_replay("/tmp/verbatim_replay_capture.json")
|
||||
.expect("save");
|
||||
|
||||
let player = ReplayPlayer::load("/tmp/verbatim_replay_capture.json").expect("load");
|
||||
let event_count = player.recording().events.len();
|
||||
assert!(event_count >= 4, "recording should have at least 4 events (3 actions + 1 step), got {}", event_count);
|
||||
assert!(
|
||||
event_count >= 4,
|
||||
"recording should have at least 4 events (3 actions + 1 step), got {}",
|
||||
event_count
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn determinism_with_spawn_and_damage() {
|
||||
let mut s1 = setup();
|
||||
s1.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 135.0, y: 120.0 });
|
||||
s1.perform_action(&AiAction::DamageEntity { id: 1, amount: 20.0 });
|
||||
s1.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 135.0,
|
||||
y: 120.0,
|
||||
});
|
||||
s1.perform_action(&AiAction::DamageEntity {
|
||||
id: 1,
|
||||
amount: 20.0,
|
||||
});
|
||||
s1.step(30);
|
||||
|
||||
let mut s2 = setup();
|
||||
s2.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 135.0, y: 120.0 });
|
||||
s2.perform_action(&AiAction::DamageEntity { id: 1, amount: 20.0 });
|
||||
s2.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 135.0,
|
||||
y: 120.0,
|
||||
});
|
||||
s2.perform_action(&AiAction::DamageEntity {
|
||||
id: 1,
|
||||
amount: 20.0,
|
||||
});
|
||||
s2.step(30);
|
||||
|
||||
let e1 = s1.get_entities().into_iter().find(|e| e.id == 1).unwrap();
|
||||
let e2 = s2.get_entities().into_iter().find(|e| e.id == 1).unwrap();
|
||||
assert!((e1.health - e2.health).abs() < 0.01, "health should match: {} vs {}", e1.health, e2.health);
|
||||
assert!(
|
||||
(e1.health - e2.health).abs() < 0.01,
|
||||
"health should match: {} vs {}",
|
||||
e1.health,
|
||||
e2.health
|
||||
);
|
||||
assert!((e1.pos[0] - e2.pos[0]).abs() < 0.1, "x should match");
|
||||
assert!((e1.pos[1] - e2.pos[1]).abs() < 0.1, "y should match");
|
||||
}
|
||||
@@ -150,5 +210,10 @@ fn hundred_tick_determinism() {
|
||||
|
||||
let p1 = s1.get_player().unwrap();
|
||||
let p2 = s2.get_player().unwrap();
|
||||
assert!((p1.pos[0] - p2.pos[0]).abs() < 0.01, "100-tick determinism failed: {} vs {}", p1.pos[0], p2.pos[0]);
|
||||
assert!(
|
||||
(p1.pos[0] - p2.pos[0]).abs() < 0.01,
|
||||
"100-tick determinism failed: {} vs {}",
|
||||
p1.pos[0],
|
||||
p2.pos[0]
|
||||
);
|
||||
}
|
||||
|
||||
+93
-17
@@ -1,5 +1,5 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup_empty() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
@@ -11,39 +11,92 @@ fn setup_empty() -> GameSession {
|
||||
#[test]
|
||||
fn entity_takes_lava_damage() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 130, w: 20, h: 3, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 114, y: 127, w: 8, h: 3, material: "lava".into() });
|
||||
s.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 118.0, y: 118.0 });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 130,
|
||||
w: 20,
|
||||
h: 3,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 114,
|
||||
y: 127,
|
||||
w: 8,
|
||||
h: 3,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 118.0,
|
||||
y: 118.0,
|
||||
});
|
||||
s.step(80);
|
||||
let entities = s.get_entities();
|
||||
let goblin = entities.into_iter().find(|e| e.kind == "Goblin");
|
||||
assert!(goblin.is_some(), "goblin should exist");
|
||||
let g = goblin.unwrap();
|
||||
assert!(g.health < 40.0, "goblin should have taken damage from lava, hp={}", g.health);
|
||||
assert!(
|
||||
g.health < 40.0,
|
||||
"goblin should have taken damage from lava, hp={}",
|
||||
g.health
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entity_dies_becomes_corpse() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 110.0, y: 120.0 });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 110.0,
|
||||
y: 120.0,
|
||||
});
|
||||
s.step(30);
|
||||
s.perform_action(&AiAction::DamageEntity { id: 1, amount: 100.0 });
|
||||
s.perform_action(&AiAction::DamageEntity {
|
||||
id: 1,
|
||||
amount: 100.0,
|
||||
});
|
||||
s.step(1);
|
||||
let entities = s.get_entities();
|
||||
let goblin = entities.into_iter().find(|e| e.id == 1);
|
||||
assert!(goblin.is_some(), "entity should still exist");
|
||||
let g = goblin.unwrap();
|
||||
assert!(!g.alive, "entity should be dead after 100 damage");
|
||||
assert_eq!(g.kind, "Corpse", "dead entity should be a corpse, got {}", g.kind);
|
||||
assert_eq!(
|
||||
g.kind, "Corpse",
|
||||
"dead entity should be a corpse, got {}",
|
||||
g.kind
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entity_on_fire_takes_damage_over_time() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 116, y: 123, w: 4, h: 2, material: "lava".into() });
|
||||
s.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 118.0, y: 120.0 });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 116,
|
||||
y: 123,
|
||||
w: 4,
|
||||
h: 2,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 118.0,
|
||||
y: 120.0,
|
||||
});
|
||||
s.step(20);
|
||||
let entities = s.get_entities();
|
||||
let goblin = entities.into_iter().find(|e| e.id == 1);
|
||||
@@ -53,7 +106,10 @@ fn entity_on_fire_takes_damage_over_time() {
|
||||
s.step(30);
|
||||
let entities2 = s.get_entities();
|
||||
if let Some(g2) = entities2.into_iter().find(|e| e.id == 1) {
|
||||
assert!(g2.health < hp_after_fire, "entity on fire should lose more health over time");
|
||||
assert!(
|
||||
g2.health < hp_after_fire,
|
||||
"entity on fire should lose more health over time"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,12 +118,32 @@ fn entity_on_fire_takes_damage_over_time() {
|
||||
#[test]
|
||||
fn entity_blocked_by_stone() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 130, w: 30, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 110, y: 126, w: 1, h: 4, material: "stone".into() });
|
||||
s.perform_action(&AiAction::Spawn { kind: "goblin".into(), x: 105.0, y: 125.0 });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 130,
|
||||
w: 30,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 110,
|
||||
y: 126,
|
||||
w: 1,
|
||||
h: 4,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::Spawn {
|
||||
kind: "goblin".into(),
|
||||
x: 105.0,
|
||||
y: 125.0,
|
||||
});
|
||||
s.step(30);
|
||||
let entities = s.get_entities();
|
||||
if let Some(g) = entities.into_iter().find(|e| e.id == 1) {
|
||||
assert!(g.pos[0] < 110.0, "goblin should be blocked by stone wall, got x={}", g.pos[0]);
|
||||
assert!(
|
||||
g.pos[0] < 110.0,
|
||||
"goblin should be blocked by stone wall, got x={}",
|
||||
g.pos[0]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup_empty() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
s.init_empty();
|
||||
s.clear_area(95, 95, 40, 35);
|
||||
s.perform_action(&AiAction::FillRect { x: 80, y: 135, w: 80, h: 15, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 80,
|
||||
y: 135,
|
||||
w: 80,
|
||||
h: 15,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s
|
||||
}
|
||||
|
||||
@@ -14,16 +20,32 @@ fn player_falls_and_lands() {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
s.init_empty();
|
||||
s.clear_area(115, 115, 20, 15);
|
||||
s.perform_action(&AiAction::FillRect { x: 110, y: 130, w: 30, h: 15, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 110,
|
||||
y: 130,
|
||||
w: 30,
|
||||
h: 15,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.step(80);
|
||||
let player = s.get_player().expect("player should exist");
|
||||
assert!(player.alive, "player should be alive");
|
||||
let y = player.pos[1];
|
||||
assert!(y < 135.0, "player should not fall through stone floor, got y={}", y);
|
||||
assert!(
|
||||
y < 135.0,
|
||||
"player should not fall through stone floor, got y={}",
|
||||
y
|
||||
);
|
||||
s.step(30);
|
||||
let player2 = s.get_player().expect("player should exist");
|
||||
let dy = (player2.pos[1] - y).abs();
|
||||
assert!(dy < 3.0, "player should have stopped falling (dy={:.2}), y={} -> {}", dy, y, player2.pos[1]);
|
||||
assert!(
|
||||
dy < 3.0,
|
||||
"player should have stopped falling (dy={:.2}), y={} -> {}",
|
||||
dy,
|
||||
y,
|
||||
player2.pos[1]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -32,11 +54,20 @@ fn player_blocked_by_stone_wall() {
|
||||
s.step(30);
|
||||
let player = s.get_player().expect("player should exist");
|
||||
let x = player.pos[0] as i32;
|
||||
s.perform_action(&AiAction::FillRect { x: x + 5, y: 125, w: 1, h: 10, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: x + 5,
|
||||
y: 125,
|
||||
w: 1,
|
||||
h: 10,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(10);
|
||||
let player = s.get_player().expect("player should exist");
|
||||
assert!(player.pos[0] < (x + 5) as f32, "player should be blocked by wall");
|
||||
assert!(
|
||||
player.pos[0] < (x + 5) as f32,
|
||||
"player should be blocked by wall"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -48,7 +79,12 @@ fn player_can_move_right() {
|
||||
s.perform_action(&AiAction::MoveRight);
|
||||
s.step(10);
|
||||
let player = s.get_player().expect("player should exist");
|
||||
assert!(player.pos[0] > initial_x, "player should have moved right: {} -> {}", initial_x, player.pos[0]);
|
||||
assert!(
|
||||
player.pos[0] > initial_x,
|
||||
"player should have moved right: {} -> {}",
|
||||
initial_x,
|
||||
player.pos[0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -57,5 +93,9 @@ fn player_survives_fall() {
|
||||
s.step(60);
|
||||
let player = s.get_player().expect("player should exist");
|
||||
assert!(player.alive, "player should survive a fall onto stone");
|
||||
assert!(player.health > 50.0, "player should not take significant damage from landing, hp={}", player.health);
|
||||
assert!(
|
||||
player.health > 50.0,
|
||||
"player should not take significant damage from landing, hp={}",
|
||||
player.health
|
||||
);
|
||||
}
|
||||
|
||||
+49
-12
@@ -1,5 +1,5 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::ReplayPlayer;
|
||||
|
||||
#[test]
|
||||
@@ -13,7 +13,8 @@ fn replay_deterministic() {
|
||||
s1.step(10);
|
||||
let state1 = s1.get_state();
|
||||
|
||||
s1.save_replay("/tmp/verbatim_test_replay.json").expect("save replay");
|
||||
s1.save_replay("/tmp/verbatim_test_replay.json")
|
||||
.expect("save replay");
|
||||
|
||||
let player = ReplayPlayer::load("/tmp/verbatim_test_replay.json").expect("load replay");
|
||||
let s2 = player.play();
|
||||
@@ -22,8 +23,18 @@ fn replay_deterministic() {
|
||||
assert_eq!(state1.tick, state2.tick, "ticks should match");
|
||||
if let (Some(p1), Some(p2)) = (&state1.player, &state2.player) {
|
||||
assert_eq!(p1.health, p2.health, "player health should match");
|
||||
assert!((p1.pos[0] - p2.pos[0]).abs() < 0.01, "player x should match: {} vs {}", p1.pos[0], p2.pos[0]);
|
||||
assert!((p1.pos[1] - p2.pos[1]).abs() < 0.01, "player y should match: {} vs {}", p1.pos[1], p2.pos[1]);
|
||||
assert!(
|
||||
(p1.pos[0] - p2.pos[0]).abs() < 0.01,
|
||||
"player x should match: {} vs {}",
|
||||
p1.pos[0],
|
||||
p2.pos[0]
|
||||
);
|
||||
assert!(
|
||||
(p1.pos[1] - p2.pos[1]).abs() < 0.01,
|
||||
"player y should match: {} vs {}",
|
||||
p1.pos[1],
|
||||
p2.pos[1]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +49,25 @@ fn replay_play_until_tick() {
|
||||
s.step(5);
|
||||
s.perform_action(&AiAction::Jump);
|
||||
s.step(10);
|
||||
s.save_replay("/tmp/verbatim_test_replay2.json").expect("save");
|
||||
s.save_replay("/tmp/verbatim_test_replay2.json")
|
||||
.expect("save");
|
||||
|
||||
let player = ReplayPlayer::load("/tmp/verbatim_test_replay2.json").expect("load");
|
||||
let s_half = player.play_until_tick(5);
|
||||
assert_eq!(s_half.tick(), 5, "should stop at tick 5, got {}", s_half.tick());
|
||||
assert_eq!(
|
||||
s_half.tick(),
|
||||
5,
|
||||
"should stop at tick 5, got {}",
|
||||
s_half.tick()
|
||||
);
|
||||
|
||||
let s_full = player.play();
|
||||
assert_eq!(s_full.tick(), 20, "full replay should reach tick 20, got {}", s_full.tick());
|
||||
assert_eq!(
|
||||
s_full.tick(),
|
||||
20,
|
||||
"full replay should reach tick 20, got {}",
|
||||
s_full.tick()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -62,8 +84,18 @@ fn same_seed_same_state() {
|
||||
|
||||
assert_eq!(state1.tick, state2.tick);
|
||||
if let (Some(p1), Some(p2)) = (&state1.player, &state2.player) {
|
||||
assert!((p1.pos[0] - p2.pos[0]).abs() < 0.01, "x mismatch: {} vs {}", p1.pos[0], p2.pos[0]);
|
||||
assert!((p1.pos[1] - p2.pos[1]).abs() < 0.01, "y mismatch: {} vs {}", p1.pos[1], p2.pos[1]);
|
||||
assert!(
|
||||
(p1.pos[0] - p2.pos[0]).abs() < 0.01,
|
||||
"x mismatch: {} vs {}",
|
||||
p1.pos[0],
|
||||
p2.pos[0]
|
||||
);
|
||||
assert!(
|
||||
(p1.pos[1] - p2.pos[1]).abs() < 0.01,
|
||||
"y mismatch: {} vs {}",
|
||||
p1.pos[1],
|
||||
p2.pos[1]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +105,8 @@ fn pipe_protocol_init_and_step() {
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_verbatim"))
|
||||
.arg("--mode").arg("pipe")
|
||||
.arg("--mode")
|
||||
.arg("pipe")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
@@ -98,9 +131,13 @@ fn pipe_protocol_init_and_step() {
|
||||
stdin.flush().expect("flush");
|
||||
let n = stdout.read(&mut buf).expect("read");
|
||||
let output2 = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
let json2: serde_json::Value = serde_json::from_str(output2.trim()).expect("parse step response");
|
||||
let json2: serde_json::Value =
|
||||
serde_json::from_str(output2.trim()).expect("parse step response");
|
||||
assert_eq!(json2["ok"], true);
|
||||
assert_eq!(json2["state"]["tick"], 10, "tick should be 10 after stepping 10");
|
||||
assert_eq!(
|
||||
json2["state"]["tick"], 10,
|
||||
"tick should be 10 after stepping 10"
|
||||
);
|
||||
|
||||
writeln!(stdin, "{{\"cmd\":\"quit\"}}").expect("write quit");
|
||||
stdin.flush().expect("flush");
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
use verbatim::game::Game;
|
||||
|
||||
#[test]
|
||||
#[ignore = "slow: generates a 12500x12500 world"]
|
||||
fn large_world_initialization() {
|
||||
let mut game = Game::new_random();
|
||||
assert!(game.grid.is_infinite());
|
||||
game.init_world();
|
||||
let (px, py) = game.player.center(&game.entities);
|
||||
assert!(px >= 0.0 && py >= 0.0);
|
||||
let foot_x = px as i32;
|
||||
let foot_y = (py + 3.0).ceil() as i32;
|
||||
assert!(
|
||||
!game.grid.get(foot_x, foot_y).is_solid(),
|
||||
"player spawn should not be inside solid: px={} py={} foot=({},{}) mat={:?}",
|
||||
px,
|
||||
py,
|
||||
foot_x,
|
||||
foot_y,
|
||||
game.grid.get(foot_x, foot_y).material
|
||||
);
|
||||
if let Some(ref root) = game.cache_dir {
|
||||
let meta = verbatim::world::cache::WorldCache::meta_path(root, game.seed);
|
||||
assert!(
|
||||
meta.exists(),
|
||||
"world cache should be written after generation"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "slow: loads cached 12500x12500 world"]
|
||||
fn large_world_cache_roundtrip() {
|
||||
let mut game = Game::new_random();
|
||||
game.init_world();
|
||||
let (px, py) = game.player.center(&game.entities);
|
||||
let item_count = game.items.all().len();
|
||||
let root = game.cache_dir.clone().unwrap();
|
||||
let seed = game.seed;
|
||||
|
||||
let mut game2 = Game::new_random();
|
||||
game2.seed = seed;
|
||||
game2.ca.seed(seed);
|
||||
game2.cache_dir = Some(root);
|
||||
game2.init_world();
|
||||
|
||||
let (px2, py2) = game2.player.center(&game2.entities);
|
||||
assert!((px - px2).abs() < 0.01 && (py - py2).abs() < 0.01);
|
||||
assert_eq!(game2.items.all().len(), item_count);
|
||||
assert!(game2.grid.is_infinite());
|
||||
}
|
||||
+60
-12
@@ -1,5 +1,5 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup_empty() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
@@ -11,28 +11,76 @@ fn setup_empty() -> GameSession {
|
||||
#[test]
|
||||
fn acid_dissolves_wood() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 118, material: "wood".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 119, material: "acid".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 119, material: "acid".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 118,
|
||||
material: "wood".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 119,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 119,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.step(30);
|
||||
assert_ne!(s.get_cell(104, 118).material, "wood", "acid should have dissolved the wood");
|
||||
assert_ne!(
|
||||
s.get_cell(104, 118).material,
|
||||
"wood",
|
||||
"acid should have dissolved the wood"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acid_does_not_dissolve_stone() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 111, material: "acid".into() });
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 110,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 111,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.step(20);
|
||||
assert_eq!(s.get_cell(105, 110).material, "stone", "acid should not dissolve stone");
|
||||
assert_eq!(
|
||||
s.get_cell(105, 110).material,
|
||||
"stone",
|
||||
"acid should not dissolve stone"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acid_flows_down() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 105, material: "acid".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 105,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.step(20);
|
||||
assert_ne!(s.get_cell(105, 105).material, "acid", "acid should have flowed down from y=105");
|
||||
assert_ne!(
|
||||
s.get_cell(105, 105).material,
|
||||
"acid",
|
||||
"acid should have flowed down from y=105"
|
||||
);
|
||||
}
|
||||
|
||||
+296
-54
@@ -1,8 +1,8 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
let mut s = GameSession::new_seeded(2);
|
||||
s.init_empty();
|
||||
s.clear_area(90, 90, 50, 50);
|
||||
s
|
||||
@@ -11,9 +11,23 @@ fn setup() -> GameSession {
|
||||
#[test]
|
||||
fn lava_flows_down_on_stone() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 115, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 116, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 115,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 116,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.step(15);
|
||||
let lava_at_floor = s.count_material_in_region(103, 122, 5, 4, "lava");
|
||||
assert!(lava_at_floor > 0, "lava should flow down to stone floor");
|
||||
@@ -22,25 +36,69 @@ fn lava_flows_down_on_stone() {
|
||||
#[test]
|
||||
fn lava_cools_to_stone_eventually() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 120, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 121, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 122, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 123, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 124, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 120,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 121,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 122,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 123,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 124,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.step(200);
|
||||
let stone_count = s.count_material_in_region(103, 120, 5, 6, "stone");
|
||||
assert!(stone_count >= 3, "lava should cool to stone eventually, got {} stone", stone_count);
|
||||
assert!(
|
||||
stone_count >= 3,
|
||||
"lava should cool to stone eventually, got {} stone",
|
||||
stone_count
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fire_spreads_through_wood_line() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
for x in 100..110 {
|
||||
s.perform_action(&AiAction::SetCell { x, y: 119, material: "wood".into() });
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x,
|
||||
y: 119,
|
||||
material: "wood".into(),
|
||||
});
|
||||
}
|
||||
s.perform_action(&AiAction::SetCell { x: 100, y: 119, material: "fire".into() });
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 100,
|
||||
y: 119,
|
||||
material: "fire".into(),
|
||||
});
|
||||
s.step(40);
|
||||
let wood_left = s.count_material_in_region(100, 118, 10, 3, "wood");
|
||||
assert_eq!(wood_left, 0, "fire should spread through entire wood line");
|
||||
@@ -49,56 +107,173 @@ fn fire_spreads_through_wood_line() {
|
||||
#[test]
|
||||
fn acid_does_not_dissolve_empty() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 104, y: 114, w: 3, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 104, y: 113, w: 1, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 106, y: 113, w: 1, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 113, material: "acid".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 104,
|
||||
y: 114,
|
||||
w: 3,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 104,
|
||||
y: 113,
|
||||
w: 1,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 106,
|
||||
y: 113,
|
||||
w: 1,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 113,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.step(5);
|
||||
let cell = s.get_cell(105, 113);
|
||||
assert!(cell.material == "acid" || cell.material == "empty",
|
||||
"acid may flow out but should not dissolve empty, got {}", cell.material);
|
||||
assert!(
|
||||
cell.material == "acid" || cell.material == "empty",
|
||||
"acid may flow out but should not dissolve empty, got {}",
|
||||
cell.material
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acid_dissolves_grass() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 119, material: "grass".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 119, material: "acid".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 118, material: "acid".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 119,
|
||||
material: "grass".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 119,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 118,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.step(20);
|
||||
assert_ne!(s.get_cell(104, 119).material, "grass", "acid should dissolve grass");
|
||||
assert_ne!(
|
||||
s.get_cell(104, 119).material,
|
||||
"grass",
|
||||
"acid should dissolve grass"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acid_dissolves_dirt() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 114, material: "dirt".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 114, material: "acid".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 113, material: "acid".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 114,
|
||||
material: "dirt".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 114,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 113,
|
||||
material: "acid".into(),
|
||||
});
|
||||
s.step(30);
|
||||
assert_ne!(s.get_cell(104, 114).material, "dirt", "acid should dissolve dirt");
|
||||
assert_ne!(
|
||||
s.get_cell(104, 114).material,
|
||||
"dirt",
|
||||
"acid should dissolve dirt"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn water_extinguishes_fire_indirectly() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 119, material: "fire".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 118, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 119,
|
||||
material: "fire".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 118,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(20);
|
||||
assert_ne!(s.get_cell(105, 119).material, "fire", "water should extinguish fire");
|
||||
assert_ne!(
|
||||
s.get_cell(105, 119).material,
|
||||
"fire",
|
||||
"water should extinguish fire"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lava_and_water_produce_both_steam_and_stone() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 95, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 101, y: 115, w: 4, h: 3, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 106, y: 115, w: 4, h: 3, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 95,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 101,
|
||||
y: 115,
|
||||
w: 4,
|
||||
h: 3,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 106,
|
||||
y: 115,
|
||||
w: 4,
|
||||
h: 3,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(30);
|
||||
let steam = s.count_material_in_region(100, 100, 20, 20, "steam");
|
||||
assert!(steam > 0, "lava + water should produce steam");
|
||||
@@ -107,41 +282,108 @@ fn lava_and_water_produce_both_steam_and_stone() {
|
||||
#[test]
|
||||
fn sand_falls_through_water() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 104, y: 118, w: 3, h: 7, material: "water".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 113, material: "sand".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 104,
|
||||
y: 118,
|
||||
w: 3,
|
||||
h: 7,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 113,
|
||||
material: "sand".into(),
|
||||
});
|
||||
s.step(30);
|
||||
let sand_at_bottom = s.get_cell(105, 124).material;
|
||||
assert_eq!(sand_at_bottom, "sand", "sand should sink through water to bottom");
|
||||
assert_eq!(
|
||||
sand_at_bottom, "sand",
|
||||
"sand should sink through water to bottom"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fire_does_not_ignite_stone() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 110, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "fire".into() });
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 110,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 110,
|
||||
material: "fire".into(),
|
||||
});
|
||||
s.step(30);
|
||||
assert_eq!(s.get_cell(104, 110).material, "stone", "fire should not ignite stone");
|
||||
assert_eq!(
|
||||
s.get_cell(104, 110).material,
|
||||
"stone",
|
||||
"fire should not ignite stone"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fire_does_not_ignite_water() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 114, material: "water".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 114, material: "fire".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 114,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 114,
|
||||
material: "fire".into(),
|
||||
});
|
||||
s.step(5);
|
||||
let cell = s.get_cell(104, 114);
|
||||
assert_ne!(cell.material, "fire", "water should never become fire, got {}", cell.material);
|
||||
assert_ne!(
|
||||
cell.material, "fire",
|
||||
"water should never become fire, got {}",
|
||||
cell.material
|
||||
);
|
||||
assert_ne!(cell.material, "wood", "water should never become wood");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn water_does_not_flow_through_dirt_wall() {
|
||||
let mut s = setup();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 109, y: 120, w: 1, h: 5, material: "dirt".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 9, h: 5, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 125,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 109,
|
||||
y: 120,
|
||||
w: 1,
|
||||
h: 5,
|
||||
material: "dirt".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 9,
|
||||
h: 5,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(30);
|
||||
let right_water = s.count_material_in_region(110, 118, 5, 8, "water");
|
||||
assert_eq!(right_water, 0, "water should not flow through dirt wall");
|
||||
|
||||
+113
-21
@@ -1,5 +1,5 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup_empty() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
@@ -11,48 +11,140 @@ fn setup_empty() -> GameSession {
|
||||
#[test]
|
||||
fn lava_flows_down() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 3, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 111, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 3,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 110,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 111,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.step(10);
|
||||
let lava_count = s.count_material_in_region(103, 110, 5, 6, "lava");
|
||||
let stone_count = s.count_material_in_region(103, 110, 5, 6, "stone");
|
||||
assert!(lava_count > 0 || stone_count >= 4,
|
||||
"lava should have flowed down or cooled to stone, lava={} stone={}", lava_count, stone_count);
|
||||
assert!(
|
||||
lava_count > 0 || stone_count >= 4,
|
||||
"lava should have flowed down or cooled to stone, lava={} stone={}",
|
||||
lava_count,
|
||||
stone_count
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lava_plus_water_makes_steam() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 99, w: 20, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 102, y: 110, w: 4, h: 3, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 108, y: 110, w: 4, h: 3, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 120,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 99,
|
||||
w: 20,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 102,
|
||||
y: 110,
|
||||
w: 4,
|
||||
h: 3,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 108,
|
||||
y: 110,
|
||||
w: 4,
|
||||
h: 3,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(40);
|
||||
let steam_count = s.count_material_in_region(100, 100, 20, 15, "steam");
|
||||
assert!(steam_count > 0, "lava + water should produce steam, got {} steam cells", steam_count);
|
||||
assert!(
|
||||
steam_count > 0,
|
||||
"lava + water should produce steam, got {} steam cells",
|
||||
steam_count
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lava_ignites_wood() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 104, y: 114, material: "wood".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 114, material: "wood".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 106, y: 114, material: "wood".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 113, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 104,
|
||||
y: 114,
|
||||
material: "wood".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 114,
|
||||
material: "wood".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 106,
|
||||
y: 114,
|
||||
material: "wood".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 113,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.step(15);
|
||||
let wood_remaining = s.count_material_in_region(103, 113, 5, 3, "wood");
|
||||
assert_eq!(wood_remaining, 0, "all wood should have been ignited by lava, got {} wood cells", wood_remaining);
|
||||
assert_eq!(
|
||||
wood_remaining, 0,
|
||||
"all wood should have been ignited by lava, got {} wood cells",
|
||||
wood_remaining
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lava_ignites_grass() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 114, w: 5, h: 1, material: "grass".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 113, material: "lava".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 114, material: "lava".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 114,
|
||||
w: 5,
|
||||
h: 1,
|
||||
material: "grass".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 113,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 114,
|
||||
material: "lava".into(),
|
||||
});
|
||||
s.step(15);
|
||||
let grass_remaining = s.count_material_in_region(99, 113, 7, 3, "grass");
|
||||
assert_eq!(grass_remaining, 0, "grass should have been ignited by lava");
|
||||
|
||||
+86
-16
@@ -1,5 +1,5 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup_empty() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
@@ -11,19 +11,53 @@ fn setup_empty() -> GameSession {
|
||||
#[test]
|
||||
fn sand_falls_down() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 105, material: "sand".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 105,
|
||||
material: "sand".into(),
|
||||
});
|
||||
s.step(10);
|
||||
assert_eq!(s.get_cell(105, 105).material, "empty", "sand should have fallen from y=105");
|
||||
assert_eq!(s.get_cell(105, 114).material, "sand", "sand should be resting on stone at y=114");
|
||||
assert_eq!(
|
||||
s.get_cell(105, 105).material,
|
||||
"empty",
|
||||
"sand should have fallen from y=105"
|
||||
);
|
||||
assert_eq!(
|
||||
s.get_cell(105, 114).material,
|
||||
"sand",
|
||||
"sand should be resting on stone at y=114"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sand_displaces_water() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 110, w: 10, h: 5, material: "water".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 105, material: "sand".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 110,
|
||||
w: 10,
|
||||
h: 5,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 105,
|
||||
material: "sand".into(),
|
||||
});
|
||||
s.step(20);
|
||||
let sand_at_bottom = s.get_cell(105, 114).material == "sand";
|
||||
assert!(sand_at_bottom, "sand should sink to bottom through water");
|
||||
@@ -32,20 +66,56 @@ fn sand_displaces_water() {
|
||||
#[test]
|
||||
fn sand_piles_on_stone() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 115, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 105, material: "sand".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 104, material: "sand".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 115,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 105,
|
||||
material: "sand".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 104,
|
||||
material: "sand".into(),
|
||||
});
|
||||
s.step(15);
|
||||
let count = s.count_material_in_region(104, 112, 3, 4, "sand");
|
||||
assert!(count >= 2, "both sand cells should have piled up, got {} sand cells", count);
|
||||
assert!(
|
||||
count >= 2,
|
||||
"both sand cells should have piled up, got {} sand cells",
|
||||
count
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sand_does_not_fall_through_stone() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 110, w: 10, h: 1, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 105, y: 105, material: "sand".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 110,
|
||||
w: 10,
|
||||
h: 1,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 105,
|
||||
y: 105,
|
||||
material: "sand".into(),
|
||||
});
|
||||
s.step(10);
|
||||
assert_eq!(s.get_cell(105, 109).material, "sand", "sand should rest on top of stone");
|
||||
assert_eq!(s.get_cell(105, 110).material, "stone", "stone should remain");
|
||||
assert_eq!(
|
||||
s.get_cell(105, 109).material,
|
||||
"sand",
|
||||
"sand should rest on top of stone"
|
||||
);
|
||||
assert_eq!(
|
||||
s.get_cell(105, 110).material,
|
||||
"stone",
|
||||
"stone should remain"
|
||||
);
|
||||
}
|
||||
|
||||
+69
-12
@@ -1,5 +1,5 @@
|
||||
use verbatim::ai::GameSession;
|
||||
use verbatim::ai::AiAction;
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn setup_empty() -> GameSession {
|
||||
let mut s = GameSession::new_seeded(42);
|
||||
@@ -11,32 +11,89 @@ fn setup_empty() -> GameSession {
|
||||
#[test]
|
||||
fn water_flows_down() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 95, y: 115, w: 40, h: 3, material: "stone".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 110, y: 105, material: "water".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 110, y: 106, material: "water".into() });
|
||||
s.perform_action(&AiAction::SetCell { x: 110, y: 107, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 95,
|
||||
y: 115,
|
||||
w: 40,
|
||||
h: 3,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 110,
|
||||
y: 105,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 110,
|
||||
y: 106,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::SetCell {
|
||||
x: 110,
|
||||
y: 107,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(20);
|
||||
let water_near_bottom = s.count_material_in_region(105, 112, 10, 4, "water");
|
||||
assert!(water_near_bottom > 0, "water should have flowed down to near the stone floor, found {} water cells near bottom", water_near_bottom);
|
||||
assert!(
|
||||
water_near_bottom > 0,
|
||||
"water should have flowed down to near the stone floor, found {} water cells near bottom",
|
||||
water_near_bottom
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn water_spreads_sideways() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 95, y: 115, w: 50, h: 3, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 110, y: 111, w: 1, h: 4, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 95,
|
||||
y: 115,
|
||||
w: 50,
|
||||
h: 3,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 110,
|
||||
y: 111,
|
||||
w: 1,
|
||||
h: 4,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(50);
|
||||
let left_count = s.count_material_in_region(100, 110, 10, 5, "water");
|
||||
let right_count = s.count_material_in_region(111, 110, 10, 5, "water");
|
||||
assert!(left_count > 0 || right_count > 0, "water should spread sideways: left={} right={}", left_count, right_count);
|
||||
assert!(
|
||||
left_count > 0 || right_count > 0,
|
||||
"water should spread sideways: left={} right={}",
|
||||
left_count,
|
||||
right_count
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn water_does_not_pass_through_stone_wall() {
|
||||
let mut s = setup_empty();
|
||||
s.perform_action(&AiAction::FillRect { x: 95, y: 115, w: 50, h: 3, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 115, y: 110, w: 1, h: 5, material: "stone".into() });
|
||||
s.perform_action(&AiAction::FillRect { x: 100, y: 110, w: 15, h: 5, material: "water".into() });
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 95,
|
||||
y: 115,
|
||||
w: 50,
|
||||
h: 3,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 115,
|
||||
y: 110,
|
||||
w: 1,
|
||||
h: 5,
|
||||
material: "stone".into(),
|
||||
});
|
||||
s.perform_action(&AiAction::FillRect {
|
||||
x: 100,
|
||||
y: 110,
|
||||
w: 15,
|
||||
h: 5,
|
||||
material: "water".into(),
|
||||
});
|
||||
s.step(30);
|
||||
let right_water = s.count_material_in_region(116, 108, 10, 10, "water");
|
||||
assert_eq!(right_water, 0, "water should not pass through stone wall");
|
||||
|
||||
@@ -0,0 +1,268 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use verbatim::ai::GameSession;
|
||||
|
||||
fn init_at_depth(depth: u32) -> GameSession {
|
||||
let mut s = GameSession::new_seeded(123);
|
||||
s.game.depth = depth;
|
||||
s.init();
|
||||
s
|
||||
}
|
||||
|
||||
fn count_distinct_materials(s: &GameSession) -> usize {
|
||||
let mut present = Vec::new();
|
||||
for y in 0..s.game.grid.height as i32 {
|
||||
for x in 0..s.game.grid.width as i32 {
|
||||
let mat = s.game.grid.get(x, y).material;
|
||||
if !present.contains(&mat) {
|
||||
present.push(mat);
|
||||
}
|
||||
}
|
||||
}
|
||||
present.len()
|
||||
}
|
||||
|
||||
fn total_empty(s: &GameSession) -> usize {
|
||||
let mut count = 0;
|
||||
for y in 0..s.game.grid.height as i32 {
|
||||
for x in 0..s.game.grid.width as i32 {
|
||||
if s.game.grid.get(x, y).is_empty() {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn player_not_in_solid(s: &GameSession) -> bool {
|
||||
let (px, py) = s.game.player.center(&s.game.entities);
|
||||
let ix = px as i32;
|
||||
let iy = py as i32;
|
||||
for dy in -1..=1 {
|
||||
for dx in -1..=1 {
|
||||
if s.game.grid.get(ix + dx, iy + dy).is_solid() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn flood_fill_count(s: &GameSession, x: i32, y: i32) -> usize {
|
||||
let w = s.game.grid.width as i32;
|
||||
let h = s.game.grid.height as i32;
|
||||
let mut visited = vec![false; (w * h) as usize];
|
||||
let mut q = VecDeque::new();
|
||||
q.push_back((x, y));
|
||||
let mut count = 0;
|
||||
while let Some((cx, cy)) = q.pop_front() {
|
||||
let idx = (cy * w + cx) as usize;
|
||||
if visited[idx] || !s.game.grid.in_bounds(cx, cy) || !s.game.grid.get(cx, cy).is_empty() {
|
||||
continue;
|
||||
}
|
||||
visited[idx] = true;
|
||||
count += 1;
|
||||
for (dx, dy) in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
|
||||
q.push_back((cx + dx, cy + dy));
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn count_empty_regions(s: &GameSession) -> usize {
|
||||
let w = s.game.grid.width as i32;
|
||||
let h = s.game.grid.height as i32;
|
||||
let mut visited = vec![false; (w * h) as usize];
|
||||
let mut regions = 0;
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let idx = (y * w + x) as usize;
|
||||
if !visited[idx] && s.game.grid.get(x, y).is_empty() {
|
||||
regions += 1;
|
||||
let mut stack = vec![(x, y)];
|
||||
while let Some((cx, cy)) = stack.pop() {
|
||||
let i = (cy * w + cx) as usize;
|
||||
if visited[i]
|
||||
|| !s.game.grid.in_bounds(cx, cy)
|
||||
|| !s.game.grid.get(cx, cy).is_empty()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
visited[i] = true;
|
||||
for (dx, dy) in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
|
||||
stack.push((cx + dx, cy + dy));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
regions
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn surface_generation_has_stairs_and_open_spawn() {
|
||||
let s = init_at_depth(1);
|
||||
assert!(
|
||||
s.find_material("stairs").is_some(),
|
||||
"surface should have stairs"
|
||||
);
|
||||
assert!(
|
||||
player_not_in_solid(&s),
|
||||
"player should spawn in an open cell"
|
||||
);
|
||||
assert!(
|
||||
count_distinct_materials(&s) >= 3,
|
||||
"surface should have several materials"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cave_generation_has_stairs_and_connected_empty() {
|
||||
let s = init_at_depth(4);
|
||||
assert!(
|
||||
s.find_material("stairs").is_some(),
|
||||
"cave should have stairs"
|
||||
);
|
||||
assert!(
|
||||
player_not_in_solid(&s),
|
||||
"player should spawn in an open cell"
|
||||
);
|
||||
|
||||
let empty = total_empty(&s);
|
||||
assert!(
|
||||
empty > 100,
|
||||
"cave should have a meaningful empty region: {}",
|
||||
empty
|
||||
);
|
||||
|
||||
let (px, py) = s.game.player.center(&s.game.entities);
|
||||
let connected = flood_fill_count(&s, px as i32, py as i32);
|
||||
assert!(
|
||||
connected >= empty * 95 / 100,
|
||||
"cave should be mostly connected: {} of {}",
|
||||
connected,
|
||||
empty
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
count_empty_regions(&s),
|
||||
1,
|
||||
"cave should be a single connected empty region"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dungeon_generation_has_stairs_and_rooms() {
|
||||
let s = init_at_depth(7);
|
||||
assert!(
|
||||
s.find_material("stairs").is_some(),
|
||||
"dungeon should have stairs"
|
||||
);
|
||||
assert!(
|
||||
player_not_in_solid(&s),
|
||||
"player should spawn in an open cell"
|
||||
);
|
||||
assert!(
|
||||
count_distinct_materials(&s) >= 3,
|
||||
"dungeon should have several materials"
|
||||
);
|
||||
|
||||
let empty = total_empty(&s);
|
||||
assert!(
|
||||
empty > 500,
|
||||
"dungeon should have many room cells: {}",
|
||||
empty
|
||||
);
|
||||
|
||||
let regions = count_empty_regions(&s);
|
||||
assert!(
|
||||
regions >= 1 && regions <= 4,
|
||||
"dungeon rooms should be connected or nearly connected: {} regions",
|
||||
regions
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_depths_produce_different_structures() {
|
||||
let s1 = init_at_depth(1);
|
||||
let s2 = init_at_depth(4);
|
||||
let s3 = init_at_depth(7);
|
||||
|
||||
let empty1 = total_empty(&s1);
|
||||
let empty2 = total_empty(&s2);
|
||||
let empty3 = total_empty(&s3);
|
||||
|
||||
assert!(
|
||||
empty1 != empty2 || empty2 != empty3,
|
||||
"depths should differ in empty space: {} {} {}",
|
||||
empty1,
|
||||
empty2,
|
||||
empty3
|
||||
);
|
||||
|
||||
let grass1 = s1.count_material_in_region(
|
||||
0,
|
||||
0,
|
||||
s1.game.grid.width as i32,
|
||||
s1.game.grid.height as i32,
|
||||
"grass",
|
||||
);
|
||||
let grass2 = s2.count_material_in_region(
|
||||
0,
|
||||
0,
|
||||
s2.game.grid.width as i32,
|
||||
s2.game.grid.height as i32,
|
||||
"grass",
|
||||
);
|
||||
let grass3 = s3.count_material_in_region(
|
||||
0,
|
||||
0,
|
||||
s3.game.grid.width as i32,
|
||||
s3.game.grid.height as i32,
|
||||
"grass",
|
||||
);
|
||||
assert!(
|
||||
grass1 > grass2 && grass2 == grass3,
|
||||
"grass should dominate surface and vanish in deeper levels: {} {} {}",
|
||||
grass1,
|
||||
grass2,
|
||||
grass3
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dungeon_has_large_empty_rooms() {
|
||||
let s = init_at_depth(8);
|
||||
let mut found_room = false;
|
||||
for y in 5..s.game.grid.height as i32 - 5 {
|
||||
for x in 5..s.game.grid.width as i32 - 5 {
|
||||
let mut w = 0;
|
||||
while x + w < s.game.grid.width as i32 - 5 && s.game.grid.get(x + w, y).is_empty() {
|
||||
w += 1;
|
||||
}
|
||||
let mut h = 0;
|
||||
while y + h < s.game.grid.height as i32 - 5 && s.game.grid.get(x, y + h).is_empty() {
|
||||
h += 1;
|
||||
}
|
||||
if w >= 5 && h >= 5 {
|
||||
found_room = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(found_room, "dungeon should contain rooms at least 5x5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn world_generation_respects_seeds() {
|
||||
let s1 = init_at_depth(3);
|
||||
let s2 = init_at_depth(3);
|
||||
let (p1, _) = s1.game.player.center(&s1.game.entities);
|
||||
let (p2, _) = s2.game.player.center(&s2.game.entities);
|
||||
assert!(
|
||||
(p1 - p2).abs() < 0.01,
|
||||
"same seed should place player at the same x"
|
||||
);
|
||||
let m1 = s1.find_material("stairs");
|
||||
let m2 = s2.find_material("stairs");
|
||||
assert_eq!(m1, m2, "same seed should place stairs at the same location");
|
||||
}
|
||||
Reference in New Issue
Block a user