openOMSI GitHub Discord
Plugin API reference

Docs / Plugins / Plugin API reference

Helpers

4 min readEdit this pagedocs/PLUGIN_API.md

On this page
  1. omsi.fmt.clock
  2. omsi.fmt.delay
  3. omsi.fmt.distance
  4. omsi.fmt.duration
  5. omsi.fmt.money
  6. omsi.fmt.number
  7. omsi.fmt.pad
  8. omsi.fmt.speed
  9. omsi.fmt.split
  10. omsi.fmt.trim
  11. omsi.json.decode
  12. omsi.json.encode
  13. omsi.util.date
  14. omsi.util.ms
  15. omsi.util.now
  16. omsi.util.random
  17. omsi.vec.angle_diff
  18. omsi.vec.bearing
  19. omsi.vec.clamp
  20. omsi.vec.distance
  21. omsi.vec.distance3
  22. omsi.vec.dot
  23. omsi.vec.heading
  24. omsi.vec.length
  25. omsi.vec.lerp
  26. omsi.vec.normalize
  27. omsi.vec.rotate
  28. omsi.vec.to_local

omsi.fmt.* formats numbers, times and distances, omsi.json.* reads and writes JSON, omsi.vec.* does map geometry, omsi.util.* gives real time and random numbers. None of them touch the game.

omsi.fmt.clock#

omsi.fmt.clock(seconds, [with_seconds]) → string

Seconds since midnight as "HH:MM" ("HH:MM:SS" with true); past midnight wraps.

Parameters: seconds number; with_seconds bool, optional.

Since 0.2.22.

Lua
omsi.fmt.clock(27000)        --> "07:30"
omsi.fmt.clock(27015, true)  --> "07:30:15"

omsi.fmt.delay#

omsi.fmt.delay(seconds) → string

A delay as a timetable display shows it: "+2:30" late, "-0:45" early, "0:00".

Parameters: seconds number.

Since 0.2.22.

Lua
omsi.fmt.delay(150)  --> "+2:30"

omsi.fmt.distance#

omsi.fmt.distance(metres) → string

A distance as "350 m" or "2.4 km".

Parameters: metres number.

Since 0.2.22.

omsi.fmt.duration#

omsi.fmt.duration(seconds) → string

A length of time as people read it: "45 s", "3 min 05 s", "1 h 20 min" (negative with a minus).

Parameters: seconds number.

Since 0.2.22.

omsi.fmt.money#

omsi.fmt.money(amount, [symbol]) → string

An amount with two decimals and a currency symbol after it ("12.50 €"; the game knows no currency of its own).

Parameters: amount number; symbol string, optional.

Since 0.2.22.

omsi.fmt.number#

omsi.fmt.number(x, [decimals], [separator]) → string

A number with decimals (0) and a thousands separator (",", " "; none by default).

Parameters: x number; decimals integer, optional; separator string, optional.

Since 0.2.22.

omsi.fmt.pad#

omsi.fmt.pad(text, width, [right]) → string

A text made width characters long with spaces (on the left with right = true, to line numbers up), or cut to it.

Parameters: text string; width integer; right bool, optional.

Since 0.2.22.

omsi.fmt.speed#

omsi.fmt.speed(kmh, [unit]) → string

A speed as "42 km/h", or in "mph" or "m/s".

Parameters: kmh number; unit string, optional.

Since 0.2.22.

omsi.fmt.split#

omsi.fmt.split(text, [separator]) → list of strings

A text cut at every separator ("," by default; plainly, no patterns).

Parameters: text string; separator string, optional.

Since 0.2.22.

omsi.fmt.trim#

omsi.fmt.trim(text) → string

A text without the spaces at its ends.

Parameters: text string.

Since 0.2.22.

omsi.json.decode#

omsi.json.decode(text) → value, or nil and the reason

Reads JSON text: objects and arrays become tables, null nil.

Parameters: text string.

Since 0.2.22.

omsi.json.encode#

omsi.json.encode(value, [pretty]) → string

A value as JSON text (a list as an array, a table with keys as an object; pretty: indented).

Parameters: value any; pretty bool, optional.

Since 0.2.22.

Lua
local text = omsi.json.encode({ line = "136", stops = { 1, 2 } })
local t = omsi.json.decode(text)

omsi.util.date#

omsi.util.date([seconds]) → table

A real time (util.now() by default) as {year, month, day, hour, minute, second, weekday} in UTC (weekday 1 Monday).

Parameters: seconds number, optional.

Since 0.2.22.

omsi.util.ms#

omsi.util.ms() → number

Milliseconds of real time since the game started: for timing a plugin's own work.

Since 0.2.22.

omsi.util.now#

omsi.util.now() → number

The real time: seconds since 1970 (UTC), with fractions.

Since 0.2.22.

omsi.util.random#

omsi.util.random([min], [max]) → number

A random number: 0 to 1 without arguments, else a whole number from min to max (as math.random, but not repeating the same row in every plugin).

Parameters: min number, optional; max number, optional.

Since 0.2.22.

omsi.vec.angle_diff#

omsi.vec.angle_diff(a, b) → number

The turn from heading a to heading b, -180 to 180 degrees (right positive).

Parameters: a number; b number.

Since 0.2.22.

omsi.vec.bearing#

omsi.vec.bearing(x1, y1, x2, y2) → number

The heading from one map point to another.

Parameters: x1 number; y1 number; x2 number; y2 number.

Since 0.2.22.

Lua
local h = omsi.vec.bearing(x1, y1, x2, y2)

omsi.vec.clamp#

omsi.vec.clamp(x, min, max) → number

x held between min and max.

Parameters: x number; min number; max number.

Since 0.2.22.

omsi.vec.distance#

omsi.vec.distance(x1, y1, x2, y2) → number

Metres between two map points (on the ground: x and y).

Parameters: x1 number; y1 number; x2 number; y2 number.

Since 0.2.22.

omsi.vec.distance3#

omsi.vec.distance3(x1, y1, z1, x2, y2, z2) → number

Metres between two points in space.

Parameters: x1 number; y1 number; z1 number; x2 number; y2 number; z2 number.

Since 0.2.22.

omsi.vec.dot#

omsi.vec.dot(x1, y1, x2, y2) → number

The dot product of two 2D vectors.

Parameters: x1 number; y1 number; x2 number; y2 number.

Since 0.2.22.

omsi.vec.heading#

omsi.vec.heading(dx, dy) → number

The heading of a direction on the map, degrees clockwise from north (0 to 360), as the game's headings.

Parameters: dx number; dy number.

Since 0.2.22.

omsi.vec.length#

omsi.vec.length(x, y, [z]) → number

The length of a vector (2D, or 3D with z).

Parameters: x number; y number; z number, optional.

Since 0.2.22.

omsi.vec.lerp#

omsi.vec.lerp(a, b, t) → number

From a to b by t (0 to 1, not held to it).

Parameters: a number; b number; t number.

Since 0.2.22.

omsi.vec.normalize#

omsi.vec.normalize(x, y, [z]) → x, y, z

The vector made 1 long (0 stays 0).

Parameters: x number; y number; z number, optional.

Since 0.2.22.

omsi.vec.rotate#

omsi.vec.rotate(x, y, degrees) → x, y

A vector turned clockwise by degrees (as headings turn).

Parameters: x number; y number; degrees number.

Since 0.2.22.

omsi.vec.to_local#

omsi.vec.to_local(x, y, ox, oy, heading) → right, ahead

A map point seen from a place facing heading: metres to the right and ahead of it.

Parameters: x number; y number; ox number; oy number; heading number.

Since 0.2.22.

Lua
local x, y, _, heading = omsi.position()
local right, ahead = omsi.vec.to_local(px, py, x, y, heading)

Something wrong or missing? Fix it on GitHub or open an issue.

↑↓ to move↵ to open