From 13ea55b4cb9499a838d86db454ef521f70111580 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Sat, 27 Aug 2022 21:48:57 +1000 Subject: [PATCH] - Replace player's `rotscrnang` and `look_ang` angles with `DAngle` objects. * Went the el cheapo route and added some floating point Build angle methods. As these and the integer build methods are really just refactoring aids, we'll come back to this in due course. --- source/common/utility/vectors.h | 16 ++++++++++++++++ source/core/gameinput.cpp | 14 +++++++------- source/core/gameinput.h | 17 +++++++++-------- source/games/blood/src/view.cpp | 8 ++++---- source/games/duke/src/gameexec.cpp | 10 +++++----- source/games/duke/src/player.cpp | 16 ++++++++-------- source/games/duke/src/premap.cpp | 4 ++-- source/games/duke/src/render.cpp | 2 +- source/games/duke/src/types.h | 2 +- source/games/exhumed/src/view.cpp | 4 ++-- source/games/sw/src/draw.cpp | 4 ++-- 11 files changed, 57 insertions(+), 40 deletions(-) diff --git a/source/common/utility/vectors.h b/source/common/utility/vectors.h index c009cb505..39fc85535 100644 --- a/source/common/utility/vectors.h +++ b/source/common/utility/vectors.h @@ -1212,6 +1212,11 @@ public: return TAngle(bang * (90. / 512)); } + static constexpr TAngle fromBuildf(double bang) + { + return TAngle(bang * (90. / 512)); + } + static constexpr TAngle fromQ16(int bang) { return TAngle(bang * (90. / 16384)); @@ -1334,6 +1339,11 @@ public: return int(Degrees_ * (512 / 90.0)); } + constexpr double Buildfang() const + { + return Degrees_ * (512 / 90.0); + } + constexpr int Q16() const { return int(Degrees_ * (16384 / 90.0)); @@ -1364,6 +1374,12 @@ public: { return clamp(Tan(), -max, max); } + + constexpr int Sgn() const + { + const auto normalized = (signed int)BAMs(); + return (normalized > 0) - (normalized < 0); + } }; template diff --git a/source/core/gameinput.cpp b/source/core/gameinput.cpp index e07eb0013..679074254 100644 --- a/source/core/gameinput.cpp +++ b/source/core/gameinput.cpp @@ -48,9 +48,9 @@ inline static fixedhoriz getscaledhoriz(const double value, const double scaleAd return buildfhoriz(scaleAdjust * ((object.asbuildf() * getTicrateScale(value)) + push)); } -inline static binangle getscaledangle(const double value, const double scaleAdjust, const binangle& object, const double push) +inline static DAngle getscaledangle(const double value, const double scaleAdjust, const DAngle& object, const double push) { - return buildfang(scaleAdjust * ((object.signedbuildf() * getTicrateScale(value)) + push)); + return DAngle::fromBuildf(scaleAdjust * ((object.Normalized180().Buildfang() * getTicrateScale(value)) + push)); } inline static void scaletozero(fixedhoriz& object, const double value, const double scaleAdjust, const double push = DBL_MAX) @@ -62,12 +62,12 @@ inline static void scaletozero(fixedhoriz& object, const double value, const dou } } -inline static void scaletozero(binangle& object, const double value, const double scaleAdjust, const double push = DBL_MAX) +inline static void scaletozero(DAngle& object, const double value, const double scaleAdjust, const double push = DBL_MAX) { - if (auto sgn = Sgn(object.signedbam())) + if (auto sgn = object.Sgn()) { object -= getscaledangle(value, scaleAdjust, object, push == DBL_MAX ? sgn * getPushScale(scaleAdjust) : push); - if (sgn != Sgn(object.signedbam())) object = bamang(0); + if (sgn != object.Sgn()) object = DAngle::fromDeg(0.); } } @@ -375,8 +375,8 @@ void PlayerAngle::applyinput(float const avel, ESyncBits* actions, double const { if (*actions & key) { - look_ang += buildfang(getTicrateScale(LOOKINGSPEED) * scaleAdjust * direction); - rotscrnang -= buildfang(getTicrateScale(ROTATESPEED) * scaleAdjust * direction); + look_ang += DAngle::fromBuildf(getTicrateScale(LOOKINGSPEED) * scaleAdjust * direction); + rotscrnang -= DAngle::fromBuildf(getTicrateScale(ROTATESPEED) * scaleAdjust * direction); } }; doLookKeys(SB_LOOK_LEFT, -1); diff --git a/source/core/gameinput.h b/source/core/gameinput.h index e67b50ce7..d1e834e34 100644 --- a/source/core/gameinput.h +++ b/source/core/gameinput.h @@ -124,7 +124,8 @@ private: struct PlayerAngle { - binangle ang, oang, look_ang, olook_ang, rotscrnang, orotscrnang; + binangle ang, oang; + DAngle look_ang, olook_ang, rotscrnang, orotscrnang; double spin; friend FSerializer& Serialize(FSerializer& arc, const char* keyname, PlayerAngle& w, PlayerAngle* def); @@ -147,11 +148,11 @@ struct PlayerAngle } // Commonly used getters. - binangle osum() { return oang + olook_ang; } - binangle sum() { return ang + look_ang; } + binangle osum() { return oang + bamang(olook_ang.BAMs()); } + binangle sum() { return ang + bamang(look_ang.BAMs()); } binangle interpolatedsum(double const smoothratio) { return interpolatedangle(osum(), sum(), smoothratio); } - binangle interpolatedlookang(double const smoothratio) { return interpolatedangle(olook_ang, look_ang, smoothratio); } - binangle interpolatedrotscrn(double const smoothratio) { return interpolatedangle(orotscrnang, rotscrnang, smoothratio); } + DAngle interpolatedlookang(double const smoothratio) { return interpolatedangle(olook_ang, look_ang, smoothratio); } + DAngle interpolatedrotscrn(double const smoothratio) { return interpolatedangle(orotscrnang, rotscrnang, smoothratio); } // Ticrate playsim adjustment helpers. void resetadjustment() { adjustment = 0; } @@ -162,9 +163,9 @@ struct PlayerAngle void unlockinput() { inputdisabled = false; } bool movementlocked() { return targetset() || inputdisabled; } - // Draw code helpers. - double look_anghalf(double const smoothratio) { return (!SyncInput() ? look_ang : interpolatedlookang(smoothratio)).signedbuildf() * 0.5; } - double looking_arc(double const smoothratio) { return fabs((!SyncInput() ? look_ang : interpolatedlookang(smoothratio)).signedbuildf()) * (1. / 9.); } + // Draw code helpers. The logic where these are used rely heavily on Build's angle period. + double look_anghalf(double const smoothratio) { return (!SyncInput() ? look_ang : interpolatedlookang(smoothratio)).Normalized180().Buildfang() * 0.5; } + double looking_arc(double const smoothratio) { return fabs((!SyncInput() ? look_ang : interpolatedlookang(smoothratio)).Normalized180().Buildfang()) * (1. / 9.); } // Ticrate playsim adjustment setters and processor. void addadjustment(binangle const value) diff --git a/source/games/blood/src/view.cpp b/source/games/blood/src/view.cpp index 87f23b9de..2c7ed9d02 100644 --- a/source/games/blood/src/view.cpp +++ b/source/games/blood/src/view.cpp @@ -75,8 +75,8 @@ void viewBackupView(int nPlayer) pView->bobWidth = pPlayer->bobWidth; pView->shakeBobY = pPlayer->swayHeight; pView->shakeBobX = pPlayer->swayWidth; - pView->look_ang = DAngle::fromBam(pPlayer->angle.look_ang.asbam()); - pView->rotscrnang = DAngle::fromBam(pPlayer->angle.rotscrnang.asbam()); + pView->look_ang = pPlayer->angle.look_ang; + pView->rotscrnang = pPlayer->angle.rotscrnang; pPlayer->angle.backup(); pPlayer->horizon.backup(); } @@ -534,13 +534,13 @@ void SetupView(int& cX, int& cY, int& cZ, DAngle& cA, fixedhoriz& cH, sectortype { cA = DAngle::fromBam(gView->angle.sum().asbam()); cH = gView->horizon.sum(); - rotscrnang = DAngle::fromBam(gView->angle.rotscrnang.asbam()); + rotscrnang = gView->angle.rotscrnang; } else { cA = DAngle::fromBam(gView->angle.interpolatedsum(gInterpolate).asbam()); cH = gView->horizon.interpolatedsum(gInterpolate); - rotscrnang = DAngle::fromBam(gView->angle.interpolatedrotscrn(gInterpolate).asbam()); + rotscrnang = gView->angle.interpolatedrotscrn(gInterpolate); } } diff --git a/source/games/duke/src/gameexec.cpp b/source/games/duke/src/gameexec.cpp index c1e883bcc..3d1f8b2b7 100644 --- a/source/games/duke/src/gameexec.cpp +++ b/source/games/duke/src/gameexec.cpp @@ -490,8 +490,8 @@ void DoPlayer(bool bSet, int lVar1, int lLabelID, int lVar2, DDukeActor* sActor, break; case PLAYER_LOOK_ANG: - if (bSet) ps[iPlayer].angle.look_ang = buildang(lValue); - else SetGameVarID(lVar2, ps[iPlayer].angle.look_ang.asbuild(), sActor, sPlayer); + if (bSet) ps[iPlayer].angle.look_ang = DAngle::fromBuild(lValue); + else SetGameVarID(lVar2, ps[iPlayer].angle.look_ang.Buildang(), sActor, sPlayer); break; case PLAYER_LAST_EXTRA: @@ -713,9 +713,9 @@ void DoPlayer(bool bSet, int lVar1, int lLabelID, int lVar2, DDukeActor* sActor, if (bSet) { ps[iPlayer].angle.orotscrnang = ps[iPlayer].angle.rotscrnang; - ps[iPlayer].angle.rotscrnang = buildang(lValue); + ps[iPlayer].angle.rotscrnang = DAngle::fromBuild(lValue); } - else SetGameVarID(lVar2, ps[iPlayer].angle.rotscrnang.asbuild(), sActor, sPlayer); + else SetGameVarID(lVar2, ps[iPlayer].angle.rotscrnang.Buildang(), sActor, sPlayer); break; case PLAYER_DEAD_FLAG: @@ -2255,7 +2255,7 @@ int ParseState::parse(void) ps[g_p].weapreccnt = 0; ps[g_p].ftq = 0; ps[g_p].vel.X = ps[g_p].vel.Y = 0; - if (!isRR()) ps[g_p].angle.orotscrnang = ps[g_p].angle.rotscrnang = buildang(0); + if (!isRR()) ps[g_p].angle.orotscrnang = ps[g_p].angle.rotscrnang = DAngle::fromDeg(0.); ps[g_p].falling_counter = 0; diff --git a/source/games/duke/src/player.cpp b/source/games/duke/src/player.cpp index 1e29797d6..75a53fe2b 100644 --- a/source/games/duke/src/player.cpp +++ b/source/games/duke/src/player.cpp @@ -122,7 +122,7 @@ void forceplayerangle(int snum) p->horizon.addadjustment(buildhoriz(64)); p->sync.actions |= SB_CENTERVIEW; - p->angle.rotscrnang = p->angle.look_ang = buildang(n >> 1); + p->angle.rotscrnang = p->angle.look_ang = DAngle::fromBuild(n >> 1); } //--------------------------------------------------------------------------- @@ -631,10 +631,10 @@ void playerisdead(int snum, int psectlotag, int fz, int cz) updatesector(p->player_int_pos().X, p->player_int_pos().Y, &p->cursector); - pushmove(p->pos, &p->cursector, 128L, (4 << 8), (20 << 8), CLIPMASK0); + pushmove(p->pos, &p->cursector, 128, (4 << 8), (20 << 8), CLIPMASK0); if (fz > cz + (16 << 8) && actor->spr.pal != 1) - p->angle.rotscrnang = buildang(p->dead_flag + ((fz + p->player_int_pos().Z) >> 7)); + p->angle.rotscrnang = DAngle::fromBuild(p->dead_flag + ((fz + p->player_int_pos().Z) >> 7)); p->on_warping_sector = 0; @@ -746,16 +746,16 @@ void player_struct::apply_seasick(double factor) if (SeaSick < 250) { if (SeaSick >= 180) - angle.rotscrnang += buildfang(24 * factor); + angle.rotscrnang += DAngle::fromDeg(24 * factor * BAngToDegree); else if (SeaSick >= 130) - angle.rotscrnang -= buildfang(24 * factor); + angle.rotscrnang -= DAngle::fromDeg(24 * factor * BAngToDegree); else if (SeaSick >= 70) - angle.rotscrnang += buildfang(24 * factor); + angle.rotscrnang += DAngle::fromDeg(24 * factor * BAngToDegree); else if (SeaSick >= 20) - angle.rotscrnang -= buildfang(24 * factor); + angle.rotscrnang -= DAngle::fromDeg(24 * factor * BAngToDegree); } if (SeaSick < 250) - angle.look_ang = buildfang(((krand() & 255) - 128) * factor); + angle.look_ang = DAngle::fromDeg(((krand() & 255) - 128) * factor * BAngToDegree); } } diff --git a/source/games/duke/src/premap.cpp b/source/games/duke/src/premap.cpp index 24dd66a03..c8b12c221 100644 --- a/source/games/duke/src/premap.cpp +++ b/source/games/duke/src/premap.cpp @@ -177,8 +177,8 @@ void resetplayerstats(int snum) p->jetpack_on = 0; p->holoduke_on = nullptr; - p->angle.olook_ang = p->angle.look_ang = buildang(512 - (((~currentLevel->levelNumber) & 1) << 10)); - p->angle.orotscrnang = p->angle.rotscrnang = buildang(0); + p->angle.olook_ang = p->angle.look_ang = DAngle::fromBuild(512 - (((~currentLevel->levelNumber) & 1) << 10)); + p->angle.orotscrnang = p->angle.rotscrnang = DAngle::fromDeg(0.); p->newOwner =nullptr; p->jumping_counter = 0; diff --git a/source/games/duke/src/render.cpp b/source/games/duke/src/render.cpp index 17fceefb3..444db6897 100644 --- a/source/games/duke/src/render.cpp +++ b/source/games/duke/src/render.cpp @@ -281,7 +281,7 @@ void displayrooms(int snum, double smoothratio, bool sceneonly) setgamepalette(setpal(p)); // set screen rotation. - rotscrnang = DAngle::fromBam((!SyncInput() ? p->angle.rotscrnang : p->angle.interpolatedrotscrn(smoothratio)).asbam()); + rotscrnang = !SyncInput() ? p->angle.rotscrnang : p->angle.interpolatedrotscrn(smoothratio); #if 0 if ((snum == myconnectindex) && (numplayers > 1)) diff --git a/source/games/duke/src/types.h b/source/games/duke/src/types.h index 464bfa796..fdb1f34cf 100644 --- a/source/games/duke/src/types.h +++ b/source/games/duke/src/types.h @@ -268,7 +268,7 @@ struct player_struct short weaprecs[256], weapreccnt; unsigned int interface_toggle_flag; - short dead_flag, show_empty_weapon; // JBF 20031220: added orotscrnang + short dead_flag, show_empty_weapon; short scuba_amount, jetpack_amount, steroids_amount, shield_amount; short pycount, frag_ps; short transporter_hold, last_full_weapon, footprintshade, boot_amount; diff --git a/source/games/exhumed/src/view.cpp b/source/games/exhumed/src/view.cpp index 1374ac101..5fb147bfc 100644 --- a/source/games/exhumed/src/view.cpp +++ b/source/games/exhumed/src/view.cpp @@ -242,13 +242,13 @@ void DrawView(double smoothRatio, bool sceneonly) { pan = PlayerList[nLocalPlayer].horizon.sum(); nAngle = DAngle::fromBam(PlayerList[nLocalPlayer].angle.sum().asbam()); - rotscrnang = DAngle::fromBam(PlayerList[nLocalPlayer].angle.rotscrnang.asbam()); + rotscrnang = PlayerList[nLocalPlayer].angle.rotscrnang; } else { pan = PlayerList[nLocalPlayer].horizon.interpolatedsum(smoothRatio); nAngle = DAngle::fromBam(PlayerList[nLocalPlayer].angle.interpolatedsum(smoothRatio).asbam()); - rotscrnang = DAngle::fromBam(PlayerList[nLocalPlayer].angle.interpolatedrotscrn(smoothRatio).asbam()); + rotscrnang = PlayerList[nLocalPlayer].angle.interpolatedrotscrn(smoothRatio); } if (!bCamera) diff --git a/source/games/sw/src/draw.cpp b/source/games/sw/src/draw.cpp index 34206a1c7..d4da336fa 100644 --- a/source/games/sw/src/draw.cpp +++ b/source/games/sw/src/draw.cpp @@ -1402,13 +1402,13 @@ void drawscreen(PLAYER* pp, double smoothratio, bool sceneonly) { tang = DAngle::fromBam(camerapp->angle.interpolatedsum(smoothratio).asbam()); thoriz = camerapp->horizon.interpolatedsum(smoothratio); - trotscrnang = DAngle::fromBam(camerapp->angle.interpolatedrotscrn(smoothratio).asbam()); + trotscrnang = camerapp->angle.interpolatedrotscrn(smoothratio); } else { tang = DAngle::fromBam(pp->angle.sum().asbam()); thoriz = pp->horizon.sum(); - trotscrnang = DAngle::fromBam(pp->angle.rotscrnang.asbam()); + trotscrnang = pp->angle.rotscrnang; } tsect = camerapp->cursector;