From 44497385808cd2567e5a448ff6ced67ee11901c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 8 Sep 2021 18:53:50 +0200 Subject: [PATCH] various networking improvements --- code/game/src/game.c | 4 +++- code/game/src/prediction.c | 27 +++++++++++++-------------- code/game/src/world/world.c | 18 ++++++++++++++---- code/game/src/world/world.h | 3 +++ code/modules/source/system_items.c | 6 +++++- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/code/game/src/game.c b/code/game/src/game.c index 286f17c..90951f9 100644 --- a/code/game/src/game.c +++ b/code/game/src/game.c @@ -186,7 +186,9 @@ void game_shutdown() { if (game_mode != GAMEKIND_HEADLESS) { world_viewers_destroy(); - platform_shutdown(); + + // TODO(zaklaus): crashes on exit + //platform_shutdown(); } } diff --git a/code/game/src/prediction.c b/code/game/src/prediction.c index b0cdaf3..63a0a5c 100644 --- a/code/game/src/prediction.c +++ b/code/game/src/prediction.c @@ -33,11 +33,11 @@ static inline float spherical_lerp(float a, float b, float t) { } float smooth_val(float cur, float tgt, uint64_t dt) { - float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_FAST_MS, WORLD_TRACKER_UPDATE_SLOW_MS))); + float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS))); #if 0 dt = 200; - factor = map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_FAST_MS, WORLD_TRACKER_UPDATE_SLOW_MS)); + factor = map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS)); zpl_printf("lerp factor: %f\n", factor); zpl_exit(0); #endif @@ -46,7 +46,7 @@ float smooth_val(float cur, float tgt, uint64_t dt) { } float smooth_val_spherical(float cur, float tgt, uint64_t dt) { - float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_FAST_MS, WORLD_TRACKER_UPDATE_SLOW_MS))); + float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS))); return spherical_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)); } @@ -75,17 +75,16 @@ void lerp_entity_positions(uint64_t key, entity_view *data) { world_view *view = game_world_view_get_active(); if (data->flag == EFLAG_INTERP) { - -#if 0 - data->x = smooth_val(data->x, data->tx, view->delta_time[data->layer_id]); - data->y = smooth_val(data->y, data->ty, view->delta_time[data->layer_id]); - data->heading = smooth_val_spherical(data->heading, data->theading, view->delta_time[data->layer_id]); -#else - (void)view; - data->x = data->tx; - data->y = data->ty; - data->heading = data->theading; -#endif + if (game_get_kind() == GAMEKIND_CLIENT) { + data->x = smooth_val(data->x, data->tx, view->delta_time[data->layer_id]); + data->y = smooth_val(data->y, data->ty, view->delta_time[data->layer_id]); + data->heading = smooth_val_spherical(data->heading, data->theading, view->delta_time[data->layer_id]); + } else { + (void)view; + data->x = data->tx; + data->y = data->ty; + data->heading = data->theading; + } } } diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 65b4c52..fae7d07 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -9,6 +9,7 @@ #include "world/worldgen/worldgen.h" #include "platform.h" #include "profiler.h" +#include "game.h" #include "packets/pkt_send_librg_update.h" @@ -282,15 +283,24 @@ static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius) } } - int32_t world_update() { profile (PROF_UPDATE_SYSTEMS) { ecs_progress(world.ecs, 0.0f); } - world_tracker_update(0, WORLD_TRACKER_UPDATE_FAST_MS, 2); - world_tracker_update(1, WORLD_TRACKER_UPDATE_NORMAL_MS, 4); - world_tracker_update(2, WORLD_TRACKER_UPDATE_SLOW_MS, 6); + uint32_t fast_ms = WORLD_TRACKER_UPDATE_FAST_MS; + uint32_t normal_ms = WORLD_TRACKER_UPDATE_NORMAL_MS; + uint32_t slow_ms = WORLD_TRACKER_UPDATE_SLOW_MS; + + if (game_get_kind() != GAMEKIND_SINGLE) { + fast_ms = WORLD_TRACKER_UPDATE_MP_FAST_MS; + normal_ms = WORLD_TRACKER_UPDATE_MP_NORMAL_MS; + slow_ms = WORLD_TRACKER_UPDATE_MP_SLOW_MS; + } + + world_tracker_update(0, fast_ms, 2); + world_tracker_update(1, normal_ms, 4); + world_tracker_update(2, slow_ms, 6); debug_replay_update(); return 0; diff --git a/code/game/src/world/world.h b/code/game/src/world/world.h index f184d0a..ae388be 100644 --- a/code/game/src/world/world.h +++ b/code/game/src/world/world.h @@ -18,6 +18,9 @@ #define WORLD_TRACKER_UPDATE_FAST_MS 10 #define WORLD_TRACKER_UPDATE_NORMAL_MS 50 #define WORLD_TRACKER_UPDATE_SLOW_MS 100 +#define WORLD_TRACKER_UPDATE_MP_FAST_MS 50 +#define WORLD_TRACKER_UPDATE_MP_NORMAL_MS 150 +#define WORLD_TRACKER_UPDATE_MP_SLOW_MS 300 #define WORLD_BLOCK_SIZE 64 #define WORLD_PKT_READER(name) int32_t name(void* data, uint32_t datalen, void *udata) diff --git a/code/modules/source/system_items.c b/code/modules/source/system_items.c index 63ad9f3..a545db5 100644 --- a/code/modules/source/system_items.c +++ b/code/modules/source/system_items.c @@ -96,8 +96,12 @@ void DropItem(ecs_iter_t *it) { if (in[i].sprint) { dropped_count /= 2; } else if (in[i].ctrl) { - dropped_count = 1; + dropped_count = dropped_count > 0 ? 1 : 0; } + + if (dropped_count == 0) + continue; + ecs_entity_t te = item_spawn(item->kind, dropped_count); item->quantity -= dropped_count;