Skip to content

math_util

Source: base/tealdef/scripts/math_util.d.tl

Notes

Types of the scalar math helpers in /scripts/mathutil.lua (MathUtil): interpolation, clamping, range mapping and Hermite curves. Loading the file also installs the same functions into Lua's global math table (math.clamp, math.lerp, math.mapClamp, ...).

MathUtil

record global base/tealdef/scripts/math_util.d.tl:3

global record MathUtil

Notes

Scalar math helpers from /scripts/mathutil.lua. Loading that file also defines the same functions on Lua's global math table.

Fields

Name Type Description
llongMax integer Large positive integer constant (92233720368548) used as an "infinite" upper limit.
llongMin integer Large negative integer constant (-92233720368548) used as an "infinite" lower limit.

Functions

lerp<T>(T, T, T) : T base/tealdef/scripts/math_util.d.tl:4

Notes

Linear interpolation a + (b - a) * s.

Parameter Type Description
#1 T Start value a (returned for s = 0).
#2 T End value b (returned for s = 1).
#3 T Interpolation factor s; not clamped.

Returns T

invLerp<T>(T, T, T) : T base/tealdef/scripts/math_util.d.tl:5

Notes

Inverse of lerp, (x - a) / (b - a); returns where x lies between a and b (0 at a, 1 at b).

Parameter Type Description
#1 T The value x.
#2 T Range start a.
#3 T Range end b.

Returns T

Details

Not clamped; values outside the range give results below 0 or above 1.

herp<T>(T, T, T, T, number) : T base/tealdef/scripts/math_util.d.tl:6

Notes

Cubic Hermite interpolation between p0 and p1 with tangents t0 and t1 at parameter s (0 to 1).

Parameter Type Description
#1 T Start point p0.
#2 T End point p1.
#3 T Tangent t0 at the start.
#4 T Tangent t1 at the end.
#5 number Curve parameter s, 0 at p0 and 1 at p1.

Returns T

Details

Works on numbers and on vector types that support +, - and multiplication by a number (the transformator utilities use it with Vec3f).

herpPrime<T>(T, T, T, T, number) : T base/tealdef/scripts/math_util.d.tl:7

Notes

Derivative of herp with respect to s, i.e. the curve tangent at s.

Parameter Type Description
#1 T Start point p0.
#2 T End point p1.
#3 T Tangent t0 at the start.
#4 T Tangent t1 at the end.
#5 number Curve parameter s.

Returns T

randf(number, number) : number base/tealdef/scripts/math_util.d.tl:8

Notes

Uniform random number between the two arguments, based on math.random().

Returns number

log2(number) : number base/tealdef/scripts/math_util.d.tl:9

Notes

Base-2 logarithm.

Returns number

clamp<T>(T, T, T) : T base/tealdef/scripts/math_util.d.tl:10

Notes

Limits a value to a range.

Parameter Type Description
#1 T The value.
#2 T Lower bound.
#3 T Upper bound.

Returns T: The lower bound if the value is below it, the upper bound if above, otherwise the value.

map<T>(T, T, T, T, T) : T base/tealdef/scripts/math_util.d.tl:11

Notes

Maps x linearly from the range a..b to the range c..d, without clamping.

Parameter Type Description
#1 T The value x.
#2 T Source range start a.
#3 T Source range end b.
#4 T Target value c (for x = a).
#5 T Target value d (for x = b).

Returns T

mapClamp<T>(T, T, T, T, T) : T base/tealdef/scripts/math_util.d.tl:12

Notes

Like map, but the result is clamped to the target range (in either order of c and d).

Parameter Type Description
#1 T The value x.
#2 T Source range start a.
#3 T Source range end b.
#4 T Target value c (for x <= a).
#5 T Target value d (for x >= b).

Returns T

Example

-- from base/content/game_mechanics/game_mechanics/industries/industries.script.tl
local spawnProbability = math_util.clamp(math_util.invLerp(rating, 0.25, 0.75), 0, 1)

round(number) : integer base/tealdef/scripts/math_util.d.tl:13

Notes

Rounds to the nearest integer (math.floor(x + 0.5); halves round up).

Returns integer

equal(number, number, number) : boolean base/tealdef/scripts/math_util.d.tl:14

Notes

Returns true if two numbers differ by at most the given epsilon (third argument).

Returns boolean