use crate::quad_mesh::QuadMesh;
use crate::{GameHandler, World};
pub const SIMULATION_BATTLEFIELD_WIDTH: usize = 192;
pub const SIMULATION_BATTLEFIELD_HEIGHT: usize = 108;
pub const SIMULATION_TEXT_LAYERS: usize = 3;
pub const SIMULATION_BATTLEFIELD_DEPTH: usize = 4;
pub fn default_simulation_maze() -> &'static str {
concat!(
" \n",
" \n",
" \n",
" \n",
" #################################################################################################### \n",
" ## # \n",
" # # \n",
" # ## #### ## # \n",
" # ###### ##################### ###### ### # \n",
" # ###### ###################### ######## ##### # \n",
" # #### #################### ########## ##### # \n",
" # ### ##### #### ### #### # \n",
" # ### #### #### ### #### # \n",
" # ### ### ### #### ### #### # \n",
" # #### ### ###### #### ### #### # ################################################## \n",
" # ### #### ### ############## ### ### # # # \n",
" # ### ##### ### ##### ######## ### ## ## # # \n",
" # ### ##### ### #### #### ### # ##### # # \n",
" # ### ###### #### ### ### ###### # # \n",
" # ### ###### ### ### ### ####### # # \n",
" # ### ###### #### # ### ######## # ###### ####### ############### # \n",
" # ### ##### ###### # ######## # # # # # # # \n",
" # ### ##### ##### ##### #### ### # # # # # # \n",
" # ### ##### ##### ######### ### # # # # # # # \n",
" # #### ##### ##### ########### #### # # # # # \n",
" # ### # ##### ##### ############# #### # # # # # \n",
" # # ######## ##### ############# ## # # # # # \n",
" # ######## ##### #### ### # # # # # \n",
" # ######## # ##### ### ## # # # # # # # \n",
" # ######## ### ##### ## # ##### # # # # # # # \n",
" # ######## # # ### # # ######### # # # # # # # \n",
" # ### #### ### # ########## # # # # # # # # \n",
" # ### ### ##### ####### # ###### # # # # # # # # \n",
" ###### # ### ### ###### #### # # # # # # # # # # # \n",
" ####### ### # ### ### ####### ### # # # # # # # # # # # \n",
" ######## #### ### #### ### ####### ######## #### # # # # # # # # # # # # \n",
" # #### #### ### ### #### ###### ########## ### ### # # # # # # # # # # # \n",
" # ### ### # #### #### #### # ########## ### ### # # # # # # # # # # # \n",
" ### ### #### #### ## ### #### #### #### # # # # # # # # # # # \n",
" ### #### ### ##### #### ### ### ### # # # # # # # # # # # \n",
" ### ### # #### # #### #### #### ### # # # # # # # # # # # \n",
" ### #### # ### ## ### #### ### # # # # # # # # # # # \n",
" #### ## ## # # ### #### ### # # # # # # # # # # # \n",
" ### ### ### ### #### ### # # # # # # # # # # # \n",
" # ### # ### ## ### # # # # # # # # # # \n",
" #### # ### ### # # # ###### # # # # # # \n",
" ## ## ### ### ### # # # # # # # # # # \n",
" #### #### # # ### #### # # # # # # # # # # \n",
" ######### ### ### ### # # # # # # # # # # \n",
" ######## ######## # # ## ### ### # # # # # # # # # # \n",
" ###### ########### ### ### ### ## # # # # # # # # # # # \n",
" # ##### ####### # # ### #### # # # # # # # # # # \n",
" # ##### ##### ### ### # # # # # # # # # # \n",
" # #### #### # # ### ### # # # # # # # # # # \n",
" ##### #### # ## ## ### ### # # # ###### # # # ####### # \n",
" #### ##### ### ##### ### #### ### # # # # # # # # # # # \n",
" ### ### ####### # ####### # ### ## ### # # # # # # # # # # # \n",
" # #### ######### ###### ### #### ### # # # # # # # # # # # \n",
" # # ###### ################### # ##### ### # # # # # # # # # # # \n",
" # ### ########## ###### ##### #### # # # # # # # # # # # \n",
" # #### ###### # #### ######## ### # # # # # # # # # # # \n",
" # ### #### ### ########### ### # # # # # # # # # # # \n",
" # ### #### # ### # ################### ### # # # # # # # # # # \n",
" # ### ### ### # ### ### ################ ### # # # # # # # # # # \n",
" # ### ### ### ### ###### # ######## ### # # # # # # # # # # \n",
" # ### ### #### # ##### ## ### # # # # # # # # # # \n",
" # ### #### #### ### ### ### # # # # # # # # # # \n",
" # #### #### # #### ## ### # # # # # # # # # # # \n",
" # #### ## # ### ### #### # # # # # # # # # # # \n",
" # #### ## # #### ##### ###### # # # # # # # # # # # \n",
" # #### # # ##################### # # # # # # # # # # # \n",
" # # ### ##################### # # # # # # # # # # \n",
" # ### # ##### ####### # # # # # # # # # # \n",
" # #### ### # # # # # # # # # # \n",
" # ###### ### #### # # # # # # # # # # \n",
" # ######## ### # #### ## # # # # # # # # # # \n",
" # # ###### # ### # ##### # # # # # # # # # # # \n",
" # ### # # # # ####### # # # ###### # # # # # \n",
" # ## ### ### ## #### ####### # # # # # # # # # \n",
" # ### ### # ## ####### ####### # # # # # # # # # \n",
" # # #### ############# ##### # ###### # # # # # # \n",
" # ### ########### ### # # # # # ######### # \n",
" # #### # ########## # # # # # # \n",
" # #### ### ####### # # # # # # \n",
" # ### # ####### # # # # # # \n",
" # #### ######## # # # # # # \n",
" # #### ################ # ###### # # # # \n",
" # ### ## ################## # # ############# ##################### \n",
" # #### ######## ############# ###### # # # \n",
" # ## ######################## # # \n",
" # ######################## # # \n",
" # ################ ##### # # \n",
" # ############### #### # # # \n",
" # ######## ### #### ### # ################################################## \n",
" # ####### ### ###### # # \n",
" # ###### ### ##### # \n",
" # ## ### ### # \n",
" # ### # # \n",
" # ### # \n",
" # # # \n",
" # # \n",
" # # \n",
" ## ## \n",
" #################################################################################################### \n",
" \n",
" \n",
" \n",
" \n",
"------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ################## ########################### ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # ###### ####### ############### # ##############\n",
"################################################################################################################# # # # # # # # ##############\n",
"################################################################################################################# # # # # # # # ##############\n",
"################################################################################################################# # # # # # # # ##############\n",
"################################################################################################################# # # # # # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"############################################################################################################################### # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"############################################################################################################################### # # # ####### # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"################################################################################################# #################### # # # # # # ##############\n",
"########################################################################################################################## # # # # # # ##############\n",
"########################################################################################################################## # # # # # # # ##############\n",
"############################################################################################################################### # # # # # ##############\n",
"########################################################################################################################## # # # # # ##############\n",
"########################################################################################################################## # # # # # ##############\n",
"########################################################################################################################## # # # # # ##############\n",
"########################################################################################################################## # # # ######### # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"########################################################################################################################## # # # # ##############\n",
"############################################################################################################################### # # # # ##############\n",
"########################################################################################################################## ############# ##################### ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# # # ##############\n",
"################################################################################################################# ######### ########################### ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################# ##############\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################# ##############################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"################################################################################################################################################################################################\n",
"------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",
)
}
pub fn create_default_simulation() -> GameHandler<World> {
let maze = default_simulation_maze();
let gradient_mesh = QuadMesh::from_text(maze)
.expect("Default simulation maze exceeds dimension limits");
let world = World::new(gradient_mesh);
GameHandler::new(world)
}
#[cfg(feature = "image")]
pub fn create_simulation_from_paths<P: AsRef<std::path::Path>>(
paths: &[P],
) -> Result<GameHandler<World>, crate::quad_mesh::QuadMeshError> {
let gradient_mesh = QuadMesh::from_paths(paths)?;
let world = World::new(gradient_mesh);
Ok(GameHandler::new(world))
}
#[cfg(feature = "image")]
pub fn create_simulation_from_images(
images: &[image::DynamicImage],
) -> Result<GameHandler<World>, crate::quad_mesh::QuadMeshError> {
let gradient_mesh = QuadMesh::from_images(images)?;
let world = World::new(gradient_mesh);
Ok(GameHandler::new(world))
}
#[cfg(test)]
mod tests {
use super::*;
fn is_sep_line(line: &str) -> bool {
if line.is_empty() {
return false;
}
let mut has_sep = false;
for c in line.chars() {
if c == '-' || c == '_' || c == '=' {
has_sep = true;
} else if c != ' ' {
return false;
}
}
has_sep
}
#[test]
fn test_maze_dimensions() {
let maze = default_simulation_maze();
let mut layers: Vec<Vec<&str>> = Vec::new();
let mut current: Vec<&str> = Vec::new();
for line in maze.lines() {
if is_sep_line(line) {
if !current.is_empty() {
layers.push(current);
current = Vec::new();
}
} else {
current.push(line);
}
}
if !current.is_empty() {
layers.push(current);
}
assert_eq!(
layers.len(),
SIMULATION_TEXT_LAYERS,
"Maze text should have {} layers",
SIMULATION_TEXT_LAYERS
);
for (layer_idx, layer) in layers.iter().enumerate() {
assert_eq!(
layer.len(),
SIMULATION_BATTLEFIELD_HEIGHT,
"Layer {} should have {} rows, got {}",
layer_idx,
SIMULATION_BATTLEFIELD_HEIGHT,
layer.len()
);
for (row_idx, line) in layer.iter().enumerate() {
assert_eq!(
line.len(),
SIMULATION_BATTLEFIELD_WIDTH,
"Layer {} row {} should have {} columns, got {}",
layer_idx,
row_idx,
SIMULATION_BATTLEFIELD_WIDTH,
line.len()
);
}
}
}
#[test]
fn test_create_default_simulation() {
let handler = create_default_simulation();
let world = handler.state();
assert!(world.place_count() > 0, "World should have places");
let mesh = world.mesh();
let cell_count = mesh.cell_count();
let total_volume = mesh.total_volume();
let walkable_coords = mesh.walkable_coords();
let (width, height, depth) = mesh.dimensions();
println!(
"Compaction: {}x{}x{}={} coords, {} walkable, {} cells (ratio: {:.1}x)",
width,
height,
depth,
total_volume,
walkable_coords,
cell_count,
walkable_coords as f32 / cell_count as f32
);
assert_eq!(depth, SIMULATION_BATTLEFIELD_DEPTH, "Mesh should have {} layers", SIMULATION_BATTLEFIELD_DEPTH);
assert!(
cell_count < total_volume,
"Mesh should have fewer cells than total coordinate space"
);
assert!(cell_count > 0, "Mesh should have some cells");
}
#[test]
fn test_maze_has_content() {
let maze = default_simulation_maze();
let wall_count = maze.chars().filter(|&c| c == '#').count();
let floor_count = maze.chars().filter(|&c| c == ' ').count();
assert!(wall_count > 1000, "Maze should have walls");
assert!(floor_count > 1000, "Maze should have walkable areas");
}
#[test]
#[cfg(feature = "image")]
fn test_create_simulation_from_images() {
let base_path = env!("CARGO_MANIFEST_DIR");
let paths = vec![
format!("{}/../../media/simulation-0.png", base_path),
format!("{}/../../media/simulation-1.png", base_path),
format!("{}/../../media/simulation-2.png", base_path),
];
let handler = create_simulation_from_paths(&paths).expect("Failed to load images");
let world = handler.state();
assert!(world.place_count() > 0, "World should have places");
let mesh = world.mesh();
let (width, height, depth) = mesh.dimensions();
assert_eq!(width, SIMULATION_BATTLEFIELD_WIDTH, "Width should match");
assert_eq!(height, SIMULATION_BATTLEFIELD_HEIGHT, "Height should match");
assert_eq!(depth, SIMULATION_BATTLEFIELD_DEPTH, "Depth should match (3 layers + 1 ground)");
let walkable_coords = mesh.walkable_coords();
println!(
"Image-based simulation: {}x{}x{}, {} walkable coords, {} cells",
width,
height,
depth,
walkable_coords,
mesh.cell_count()
);
assert!(walkable_coords > 1000, "Should have walkable area");
}
#[test]
#[cfg(feature = "image")]
fn test_generate_ascii_from_images() {
use shortestpath::mesh_source::{repr_source_3d, Source3D, Source3DFromImage};
let base_path = env!("CARGO_MANIFEST_DIR");
let paths = vec![
format!("{}/../../media/simulation-0.png", base_path),
format!("{}/../../media/simulation-1.png", base_path),
format!("{}/../../media/simulation-2.png", base_path),
];
let source = Source3DFromImage::from_paths(&paths, 0.5).unwrap();
let repr = repr_source_3d(&source);
std::fs::write("/tmp/simulation_ascii.txt", &repr).unwrap();
println!("ASCII art written to /tmp/simulation_ascii.txt");
println!("Dimensions: {}x{}x{}", source.width(), source.height(), source.depth());
let lines: Vec<&str> = repr.lines().collect();
let layer_sep_count = lines.iter().filter(|l| l.starts_with("----")).count();
assert_eq!(layer_sep_count, 3, "Should have 3 layer separators");
}
}