From 5851c0f2c681ba6e77835d9fe948cb4831a938f8 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Wed, 28 Sep 2022 13:37:30 +1000 Subject: [PATCH] - Clean up some q16horiz usage and how the view pitch is clamped. --- source/core/fixedhorizon.h | 3 +-- source/core/gamefuncs.h | 16 ++++++++++++++++ source/core/gameinput.cpp | 2 +- source/core/gameinput.h | 2 +- source/core/gamestruct.h | 4 ++-- source/core/rendering/hw_entrypoint.cpp | 2 +- source/games/blood/src/blood.h | 4 ++-- source/games/blood/src/player.cpp | 2 +- source/games/blood/src/view.cpp | 1 - source/games/duke/src/gameexec.cpp | 2 +- source/games/duke/src/render.cpp | 2 -- source/games/exhumed/src/exhumed.h | 4 ++-- source/games/exhumed/src/player.cpp | 6 +++--- source/games/exhumed/src/view.cpp | 1 - source/games/sw/src/draw.cpp | 2 +- source/games/sw/src/game.h | 2 +- source/games/sw/src/jsector.cpp | 2 +- source/games/sw/src/player.cpp | 6 +++--- 18 files changed, 37 insertions(+), 26 deletions(-) diff --git a/source/core/fixedhorizon.h b/source/core/fixedhorizon.h index 03da38e1f..864190702 100644 --- a/source/core/fixedhorizon.h +++ b/source/core/fixedhorizon.h @@ -53,7 +53,6 @@ class FSerializer; inline double HorizToPitch(double horiz) { return atan2(horiz, 128) * (180. / pi::pi()); } inline double HorizToPitch(fixed_t q16horiz) { return atan2(q16horiz, IntToFixed(128)) * (180. / pi::pi()); } -inline fixed_t PitchToHoriz(double pitch) { return xs_CRoundToInt(clamp(IntToFixed(128) * tan(pitch * (pi::pi() / 180.)), INT32_MIN, INT32_MAX)); } //--------------------------------------------------------------------------- @@ -183,7 +182,7 @@ public: inline constexpr fixedhoriz q16horiz(fixed_t v) { return fixedhoriz(v); } inline constexpr fixedhoriz buildhoriz(int v) { return fixedhoriz(IntToFixed(v)); } inline constexpr fixedhoriz tanhoriz(double v) { return fixedhoriz(FloatToFixed<23>(v)); } -inline fixedhoriz pitchhoriz(double v) { return fixedhoriz(PitchToHoriz(v)); } +inline fixedhoriz pitchhoriz(double v) { return fixedhoriz(fixed_t(clamp(IntToFixed(128) * tan(v * (pi::pi() / 180.)), -INT32_MAX, INT32_MAX))); } inline FSerializer &Serialize(FSerializer &arc, const char *key, fixedhoriz &obj, fixedhoriz *defval) { diff --git a/source/core/gamefuncs.h b/source/core/gamefuncs.h index 4418247d4..69b791deb 100644 --- a/source/core/gamefuncs.h +++ b/source/core/gamefuncs.h @@ -1,6 +1,7 @@ #pragma once #include "gamecontrol.h" +#include "gamestruct.h" #include "build.h" #include "coreactor.h" #include "fixedhorizon.h" @@ -536,6 +537,21 @@ inline double BobVal(double val) return g_sinbam(xs_CRoundToUInt(val * (1 << 21))); } +inline double GetMinPitch() +{ + return gi->playerPitchMin(); +} + +inline double GetMaxPitch() +{ + return gi->playerPitchMax(); +} + +inline double ClampViewPitch(const double pitch) +{ + return clamp(pitch, GetMinPitch(), GetMaxPitch()); +} + //========================================================================== // // old deprecated integer versions diff --git a/source/core/gameinput.cpp b/source/core/gameinput.cpp index 08ad4567f..489265fcd 100644 --- a/source/core/gameinput.cpp +++ b/source/core/gameinput.cpp @@ -256,7 +256,7 @@ void PlayerHorizon::applyinput(float const horz, ESyncBits* actions, double cons doKbdInput(SB_LOOK_UP, SB_LOOK_DOWN, LOOKSPEED, false); // clamp before converting back to horizon - horiz = q16horiz(clamp(PitchToHoriz(pitch), gi->playerHorizMin(), gi->playerHorizMax())); + horiz = pitchhoriz(ClampViewPitch(pitch)); } // return to center if conditions met. diff --git a/source/core/gameinput.h b/source/core/gameinput.h index c08d3e06a..28c76f9d8 100644 --- a/source/core/gameinput.h +++ b/source/core/gameinput.h @@ -62,7 +62,7 @@ struct PlayerHorizon void settarget(fixedhoriz value, bool const backup = false) { // Clamp incoming variable because sometimes the caller can exceed bounds. - value = q16horiz(clamp(value.asq16(), gi->playerHorizMin(), gi->playerHorizMax())); + value = pitchhoriz(ClampViewPitch(value.Degrees())); if (!SyncInput() && !backup) { diff --git a/source/core/gamestruct.h b/source/core/gamestruct.h index cfa785bf0..bf5a319b4 100644 --- a/source/core/gamestruct.h +++ b/source/core/gamestruct.h @@ -109,8 +109,8 @@ struct GameInterface virtual void LevelCompleted(MapRecord* map, int skill) {} virtual bool DrawAutomapPlayer(const DVector2& mxy, const DVector2& cpos, const DAngle cang, const DVector2& xydim, const double czoom, double const interpfrac) { return false; } virtual void SetTileProps(int tile, int surf, int vox, int shade) {} - virtual fixed_t playerHorizMin() { return IntToFixed(-200); } - virtual fixed_t playerHorizMax() { return IntToFixed(200); } + virtual double playerPitchMin() { return -57.375; } + virtual double playerPitchMax() { return 57.375; } virtual void WarpToCoords(double x, double y, double z, DAngle a, int h) {} virtual void ToggleThirdPerson() { } virtual void SwitchCoopView() { Printf("Unsupported command\n"); } diff --git a/source/core/rendering/hw_entrypoint.cpp b/source/core/rendering/hw_entrypoint.cpp index 0a86e8473..c965f1679 100644 --- a/source/core/rendering/hw_entrypoint.cpp +++ b/source/core/rendering/hw_entrypoint.cpp @@ -200,7 +200,7 @@ FRenderViewpoint SetupViewpoint(DCoreActor* cam, const DVector3& position, int s r_viewpoint.SectCount = sectnum; r_viewpoint.Pos = { position.X, -position.Y, -position.Z }; r_viewpoint.HWAngles.Yaw = FAngle::fromDeg(-90.f + (float)angle.Degrees()); - r_viewpoint.HWAngles.Pitch = FAngle::fromDeg(-horizon.Degrees()); + r_viewpoint.HWAngles.Pitch = FAngle::fromDeg(-ClampViewPitch(horizon.Degrees())); r_viewpoint.HWAngles.Roll = FAngle::fromDeg(-(float)rollang.Degrees()); r_viewpoint.FieldOfView = FAngle::fromDeg(fov > 0? fov : (float)r_fov); r_viewpoint.RotAngle = angle.BAMs(); diff --git a/source/games/blood/src/blood.h b/source/games/blood/src/blood.h index 3775d9ee8..eb867a315 100644 --- a/source/games/blood/src/blood.h +++ b/source/games/blood/src/blood.h @@ -133,8 +133,8 @@ struct GameInterface : public ::GameInterface void LevelCompleted(MapRecord* map, int skill) override; bool DrawAutomapPlayer(const DVector2& mxy, const DVector2& cpos, const DAngle cang, const DVector2& xydim, const double czoom, double const interpfrac) override; void SetTileProps(int til, int surf, int vox, int shade) override; - fixed_t playerHorizMin() override { return IntToFixed(-180); } - fixed_t playerHorizMax() override { return IntToFixed(120); } + double playerPitchMin() override { return -54.575; } + double playerPitchMax() override { return 43.15; } void WarpToCoords(double x, double y, double z, DAngle a, int h) override; void ToggleThirdPerson() override; void SwitchCoopView() override; diff --git a/source/games/blood/src/player.cpp b/source/games/blood/src/player.cpp index 13c0c6571..41fd771d8 100644 --- a/source/games/blood/src/player.cpp +++ b/source/games/blood/src/player.cpp @@ -1556,7 +1556,7 @@ void ProcessInput(PLAYER* pPlayer) } pPlayer->deathTime += 4; if (!bSeqStat) - pPlayer->horizon.addadjustment(q16horiz(MulScale(0x8000 - (Cos(ClipHigh(pPlayer->deathTime << 3, 1024)) >> 15), gi->playerHorizMax(), 16) - pPlayer->horizon.horiz.asq16())); + pPlayer->horizon.addadjustment(q16horiz(MulScale(0x8000 - (Cos(ClipHigh(pPlayer->deathTime << 3, 1024)) >> 15), pitchhoriz(gi->playerPitchMax()).asq16(), 16) - pPlayer->horizon.horiz.asq16())); if (pPlayer->curWeapon) pInput->setNewWeapon(pPlayer->curWeapon); if (pInput->actions & SB_OPEN) diff --git a/source/games/blood/src/view.cpp b/source/games/blood/src/view.cpp index b0d531a5f..784215835 100644 --- a/source/games/blood/src/view.cpp +++ b/source/games/blood/src/view.cpp @@ -721,7 +721,6 @@ void viewDrawScreen(bool sceneonly) cPos.Z = ceilingZ + 1; } } - cH = q16horiz(ClipRange(cH.asq16(), gi->playerHorizMin(), gi->playerHorizMax())); if ((tilt.Degrees() || bDelirium) && !sceneonly) { diff --git a/source/games/duke/src/gameexec.cpp b/source/games/duke/src/gameexec.cpp index 3cb62e309..ccd451810 100644 --- a/source/games/duke/src/gameexec.cpp +++ b/source/games/duke/src/gameexec.cpp @@ -925,7 +925,7 @@ void DoPlayer(bool bSet, int lVar1, int lLabelID, int lVar2, DDukeActor* sActor, case PLAYER_RETURN_TO_CENTER: if (bSet) ps[iPlayer].sync.actions |= SB_CENTERVIEW; - else SetGameVarID(lVar2, ps[iPlayer].sync.actions & SB_CENTERVIEW ? abs(int(ps[iPlayer].horizon.horiz.asq16() * (9. / gi->playerHorizMax()))) : 0, sActor, sPlayer); + else SetGameVarID(lVar2, ps[iPlayer].sync.actions & SB_CENTERVIEW ? abs(int(ps[iPlayer].horizon.horiz.asq16() * (9. / pitchhoriz(GetMaxPitch()).asq16()))) : 0, sActor, sPlayer); break; default: diff --git a/source/games/duke/src/render.cpp b/source/games/duke/src/render.cpp index b0c60c8d1..393c427ac 100644 --- a/source/games/duke/src/render.cpp +++ b/source/games/duke/src/render.cpp @@ -357,8 +357,6 @@ void displayrooms(int snum, double interpfrac, bool sceneonly) getzsofslopeptr(sect, cpos, &cz, &fz); cpos.Z = min(max(cpos.Z, cz + 4), fz - 4); } - - choriz = clamp(choriz, q16horiz(gi->playerHorizMin()), q16horiz(gi->playerHorizMax())); } auto cstat = viewer->spr.cstat; diff --git a/source/games/exhumed/src/exhumed.h b/source/games/exhumed/src/exhumed.h index d8015e381..f05a9751e 100644 --- a/source/games/exhumed/src/exhumed.h +++ b/source/games/exhumed/src/exhumed.h @@ -231,8 +231,8 @@ struct GameInterface : public ::GameInterface void LevelCompleted(MapRecord *map, int skill) override; void NextLevel(MapRecord *map, int skill) override; bool DrawAutomapPlayer(const DVector2& mxy, const DVector2& cpos, const DAngle cang, const DVector2& xydim, const double czoom, double const interpfrac) override; - fixed_t playerHorizMin() override { return IntToFixed(-150); } - fixed_t playerHorizMax() override { return IntToFixed(150); } + double playerPitchMin() override { return -49.5; } + double playerPitchMax() override { return 49.5; } void WarpToCoords(double x, double y, double z, DAngle ang, int horz) override; void ToggleThirdPerson() override; DVector3 chaseCamPos(DAngle ang, fixedhoriz horiz) { return DVector3(-ang.ToVector() * 96., horiz.Tan() * 96.); } diff --git a/source/games/exhumed/src/player.cpp b/source/games/exhumed/src/player.cpp index b25b6129e..9b8438193 100644 --- a/source/games/exhumed/src/player.cpp +++ b/source/games/exhumed/src/player.cpp @@ -1162,7 +1162,7 @@ void AIPlayer::Tick(RunListEvent* ev) sectdone: if (!PlayerList[nPlayer].bPlayerPan && !PlayerList[nPlayer].bLockPan) { - PlayerList[nPlayer].nDestVertPan = q16horiz(clamp((int((spr_pos.Z - pPlayerActor->spr.pos.Z) * zworldtoint)) << 9, gi->playerHorizMin(), gi->playerHorizMax())); + PlayerList[nPlayer].nDestVertPan = maphoriz((spr_pos.Z - pPlayerActor->spr.pos.Z) * 2.); } playerPos -= pPlayerActor->spr.pos.XY(); @@ -2630,9 +2630,9 @@ sectdone: { PlayerList[nPlayer].horizon.addadjustment(buildhoriz(dVertPan[nPlayer])); - if (PlayerList[nPlayer].horizon.horiz.asq16() > gi->playerHorizMax()) + if (PlayerList[nPlayer].horizon.horiz.asq16() >= IntToFixed(100)) { - PlayerList[nPlayer].horizon.settarget(q16horiz(gi->playerHorizMax())); + PlayerList[nPlayer].horizon.settarget(buildhoriz(99)); } else if (PlayerList[nPlayer].horizon.horiz.asq16() <= 0) { diff --git a/source/games/exhumed/src/view.cpp b/source/games/exhumed/src/view.cpp index ab9995fd9..b7bc983e8 100644 --- a/source/games/exhumed/src/view.cpp +++ b/source/games/exhumed/src/view.cpp @@ -255,7 +255,6 @@ void DrawView(double interpfrac, bool sceneonly) pPlayerActor->spr.cstat |= CSTAT_SPRITE_TRANSLUCENT; pDop->spr.cstat |= CSTAT_SPRITE_INVISIBLE; } - nCamerapan = q16horiz(clamp(nCamerapan.asq16(), gi->playerHorizMin(), gi->playerHorizMax())); } if (nSnakeCam >= 0 && !sceneonly) diff --git a/source/games/sw/src/draw.cpp b/source/games/sw/src/draw.cpp index ef31fbf9a..6b21f0148 100644 --- a/source/games/sw/src/draw.cpp +++ b/source/games/sw/src/draw.cpp @@ -1311,7 +1311,7 @@ void drawscreen(PLAYER* pp, double interpfrac, bool sceneonly) } // recoil only when not in camera - thoriz = q16horiz(clamp(thoriz.asq16() + interpolatedvalue(pp->recoil_ohorizoff, pp->recoil_horizoff, interpfrac), gi->playerHorizMin(), gi->playerHorizMax())); + thoriz += interpolatedvalue(pp->recoil_ohorizoff, pp->recoil_horizoff, interpfrac); } if (automapMode != am_full) diff --git a/source/games/sw/src/game.h b/source/games/sw/src/game.h index a99c0e2f7..2d5a92581 100644 --- a/source/games/sw/src/game.h +++ b/source/games/sw/src/game.h @@ -593,7 +593,7 @@ struct PLAYER int16_t recoil_amt; int16_t recoil_speed; int16_t recoil_ndx; - fixed_t recoil_ohorizoff, recoil_horizoff; + fixedhoriz recoil_ohorizoff, recoil_horizoff; DVector3 Revolve; DAngle RevolveDeltaAng; diff --git a/source/games/sw/src/jsector.cpp b/source/games/sw/src/jsector.cpp index 93fda136d..4300136a7 100644 --- a/source/games/sw/src/jsector.cpp +++ b/source/games/sw/src/jsector.cpp @@ -593,7 +593,7 @@ void JS_DrawCameras(PLAYER* pp, const DVector3& campos, double smoothratio) } // Set the horizon value. - auto camhoriz = q16horiz(clamp(IntToFixed(SP_TAG7(camactor) - 100), gi->playerHorizMin(), gi->playerHorizMax())); + auto camhoriz = maphoriz(SP_TAG7(camactor) - 100); // If player is dead still then update at MoveSkip4 // rate. diff --git a/source/games/sw/src/player.cpp b/source/games/sw/src/player.cpp index 6fe289546..5a1a53469 100644 --- a/source/games/sw/src/player.cpp +++ b/source/games/sw/src/player.cpp @@ -1738,7 +1738,7 @@ void DoPlayerBeginRecoil(PLAYER* pp, short pix_amt) pp->recoil_amt = pix_amt; pp->recoil_speed = 80; pp->recoil_ndx = 0; - pp->recoil_ohorizoff = pp->recoil_horizoff = 0; + pp->recoil_ohorizoff = pp->recoil_horizoff = pitchhoriz(nullAngle.Degrees()); } //--------------------------------------------------------------------------- @@ -1755,13 +1755,13 @@ void DoPlayerRecoil(PLAYER* pp) if (BobVal(pp->recoil_ndx) < 0) { pp->Flags &= ~(PF_RECOIL); - pp->recoil_ohorizoff = pp->recoil_horizoff = 0; + pp->recoil_ohorizoff = pp->recoil_horizoff = pitchhoriz(nullAngle.Degrees()); return; } // move pp->q16horiz up and down pp->recoil_ohorizoff = pp->recoil_horizoff; - pp->recoil_horizoff = pp->recoil_amt * BobVal(pp->recoil_ndx) * 4; + pp->recoil_horizoff = pitchhoriz(HorizToPitch(pp->recoil_amt * BobVal(pp->recoil_ndx))); } //---------------------------------------------------------------------------