Basic proc gen with hash noise

This commit is contained in:
2025-03-01 15:19:53 -06:00
parent bdf4f7a7b6
commit 8c0601c7aa
6 changed files with 78 additions and 28 deletions

View File

@@ -12,7 +12,8 @@ WORLD_DATA_PATH :: "data/worlds"
World :: struct {
data_dir: string,
chunks: map[Vec2i]Chunk
chunks: map[Vec2i]Chunk,
seed: u32
}
Chunk :: struct #packed {
@@ -20,7 +21,7 @@ Chunk :: struct #packed {
tiles: [CHUNK_SIZE][CHUNK_SIZE]Tile,
}
create_world :: proc(name:string) -> World {
create_world :: proc(name:string, seed:u32) -> World {
data_dir := fmt.tprintf("%v/%v", WORLD_DATA_PATH, name)
if !os.is_dir(data_dir) {
fmt.printfln("Data dir: %v does not exist", data_dir)
@@ -34,11 +35,12 @@ create_world :: proc(name:string) -> World {
return World {
data_dir = data_dir,
chunks = make(map[Vec2i]Chunk),
chunks = make(map[Vec2i]Chunk),
seed = seed
}
}
load_world :: proc(name:string) -> World {
load_world :: proc(name:string, seed:u32) -> World {
dir := fmt.tprintf("%v/%v", WORLD_DATA_PATH, name)
if !os.is_dir(dir) {
panic("Couldnt load world")
@@ -47,6 +49,7 @@ load_world :: proc(name:string) -> World {
return World {
data_dir = dir,
chunks = make(map[Vec2i]Chunk),
seed = seed
}
}
@@ -97,7 +100,7 @@ load_chunk :: proc(pos:Vec2i, w:^World) -> Chunk {
data, err := os.read_entire_file_from_filename_or_err(filename)
if err != nil {
// fmt.printfln("No chunk %v found, generating new chunk", pos)
chunk := generate_chunk(pos)
chunk := generate_chunk(pos, w.seed)
save_chunk(&chunk, w)
return chunk
}
@@ -147,21 +150,43 @@ unload_chunk :: proc(pos:Vec2i, w:^World) {
}
}
generate_chunk :: proc(pos:Vec2i) -> Chunk {
generate_chunk :: proc(pos:Vec2i, seed:u32) -> Chunk {
chunk := Chunk {position = pos}
for x in 0..<CHUNK_SIZE {
for y in 0..<CHUNK_SIZE {
chunk.tiles[x][y] = nothing_tile
world_x := pos.x * CHUNK_SIZE + x
world_y := pos.y * CHUNK_SIZE + y
chunk.tiles[x][y] = generate_tile(world_x, world_y, seed)
}
}
center_pos := Vec2i{CHUNK_SIZE/2, CHUNK_SIZE/2}
set_chunk_tile(&chunk, tree_tile, center_pos)
return chunk
}
generate_tile :: proc(x, y: int, seed: u32) -> Tile {
base_noise := hash_noise(x, y, seed)
cluster_noise := hash_noise(x / 3, y / 3, seed + 12345) // Larger scale noise for clusters
if base_noise < 0.70 {
return nothing_tile
} else if base_noise < 0.85 {
return grass_tile
} else if base_noise < 0.95 {
if cluster_noise > 0.5 { // Favor trees in cluster regions
return tree_tile
}
return grass_tile
} else {
if cluster_noise > 0.4 { // Only allow ponds in certain areas
return water_tile
}
return nothing_tile
}
}
get_chunk :: proc(w:^World, chunk_pos:Vec2i) -> ^Chunk {
chunk, exists := w.chunks[chunk_pos]
if !exists {