diff --git a/src/pp/pp_ent.c b/src/pp/pp_ent.c index 27ae04f1..e30acc43 100644 --- a/src/pp/pp_ent.c +++ b/src/pp/pp_ent.c @@ -3,23 +3,8 @@ #define SIM_ENT_COLLISION_DEBUG_BASIS_Uid (UID(0x302c01182013bb02, 0x570bd270399d11a5)) #define SIM_ENT_TILE_CHUNK_BASIS_Uid (UID(0x3ce42de071dd226b, 0x9b566f7df30c813a)) -internal u32 index_from_ent(Snapshot *ss, Entity *ent) -{ - return ent - ss->ents; -} - -internal Entity *ent_from_index(Snapshot *ss, u32 index) -{ - if (index > 0 && index < ss->num_ents_reserved) { - return &ss->ents[index]; - } else { - return sim_ent_nil(); - } -} - -/* ========================== * - * Entity allocation - * ========================== */ +//////////////////////////////// +//~ Acquire Entity *sim_ent_acquire_raw(Snapshot *ss, Entity *parent, EntityId id) { @@ -95,6 +80,9 @@ Entity *sim_ent_acquire_sync_dst(Entity *parent, EntityId ent_id, EntityId owner return e; } +//////////////////////////////// +//~ Release + void sim_ent_release_raw(Entity *ent) { Snapshot *ss = ent->ss; @@ -153,9 +141,8 @@ void sim_ent_release_all_with_prop(Snapshot *ss, EntProp prop) EndScratch(scratch); } -/* ========================== * - * Activate - * ========================== */ +//////////////////////////////// +//~ Activate void sim_ent_activate(Entity *ent, u64 current_tick) { @@ -164,11 +151,24 @@ void sim_ent_activate(Entity *ent, u64 current_tick) ++ent->continuity_gen; } -/* ========================== * - * Entity id - * ========================== */ +//////////////////////////////// +//~ Entity id -internal EntBin *bin_from_id(Snapshot *ss, EntityId id) +u32 index_from_ent(Snapshot *ss, Entity *ent) +{ + return ent - ss->ents; +} + +Entity *ent_from_index(Snapshot *ss, u32 index) +{ + if (index > 0 && index < ss->num_ents_reserved) { + return &ss->ents[index]; + } else { + return sim_ent_nil(); + } +} + +EntBin *bin_from_id(Snapshot *ss, EntityId id) { return &ss->id_bins[id.uid.lo % ss->num_id_bins]; } @@ -297,9 +297,8 @@ EntityId sim_ent_tile_chunk_id_from_tile_chunk_index(Vec2I32 chunk_index) return result; } -/* ========================== * - * Entity query - * ========================== */ +//////////////////////////////// +//~ Query Entity *sim_ent_find_first_match_one(Snapshot *ss, EntProp prop) { @@ -336,9 +335,8 @@ Entity *sim_ent_find_first_match_all(Snapshot *ss, EntPropArray props) return sim_ent_nil(); } -/* ========================== * - * Entity tree - * ========================== */ +//////////////////////////////// +//~ Tree void sim_ent_link_parent(Entity *ent, Entity *parent) { @@ -396,11 +394,10 @@ void sim_ent_unlink_from_parent(Entity *ent) ent->next = SIM_ENT_NIL_ID; } -/* ========================== * - * Entity xform - * ========================== */ +//////////////////////////////// +//~ Xform -internal void sim_ent_mark_child_xforms_dirty(Snapshot *ss, Entity *ent) +void sim_ent_mark_child_xforms_dirty(Snapshot *ss, Entity *ent) { for (Entity *child = sim_ent_from_id(ss, ent->first); child->valid; child = sim_ent_from_id(ss, child->next)) { if (child->_is_xform_dirty) { @@ -412,7 +409,7 @@ internal void sim_ent_mark_child_xforms_dirty(Snapshot *ss, Entity *ent) } } -internal Xform sim_ent_get_xform_internal(Snapshot *ss, Entity *ent) +Xform sim_ent_get_xform_internal(Snapshot *ss, Entity *ent) { Xform xf; if (ent->_is_xform_dirty) { @@ -487,9 +484,8 @@ void sim_ent_set_local_xform(Entity *ent, Xform xf) } } -/* ========================== * - * Entity movement - * ========================== */ +//////////////////////////////// +//~ Movement void sim_ent_set_linear_velocity(Entity *ent, Vec2 velocity) { @@ -555,9 +551,8 @@ void sim_ent_apply_torque(Entity *ent, f32 torque) } } -/* ========================== * - * Tile - * ========================== */ +//////////////////////////////// +//~ Tile Entity *sim_tile_chunk_from_chunk_index(Snapshot *ss, Vec2I32 chunk_index) { @@ -579,9 +574,8 @@ TileKind sim_get_chunk_tile(Entity *chunk_ent, Vec2I32 local_tile_index) return result; } -/* ========================== * - * Entity lerp - * ========================== */ +//////////////////////////////// +//~ Lerp void sim_ent_lerp(Entity *e, Entity *e0, Entity *e1, f64 blend) { @@ -619,9 +613,8 @@ void sim_ent_lerp(Entity *e, Entity *e0, Entity *e1, f64 blend) } } -/* ========================== * - * Entity sync - * ========================== */ +//////////////////////////////// +//~ Sync /* Walks a local & remote ent tree and allocates any missing net dst ents from remote src ents */ void sim_ent_sync_acquire_tree(Entity *local_parent, Entity *remote, EntityId remote_player) @@ -672,20 +665,12 @@ void sim_ent_sync(Entity *local, Entity *remote) sim_ent_enable_prop(local, SEPROP_SYNC_DST); } - - - - - - - - +//////////////////////////////// +//~ Encode / decode #if 1 -/* ========================== * - * Entity encode - * ========================== */ +//- Encode void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1) { @@ -710,9 +695,7 @@ void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1) e1->ss = ss; } -/* ========================== * - * Entity decode - * ========================== */ +//- Decode void sim_ent_decode(BB_Reader *br, Entity *e) { @@ -735,9 +718,7 @@ void sim_ent_decode(BB_Reader *br, Entity *e) #else -/* ========================== * - * Entity encode - * ========================== */ +//- Encode void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1) { @@ -768,9 +749,7 @@ void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1) e1->ss = ss; } -/* ========================== * - * Entity decode - * ========================== */ +//- Decode void sim_ent_decode(BB_Reader *br, Entity *e) { diff --git a/src/pp/pp_ent.h b/src/pp/pp_ent.h index 506a3139..3e56b9b2 100644 --- a/src/pp/pp_ent.h +++ b/src/pp/pp_ent.h @@ -1,6 +1,9 @@ #define SIM_ENT_NIL_ID ((EntityId) { UID(0, 0) }) #define SIM_ENT_ROOT_ID ((EntityId) { UID(0xaaaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaaa) }) +//////////////////////////////// +//~ Entity props + typedef i32 EntProp; enum { SEPROP_ACTIVE, SEPROP_RELEASE, @@ -60,11 +63,13 @@ typedef i32 EntProp; enum { SEPROP_COUNT }; +//////////////////////////////// +//~ Entity + Struct(Entity) { Snapshot *ss; - /* ====================================================================== */ - /* Metadata */ + //- Metadata b32 valid; /* Is this ent allocated in memory that can be written to (can always be read) */ EntityId id; @@ -92,8 +97,7 @@ Struct(Entity) { u32 prev_in_id_bin; u32 next_free; - /* ====================================================================== */ - /* Sync */ + //- Sync /* SEPROP_SYNC_SRC */ /* SEPROP_SYNC_DST */ @@ -104,28 +108,24 @@ Struct(Entity) { /* Id of the player that should predict simulation of this this entity locally */ EntityId predictor; - /* ====================================================================== */ - /* Position */ + //- Position /* Use xform getters & setters to access. */ Xform _local_xform; /* Transform in relation to parent ent (or the world if ent has no parent) */ Xform _xform; /* Calculated from ent tree */ b32 _is_xform_dirty; - /* ====================================================================== */ - /* Activation */ + //- Activation /* If 0, the ent will auto activate at start of next tick if not already active. */ u64 activation_tick; - /* ====================================================================== */ - /* Layer */ + //- Layer i32 layer; i32 final_layer; /* Calculated each tick from ent tree */ - /* ====================================================================== */ - /* Cmd */ + //- Cmd /* SEPROP_CMD */ @@ -141,8 +141,7 @@ Struct(Entity) { /* Chat cmd */ //String cmd_chat_msg; - /* ====================================================================== */ - /* Chat */ + //- Chat /* SEPROP_CHAT */ @@ -150,8 +149,7 @@ Struct(Entity) { //String chat_msg; - /* ====================================================================== */ - /* Tile */ + //- Tile /* SEPROP_TILE_CHUNK */ @@ -159,8 +157,7 @@ Struct(Entity) { u8 tile_chunk_tiles[SIM_TILES_PER_CHUNK_SQRT * SIM_TILES_PER_CHUNK_SQRT]; Vec2I32 tile_chunk_index; - /* ====================================================================== */ - /* Client */ + //- Client /* SEPROP_PLAYER */ @@ -183,8 +180,7 @@ Struct(Entity) { i64 player_last_rtt_ns; f64 player_average_rtt_seconds; - /* ====================================================================== */ - /* Collider */ + //- Collider Vec2 collision_dir; /* If set, then only collisions coming from this direction will generate contacts (used for walls to prevent ghost collisions) */ CLD_Shape local_collider; @@ -195,8 +191,7 @@ Struct(Entity) { SpaceEntryHandle space_handle; - /* ====================================================================== */ - /* Constraints / joints */ + //- Constraints / joints /* SEPROP_CONSTRAINT_CONTACT */ ContactConstraint contact_constraint_data; @@ -210,8 +205,7 @@ Struct(Entity) { /* SEPROP_WELD_JOINT */ WeldJoint weld_joint_data; - /* ====================================================================== */ - /* Control */ + //- Control /* SEPROP_CONTROLLED */ @@ -227,8 +221,7 @@ Struct(Entity) { EntityId move_joint; EntityId aim_joint; - /* ====================================================================== */ - /* Physics */ + //- Physics /* SEPROP_DYNAMIC */ @@ -253,8 +246,7 @@ Struct(Entity) { f32 linear_damping; f32 angular_damping; - /* ====================================================================== */ - /* Sprite */ + //- Sprite S_Tag sprite; String sprite_span_name; @@ -265,43 +257,37 @@ Struct(Entity) { Xform sprite_local_xform; /* Sprite transform in relation to ent */ - /* ====================================================================== */ - /* Animation */ + //- Animation /* SEPROP_ANIMATING */ i64 animation_last_frame_change_time_ns; u32 animation_frame; - /* ====================================================================== */ - /* Attachment */ + //- Attachment /* SEPROP_ATTACHED */ /* Slice name on the parent ent's sprite to attach to */ String attach_slice; - /* ====================================================================== */ - /* Equip */ + //- Equip EntityId equipped; - /* ====================================================================== */ - /* Chucker */ + //- Chucker /* SEPROP_WEAPON_CHUCKER */ EntityId chucker_zone; EntityId chucker_joint; - /* ====================================================================== */ - /* Chucker zone */ + //- Chucker zone /* SEPROP_CHUCKER_ZONE */ EntityId chucker_zone_ent; u64 chucker_zone_ent_tick; - /* ====================================================================== */ - /* Triggerable */ + //- Triggerable i32 num_primary_triggers; i32 num_secondary_triggers; @@ -312,8 +298,7 @@ Struct(Entity) { i64 last_primary_fire_ns; i64 last_secondary_fire_ns; - /* ====================================================================== */ - /* Trigger */ + //- Trigger /* How many times has this trigger been triggered this tick */ i64 triggered_count; @@ -322,8 +307,7 @@ Struct(Entity) { //EntityId trigger_out_left; //EntityId trigger_out_right; - /* ====================================================================== */ - /* Bullet */ + //- Bullet EntityId bullet_src; EntityId bullet_tracer; @@ -335,14 +319,12 @@ Struct(Entity) { f32 bullet_explosion_radius; b32 bullet_has_hit; - /* ====================================================================== */ - /* Explosion */ + //- Explosion f32 explosion_strength; f32 explosion_radius; - /* ====================================================================== */ - /* Tracer */ + //- Tracer /* SEPROP_TRACER */ @@ -354,8 +336,7 @@ Struct(Entity) { Vec2 tracer_gradient_start; Vec2 tracer_gradient_end; - /* ====================================================================== */ - /* Quake */ + //- Quake /* SEPROP_QUAKE */ @@ -363,8 +344,7 @@ Struct(Entity) { f32 quake_frequency; f32 quake_fade; /* How much intensity to lose per second */ - /* ====================================================================== */ - /* Testing */ + //- Testing /* SEPROP_TEST */ b32 test_initialized; @@ -376,8 +356,7 @@ Struct(Entity) { MIX_TrackDesc sound_desc; MIX_Handle sound_handle; - /* ====================================================================== */ - /* Camera */ + //- Camera /* SEPROP_CAMERA */ EntityId camera_follow; @@ -406,19 +385,14 @@ Struct(EntBin) { u32 last; }; -/* ========================== * - * Nil - * ========================== */ - Inline Entity *sim_ent_nil(void) { extern Readonly Entity **_g_sim_ent_nil; return *_g_sim_ent_nil; } -/* ========================== * - * Id helpers - * ========================== */ +//////////////////////////////// +//~ Id helpers Inline b32 sim_ent_id_eq(EntityId a, EntityId b) { @@ -430,9 +404,8 @@ Inline b32 sim_ent_id_is_nil(EntityId id) return EqUid(id.uid, SIM_ENT_NIL_ID.uid); } -/* ========================== * - * Property helpers - * ========================== */ +//////////////////////////////// +//~ Property helpers Inline void sim_ent_enable_prop(Entity *ent, EntProp prop) { @@ -483,11 +456,9 @@ Inline b32 sim_ent_should_simulate(Entity *ent) return result; } -/* ========================== * - * Entity functions - * ========================== */ +//////////////////////////////// +//~ Acquire operations -/* Acquire */ Entity *sim_ent_acquire_raw(Snapshot *ss, Entity *parent, EntityId id); Entity *sim_ent_acquire_local(Entity *parent); Entity *sim_ent_acquire_local_with_id(Entity *parent, EntityId id); @@ -495,14 +466,24 @@ Entity *sim_ent_acquire_sync_src(Entity *parent); Entity *sim_ent_acquire_sync_src_with_id(Entity *parent, EntityId id); Entity *sim_ent_acquire_sync_dst(Entity *parent, EntityId ent_id, EntityId owner_id); +//////////////////////////////// +//~ Release operations + void sim_ent_release_raw(Entity *ent); void sim_ent_release(Entity *ent); void sim_ent_release_all_with_prop(Snapshot *ss, EntProp prop); -/* Activate */ +//////////////////////////////// +//~ Activate operations + void sim_ent_activate(Entity *ent, u64 current_tick); -/* Id */ +//////////////////////////////// +//~ Id operations + +u32 index_from_ent(Snapshot *ss, Entity *ent); +Entity *ent_from_index(Snapshot *ss, u32 index); +EntBin *bin_from_id(Snapshot *ss, EntityId id); void sim_ent_set_id(Entity *ent, EntityId id); Entity *sim_ent_from_id(Snapshot *ss, EntityId id); EntityId sim_ent_random_id(void); @@ -510,21 +491,31 @@ EntityId sim_ent_contact_constraint_id_from_contacting_ids(EntityId player_id, E EntityId sim_ent_collision_debug_id_from_ids(EntityId player_id, EntityId id0, EntityId id1); EntityId sim_ent_tile_chunk_id_from_tile_chunk_index(Vec2I32 chunk_start); -/* Query */ +//////////////////////////////// +//~ Query operations + Entity *sim_ent_find_first_match_one(Snapshot *ss, EntProp prop); Entity *sim_ent_find_first_match_all(Snapshot *ss, EntPropArray props); -/* Tree */ +//////////////////////////////// +//~ Tree operations + void sim_ent_link_parent(Entity *parent, Entity *child); void sim_ent_unlink_from_parent(Entity *ent); -/* Xform */ +//////////////////////////////// +//~ Xform operations + +void sim_ent_mark_child_xforms_dirty(Snapshot *ss, Entity *ent); +Xform sim_ent_get_xform_internal(Snapshot *ss, Entity *ent); Xform sim_ent_get_xform(Entity *ent); Xform sim_ent_get_local_xform(Entity *ent); void sim_ent_set_xform(Entity *ent, Xform xf); void sim_ent_set_local_xform(Entity *ent, Xform xf); -/* Movement */ +//////////////////////////////// +//~ Movement operations + void sim_ent_set_linear_velocity(Entity *ent, Vec2 velocity); void sim_ent_set_angular_velocity(Entity *ent, f32 velocity); void sim_ent_apply_linear_impulse(Entity *ent, Vec2 impulse, Vec2 world_point); @@ -533,18 +524,26 @@ void sim_ent_apply_force_to_center(Entity *ent, Vec2 force); void sim_ent_apply_angular_impulse(Entity *ent, f32 impulse); void sim_ent_apply_torque(Entity *ent, f32 torque); -/* Tile */ +//////////////////////////////// +//~ Tile operations + Entity *sim_tile_chunk_from_chunk_index(Snapshot *ss, Vec2I32 chunk_index); Entity *sim_tile_chunk_from_world_tile_index(Snapshot *ss, Vec2I32 world_tile_index); TileKind sim_get_chunk_tile(Entity *chunk_ent, Vec2I32 local_tile_index); -/* Lerp */ +//////////////////////////////// +//~ Lerp operations + void sim_ent_lerp(Entity *e, Entity *e0, Entity *e1, f64 blend); -/* Sync */ +//////////////////////////////// +//~ Sync operations + void sim_ent_sync_acquire_tree(Entity *local_parent, Entity *remote, EntityId remote_player); void sim_ent_sync(Entity *local, Entity *remote); -/* Encode / decode */ +//////////////////////////////// +//~ Encode / decode operations + void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1); void sim_ent_decode(BB_Reader *br, Entity *e);