Made maxAffordable be relative rather than absolute

This commit is contained in:
thepaperpilot 2023-02-05 00:24:50 -06:00
parent 60625ec9a0
commit 536b3e0f17

View file

@ -1,7 +1,7 @@
import { Resource } from "features/resources/resource"; import { Resource } from "features/resources/resource";
import Decimal, { DecimalSource } from "util/bignum"; import Decimal, { DecimalSource } from "util/bignum";
import { Computable, convertComputable, ProcessedComputable } from "util/computed"; import { Computable, convertComputable, ProcessedComputable } from "util/computed";
import { computed, ref, Ref, unref } from "vue"; import { computed, ComputedRef, ref, Ref, unref } from "vue";
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
export type GenericFormula = Formula<any>; export type GenericFormula = Formula<any>;
@ -776,11 +776,14 @@ export default class Formula<T extends [FormulaSource] | FormulaSource[]> {
| undefined; | undefined;
private readonly internalHasVariable: boolean; private readonly internalHasVariable: boolean;
public readonly innermostVariable: ProcessedComputable<DecimalSource> | undefined;
constructor(options: FormulaOptions<T>) { constructor(options: FormulaOptions<T>) {
// Variable case // Variable case
if ("variable" in options) { if ("variable" in options) {
this.inputs = [options.variable] as T; this.inputs = [options.variable] as T;
this.internalHasVariable = true; this.internalHasVariable = true;
this.innermostVariable = options.variable;
return; return;
} }
// Constant case // Constant case
@ -811,6 +814,9 @@ export default class Formula<T extends [FormulaSource] | FormulaSource[]> {
this.internalHasVariable = this.internalHasVariable =
numVariables === 1 || (numVariables === 0 && hasVariable === true); numVariables === 1 || (numVariables === 0 && hasVariable === true);
if (this.internalHasVariable) {
this.innermostVariable = variable?.innermostVariable;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
this.internalInvert = this.internalInvert =
@ -2004,7 +2010,7 @@ export function calculateMaxAffordable(
} }
return Decimal.floor( return Decimal.floor(
formula.invertIntegral(Decimal.add(resource.value, formula.evaluateIntegral())) formula.invertIntegral(Decimal.add(resource.value, formula.evaluateIntegral()))
); ).sub(unref(formula.innermostVariable) ?? 0);
} else { } else {
if (!formula.isInvertible()) { if (!formula.isInvertible()) {
throw "Cannot calculate max affordable of non-invertible formula"; throw "Cannot calculate max affordable of non-invertible formula";
@ -2013,13 +2019,11 @@ export function calculateMaxAffordable(
} }
}); });
const cost = computed(() => { const cost = computed(() => {
const newValue = maxAffordable.value.add(unref(formula.innermostVariable) ?? 0);
if (unref(computedSpendResources)) { if (unref(computedSpendResources)) {
return Decimal.sub( return Decimal.sub(formula.evaluateIntegral(newValue), formula.evaluateIntegral());
formula.evaluateIntegral(maxAffordable.value),
formula.evaluateIntegral()
);
} else { } else {
return formula.evaluate(maxAffordable.value); return formula.evaluate(newValue);
} }
}); });
return { maxAffordable, cost }; return { maxAffordable, cost };