From 1bec4922f907372bbe67bda7955561f90aaad9a1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 23 Oct 2022 08:46:52 +1100 Subject: [PATCH] Further simplify LocationUpdate by also integrating interpolation flag --- src/Camera.c | 2 +- src/Chat.c | 2 +- src/Entity.c | 18 +++++++++--------- src/Entity.h | 13 +++++++++++-- src/EntityComponents.c | 10 ++++++---- src/EntityComponents.h | 4 ++-- src/Input.c | 4 ++-- src/Protocol.c | 38 ++++++++++++++++++-------------------- 8 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/Camera.c b/src/Camera.c index 489e9547c..320fba830 100644 --- a/src/Camera.c +++ b/src/Camera.c @@ -95,7 +95,7 @@ static void PerspectiveCamera_UpdateMouseRotation(double delta) { if (update.pitch >= 90.0f && update.pitch <= 270.0f) { update.pitch = p->Interp.Next.Pitch < 180.0f ? 90.0f : 270.0f; } - e->VTABLE->SetLocation(e, &update, false); + e->VTABLE->SetLocation(e, &update); } static void PerspectiveCamera_UpdateMouse(double delta) { diff --git a/src/Chat.c b/src/Chat.c index ece8d8256..f6f5238ff 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -633,7 +633,7 @@ static void TeleportCommand_Execute(const cc_string* args, int argsCount) { update.flags = LU_INCLUDES_POS; update.pos = v; - e->VTABLE->SetLocation(e, &update, false); + e->VTABLE->SetLocation(e, &update); } static struct ChatCommand TeleportCommand = { diff --git a/src/Entity.c b/src/Entity.c index fb0220301..3a47fe756 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -30,7 +30,7 @@ const char* const ShadowMode_Names[SHADOW_MODE_COUNT] = { "None", "SnapToBlock", /*########################################################################################################################* *---------------------------------------------------------Entity----------------------------------------------------------* *#########################################################################################################################*/ -static PackedCol Entity_GetCol(struct Entity* e) { +static PackedCol Entity_GetColor(struct Entity* e) { Vec3 eyePos = Entity_GetEyePosition(e); IVec3 pos; IVec3_Floor(&pos, &eyePos); return Lighting.Color(pos.X, pos.Y, pos.Z); @@ -816,9 +816,9 @@ static void LocalPlayer_InputUp(void* obj, int key) { LocalPlayer_InputSet(key, false); } -static void LocalPlayer_SetLocation(struct Entity* e, struct LocationUpdate* update, cc_bool interpolate) { +static void LocalPlayer_SetLocation(struct Entity* e, struct LocationUpdate* update) { struct LocalPlayer* p = (struct LocalPlayer*)e; - LocalInterpComp_SetLocation(&p->Interp, update, interpolate); + LocalInterpComp_SetLocation(&p->Interp, update); } static void LocalPlayer_Tick(struct Entity* e, double delta) { @@ -889,7 +889,7 @@ static void LocalPlayer_GetMovement(float* xMoving, float* zMoving) { } static const struct EntityVTABLE localPlayer_VTABLE = { - LocalPlayer_Tick, Player_Despawn, LocalPlayer_SetLocation, Entity_GetCol, + LocalPlayer_Tick, Player_Despawn, LocalPlayer_SetLocation, Entity_GetColor, LocalPlayer_RenderModel, LocalPlayer_RenderName }; static void LocalPlayer_Init(void) { @@ -992,7 +992,7 @@ static void LocalPlayer_DoRespawn(void) { update.pos = spawn; update.yaw = p->SpawnYaw; update.pitch = p->SpawnPitch; - p->Base.VTABLE->SetLocation(&p->Base, &update, false); + p->Base.VTABLE->SetLocation(&p->Base, &update); Vec3_Set(p->Base.Velocity, 0,0,0); /* Update onGround, otherwise if 'respawn' then 'space' is pressed, you still jump into the air if onGround was true before */ @@ -1103,7 +1103,7 @@ void LocalPlayer_MoveToSpawn(void) { update.yaw = p->SpawnYaw; update.pitch = p->SpawnPitch; - p->Base.VTABLE->SetLocation(&p->Base, &update, false); + p->Base.VTABLE->SetLocation(&p->Base, &update); /* TODO: This needs to be before new map... */ Camera.CurrentPos = Camera.Active->GetPosition(0.0f); } @@ -1124,9 +1124,9 @@ void LocalPlayer_CalcDefaultSpawn(void) { *#########################################################################################################################*/ struct NetPlayer NetPlayers_List[ENTITIES_SELF_ID]; -static void NetPlayer_SetLocation(struct Entity* e, struct LocationUpdate* update, cc_bool interpolate) { +static void NetPlayer_SetLocation(struct Entity* e, struct LocationUpdate* update) { struct NetPlayer* p = (struct NetPlayer*)e; - NetInterpComp_SetLocation(&p->Interp, update, interpolate); + NetInterpComp_SetLocation(&p->Interp, update); } static void NetPlayer_Tick(struct Entity* e, double delta) { @@ -1160,7 +1160,7 @@ static void NetPlayer_RenderName(struct Entity* e) { } static const struct EntityVTABLE netPlayer_VTABLE = { - NetPlayer_Tick, Player_Despawn, NetPlayer_SetLocation, Entity_GetCol, + NetPlayer_Tick, Player_Despawn, NetPlayer_SetLocation, Entity_GetColor, NetPlayer_RenderModel, NetPlayer_RenderName }; void NetPlayer_Init(struct NetPlayer* p) { diff --git a/src/Entity.h b/src/Entity.h index 412df270f..1fb4250ff 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -31,13 +31,22 @@ extern const char* const ShadowMode_Names[SHADOW_MODE_COUNT]; enum EntityType { ENTITY_TYPE_NONE, ENTITY_TYPE_PLAYER }; +/* Which fields are included/valid in a LocationUpdate */ #define LU_INCLUDES_POS 0x01 #define LU_INCLUDES_PITCH 0x02 #define LU_INCLUDES_YAW 0x04 #define LU_INCLUDES_ROTX 0x08 #define LU_INCLUDES_ROTZ 0x10 -#define LU_FLAG_RELATIVEPOS 0x20 +/* If set, then new position is calculated by adding current position to update->pos */ +/* If not set, then new position is just update->pos */ +#define LU_FLAG_RELATIVEPOS 0x20 +/* TODO fill this in when implemented */ +#define LU_FLAG_X 0x40 +/* If set, then linearly interpolates between current and new state */ +/* If not set, then current state is immediately updated to new state */ +#define LU_FLAG_INTERPOLATE 0x80 + /* Represents a location update for an entity. Can be a relative position, full position, and/or an orientation update. */ struct LocationUpdate { Vec3 pos; @@ -49,7 +58,7 @@ struct Entity; struct EntityVTABLE { void (*Tick)(struct Entity* e, double delta); void (*Despawn)(struct Entity* e); - void (*SetLocation)(struct Entity* e, struct LocationUpdate* update, cc_bool interpolate); + void (*SetLocation)(struct Entity* e, struct LocationUpdate* update); PackedCol (*GetCol)(struct Entity* e); void (*RenderModel)(struct Entity* e, double deltaTime, float t); void (*RenderName)(struct Entity* e); diff --git a/src/EntityComponents.c b/src/EntityComponents.c index 5c1c395c1..5060158b0 100644 --- a/src/EntityComponents.c +++ b/src/EntityComponents.c @@ -348,10 +348,11 @@ static void NetInterpComp_AddState(struct NetInterpComp* interp, struct InterpSt interp->States[interp->StatesCount] = state; interp->StatesCount++; } -void NetInterpComp_SetLocation(struct NetInterpComp* interp, struct LocationUpdate* update, cc_bool interpolate) { +void NetInterpComp_SetLocation(struct NetInterpComp* interp, struct LocationUpdate* update) { struct InterpState last = interp->Cur; struct InterpState* cur = &interp->Cur; - cc_uint8 flags = update->flags; + cc_uint8 flags = update->flags; + cc_bool interpolate = flags & LU_FLAG_INTERPOLATE; if (flags & LU_INCLUDES_POS) InterpComp_SetPos(cur, update); if (flags & LU_INCLUDES_ROTX) cur->RotX = Math_ClampAngle(update->rotX); @@ -400,11 +401,12 @@ static void LocalInterpComp_Angle(float* prev, float* next, float value, cc_bool if (!interpolate) *prev = value; } -void LocalInterpComp_SetLocation(struct InterpComp* interp, struct LocationUpdate* update, cc_bool interpolate) { +void LocalInterpComp_SetLocation(struct InterpComp* interp, struct LocationUpdate* update) { struct Entity* entity = &LocalPlayer_Instance.Base; struct InterpState* prev = &interp->Prev; struct InterpState* next = &interp->Next; - cc_uint8 flags = update->flags; + cc_uint8 flags = update->flags; + cc_bool interpolate = flags & LU_FLAG_INTERPOLATE; float yOffset; if (flags & LU_INCLUDES_POS) { diff --git a/src/EntityComponents.h b/src/EntityComponents.h index b719ac8b8..efa71392e 100644 --- a/src/EntityComponents.h +++ b/src/EntityComponents.h @@ -88,7 +88,7 @@ struct InterpComp { InterpComp_Layout }; void InterpComp_LerpAngles(struct InterpComp* interp, struct Entity* entity, float t); -void LocalInterpComp_SetLocation(struct InterpComp* interp, struct LocationUpdate* update, cc_bool interpolate); +void LocalInterpComp_SetLocation(struct InterpComp* interp, struct LocationUpdate* update); void LocalInterpComp_AdvanceState(struct InterpComp* interp); /* Entity component that performs interpolation for network players */ @@ -100,7 +100,7 @@ struct NetInterpComp { struct InterpState States[10]; }; -void NetInterpComp_SetLocation(struct NetInterpComp* interp, struct LocationUpdate* update, cc_bool interpolate); +void NetInterpComp_SetLocation(struct NetInterpComp* interp, struct LocationUpdate* update); void NetInterpComp_AdvanceState(struct NetInterpComp* interp); /* Entity component that draws square and circle shadows beneath entities */ diff --git a/src/Input.c b/src/Input.c index 67de0b277..cc3a009c1 100644 --- a/src/Input.c +++ b/src/Input.c @@ -654,7 +654,7 @@ static cc_bool PushbackPlace(struct AABB* blockBB) { update.flags = LU_INCLUDES_POS; update.pos = pos; - p->VTABLE->SetLocation(p, &update, false); + p->VTABLE->SetLocation(p, &update); return true; } @@ -713,7 +713,7 @@ static cc_bool CheckIsFree(BlockID block) { update.flags = LU_INCLUDES_POS; update.pos = nextPos; - p->VTABLE->SetLocation(p, &update, false); + p->VTABLE->SetLocation(p, &update); return true; } diff --git a/src/Protocol.c b/src/Protocol.c index 946154cc3..5e5758343 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -134,7 +134,7 @@ static void CheckName(EntityID id, cc_string* name, cc_string* skin) { RemoveEndPlus(skin); } -static void Classic_ReadAbsoluteLocation(cc_uint8* data, EntityID id, cc_bool interpolate); +static void Classic_ReadAbsoluteLocation(cc_uint8* data, EntityID id, cc_uint8 flags); static void AddEntity(cc_uint8* data, EntityID id, const cc_string* name, const cc_string* skin, cc_bool readPosition) { struct LocalPlayer* p = &LocalPlayer_Instance; struct Entity* e; @@ -153,7 +153,7 @@ static void AddEntity(cc_uint8* data, EntityID id, const cc_string* name, const Entity_SetName(e, name); if (!readPosition) return; - Classic_ReadAbsoluteLocation(data, id, false); + Classic_ReadAbsoluteLocation(data, id, 0); if (id != ENTITIES_SELF_ID) return; p->Spawn = p->Base.Position; @@ -168,9 +168,9 @@ void Protocol_RemoveEntity(EntityID id) { Entities_Remove(id); } -static void UpdateLocation(EntityID id, struct LocationUpdate* update, cc_bool interpolate) { +static void UpdateLocation(EntityID id, struct LocationUpdate* update) { struct Entity* e = Entities.List[id]; - if (e) { e->VTABLE->SetLocation(e, update, interpolate); } + if (e) { e->VTABLE->SetLocation(e, update); } } static void UpdateUserType(struct HacksComp* hacks, cc_uint8 value) { @@ -602,41 +602,41 @@ static void Classic_AddEntity(cc_uint8* data) { static void Classic_EntityTeleport(cc_uint8* data) { EntityID id = *data++; - Classic_ReadAbsoluteLocation(data, id, true); + Classic_ReadAbsoluteLocation(data, id, LU_FLAG_INTERPOLATE); } static void Classic_RelPosAndOrientationUpdate(cc_uint8* data) { struct LocationUpdate update; EntityID id = data[0]; - update.flags = LU_INCLUDES_POS | LU_INCLUDES_YAW | LU_INCLUDES_PITCH | LU_FLAG_RELATIVEPOS; + update.flags = LU_INCLUDES_POS | LU_INCLUDES_YAW | LU_INCLUDES_PITCH | LU_FLAG_RELATIVEPOS | LU_FLAG_INTERPOLATE; update.pos.X = (cc_int8)data[1] / 32.0f; update.pos.Y = (cc_int8)data[2] / 32.0f; update.pos.Z = (cc_int8)data[3] / 32.0f; update.yaw = Math_Packed2Deg(data[4]); update.pitch = Math_Packed2Deg(data[5]); - UpdateLocation(id, &update, true); + UpdateLocation(id, &update); } static void Classic_RelPositionUpdate(cc_uint8* data) { struct LocationUpdate update; EntityID id = data[0]; - update.flags = LU_INCLUDES_POS | LU_FLAG_RELATIVEPOS; + update.flags = LU_INCLUDES_POS | LU_FLAG_RELATIVEPOS | LU_FLAG_INTERPOLATE; update.pos.X = (cc_int8)data[1] / 32.0f; update.pos.Y = (cc_int8)data[2] / 32.0f; update.pos.Z = (cc_int8)data[3] / 32.0f; - UpdateLocation(id, &update, true); + UpdateLocation(id, &update); } static void Classic_OrientationUpdate(cc_uint8* data) { struct LocationUpdate update; EntityID id = data[0]; - update.flags = LU_INCLUDES_YAW | LU_INCLUDES_PITCH; + update.flags = LU_INCLUDES_YAW | LU_INCLUDES_PITCH| LU_FLAG_INTERPOLATE; update.yaw = Math_Packed2Deg(data[1]); update.pitch = Math_Packed2Deg(data[2]); - UpdateLocation(id, &update, true); + UpdateLocation(id, &update); } static void Classic_RemoveEntity(cc_uint8* data) { @@ -680,11 +680,9 @@ static void Classic_SetPermission(cc_uint8* data) { HacksComp_RecheckFlags(hacks); } -static void Classic_ReadAbsoluteLocation(cc_uint8* data, EntityID id, cc_bool interpolate) { +static void Classic_ReadAbsoluteLocation(cc_uint8* data, EntityID id, cc_uint8 flags) { struct LocationUpdate update; int x, y, z; - Vec3 pos; - float yaw, pitch; if (cpe_extEntityPos) { x = (int)Stream_GetU32_BE(&data[0]); @@ -705,7 +703,7 @@ static void Classic_ReadAbsoluteLocation(cc_uint8* data, EntityID id, cc_bool in /* so to simplify things, just always add 22 to Y*/ if (id == ENTITIES_SELF_ID) y += 22; - update.flags = LU_INCLUDES_POS | LU_INCLUDES_PITCH | LU_INCLUDES_YAW; + update.flags = LU_INCLUDES_POS | LU_INCLUDES_PITCH | LU_INCLUDES_YAW | flags; update.pos.X = x/32.0f; update.pos.Y = y/32.0f; update.pos.Z = z/32.0f; @@ -713,7 +711,7 @@ static void Classic_ReadAbsoluteLocation(cc_uint8* data, EntityID id, cc_bool in update.pitch = Math_Packed2Deg(*data++); if (id == ENTITIES_SELF_ID) classic_receivedFirstPos = true; - UpdateLocation(id, &update, interpolate); + UpdateLocation(id, &update); } static void Classic_Reset(void) { @@ -1286,13 +1284,13 @@ static void CPE_SetEntityProperty(cc_uint8* data) { switch (type) { case 0: - update.flags = LU_INCLUDES_ROTX; + update.flags = LU_INCLUDES_ROTX | LU_FLAG_INTERPOLATE; update.rotX = (float)value; break; case 1: - update.flags = LU_INCLUDES_YAW; + update.flags = LU_INCLUDES_YAW | LU_FLAG_INTERPOLATE; update.yaw = (float)value; break; case 2: - update.flags = LU_INCLUDES_ROTZ; + update.flags = LU_INCLUDES_ROTZ | LU_FLAG_INTERPOLATE; update.rotZ = (float)value; break; case 3: @@ -1312,7 +1310,7 @@ static void CPE_SetEntityProperty(cc_uint8* data) { default: return; } - e->VTABLE->SetLocation(e, &update, true); + e->VTABLE->SetLocation(e, &update); } static void CPE_TwoWayPing(cc_uint8* data) {