Make conversions pass the variable to the formula
This commit is contained in:
parent
3e0aafcf02
commit
98cb997056
1 changed files with 30 additions and 11 deletions
|
@ -1,7 +1,12 @@
|
||||||
import type { OptionsFunc, Replace } from "features/feature";
|
import type { OptionsFunc, Replace } from "features/feature";
|
||||||
import { setDefault } from "features/feature";
|
import { setDefault } from "features/feature";
|
||||||
import type { Resource } from "features/resources/resource";
|
import type { Resource } from "features/resources/resource";
|
||||||
import { InvertibleFormula } from "game/formulas/types";
|
import Formula from "game/formulas/formulas";
|
||||||
|
import {
|
||||||
|
IntegrableFormula,
|
||||||
|
InvertibleFormula,
|
||||||
|
InvertibleIntegralFormula
|
||||||
|
} from "game/formulas/types";
|
||||||
import type { BaseLayer } from "game/layers";
|
import type { BaseLayer } from "game/layers";
|
||||||
import type { DecimalSource } from "util/bignum";
|
import type { DecimalSource } from "util/bignum";
|
||||||
import Decimal from "util/bignum";
|
import Decimal from "util/bignum";
|
||||||
|
@ -15,9 +20,11 @@ import { computed, unref } from "vue";
|
||||||
export interface ConversionOptions {
|
export interface ConversionOptions {
|
||||||
/**
|
/**
|
||||||
* The formula used to determine how much {@link gainResource} should be earned by this converting.
|
* The formula used to determine how much {@link gainResource} should be earned by this converting.
|
||||||
* When evaluating, the variable will always be overidden to the amount of {@link baseResource}.
|
* The passed value will be a Formula representing the {@link baseResource} variable.
|
||||||
*/
|
*/
|
||||||
formula: InvertibleFormula;
|
formula: (
|
||||||
|
variable: InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula
|
||||||
|
) => InvertibleFormula;
|
||||||
/**
|
/**
|
||||||
* How much of the output resource the conversion can currently convert for.
|
* How much of the output resource the conversion can currently convert for.
|
||||||
* Typically this will be set for you in a conversion constructor.
|
* Typically this will be set for you in a conversion constructor.
|
||||||
|
@ -98,6 +105,7 @@ export type Conversion<T extends ConversionOptions> = Replace<
|
||||||
export type GenericConversion = Replace<
|
export type GenericConversion = Replace<
|
||||||
Conversion<ConversionOptions>,
|
Conversion<ConversionOptions>,
|
||||||
{
|
{
|
||||||
|
formula: InvertibleFormula;
|
||||||
currentGain: ProcessedComputable<DecimalSource>;
|
currentGain: ProcessedComputable<DecimalSource>;
|
||||||
actualGain: ProcessedComputable<DecimalSource>;
|
actualGain: ProcessedComputable<DecimalSource>;
|
||||||
currentAt: ProcessedComputable<DecimalSource>;
|
currentAt: ProcessedComputable<DecimalSource>;
|
||||||
|
@ -120,9 +128,14 @@ export function createConversion<T extends ConversionOptions>(
|
||||||
return createLazyProxy(() => {
|
return createLazyProxy(() => {
|
||||||
const conversion = optionsFunc();
|
const conversion = optionsFunc();
|
||||||
|
|
||||||
|
(conversion as GenericConversion).formula = conversion.formula(
|
||||||
|
Formula.variable(conversion.baseResource)
|
||||||
|
);
|
||||||
if (conversion.currentGain == null) {
|
if (conversion.currentGain == null) {
|
||||||
conversion.currentGain = computed(() => {
|
conversion.currentGain = computed(() => {
|
||||||
let gain = conversion.formula.evaluate(conversion.baseResource.value);
|
let gain = (conversion as GenericConversion).formula.evaluate(
|
||||||
|
conversion.baseResource.value
|
||||||
|
);
|
||||||
gain = Decimal.floor(gain).max(0);
|
gain = Decimal.floor(gain).max(0);
|
||||||
|
|
||||||
if (unref(conversion.buyMax) === false) {
|
if (unref(conversion.buyMax) === false) {
|
||||||
|
@ -136,14 +149,14 @@ export function createConversion<T extends ConversionOptions>(
|
||||||
}
|
}
|
||||||
if (conversion.currentAt == null) {
|
if (conversion.currentAt == null) {
|
||||||
conversion.currentAt = computed(() => {
|
conversion.currentAt = computed(() => {
|
||||||
return conversion.formula.invert(
|
return (conversion as GenericConversion).formula.invert(
|
||||||
Decimal.floor(unref((conversion as GenericConversion).currentGain))
|
Decimal.floor(unref((conversion as GenericConversion).currentGain))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (conversion.nextAt == null) {
|
if (conversion.nextAt == null) {
|
||||||
conversion.nextAt = computed(() => {
|
conversion.nextAt = computed(() => {
|
||||||
return conversion.formula.invert(
|
return (conversion as GenericConversion).formula.invert(
|
||||||
Decimal.floor(unref((conversion as GenericConversion).currentGain)).add(1)
|
Decimal.floor(unref((conversion as GenericConversion).currentGain)).add(1)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -205,7 +218,9 @@ export function createIndependentConversion<S extends ConversionOptions>(
|
||||||
|
|
||||||
if (conversion.currentGain == null) {
|
if (conversion.currentGain == null) {
|
||||||
conversion.currentGain = computed(() => {
|
conversion.currentGain = computed(() => {
|
||||||
let gain = conversion.formula.evaluate(conversion.baseResource.value);
|
let gain = (conversion as unknown as GenericConversion).formula.evaluate(
|
||||||
|
conversion.baseResource.value
|
||||||
|
);
|
||||||
gain = Decimal.floor(gain).max(conversion.gainResource.value);
|
gain = Decimal.floor(gain).max(conversion.gainResource.value);
|
||||||
if (unref(conversion.buyMax) === false) {
|
if (unref(conversion.buyMax) === false) {
|
||||||
gain = gain.min(Decimal.add(conversion.gainResource.value, 1));
|
gain = gain.min(Decimal.add(conversion.gainResource.value, 1));
|
||||||
|
@ -216,7 +231,9 @@ export function createIndependentConversion<S extends ConversionOptions>(
|
||||||
if (conversion.actualGain == null) {
|
if (conversion.actualGain == null) {
|
||||||
conversion.actualGain = computed(() => {
|
conversion.actualGain = computed(() => {
|
||||||
let gain = Decimal.sub(
|
let gain = Decimal.sub(
|
||||||
conversion.formula.evaluate(conversion.baseResource.value),
|
(conversion as unknown as GenericConversion).formula.evaluate(
|
||||||
|
conversion.baseResource.value
|
||||||
|
),
|
||||||
conversion.gainResource.value
|
conversion.gainResource.value
|
||||||
).max(0);
|
).max(0);
|
||||||
|
|
||||||
|
@ -227,9 +244,11 @@ export function createIndependentConversion<S extends ConversionOptions>(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
setDefault(conversion, "convert", function () {
|
setDefault(conversion, "convert", function () {
|
||||||
const amountGained = unref((conversion as GenericConversion).actualGain);
|
const amountGained = unref((conversion as unknown as GenericConversion).actualGain);
|
||||||
conversion.gainResource.value = unref((conversion as GenericConversion).currentGain);
|
conversion.gainResource.value = unref(
|
||||||
(conversion as GenericConversion).spend(amountGained);
|
(conversion as unknown as GenericConversion).currentGain
|
||||||
|
);
|
||||||
|
(conversion as unknown as GenericConversion).spend(amountGained);
|
||||||
conversion.onConvert?.(amountGained);
|
conversion.onConvert?.(amountGained);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue