From cf173f3d50dd745c3414183049e8d4c67268d538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 8 Nov 2021 16:54:41 +0100 Subject: [PATCH] general improvements and cleanup --- .gitignore | 1 + code/game/src/debug_ui_widgets.c | 5 +- code/game/src/platform_raylib.c | 4 +- code/game/src/world/world.c | 135 ++++++++++++++++++------------- run_client_remote.bat | 2 +- run_server.bat | 4 +- 6 files changed, 85 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index e0229ff..068ad2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build build_rel +screenshots build.bat run.bat clean.bat diff --git a/code/game/src/debug_ui_widgets.c b/code/game/src/debug_ui_widgets.c index a5b1429..934df6e 100644 --- a/code/game/src/debug_ui_widgets.c +++ b/code/game/src/debug_ui_widgets.c @@ -120,10 +120,7 @@ DrawNetworkStats(debug_item *it, float xpos, float ypos) { #define _kb(x) ((x) / 1024) debug_draw_result r; - r = DrawFormattedText(xpos, ypos, TextFormat("dn total: %d kb", _kb(s.incoming_total))); - r = DrawFormattedText(xpos, r.y, TextFormat("recv total: %lld kb", _kb(s.total_received))); - - r = DrawFormattedText(xpos, r.y, TextFormat("up total: %lld kb", _kb(s.outgoing_total))); + r = DrawFormattedText(xpos, ypos, TextFormat("recv total: %lld kb", _kb(s.total_received))); r = DrawFormattedText(xpos, r.y, TextFormat("sent total: %lld kb", _kb(s.total_sent))); r = DrawFormattedText(xpos, r.y, TextFormat("dn rate: %.02f kb/sec (%.02f kbit/sec)", _kb(s.incoming_bandwidth), _kb(s.incoming_bandwidth * 8.0f))); diff --git a/code/game/src/platform_raylib.c b/code/game/src/platform_raylib.c index ca3d663..2b95b5a 100644 --- a/code/game/src/platform_raylib.c +++ b/code/game/src/platform_raylib.c @@ -37,6 +37,7 @@ void platform_init() { renderer_init(); } +inline static void display_conn_status() { if (game_is_networked()) { if (network_client_is_connected()) { @@ -60,6 +61,7 @@ uint8_t platform_is_running() { static game_keystate_data last_input_data = {0}; +inline static void platform_input_update_input_frame(game_keystate_data data) { // NOTE(zaklaus): Test if there are any changes if (data.x != last_input_data.x) goto send_data; @@ -110,7 +112,7 @@ void platform_input() { mouse_pos.y -= 0.5f; mouse_pos = Vector2Normalize(mouse_pos); - if (IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) { + if (game_get_kind() == GAMEKIND_SINGLE && IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) { x = mouse_pos.x; y = -mouse_pos.y; } diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index ee61bdf..a72a990 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -142,27 +142,41 @@ void world_setup_pkt_handlers(world_pkt_reader_proc *reader_proc, world_pkt_writ world.writer_proc = writer_proc; } -int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) { - if (world.data) { - return 0; +static inline +world_chunk_setup_grid(void) { + for (int i = 0; i < zpl_square(world.chunk_amount); ++i) { + ecs_entity_t e = ecs_new(world.ecs, 0); + ecs_set(world.ecs, e, Classify, {.id = EKIND_CHUNK }); + Chunk *chunk = ecs_get_mut(world.ecs, e, Chunk, NULL); + librg_entity_track(world.tracker, e); + librg_entity_chunk_set(world.tracker, e, i); + librg_chunk_to_chunkpos(world.tracker, i, &chunk->x, &chunk->y, NULL); + world.chunk_mapping[i] = e; + world.block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(world.chunk_size)); + world.outer_block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(world.chunk_size)); + chunk->id = i; + chunk->is_dirty = false; + + for (int y = 0; y < world.chunk_size; y += 1) { + for (int x = 0; x < world.chunk_size; x += 1) { + int chk_x = chunk->x * world.chunk_size; + int chk_y = chunk->y * world.chunk_size; + + block_id *c = &world.block_mapping[i][(y*world.chunk_size)+x]; + *c = world.data[(chk_y+y)*world.dim + (chk_x+x)]; + + c = &world.outer_block_mapping[i][(y*world.chunk_size)+x]; + *c = world.outer_data[(chk_y+y)*world.dim + (chk_x+x)]; + } + } } +} + +static inline +void world_configure_tracker(void) { + world.tracker = librg_world_create(); - world.is_paused = false; - world.seed = seed; - world.chunk_size = chunk_size; - world.chunk_amount = chunk_amount; - - world.dim = (world.chunk_size * world.chunk_amount); - world.size = world.dim * world.dim; - - if (world.tracker == NULL) { - world.tracker = librg_world_create(); - } - - if (world.tracker == NULL) { - zpl_printf("[ERROR] An error occurred while trying to create a server world.\n"); - return WORLD_ERROR_TRACKER_FAILED; - } + ZPL_ASSERT_MSG(world.tracker, "[ERROR] An error occurred while trying to create a server world."); /* config our world grid */ librg_config_chunksize_set(world.tracker, WORLD_BLOCK_SIZE * world.chunk_size, WORLD_BLOCK_SIZE * world.chunk_size, 0); @@ -172,69 +186,74 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) { librg_event_set(world.tracker, LIBRG_WRITE_CREATE, tracker_write_create); librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove); librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, tracker_write_update); - +} + +static inline +void world_init_worldgen_data(void) { world.data = zpl_malloc(sizeof(block_id)*world.size); world.outer_data = zpl_malloc(sizeof(block_id)*world.size); - if (!world.data || !world.outer_data) { - return WORLD_ERROR_OUTOFMEM; - } - + ZPL_ASSERT(world.data && world.outer_data); +} + +static inline +void world_setup_ecs(void) { world.ecs = ecs_init(); ECS_IMPORT(world.ecs, Components); ECS_IMPORT(world.ecs, Systems); world.ecs_update = ecs_query_new(world.ecs, "components.ClientInfo, components.Position"); - world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t)*zpl_square(chunk_amount)); - world.block_mapping = zpl_malloc(sizeof(block_id*)*zpl_square(chunk_amount)); - world.outer_block_mapping = zpl_malloc(sizeof(block_id*)*zpl_square(chunk_amount)); +} + +static inline +void world_init_mapping(void) { + world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t)*zpl_square(world.chunk_amount)); + world.block_mapping = zpl_malloc(sizeof(block_id*)*zpl_square(world.chunk_amount)); + world.outer_block_mapping = zpl_malloc(sizeof(block_id*)*zpl_square(world.chunk_amount)); world_snapshot_init(&streamer_snapshot, zpl_heap()); - +} + +static inline +void world_generate_instance(void) { int32_t world_build_status = worldgen_test(&world); ZPL_ASSERT(world_build_status >= 0); for (int i = 0; i < zpl_square(world.dim); ++i) { if (world.data[i] == 0) { ZPL_PANIC("Worldgen failure! Block %d is unset!\n", i); - return -1; + return; } } - - for (int i = 0; i < zpl_square(world.chunk_amount); ++i) { - ecs_entity_t e = ecs_new(world.ecs, 0); - ecs_set(world.ecs, e, Classify, {.id = EKIND_CHUNK }); - Chunk *chunk = ecs_get_mut(world.ecs, e, Chunk, NULL); - librg_entity_track(world.tracker, e); - librg_entity_chunk_set(world.tracker, e, i); - librg_chunk_to_chunkpos(world.tracker, i, &chunk->x, &chunk->y, NULL); - world.chunk_mapping[i] = e; - world.block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(chunk_size)); - world.outer_block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(chunk_size)); - chunk->id = i; - chunk->is_dirty = false; - - for (int y = 0; y < chunk_size; y += 1) { - for (int x = 0; x < chunk_size; x += 1) { - int chk_x = chunk->x * chunk_size; - int chk_y = chunk->y * chunk_size; - - block_id *c = &world.block_mapping[i][(y*chunk_size)+x]; - *c = world.data[(chk_y+y)*world.dim + (chk_x+x)]; - - c = &world.outer_block_mapping[i][(y*chunk_size)+x]; - *c = world.outer_data[(chk_y+y)*world.dim + (chk_x+x)]; - } - } - } - +} + +static inline +void world_free_worldgen_data(void) { zpl_mfree(world.data); zpl_mfree(world.outer_data); world.data = NULL; world.outer_data = NULL; +} + +int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) { + world.is_paused = false; + world.seed = seed; + world.chunk_size = chunk_size; + world.chunk_amount = chunk_amount; + + world.dim = (world.chunk_size * world.chunk_amount); + world.size = world.dim * world.dim; + + world_configure_tracker(); + world_setup_ecs(); + world_init_worldgen_data(); + world_generate_instance(); + world_init_mapping(); + world_chunk_setup_grid(); + world_free_worldgen_data(); zpl_printf("[INFO] Created a new server world\n"); - return world_build_status; + return WORLD_ERROR_NONE; } int32_t world_destroy(void) { diff --git a/run_client_remote.bat b/run_client_remote.bat index 7a125cb..d661d3f 100644 --- a/run_client_remote.bat +++ b/run_client_remote.bat @@ -1,5 +1,5 @@ @echo off -call package.bat +call package.bat SKIP_DEPLOY pkg\eco2d.exe -v %* diff --git a/run_server.bat b/run_server.bat index 263f98b..bba0ace 100644 --- a/run_server.bat +++ b/run_server.bat @@ -1,5 +1,5 @@ - @echo off +@echo off -call package.bat +call package.bat SKIP_DEPLOY cls pkg\eco2d.exe -d %*