Standard Library#
A standard library of mathematics and printing for server side functions, and of the colors other services use.
Note
Vectors are position and direction tuples, and rotations are unit quaternion tuples in the order x, y, z, w the SpaceCenter service uses. Angles are in radians unless stated otherwise.
- StdLib.getPi()#
The constant pi.
- Property:
Read-only, cannot be set
- Return type:
number
- StdLib.getE()#
The constant e, the base of the natural logarithm.
- Property:
Read-only, cannot be set
- Return type:
number
- StdLib.abs(x)#
The absolute value of a number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.sign(x)#
The sign of a number: -1 if it is negative, 1 if it is positive, and 0 if it is zero or not a number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.copySign(x, y)#
The magnitude of the first number with the sign of the second.
- Arguments:
x (
number) – The number giving the magnitude.y (
number) – The number giving the sign.
- Return type:
number
- StdLib.isNan(x)#
Whether a number is not a number.
- Arguments:
x (
number) – The number.
- Return type:
boolean
- StdLib.isFinite(x)#
Whether a number is neither infinite nor not a number.
- Arguments:
x (
number) – The number.
- Return type:
boolean
- StdLib.floor(x)#
The largest whole number less than or equal to the given number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.ceiling(x)#
The smallest whole number greater than or equal to the given number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.round(x[, digits])#
The given number rounded to the given number of decimal places, with a halfway value rounded to the nearer even number.
- Arguments:
x (
number) – The number.digits (
number) – The number of decimal places to round to. Negative to round to tens, hundreds and beyond. Defaults to0.
- Return type:
number
Note
Throws an exception if the rounded value is too large for a double.
- StdLib.roundHalfAwayFromZero(x[, digits])#
The given number rounded to the given number of decimal places, with a halfway value rounded away from zero.
- Arguments:
x (
number) – The number.digits (
number) – The number of decimal places to round to. Negative to round to tens, hundreds and beyond. Defaults to0.
- Return type:
number
Note
Throws an exception if the rounded value is too large for a double.
- StdLib.roundHalfUp(x[, digits])#
The given number rounded to the given number of decimal places, with a halfway value rounded toward positive infinity.
- Arguments:
x (
number) – The number.digits (
number) – The number of decimal places to round to. Negative to round to tens, hundreds and beyond. Defaults to0.
- Return type:
number
Note
Throws an exception if the rounded value is too large for a double.
- StdLib.sqrt(x)#
The square root of a number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.hypot(x, y)#
The length of the hypotenuse of a right triangle with the given sides.
- Arguments:
x (
number) – The first side.y (
number) – The second side.
- Return type:
number
- StdLib.sin(x)#
The sine of an angle, in radians.
- Arguments:
x (
number) – The angle.
- Return type:
number
- StdLib.cos(x)#
The cosine of an angle, in radians.
- Arguments:
x (
number) – The angle.
- Return type:
number
- StdLib.tan(x)#
The tangent of an angle, in radians.
- Arguments:
x (
number) – The angle.
- Return type:
number
- StdLib.asin(x)#
The angle, in radians, whose sine is the given number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.acos(x)#
The angle, in radians, whose cosine is the given number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.atan(x)#
The angle, in radians, whose tangent is the given number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.atan2(y, x)#
The angle, in radians, between the positive x axis and the point
(x, y).- Arguments:
y (
number) – The y coordinate.x (
number) – The x coordinate.
- Return type:
number
- StdLib.log(x[, newBase])#
The logarithm of a number, natural unless another base is given.
- Arguments:
x (
number) – The number.newBase (
number) – The base of the logarithm. Defaults to2.718281828459045.
- Return type:
number
- StdLib.log10(x)#
The base 10 logarithm of a number.
- Arguments:
x (
number) – The number.
- Return type:
number
- StdLib.exp(x)#
The number e raised to the given power.
- Arguments:
x (
number) – The power.
- Return type:
number
- StdLib.min(x, y)#
The smaller of two numbers.
- Arguments:
x (
number) – The first number.y (
number) – The second number.
- Return type:
number
- StdLib.max(x, y)#
The larger of two numbers.
- Arguments:
x (
number) – The first number.y (
number) – The second number.
- Return type:
number
- StdLib.clamp(x, min, max)#
The given number limited to the given range.
- Arguments:
x (
number) – The number.min (
number) – The minimum of the range.max (
number) – The maximum of the range.
- Return type:
number
Note
Throws an exception if the minimum is greater than the maximum.
- StdLib.lerp(a, b, t)#
Linear interpolation between two numbers.
- Arguments:
a (
number) – The number when t is 0.b (
number) – The number when t is 1.t (
number) – The interpolation parameter.
- Return type:
number
Note
The parameter is not limited to the range 0 to 1, and a value outside it extrapolates.
- StdLib.inverseLerp(a, b, x)#
The interpolation parameter of a number between two others, limited to the range 0 to 1.
- Arguments:
a (
number) – The number at 0.b (
number) – The number at 1.x (
number) – The number.
- Return type:
number
Note
Returns 0 if the two numbers are equal.
- StdLib.degreesToRadians(x)#
The angle in radians of an angle in degrees.
- Arguments:
x (
number) – The angle in degrees.
- Return type:
number
- StdLib.radiansToDegrees(x)#
The angle in degrees of an angle in radians.
- Arguments:
x (
number) – The angle in radians.
- Return type:
number
- StdLib.vectorAdd(u, v)#
The sum of two vectors.
- Arguments:
u (
[number, number, number]) – The first vector.v (
[number, number, number]) – The second vector.
- Return type:
[number, number, number]
- StdLib.vectorSubtract(u, v)#
The difference of two vectors.
- Arguments:
u (
[number, number, number]) – The first vector.v (
[number, number, number]) – The second vector.
- Return type:
[number, number, number]
- StdLib.vectorScale(v, scale)#
A vector scaled by a number.
- Arguments:
v (
[number, number, number]) – The vector.scale (
number) – The scaling factor.
- Return type:
[number, number, number]
- StdLib.vectorDot(u, v)#
The dot product of two vectors.
- Arguments:
u (
[number, number, number]) – The first vector.v (
[number, number, number]) – The second vector.
- Return type:
number
- StdLib.vectorCross(u, v)#
The cross product of two vectors.
- Arguments:
u (
[number, number, number]) – The first vector.v (
[number, number, number]) – The second vector.
- Return type:
[number, number, number]
- StdLib.vectorMagnitude(v)#
The magnitude (length) of a vector.
- Arguments:
v (
[number, number, number]) – The vector.
- Return type:
number
- StdLib.vectorNormalize(v)#
A vector scaled to a magnitude of 1.
- Arguments:
v (
[number, number, number]) – The vector.
- Return type:
[number, number, number]
Note
Throws an exception if the vector is zero.
- StdLib.vectorDistance(u, v)#
The distance between two positions.
- Arguments:
u (
[number, number, number]) – The first position.v (
[number, number, number]) – The second position.
- Return type:
number
- StdLib.vectorAngle(u, v)#
The angle between two vectors, in radians, between 0 and \(\pi\).
- Arguments:
u (
[number, number, number]) – The first vector.v (
[number, number, number]) – The second vector.
- Return type:
number
Note
Throws an exception if either vector is zero.
- StdLib.vectorLerp(u, v, t)#
Linear interpolation between two vectors.
- Arguments:
u (
[number, number, number]) – The vector when t is 0.v (
[number, number, number]) – The vector when t is 1.t (
number) – The interpolation parameter.
- Return type:
[number, number, number]
Note
The parameter is not limited to the range 0 to 1, and a value outside it extrapolates.
- StdLib.quaternionMultiply(q, r)#
The composition of two rotations: the rotation r followed by the rotation q.
- Arguments:
q (
[number, number, number, number]) – The second rotation.r (
[number, number, number, number]) – The first rotation.
- Return type:
[number, number, number, number]
Note
Throws an exception if either quaternion is zero.
- StdLib.quaternionInverse(q)#
The inverse of a rotation.
- Arguments:
q (
[number, number, number, number]) – The rotation.
- Return type:
[number, number, number, number]
Note
Throws an exception if the quaternion is zero.
- StdLib.quaternionRotateVector(q, v)#
A vector rotated by a rotation.
- Arguments:
q (
[number, number, number, number]) – The rotation.v (
[number, number, number]) – The vector.
- Return type:
[number, number, number]
Note
Throws an exception if the quaternion is zero.
- StdLib.quaternionFromAxisAngle(axis, angle)#
The rotation about an axis by an angle.
- Arguments:
axis (
[number, number, number]) – The axis of rotation.angle (
number) – The angle of rotation, in radians.
- Return type:
[number, number, number, number]
Note
Throws an exception if the axis is zero.
- StdLib.quaternionToAxisAngle(q)#
The axis and angle of a rotation.
- Arguments:
q (
[number, number, number, number]) – The rotation.
- Return type:
Note
Throws an exception if the quaternion is zero.
- StdLib.quaternionFromToRotation(u, v)#
The shortest rotation that takes one direction onto another.
- Arguments:
u (
[number, number, number]) – The direction to rotate from.v (
[number, number, number]) – The direction to rotate to.
- Return type:
[number, number, number, number]
Note
Opposite directions give a half turn about an arbitrary perpendicular axis. Throws an exception if either vector is zero.
- StdLib.quaternionLookRotation(forward, up)#
The rotation that takes the z axis onto a forward direction, and the y axis toward an up direction.
- Arguments:
forward (
[number, number, number]) – The direction of the z axis.up (
[number, number, number]) – The direction the y axis points toward.
- Return type:
[number, number, number, number]
Note
The up direction need not be perpendicular to the forward direction. Throws an exception if either vector is zero, or if they are parallel.
- StdLib.quaternionAngle(q, r)#
The angle of the rotation that takes one rotation to another, in radians, between 0 and \(\pi\).
- Arguments:
q (
[number, number, number, number]) – The first rotation.r (
[number, number, number, number]) – The second rotation.
- Return type:
number
Note
Throws an exception if either quaternion is zero.
- StdLib.quaternionSlerp(q, r, t)#
Spherical linear interpolation between two rotations.
- Arguments:
q (
[number, number, number, number]) – The rotation when t is 0.r (
[number, number, number, number]) – The rotation when t is 1.t (
number) – The interpolation parameter.
- Return type:
[number, number, number, number]
Note
The parameter is not limited to the range 0 to 1, and a value outside it extrapolates. Throws an exception if either quaternion is zero.
- StdLib.print(message)#
Writes a message to the game’s log, for debugging.
- Arguments:
message (
string) – The message.
Note
A function evaluated by a stream or an event prints on every update. The message is written at the Info level, so a server log level of Warning or Error hides it.
- class StdLib.AxisAngle(fields)#
A rotation as an axis and an angle.
Note
- AxisAngle.axis#
The axis of the rotation, as a unit vector, or zero for no rotation.
- Type:
[number, number, number]
- AxisAngle.angle#
The angle of the rotation, in radians between 0 and \(\pi\).
- Type:
number
- class StdLib.ColorRGB(fields)#
A color, as red, green and blue components between 0 and 1.
- ColorRGB.r#
The red component.
- Type:
number
- ColorRGB.g#
The green component.
- Type:
number
- ColorRGB.b#
The blue component.
- Type:
number
- class StdLib.ColorRGBA(fields)#
A color, as red, green, blue and alpha components between 0 and 1.
Note
An alpha of 0 is fully transparent and 1 is fully opaque.
- ColorRGBA.r#
The red component.
- Type:
number
- ColorRGBA.g#
The green component.
- Type:
number
- ColorRGBA.b#
The blue component.
- Type:
number
- ColorRGBA.a#
The alpha component.
- Type:
number