From 3489c83cc46fe8f8389a732706e29c3488e74076 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Mon, 27 Feb 2023 22:44:30 -0600 Subject: [PATCH] Add util for creating formula from a modifier --- src/data/common.tsx | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/data/common.tsx b/src/data/common.tsx index b0becca..ec3317e 100644 --- a/src/data/common.tsx +++ b/src/data/common.tsx @@ -8,7 +8,7 @@ import { GenericMilestone } from "features/milestones/milestone"; import { displayResource, Resource } from "features/resources/resource"; import type { GenericTree, GenericTreeNode, TreeNode, TreeNodeOptions } from "features/trees/tree"; import { createTreeNode } from "features/trees/tree"; -import { GenericFormula } from "game/formulas"; +import Formula, { FormulaSource, GenericFormula, InvertibleFormula } from "game/formulas"; import type { Modifier } from "game/modifiers"; import type { Persistent } from "game/persistence"; import { DefaultValue, persistent } from "game/persistence"; @@ -490,3 +490,24 @@ export function createFormulaPreview( return formatSmall(formula.evaluate()); }); } + +export function modifierToFormula( + modifier: WithRequired, + base: T +): T; +export function modifierToFormula(modifier: Modifier, base: FormulaSource): GenericFormula; +export function modifierToFormula(modifier: Modifier, base: FormulaSource) { + return new Formula({ + inputs: [base], + evaluate: val => modifier.apply(val), + invert: + "revert" in modifier && modifier.revert != null + ? (val, lhs) => { + if (lhs instanceof Formula && lhs.hasVariable()) { + return lhs.invert(modifier.revert!(val)); + } + throw "Could not invert due to no input being a variable"; + } + : undefined + }); +}