antizona

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 5ffc8ac1a85d6b058b127a3e0891b87dc9551b66
parent 633a6f36e401faca4226238463843d0b5df69a8e
Author: averagecoder <averagecoder@noreply.codeberg.org>
Date:   Mon, 24 Aug 2026 18:09:07 +0300

feat: add net buffer

Diffstat:
Msrc/render.c | 123++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 91 insertions(+), 32 deletions(-)

diff --git a/src/render.c b/src/render.c @@ -135,12 +135,19 @@ struct mesh { float spr_x, spr_y, spr_z, spr_r, total_area; }; +struct net_snap { + float x, y, z; + float yaw; + double time; +}; + struct net_player { uint8_t id; uint16_t skin; float cur_x, cur_y, cur_z; - float target_x, target_y, target_z; - float cur_yaw, target_yaw; + float cur_yaw; + struct net_snap snaps[4]; + int snap_count; int active; }; @@ -801,7 +808,31 @@ net_send_chat(const char *msg) } static void -net_poll(struct sys_gfx *g) +net_player_add_snap(struct net_player *np, float x, float y, float z, float yaw, double now) +{ + int i; + + if (np->snap_count < 4) { + np->snaps[np->snap_count].x = x; + np->snaps[np->snap_count].y = y; + np->snaps[np->snap_count].z = z; + np->snaps[np->snap_count].yaw = yaw; + np->snaps[np->snap_count].time = now; + np->snap_count++; + } else { + for (i = 0; i < 3; i++) { + np->snaps[i] = np->snaps[i + 1]; + } + np->snaps[3].x = x; + np->snaps[3].y = y; + np->snaps[3].z = z; + np->snaps[3].yaw = yaw; + np->snaps[3].time = now; + } +} + +static void +net_poll(struct sys_gfx *g, double now) { uint8_t buf[2048]; ssize_t n; @@ -850,22 +881,18 @@ net_poll(struct sys_gfx *g) net_players[id].cur_y = ws->players[i].y; net_players[id].cur_z = ws->players[i].z; net_players[id].cur_yaw = ws->players[i].yaw; - net_players[id].target_x = ws->players[i].x; - net_players[id].target_y = ws->players[i].y; - net_players[id].target_z = ws->players[i].z; - net_players[id].target_yaw = ws->players[i].yaw; + net_players[id].snap_count = 0; net_players[id].active = 1; - } else { - net_players[id].skin = ws->players[i].skin; - net_players[id].target_x = ws->players[i].x; - net_players[id].target_y = ws->players[i].y; - net_players[id].target_z = ws->players[i].z; - net_players[id].target_yaw = ws->players[i].yaw; } + net_players[id].skin = ws->players[i].skin; + net_player_add_snap(&net_players[id], + ws->players[i].x, ws->players[i].y, ws->players[i].z, + ws->players[i].yaw, now); } for (i = 0; i < MAX_PLAYERS; i++) { if (!seen[i]) { net_players[i].active = 0; + net_players[i].snap_count = 0; } } } @@ -897,32 +924,62 @@ net_send_leave(void) } static void -net_interpolate(float dt) +net_interpolate(double now) { - float f, dyaw; - int i; + double render_time, dt_snap, t; + float dyaw; + int i, k; + struct net_player *np; + struct net_snap *s0, *s1; - f = 15.0f * dt; - if (f > 1.0f) { - f = 1.0f; - } + render_time = now - 0.066; for (i = 0; i < MAX_PLAYERS; i++) { - if (!net_players[i].active || i == my_player_id) { + np = &net_players[i]; + if (!np->active || i == my_player_id || np->snap_count < 2) { continue; } - net_players[i].cur_x += (net_players[i].target_x - net_players[i].cur_x) * f; - net_players[i].cur_y += (net_players[i].target_y - net_players[i].cur_y) * f; - net_players[i].cur_z += (net_players[i].target_z - net_players[i].cur_z) * f; - dyaw = net_players[i].target_yaw - net_players[i].cur_yaw; - while (dyaw > 3.14159265f) { - dyaw -= 6.2831853f; + s0 = NULL; + s1 = NULL; + + for (k = 0; k < np->snap_count - 1; k++) { + if (render_time >= np->snaps[k].time && + render_time <= np->snaps[k + 1].time) { + s0 = &np->snaps[k]; + s1 = &np->snaps[k + 1]; + break; + } } - while (dyaw < -3.14159265f) { - dyaw += 6.2831853f; + + if (s0 && s1) { + dt_snap = s1->time - s0->time; + t = (dt_snap > 1e-5) ? (render_time - s0->time) / dt_snap : 0.0; + if (t < 0.0) t = 0.0; + if (t > 1.0) t = 1.0; + + np->cur_x = s0->x + (s1->x - s0->x) * t; + np->cur_y = s0->y + (s1->y - s0->y) * t; + np->cur_z = s0->z + (s1->z - s0->z) * t; + + dyaw = s1->yaw - s0->yaw; + while (dyaw > 3.14159265f) dyaw -= 6.2831853f; + while (dyaw < -3.14159265f) dyaw += 6.2831853f; + np->cur_yaw = s0->yaw + dyaw * t; + } else if (render_time > np->snaps[np->snap_count - 1].time) { + s0 = &np->snaps[np->snap_count - 2]; + s1 = &np->snaps[np->snap_count - 1]; + dt_snap = s1->time - s0->time; + t = (dt_snap > 1e-5) ? (render_time - s1->time) / dt_snap : 0.0; + if (t > 2.0) { + t = 2.0; + } + + np->cur_x = s1->x + (s1->x - s0->x) * t; + np->cur_y = s1->y + (s1->y - s0->y) * t; + np->cur_z = s1->z + (s1->z - s0->z) * t; + np->cur_yaw = s1->yaw; } - net_players[i].cur_yaw += dyaw * f; } } @@ -2796,6 +2853,8 @@ main(int argc, char *argv[]) if (dt > 0.1f) dt = 0.1f; if (dt < 0.001f) dt = 0.001f; + double cur_time = t_end.tv_sec + t_end.tv_nsec * 1e-9; + g.game_time_sec += dt; g.game_time_hours += dt * (24.0f / 1440.0f); if (g.game_time_hours >= 24.0f) { @@ -2808,8 +2867,8 @@ main(int argc, char *argv[]) } } - net_poll(&g); - net_interpolate(dt); + net_poll(&g, cur_time); + net_interpolate(cur_time); net_timer += dt; if (net_timer >= 0.033f) {