From 227b654e50dce5c4b114c428e8f71d5cde547214 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sat, 14 Mar 2026 20:56:06 -0400 Subject: [PATCH] hex coords unit tests wip --- engine/hex/src/hex_coord.rs | 6 +++--- engine/hex/src/tests.rs | 6 +++++- engine/world_generation/src/generators/chunk_colliders.rs | 2 +- engine/world_generation/src/generators/mesh_generator.rs | 4 ++-- .../src/generators/packed_mesh_generator.rs | 2 +- engine/world_generation/src/map/map.rs | 2 +- engine/world_generation/src/map/map_utils.rs | 4 ++-- engine/world_generation/src/map/mesh_chunk.rs | 2 +- game/units/src/nav_data.rs | 4 ++-- 9 files changed, 18 insertions(+), 14 deletions(-) diff --git a/engine/hex/src/hex_coord.rs b/engine/hex/src/hex_coord.rs index 8f50a4e..0098be5 100644 --- a/engine/hex/src/hex_coord.rs +++ b/engine/hex/src/hex_coord.rs @@ -103,7 +103,7 @@ impl HexCoord }; } - pub fn from_grid_pos(x: usize, z: usize) -> Self + pub fn from_offset_pos(x: usize, z: usize) -> Self { return HexCoord::new(x as i32 - (z as i32 / 2), z as i32); } @@ -250,9 +250,9 @@ impl HexCoord return (hex * -1).zxy(); } - pub fn scale(&self, dir: i32, radius: usize) -> HexCoord + pub fn scale(&self, dir: usize, radius: usize) -> HexCoord { - let s = Self::DIRECTIONS[(dir % 6) as usize] * radius as i32; + let s = Self::DIRECTIONS[dir % 6] * radius as i32; return Self::from_axial(self.hex.xy() + s.xy()); } diff --git a/engine/hex/src/tests.rs b/engine/hex/src/tests.rs index 41e10f6..f25defe 100644 --- a/engine/hex/src/tests.rs +++ b/engine/hex/src/tests.rs @@ -3,5 +3,9 @@ use super::prelude::*; #[test] fn create_coord() { - let coord = HexCoord::from_grid_pos(3, 3); + let center = HexCoord::from_offset_pos(3, 3); + for dir in 0..6 + { + assert_eq!(center.get_neighbor(dir).get_neighbor(dir), center.scale(dir, 2)); + } } diff --git a/engine/world_generation/src/generators/chunk_colliders.rs b/engine/world_generation/src/generators/chunk_colliders.rs index a8220eb..6c3d62a 100644 --- a/engine/world_generation/src/generators/chunk_colliders.rs +++ b/engine/world_generation/src/generators/chunk_colliders.rs @@ -18,7 +18,7 @@ pub fn generate_chunk_collider(chunk: &MeshChunkData) -> (Vec, Vec<[u32; 3 for x in 0..Chunk::SIZE { let height = chunk.heights[x + z * Chunk::SIZE]; - let coord = HexCoord::from_grid_pos(x, z); + let coord = HexCoord::from_offset_pos(x, z); let neighbors = chunk.get_neighbors(&coord); let off_pos = Vec3::new(x as f32, height, z as f32); let tile_pos = offset3d_to_world(off_pos); diff --git a/engine/world_generation/src/generators/mesh_generator.rs b/engine/world_generation/src/generators/mesh_generator.rs index 321545c..2f2fb8f 100644 --- a/engine/world_generation/src/generators/mesh_generator.rs +++ b/engine/world_generation/src/generators/mesh_generator.rs @@ -25,7 +25,7 @@ pub fn generate_chunk_mesh(chunk: &MeshChunkData) -> Mesh let height = chunk.heights[idx]; let off_pos = Vec3::new(x as f32, height, z as f32); let tile_pos = offset3d_to_world(off_pos); - let coord = HexCoord::from_grid_pos(x, z); + let coord = HexCoord::from_offset_pos(x, z); let n = chunk.get_neighbors(&coord); create_tile( @@ -121,7 +121,7 @@ pub fn generate_chunk_water_mesh(chunk: &MeshChunkData, sealevel: f32, map_width } let off_pos = Vec3::new(x as f32, sealevel, z as f32); let tile_pos = offset3d_to_world(off_pos); - let coord = HexCoord::from_grid_pos(x, z); + let coord = HexCoord::from_offset_pos(x, z); let (n, neighbor_has_land) = chunk.get_neighbors_with_water_info(&coord); create_tile_water_surface( diff --git a/engine/world_generation/src/generators/packed_mesh_generator.rs b/engine/world_generation/src/generators/packed_mesh_generator.rs index 476752c..1590bce 100644 --- a/engine/world_generation/src/generators/packed_mesh_generator.rs +++ b/engine/world_generation/src/generators/packed_mesh_generator.rs @@ -19,7 +19,7 @@ pub fn generate_packed_chunk_mesh(chunk: &MeshChunkData) -> Mesh { let idx = x + z * Chunk::SIZE; let height = chunk.heights[idx]; - let coord = HexCoord::from_grid_pos(x, z); + let coord = HexCoord::from_offset_pos(x, z); let n = chunk.get_neighbors(&coord); create_packed_tile( diff --git a/engine/world_generation/src/map/map.rs b/engine/world_generation/src/map/map.rs index bf31138..f72856f 100644 --- a/engine/world_generation/src/map/map.rs +++ b/engine/world_generation/src/map/map.rs @@ -58,7 +58,7 @@ impl Map { for x in 0..Chunk::SIZE { - let coord = HexCoord::from_grid_pos(x + cx, z + cz); + let coord = HexCoord::from_offset_pos(x + cx, z + cz); let index = coord.to_chunk_local_index(); if !self.is_in_bounds(&coord) diff --git a/engine/world_generation/src/map/map_utils.rs b/engine/world_generation/src/map/map_utils.rs index a9808b2..b300bec 100644 --- a/engine/world_generation/src/map/map_utils.rs +++ b/engine/world_generation/src/map/map_utils.rs @@ -64,7 +64,7 @@ pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer, Vec, Vec>) { image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| { - let coord = HexCoord::from_grid_pos(x as usize, y as usize); + let coord = HexCoord::from_offset_pos(x as usize, y as usize); let right = coord.get_neighbor(1); let height = map.sample_height(&coord); @@ -155,7 +155,7 @@ pub fn update_biome_map(map: &Map, biome_map: &BiomeMap, image: &mut ImageBuffer { let map_biome_count = map.biome_count as f32; image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| { - let coord = HexCoord::from_grid_pos(x as usize, y as usize); + let coord = HexCoord::from_offset_pos(x as usize, y as usize); let biome_blend = biome_map.get_biome(x as i32, y as i32).unwrap(); let right = coord.get_neighbor(1); let mut color = Oklaba::BLACK; diff --git a/engine/world_generation/src/map/mesh_chunk.rs b/engine/world_generation/src/map/mesh_chunk.rs index a5df6af..7d30d9f 100644 --- a/engine/world_generation/src/map/mesh_chunk.rs +++ b/engine/world_generation/src/map/mesh_chunk.rs @@ -74,7 +74,7 @@ impl MeshChunkData { for x in 0..Chunk::SIZE { - let coord = HexCoord::from_grid_pos(x + offset_x, z + offset_z); + let coord = HexCoord::from_offset_pos(x + offset_x, z + offset_z); let idx = coord.to_chunk_local_index(); let h = self.heights[idx]; self.distance_to_land[idx] = if h > self.sealevel { 0.0 } else { 4.0 }; diff --git a/game/units/src/nav_data.rs b/game/units/src/nav_data.rs index dbb2507..d744251 100644 --- a/game/units/src/nav_data.rs +++ b/game/units/src/nav_data.rs @@ -55,7 +55,7 @@ impl NavData { for x in 0..w { - let coord = HexCoord::from_grid_pos(x, y); + let coord = HexCoord::from_offset_pos(x, y); let height = map.sample_height(&coord); let tile = NavTile { coord, @@ -83,7 +83,7 @@ impl NavData { for x in 0..w { - let coord = HexCoord::from_grid_pos(x, y); + let coord = HexCoord::from_offset_pos(x, y); let height = map.sample_height(&coord); let tile = NavTile { coord,