From f1985c5815ae54536e239c1ee6007cae14f85db1 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 12 Feb 2025 11:18:56 -0600 Subject: [PATCH] level clear test --- src/sim.c | 56 ++++++++++++++++++++++++++++++++++++--------------- src/sim_ent.c | 9 +++++---- src/sim_ent.h | 2 +- src/user.c | 2 +- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/sim.c b/src/sim.c index 917d6520..40ad7900 100644 --- a/src/sim.c +++ b/src/sim.c @@ -250,6 +250,16 @@ INTERNAL struct sim_ent *spawn_test_player_camera(struct sim_ctx *ctx, struct si return camera_ent; } +INTERNAL void test_clear_level(struct sim_ctx *ctx) +{ + for (u64 j = 0; j < ctx->world->num_ents_reserved; ++j) { + struct sim_ent *ent = &ctx->world->ents[j]; + if (ent->valid) { + sim_ent_enable_prop(ent, SIM_ENT_PROP_RELEASE_NEXT_TICK); + } + } +} + /* ========================== * * Release entities * ========================== */ @@ -263,9 +273,7 @@ INTERNAL void release_entities_with_prop(struct sim_ctx *ctx, enum sim_ent_prop u64 ents_to_release_count = 0; for (u64 ent_index = 0; ent_index < ctx->world->num_ents_reserved; ++ent_index) { struct sim_ent *ent = &ctx->world->ents[ent_index]; - if (!ent->valid) continue; - - if (sim_ent_has_prop(ent, prop)) { + if (ent->valid && sim_ent_has_prop(ent, prop)) { *arena_push(scratch.arena, struct sim_ent *) = ent; ++ents_to_release_count; } @@ -283,13 +291,13 @@ INTERNAL void release_entities_with_prop(struct sim_ctx *ctx, enum sim_ent_prop } } - /* Release from store */ + /* Release from snapshot */ /* TODO: Breadth first iteration to only release parent entities (since * child entities will be released along with parent anyway) */ for (u64 i = 0; i < ents_to_release_count; ++i) { struct sim_ent *ent = ents_to_release[i]; - if (ent->valid) { - sim_ent_release(ctx->world, ent); + if (ent->is_top && !ent->is_root) { + sim_ent_release(ent); } } @@ -514,14 +522,6 @@ void sim_update(struct sim_ctx *ctx, i64 target_dt_ns) struct sim_client *client = sim_client_from_channel_id(ctx->world, channel_id); if (!client->valid && kind == SIM_CMD_KIND_SIM_CLIENT_CONNECT && !host_channel_id_is_nil(channel_id)) { client = sim_client_alloc(ctx->world, channel_id); - /* TODO: Create player ent not here */ - /* FIXME: Player ent never released upon disconnect */ - struct sim_ent *player_ent = spawn_test_player(ctx); - sim_ent_enable_prop(player_ent, SIM_ENT_PROP_CONTROLLED); - struct sim_ent *camera_ent = spawn_test_player_camera(ctx, player_ent); - client->control_ent = player_ent->handle; - client->camera_ent = camera_ent->handle; - player_ent->controlling_client = client->handle; } } @@ -549,7 +549,7 @@ void sim_update(struct sim_ctx *ctx, i64 target_dt_ns) } /* ========================== * - * Process sim cmds + * Process client sim cmds * ========================== */ /* Process client cmds */ @@ -619,7 +619,7 @@ void sim_update(struct sim_ctx *ctx, i64 target_dt_ns) /* Clear level */ case SIM_CMD_KIND_CLEAR_ALL: { - /* TODO */ + test_clear_level(ctx); } break; /* Spawn test */ @@ -650,6 +650,30 @@ void sim_update(struct sim_ctx *ctx, i64 target_dt_ns) } } + /* ========================== * + * Create client ents + * ========================== */ + + ctx->oldest_client_ack_tick = ctx->world->tick; + for (u64 i = 0; i < ctx->world->num_clients_reserved; ++i) { + struct sim_client *client = &ctx->world->clients[i]; + if (client->valid) { + /* FIXME: Ents never released when client disconnects */ + struct sim_ent *player_ent = sim_ent_from_handle(ctx->world, client->control_ent); + if (!player_ent->valid) { + player_ent = spawn_test_player(ctx); + sim_ent_enable_prop(player_ent, SIM_ENT_PROP_CONTROLLED); + client->control_ent = player_ent->handle; + player_ent->controlling_client = client->handle; + } + struct sim_ent *camera_ent = sim_ent_from_handle(ctx->world, client->camera_ent); + if (!camera_ent->valid) { + camera_ent = spawn_test_player_camera(ctx, player_ent); + client->camera_ent = camera_ent->handle; + } + } + } + /* ========================== * * Update entity control from client control * ========================== */ diff --git a/src/sim_ent.c b/src/sim_ent.c index 2c59923f..24dac1ee 100644 --- a/src/sim_ent.c +++ b/src/sim_ent.c @@ -40,13 +40,14 @@ struct sim_ent *sim_ent_alloc(struct sim_ent *parent) return e; } -INTERNAL void sim_ent_release_internal(struct sim_snapshot *ss, struct sim_ent *ent) +INTERNAL void sim_ent_release_internal(struct sim_ent *ent) { + struct sim_snapshot *ss = ent->ss; /* Release children */ struct sim_ent_handle first_handle = ent->first; if (first_handle.gen) { for (struct sim_ent *child = sim_ent_from_handle(ss, first_handle); child->valid; child = sim_ent_from_handle(ss, child->next)) { - sim_ent_release_internal(ss, child); + sim_ent_release_internal(child); } } @@ -58,12 +59,12 @@ INTERNAL void sim_ent_release_internal(struct sim_snapshot *ss, struct sim_ent * --ss->num_ents_allocated; } -void sim_ent_release(struct sim_snapshot *ss, struct sim_ent *ent) +void sim_ent_release(struct sim_ent *ent) { if (ent->parent.gen) { sim_ent_unlink_from_parent(ent); } - sim_ent_release_internal(ss, ent); + sim_ent_release_internal(ent); } /* ========================== * diff --git a/src/sim_ent.h b/src/sim_ent.h index 7d7fb7bc..0b93be5d 100644 --- a/src/sim_ent.h +++ b/src/sim_ent.h @@ -365,7 +365,7 @@ INLINE b32 sim_ent_is_valid_and_active(struct sim_ent *ent) /* Alloc */ struct sim_ent *sim_ent_alloc(struct sim_ent *parent); -void sim_ent_release(struct sim_snapshot *ss, struct sim_ent *ent); +void sim_ent_release(struct sim_ent *ent); /* Xform */ struct xform sim_ent_get_xform(struct sim_ent *ent); diff --git a/src/user.c b/src/user.c index 4fc52beb..c5ecf753 100644 --- a/src/user.c +++ b/src/user.c @@ -1530,7 +1530,7 @@ INTERNAL void user_update(void) draw_text(G.ui_cmd_buffer, font, pos, string_format(temp.arena, LIT("Read from local sim: %F mbit/s"), FMT_FLOAT_P((f64)G.client_bytes_read.last_second * 8 / 1000 / 1000, 3))); pos.y += spacing; - draw_text(G.ui_cmd_buffer, font, pos, string_format(temp.arena, LIT("Sent to local sim: %F mbit/s"), FMT_FLOAT_P((f64)G.client_bytes_sent.last_second * 8 / 1000 / 1000, 3))); + draw_text(G.ui_cmd_buffer, font, pos, string_format(temp.arena, LIT("Send to local sim: %F mbit/s"), FMT_FLOAT_P((f64)G.client_bytes_sent.last_second * 8 / 1000 / 1000, 3))); pos.y += spacing; pos.y += spacing;