Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Preset Formulas and Math Parser Documentation

glilienthal edited this page Apr 21, 2020 · 10 revisions

Overview

As you may or may not be aware, many of the formula that control aspects of KCT’s calculations for time or costs are directly modifiable as part of the Presets system. Parameters such as the cost of purchasing upgrades, the rate that tech nodes unlock, and even the build point algorithms themselves can be redefined and tweaked. These formula have several peculiarities and restrictions, which this document aims to explain.

Available Operators

The operations available are restricted to:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • ^ (exponent)
  • % (integer modulus)

Order of Operations (or lack thereof)

The math parser operates not with the traditional order of operations (PEMDAS), but instead formula are parsed from left to right. This means a string such as “1+1*2” will be evaluated to “4” (as 1+1=2 and 2*2=4) rather than “3” if parsed using order of operations. There are two ways to get around this restriction. The first is to arrange your formula such that the order you want is the order it is parsed, in this case by moving the multiplication to be first, ie “1*2+1” which will evaluate to “3”. When that isn't an option, parenthesis can be used to evaluate the contents first, ie “1+(1*2)” will be evaluated to “3”.

Available Function Operations

Several functions in addition to the standard operations are made available. These functions (where x and y are any other valid formula constructs) are:

  • ( and ) = parenthesis. Anything inside is computed first, as soon as they are encountered
  • l(x) = natural log of x
  • L(x) = log base 10 of x
  • min(x,y) = either x or y, whichever is lower (more negative)
  • max(x,y) = either x or y, whichever is higher (more positive)
  • sign(x) = 1 if x>=0 and -1 if x<0
  • abs(x) = x if x>=0 or -x if x<0 (the magnitude of x)

Functions can be nested within other functions, for example “min(l(10), L(10))” will take the minimum value of the natural log of 10 and the log base 10 of 10 (which is 1 for the L(10) case).

Variables

Several variables are available for most, if not all, functions representing related data from KSP such as vessel cost and mass or number of upgrades purchased. These variables change per function and will be covered below for each particular formula. All variables must be framed in square brackets, such as “[N]” (without quotes). They are replaced with the appropriate value when the parser is called. Note that not all of the formula listed below use every variable available in their default states; some have more variables exposed than they use.


Available formula as of KCT v1.3 (more are being exposed with each update):

NodeFormula = "2^([N]+1) / 86400"
    Purpose: Defines the rate at which tech nodes are progressed. Negative means disabled.
    Variables: N=number of Development upgrades, S=cost of a node in science points, R=R&D center level (0-2), O=Overall multiplier, 
    I=index in list (if not applied equally to all tech items, buttons appear to let the user reorganize the list), All "Crew Variables"

UpgradeFundsFormula = "min(2^([N]+4) * 1000, 1024000)"
    Purpose: Defines the cost of buying upgrades with funds. Negative means disabled.
    Variables: N=number of upgrades purchased with funds

UpgradeScienceFormula = "min(2^([N]+2) * 1.0, 512)"
    Purpose: Defines the cost of buying upgrades with science. Negative means disabled.
    Variables: N=number of upgrades purchased with science

ResearchFormula = "[N]*0.5/86400"
    Purpose: Defines how much science is earned for building vessels. Negative means disabled.
    Variables: N=number of upgrades of Research purchased, R=R&D center level, All "Crew Variables"

EffectivePartFormula = "min([C]/([I] + ([B]*([U]+1))), [C])"
    Purpose: Controls the “effective cost” of a part for determining build point values.
    Variables: C=cost, c=dry cost, M=wet mass, m=dry mass, U=part tracker, O=overall multiplier, I=inventory effect (0 if not in inv), B=build effect

ProceduralPartFormula = "(([C]-[A]) + ([A]*10/max([I],1))) / max([B]*([U]+1),1)"
    Purpose: Controls the “effective cost” of procedural parts.
    Variables: C=cost, c=dry cost, A=cost covered by inv, M=wet mass, m=dry mass, U=part tracker, O=overall multiplier, I=inventory effect (0 if not in inv), B=build effect

BPFormula = "([E]^(1/2))*2000*[O]"
    Purpose: Determines the actual BP value of a craft from the total effective cost.
    Variables: E=total effective cost, O=overall multiplier

KSCUpgradeFormula = "([C]^(1/2))*1000*[O]"
    Purpose: Defines the BP value for KSC building upgrades
    Variables: C=upgrade cost, O=overall multiplier

ReconditioningFormula = "min([M]*[O]*[E], [X])*abs([RE]-[S]) / (3-[L])"
    Purpose: Defines the BP value of reconditioning, rollout, and recovery items.
    Variables: O=overall multiplier, E=reconditioning effect setting, X=maximum reconditioning setting, M=loaded vessel mass, m=empty vessel mass, C=loaded vessel cost, c=empty vessel cost, VAB=1 if a VAB vessel (useless currently), BP=total vessel BP, L=launch facility upgrade level, EL=Editor upgrade level, RE=1 if reconditioning or 0 if rollout/rollback/recovery, S=Rollout/Recon split (between 0 and 1), All "Crew Variables"

BuildRateFormula = "(([I]+1)*0.05*[N] + max(0.1-[I], 0))*sign(2*[L]-[I]+1)"
	Purpose: Defines build rates and how they change with upgrades
	Variables: N=num upgrades, I=rate index (0 based), L=VAB/SPH upgrade level, R=R&D level, S=Number of unlocked tech nodes, All "Crew Variables"
	Note: This uses some tricks to achieve various goals. First, the max() section sets the first build rate to start at 0.1 and all the others to start at 0. Second, the *sign() section limits the number of rates you can have based on the VAB upgrade level (2 rates at level 0, 4 rates at 1, 6 at 2). It works because a negative rate is considered "disabled"

SimCostFormula = "max([C]/50000 * min([PM]/[KM], 80) * ([S]/10 + 1) * ([A]/10 + 1) * ([L]^0.5) * 100, 500)" 
	Purpose: Defines the cost of simulations around a planetary body
	Variables: M=body mass, PM = parent mass, A = presence of atmosphere (1 or 0), m = mass of vessel, C = cost of vessel, s = # times simulated this editor session, SMA = ratio parent planet SMA to Kerbin SMA, L = Simulation length in seconds, KM = Kerbin Mass, S = 1/0 if a satellite(moon)
	
KerbinSimCostFormula = "max([C]/50000 * ([L]^0.5) * 10, 100)"
	Purpose: Defines the cost of simulations that start from the launchpad/runway on Kerbin/Earth
	Variables: SAME AS SimCostFormula
	
UpgradeResetFormula = "2*([N]+1)"
	Purpose: Defines the number of upgrades that must be temporarily spent to reset the upgrade point allocation
	Variables: N=number of times upgrades have been reset. Negative values disable.

InventorySaleFormula = "([V]+[P] / 10000)^(0.5)"
	Purpose: Gives the *total* number of upgrade points received for clearing out the inventory
	Variables: V=Current Inventory value in funds, P=Value of all previous cleared inventories combined

RolloutCostFormula = "0"
	Purpose: Adds a cost for rolling out a vessel (requested by RP-0 devs) which is taken in 10% increments at each 10% of time. If you can't afford it, rollout is halted.
	Variables: M=Vessel loaded mass, m=vessel empty mass, C=vessel loaded cost, c=vessel empty cost, BP=vessel BPs, EL=editor level, L=launch site level (pad), VAB=1 if VAB craft, 0 if SPH (useless currently), All "Crew Variables"

NewLaunchPadCostFormula = "100000*([N]^3)"
	Purpose: Defines the cost of purchasing new launchpads.
	Variables: N=number of existing launchpads (including the default pad)

Note: Several formulas include "Crew Variables". Rather than list them several times above, they are listed once below:

PiK = Number of "Pilot" Kerbals currently Available at KSC (does not include assigned Kerbals)
EnK = Number of "Engineer" Kerbals currently Available at KSC
ScK = Number of "Scientist" Kerbals currently Available at KSC

TPiK = Total number of living "Pilot" Kerbals (Available at KSC and Assigned)
TEnK = Total number of living "Engineer" Kerbals
TScK = Total number of living "Scientist" Kerbals

PiL = Total number of "Pilot" Kerbal levels Available at KSC (does not include assigned Kerbals)
EnL = Total number of "Engineer" Kerbal levels Available at KSC
ScL = Total number of "Scientist" Kerbal levels Available at KSC

TPiL = Total number of "Pilot" levels for all living Kerbals (Available and Assigned)
TEnL = Total number of "Engineer" levels for all living Kerbals
TScL = Total number of "Scientist" levels for all living Kerbals

Example: You have 3 pilots (a level 1 and a level 3 at KSC and a level 2 on a ship), 2 engineers (a level 3 at KSC and a level 4 on a ship), and 1 scientist (a level 2 on a ship somewhere). This would be the breakdown (sorted by class):

Pilots: PiK = 2, TPiK = 3, PiL = 4, TPiL = 6
Engineers: EnK = 1, TEnK = 2, EnL = 3, TEnL = 7
Scientists: ScK = 0, TScK = 1, ScL = 0, TScL = 2