From bdb04aa2b08acf4cea7169f840b41b0e5c45a027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 11 Jan 2021 17:20:12 +0100 Subject: [PATCH] worldgen basic demo --- code/apps/server/CMakeLists.txt | 3 ++ code/apps/server/header/blocks.h | 12 +++---- code/apps/server/header/blocks_info.h | 18 ++++++++++ code/apps/server/header/world_gen.h | 3 ++ code/apps/server/source/blocks.c | 52 ++++++++++++--------------- code/apps/server/source/blocks_list.c | 7 ++++ code/apps/server/source/options.c | 19 +++++----- code/apps/server/source/world.c | 12 ++----- code/apps/server/source/world_gen.c | 31 ++++++++++++++++ data/blocks.csv | 3 -- 10 files changed, 103 insertions(+), 57 deletions(-) create mode 100644 code/apps/server/header/blocks_info.h create mode 100644 code/apps/server/header/world_gen.h create mode 100644 code/apps/server/source/blocks_list.c create mode 100644 code/apps/server/source/world_gen.c delete mode 100644 data/blocks.csv diff --git a/code/apps/server/CMakeLists.txt b/code/apps/server/CMakeLists.txt index d002ee7..9476d75 100644 --- a/code/apps/server/CMakeLists.txt +++ b/code/apps/server/CMakeLists.txt @@ -4,13 +4,16 @@ add_executable(eco2d-server source/perlin.c source/options.c source/world.c + source/world_gen.c source/blocks.c header/network.h header/perlin.h header/options.h header/world.h + header/world_gen.h header/blocks.h + header/blocks_info.h ) include_directories(eco2d-server header) diff --git a/code/apps/server/header/blocks.h b/code/apps/server/header/blocks.h index cf18ff4..c9b7730 100644 --- a/code/apps/server/header/blocks.h +++ b/code/apps/server/header/blocks.h @@ -1,18 +1,18 @@ #pragma once #include "system.h" -#define BLOCKS_ERROR_NONE +0x0000 -#define BLOCKS_ERROR_OUTOFMEM -0x0001 -#define BLOCKS_ERROR_NOTFOUND -0x0002 -#define BLOCKS_ERROR_INVALID -0x0003 +#define BLOCK_INVALID 0xF typedef enum { BLOCK_FLAG_COLLISION = (1 << 0) } block_flags; -int32_t blocks_init(void); -int32_t blocks_destroy(void); +#include "blocks_info.h" + +uint8_t blocks_find(uint32_t biome, uint32_t kind); // persisting buffer char *blocks_get_name(uint8_t id); +uint8_t blocks_get_tex_id(uint8_t id); +char blocks_get_symbol(uint8_t id); uint32_t blocks_get_flags(uint8_t id); diff --git a/code/apps/server/header/blocks_info.h b/code/apps/server/header/blocks_info.h new file mode 100644 index 0000000..547b70c --- /dev/null +++ b/code/apps/server/header/blocks_info.h @@ -0,0 +1,18 @@ +#pragma once + +typedef enum { + BLOCK_KIND_DEV, + BLOCK_KIND_GROUND, + BLOCK_KIND_WATER, + BLOCK_KIND_WALL, + BLOCK_KIND_HOLE, +} block_kind; + +typedef enum { + BLOCK_BIOME_DEV, + BLOCK_BIOME_PLAIN, + BLOCK_BIOME_FOREST, + BLOCK_BIOME_DESERT, + BLOCK_BIOME_ICE, + BLOCK_BIOME_OCEAN, +} block_biome; diff --git a/code/apps/server/header/world_gen.h b/code/apps/server/header/world_gen.h new file mode 100644 index 0000000..6c0d59a --- /dev/null +++ b/code/apps/server/header/world_gen.h @@ -0,0 +1,3 @@ +#pragma once + +int32_t world_gen(uint8_t *world, uint32_t size, uint32_t width, uint32_t height, int32_t seed); diff --git a/code/apps/server/source/blocks.c b/code/apps/server/source/blocks.c index f2ac048..221abaa 100644 --- a/code/apps/server/source/blocks.c +++ b/code/apps/server/source/blocks.c @@ -4,47 +4,39 @@ // todo: csv parsing + utils #define BLOCK_NAMELEN 80 +#define BLOCKS_COUNT (sizeof(blocks)/sizeof(block)) typedef struct { - uint8_t id; + uint8_t tex_id; char name[BLOCK_NAMELEN]; uint32_t flags; + uint32_t kind; + uint32_t biome; + char symbol; } block; -static block *blocks = NULL; -static uint32_t blocks_count = 0; +#include "blocks_list.c" -int blocks_comparer(void const *a, void const *b) { - block *ba = (block*)a; - uint8_t bb = *(uint8_t*)b; - return ba->id < bb ? -1 : ba->id > bb; -} - -static block *blocks_find(uint8_t id) { - ZPL_ASSERT_NOT_NULL(blocks); - int32_t index = zpl_binary_search((void*)blocks, blocks_count, sizeof(block), (void*)&id, blocks_comparer); - ZPL_ASSERT_MSG(index != -1, "block could not be found"); - return &blocks[index]; -} - -int32_t blocks_init(void) { - // todo read csv by lines, linecount-1 == blocks_count - // preallocate and assign values - return BLOCKS_ERROR_NONE; -} - -int32_t blocks_destroy(void) { - ZPL_ASSERT_NOT_NULL(blocks); - zpl_mfree(blocks); - return BLOCKS_ERROR_NONE; +uint8_t blocks_find(uint32_t biome, uint32_t kind) { + for (int i=0; iname; + return blocks[id].name; +} + +uint8_t blocks_get_tex_id(uint8_t id) { + return blocks[id].tex_id; +} + +char blocks_get_symbol(uint8_t id) { + return blocks[id].symbol; } uint32_t blocks_get_flags(uint8_t id) { - ZPL_ASSERT_NOT_NULL(blocks); - return blocks_find(id)->flags; + return blocks[id].flags; } diff --git a/code/apps/server/source/blocks_list.c b/code/apps/server/source/blocks_list.c new file mode 100644 index 0000000..2786a1d --- /dev/null +++ b/code/apps/server/source/blocks_list.c @@ -0,0 +1,7 @@ +#include "blocks.h" + +static block blocks[] = { + {.tex_id = 0, .name = "base-ground", .flags = 0, .kind = BLOCK_KIND_GROUND, .biome = 0, .symbol = '.'}, + {.tex_id = 1, .name = "base-wall", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WALL, .biome = 0, .symbol = '#'}, + {.tex_id = 2, .name = "base-water", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WATER, .biome = 0, .symbol = '~'}, +}; diff --git a/code/apps/server/source/options.c b/code/apps/server/source/options.c index a75e0b6..3f0a5d9 100644 --- a/code/apps/server/source/options.c +++ b/code/apps/server/source/options.c @@ -1,18 +1,19 @@ #include "options.h" -#include "perlin.h" +#include "world.h" +#include "blocks.h" #include "zpl.h" #define TEST_MAP_DIM 32 -#define TEST_MAP_DEPTH 18 - -static char *map_pattern = "~~..,,oo---OO^^^@@"; void generate_minimap(int32_t seed) { - for (uint32_t y=0; y 0 && i % TEST_MAP_DIM == 0) { + zpl_printf("\n"); } - zpl_printf("\n"); + zpl_printf("%c", blocks_get_symbol(world[i])); } + world_destroy(); } diff --git a/code/apps/server/source/world.c b/code/apps/server/source/world.c index cae2687..2f6deba 100644 --- a/code/apps/server/source/world.c +++ b/code/apps/server/source/world.c @@ -4,8 +4,7 @@ static uint8_t *world = NULL; static uint32_t world_size = 0; static uint32_t world_width = 0; - -static int32_t world_gen(int32_t seed); +static uint32_t world_height = 0; int32_t world_init(int32_t seed, uint8_t width, uint8_t height) { if (world) { @@ -13,12 +12,13 @@ int32_t world_init(int32_t seed, uint8_t width, uint8_t height) { } world_size = width*height; world_width = width; + world_height = height; world = zpl_malloc(sizeof(uint8_t)*world_size); if (!world) { return WORLD_ERROR_OUTOFMEM; } - return world_gen(seed); + return world_gen(world, world_size, world_width, world_height, seed); } int32_t world_destroy(void) { @@ -34,9 +34,3 @@ uint32_t world_buf(uint8_t const **ptr, uint32_t *width) { if (width) *width = world_width; return world_size; } - -static int32_t world_gen(int32_t seed) { - // TODO: perform world gen - - return WORLD_ERROR_NONE; -} diff --git a/code/apps/server/source/world_gen.c b/code/apps/server/source/world_gen.c new file mode 100644 index 0000000..92e3140 --- /dev/null +++ b/code/apps/server/source/world_gen.c @@ -0,0 +1,31 @@ +#include "world.h" +#include "blocks.h" +#include "zpl.h" + +static void world_fill(uint8_t *world, uint32_t width, uint32_t id, uint32_t x, uint32_t y, uint32_t w, uint32_t h) { + for (uint32_t cy=y; cy