rename 'client' -> 'sim_client'

This commit is contained in:
jacob 2025-02-08 09:52:31 -06:00
parent 4cbc6b6d59
commit 01274013e6
7 changed files with 108 additions and 107 deletions

View File

@ -1,61 +0,0 @@
#ifndef CLIENT_H
#define CLIENT_H
struct client_handle {
b32 valid;
u32 idx;
u32 gen;
};
struct client {
b32 valid;
struct client_handle handle;
struct host_channel_id channel_id;
u64 channel_hash;
struct client *next_free;
struct client *next_hash;
struct client *prev_hash;
struct entity_handle ent;
};
struct channel_lookup_bucket {
struct client *first;
struct client *last;
};
struct client_store {
b32 valid;
struct arena arena;
struct channel_lookup_bucket *channel_lookup_buckets;
u64 num_channel_lookup_buckets;
struct client *clients;
struct client *first_free_client;
u64 clients_reserved;
};
INLINE struct client *client_nil(void)
{
extern READONLY struct client _g_client_nil;
return &_g_client_nil;
}
INLINE struct client_store *client_store_nil(void)
{
extern READONLY struct client_store _g_client_store_nil;
return &_g_client_store_nil;
}
struct client_store *client_store_alloc(void);
void client_store_release(struct client_store *store);
struct client_store *client_store_from_client(struct client *client);
struct client *client_from_handle(struct client_store *store, struct client_handle handle);
struct client *client_from_channel_id(struct client_store *store, struct host_channel_id channel_id);
struct client *client_alloc(struct client_store *store, struct host_channel_id channel_id);
void client_release(struct client *client);
#endif

View File

@ -4,7 +4,7 @@
#include "sprite.h"
#include "mixer.h"
#include "phys.h"
#include "client.h"
#include "sim_client.h"
enum entity_prop {
ENTITY_PROP_NONE,

View File

@ -63,9 +63,9 @@ struct phys_collision_data_array phys_create_and_update_contacts(struct arena *a
struct aabb aabb = collider_aabb_from_collider(&check0_collider, check0_xf);
struct space_iter iter = space_iter_begin_aabb(space, aabb);
struct space_entry *client;
while ((client = space_iter_next(&iter))) {
struct entity *check1 = entity_from_handle(store, client->ent);
struct space_entry *space_entry;
while ((space_entry = space_iter_next(&iter))) {
struct entity *check1 = entity_from_handle(store, space_entry->ent);
if (check1 == check0) continue;
if (!entity_is_valid_and_active(check1)) continue;
if (!(entity_has_prop(check1, ENTITY_PROP_PHYSICAL_DYNAMIC) || entity_has_prop(check1, ENTITY_PROP_PHYSICAL_KINEMATIC))) continue;
@ -95,11 +95,11 @@ struct phys_collision_data_array phys_create_and_update_contacts(struct arena *a
}
struct entity_lookup_key key = entity_lookup_key_from_two_handles(e0->handle, e1->handle);
struct entity_lookup_entry *entry = entity_lookup_get(contact_lookup, key);
struct entity_lookup_entry *constraint_entry= entity_lookup_get(contact_lookup, key);
struct entity *constraint_ent = entity_nil();
if (entry) {
constraint_ent = entity_from_handle(store, entry->entity);
if (constraint_entry) {
constraint_ent = entity_from_handle(store, constraint_entry->entity);
if (entity_is_valid_and_active(constraint_ent)) {
if (constraint_ent->contact_constraint_data.last_phys_iteration >= phys_iteration) {
/* Already processed constraint this iteration */
@ -108,9 +108,9 @@ struct phys_collision_data_array phys_create_and_update_contacts(struct arena *a
constraint_ent->contact_constraint_data.last_phys_iteration = phys_iteration;
}
} else {
/* Constraint ent no longer valid, delete entry */
entity_lookup_remove(contact_lookup, entry);
entry = NULL;
/* Constraint ent no longer valid, delete constraint_entry*/
entity_lookup_remove(contact_lookup, constraint_entry);
constraint_entry= NULL;
}
}
@ -136,7 +136,7 @@ struct phys_collision_data_array phys_create_and_update_contacts(struct arena *a
/* TODO: Should we recalculate normal as more contact points are added? */
entity_enable_prop(constraint_ent, ENTITY_PROP_CONTACT_CONSTRAINT);
entity_activate(constraint_ent, tick_id);
ASSERT(!entry); /* Existing entry should never be present here */
ASSERT(!constraint_entry); /* Existing entry should never be present here */
entity_lookup_set(contact_lookup, key, constraint_ent->handle);
}
@ -1084,9 +1084,9 @@ f32 phys_determine_earliest_toi_for_bullets(struct phys_ctx *ctx, f32 step_dt, f
struct aabb combined_aabb = collider_aabb_from_combined_aabb(aabb_t0, aabb_t1);
struct space_iter iter = space_iter_begin_aabb(space, combined_aabb);
struct space_entry *client;
while ((client = space_iter_next(&iter))) {
struct entity *e1 = entity_from_handle(store, client->ent);
struct space_entry *entry;
while ((entry = space_iter_next(&iter))) {
struct entity *e1 = entity_from_handle(store, entry->ent);
if (e1 == e0) continue;
if (!entity_is_valid_and_active(e1)) continue;
if (!(entity_has_prop(e1, ENTITY_PROP_PHYSICAL_DYNAMIC) || entity_has_prop(e1, ENTITY_PROP_PHYSICAL_KINEMATIC))) continue;

View File

@ -1,4 +1,6 @@
#include "sim.h"
#include "entity.h"
#include "sim_client.h"
#include "sys.h"
#include "util.h"
#include "world.h"
@ -15,7 +17,6 @@
#include "rng.h"
#include "space.h"
#include "byteio.h"
#include "client.h"
#include "host.h"
GLOBAL struct {
@ -43,7 +44,7 @@ GLOBAL struct {
struct entity_lookup collision_debug_lookup;
#endif
struct space *space;
struct client_store *client_store;
struct sim_client_store *client_store;
/* Tick */
struct world tick;
@ -543,7 +544,7 @@ INTERNAL void sim_update(void)
enum sim_cmd_kind kind = cmd->kind;
struct host_channel_id channel_id = cmd->channel_id;
struct client *client = client_from_channel_id(G.client_store, channel_id);
struct sim_client *client = client_from_channel_id(G.client_store, channel_id);
if (client->valid || host_channel_id_is_nil(channel_id)) {
switch (kind) {
/* Cursor */
@ -566,7 +567,7 @@ INTERNAL void sim_update(void)
} break;
/* Disconnect client */
case SIM_CMD_KIND_CLIENT_DISCONNECT:
case SIM_CMD_KIND_SIM_CLIENT_DISCONNECT:
{
if (client->valid) {
struct entity *client_ent = entity_from_handle(entity_store, client->ent);
@ -581,7 +582,7 @@ INTERNAL void sim_update(void)
default: break;
};
} else if (kind == SIM_CMD_KIND_CLIENT_CONNECT && !host_channel_id_is_nil(channel_id) && !client->valid) {
} else if (kind == SIM_CMD_KIND_SIM_CLIENT_CONNECT && !host_channel_id_is_nil(channel_id) && !client->valid) {
/* Connect client */
client = client_alloc(G.client_store, channel_id);
struct entity *client_ent = entity_alloc(root);
@ -1396,7 +1397,7 @@ void sim_cmds_from_host_events(struct arena *arena, struct host_event_array host
case HOST_EVENT_KIND_CHANNEL_OPENED:
{
struct sim_cmd *cmd = arena_push_zero(arena, struct sim_cmd);
cmd->kind = SIM_CMD_KIND_CLIENT_CONNECT;
cmd->kind = SIM_CMD_KIND_SIM_CLIENT_CONNECT;
cmd->channel_id = host_event.channel_id;
if (cmds_out->last) {
cmds_out->last->next = cmd;
@ -1409,7 +1410,7 @@ void sim_cmds_from_host_events(struct arena *arena, struct host_event_array host
case HOST_EVENT_KIND_CHANNEL_CLOSED:
{
struct sim_cmd *cmd = arena_push_zero(arena, struct sim_cmd);
cmd->kind = SIM_CMD_KIND_CLIENT_DISCONNECT;
cmd->kind = SIM_CMD_KIND_SIM_CLIENT_DISCONNECT;
cmd->disconnect_reason = LIT("Connection lost");
if (cmds_out->last) {
cmds_out->last->next = cmd;

View File

@ -44,8 +44,8 @@ enum sim_cmd_kind {
SIM_CMD_KIND_PLAYER_MOVE,
SIM_CMD_KIND_PLAYER_FIRE,
SIM_CMD_KIND_CLIENT_CONNECT,
SIM_CMD_KIND_CLIENT_DISCONNECT,
SIM_CMD_KIND_SIM_CLIENT_CONNECT,
SIM_CMD_KIND_SIM_CLIENT_DISCONNECT,
/* Testing */
SIM_CMD_KIND_CLEAR_ALL,

View File

@ -1,4 +1,4 @@
#include "client.h"
#include "sim_client.h"
#include "host.h"
#include "arena.h"
#include "util.h"
@ -8,39 +8,39 @@
/* Offset in bytes from start of store struct to start of clients array (assume adjacently allocated) */
/* FIXME: Incorrect since buckets are also allocated */
#define STORE_CLIENTS_OFFSET (sizeof(struct client_store) + (sizeof(struct client_store) % alignof(struct client)))
#define STORE_CLIENTS_OFFSET (sizeof(struct sim_client_store) + (sizeof(struct sim_client_store) % alignof(struct sim_client)))
/* Accessed via client_nil() */
READONLY struct client _g_client_nil = { .valid = false };
READONLY struct client_store _g_client_store_nil = { .valid = false };
READONLY struct sim_client _g_client_nil = { .valid = false };
READONLY struct sim_client_store _g_client_store_nil = { .valid = false };
/* ========================== *
* Store
* ========================== */
struct client_store *client_store_alloc(void)
struct sim_client_store *client_store_alloc(void)
{
struct arena arena = arena_alloc(GIGABYTE(64));
struct client_store *store = arena_push_zero(&arena, struct client_store);
struct sim_client_store *store = arena_push_zero(&arena, struct sim_client_store);
store->arena = arena;
store->num_channel_lookup_buckets = CHANNEL_LOOKUP_BUCKETS;
store->channel_lookup_buckets = arena_push_array_zero(&arena, struct channel_lookup_bucket, store->num_channel_lookup_buckets);
store->clients = arena_dry_push(&arena, struct client);
store->clients = arena_dry_push(&arena, struct sim_client);
return store;
}
void client_store_release(struct client_store *store)
void client_store_release(struct sim_client_store *store)
{
arena_release(&store->arena);
}
struct client_store *client_store_from_client(struct client *client)
struct sim_client_store *client_store_from_client(struct sim_client *client)
{
if (client->valid) {
u64 first_client_addr = (u64)(client - client->handle.idx);
struct client_store *client_store = (struct client_store *)(first_client_addr - STORE_CLIENTS_OFFSET);
ASSERT(client_store->clients == (struct client *)first_client_addr);
struct sim_client_store *client_store = (struct sim_client_store *)(first_client_addr - STORE_CLIENTS_OFFSET);
ASSERT(client_store->clients == (struct sim_client *)first_client_addr);
return client_store;
} else {
return client_store_nil();
@ -56,10 +56,10 @@ INTERNAL u64 hash_from_channel_id(struct host_channel_id channel_id)
return hash_fnv64(HASH_FNV64_BASIS, STRING_FROM_STRUCT(&channel_id));
}
struct client *client_from_handle(struct client_store *store, struct client_handle handle)
struct sim_client *client_from_handle(struct sim_client_store *store, struct client_handle handle)
{
if (handle.gen != 0 && handle.idx < store->clients_reserved) {
struct client *client = &store->clients[handle.idx];
struct sim_client *client = &store->clients[handle.idx];
if (client->handle.gen == handle.gen) {
return client;
}
@ -67,13 +67,13 @@ struct client *client_from_handle(struct client_store *store, struct client_hand
return client_nil();
}
struct client *client_from_channel_id(struct client_store *store, struct host_channel_id channel_id)
struct sim_client *client_from_channel_id(struct sim_client_store *store, struct host_channel_id channel_id)
{
struct client *res = client_nil();
struct sim_client *res = client_nil();
u64 channel_hash = hash_from_channel_id(channel_id);
u64 bucket_index = channel_hash % store->num_channel_lookup_buckets;
struct channel_lookup_bucket *bucket = &store->channel_lookup_buckets[bucket_index];
for (struct client *client = bucket->first; client; client = client->next_hash) {
for (struct sim_client *client = bucket->first; client; client = client->next_hash) {
if (client->channel_hash == channel_hash) {
res = client;
break;
@ -82,9 +82,9 @@ struct client *client_from_channel_id(struct client_store *store, struct host_ch
return res;
}
struct client *client_alloc(struct client_store *store, struct host_channel_id channel_id)
struct sim_client *client_alloc(struct sim_client_store *store, struct host_channel_id channel_id)
{
struct client *client = NULL;
struct sim_client *client = NULL;
struct client_handle handle = ZI;
if (store->first_free_client) {
client = store->first_free_client;
@ -92,7 +92,7 @@ struct client *client_alloc(struct client_store *store, struct host_channel_id c
handle = client->handle;
++handle.gen;
} else {
client = arena_push(&store->arena, struct client);
client = arena_push(&store->arena, struct sim_client);
handle.gen = 1;
handle.idx = store->clients_reserved;
++store->clients_reserved;
@ -118,9 +118,9 @@ struct client *client_alloc(struct client_store *store, struct host_channel_id c
return client;
}
void client_release(struct client *client)
void client_release(struct sim_client *client)
{
struct client_store *store = client_store_from_client(client);
struct sim_client_store *store = client_store_from_client(client);
client->valid = false;
++client->handle.gen;
client->next_free = store->first_free_client;
@ -129,8 +129,8 @@ void client_release(struct client *client)
/* Remove from channel lookup */
u64 bucket_index = client->channel_hash % store->num_channel_lookup_buckets;
struct channel_lookup_bucket *bucket = &store->channel_lookup_buckets[bucket_index];
struct client *prev = client->prev_hash;
struct client *next = client->next_hash;
struct sim_client *prev = client->prev_hash;
struct sim_client *next = client->next_hash;
if (prev) {
prev->next_hash = next;
} else {

61
src/sim_client.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef SIM_CLIENT_H
#define SIM_CLIENT_H
struct client_handle {
b32 valid;
u32 idx;
u32 gen;
};
struct sim_client {
b32 valid;
struct client_handle handle;
struct host_channel_id channel_id;
u64 channel_hash;
struct sim_client *next_free;
struct sim_client *next_hash;
struct sim_client *prev_hash;
struct entity_handle ent;
};
struct channel_lookup_bucket {
struct sim_client *first;
struct sim_client *last;
};
struct sim_client_store {
b32 valid;
struct arena arena;
struct channel_lookup_bucket *channel_lookup_buckets;
u64 num_channel_lookup_buckets;
struct sim_client *clients;
struct sim_client *first_free_client;
u64 clients_reserved;
};
INLINE struct sim_client *client_nil(void)
{
extern READONLY struct sim_client _g_client_nil;
return &_g_client_nil;
}
INLINE struct sim_client_store *client_store_nil(void)
{
extern READONLY struct sim_client_store _g_client_store_nil;
return &_g_client_store_nil;
}
struct sim_client_store *client_store_alloc(void);
void client_store_release(struct sim_client_store *store);
struct sim_client_store *client_store_from_client(struct sim_client *client);
struct sim_client *client_from_handle(struct sim_client_store *store, struct client_handle handle);
struct sim_client *client_from_channel_id(struct sim_client_store *store, struct host_channel_id channel_id);
struct sim_client *client_alloc(struct sim_client_store *store, struct host_channel_id channel_id);
void client_release(struct sim_client *client);
#endif