From 04e98f2f770c22cb0466e8b00eaf84730d863be9 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 28 Sep 2022 20:17:01 +0200 Subject: [PATCH] - avoid inflation of random functions. --- source/common/utility/m_fixed.h | 10 ++++++++++ source/games/blood/src/common_game.h | 24 ++++++++++-------------- source/games/blood/src/gib.cpp | 6 +++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/source/common/utility/m_fixed.h b/source/common/utility/m_fixed.h index e4dd17bf9..1c9a08a42 100644 --- a/source/common/utility/m_fixed.h +++ b/source/common/utility/m_fixed.h @@ -21,6 +21,11 @@ constexpr fixed_t FloatToFixed(double f) return int(f * (1 << b)); } +constexpr fixed_t FloatToFixed(double f, int b) +{ + return int(f * (1 << b)); +} + template inline constexpr fixed_t IntToFixed(int32_t f) { @@ -33,6 +38,11 @@ inline constexpr double FixedToFloat(fixed_t f) return f * (1. / (1 << b)); } +inline constexpr double FixedToFloat(fixed_t f, int b) +{ + return f * (1. / (1 << b)); +} + template inline constexpr int32_t FixedToInt(fixed_t f) { diff --git a/source/games/blood/src/common_game.h b/source/games/blood/src/common_game.h index e954c6c74..8890bcdfe 100644 --- a/source/games/blood/src/common_game.h +++ b/source/games/blood/src/common_game.h @@ -537,19 +537,20 @@ inline uint8_t Chance(int a1) return wrand() < (a1 >> 1); } +// ------------------------------------------------ inline unsigned int Random(int a1) { return MulScale(wrand(), a1, 15); } -inline double RandomX(double a1) +inline double RandomF(int a1) { - return FixedToFloat<4>(Random(FloatToFixed<4>(a1))); + return FixedToFloat(Random(a1)); } -inline double RandomZ(double a1) +inline double RandomF(double val, int scale) { - return FixedToFloat<8>(Random(FloatToFixed<8>(a1))); + return FixedToFloat(Random(FloatToFixed(val, scale)), scale); } inline DAngle RandomAngle(int base = 2048) @@ -557,6 +558,7 @@ inline DAngle RandomAngle(int base = 2048) return DAngle::fromBuild(MulScale(wrand(), base, 15)); } +// ------------------------------------------------ inline int Random2(int a1) { return MulScale(wrand(), a1, 14) - a1; @@ -564,21 +566,15 @@ inline int Random2(int a1) inline double Random2F(int a1) { - return FixedToFloat(MulScale(wrand(), a1, 14) - a1); + return FixedToFloat(Random2(a1)); } -inline double Random2X(double a1) +inline double Random2F(double val, int scale) { - return FixedToFloat<4>(Random2(FloatToFixed<4>(a1))); + return FixedToFloat(Random2(FloatToFixed(val, scale)), scale); } -inline double Random2Z(double a1) -{ - return FixedToFloat<8>(Random2(FloatToFixed<8>(a1))); -} - - - +// ------------------------------------------------ inline int Random3(int a1) { return MulScale(wrand() + wrand(), a1, 15) - a1; diff --git a/source/games/blood/src/gib.cpp b/source/games/blood/src/gib.cpp index 87d343291..e99807199 100644 --- a/source/games/blood/src/gib.cpp +++ b/source/games/blood/src/gib.cpp @@ -455,9 +455,9 @@ void GibFX(walltype* pWall, GIBFX* pGFX, double ceilZ, const DVector3& spread, C for (int i = 0; i < nCount; i++) { DVector3 r; - r.Z = RandomZ(spread.Z) + ceilZ; - r.Y = RandomX(spread.Y) + pWall->pos.Y; - r.X = RandomX(spread.X) + pWall->pos.X; + r.Z = RandomF(spread.Z, 8) + ceilZ; + r.Y = RandomF(spread.Y, 4) + pWall->pos.Y; + r.X = RandomF(spread.X, 4) + pWall->pos.X; auto pGib = gFX.fxSpawnActor(pGFX->fxId, pSector, r, 0); if (pGib) {