level clear test

This commit is contained in:
jacob 2025-02-12 11:18:56 -06:00
parent 98c70b46ba
commit f1985c5815
4 changed files with 47 additions and 22 deletions

View File

@ -250,6 +250,16 @@ INTERNAL struct sim_ent *spawn_test_player_camera(struct sim_ctx *ctx, struct si
return camera_ent; 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 * 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; u64 ents_to_release_count = 0;
for (u64 ent_index = 0; ent_index < ctx->world->num_ents_reserved; ++ent_index) { for (u64 ent_index = 0; ent_index < ctx->world->num_ents_reserved; ++ent_index) {
struct sim_ent *ent = &ctx->world->ents[ent_index]; struct sim_ent *ent = &ctx->world->ents[ent_index];
if (!ent->valid) continue; if (ent->valid && sim_ent_has_prop(ent, prop)) {
if (sim_ent_has_prop(ent, prop)) {
*arena_push(scratch.arena, struct sim_ent *) = ent; *arena_push(scratch.arena, struct sim_ent *) = ent;
++ents_to_release_count; ++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 /* TODO: Breadth first iteration to only release parent entities (since
* child entities will be released along with parent anyway) */ * child entities will be released along with parent anyway) */
for (u64 i = 0; i < ents_to_release_count; ++i) { for (u64 i = 0; i < ents_to_release_count; ++i) {
struct sim_ent *ent = ents_to_release[i]; struct sim_ent *ent = ents_to_release[i];
if (ent->valid) { if (ent->is_top && !ent->is_root) {
sim_ent_release(ctx->world, ent); 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); 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)) { 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); 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 */ /* Process client cmds */
@ -619,7 +619,7 @@ void sim_update(struct sim_ctx *ctx, i64 target_dt_ns)
/* Clear level */ /* Clear level */
case SIM_CMD_KIND_CLEAR_ALL: case SIM_CMD_KIND_CLEAR_ALL:
{ {
/* TODO */ test_clear_level(ctx);
} break; } break;
/* Spawn test */ /* 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 * Update entity control from client control
* ========================== */ * ========================== */

View File

@ -40,13 +40,14 @@ struct sim_ent *sim_ent_alloc(struct sim_ent *parent)
return e; 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 */ /* Release children */
struct sim_ent_handle first_handle = ent->first; struct sim_ent_handle first_handle = ent->first;
if (first_handle.gen) { 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)) { 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; --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) { if (ent->parent.gen) {
sim_ent_unlink_from_parent(ent); sim_ent_unlink_from_parent(ent);
} }
sim_ent_release_internal(ss, ent); sim_ent_release_internal(ent);
} }
/* ========================== * /* ========================== *

View File

@ -365,7 +365,7 @@ INLINE b32 sim_ent_is_valid_and_active(struct sim_ent *ent)
/* Alloc */ /* Alloc */
struct sim_ent *sim_ent_alloc(struct sim_ent *parent); 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 */ /* Xform */
struct xform sim_ent_get_xform(struct sim_ent *ent); struct xform sim_ent_get_xform(struct sim_ent *ent);

View File

@ -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))); 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; 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;
pos.y += spacing; pos.y += spacing;