Pass LocalPlayer instance explicitly in more places, rather than relying on an implicit global

This commit is contained in:
UnknownShadow200 2024-04-25 08:39:34 +10:00
parent cbf8b01447
commit cf7b353572
12 changed files with 92 additions and 97 deletions

View File

@ -42,19 +42,18 @@ static void PerspectiveCamera_GetProjection(struct Matrix* proj) {
Gfx_CalcPerspectiveMatrix(proj, fovy, aspectRatio, (float)Game_ViewDistance);
}
static void PerspectiveCamera_GetView(struct Matrix* mat) {
static void PerspectiveCamera_GetView(struct LocalPlayer* p, struct Matrix* mat) {
Vec3 pos = Camera.CurrentPos;
Vec2 rot = Camera.Active->GetOrientation();
Vec2 rot = Camera.Active->GetOrientation(p);
Matrix_LookRot(mat, pos, rot);
Matrix_MulBy(mat, &Camera.TiltM);
}
static void PerspectiveCamera_GetPickedBlock(struct RayTracer* t) {
struct Entity* p = &LocalPlayer_Instance.Base;
Vec3 dir = Vec3_GetDirVector(p->Yaw * MATH_DEG2RAD, p->Pitch * MATH_DEG2RAD + Camera.TiltPitch);
Vec3 eyePos = Entity_GetEyePosition(p);
float reach = LocalPlayer_Instance.ReachDistance;
Picking_CalcPickedBlock(&eyePos, &dir, reach, t);
static void PerspectiveCamera_GetPickedBlock(struct LocalPlayer* p, struct RayTracer* t) {
struct Entity* e = &p->Base;
Vec3 dir = Vec3_GetDirVector(e->Yaw * MATH_DEG2RAD, e->Pitch * MATH_DEG2RAD + Camera.TiltPitch);
Vec3 eyePos = Entity_GetEyePosition(e);
Picking_CalcPickedBlock(&eyePos, &dir, p->ReachDistance, t);
}
#define CAMERA_SENSI_FACTOR (0.0002f / 3.0f * MATH_RAD2DEG)
@ -86,8 +85,8 @@ static Vec2 PerspectiveCamera_GetMouseDelta(double delta) {
return v;
}
static void PerspectiveCamera_UpdateMouseRotation(double delta) {
struct Entity* e = &LocalPlayer_Instance.Base;
static void PerspectiveCamera_UpdateMouseRotation(struct LocalPlayer* p, double delta) {
struct Entity* e = &p->Base;
struct LocationUpdate update;
Vec2 rot = PerspectiveCamera_GetMouseDelta(delta);
@ -108,15 +107,14 @@ static void PerspectiveCamera_UpdateMouseRotation(double delta) {
e->VTABLE->SetLocation(e, &update);
}
static void PerspectiveCamera_UpdateMouse(double delta) {
static void PerspectiveCamera_UpdateMouse(struct LocalPlayer* p, double delta) {
if (!Gui.InputGrab && Window_Main.Focused) Window_UpdateRawMouse();
PerspectiveCamera_UpdateMouseRotation(delta);
PerspectiveCamera_UpdateMouseRotation(p, delta);
cam_deltaX = 0; cam_deltaY = 0;
}
static void PerspectiveCamera_CalcViewBobbing(float t, float velTiltScale) {
struct LocalPlayer* p = &LocalPlayer_Instance;
static void PerspectiveCamera_CalcViewBobbing(struct LocalPlayer* p, float t, float velTiltScale) {
struct Entity* e = &p->Base;
struct Matrix tiltY, velX;
@ -148,19 +146,19 @@ static void PerspectiveCamera_CalcViewBobbing(float t, float velTiltScale) {
/*########################################################################################################################*
*---------------------------------------------------First person camera---------------------------------------------------*
*#########################################################################################################################*/
static Vec2 FirstPersonCamera_GetOrientation(void) {
struct Entity* p = &LocalPlayer_Instance.Base;
static Vec2 FirstPersonCamera_GetOrientation(struct LocalPlayer* p) {
struct Entity* e = &p->Base;
Vec2 v;
v.x = p->Yaw * MATH_DEG2RAD;
v.y = p->Pitch * MATH_DEG2RAD;
v.x = e->Yaw * MATH_DEG2RAD;
v.y = e->Pitch * MATH_DEG2RAD;
return v;
}
static Vec3 FirstPersonCamera_GetPosition(float t) {
struct Entity* p = &LocalPlayer_Instance.Base;
Vec3 camPos = Entity_GetEyePosition(p);
float yaw = p->Yaw * MATH_DEG2RAD;
PerspectiveCamera_CalcViewBobbing(t, 1);
static Vec3 FirstPersonCamera_GetPosition(struct LocalPlayer* p, float t) {
struct Entity* e = &p->Base;
Vec3 camPos = Entity_GetEyePosition(e);
float yaw = e->Yaw * MATH_DEG2RAD;
PerspectiveCamera_CalcViewBobbing(p, t, 1);
camPos.y += Camera.BobbingVer;
camPos.x += Camera.BobbingHor * (float)Math_Cos(yaw);
@ -185,11 +183,11 @@ static struct Camera cam_FirstPerson = {
#define DEF_ZOOM 3.0f
static float dist_third = DEF_ZOOM, dist_forward = DEF_ZOOM;
static Vec2 ThirdPersonCamera_GetOrientation(void) {
struct Entity* p = &LocalPlayer_Instance.Base;
static Vec2 ThirdPersonCamera_GetOrientation(struct LocalPlayer* p) {
struct Entity* e = &p->Base;
Vec2 v;
v.x = p->Yaw * MATH_DEG2RAD;
v.y = p->Pitch * MATH_DEG2RAD;
v.x = e->Yaw * MATH_DEG2RAD;
v.y = e->Pitch * MATH_DEG2RAD;
if (cam_isForwardThird) { v.x += MATH_PI; v.y = -v.y; }
v.x += cam_rotOffset.x * MATH_DEG2RAD;
@ -197,24 +195,24 @@ static Vec2 ThirdPersonCamera_GetOrientation(void) {
return v;
}
static float ThirdPersonCamera_GetZoom(void) {
static float ThirdPersonCamera_GetZoom(struct LocalPlayer* p) {
float dist = cam_isForwardThird ? dist_forward : dist_third;
/* Don't allow zooming out when -fly */
if (dist > DEF_ZOOM && !LocalPlayer_CheckCanZoom()) dist = DEF_ZOOM;
if (dist > DEF_ZOOM && !LocalPlayer_CheckCanZoom(p)) dist = DEF_ZOOM;
return dist;
}
static Vec3 ThirdPersonCamera_GetPosition(float t) {
struct Entity* p = &LocalPlayer_Instance.Base;
float dist = ThirdPersonCamera_GetZoom();
static Vec3 ThirdPersonCamera_GetPosition(struct LocalPlayer* p, float t) {
struct Entity* e = &p->Base;
float dist = ThirdPersonCamera_GetZoom(p);
Vec3 target, dir;
Vec2 rot;
PerspectiveCamera_CalcViewBobbing(t, dist);
target = Entity_GetEyePosition(p);
PerspectiveCamera_CalcViewBobbing(p, t, dist);
target = Entity_GetEyePosition(e);
target.y += Camera.BobbingVer;
rot = Camera.Active->GetOrientation();
rot = Camera.Active->GetOrientation(p);
dir = Vec3_GetDirVector(rot.x, rot.y);
Vec3_Negate(&dir, &dir);

View File

@ -8,6 +8,7 @@ Copyright 2014-2023 ClassiCube | Licensed under BSD-3
struct RayTracer;
struct Camera;
struct IGameComponent;
struct LocalPlayer;
extern struct IGameComponent Camera_Component;
/* Shared data for cameras. */
@ -45,16 +46,16 @@ struct Camera {
/* Calculates the current projection matrix of this camera. */
void (*GetProjection)(struct Matrix* proj);
/* Calculates the current modelview matrix of this camera. */
void (*GetView)(struct Matrix* view);
void (*GetView)(struct LocalPlayer* p, struct Matrix* view);
/* Returns the current orientation of the camera. */
Vec2 (*GetOrientation)(void);
Vec2 (*GetOrientation)(struct LocalPlayer* p);
/* Returns the current interpolated position of the camera. */
Vec3 (*GetPosition)(float t);
Vec3 (*GetPosition)(struct LocalPlayer* p, float t);
/* Called to update the camera's state. */
/* Typically, this is used to adjust yaw/pitch based on accumulated mouse movement. */
void (*UpdateMouse)(double delta);
void (*UpdateMouse)(struct LocalPlayer* p, double delta);
/* Called when mouse/pointer has moved. */
void (*OnRawMovement)(float deltaX, float deltaY);
/* Called when user closes all menus, and is interacting with camera again. */
@ -63,7 +64,7 @@ struct Camera {
void (*LoseFocus)(void);
/* Calculates selected block in the world, based on camera's current state */
void (*GetPickedBlock)(struct RayTracer* t);
void (*GetPickedBlock)(struct LocalPlayer* p, struct RayTracer* t);
/* Zooms the camera in or out when scrolling mouse wheel. */
cc_bool (*Zoom)(float amount);

View File

@ -615,13 +615,12 @@ struct IGameComponent TabList_Component = {
*#########################################################################################################################*/
struct LocalPlayer LocalPlayer_Instance;
static cc_bool hackPermMsgs;
float LocalPlayer_JumpHeight(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
float LocalPlayer_JumpHeight(struct LocalPlayer* p) {
return (float)PhysicsComp_CalcMaxHeight(p->Physics.JumpVel);
}
void LocalPlayer_SetInterpPosition(float t) {
struct LocalPlayer* p = &LocalPlayer_Instance;
void LocalPlayer_SetInterpPosition(struct LocalPlayer* p, float t) {
if (!(p->Hacks.WOMStyleHacks && p->Hacks.Noclip)) {
Vec3_Lerp(&p->Base.Position, &p->Base.prev.pos, &p->Base.next.pos, t);
}
@ -789,8 +788,7 @@ static void LocalPlayer_Init(void) {
hackPermMsgs = Options_GetBool(OPT_HACK_PERM_MSGS, true);
}
void LocalPlayer_ResetJumpVelocity(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
void LocalPlayer_ResetJumpVelocity(struct LocalPlayer* p) {
cc_bool higher = HacksComp_CanJumpHigher(&p->Hacks);
p->Physics.JumpVel = higher ? p->Physics.UserJumpVel : 0.42f;
@ -801,7 +799,7 @@ static void LocalPlayer_Reset(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
p->ReachDistance = 5.0f;
Vec3_Set(p->Base.Velocity, 0,0,0);
LocalPlayer_ResetJumpVelocity();
LocalPlayer_ResetJumpVelocity(p);
}
static void LocalPlayer_OnNewMap(void) {
@ -816,8 +814,7 @@ static void LocalPlayer_OnNewMap(void) {
}
static cc_bool LocalPlayer_IsSolidCollide(BlockID b) { return Blocks.Collide[b] == COLLIDE_SOLID; }
static void LocalPlayer_DoRespawn(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
static void LocalPlayer_DoRespawn(struct LocalPlayer* p) {
struct LocationUpdate update;
struct AABB bb;
Vec3 spawn = p->Spawn;
@ -863,10 +860,9 @@ static void LocalPlayer_DoRespawn(void) {
p->Base.OnGround = Entity_TouchesAny(&bb, LocalPlayer_IsSolidCollide);
}
cc_bool LocalPlayer_HandleRespawn(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
cc_bool LocalPlayer_HandleRespawn(struct LocalPlayer* p) {
if (p->Hacks.CanRespawn) {
LocalPlayer_DoRespawn();
LocalPlayer_DoRespawn(p);
return true;
} else if (!p->_warnedRespawn) {
p->_warnedRespawn = true;
@ -875,8 +871,7 @@ cc_bool LocalPlayer_HandleRespawn(void) {
return false;
}
cc_bool LocalPlayer_HandleSetSpawn(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
cc_bool LocalPlayer_HandleSetSpawn(struct LocalPlayer* p) {
if (p->Hacks.CanRespawn) {
if (!p->Hacks.CanNoclip && !p->Base.OnGround) {
@ -896,11 +891,10 @@ cc_bool LocalPlayer_HandleSetSpawn(void) {
p->SpawnYaw = p->Base.Yaw;
p->SpawnPitch = p->Base.Pitch;
}
return LocalPlayer_HandleRespawn();
return LocalPlayer_HandleRespawn(p);
}
cc_bool LocalPlayer_HandleFly(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
cc_bool LocalPlayer_HandleFly(struct LocalPlayer* p) {
if (p->Hacks.CanFly && p->Hacks.Enabled) {
HacksComp_SetFlying(&p->Hacks, !p->Hacks.Flying);
return true;
@ -911,8 +905,7 @@ cc_bool LocalPlayer_HandleFly(void) {
return false;
}
cc_bool LocalPlayer_HandleNoclip(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
cc_bool LocalPlayer_HandleNoclip(struct LocalPlayer* p) {
if (p->Hacks.CanNoclip && p->Hacks.Enabled) {
if (p->Hacks.WOMStyleHacks) return true; /* don't handle this here */
if (p->Hacks.Noclip) p->Base.Velocity.y = 0;
@ -926,8 +919,7 @@ cc_bool LocalPlayer_HandleNoclip(void) {
return false;
}
cc_bool LocalPlayer_HandleJump(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
cc_bool LocalPlayer_HandleJump(struct LocalPlayer* p) {
struct HacksComp* hacks = &p->Hacks;
struct PhysicsComp* physics = &p->Physics;
int maxJumps;
@ -945,8 +937,7 @@ cc_bool LocalPlayer_HandleJump(void) {
return false;
}
cc_bool LocalPlayer_CheckCanZoom(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
cc_bool LocalPlayer_CheckCanZoom(struct LocalPlayer* p) {
if (p->Hacks.CanFly) return true;
if (!p->_warnedZoom) {
@ -956,8 +947,7 @@ cc_bool LocalPlayer_CheckCanZoom(void) {
return false;
}
void LocalPlayer_MoveToSpawn(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
void LocalPlayer_MoveToSpawn(struct LocalPlayer* p) {
struct LocationUpdate update;
update.flags = LU_HAS_POS | LU_HAS_YAW | LU_HAS_PITCH;
@ -967,11 +957,10 @@ void LocalPlayer_MoveToSpawn(void) {
p->Base.VTABLE->SetLocation(&p->Base, &update);
/* TODO: This needs to be before new map... */
Camera.CurrentPos = Camera.Active->GetPosition(0.0f);
Camera.CurrentPos = Camera.Active->GetPosition(p, 0.0f);
}
void LocalPlayer_CalcDefaultSpawn(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
void LocalPlayer_CalcDefaultSpawn(struct LocalPlayer* p) {
float x = (World.Width / 2) + 0.5f;
float z = (World.Length / 2) + 0.5f;

View File

@ -236,18 +236,18 @@ struct LocalPlayer {
extern struct LocalPlayer LocalPlayer_Instance;
/* Returns how high (in blocks) the player can jump. */
float LocalPlayer_JumpHeight(void);
float LocalPlayer_JumpHeight(struct LocalPlayer* p);
/* Interpolates current position and orientation between Interp.Prev and Interp.Next */
void LocalPlayer_SetInterpPosition(float t);
void LocalPlayer_ResetJumpVelocity(void);
cc_bool LocalPlayer_CheckCanZoom(void);
void LocalPlayer_SetInterpPosition(struct LocalPlayer* p, float t);
void LocalPlayer_ResetJumpVelocity(struct LocalPlayer* p);
cc_bool LocalPlayer_CheckCanZoom(struct LocalPlayer* p);
/* Moves local player back to spawn point. */
void LocalPlayer_MoveToSpawn(void);
void LocalPlayer_CalcDefaultSpawn(void);
void LocalPlayer_MoveToSpawn(struct LocalPlayer* p);
void LocalPlayer_CalcDefaultSpawn(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleRespawn(void);
cc_bool LocalPlayer_HandleSetSpawn(void);
cc_bool LocalPlayer_HandleFly(void);
cc_bool LocalPlayer_HandleNoclip(void);
cc_bool LocalPlayer_HandleJump(void);
cc_bool LocalPlayer_HandleRespawn(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleSetSpawn(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleFly(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleNoclip(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleJump(struct LocalPlayer* p);
#endif

View File

@ -18,6 +18,7 @@
#include "Camera.h"
#include "Particle.h"
#include "Options.h"
#include "Entity.h"
cc_bool EnvRenderer_Legacy, EnvRenderer_Minimal;
@ -322,7 +323,8 @@ void EnvRenderer_RenderSkybox(void) {
/* Rotate around camera */
pos = Camera.CurrentPos;
Vec3_Set(Camera.CurrentPos, 0,0,0);
Camera.Active->GetView(&view); Matrix_MulBy(&m, &view);
Camera.Active->GetView(&LocalPlayer_Instance, &view);
Matrix_MulBy(&m, &view);
Camera.CurrentPos = pos;
Gfx_LoadMatrix(MATRIX_VIEW, &m);

View File

@ -84,8 +84,8 @@ cc_result Map_LoadFrom(const cc_string* path) {
if (res) Logger_SysWarn2(res, "decoding", path);
World_SetNewMap(World.Blocks, World.Width, World.Height, World.Length);
if (calcDefaultSpawn) LocalPlayer_CalcDefaultSpawn();
LocalPlayer_MoveToSpawn();
if (calcDefaultSpawn) LocalPlayer_CalcDefaultSpawn(&LocalPlayer_Instance);
LocalPlayer_MoveToSpawn(&LocalPlayer_Instance);
relPath = *path;
Utils_UNSAFE_GetFilename(&relPath);

View File

@ -479,7 +479,7 @@ void Game_SetFpsLimit(int method) {
}
static void UpdateViewMatrix(void) {
Camera.Active->GetView(&Gfx.View);
Camera.Active->GetView(&LocalPlayer_Instance, &Gfx.View);
FrustumCulling_CalcFrustumEquations(&Gfx.Projection, &Gfx.View);
}
@ -624,7 +624,7 @@ static void Game_RenderFrame(double delta) {
Game_Vertices = 0;
if (Input.Sources & INPUT_SOURCE_GAMEPAD) Gamepad_Tick(delta);
Camera.Active->UpdateMouse(delta);
Camera.Active->UpdateMouse(&LocalPlayer_Instance, delta);
if (!Window_Main.Focused && !Gui.InputGrab) Gui_ShowPauseMenu();
@ -635,9 +635,9 @@ static void Game_RenderFrame(double delta) {
PerformScheduledTasks(delta);
entTask = tasks[entTaskI];
t = (float)(entTask.accumulator / entTask.interval);
LocalPlayer_SetInterpPosition(t);
LocalPlayer_SetInterpPosition(&LocalPlayer_Instance, t);
Camera.CurrentPos = Camera.Active->GetPosition(t);
Camera.CurrentPos = Camera.Active->GetPosition(&LocalPlayer_Instance, t);
/* NOTE: EnvRenderer_UpdateFog also also sets clear color */
EnvRenderer_UpdateFog();
UpdateViewMatrix();
@ -648,7 +648,7 @@ static void Game_RenderFrame(double delta) {
Gfx_ClearBuffers(GFX_BUFFER_COLOR | GFX_BUFFER_DEPTH);
if (!Gui_GetBlocksWorld()) {
Camera.Active->GetPickedBlock(&Game_SelectedPos); /* TODO: only pick when necessary */
Camera.Active->GetPickedBlock(&LocalPlayer_Instance, &Game_SelectedPos); /* TODO: only pick when necessary */
Camera_KeyLookUpdate(delta);
InputHandler_Tick();

View File

@ -1038,16 +1038,18 @@ static void HandleHotkeyDown(int key) {
}
static cc_bool HandleLocalPlayerKey(int key) {
struct LocalPlayer* p = &LocalPlayer_Instance;
if (KeyBind_Claims(KEYBIND_RESPAWN, key)) {
return LocalPlayer_HandleRespawn();
return LocalPlayer_HandleRespawn(p);
} else if (KeyBind_Claims(KEYBIND_SET_SPAWN, key)) {
return LocalPlayer_HandleSetSpawn();
return LocalPlayer_HandleSetSpawn(p);
} else if (KeyBind_Claims(KEYBIND_FLY, key)) {
return LocalPlayer_HandleFly();
return LocalPlayer_HandleFly(p);
} else if (KeyBind_Claims(KEYBIND_NOCLIP, key)) {
return LocalPlayer_HandleNoclip();
return LocalPlayer_HandleNoclip(p);
} else if (KeyBind_Claims(KEYBIND_JUMP, key)) {
return LocalPlayer_HandleJump();
return LocalPlayer_HandleJump(p);
}
return false;
}

View File

@ -3147,7 +3147,10 @@ static void HacksSettingsScreen_SetClipping(const cc_string* v) {
Camera.Clipping = Menu_SetBool(v, OPT_CAMERA_CLIPPING);
}
static void HacksSettingsScreen_GetJump(cc_string* v) { String_AppendFloat(v, LocalPlayer_JumpHeight(), 3); }
static void HacksSettingsScreen_GetJump(cc_string* v) {
String_AppendFloat(v, LocalPlayer_JumpHeight(&LocalPlayer_Instance), 3);
}
static void HacksSettingsScreen_SetJump(const cc_string* v) {
cc_string str; char strBuffer[STRING_SIZE];
struct PhysicsComp* physics;

View File

@ -1221,7 +1221,7 @@ static void CPE_HackControl(cc_uint8* data) {
jumpHeight = Stream_GetU16_BE(data + 5);
if (jumpHeight == UInt16_MaxValue) { /* special value of -1 to reset default */
LocalPlayer_ResetJumpVelocity();
LocalPlayer_ResetJumpVelocity(p);
} else {
p->Physics.JumpVel = PhysicsComp_CalcJumpVelocity(jumpHeight / 32.0f);
p->Physics.ServerJumpVel = p->Physics.JumpVel;

View File

@ -2002,8 +2002,8 @@ static void GeneratingScreen_EndGeneration(void) {
Gen_Blocks = NULL;
World.Seed = Gen_Seed;
LocalPlayer_CalcDefaultSpawn();
LocalPlayer_MoveToSpawn();
LocalPlayer_CalcDefaultSpawn(&LocalPlayer_Instance);
LocalPlayer_MoveToSpawn(&LocalPlayer_Instance);
}
static void GeneratingScreen_Update(void* screen, double delta) {

View File

@ -407,7 +407,7 @@ static void MPConnection_Tick(struct ScheduledTask* task) {
if (cpe_needD3Fix && lastOpcode == OPCODE_HACK_CONTROL && (opcode == 0x00 || opcode == 0xFF)) {
Platform_LogConst("Skipping invalid HackControl byte from D3 server");
readCur++;
LocalPlayer_ResetJumpVelocity();
LocalPlayer_ResetJumpVelocity(&LocalPlayer_Instance);
continue;
}