From c7fa284db4d9338d69a77590cb0338445317b1a4 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 21 Dec 2020 20:43:14 +0000 Subject: [PATCH] Move scale factor setting scaling to common function --- src/economy.cpp | 32 ++++++++++++++++++++++++++++++++ src/economy_func.h | 3 +++ src/town_cmd.cpp | 28 +--------------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/economy.cpp b/src/economy.cpp index 205d8830e3..321c13035b 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -2397,3 +2397,35 @@ CommandCost CmdBuyCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } return cost; } + +uint ScaleQuantity(uint amount, int scale_factor) +{ + scale_factor += 200; // ensure factor is positive + assert(scale_factor >= 0); + int cf = (scale_factor / 10) - 20; + int fine = scale_factor % 10; + return ScaleQuantity(amount, cf, fine); +} + +uint ScaleQuantity(uint amount, int cf, int fine) +{ + if (fine != 0) { + // 2^0.1 << 16 to 2^0.9 << 16 + const uint32 adj[9] = {70239, 75281, 80684, 86475, 92681, 99334, 106463, 114104, 122294}; + uint64 scaled_amount = ((uint64) amount) * ((uint64) adj[fine - 1]); + amount = scaled_amount >> 16; + } + + // apply scale factor + if (cf < 0) { + // approx (amount / 2^cf) + // adjust with a constant offset of {(2 ^ cf) - 1} (i.e. add cf * 1-bits) before dividing to ensure that it doesn't become zero + // this skews the curve a little so that isn't entirely exponential, but will still decrease + amount = (amount + ((1 << -cf) - 1)) >> -cf; + } else if (cf > 0) { + // approx (amount * 2^cf) + amount = amount << cf; + } + + return amount; +} diff --git a/src/economy_func.h b/src/economy_func.h index d26b344e9a..fcd85c015b 100644 --- a/src/economy_func.h +++ b/src/economy_func.h @@ -49,4 +49,7 @@ static inline bool EconomyIsInRecession() return _economy.fluct <= 0; } +uint ScaleQuantity(uint amount, int scale_factor); +uint ScaleQuantity(uint amount, int cf, int fine); + #endif /* ECONOMY_FUNC_H */ diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index df0d00a95b..7f594122ae 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -639,38 +639,12 @@ static void MakeTownHouseBigger(TileIndex tile) */ static void TownGenerateCargo (Town *t, CargoID ct, uint amount, StationFinder &stations, bool economy_adjust) { - // custom cargo generation factor - int factor = _settings_game.economy.town_cargo_scale_factor; - // when the economy flunctuates, everyone wants to stay at home if (economy_adjust && EconomyIsInRecession()) { amount = (amount + 1) >> 1; } - factor += 200; // ensure factor is positive - assert(factor >= 0); - int cf = (factor / 10) - 20; - int fine = factor % 10; - if (fine != 0) { - // 2^0.1 << 16 to 2^0.9 << 16 - const uint32 adj[9] = {70239, 75281, 80684, 86475, 92681, 99334, 106463, 114104, 122294}; - uint64 scaled_amount = ((uint64) amount) * ((uint64) adj[fine - 1]); - amount = scaled_amount >> 16; - } - - // apply custom factor? - if (cf < 0) { - // approx (amount / 2^cf) - // adjust with a constant offset of {(2 ^ cf) - 1} (i.e. add cf * 1-bits) before dividing to ensure that it doesn't become zero - // this skews the curve a little so that isn't entirely exponential, but will still decrease - amount = (amount + ((1 << -cf) - 1)) >> -cf; - } - - else if (cf > 0) { - // approx (amount * 2^cf) - // XXX: overflow? - amount = amount << cf; - } + amount = ScaleQuantity(amount, _settings_game.economy.town_cargo_scale_factor); // calculate for town stats