Move scale factor setting scaling to common function

pull/211/head
Jonathan G Rennison 3 years ago
parent bfbd2de477
commit c7fa284db4

@ -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;
}

@ -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 */

@ -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

Loading…
Cancel
Save