diff --git a/src/sim_ent.c b/src/sim_ent.c index 1038dcd6..d5700e7c 100644 --- a/src/sim_ent.c +++ b/src/sim_ent.c @@ -32,7 +32,7 @@ struct sim_ent *sim_ent_alloc_raw(struct sim_snapshot *ss) return ent; } -/* Allocates a new entity that will not touch the network */ +/* Allocates a new entity that will not sync */ struct sim_ent *sim_ent_alloc_local(struct sim_ent *parent) { ASSERT(parent->valid); @@ -46,7 +46,7 @@ struct sim_ent *sim_ent_alloc_local(struct sim_ent *parent) } /* Allocates a new entity with a random uid to be synced to clients */ -struct sim_ent *sim_ent_alloc_net_src(struct sim_ent *parent) +struct sim_ent *sim_ent_alloc_sync_src(struct sim_ent *parent) { struct sim_snapshot *ss = parent->ss; struct sim_ent *e = sim_ent_alloc_raw(ss); @@ -54,14 +54,14 @@ struct sim_ent *sim_ent_alloc_net_src(struct sim_ent *parent) sim_ent_set_uid(e, rng_rand_uid()); - sim_ent_enable_prop(e, SIM_ENT_PROP_NET_SRC); - e->net_src_client = ss->client->handle; + sim_ent_enable_prop(e, SIM_ENT_PROP_SYNC_SRC); + e->sync_src_client = ss->client->handle; return e; } /* Allocates a new entity that will sync with incoming net src ents containing uid, and coming from the specified client */ -struct sim_ent *sim_ent_alloc_net_dst(struct sim_ent *parent, struct sim_client_handle client_handle, struct uid uid) +struct sim_ent *sim_ent_alloc_sync_dst(struct sim_ent *parent, struct sim_client_handle client_handle, struct uid uid) { struct sim_snapshot *ss = parent->ss; struct sim_ent *e = sim_ent_alloc_raw(ss); @@ -69,8 +69,8 @@ struct sim_ent *sim_ent_alloc_net_dst(struct sim_ent *parent, struct sim_client_ sim_ent_set_uid(e, uid); - sim_ent_enable_prop(e, SIM_ENT_PROP_NET_DST); - e->net_src_client = client_handle; + sim_ent_enable_prop(e, SIM_ENT_PROP_SYNC_DST); + e->sync_src_client = client_handle; return e; } diff --git a/src/sim_ent.h b/src/sim_ent.h index 11c15bb3..db95355f 100644 --- a/src/sim_ent.h +++ b/src/sim_ent.h @@ -16,8 +16,8 @@ enum sim_ent_prop { SIM_ENT_PROP_ACTIVE, SIM_ENT_PROP_RELEASE, - SIM_ENT_PROP_NET_SRC, /* This entity should be networked to other clients */ - SIM_ENT_PROP_NET_DST, /* This entity is not locally created, and should sync with incoming net src ents */ + SIM_ENT_PROP_SYNC_SRC, /* This entity should be networked to other clients */ + SIM_ENT_PROP_SYNC_DST, /* This entity is not locally created, and should sync with incoming net src ents */ SIM_ENT_PROP_CLIENT, SIM_ENT_PROP_LOCAL_CLIENT, @@ -98,13 +98,13 @@ struct sim_ent { struct sim_ent_handle next_free; /* ====================================================================== */ - /* Net */ + /* Sync */ - /* SIM_ENT_PROP_NET_SRC */ - /* SIM_ENT_PROP_NET_DST */ + /* SIM_ENT_PROP_SYNC_SRC */ + /* SIM_ENT_PROP_SYNC_DST */ - /* Handle of the client that owns the src ent of this dst ent */ - struct sim_client_handle net_src_client; + /* Handle of the client that this ent was synced from */ + struct sim_client_handle sync_src_client; /* ====================================================================== */ /* Position */ @@ -398,8 +398,8 @@ INLINE b32 sim_ent_is_valid_and_active(struct sim_ent *ent) /* Alloc */ struct sim_ent *sim_ent_alloc_raw(struct sim_snapshot *ss); struct sim_ent *sim_ent_alloc_local(struct sim_ent *parent); -struct sim_ent *sim_ent_alloc_net_src(struct sim_ent *parent); -struct sim_ent *sim_ent_alloc_net_dst(struct sim_ent *parent, struct sim_client_handle client_handle, struct uid uid); +struct sim_ent *sim_ent_alloc_sync_src(struct sim_ent *parent); +struct sim_ent *sim_ent_alloc_sync_dst(struct sim_ent *parent, struct sim_client_handle client_handle, struct uid uid); void sim_ent_release_raw(struct sim_ent *ent); void sim_ent_release(struct sim_ent *ent); diff --git a/src/sim_step.c b/src/sim_step.c index 2657e1a4..d6faefa4 100644 --- a/src/sim_step.c +++ b/src/sim_step.c @@ -201,7 +201,6 @@ INTERNAL void spawn_test_entities(struct sim_snapshot *world, struct v2 offset) /* Enemy */ { - //struct sim_ent *e = sim_ent_alloc_net_src(root); struct sim_ent *e = sim_ent_alloc_local(root); struct v2 pos = V2(1, -2); @@ -225,7 +224,7 @@ INTERNAL void spawn_test_entities(struct sim_snapshot *world, struct v2 offset) /* Big box */ #if 1 { - struct sim_ent *e = sim_ent_alloc_net_src(root); + struct sim_ent *e = sim_ent_alloc_sync_src(root); struct v2 pos = V2(1, -0.5); pos = v2_add(pos, offset); @@ -249,7 +248,7 @@ INTERNAL void spawn_test_entities(struct sim_snapshot *world, struct v2 offset) /* Tiny box */ #if 0 { - struct sim_ent *e = sim_ent_alloc_net_src(root); + struct sim_ent *e = sim_ent_alloc_sync_src(root); struct v2 pos = V2(1, -0.5); pos = v2_add(pos, offset); @@ -279,7 +278,7 @@ INTERNAL struct sim_ent *spawn_test_player(struct sim_snapshot *world) //if (!ctx->extra_spawn) { { - struct sim_ent *e = sim_ent_alloc_net_src(root); + struct sim_ent *e = sim_ent_alloc_sync_src(root); struct v2 pos = V2(1, -1); @@ -332,7 +331,7 @@ INTERNAL struct sim_ent *spawn_test_player(struct sim_snapshot *world) /* Player weapon */ if (player_ent->valid) { - struct sim_ent *e = sim_ent_alloc_net_src(player_ent); + struct sim_ent *e = sim_ent_alloc_sync_src(player_ent); e->sprite = sprite_tag_from_path(LIT("res/graphics/gun.ase")); sim_ent_enable_prop(e, SIM_ENT_PROP_ATTACHED); @@ -355,7 +354,7 @@ INTERNAL struct sim_ent *spawn_test_player_camera(struct sim_snapshot *world, st struct sim_ent *camera_ent = sim_ent_nil(); if (player_ent->valid) { - camera_ent = sim_ent_alloc_net_src(root); + camera_ent = sim_ent_alloc_sync_src(root); sim_ent_set_xform(camera_ent, XFORM_IDENT); sim_ent_enable_prop(camera_ent, SIM_ENT_PROP_CAMERA); @@ -528,7 +527,7 @@ INTERNAL PHYS_COLLISION_CALLBACK_FUNC_DEF(on_collision, collision_data_array, st /* TODO: Remove this */ { struct xform xf = XFORM_TRS(.t = point, .r = rng_rand_f32(0, TAU)); - struct sim_ent *decal = sim_ent_alloc_net_src(root); + struct sim_ent *decal = sim_ent_alloc_sync_src(root); decal->sprite = sprite_tag_from_path(LIT("res/graphics/blood.ase")); decal->sprite_tint = RGBA_32_F(1, 1, 1, 0.25f); decal->layer = SIM_LAYER_FLOOR_DECALS; @@ -966,7 +965,7 @@ void sim_step(struct sim_step_ctx *ctx) /* Spawn bullet */ struct sim_ent *bullet; { - bullet = sim_ent_alloc_net_src(root); + bullet = sim_ent_alloc_sync_src(root); bullet->bullet_src = ent->handle; bullet->bullet_src_pos = rel_pos; @@ -993,7 +992,7 @@ void sim_step(struct sim_step_ctx *ctx) /* Spawn tracer */ { - struct sim_ent *tracer = sim_ent_alloc_net_src(root); + struct sim_ent *tracer = sim_ent_alloc_sync_src(root); tracer->tracer_fade_duration = 0.025f; tracer->layer = SIM_LAYER_TRACERS; sim_ent_enable_prop(tracer, SIM_ENT_PROP_TRACER); @@ -1014,7 +1013,7 @@ void sim_step(struct sim_step_ctx *ctx) if (sim_ent_has_prop(ent, SIM_ENT_PROP_CONTROLLED)) { struct sim_ent *joint_ent = sim_ent_from_handle(world, ent->move_joint); if (!sim_ent_is_valid_and_active(joint_ent)) { - joint_ent = sim_ent_alloc_net_src(root); + joint_ent = sim_ent_alloc_sync_src(root); joint_ent->mass_unscaled = F32_INFINITY; joint_ent->inertia_unscaled = F32_INFINITY; sim_ent_enable_prop(joint_ent, SIM_ENT_PROP_MOTOR_JOINT); @@ -1051,7 +1050,7 @@ void sim_step(struct sim_step_ctx *ctx) /* Retrieve / create aim joint */ struct sim_ent *joint_ent = sim_ent_from_handle(world, ent->aim_joint); if (!sim_ent_is_valid_and_active(joint_ent)) { - joint_ent = sim_ent_alloc_net_src(root); + joint_ent = sim_ent_alloc_sync_src(root); joint_ent->mass_unscaled = F32_INFINITY; joint_ent->inertia_unscaled = F32_INFINITY; sim_ent_enable_prop(joint_ent, SIM_ENT_PROP_PHYSICAL_KINEMATIC); /* Since we'll be setting velocity manually */ @@ -1148,7 +1147,7 @@ void sim_step(struct sim_step_ctx *ctx) def.max_torque = ent->angular_ground_friction; if (joint_ent->motor_joint_data.max_force != def.max_force || joint_ent->motor_joint_data.max_torque != def.max_torque) { if (!sim_ent_is_valid_and_active(joint_ent)) { - joint_ent = sim_ent_alloc_net_src(root); + joint_ent = sim_ent_alloc_sync_src(root); sim_ent_enable_prop(joint_ent, SIM_ENT_PROP_MOTOR_JOINT); sim_ent_enable_prop(joint_ent, SIM_ENT_PROP_ACTIVE); joint_ent->motor_joint_data = phys_motor_joint_from_def(def); @@ -1208,7 +1207,7 @@ void sim_step(struct sim_step_ctx *ctx) if (sim_ent_is_valid_and_active(target_ent)) { if (!sim_ent_is_valid_and_active(joint_ent)) { /* FIXME: Joint ent may never release */ - joint_ent = sim_ent_alloc_net_src(root); + joint_ent = sim_ent_alloc_sync_src(root); joint_ent->mass_unscaled = F32_INFINITY; joint_ent->inertia_unscaled = F32_INFINITY; client_ent->client_dbg_drag_joint_ent = joint_ent->handle; @@ -1314,7 +1313,7 @@ void sim_step(struct sim_step_ctx *ctx) /* Spawn quake */ { - struct sim_ent *quake = sim_ent_alloc_net_src(root); + struct sim_ent *quake = sim_ent_alloc_sync_src(root); sim_ent_set_xform(quake, XFORM_POS(pos)); quake->quake_intensity = 0.2f; quake->quake_fade = quake->quake_intensity / 0.1f; diff --git a/src/user.c b/src/user.c index c0b7a8af..af5237de 100644 --- a/src/user.c +++ b/src/user.c @@ -359,8 +359,8 @@ INTERNAL struct string get_ent_debug_text(struct arena *arena, struct sim_ent *e { res.len += string_copy(arena, LIT("net: ")).len; - b32 transmitting = sim_ent_has_prop(ent, SIM_ENT_PROP_NET_SRC); - b32 receiving = sim_ent_has_prop(ent, SIM_ENT_PROP_NET_DST); + b32 transmitting = sim_ent_has_prop(ent, SIM_ENT_PROP_SYNC_SRC); + b32 receiving = sim_ent_has_prop(ent, SIM_ENT_PROP_SYNC_DST); if (transmitting & receiving) { res.len += string_copy(arena, LIT(" recv & send")).len; } else if (transmitting) { @@ -1851,7 +1851,7 @@ INTERNAL struct sim_ent_handle _translate(struct sim_snapshot *local_ss, struct return local_handle; } -INTERNAL void sim_ent_sync_remote(struct sim_ent *local, struct sim_ent *remote) +INTERNAL void sim_ent_sync_tree(struct sim_ent *local, struct sim_ent *remote) { struct sim_snapshot *local_ss = local->ss; struct sim_snapshot *remote_ss = remote->ss; @@ -1860,13 +1860,13 @@ INTERNAL void sim_ent_sync_remote(struct sim_ent *local, struct sim_ent *remote) struct sim_ent old = *local; MEMCPY_STRUCT(local, remote); - sim_ent_disable_prop(local, SIM_ENT_PROP_NET_SRC); - sim_ent_enable_prop(local, SIM_ENT_PROP_NET_DST); + sim_ent_disable_prop(local, SIM_ENT_PROP_SYNC_SRC); + sim_ent_enable_prop(local, SIM_ENT_PROP_SYNC_DST); /* Keep non-remote handles */ local->ss = old.ss; local->handle = old.handle; - local->net_src_client = remote_client_handle; + local->sync_src_client = remote_client_handle; local->_uid = old._uid; local->parent = old.parent; local->prev = old.prev; @@ -1895,10 +1895,10 @@ INTERNAL void sim_ent_sync_remote(struct sim_ent *local, struct sim_ent *remote) /* Walks a local & remote ent tree and allocates net dst ents from remote src ents */ -INTERNAL void sim_ent_alloc_any_missing_net_dst(struct sim_ent *local_parent, struct sim_ent *remote) +INTERNAL void sim_ent_alloc_any_new_sync_dsts(struct sim_ent *local_parent, struct sim_ent *remote) { __prof; - if (sim_ent_has_prop(remote, SIM_ENT_PROP_NET_SRC)) { + if (sim_ent_has_prop(remote, SIM_ENT_PROP_SYNC_SRC)) { struct sim_snapshot *local_ss = local_parent->ss; struct sim_snapshot *remote_ss = remote->ss; struct sim_client_handle remote_client_handle = remote_ss->client->handle; @@ -1906,16 +1906,15 @@ INTERNAL void sim_ent_alloc_any_missing_net_dst(struct sim_ent *local_parent, st struct uid uid = sim_ent_get_uid(remote); struct sim_ent *local_ent = sim_ent_from_uid(local_ss, uid); if (!local_ent->valid) { - local_ent = sim_ent_alloc_net_dst(local_parent, remote_client_handle, uid); + local_ent = sim_ent_alloc_sync_dst(local_parent, remote_client_handle, uid); } for (struct sim_ent *remote_child = sim_ent_from_handle(remote_ss, remote->first); remote_child->valid; remote_child = sim_ent_from_handle(remote_ss, remote_child->next)) { - sim_ent_alloc_any_missing_net_dst(local_ent, remote_child); + sim_ent_alloc_any_new_sync_dsts(local_ent, remote_child); } } } - -INTERNAL void sim_snapshot_sync_net_ents(struct sim_snapshot *local_ss, struct sim_snapshot *remote_ss) +INTERNAL void sim_snapshot_sync(struct sim_snapshot *local_ss, struct sim_snapshot *remote_ss) { __prof; @@ -1926,22 +1925,22 @@ INTERNAL void sim_snapshot_sync_net_ents(struct sim_snapshot *local_ss, struct s /* Create new ents from remote */ for (struct sim_ent *remote_top = sim_ent_from_handle(remote_ss, remote_root->first); remote_top->valid; remote_top = sim_ent_from_handle(remote_ss, remote_top->next)) { - sim_ent_alloc_any_missing_net_dst(local_root, remote_top); + sim_ent_alloc_any_new_sync_dsts(local_root, remote_top); } /* Sync ents with remote */ for (u64 i = 0; i < local_ss->num_ents_reserved; ++i) { struct sim_ent *local_ent = &local_ss->ents[i]; - if (local_ent->valid && sim_ent_has_prop(local_ent, SIM_ENT_PROP_NET_DST) && sim_client_handle_eq(local_ent->net_src_client, remote_client_handle)) { + if (local_ent->valid && sim_ent_has_prop(local_ent, SIM_ENT_PROP_SYNC_DST) && sim_client_handle_eq(local_ent->sync_src_client, remote_client_handle)) { struct uid uid = sim_ent_get_uid(local_ent); struct sim_ent *remote_ent = sim_ent_from_uid(remote_ss, uid); if (remote_ent->valid) { /* Copy all ent data from remote */ - sim_ent_sync_remote(local_ent, remote_ent); + sim_ent_sync_tree(local_ent, remote_ent); } else { /* Remote ent is no longer valid / networked, release it */ sim_ent_enable_prop(local_ent, SIM_ENT_PROP_RELEASE); - sim_ent_disable_prop(local_ent, SIM_ENT_PROP_NET_DST); + sim_ent_disable_prop(local_ent, SIM_ENT_PROP_SYNC_DST); } } } @@ -2144,9 +2143,9 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) sim_snapshot_decode(&br, ss); for (u64 i = 0; i < ss->num_ents_reserved; ++i) { struct sim_ent *ent = &ss->ents[i]; - if (ent->valid && sim_ent_has_prop(ent, SIM_ENT_PROP_NET_DST)) { - sim_ent_disable_prop(ent, SIM_ENT_PROP_NET_DST); - sim_ent_enable_prop(ent, SIM_ENT_PROP_NET_SRC); + if (ent->valid && sim_ent_has_prop(ent, SIM_ENT_PROP_SYNC_DST)) { + sim_ent_disable_prop(ent, SIM_ENT_PROP_SYNC_DST); + sim_ent_enable_prop(ent, SIM_ENT_PROP_SYNC_SRC); } } } else { @@ -2174,7 +2173,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) /* Find / create local control cmd ent */ struct sim_ent *control_cmd_ent = sim_ent_find_first_match_one(user_input_ss, SIM_ENT_PROP_CMD_CONTROL); if (!control_cmd_ent->valid) { - control_cmd_ent = sim_ent_alloc_net_src(user_input_root); + control_cmd_ent = sim_ent_alloc_sync_src(user_input_root); sim_ent_enable_prop(control_cmd_ent, SIM_ENT_PROP_CMD_CONTROL); sim_ent_activate(control_cmd_ent, user_input_ss->tick); } @@ -2206,7 +2205,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) struct sim_ent *client_ent = sim_ent_from_uid(local_ss, client->ent_uid); if (!client_ent->valid) { /* FIXME: Client ent never released upon disconnect */ - client_ent = sim_ent_alloc_net_src(local_root); + client_ent = sim_ent_alloc_sync_src(local_root); client_ent->client_handle = client->handle; sim_ent_enable_prop(client_ent, SIM_ENT_PROP_CLIENT); sim_ent_enable_prop(client_ent, SIM_ENT_PROP_ACTIVE); @@ -2222,7 +2221,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) if (client != publish_client && client != local_client) { struct sim_snapshot *client_ss = sim_snapshot_from_tick(client, step_tick); if (client_ss->valid) { - sim_snapshot_sync_net_ents(local_ss, client_ss); + sim_snapshot_sync(local_ss, client_ss); if (!is_master && client == master_client) { local_ss->local_client_ent_uid = client_ss->local_client_ent_uid; } @@ -2236,8 +2235,8 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) /* Point incoming client cmds to correct client ents */ for (u64 i = 0; i < local_ss->num_ents_reserved; ++i) { struct sim_ent *ent = &local_ss->ents[i]; - if (ent->valid && sim_ent_has_prop(ent, SIM_ENT_PROP_NET_DST)) { - struct sim_client *src_client = sim_client_from_handle(store, ent->net_src_client); + if (ent->valid && sim_ent_has_prop(ent, SIM_ENT_PROP_SYNC_DST)) { + struct sim_client *src_client = sim_client_from_handle(store, ent->sync_src_client); struct sim_ent *client_ent = sim_ent_from_uid(local_ss, src_client->ent_uid); ent->cmd_client = client_ent->handle; } @@ -2246,8 +2245,8 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) /* Mark all local commands as networked */ for (u64 i = 0; i < local_ss->num_ents_reserved; ++i) { struct sim_ent *ent = &local_ss->ents[i]; - if (ent->valid && sim_ent_has_prop(ent, SIM_ENT_PROP_CMD_CONTROL) && sim_ent_has_prop(ent, SIM_ENT_PROP_NET_DST)) { - sim_ent_enable_prop(ent, SIM_ENT_PROP_NET_SRC); + if (ent->valid && sim_ent_has_prop(ent, SIM_ENT_PROP_CMD_CONTROL) && sim_ent_has_prop(ent, SIM_ENT_PROP_SYNC_DST)) { + sim_ent_enable_prop(ent, SIM_ENT_PROP_SYNC_SRC); } } } @@ -2372,7 +2371,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(user_local_sim_thread_entry_point, arg) } else { pub_ss = sim_snapshot_alloc(publish_client, sim_snapshot_from_tick(publish_client, publish_client->last_tick), local_ss->tick + 5); } - sim_snapshot_sync_net_ents(pub_ss, local_ss); + sim_snapshot_sync(pub_ss, local_ss); } #endif