- Further work within `PlayerAngle` and `PlayerHorizon` structs following 39fe9efaff and bf2d8078a4.

* Reduce code repetition.
* Clamp incoming horizon when using `settarget()` methods.
* Eliminate double calculation that was occurring in `processhelpers()`.
This commit is contained in:
Mitchell Richters 2021-04-02 22:46:43 +11:00
parent dff6b2a732
commit 4ffe004483
1 changed files with 98 additions and 110 deletions

View File

@ -3,6 +3,7 @@
#include "m_fixed.h" #include "m_fixed.h"
#include "binaryangle.h" #include "binaryangle.h"
#include "gamecvars.h" #include "gamecvars.h"
#include "gamestruct.h"
#include "packet.h" #include "packet.h"
int getincangle(int a, int na); int getincangle(int a, int na);
@ -12,8 +13,7 @@ lookangle getincanglebam(binangle a, binangle na);
struct PlayerHorizon struct PlayerHorizon
{ {
fixedhoriz horiz, ohoriz, horizoff, ohorizoff, target; fixedhoriz horiz, ohoriz, horizoff, ohorizoff;
double adjustment;
void backup() void backup()
{ {
@ -29,14 +29,7 @@ struct PlayerHorizon
void addadjustment(double value) void addadjustment(double value)
{ {
if (!SyncInput()) __addadjustment(q16horiz(FloatToFixed(value)));
{
adjustment += value * FRACUNIT;
}
else
{
horiz += q16horiz(FloatToFixed(value));
}
} }
void resetadjustment() void resetadjustment()
@ -46,53 +39,35 @@ struct PlayerHorizon
void settarget(int value, bool backup = false) void settarget(int value, bool backup = false)
{ {
if (!SyncInput() && !backup) __settarget(buildhoriz(clamp(value, FixedToInt(gi->playerHorizMin()), FixedToInt(gi->playerHorizMax()))), backup);
{
target = buildhoriz(value);
if (target.asq16() == 0) target = q16horiz(1);
}
else
{
horiz = buildhoriz(value);
if (backup) ohoriz = horiz;
}
} }
void settarget(double value, bool backup = false) void settarget(double value, bool backup = false)
{ {
if (!SyncInput() && !backup) __settarget(buildfhoriz(clamp(value, FixedToFloat(gi->playerHorizMin()), FixedToFloat(gi->playerHorizMax()))), backup);
{
target = buildfhoriz(value);
if (target.asq16() == 0) target = q16horiz(1);
}
else
{
horiz = buildfhoriz(value);
if (backup) ohoriz = horiz;
}
} }
void settarget(fixedhoriz value, bool backup = false) void settarget(fixedhoriz value, bool backup = false)
{ {
if (!SyncInput() && !backup) __settarget(q16horiz(clamp(value.asq16(), gi->playerHorizMin(), gi->playerHorizMax())), backup);
{ }
target = value;
if (target.asq16() == 0) target = q16horiz(1); bool targetset()
} {
else return target.asq16();
{
horiz = value;
if (backup) ohoriz = horiz;
}
} }
void processhelpers(double const scaleAdjust) void processhelpers(double const scaleAdjust)
{ {
if (target.asq16()) if (targetset())
{ {
horiz += q16horiz(xs_CRoundToInt(scaleAdjust * (target - horiz).asq16())); auto delta = (target - horiz).asq16();
if (abs((horiz - target).asq16()) < FRACUNIT) if (abs(delta) > FRACUNIT)
{
horiz += q16horiz(xs_CRoundToInt(scaleAdjust * delta));
}
else
{ {
horiz = target; horiz = target;
target = q16horiz(0); target = q16horiz(0);
@ -121,13 +96,42 @@ struct PlayerHorizon
fixed_t const curr = sum().asq16(); fixed_t const curr = sum().asq16();
return q16horiz(prev + xs_CRoundToInt(ratio * (curr - prev))); return q16horiz(prev + xs_CRoundToInt(ratio * (curr - prev)));
} }
private:
fixedhoriz target;
double adjustment;
void __addadjustment(fixedhoriz value)
{
if (!SyncInput())
{
adjustment += value.asq16();
}
else
{
horiz += value;
}
}
void __settarget(fixedhoriz value, bool backup = false)
{
if (!SyncInput() && !backup)
{
target = value;
if (!targetset()) target = q16horiz(1);
}
else
{
horiz = value;
if (backup) ohoriz = horiz;
}
}
}; };
struct PlayerAngle struct PlayerAngle
{ {
binangle ang, oang, target; binangle ang, oang;
lookangle look_ang, olook_ang, rotscrnang, orotscrnang, spin; lookangle look_ang, olook_ang, rotscrnang, orotscrnang, spin;
double adjustment;
void backup() void backup()
{ {
@ -145,50 +149,22 @@ struct PlayerAngle
void addadjustment(int value) void addadjustment(int value)
{ {
if (!SyncInput()) __addadjustment(buildang(value));
{
adjustment += BAngToBAM(value);
}
else
{
ang += buildang(value);
}
} }
void addadjustment(double value) void addadjustment(double value)
{ {
if (!SyncInput()) __addadjustment(buildfang(value));
{
adjustment += value * BAMUNIT;
}
else
{
ang += bamang(xs_CRoundToUInt(value * BAMUNIT));
}
} }
void addadjustment(lookangle value) void addadjustment(lookangle value)
{ {
if (!SyncInput()) __addadjustment(bamang(value.asbam()));
{
adjustment += value.asbam();
}
else
{
ang += bamang(value.asbam());
}
} }
void addadjustment(binangle value) void addadjustment(binangle value)
{ {
if (!SyncInput()) __addadjustment(value);
{
adjustment += value.asbam();
}
else
{
ang += value;
}
} }
void resetadjustment() void resetadjustment()
@ -198,53 +174,35 @@ struct PlayerAngle
void settarget(int value, bool backup = false) void settarget(int value, bool backup = false)
{ {
if (!SyncInput() && !backup) __settarget(buildang(value & 2047), backup);
{
target = buildang(value & 2047);
if (target.asbam() == 0) target = bamang(1);
}
else
{
ang = buildang(value & 2047);
if (backup) oang = ang;
}
} }
void settarget(double value, bool backup = false) void settarget(double value, bool backup = false)
{ {
if (!SyncInput() && !backup) __settarget(buildfang(fmod(value, 2048)), backup);
{
target = buildfang(fmod(value, 2048));
if (target.asbam() == 0) target = bamang(1);
}
else
{
ang = buildfang(fmod(value, 2048));
if (backup) oang = ang;
}
} }
void settarget(binangle value, bool backup = false) void settarget(binangle value, bool backup = false)
{ {
if (!SyncInput() && !backup) __settarget(value, backup);
{ }
target = value;
if (target.asbam() == 0) target = bamang(1); bool targetset()
} {
else return target.asbam();
{
ang = value;
if (backup) oang = ang;
}
} }
void processhelpers(double const scaleAdjust) void processhelpers(double const scaleAdjust)
{ {
if (target.asbam()) if (targetset())
{ {
ang += bamang(xs_CRoundToUInt(scaleAdjust * getincanglebam(ang, target).asbam())); auto delta = getincanglebam(ang, target).asbam();
if (getincanglebam(ang, target).asbam() < BAMUNIT) if (delta > BAMUNIT)
{
ang += bamang(xs_CRoundToUInt(scaleAdjust * delta));
}
else
{ {
ang = target; ang = target;
target = bamang(0); target = bamang(0);
@ -291,6 +249,36 @@ struct PlayerAngle
{ {
return (!SyncInput() ? look_ang : interpolatedlookang(smoothratio)).asbam() * (0.5 / BAMUNIT); // Used within draw code for weapon and crosshair when looking left/right. return (!SyncInput() ? look_ang : interpolatedlookang(smoothratio)).asbam() * (0.5 / BAMUNIT); // Used within draw code for weapon and crosshair when looking left/right.
} }
private:
binangle target;
double adjustment;
void __addadjustment(binangle value)
{
if (!SyncInput())
{
adjustment += value.asbam();
}
else
{
ang += value;
}
}
void __settarget(binangle value, bool backup = false)
{
if (!SyncInput() && !backup)
{
target = value;
if (!targetset()) target = bamang(1);
}
else
{
ang = value;
if (backup) oang = ang;
}
}
}; };
class FSerializer; class FSerializer;