Tighten cabin placement and add ladders/foundations

This commit is contained in:
chelsea
2025-12-02 21:29:53 -06:00
parent c8ad0a06e8
commit 678583a4ed
4 changed files with 199 additions and 48 deletions

View File

@@ -132,9 +132,13 @@ static void generate_chunk_flowers(worldgen_ctx *ctx, int chunk_x, int chunk_z,
static void generate_chunk_cabins(worldgen_ctx *ctx, int chunk_x, int chunk_z, column_data columns[CHUNK_SIZE][CHUNK_SIZE], chunk_data *out);
static void trail_node_position(worldgen_ctx *ctx, int node_x, int node_z, double spacing, double *out_x, double *out_z);
static void carve_trail_pad(worldgen_ctx *ctx, int chunk_x, int chunk_z, chunk_data *out, column_data columns[CHUNK_SIZE][CHUNK_SIZE], int center_x, int center_z, int radius, int target_height);
static void carve_trail_span(worldgen_ctx *ctx, int chunk_x, int chunk_z, chunk_data *out, column_data columns[CHUNK_SIZE][CHUNK_SIZE], int x0, int z0, int x1, int z1, int width);
static double old_growth_plains_mask(worldgen_ctx *ctx, int x, int z);
static double old_growth_grove_mask(worldgen_ctx *ctx, int x, int z);
static uint16_t generate_normal_ores(worldgen_ctx *ctx, int x, int y, int z, const column_data *col);
static void connect_cabin_to_trail(worldgen_ctx *ctx, int chunk_x, int chunk_z,
column_data columns[CHUNK_SIZE][CHUNK_SIZE], chunk_data *chunk,
int door_x, int door_z, int door_side, int path_width);
static uint32_t hash_coords(int x, int z, uint32_t seed) {
uint32_t h = (uint32_t)(x * 374761393 + z * 668265263) ^ seed;
@@ -1373,11 +1377,14 @@ typedef struct {
uint16_t stair_e_block;
uint16_t stair_w_block;
uint16_t window_block;
uint16_t foundation_block;
double log_decay;
double plank_decay;
double window_chance;
double door_decay;
int door_rect_index;
int has_ladder;
int path_width;
} cabin_blueprint;
static const cabin_rect CABIN_RECT_SMALL[] = {
@@ -1392,11 +1399,6 @@ static const cabin_rect CABIN_RECT_TWO_STORY[] = {
{0, 0, 3, 3}
};
static const cabin_rect CABIN_RECT_L_SHAPE[] = {
{0, 0, 3, 3},
{3, -2, 2, 2}
};
static const cabin_blueprint CABIN_BLUEPRINTS[] = {
{
CABIN_RECT_SMALL,
@@ -1411,11 +1413,14 @@ static const cabin_blueprint CABIN_BLUEPRINTS[] = {
BLOCK_SPRUCE_STAIRS_E,
BLOCK_SPRUCE_STAIRS_W,
BLOCK_GLASS_PANE,
BLOCK_STONE,
0.04,
0.25,
0.45,
0.35,
0
0.08,
0,
0,
2
},
{
CABIN_RECT_LONG,
@@ -1430,11 +1435,14 @@ static const cabin_blueprint CABIN_BLUEPRINTS[] = {
BLOCK_SPRUCE_STAIRS_E,
BLOCK_SPRUCE_STAIRS_W,
BLOCK_GLASS_PANE,
BLOCK_STONE,
0.05,
0.3,
0.4,
0.4,
0
0.1,
0,
0,
3
},
{
CABIN_RECT_TWO_STORY,
@@ -1449,30 +1457,14 @@ static const cabin_blueprint CABIN_BLUEPRINTS[] = {
BLOCK_SPRUCE_STAIRS_E,
BLOCK_SPRUCE_STAIRS_W,
BLOCK_GLASS_PANE,
BLOCK_STONE,
0.04,
0.22,
0.5,
0.3,
0
},
{
CABIN_RECT_L_SHAPE,
sizeof(CABIN_RECT_L_SHAPE) / sizeof(CABIN_RECT_L_SHAPE[0]),
3,
0.08,
0,
1,
BLOCK_OAK_PLANKS,
BLOCK_OAK_PLANKS,
BLOCK_OAK_LOG_X,
BLOCK_OAK_LOG_Z,
BLOCK_OAK_LOG,
BLOCK_SPRUCE_STAIRS_E,
BLOCK_SPRUCE_STAIRS_W,
BLOCK_GLASS_PANE,
0.04,
0.28,
0.45,
0.4,
0
2
}
};
@@ -1491,6 +1483,31 @@ static void update_columns_for_rect(column_data columns[CHUNK_SIZE][CHUNK_SIZE],
}
}
static int rect_overlaps_mask(unsigned char occupancy[CHUNK_SIZE][CHUNK_SIZE], int chunk_origin_x, int chunk_origin_z,
int x0, int x1, int z0, int z1, int padding) {
for (int wz = z0 - padding; wz <= z1 + padding; ++wz) {
for (int wx = x0 - padding; wx <= x1 + padding; ++wx) {
int lx = wx - chunk_origin_x;
int lz = wz - chunk_origin_z;
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE) continue;
if (occupancy[lx][lz]) return 1;
}
}
return 0;
}
static void mark_rect_occupancy(unsigned char occupancy[CHUNK_SIZE][CHUNK_SIZE], int chunk_origin_x, int chunk_origin_z,
int x0, int x1, int z0, int z1, int padding) {
for (int wz = z0 - padding; wz <= z1 + padding; ++wz) {
for (int wx = x0 - padding; wx <= x1 + padding; ++wx) {
int lx = wx - chunk_origin_x;
int lz = wz - chunk_origin_z;
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE) continue;
occupancy[lx][lz] = 1;
}
}
}
static void build_cabin_roof(const cabin_blueprint *bp, int chunk_x, int chunk_z, chunk_data *chunk,
int rect_center_x, int rect_center_z, int half_w, int half_l,
int roof_base_y, rng_state *rng) {
@@ -1533,11 +1550,15 @@ static void build_cabin_roof(const cabin_blueprint *bp, int chunk_x, int chunk_z
}
static void place_wall_window(chunk_data *chunk, int chunk_origin_x, int chunk_origin_z,
int wx, int wz, int window_y, uint16_t window_block) {
int wx, int wz, int window_y, uint16_t window_block, int height) {
int lx = wx - chunk_origin_x;
int lz = wz - chunk_origin_z;
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE) return;
set_block_with_height(chunk, lx, lz, window_y, window_block);
for (int h = 0; h < height; ++h) {
int y = window_y + h;
if (y >= CHUNK_HEIGHT) break;
set_block_with_height(chunk, lx, lz, y, window_block);
}
}
static void build_cabin_rect(worldgen_ctx *ctx, const cabin_blueprint *bp, const cabin_rect *rect,
@@ -1564,11 +1585,27 @@ static void build_cabin_rect(worldgen_ctx *ctx, const cabin_blueprint *bp, const
}
}
uint16_t foundation_block = bp->foundation_block ? bp->foundation_block : BLOCK_STONE;
for (int wz = z0; wz <= z1; ++wz) {
for (int wx = x0; wx <= x1; ++wx) {
int lx = wx - chunk_origin_x;
int lz = wz - chunk_origin_z;
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE) continue;
int column_height = columns[lx][lz].height;
if (column_height < base_floor_y) {
for (int y = column_height + 1; y <= base_floor_y; ++y) {
set_block_with_height(chunk, lx, lz, y, foundation_block);
}
columns[lx][lz].height = base_floor_y;
}
}
}
for (int story = 0; story < bp->stories; ++story) {
int story_floor = base_floor_y + story * bp->wall_height;
if (story_floor >= CHUNK_HEIGHT - 4) break;
for (int wz = z0; wz <= z1; ++wz) {
for (int wx = x0; wx <= x1; ++wx) {
for (int wz = z0 + 1; wz <= z1 - 1; ++wz) {
for (int wx = x0 + 1; wx <= x1 - 1; ++wx) {
int lx = wx - chunk_origin_x;
int lz = wz - chunk_origin_z;
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE) continue;
@@ -1616,24 +1653,37 @@ static void build_cabin_rect(worldgen_ctx *ctx, const cabin_blueprint *bp, const
if (bp->window_block && bp->window_chance > 0.0) {
int window_y = story_floor + 2;
for (int wx = x0 + 1; wx <= x1 - 1; wx += 2) {
int window_height = (bp->wall_height >= 3) ? 2 : 1;
int min_win_x = x0 + 2;
int max_win_x = x1 - 2;
if (min_win_x > max_win_x) {
min_win_x = x0 + 1;
max_win_x = x1 - 1;
}
int min_win_z = z0 + 2;
int max_win_z = z1 - 2;
if (min_win_z > max_win_z) {
min_win_z = z0 + 1;
max_win_z = z1 - 1;
}
for (int wx = min_win_x; wx <= max_win_x; wx += 2) {
if (story_has_door && door_side == 0 && wx == door_x) continue;
if (rng_next_f64(rng) < bp->window_chance) {
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, wx, z0, window_y, bp->window_block);
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, wx, z0, window_y, bp->window_block, window_height);
}
if (story_has_door && door_side == 1 && wx == door_x) continue;
if (rng_next_f64(rng) < bp->window_chance) {
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, wx, z1, window_y, bp->window_block);
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, wx, z1, window_y, bp->window_block, window_height);
}
}
for (int wz = z0 + 1; wz <= z1 - 1; wz += 2) {
for (int wz = min_win_z; wz <= max_win_z; wz += 2) {
if (story_has_door && door_side == 2 && wz == door_z) continue;
if (rng_next_f64(rng) < bp->window_chance) {
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, x0, wz, window_y, bp->window_block);
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, x0, wz, window_y, bp->window_block, window_height);
}
if (story_has_door && door_side == 3 && wz == door_z) continue;
if (rng_next_f64(rng) < bp->window_chance) {
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, x1, wz, window_y, bp->window_block);
place_wall_window(chunk, chunk_origin_x, chunk_origin_z, x1, wz, window_y, bp->window_block, window_height);
}
}
}
@@ -1656,6 +1706,29 @@ static void build_cabin_rect(worldgen_ctx *ctx, const cabin_blueprint *bp, const
}
}
}
if (bp->has_ladder && story < bp->stories - 1) {
int ladder_x = rect_center_x;
int ladder_z = z0 + 1;
uint16_t ladder_block = BLOCK_LADDER_S;
switch ((story + door_side) % 4) {
case 0: ladder_x = rect_center_x; ladder_z = z0 + 1; ladder_block = BLOCK_LADDER_S; break;
case 1: ladder_x = rect_center_x; ladder_z = z1 - 1; ladder_block = BLOCK_LADDER_N; break;
case 2: ladder_x = x0 + 1; ladder_z = rect_center_z; ladder_block = BLOCK_LADDER_E; break;
case 3: ladder_x = x1 - 1; ladder_z = rect_center_z; ladder_block = BLOCK_LADDER_W; break;
}
int lx = ladder_x - chunk_origin_x;
int lz = ladder_z - chunk_origin_z;
if (lx >= 0 && lx < CHUNK_SIZE && lz >= 0 && lz < CHUNK_SIZE) {
for (int y = story_floor + 1; y <= story_floor + bp->wall_height; ++y) {
set_block_with_height(chunk, lx, lz, y, ladder_block);
}
int next_floor = story_floor + bp->wall_height;
if (next_floor < CHUNK_HEIGHT) {
set_block_with_height(chunk, lx, lz, next_floor, BLOCK_AIR);
}
}
}
}
int roof_base = base_floor_y + bp->wall_height * bp->stories;
@@ -1677,6 +1750,7 @@ static void build_cabin_rect(worldgen_ctx *ctx, const cabin_blueprint *bp, const
}
static int try_place_cabin(worldgen_ctx *ctx, int chunk_x, int chunk_z, chunk_data *chunk, column_data columns[CHUNK_SIZE][CHUNK_SIZE],
unsigned char occupancy[CHUNK_SIZE][CHUNK_SIZE],
int center_x, int center_z, const cabin_blueprint *bp, rng_state *rng) {
int chunk_origin_x = chunk_x * CHUNK_SIZE;
int chunk_origin_z = chunk_z * CHUNK_SIZE;
@@ -1705,6 +1779,9 @@ static int try_place_cabin(worldgen_ctx *ctx, int chunk_x, int chunk_z, chunk_da
rect_x1[i] = x1;
rect_z0[i] = z0;
rect_z1[i] = z1;
if (rect_overlaps_mask(occupancy, chunk_origin_x, chunk_origin_z, x0, x1, z0, z1, 3)) {
return 0;
}
for (int wz = z0; wz <= z1; ++wz) {
for (int wx = x0; wx <= x1; ++wx) {
int lx = wx - chunk_origin_x;
@@ -1734,19 +1811,35 @@ static int try_place_cabin(worldgen_ctx *ctx, int chunk_x, int chunk_z, chunk_da
door_z = rect_center_z[door_rect_index];
}
int door_offset = rng_range_inclusive(rng, -1, 1);
if (door_side == 0 || door_side == 1) {
door_x = clamp_int(door_x + door_offset, rect_x0[door_rect_index] + 1, rect_x1[door_rect_index] - 1);
} else {
door_z = clamp_int(door_z + door_offset, rect_z0[door_rect_index] + 1, rect_z1[door_rect_index] - 1);
}
int door_margin = 2;
if (door_side == 0 || door_side == 1) {
int min_door_x = rect_x0[door_rect_index] + door_margin;
int max_door_x = rect_x1[door_rect_index] - door_margin;
if (min_door_x > max_door_x) {
min_door_x = rect_x0[door_rect_index] + 1;
max_door_x = rect_x1[door_rect_index] - 1;
}
door_x = clamp_int(door_x + door_offset, min_door_x, max_door_x);
} else {
int min_door_z = rect_z0[door_rect_index] + door_margin;
int max_door_z = rect_z1[door_rect_index] - door_margin;
if (min_door_z > max_door_z) {
min_door_z = rect_z0[door_rect_index] + 1;
max_door_z = rect_z1[door_rect_index] - 1;
}
door_z = clamp_int(door_z + door_offset, min_door_z, max_door_z);
}
for (size_t i = 0; i < bp->rect_count; ++i) {
int has_door = (int)i == door_rect_index;
build_cabin_rect(ctx, bp, &bp->rects[i], chunk_x, chunk_z, chunk, columns,
rect_center_x[i], rect_center_z[i], base_floor_y,
door_x, door_z, door_side, has_door, rng);
mark_rect_occupancy(occupancy, chunk_origin_x, chunk_origin_z, rect_x0[i], rect_x1[i], rect_z0[i], rect_z1[i], 3);
}
connect_cabin_to_trail(ctx, chunk_x, chunk_z, columns, chunk, door_x, door_z, door_side, bp->path_width);
return 1;
}
@@ -1758,6 +1851,8 @@ static void generate_chunk_cabins(worldgen_ctx *ctx, int chunk_x, int chunk_z, c
double spawn_chance = 0.015 + noise * 0.02;
if (rng_next_f64(&rng) > spawn_chance) return;
size_t blueprint_count = sizeof(CABIN_BLUEPRINTS) / sizeof(CABIN_BLUEPRINTS[0]);
unsigned char occupancy[CHUNK_SIZE][CHUNK_SIZE];
memset(occupancy, 0, sizeof(occupancy));
int attempts = 1 + (rng_next_f64(&rng) < 0.2 ? 1 : 0);
while (attempts-- > 0) {
const cabin_blueprint *bp = &CABIN_BLUEPRINTS[rng_range_inclusive(&rng, 0, (int)blueprint_count - 1)];
@@ -1781,12 +1876,56 @@ static void generate_chunk_cabins(worldgen_ctx *ctx, int chunk_x, int chunk_z, c
int local_cz = rng_range_inclusive(&rng, min_local_z, max_local_z);
int world_cx = chunk_x * CHUNK_SIZE + local_cx;
int world_cz = chunk_z * CHUNK_SIZE + local_cz;
if (try_place_cabin(ctx, chunk_x, chunk_z, out, columns, world_cx, world_cz, bp, &rng)) {
if (try_place_cabin(ctx, chunk_x, chunk_z, out, columns, occupancy, world_cx, world_cz, bp, &rng)) {
break;
}
}
}
static int find_nearest_trail_block(column_data columns[CHUNK_SIZE][CHUNK_SIZE], chunk_data *chunk, int chunk_x, int chunk_z,
int start_x, int start_z, int search_radius, int *out_x, int *out_z) {
int chunk_origin_x = chunk_x * CHUNK_SIZE;
int chunk_origin_z = chunk_z * CHUNK_SIZE;
int local_start_x = start_x - chunk_origin_x;
int local_start_z = start_z - chunk_origin_z;
if (local_start_x < 0 || local_start_x >= CHUNK_SIZE || local_start_z < 0 || local_start_z >= CHUNK_SIZE) return 0;
for (int radius = 2; radius <= search_radius; ++radius) {
for (int dz = -radius; dz <= radius; ++dz) {
for (int dx = -radius; dx <= radius; ++dx) {
if (abs(dx) != radius && abs(dz) != radius) continue;
int world_x = start_x + dx;
int world_z = start_z + dz;
int lx = world_x - chunk_origin_x;
int lz = world_z - chunk_origin_z;
if (lx < 0 || lx >= CHUNK_SIZE || lz < 0 || lz >= CHUNK_SIZE) continue;
int column_height = columns[lx][lz].height;
if (column_height < 0 || column_height >= CHUNK_HEIGHT) continue;
uint16_t block = chunk->blocks[column_height][lx][lz];
if (block == BLOCK_GRAVEL) {
*out_x = world_x;
*out_z = world_z;
return 1;
}
}
}
}
return 0;
}
static void connect_cabin_to_trail(worldgen_ctx *ctx, int chunk_x, int chunk_z,
column_data columns[CHUNK_SIZE][CHUNK_SIZE], chunk_data *chunk,
int door_x, int door_z, int door_side, int path_width) {
int step_x = (door_side == 2) ? -1 : (door_side == 3) ? 1 : 0;
int step_z = (door_side == 0) ? -1 : (door_side == 1) ? 1 : 0;
int start_x = door_x + step_x;
int start_z = door_z + step_z;
int target_x = 0, target_z = 0;
if (!find_nearest_trail_block(columns, chunk, chunk_x, chunk_z, start_x, start_z, 24, &target_x, &target_z)) {
return;
}
carve_trail_span(ctx, chunk_x, chunk_z, chunk, columns, start_x, start_z, target_x, target_z, path_width > 0 ? path_width : 2);
}
static uint16_t generate_normal_ores(worldgen_ctx *ctx, int x, int y, int z, const column_data *col) {
uint32_t seed = (uint32_t)ctx->world_seed;
double cluster;
@@ -1885,6 +2024,7 @@ static int column_has_manmade_surface(chunk_data *chunk, int chunk_x, int chunk_
block == BLOCK_OAK_LOG_X || block == BLOCK_OAK_LOG_Z ||
block == BLOCK_SPRUCE_LOG_X || block == BLOCK_SPRUCE_LOG_Z ||
block == BLOCK_GLASS_PANE || block == BLOCK_SPRUCE_STAIRS_E || block == BLOCK_SPRUCE_STAIRS_W ||
block == BLOCK_LADDER_N || block == BLOCK_LADDER_S || block == BLOCK_LADDER_E || block == BLOCK_LADDER_W ||
block == BLOCK_SPRUCE_DOOR_N_LOWER || block == BLOCK_SPRUCE_DOOR_N_UPPER ||
block == BLOCK_SPRUCE_DOOR_S_LOWER || block == BLOCK_SPRUCE_DOOR_S_UPPER ||
block == BLOCK_SPRUCE_DOOR_E_LOWER || block == BLOCK_SPRUCE_DOOR_E_UPPER ||