power_play/src/pp/pp_ent.c
2025-08-01 04:36:37 -05:00

801 lines
24 KiB
C

/* Id magic number constants (to be used in conjunction with ent ids in deterministic id combinations) */
#define SIM_ENT_CONTACT_BASIS_Uid (UID(0x6a2a5d2dbecf534f, 0x0a8ca7c372a015af))
#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
* ========================== */
Entity *sim_ent_alloc_raw(Snapshot *ss, Entity *parent, EntityId id)
{
Assert(parent->valid);
Assert(ss->valid);
Assert(ss == parent->ss);
Entity *ent;
if (ss->first_free_ent > 0 && ss->first_free_ent < ss->num_ents_reserved) {
/* Reuse from free list */
ent = &ss->ents[ss->first_free_ent];
ss->first_free_ent = ent->next_free;
} else {
/* Make new */
ent = PushStructNoZero(ss->ents_arena, Entity);
++ss->num_ents_reserved;
}
*ent = *sim_ent_nil();
ent->ss = ss;
ent->valid = 1;
ent->owner = ss->client->player_id;
ent->_is_xform_dirty = 1;
++ss->num_ents_allocated;
sim_ent_set_id(ent, id);
sim_ent_link_parent(ent, parent);
return ent;
}
/* Allocates a new entity that will not sync */
Entity *sim_ent_alloc_local(Entity *parent)
{
Snapshot *ss = parent->ss;
Entity *e = sim_ent_alloc_raw(ss, parent, sim_ent_random_id());
e->owner = ss->local_player;
return e;
}
Entity *sim_ent_alloc_local_with_id(Entity *parent, EntityId id)
{
Snapshot *ss = parent->ss;
Entity *e = sim_ent_alloc_raw(ss, parent, id);
e->owner = ss->local_player;
return e;
}
/* Allocates a new entity to be synced to clients */
Entity *sim_ent_alloc_sync_src(Entity *parent)
{
Snapshot *ss = parent->ss;
Entity *e = sim_ent_alloc_raw(ss, parent, sim_ent_random_id());
sim_ent_enable_prop(e, SEPROP_SYNC_SRC);
e->owner = ss->local_player;
return e;
}
Entity *sim_ent_alloc_sync_src_with_id(Entity *parent, EntityId id)
{
Snapshot *ss = parent->ss;
Entity *e = sim_ent_alloc_raw(ss, parent, id);
sim_ent_enable_prop(e, SEPROP_SYNC_SRC);
e->owner = ss->local_player;
return e;
}
/* Allocates a new entity that will sync with incoming net src ents containing id, and coming from the specified owner */
Entity *sim_ent_alloc_sync_dst(Entity *parent, EntityId ent_id, EntityId owner_id)
{
Snapshot *ss = parent->ss;
Entity *e = sim_ent_alloc_raw(ss, parent, ent_id);
sim_ent_enable_prop(e, SEPROP_SYNC_DST);
e->owner = owner_id;
return e;
}
void sim_ent_release_raw(Entity *ent)
{
Snapshot *ss = ent->ss;
/* Release children */
Entity *child = sim_ent_from_id(ss, ent->first);
while (child->valid) {
Entity *next = sim_ent_from_id(ss, child->next);
sim_ent_release_raw(child);
child = next;
}
/* Release uid */
sim_ent_set_id(ent, SIM_ENT_NIL_ID);
/* Release */
ent->valid = 0;
ent->next_free = ss->first_free_ent;
ss->first_free_ent = index_from_ent(ss, ent);
--ss->num_ents_allocated;
}
void sim_ent_release(Entity *ent)
{
Snapshot *ss = ent->ss;
Entity *parent = sim_ent_from_id(ss, ent->parent);
if (parent->valid) {
sim_ent_unlink_from_parent(ent);
}
sim_ent_release_raw(ent);
}
void sim_ent_release_all_with_prop(Snapshot *ss, EntProp prop)
{
TempArena scratch = BeginScratchNoConflict();
Entity **ents_to_release = PushDry(scratch.arena, Entity *);
u64 ents_to_release_count = 0;
for (u64 ent_index = 0; ent_index < ss->num_ents_reserved; ++ent_index) {
Entity *ent = &ss->ents[ent_index];
if (ent->valid && sim_ent_has_prop(ent, prop)) {
*PushStructNoZero(scratch.arena, Entity *) = ent;
++ents_to_release_count;
}
}
/* 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) {
Entity *ent = ents_to_release[i];
if (ent->valid && !ent->is_root && !sim_ent_has_prop(ent, SEPROP_CMD) && !sim_ent_has_prop(ent, SEPROP_PLAYER)) {
sim_ent_release(ent);
}
}
EndScratch(scratch);
}
/* ========================== *
* Activate
* ========================== */
void sim_ent_activate(Entity *ent, u64 current_tick)
{
sim_ent_enable_prop(ent, SEPROP_ACTIVE);
ent->activation_tick = current_tick;
++ent->continuity_gen;
}
/* ========================== *
* Entity id
* ========================== */
internal EntBin *bin_from_id(Snapshot *ss, EntityId id)
{
return &ss->id_bins[id.uid.lo % ss->num_id_bins];
}
/* NOTE: This should only really happen during ent allocation (it doesn't make sense for an allocated ent's id to change) */
void sim_ent_set_id(Entity *ent, EntityId id)
{
Snapshot *ss = ent->ss;
EntityId old_id = ent->id;
if (!sim_ent_id_eq(old_id, id)) {
/* Release old from lookup */
if (!sim_ent_id_is_nil(old_id)) {
EntBin *bin = bin_from_id(ss, old_id);
u32 prev_index = 0;
u32 next_index = 0;
u32 search_index = bin->first;
Entity *prev = sim_ent_nil();
Entity *next = sim_ent_nil();
Entity *search = ent_from_index(ss, search_index);
while (search->valid) {
next_index = search->next_in_id_bin;
next = ent_from_index(ss, next_index);
if (sim_ent_id_eq(search->id, old_id)) {
break;
}
prev_index = search_index;
prev = search;
search_index = next_index;
search = next;
}
/* Old id not in bin, this should be impossible. */
Assert(search->valid);
if (prev->valid) {
prev->next_in_id_bin = next_index;
} else {
bin->first = next_index;
}
if (next->valid) {
next->prev_in_id_bin = prev_index;
} else {
bin->last = prev_index;
}
}
/* Insert new id into lookup */
if (!sim_ent_id_is_nil(id)) {
#if RtcIsEnabled
{
Entity *existing = sim_ent_from_id(ss, id);
/* Collision should be extremely unlikely under normal circumstances, there's probably a logic error somewhere. */
Assert(!existing->valid);
}
#endif
EntBin *bin = bin_from_id(ss, id);
u32 ent_index = index_from_ent(ss, ent);
Entity *last = ent_from_index(ss, bin->last);
if (last->valid) {
last->next_in_id_bin = ent_index;
ent->prev_in_id_bin = bin->last;
} else {
bin->first = ent_index;
ent->prev_in_id_bin = 0;
}
bin->last = ent_index;
}
ent->id = id;
}
}
Entity *sim_ent_from_id(Snapshot *ss, EntityId id)
{
Entity *result = sim_ent_nil();
if (!sim_ent_id_is_nil(id) && ss->valid) {
EntBin *bin = bin_from_id(ss, id);
for (Entity *e = ent_from_index(ss, bin->first); e->valid; e = ent_from_index(ss, e->next_in_id_bin)) {
if (sim_ent_id_eq(e->id, id)) {
result = e;
break;
}
}
}
return result;
}
EntityId sim_ent_random_id(void)
{
EntityId result = ZI;
result.uid = UidFromTrueRand();
return result;
}
/* Returns the deterministic id of the contact constraint ent id that should be produced from e0 & e1 colliding */
EntityId sim_ent_contact_constraint_id_from_contacting_ids(EntityId player_id, EntityId id0, EntityId id1)
{
EntityId result = ZI;
result.uid = SIM_ENT_CONTACT_BASIS_Uid;
result.uid = CombineUid(result.uid, player_id.uid);
result.uid = CombineUid(result.uid, id0.uid);
result.uid = CombineUid(result.uid, id1.uid);
return result;
}
/* Returns the deterministic id of the debug contact constraint ent id that should be produced from e0 & e1 colliding */
EntityId sim_ent_collision_debug_id_from_ids(EntityId player_id, EntityId id0, EntityId id1)
{
EntityId result = ZI;
result.uid = SIM_ENT_COLLISION_DEBUG_BASIS_Uid;
result.uid = CombineUid(result.uid, player_id.uid);
result.uid = CombineUid(result.uid, id0.uid);
result.uid = CombineUid(result.uid, id1.uid);
return result;
}
/* Returns the deterministic id of the tile chunk that should be produced at chunk pos */
EntityId sim_ent_tile_chunk_id_from_tile_chunk_index(Vec2I32 chunk_index)
{
EntityId result = ZI;
result.uid = SIM_ENT_TILE_CHUNK_BASIS_Uid;
result.uid = CombineUid(result.uid, UID(RandU64FromSeed(chunk_index.x), RandU64FromSeed(chunk_index.y)));
return result;
}
/* ========================== *
* Entity query
* ========================== */
Entity *sim_ent_find_first_match_one(Snapshot *ss, EntProp prop)
{
u64 count = ss->num_ents_reserved;
Entity *entities = ss->ents;
for (u64 ent_index = 0; ent_index < count; ++ent_index) {
Entity *ent = &entities[ent_index];
if (ent->valid && sim_ent_has_prop(ent, prop)) {
return ent;
}
}
return sim_ent_nil();
}
Entity *sim_ent_find_first_match_all(Snapshot *ss, EntPropArray props)
{
u64 count = ss->num_ents_reserved;
Entity *entities = ss->ents;
for (u64 ent_index = 0; ent_index < count; ++ent_index) {
Entity *ent = &entities[ent_index];
if (ent->valid) {
b32 all = 1;
for (u64 i = 0; i < props.count; ++i) {
if (!sim_ent_has_prop(ent, props.props[i])) {
all = 0;
break;
}
}
if (all) {
return ent;
}
}
}
return sim_ent_nil();
}
/* ========================== *
* Entity tree
* ========================== */
void sim_ent_link_parent(Entity *ent, Entity *parent)
{
Snapshot *ss = ent->ss;
Entity *old_parent = sim_ent_from_id(ss, ent->parent);
if (old_parent->valid) {
/* Unlink from current parent */
sim_ent_unlink_from_parent(ent);
}
EntityId ent_id = ent->id;
EntityId last_child_id = parent->last;
Entity *last_child = sim_ent_from_id(ss, last_child_id);
if (last_child->valid) {
ent->prev = last_child_id;
last_child->next = ent_id;
} else {
parent->first = ent_id;
}
parent->last = ent_id;
if (parent->is_root) {
ent->is_top = 1;
ent->top = ent_id;
} else {
ent->top = parent->top;
}
ent->parent = parent->id;
}
/* NOTE: Entity will be dangling after calling this, should re-link to root ent. */
void sim_ent_unlink_from_parent(Entity *ent)
{
Snapshot *ss = ent->ss;
EntityId parent_id = ent->parent;
Entity *parent = sim_ent_from_id(ss, parent_id);
Entity *prev = sim_ent_from_id(ss, ent->prev);
Entity *next = sim_ent_from_id(ss, ent->next);
/* Unlink from parent & siblings */
if (prev->valid) {
prev->next = next->id;
} else {
parent->first = next->id;
}
if (next->valid) {
next->prev = prev->id;
} else {
parent->last = prev->id;
}
ent->prev = SIM_ENT_NIL_ID;
ent->next = SIM_ENT_NIL_ID;
}
/* ========================== *
* Entity xform
* ========================== */
internal 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) {
break;
} else {
child->_is_xform_dirty = 1;
sim_ent_mark_child_xforms_dirty(ss, child);
}
}
}
internal Xform sim_ent_get_xform_internal(Snapshot *ss, Entity *ent)
{
Xform xf;
if (ent->_is_xform_dirty) {
if (ent->is_top) {
xf = ent->_local_xform;
} else {
Entity *parent = sim_ent_from_id(ss, ent->parent);
xf = sim_ent_get_xform_internal(ss, parent);
xf = MulXform(xf, ent->_local_xform);
ent->_xform = xf;
ent->_is_xform_dirty = 0;
}
ent->_xform = xf;
ent->_is_xform_dirty = 0;
} else {
xf = ent->_xform;
}
return xf;
}
Xform sim_ent_get_xform(Entity *ent)
{
Xform xf;
if (ent->_is_xform_dirty) {
if (ent->is_top) {
xf = ent->_local_xform;
} else {
Snapshot *ss = ent->ss;
Entity *parent = sim_ent_from_id(ss, ent->parent);
xf = sim_ent_get_xform_internal(ss, parent);
xf = MulXform(xf, ent->_local_xform);
ent->_xform = xf;
ent->_is_xform_dirty = 0;
}
ent->_xform = xf;
ent->_is_xform_dirty = 0;
} else {
xf = ent->_xform;
}
return xf;
}
Xform sim_ent_get_local_xform(Entity *ent)
{
return ent->_local_xform;
}
void sim_ent_set_xform(Entity *ent, Xform xf)
{
if (!EqXform(xf, ent->_xform)) {
Snapshot *ss = ent->ss;
/* Update local xform */
if (ent->is_top) {
ent->_local_xform = xf;
} else {
Entity *parent = sim_ent_from_id(ss, ent->parent);
Xform parent_global = sim_ent_get_xform_internal(ss, parent);
ent->_local_xform = MulXform(InvertXform(parent_global), xf);
}
ent->_xform = xf;
ent->_is_xform_dirty = 0;
sim_ent_mark_child_xforms_dirty(ss, ent);
}
}
void sim_ent_set_local_xform(Entity *ent, Xform xf)
{
if (!EqXform(xf, ent->_local_xform)) {
ent->_local_xform = xf;
ent->_is_xform_dirty = 1;
sim_ent_mark_child_xforms_dirty(ent->ss, ent);
}
}
/* ========================== *
* Entity movement
* ========================== */
void sim_ent_set_linear_velocity(Entity *ent, Vec2 velocity)
{
if (sim_ent_has_prop(ent, SEPROP_KINEMATIC) || sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
ent->linear_velocity = ClampVec2Len(velocity, SIM_MAX_LINEAR_VELOCITY);
}
}
void sim_ent_set_angular_velocity(Entity *ent, f32 velocity)
{
if (sim_ent_has_prop(ent, SEPROP_KINEMATIC) || sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
ent->angular_velocity = ClampF32(velocity, -SIM_MAX_ANGULAR_VELOCITY, SIM_MAX_ANGULAR_VELOCITY);
}
}
void sim_ent_apply_linear_impulse(Entity *ent, Vec2 impulse, Vec2 point)
{
if (sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
Xform xf = sim_ent_get_xform(ent);
Vec2 center = xf.og;
f32 scale = AbsF32(DeterminantFromXform(xf));
f32 inv_mass = 1.f / (ent->mass_unscaled * scale);
f32 inv_inertia = 1.f / (ent->inertia_unscaled * scale);
Vec2 vcp = SubVec2(point, center);
sim_ent_set_linear_velocity(ent, AddVec2(ent->linear_velocity, MulVec2(impulse, inv_mass)));
sim_ent_set_angular_velocity(ent, WedgeVec2(vcp, impulse) * inv_inertia);
}
}
void sim_ent_apply_linear_impulse_to_center(Entity *ent, Vec2 impulse)
{
if (sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
Xform xf = sim_ent_get_xform(ent);
f32 scale = AbsF32(DeterminantFromXform(xf));
f32 inv_mass = 1.f / (ent->mass_unscaled * scale);
sim_ent_set_linear_velocity(ent, AddVec2(ent->linear_velocity, MulVec2(impulse, inv_mass)));
}
}
void sim_ent_apply_force_to_center(Entity *ent, Vec2 force)
{
if (sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
ent->force = AddVec2(ent->force, force);
}
}
void sim_ent_apply_angular_impulse(Entity *ent, f32 impulse)
{
if (sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
Xform xf = sim_ent_get_xform(ent);
f32 scale = AbsF32(DeterminantFromXform(xf));
f32 inv_inertia = 1.f / (ent->inertia_unscaled * scale);
sim_ent_set_angular_velocity(ent, ent->angular_velocity + impulse * inv_inertia);
}
}
void sim_ent_apply_torque(Entity *ent, f32 torque)
{
if (sim_ent_has_prop(ent, SEPROP_DYNAMIC)) {
ent->torque += torque;
}
}
/* ========================== *
* Tile
* ========================== */
Entity *sim_tile_chunk_from_chunk_index(Snapshot *ss, Vec2I32 chunk_index)
{
EntityId chunk_id = sim_ent_tile_chunk_id_from_tile_chunk_index(chunk_index);
Entity *chunk_ent = sim_ent_from_id(ss, chunk_id);
return chunk_ent;
}
Entity *sim_tile_chunk_from_world_tile_index(Snapshot *ss, Vec2I32 world_tile_index)
{
Vec2I32 chunk_index = sim_tile_chunk_index_from_world_tile_index(world_tile_index);
Entity *chunk_ent = sim_tile_chunk_from_chunk_index(ss, chunk_index);
return chunk_ent;
}
TileKind sim_get_chunk_tile(Entity *chunk_ent, Vec2I32 local_tile_index)
{
TileKind result = chunk_ent->tile_chunk_tiles[local_tile_index.x + (local_tile_index.y * SIM_TILES_PER_CHUNK_SQRT)];
return result;
}
/* ========================== *
* Entity lerp
* ========================== */
void sim_ent_lerp(Entity *e, Entity *e0, Entity *e1, f64 blend)
{
if (sim_ent_is_valid_and_active(e0) && sim_ent_is_valid_and_active(e1)
&& sim_ent_id_eq(e0->id, e1->id)
&& e0->continuity_gen == e1->continuity_gen) {
e->_local_xform = LerpXform(e0->_local_xform, e1->_local_xform, blend);
if (e->is_top) {
/* TODO: Cache parent & child xforms in sim */
Xform e0_xf = sim_ent_get_xform(e0);
Xform e1_xf = sim_ent_get_xform(e1);
sim_ent_set_xform(e, LerpXform(e0_xf, e1_xf, blend));
}
e->control_force = LerpF32(e0->control_force, e1->control_force, blend);
e->control_torque = LerpF32(e0->control_torque, e1->control_torque, blend);
e->linear_velocity = LerpVec2(e0->linear_velocity, e1->linear_velocity, blend);
e->angular_velocity = LerpAngleF32(e0->angular_velocity, e1->angular_velocity, blend);
e->control.move = LerpVec2(e0->control.move, e1->control.move, blend);
e->control.focus = LerpVec2(e0->control.focus, e1->control.focus, blend);
e->sprite_local_xform = LerpXform(e0->sprite_local_xform, e1->sprite_local_xform, blend);
e->animation_last_frame_change_time_ns = LerpI64(e0->animation_last_frame_change_time_ns, e1->animation_last_frame_change_time_ns, (f64)blend);
e->animation_frame = (u32)RoundF32ToI32(LerpF32(e0->animation_frame, e1->animation_frame, blend));
e->camera_quad_xform = LerpXform(e0->camera_quad_xform, e1->camera_quad_xform, blend);
e->camera_xform_target = LerpXform(e0->camera_xform_target, e1->camera_xform_target, blend);
e->shake = LerpF32(e0->shake, e1->shake, blend);
e->tracer_gradient_start = LerpVec2(e0->tracer_gradient_start, e1->tracer_gradient_start, blend);
e->tracer_gradient_end = LerpVec2(e0->tracer_gradient_end, e1->tracer_gradient_end, blend);
}
}
/* ========================== *
* Entity sync
* ========================== */
/* Walks a local & remote ent tree and allocates any missing net dst ents from remote src ents */
void sim_ent_sync_alloc_tree(Entity *local_parent, Entity *remote, EntityId remote_player)
{
__prof;
if (sim_ent_has_prop(remote, SEPROP_SYNC_SRC)) {
Snapshot *local_ss = local_parent->ss;
Snapshot *remote_ss = remote->ss;
EntityId id = remote->id;
Entity *local_ent = sim_ent_from_id(local_ss, id);
if (!local_ent->valid) {
local_ent = sim_ent_alloc_sync_dst(local_parent, id, remote_player);
}
for (Entity *remote_child = sim_ent_from_id(remote_ss, remote->first); remote_child->valid; remote_child = sim_ent_from_id(remote_ss, remote_child->next)) {
sim_ent_sync_alloc_tree(local_ent, remote_child, remote_player);
}
}
}
/* Copies data between two synced entities */
void sim_ent_sync(Entity *local, Entity *remote)
{
Entity old = *local;
CopyStruct(local, remote);
/* Why would 2 ents w/ different uids ever be synced? */
Assert(sim_ent_id_eq(local->id, old.id));
local->ss = old.ss;
local->id = old.id;
/* Keep local tree */
local->parent = old.parent;
local->prev = old.prev;
local->next = old.next;
local->first = old.first;
local->last = old.last;
local->top = old.top;
local->owner = old.owner;
/* Keep indices */
local->next_in_id_bin = old.next_in_id_bin;
local->prev_in_id_bin = old.prev_in_id_bin;
local->next_free = old.next_free;
sim_ent_disable_prop(local, SEPROP_SYNC_SRC);
sim_ent_enable_prop(local, SEPROP_SYNC_DST);
}
#if 1
/* ========================== *
* Entity encode
* ========================== */
void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1)
{
Snapshot *ss = e1->ss;
/* FIXME: Things like xforms need to be retreived manually rather than memcopied. */
/* TODO: Granular delta encoding */
u64 pos = 0;
e1->ss = e0->ss;
while (pos < sizeof(*e1)) {
u64 chunk_size = MinU64(pos + 8, sizeof(*e1)) - pos;
u8 *chunk0 = (u8 *)e0 + pos;
u8 *chunk1 = (u8 *)e1 + pos;
if (BB_WriteBit(bw, !EqBytes(chunk0, chunk1, chunk_size))) {
u64 bits = 0;
CopyBytes(&bits, chunk1, chunk_size);
BB_WriteUBits(bw, bits, 64);
}
pos += 8;
}
e1->ss = ss;
}
/* ========================== *
* Entity decode
* ========================== */
void sim_ent_decode(BB_Reader *br, Entity *e)
{
Snapshot *old_ss = e->ss;
{
u64 pos = 0;
while (pos < sizeof(*e)) {
u8 *chunk = (u8 *)e + pos;
if (BB_ReadBit(br)) {
u64 chunk_size = MinU64(pos + 8, sizeof(*e)) - pos;
u64 bits = BB_ReadUBits(br, 64);
CopyBytes(chunk, &bits, chunk_size);
}
pos += 8;
}
}
e->ss = old_ss;
}
#else
/* ========================== *
* Entity encode
* ========================== */
void sim_ent_encode(BB_Writer *bw, Entity *e0, Entity *e1)
{
Snapshot *ss = e1->ss;
/* FIXME: Things like xforms need to be retreived manually rather than memcopied.
* This will also be true for things like ent handles once uids are implemented. */
/* TODO: Granular delta encoding */
u64 pos = 0;
e1->ss = e0->ss;
while (pos < sizeof(*e1)) {
u64 chunk_size = MinU64(pos + 8, sizeof(*e1)) - pos;
u8 *chunk0 = (u8 *)e0 + pos;
u8 *chunk1 = (u8 *)e1 + pos;
if (EqBytes(chunk0, chunk1, chunk_size)) {
BB_WriteBit(bw, 0);
} else {
BB_WriteBit(bw, 1);
u64 bits = 0;
CopyBytes(&bits, chunk1, chunk_size);
BB_WriteUBits(bw, bits, 64);
}
pos += 8;
}
e1->ss = ss;
}
/* ========================== *
* Entity decode
* ========================== */
void sim_ent_decode(BB_Reader *br, Entity *e)
{
Entity decoded = *e;
{
u64 pos = 0;
while (pos < sizeof(decoded)) {
u8 *chunk = (u8 *)&decoded + pos;
if (BB_ReadBit(br)) {
u64 chunk_size = MinU64(pos + 8, sizeof(decoded)) - pos;
u64 bits = BB_ReadUBits(br, 64);
CopyBytes(chunk, &bits, chunk_size);
}
pos += 8;
}
}
decoded.ss = e->ss;
EntityId old_id = e->id;
EntityId new_id = decoded.id;
CopyStruct(e, &decoded);
e->id = old_id;
if (!sim_ent_id_eq(old_id, new_id)) {
sim_ent_set_id(e, new_id);
}
}
#endif