hex coords unit tests wip
Some checks failed
Rust / build (push) Failing after 4s

This commit is contained in:
2026-03-14 20:56:06 -04:00
parent 13a74f7620
commit 227b654e50
9 changed files with 18 additions and 14 deletions

View File

@@ -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); return HexCoord::new(x as i32 - (z as i32 / 2), z as i32);
} }
@@ -250,9 +250,9 @@ impl HexCoord
return (hex * -1).zxy(); 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()); return Self::from_axial(self.hex.xy() + s.xy());
} }

View File

@@ -3,5 +3,9 @@ use super::prelude::*;
#[test] #[test]
fn create_coord() 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));
}
} }

View File

@@ -18,7 +18,7 @@ pub fn generate_chunk_collider(chunk: &MeshChunkData) -> (Vec<Vec3>, Vec<[u32; 3
for x in 0..Chunk::SIZE for x in 0..Chunk::SIZE
{ {
let height = chunk.heights[x + z * 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 neighbors = chunk.get_neighbors(&coord);
let off_pos = Vec3::new(x as f32, height, z as f32); let off_pos = Vec3::new(x as f32, height, z as f32);
let tile_pos = offset3d_to_world(off_pos); let tile_pos = offset3d_to_world(off_pos);

View File

@@ -25,7 +25,7 @@ pub fn generate_chunk_mesh(chunk: &MeshChunkData) -> Mesh
let height = chunk.heights[idx]; let height = chunk.heights[idx];
let off_pos = Vec3::new(x as f32, height, z as f32); let off_pos = Vec3::new(x as f32, height, z as f32);
let tile_pos = offset3d_to_world(off_pos); 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); let n = chunk.get_neighbors(&coord);
create_tile( 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 off_pos = Vec3::new(x as f32, sealevel, z as f32);
let tile_pos = offset3d_to_world(off_pos); 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); let (n, neighbor_has_land) = chunk.get_neighbors_with_water_info(&coord);
create_tile_water_surface( create_tile_water_surface(

View File

@@ -19,7 +19,7 @@ pub fn generate_packed_chunk_mesh(chunk: &MeshChunkData) -> Mesh
{ {
let idx = x + z * Chunk::SIZE; let idx = x + z * Chunk::SIZE;
let height = chunk.heights[idx]; 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); let n = chunk.get_neighbors(&coord);
create_packed_tile( create_packed_tile(

View File

@@ -58,7 +58,7 @@ impl Map
{ {
for x in 0..Chunk::SIZE 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(); let index = coord.to_chunk_local_index();
if !self.is_in_bounds(&coord) if !self.is_in_bounds(&coord)

View File

@@ -64,7 +64,7 @@ pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer<image::Rgba<u8>, Vec<u8
pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer<image::Rgba<u8>, Vec<u8>>) pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer<image::Rgba<u8>, Vec<u8>>)
{ {
image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| { 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 right = coord.get_neighbor(1);
let height = map.sample_height(&coord); 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; let map_biome_count = map.biome_count as f32;
image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| { 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 biome_blend = biome_map.get_biome(x as i32, y as i32).unwrap();
let right = coord.get_neighbor(1); let right = coord.get_neighbor(1);
let mut color = Oklaba::BLACK; let mut color = Oklaba::BLACK;

View File

@@ -74,7 +74,7 @@ impl MeshChunkData
{ {
for x in 0..Chunk::SIZE 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 idx = coord.to_chunk_local_index();
let h = self.heights[idx]; let h = self.heights[idx];
self.distance_to_land[idx] = if h > self.sealevel { 0.0 } else { 4.0 }; self.distance_to_land[idx] = if h > self.sealevel { 0.0 } else { 4.0 };

View File

@@ -55,7 +55,7 @@ impl NavData
{ {
for x in 0..w 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 height = map.sample_height(&coord);
let tile = NavTile { let tile = NavTile {
coord, coord,
@@ -83,7 +83,7 @@ impl NavData
{ {
for x in 0..w 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 height = map.sample_height(&coord);
let tile = NavTile { let tile = NavTile {
coord, coord,