From 8db220d344e490531068f4962fb3db159361b0a0 Mon Sep 17 00:00:00 2001 From: Nikolay Borodin Date: Mon, 28 Oct 2024 15:51:32 +0200 Subject: [PATCH] Removed floating point math from calculation --- data/mp/stats/propulsion.json | 6 +++--- lib/framework/math_ext.h | 7 +++++++ src/droid.cpp | 2 +- src/stats.cpp | 2 +- src/statsdef.h | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/data/mp/stats/propulsion.json b/data/mp/stats/propulsion.json index 9ae8c4f7108..d2f63a0de52 100644 --- a/data/mp/stats/propulsion.json +++ b/data/mp/stats/propulsion.json @@ -33,7 +33,7 @@ "type": "Legged", "usageClass": "Cyborg", "weight": 50, - "repairFactor": 1.25 + "repairFactor": 125 }, "HalfTrack": { "buildPoints": 75, @@ -108,7 +108,7 @@ "speed": 300, "type": "Hover", "weight": 200, - "repairFactor": 1.5 + "repairFactor": 150 }, "tracked01": { "buildPoints": 125, @@ -135,6 +135,6 @@ "speed": 175, "type": "Wheeled", "weight": 300, - "repairFactor": 2 + "repairFactor": 200 } } diff --git a/lib/framework/math_ext.h b/lib/framework/math_ext.h index a5b5dc00072..c04489b06fd 100644 --- a/lib/framework/math_ext.h +++ b/lib/framework/math_ext.h @@ -111,6 +111,13 @@ static inline WZ_DECL_CONST T clip(T x, T min, T max) return x < min ? min : x > max ? max : x; } +template +static inline WZ_DECL_CONST T idiv_round(T numerator, T denominator) +{ + return ((numerator < 0) == (denominator < 0)) + ? ((numerator + denominator /2 ) / denominator) + : ((numerator - denominator / 2) / denominator); +} /*! * Clips x to boundaries diff --git a/src/droid.cpp b/src/droid.cpp index 31c755d925b..ccaf0292178 100644 --- a/src/droid.cpp +++ b/src/droid.cpp @@ -1292,7 +1292,7 @@ static bool droidUpdateDroidRepairBase(DROID *psRepairDroid, DROID *psDroidToRep int iPointsToAdd = gameTimeAdjustedAverage(iRepairRateNumerator, iRepairRateDenominator); - iPointsToAdd = static_cast(std::round(iPointsToAdd * psDroidToRepair->getPropulsionStats()->repairFactor)); + iPointsToAdd = idiv_round(iPointsToAdd * psDroidToRepair->getPropulsionStats()->repairFactor, 100); psDroidToRepair->body = clip(psDroidToRepair->body + iPointsToAdd, 0, psDroidToRepair->originalBody); diff --git a/src/stats.cpp b/src/stats.cpp index b7e7bda40d7..bc8f7011f9a 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -822,7 +822,7 @@ bool loadPropulsionStats(WzConfig &ini) psStats->acceleration = ini.value("acceleration", 250).toInt(); ASSERT(psStats->acceleration != 0, "\"%s\".\"acceleration\" is 0", psStats->id.toUtf8().c_str()); psStats->deceleration = ini.value("deceleration", 800).toInt(); - psStats->repairFactor = ini.value("repairFactor", 1.0f).toFloat(); + psStats->repairFactor = ini.value("repairFactor", 100).toInt(); psStats->skidDeceleration = ini.value("skidDeceleration", 600).toInt(); psStats->pIMD = nullptr; psStats->pIMD = statsGetIMD(ini, psStats, "model"); diff --git a/src/statsdef.h b/src/statsdef.h index 3b185a2f9ff..dc1ba207ef3 100644 --- a/src/statsdef.h +++ b/src/statsdef.h @@ -338,7 +338,7 @@ struct PROPULSION_STATS : public COMPONENT_STATS unsigned skidDeceleration = 0; unsigned deceleration = 0; unsigned acceleration = 0; - float repairFactor = 0; + unsigned repairFactor = 0; struct UPGRADE : COMPONENT_STATS::UPGRADE {