From 72531e61dbe53cbc4aedd4a8ea4aee4c8baed36f Mon Sep 17 00:00:00 2001 From: Mitch Richters Date: Sun, 7 Nov 2021 11:37:12 +1100 Subject: [PATCH] - gameinput.cpp/h: Consolidate all the return to zero code for angles and horizons into class members, using technique from `horizoff` as basis. --- source/core/gameinput.cpp | 34 ++++++++-------------------------- source/core/gameinput.h | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/source/core/gameinput.cpp b/source/core/gameinput.cpp index dee143086..a1773f4ea 100644 --- a/source/core/gameinput.cpp +++ b/source/core/gameinput.cpp @@ -320,14 +320,8 @@ void PlayerHorizon::applyinput(float const horz, ESyncBits* actions, double cons // return to center if conditions met. if ((*actions & SB_CENTERVIEW) && !(*actions & (SB_LOOK_UP|SB_LOOK_DOWN)) && horiz.asq16()) { - // move horiz back to 0 - horiz -= getscaledhoriz(CNTRSPEED, scaleAdjust, &horiz); - if (abs(horiz.asq16()) < (FRACUNIT >> 2)) - { - // not looking anymore because horiz is back at 0 - horiz = q16horiz(0); - *actions &= ~SB_CENTERVIEW; - } + scaletozero(horiz, CNTRSPEED, scaleAdjust); + if (!horiz.asq16()) *actions &= ~SB_CENTERVIEW; } } else @@ -374,20 +368,9 @@ enum void PlayerAngle::applyinput(float const avel, ESyncBits* actions, double const scaleAdjust) { - if (rotscrnang.asbam()) - { - // return rotscrnang to 0 - auto sgn = Sgn(rotscrnang.signedbam()); - rotscrnang -= getscaledangle(LOOKROTRETBASE, scaleAdjust, &rotscrnang, sgn); - if (sgn != Sgn(rotscrnang.signedbam())) rotscrnang = bamang(0); - } - - if (look_ang.asbam()) - { - // return look_ang to 0 - look_ang -= getscaledangle(+LOOKROTRETBASE * 0.5, scaleAdjust, &look_ang); - if (abs(look_ang.signedbam()) < (BAMUNIT >> 2)) look_ang = bamang(0); - } + // Process angle return to zeros. + if (rotscrnang.asbam()) scaletozero(rotscrnang, LOOKROTRETBASE, scaleAdjust); + if (look_ang.asbam()) scaletozero(look_ang, +LOOKROTRETBASE * 0.5, scaleAdjust); // Process keyboard input. auto doLookKeys = [&](ESyncBits_ const key, double const direction) @@ -501,16 +484,15 @@ void PlayerHorizon::calcviewpitch(vec2_t const pos, binangle const ang, bool con { // tilt when climbing but you can't even really tell it. if (horizoff.asq16() < IntToFixed(100)) - { auto temphorizoff = buildhoriz(100) - horizoff; + { + auto temphorizoff = buildhoriz(100) - horizoff; horizoff += getscaledhoriz(4.375, scaleAdjust, &temphorizoff, 1.); } } else if (horizoff.asq16()) { // Make horizoff grow towards 0 since horizoff is not modified when you're not on a slope. - auto sgn = Sgn(horizoff.asq16()); - horizoff -= getscaledhoriz(4.375, scaleAdjust, &horizoff, sgn); - if (sgn != Sgn(horizoff.asq16())) horizoff = q16horiz(0); + scaletozero(horizoff, 4.375, scaleAdjust, Sgn(horizoff.asq16())); } } } diff --git a/source/core/gameinput.h b/source/core/gameinput.h index 1920792aa..3d372acff 100644 --- a/source/core/gameinput.h +++ b/source/core/gameinput.h @@ -70,11 +70,17 @@ struct PlayerHorizon // Draw code helpers. double horizsumfrac(double const smoothratio) { return (!SyncInput() ? sum() : interpolatedsum(smoothratio)).asbuildf() * (1. / 16.); } - // Ticrate scale helper. + // Ticrate scale helpers. fixedhoriz getscaledhoriz(double const value, double const scaleAdjust = 1., fixedhoriz* const object = nullptr, double const push = 0.) { return buildfhoriz(scaleAdjust * (((object ? object->asbuildf() : 1.) * getTicrateScale(value)) + push)); } + void scaletozero(fixedhoriz& object, double const value, double const scaleAdjust, double const push = 0.) + { + auto sgn = Sgn(object.asq16()); + object -= getscaledhoriz(value, scaleAdjust, &object, push == 0 ? sgn * (1. / 3.) : push); + if (sgn != Sgn(object.asq16())) object = q16horiz(0); + } // Ticrate playsim adjustment processor. void processhelpers(double const scaleAdjust) @@ -181,11 +187,17 @@ struct PlayerAngle 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.); } - // Ticrate scale helper. + // Ticrate scale helpers. binangle getscaledangle(double const value, double const scaleAdjust = 1., binangle* const object = nullptr, double const push = 0.) { return buildfang(scaleAdjust * (((object ? object->signedbuildf() : 1.) * getTicrateScale(value)) + push)); } + void scaletozero(binangle& object, double const value, double const scaleAdjust, double const push = 0.) + { + auto sgn = Sgn(object.signedbam()); + object -= getscaledangle(value, scaleAdjust, &object, push == 0 ? sgn * (1. / 3.) : push); + if (sgn != Sgn(object.signedbam())) object = bamang(0); + } // Ticrate playsim adjustment processor. void processhelpers(double const scaleAdjust)