2023-04-24 15:44:16 +00:00
import { _ as e , c as a , o as s , N as o } from "./chunks/framework.0799945b.js" ; const f = JSON . parse ( '{"title":"Formulas","description":"","frontmatter":{},"headers":[],"relativePath":"guide/important-concepts/formulas.md","lastUpdated":1682351002000}' ) , t = { name : "guide/important-concepts/formulas.md" } , n = o ( ` <h1 id="formulas" tabindex="-1">Formulas <a class="header-anchor" href="#formulas" aria-label="Permalink to "Formulas""> </a></h1><p>Profectus utilizes formulas for various features, such as increasing requirements for repeatables and challenges or determining resource gains in conversions. These formulas often need to be inverted or integrated to enable features like buying multiple levels of a repeatable at once or determining when a conversion will increase resource gains. The Formula class can handle these operations, supporting every function Decimal does, while tracking the operations internally.</p><p>For example, a cost function like <code>Decimal.pow(this.amount, 1.05).times(100)</code> can be represented using a Formula: <code>Formula.variable(this.amount).pow(1.05).times(100)</code>.</p><div class="language-ts"><button title="Copy Code" class="copy"></button><span class="lang">ts</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">const</span><span style="color:#A6ACCD;"> myRepeatable </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">createRepeatable</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">=></span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">{</span></span>
2023-04-23 19:47:59 +00:00
< span class = "line" > < span style = "color:#A6ACCD;" > < / s p a n > < s p a n s t y l e = " c o l o r : # F 0 7 1 7 8 ; " > r e q u i r e m e n t s < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > : < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > < / s p a n > < s p a n s t y l e = " c o l o r : # 8 2 A A F F ; " > c r e a t e C o s t R e q u i r e m e n t < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ( < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > ( ) < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > < / s p a n > < s p a n s t y l e = " c o l o r : # C 7 9 2 E A ; " > = & g t ; < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ( < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > { < / s p a n > < / s p a n >
< span class = "line" > < span style = "color:#A6ACCD;" > < / s p a n > < s p a n s t y l e = " c o l o r : # F 0 7 1 7 8 ; " > r e s o u r c e < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > : < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > p o i n t s < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > , < / s p a n > < / s p a n >
< span class = "line" > < span style = "color:#A6ACCD;" > < / s p a n > < s p a n s t y l e = " c o l o r : # F 0 7 1 7 8 ; " > c o s t < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > : < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > F o r m u l a < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > . < / s p a n > < s p a n s t y l e = " c o l o r : # 8 2 A A F F ; " > v a r i a b l e < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ( m y R e p e a t a b l e < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > . < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > a m o u n t ) < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > . < / s p a n > < s p a n s t y l e = " c o l o r : # 8 2 A A F F ; " > p o w _ b a s e < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ( < / s p a n > < s p a n s t y l e = " c o l o r : # F 7 8 C 6 C ; " > 1 . 0 5 < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ) < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > . < / s p a n > < s p a n s t y l e = " c o l o r : # 8 2 A A F F ; " > t i m e s < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ( < / s p a n > < s p a n s t y l e = " c o l o r : # F 7 8 C 6 C ; " > 1 0 0 < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ) < / s p a n > < / s p a n >
< span class = "line" > < span style = "color:#A6ACCD;" > < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > } < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ) ) < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > , < / s p a n > < / s p a n >
< span class = "line" > < span style = "color:#A6ACCD;" > < / s p a n > < s p a n s t y l e = " c o l o r : # F 0 7 1 7 8 ; " > m a x i m i z e < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > : < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > < / s p a n > < s p a n s t y l e = " c o l o r : # F F 9 C A C ; " > t r u e < / s p a n > < / s p a n >
< span class = "line" > < span style = "color:#89DDFF;" > } < / s p a n > < s p a n s t y l e = " c o l o r : # A 6 A C C D ; " > ) ) < / s p a n > < s p a n s t y l e = " c o l o r : # 8 9 D D F F ; " > ; < / s p a n > < / s p a n > < / c o d e > < / p r e > < / d i v > < h 2 i d = " s o f t c a p s " t a b i n d e x = " - 1 " > S o f t c a p s < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # s o f t c a p s " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; S o f t c a p s & q u o t ; " > < / a > < / h 2 > < p > O f t e n g a m e s i n c o r p o r a t e & q u o t ; s o f t c a p s & q u o t ; o n f o r m u l a s - t h a t m e a n s m a k i n g t h e f o r m u l a s t e p - w i s e , w h e r e e v e r y t h i n g a b o v e a c e r t a i n v a l u e h a s a n a d d i t i o n a l o p e r a t i o n o n i t t h a t m a k e s i t s c a l e d i f f e r e n t l y . F o r m u l a s s u p p o r t t h i s v i a < c o d e > F o r m u l a . s t e p < / c o d e > , w h i c h t a k e s a t h r e s h o l d v a l u e a n d a f u n c t i o n t o m o d i f y t h e f o r m u l a a p p r o p r i a t e l y . T h e f u n c t i o n g e t s a f o r m u l a t h a t w i l l c o n t a i n a v a r i a b l e f o r t h e a m o u n t < e m > a b o v e < / e m > t h e t h r e s h o l d o f t h e o r i g i n a l v a l u e . W h a t e v e r r e s u l t i s r e t u r n e d w i l l t h e n b e a d d e d b a c k o n t o t h e t h r e s h o l d t o a c h i e v e t h e s o f t c a p p e d v a l u e . T a k e , f o r e x a m p l e , a f o r m u l a < c o d e > F o r m u l a . v a r i a b l e ( ) . d i v ( 1 0 ) . s q r t ( ) < / c o d e > t h a t w e w a n t t o a p p l y a s o f t c a p t o - l e t & # 3 9 ; s s a y i t s h o u l d s q u a r e r o o t e v e r y t h i n g a f t e r 1 e 1 0 0 . T h e s o f t c a p p e d f o r m u l a w i l l l o o k l i k e t h i s : < c o d e > F o r m u l a . v a r i a b l e ( ) . d i v ( 1 0 ) . s q r t ( ) . s t e p ( 1 e 1 0 0 , f = & g t ; f . s q r t ( ) ) < / c o d e > . < / p > < h 2 i d = " l i m i t a t i o n s " t a b i n d e x = " - 1 " > L i m i t a t i o n s < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # l i m i t a t i o n s " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; L i m i t a t i o n s & q u o t ; " > < / a > < / h 2 > < h 3 i d = " s i n g l e - v a r i a b l e " t a b i n d e x = " - 1 " > S i n g l e V a r i a b l e < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # s i n g l e - v a r i a b l e " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; S i n g l e V a r i a b l e & q u o t ; " > < / a > < / h 3 > < p > F o r i n v e r t i n g a n d i n t e g r a t i n g , f o r m u l a s s h o u l d h a v e a s i n g l e v a r i a b l e , w h i c h s e r v e s a s t h e i n p u t . A l t h o u g h o t h e r p a r t s o f t h e f o r m u l a c a n b e c o m p u t e d v a l u e s , t h e y m u s t n o t b e d e p e n d e n t o n t h e i n p u t v a r i a b l e f o r i n v e r t i n g o r i n t e g r a t i n g p u r p o s e s . F o r m u l a s w o r k c o r r e c t l y a s l o n g a s c h a n g i n g o t h e r p a r t s o f t h e f o r m u l a d o e s n & # 3 9 ; t a f f e c t t h e i n p u t v a r i a b l e . < / p > < p > T h e v a r i a b l e i s d e f i n e d b y w r a p p i n g t h e i n p u t t o t h e f o r m u l a i n < c o d e > F o r m u l a . v a r i a b l e < / c o d e > . I n t h e c o s t r e q u i r e m e n t e x a m p l e a b o v e , t h e r e p e a t a b l e & # 3 9 ; s < c o d e > a m o u n t < / c o d e > i s t h e i n p u t t o t h e v a r i a b l e . T h a t m e a n s i n v e r t i n g t h e f u n c t i o n w o u l d c a l c u l a t e t h e a m o u n t b a s e d o n t h e c o s t , a n d i n t e g r a t i n g w o u l d b e f i n d i n g t h e a r e a u n d e r t h e c u r v e w h e r e t h e r e p e a t a b l e & # 3 9 ; s < c o d e > a m o u n t < / c o d e > i s t h e x a x i s a n d t h e o u t p u t o f t h e f o r m u l a i s t h e y a x i s . < / p > < p > I f t h e c o s t f o r m u l a h a d n o t m a r k e d a v a r i a b l e , e . g . < c o d e > F o r m u l a . p o w ( 1 . 0 5 , m y R e p e a t a b l e . a m o u n t ) . t i m e s ( 1 0 0 ) < / c o d e > t h e n t h e f o r m u l a w o u l d n o t b e i n v e r t i b l e o r i n t e g r a b l e , a n d w o u l d t h r o w a n e r r o r i f e i t h e r o p e r a t i o n w a s t r i e d . W h i l e < c o d e > e v a l u a t e ( ) < / c o d e > w i l l w o r k a s e x p e c t e d , p a s s i n g a v a l u e t o < c o d e > e v a l u a t e ( ) < / c o d e > w i l l n o t , a s i t w i l l n o t k n o w w h a t v a l u e t o o v e r r i d e . < / p > < h 3 i d = " i n v e r t i b i l i t y - a n d - i n t e g r a b i l i t y " t a b i n d e x = " - 1 " > I n v e r t i b i l i t y a n d I n t e g r a b i l i t y < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # i n v e r t i b i l i t y - a n d - i n t e g r a b i l i t y " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; I n v e r t i b i l i t y a n d I n t e g r a b i l i t y & q u o t ; " > < / a > < / h 3 > < p > C e r t a i n o p e r a t i o n s m a y n o t s u p p o r t i n v e r t i n g o r i n t e g r a t i n g . F u n c t i o n s s u c h a s r o u n d i n g o r c l a m p i n g a r e n o n - i n v e r t i b l e , w h i l e o t h e r s l i k e s u p e r - l o g a r e n o n - i n t e g r a b l e . Y o u m a y o n l y u s e a s i n g l e c o m p l e x o p e r a t i o n i n a d d i t i o n t o a d d , s u b , m u l t , a n d d i v o p e r a t i o n s . F o r f o r m u l a s w i t h t w o c o m p l e x o p e r a t i o n s , a < a h r e f = " # c u s t o m - f o r m u l a s " > c u s t o m f o r m u l a < / a > i s r e q u i r e d . < / p > < h 3 i d = " s p e n d i n g - r e s o u r c e s " t a b i n d e x = " - 1 " > S p e n d i n g R e s o u r c e s < a c l a s s = " h e a d e r - a n c h o r " h r e f = " # s p e n d i n g - r e s o u r c e s " a r i a - l a b e l = " P e r m a l i n k t o & q u o t ; S p e n d i n g R e s o u r c e s & q u o t ; " > < / a > < / h 3 > < p > W h e n w o r k i n g w i t h f o r m u l a s , t h e < c o d e > s p e n d R e s o u r c e s < / c o d e > p r o p e r t y d e t e r m i n e s w h e t h e r t h e f o r m u l a n e e d s t o b e i n v e r t i b l e o r i n t e g r a b l e . T h e p r o p e r t y i s u s e d i n t w o u t i l i t i e s : < a h r e f = " / a p i / m o d u l e s / g a m e / f o r m u l a s / f o r m u l a s # c a l c u l a t e m a x a f f o r d a b l e " > c a l c u l a t e M a x A f f o r d a b l e < / a > a n d < a h r e f = " / a p i / m o d u l e s / g a m e / f o r m u l a s / f o r m u l a s # c a l c u l a t e c o s t " > c a l c u l a t e C o s t < / a > . T h e s e u t i l i t i e s a r e u l t i m a t e l y e m p l o y e d w h e r e v e r f o r m u l a s a r e u s e d , s u c h a s i n c o s t r e q u i r e m e n t s o r c o n v e r s i o n s . < / p > < p > S p e n d i n g r e s o u r c e s r e f e r s t o w h e t h e r m a x a f f o r d a b i l i t y a n d c o s t c a l c u l a t i o n s s h o u l d a c c o u n t f o r r e s o u r c e s s p e n t . I f s p e n d i n g r e s o u r c e s i