From 034d7d3df700818d43b82bccd3bbb4babb2686e5 Mon Sep 17 00:00:00 2001 From: citrons Date: Thu, 12 Oct 2023 17:30:53 -0500 Subject: improve world generation --- generator.c | 62 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'generator.c') diff --git a/generator.c b/generator.c index 74848d0..43f898f 100644 --- a/generator.c +++ b/generator.c @@ -3,52 +3,54 @@ #include "world.h" #include "random.h" -#define MAX_INFLUENCE 10 +#define FULLNESS 15.0 #define SPARSITY 40.0 -#define CAVERNITY 3 +#define CAVERNITY 20.0 #define SURFACE_COLLECTIBLE_RARITY 70 +static void fill_circle(Uint8 tiles[], int x, int y, float radius, tile t) { + int boundary = SDL_fabs(radius) + 0.5; + int min_x = SDL_max(x - boundary, 0); + int max_x = SDL_min(x + boundary, CHUNK_DIM - 1); + int min_y = SDL_max(y - boundary, 0); + int max_y = SDL_min(y + boundary, CHUNK_DIM - 1); + for (int iy = min_y; iy <= max_y; iy++) { + for (int ix = min_x; ix <= max_x; ix++) { + int dx = ix - x; + int dy = iy - y; + if (dx * dx + dy * dy <= radius * radius) { + if (dx != 0 || dy != 0 || radius > 0.5) { + tiles[tile_index((SDL_Point) {ix, iy})] = t; + } + } + } + } +} + void generate_chunk(world *w, chunk *c) { SDL_memset(c->tiles, TILE_EMPTY, sizeof(c->tiles)); if (c->pos.y < 0) return; static float noise_params[CHUNK_DIM * CHUNK_DIM]; - SDL_memset(noise_params, 0, sizeof(noise_params)); for (int i = 0; i < CHUNK_DIM * CHUNK_DIM; i++) { - if (rand_int() % CAVERNITY == 0) { - noise_params[i] = - SDL_pow(rand_float(), SPARSITY) * MAX_INFLUENCE; + float radius = SDL_pow(rand_float(), SPARSITY); + if (rand_int() % 4 == 0) radius = -radius; + radius *= (radius > 0) ? FULLNESS : CAVERNITY; + if (c->pos.y == 0) { + int y = i / CHUNK_DIM; + if (y < 96) radius = 0; + else if (radius > 0) radius *= 1.8; } + noise_params[i] = radius; } for (int y = 0; y < CHUNK_DIM; y++) { for (int x = 0; x < CHUNK_DIM; x++) { - float influence = noise_params[tile_index((SDL_Point) {x, y})]; - if (c->pos.y == 0) { - if (y < 96) influence = 0; - else influence *= 1.8; - } - - int boundary = influence + 0.5; - int min_x = SDL_max(x - boundary, 0); - int max_x = SDL_min(x + boundary, CHUNK_DIM - 1); - int min_y = SDL_max(y - boundary, 0); - int max_y = SDL_min(y + boundary, CHUNK_DIM - 1); - - for (int iy = min_y; iy <= max_y; iy++) { - for (int ix = min_x; ix <= max_x; ix++) { - int dx = ix - x; - int dy = iy - y; - if (dx * dx + dy * dy <= influence * influence) { - if (dx != 0 || dy != 0 || influence > 0.5) { - c->tiles[ - tile_index((SDL_Point) {ix, iy})] = TILE_WALL; - } - } - } - } + float radius = noise_params[tile_index((SDL_Point) {x, y})]; + tile t = radius > 0 ? TILE_WALL : TILE_EMPTY; + fill_circle(c->tiles, x, y, SDL_abs(radius), t); } } -- cgit v1.2.3