From 1c72255e09a47e7344a1addc0ef363a05fb4f608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 12 May 2021 16:02:12 +0200 Subject: [PATCH] small worldgen reorg --- code/common/CMakeLists.txt | 2 + code/common/world/world.c | 30 +++---------- code/common/world/world.h | 17 ++++++++ code/common/world/worldgen/worldgen.h | 4 ++ .../{world_gen.c => worldgen/worldgen_test.c} | 43 +++++++++++-------- 5 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 code/common/world/worldgen/worldgen.h rename code/common/world/{world_gen.c => worldgen/worldgen_test.c} (70%) diff --git a/code/common/CMakeLists.txt b/code/common/CMakeLists.txt index 5a97693..f7201c9 100644 --- a/code/common/CMakeLists.txt +++ b/code/common/CMakeLists.txt @@ -3,6 +3,8 @@ set(SRCS ${SRCS} world/blocks.c world/perlin.c world/world.c + + world/worldgen/worldgen_test.c ) include_directories(../modules . world) add_library(eco2d-common STATIC ${SRCS}) \ No newline at end of file diff --git a/code/common/world/world.c b/code/common/world/world.c index 33e5791..30c5907 100644 --- a/code/common/world/world.c +++ b/code/common/world/world.c @@ -5,31 +5,13 @@ #include "modules/physics.h" #include "world/world.h" #include "entity_view.h" +#include "worldgen/worldgen.h" #include "platform.h" #include "packets/pkt_send_librg_update.h" -typedef struct { - uint8_t *data; - uint32_t seed; - uint32_t size; - uint16_t block_size; - uint16_t chunk_size; - uint16_t chunk_amount; - uint16_t dim; - uint64_t tracker_update[3]; - uint8_t active_layer_id; - ecs_world_t *ecs; - ecs_query_t *ecs_update; - librg_world *tracker; - world_pkt_reader_proc *reader_proc; - world_pkt_writer_proc *writer_proc; -} world_data; - static world_data world = {0}; -int32_t world_gen(); - entity_view world_build_entity_view(int64_t e) { ECS_IMPORT(world_ecs(), General); ECS_IMPORT(world_ecs(), Physics); @@ -76,8 +58,8 @@ int32_t tracker_write_create(librg_world *w, librg_event *e) { } int32_t tracker_write_remove(librg_world *w, librg_event *e) { - zpl_unused(e); - zpl_unused(w); + (void)e; + (void)w; #ifdef WORLD_LAYERING if (world.active_layer_id != WORLD_TRACKER_LAYERS-1) { // NOTE(zaklaus): reject updates from smaller layers @@ -153,7 +135,7 @@ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint1 ECS_IMPORT(world.ecs, Net); world.ecs_update = ecs_query_new(world.ecs, "net.ClientInfo, general.Position"); - int32_t world_build_status = world_gen(); + int32_t world_build_status = worldgen_test(&world); ZPL_ASSERT(world_build_status >= 0); for (int i = 0; i < world.chunk_amount * world.chunk_amount; ++i) { @@ -211,7 +193,7 @@ static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius) zpl_printf("[error] an error happened writing the world %d\n", result); } - pkt_send_librg_update((void*)p[i].peer, p[i].view_id, ticker, buffer, datalen); + pkt_send_librg_update((uint64_t)p[i].peer, p[i].view_id, ticker, buffer, datalen); } } } @@ -271,5 +253,3 @@ uint16_t world_chunk_amount(void) { uint16_t world_dim(void) { return world.block_size * world.chunk_size * world.chunk_amount; } - -#include "world_gen.c" diff --git a/code/common/world/world.h b/code/common/world/world.h index 8c60a84..fd7ed94 100644 --- a/code/common/world/world.h +++ b/code/common/world/world.h @@ -23,6 +23,23 @@ typedef WORLD_PKT_READER(world_pkt_reader_proc); #define WORLD_PKT_WRITER(name) int32_t name(pkt_header *pkt, void *udata) typedef WORLD_PKT_WRITER(world_pkt_writer_proc); +typedef struct { + uint8_t *data; + uint32_t seed; + uint32_t size; + uint16_t block_size; + uint16_t chunk_size; + uint16_t chunk_amount; + uint16_t dim; + uint64_t tracker_update[3]; + uint8_t active_layer_id; + ecs_world_t *ecs; + ecs_query_t *ecs_update; + librg_world *tracker; + world_pkt_reader_proc *reader_proc; + world_pkt_writer_proc *writer_proc; +} world_data; + void world_setup_pkt_handlers(world_pkt_reader_proc *reader_proc, world_pkt_writer_proc *writer_proc); int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount); int32_t world_destroy(void); diff --git a/code/common/world/worldgen/worldgen.h b/code/common/world/worldgen/worldgen.h new file mode 100644 index 0000000..384452e --- /dev/null +++ b/code/common/world/worldgen/worldgen.h @@ -0,0 +1,4 @@ +#pragma once +#include "system.h" + +int32_t worldgen_test(void *world); diff --git a/code/common/world/world_gen.c b/code/common/world/worldgen/worldgen_test.c similarity index 70% rename from code/common/world/world_gen.c rename to code/common/world/worldgen/worldgen_test.c index 659c4e3..ae10211 100644 --- a/code/common/world/world_gen.c +++ b/code/common/world/worldgen/worldgen_test.c @@ -13,12 +13,14 @@ typedef WORLD_BLOCK_OBSERVER(world_block_observer_proc); #define WORLD_PERLIN_FREQ 1.0 #define WORLD_PERLIN_OCTAVES 1 +static world_data *world; + static void world_fill_rect(uint8_t id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) { for (uint32_t cy=y; cy= world.dim) continue; - if (cy < 0 || cy >= world.dim) continue; - uint32_t i = (cy*world.dim) + cx; + if (cx < 0 || cx >= world->dim) continue; + if (cy < 0 || cy >= world->dim) continue; + uint32_t i = (cy*world->dim) + cx; if (proc) { uint8_t new_id = (*proc)(id, i); @@ -28,7 +30,7 @@ static void world_fill_rect(uint8_t id, uint32_t x, uint32_t y, uint32_t w, uint else continue; } - world.data[i] = id; + world->data[i] = id; } } } @@ -36,9 +38,9 @@ static void world_fill_rect(uint8_t id, uint32_t x, uint32_t y, uint32_t w, uint static void world_fill_circle(uint8_t id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) { for (uint32_t cy=y; cy= world.dim) continue; - if (cy < 0 || cy >= world.dim) continue; - uint32_t i = (cy*world.dim) + cx; + if (cx < 0 || cx >= world->dim) continue; + if (cy < 0 || cy >= world->dim) continue; + uint32_t i = (cy*world->dim) + cx; if (proc) { uint8_t new_id = (*proc)(id, i); @@ -48,7 +50,7 @@ static void world_fill_circle(uint8_t id, uint32_t x, uint32_t y, uint32_t w, ui else continue; } - world.data[i] = id; + world->data[i] = id; } } } @@ -62,8 +64,8 @@ static void world_fill_rect_anchor(uint8_t id, uint32_t x, uint32_t y, uint32_t static WORLD_BLOCK_OBSERVER(shaper) { uint32_t biome = blocks_get_biome(id); uint32_t kind = blocks_get_kind(id); - uint32_t old_biome = blocks_get_biome(world.data[block_idx]); - uint32_t old_kind = blocks_get_kind(world.data[block_idx]); + uint32_t old_biome = blocks_get_biome(world->data[block_idx]); + uint32_t old_kind = blocks_get_kind(world->data[block_idx]); if (biome == old_biome) { if (kind == BLOCK_KIND_WALL && kind == old_kind) { @@ -78,10 +80,10 @@ static WORLD_BLOCK_OBSERVER(shaper) { } static uint8_t world_perlin_cond(uint32_t block_idx, double chance) { - uint32_t x = block_idx % world.dim; - uint32_t y = block_idx / world.dim; + uint32_t x = block_idx % world->dim; + uint32_t y = block_idx / world->dim; - return perlin_fbm(world.seed, x, y, WORLD_PERLIN_FREQ, WORLD_PERLIN_OCTAVES) < chance; + return perlin_fbm(world->seed, x, y, WORLD_PERLIN_FREQ, WORLD_PERLIN_OCTAVES) < chance; } static WORLD_BLOCK_OBSERVER(shaper_noise80) { @@ -104,31 +106,34 @@ static void world_fill_mountain(uint32_t x, uint32_t y) { #define RAND_RANGE(x,y) (x + (int)rand()%(y-(x))) -int32_t world_gen() { +int32_t worldgen_test(world_data *wld) { + // TODO(zaklaus): pass world as an arg instead + world = wld; + // TODO: perform world gen // atm, we will fill the world with ground and surround it by walls uint8_t wall_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WALL); uint8_t grnd_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_GROUND); uint8_t watr_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER); - srand(world.seed); + srand(world->seed); // walls - world_fill_rect(wall_id, 0, 0, world.dim, world.dim, NULL); + world_fill_rect(wall_id, 0, 0, world->dim, world->dim, NULL); // ground - world_fill_rect(grnd_id, 1, 1, world.dim-2, world.dim-2, NULL); + world_fill_rect(grnd_id, 1, 1, world->dim-2, world->dim-2, NULL); // water for (int i=0; idim), RAND_RANGE(0, world->dim), 4+RAND_RANGE(0,3), 4+RAND_RANGE(0,3), 0.5f, 0.5f, shaper_noise33); } const uint32_t HILLS_SIZE = 21; // hills for (int i=0; idim), RAND_RANGE(0, world->dim), RAND_RANGE(0,HILLS_SIZE), RAND_RANGE(0,HILLS_SIZE), 0.5f, 0.5f, shaper_noise33); } return WORLD_ERROR_NONE;