Route cabin paths using trail network

This commit is contained in:
chelsea
2025-12-02 21:56:51 -06:00
parent f79c8a0dfa
commit 39a44adcfa
2 changed files with 37 additions and 4 deletions

Binary file not shown.

View File

@@ -1947,6 +1947,32 @@ static int find_nearest_trail_block(column_data columns[CHUNK_SIZE][CHUNK_SIZE],
return 0;
}
static int find_nearest_trail_point_from_segments(worldgen_ctx *ctx, int start_x, int start_z, int *out_x, int *out_z) {
if (!ctx || ctx->trail_segment_count == 0) return 0;
double best = DBL_MAX;
int bx = 0, bz = 0;
for (size_t i = 0; i < ctx->trail_segment_count; ++i) {
trail_segment *seg = &ctx->trail_segments[i];
if (!seg || seg->count < 1 || !seg->points) continue;
for (int p = 0; p < seg->count; ++p) {
int tx = seg->points[p * 2];
int tz = seg->points[p * 2 + 1];
double dx = (double)(tx - start_x);
double dz = (double)(tz - start_z);
double d2 = dx * dx + dz * dz;
if (d2 < best) {
best = d2;
bx = tx;
bz = tz;
}
}
}
if (best == DBL_MAX) return 0;
*out_x = bx;
*out_z = bz;
return 1;
}
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) {
@@ -1954,13 +1980,20 @@ static void connect_cabin_to_trail(worldgen_ctx *ctx, int chunk_x, int chunk_z,
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 spur_len = 20;
int spur_len = 24;
if (path_width < 2) path_width = 2;
carve_cabin_path(ctx, chunk_x, chunk_z, chunk, columns, start_x, start_z, step_x, step_z, spur_len, path_width);
int target_x = 0, target_z = 0;
if (!find_nearest_trail_block(columns, chunk, chunk_x, chunk_z, start_x, start_z, 96, &target_x, &target_z)) {
int fallback_x = start_x + step_x * 64;
int fallback_z = start_z + step_z * 64;
int found = 0;
if (ctx && ctx->enable_trails) {
found = find_nearest_trail_point_from_segments(ctx, start_x, start_z, &target_x, &target_z);
}
if (!found) {
found = find_nearest_trail_block(columns, chunk, chunk_x, chunk_z, start_x, start_z, 128, &target_x, &target_z);
}
if (!found) {
int fallback_x = start_x + step_x * 80;
int fallback_z = start_z + step_z * 80;
carve_trail_span(ctx, chunk_x, chunk_z, chunk, columns, start_x, start_z, fallback_x, fallback_z, path_width);
return;
}