Make scaling functions use computables instead of processed computables

This commit is contained in:
thepaperpilot 2022-05-01 19:23:16 -05:00
parent 17642b95a6
commit 4f3d73acd7

View file

@ -4,6 +4,7 @@ import Decimal, { DecimalSource } from "util/bignum";
import { WithRequired } from "util/common"; import { WithRequired } from "util/common";
import { import {
Computable, Computable,
convertComputable,
GetComputableTypeWithDefault, GetComputableTypeWithDefault,
processComputable, processComputable,
ProcessedComputable ProcessedComputable
@ -226,18 +227,20 @@ export interface ScalingFunction {
* | 20 | 6 | * | 20 | 6 |
*/ */
export function createLinearScaling( export function createLinearScaling(
base: DecimalSource | Ref<DecimalSource>, base: Computable<DecimalSource>,
coefficient: DecimalSource | Ref<DecimalSource> coefficient: Computable<DecimalSource>
): ScalingFunction { ): ScalingFunction {
const processedBase = convertComputable(base);
const processedCoefficient = convertComputable(coefficient);
return { return {
currentGain(conversion) { currentGain(conversion) {
if (Decimal.lt(conversion.baseResource.value, unref(base))) { if (Decimal.lt(conversion.baseResource.value, unref(processedBase))) {
return 0; return 0;
} }
return Decimal.sub(conversion.baseResource.value, unref(base)) return Decimal.sub(conversion.baseResource.value, unref(processedBase))
.sub(1) .sub(1)
.times(unref(coefficient)) .times(unref(processedCoefficient))
.add(1); .add(1);
}, },
currentAt(conversion) { currentAt(conversion) {
@ -246,7 +249,9 @@ export function createLinearScaling(
current = conversion.gainModifier.revert(current); current = conversion.gainModifier.revert(current);
} }
current = Decimal.max(0, current); current = Decimal.max(0, current);
return Decimal.sub(current, 1).div(unref(coefficient)).add(unref(base)); return Decimal.sub(current, 1)
.div(unref(processedCoefficient))
.add(unref(processedBase));
}, },
nextAt(conversion) { nextAt(conversion) {
let next: DecimalSource = Decimal.add(unref(conversion.currentGain), 1); let next: DecimalSource = Decimal.add(unref(conversion.currentGain), 1);
@ -254,7 +259,10 @@ export function createLinearScaling(
next = conversion.gainModifier.revert(next); next = conversion.gainModifier.revert(next);
} }
next = Decimal.max(0, next); next = Decimal.max(0, next);
return Decimal.sub(next, 1).div(unref(coefficient)).add(unref(base)).max(unref(base)); return Decimal.sub(next, 1)
.div(unref(processedCoefficient))
.add(unref(processedBase))
.max(unref(processedBase));
} }
}; };
} }
@ -273,17 +281,19 @@ export function createLinearScaling(
* | 250 | 5 | * | 250 | 5 |
*/ */
export function createPolynomialScaling( export function createPolynomialScaling(
base: DecimalSource | Ref<DecimalSource>, base: Computable<DecimalSource>,
exponent: DecimalSource | Ref<DecimalSource> exponent: Computable<DecimalSource>
): ScalingFunction { ): ScalingFunction {
const processedBase = convertComputable(base);
const processedExponent = convertComputable(exponent);
return { return {
currentGain(conversion) { currentGain(conversion) {
if (Decimal.lt(conversion.baseResource.value, unref(base))) { if (Decimal.lt(conversion.baseResource.value, unref(processedBase))) {
return 0; return 0;
} }
const gain = Decimal.div(conversion.baseResource.value, unref(base)).pow( const gain = Decimal.div(conversion.baseResource.value, unref(processedBase)).pow(
unref(exponent) unref(processedExponent)
); );
if (gain.isNan()) { if (gain.isNan()) {
@ -297,7 +307,7 @@ export function createPolynomialScaling(
current = conversion.gainModifier.revert(current); current = conversion.gainModifier.revert(current);
} }
current = Decimal.max(0, current); current = Decimal.max(0, current);
return Decimal.root(current, unref(exponent)).times(unref(base)); return Decimal.root(current, unref(processedExponent)).times(unref(processedBase));
}, },
nextAt(conversion) { nextAt(conversion) {
let next: DecimalSource = Decimal.add(unref(conversion.currentGain), 1); let next: DecimalSource = Decimal.add(unref(conversion.currentGain), 1);
@ -305,7 +315,9 @@ export function createPolynomialScaling(
next = conversion.gainModifier.revert(next); next = conversion.gainModifier.revert(next);
} }
next = Decimal.max(0, next); next = Decimal.max(0, next);
return Decimal.root(next, unref(exponent)).times(unref(base)).max(unref(base)); return Decimal.root(next, unref(processedExponent))
.times(unref(processedBase))
.max(unref(processedBase));
} }
}; };
} }
@ -390,10 +402,11 @@ export function createIndependentConversion<S extends ConversionOptions>(
export function setupPassiveGeneration( export function setupPassiveGeneration(
layer: GenericLayer, layer: GenericLayer,
conversion: GenericConversion, conversion: GenericConversion,
rate: ProcessedComputable<DecimalSource> = 1 rate: Computable<DecimalSource> = 1
): void { ): void {
const processedRate = convertComputable(rate);
layer.on("preUpdate", diff => { layer.on("preUpdate", diff => {
const currRate = isRef(rate) ? rate.value : rate; const currRate = unref(processedRate);
if (Decimal.neq(currRate, 0)) { if (Decimal.neq(currRate, 0)) {
conversion.gainResource.value = Decimal.add( conversion.gainResource.value = Decimal.add(
conversion.gainResource.value, conversion.gainResource.value,