BLADE: Use "grow" semantics for population estimate (#1624)

This changes the way percantage-based population estimates work, so that
they have a similar mechanism to grow(). This allows natural recovery
from 0, and also falling to 0 in finite steps.

This also changes the formula for reducing estimates to est / (1 + p)
instead of est * (1 - p). The latter becomes overpowered at large p,
we've seen this formula issue elsewhere (such as BN modifiers).
This commit is contained in:
David Walker 2024-09-13 23:41:10 -07:00 committed by GitHub
parent 3d7a6f70d8
commit f39402e8be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -44,13 +44,17 @@ export class City {
/** @param {number} p - the percentage change, not the multiplier. e.g. pass in p = 5 for 5% */ /** @param {number} p - the percentage change, not the multiplier. e.g. pass in p = 5 for 5% */
improvePopulationEstimateByPercentage(p: number, skillMult = 1): void { improvePopulationEstimateByPercentage(p: number, skillMult = 1): void {
p = clampNumber((p * skillMult) / 100); const percentage = clampNumber(p * skillMult);
const diff = Math.abs(this.popEst - this.pop); const m = percentage / 100;
// Change would overshoot actual population -> make estimate accurate if (this.popEst < this.pop) {
if (diff <= p * this.popEst) this.popEst = this.pop; // We use an additive factor so we don't get "stuck" at 0.
// Otherwise make estimate closer by n const popGrown = (this.popEst + percentage) * (1 + m);
else if (this.popEst < this.pop) this.popEst = clampNumber(this.popEst * (1 + p), 1); this.popEst = clampNumber(popGrown, 0, this.pop);
else this.popEst = clampNumber(this.popEst * (1 - p)); } else {
// We use an subtractive factor so we can reach 0 in finite steps.
const popShrunk = (this.popEst - percentage) / (1 + m);
this.popEst = clampNumber(popShrunk, this.pop);
}
} }
/** /**