Lua for IC10

Open the playground In-game mod (Steam)

Getting started

You write a small subset of Lua; it is compiled to normal IC10 that runs on any IC chip. Nothing special runs in the game: other players and servers only see IC10.

local sensor = d0 --[[@as StructureGasSensor]]
local vent = d1 --[[@as StructureActiveVent]]
local TARGET = 101

while true do
  local p = sensor.Pressure
  if p < TARGET - 5 then
    vent.On = 1
  elseif p > TARGET + 5 then
    vent.On = 0
  end
  yield()
end

This becomes 8 lines of IC10 using 1 register. Try it in the playground: the IC10 updates as you type, and the emulator runs it with editable devices.

In-game mod

Lua for IC10 on the Steam Workshop adds Lua as a language to the IC10 Editor mod.

  1. Install BepInEx and StationeersLaunchPad, subscribe to IC10 Editor and Lua for IC10.
  2. Open a chip (IC housing or computer), choose Lua (IC10) in the language box in the top right corner. (Another mod, StationeersLua, adds a different language called just Lua.)
  3. Write Lua. The compiled IC10 is shown on the right, the line/register count and errors at the bottom.
  4. Confirm or Export writes the IC10 to the chip.
OptionWhat it does
InlineInline small and single-use functions (fewer lines, no jal).
Keep LuaOff (default): your Lua is saved on your computer (BepInEx/config/LuaIC10/sources) and the chip gets one line # lua-src …; the chip opens as Lua again on your computer. On: the whole Lua source is stored on the chip as a compressed comment, so it opens as Lua for everyone (uses more of the 128 lines / 4096 bytes).
IC10Show or hide the IC10 preview next to the Lua.
chip:For scripts with several chips: which chip's program goes on this chip.

Hover a Lua line to see its IC10. Library scripts are saved as Lua.

Devices and pins

The pins d0–d5 are the devices wired to the housing; db is the housing itself.

local sensor = d0 --[[@as StructureGasSensor]]   -- an alias: no code, no register
local tank = device(12345)                        -- a device by ReferenceId (ld/sd)

if d2.exists then         -- is something connected to d2?
  d2.On = 1
end

The --[[@as Type]] comment is optional. It tells the editors (playground, VS Code, Zed, the in-game mod) which device it is, so they can complete its properties and warn about typos. Use the prefab name from the Stationpedia.

Properties, slots, reagents

LuaIC10
local t = sensor.Temperaturel r0 d0 Temperature
vent.On = 1s d1 On 1
d0.slots[0].Occupiedls r0 d0 0 Occupied (slots start at 0, as in the game)
d0.slots[1].Lock = 1ss d0 1 Lock 1
reagent(d0, "Contents", hash("ItemIronOre"))lr — modes: Contents, Required, Recipe, TotalContents
d2.existssdse; in an if: bdse / bdns

Variables and constants

Control flow and timing

if a then … elseif b then … else … end
while cond do … end
repeat … until cond
for i = 1, 10 do … end          -- the step must be a constant: for i = 10, 1, -1
break, goto label / ::label::, do … end

yield()      -- wait for the next tick: put one in every main loop
sleep(2)     -- wait 2 seconds
hcf()        -- stop the chip

A loop without yield() or sleep() runs many times per tick, exactly like in IC10.

Functions

local function clamp01(x)
  return math.clamp(x, 0, 1)
end
d0.Setting = clamp01(d1.Setting)

Arrays and memory

local limits = {290, 300, 310}   -- fixed array
local history = array(10)        -- 10 slots set to 0 (array(n, fill))
history[i] = limits[2] + #limits

Arrays live in the chip's own stack memory (get/put db), from address 511 down. Indexes start at 1 like Lua. #t is a constant.

Batch (all devices of a type)

local PANELS = hash("StructureSolarPanel")
batch.write(PANELS, "Horizontal", 90)            -- sb
local avg = batch.read(PANELS, "Charge")         -- lb, mode Average
local sum = batch.read(PANELS, "Charge", "Sum")  -- Average, Sum, Minimum, Maximum
batch.write_named(PANELS, hash("North"), "On", 1) -- sbn
local n = batch.read_named(PANELS, hash("North"), "Charge", "Maximum") -- lbn
-- slots of every device of a type
local full = batch.read_slot(PANELS, 0, "Occupied", "Sum")  -- lbs
local f2 = batch.read_named_slot(PANELS, hash("North"), 0, "Occupied") -- lbns
batch.write_slot(PANELS, 0, "Lock", 1)                     -- sbs

Batch operations reach every device of that type on the chip's data network.

math and built-ins

LuaIC10
math.abs floor ceil sqrt exp logabs floor ceil sqrt exp log
math.sin cos tan asin acos, math.atan(y, x)sin cos tan asin acos atan / atan2
math.max(a, b, …), math.min(…)max, min
math.random(), math.pirand, constant
math.round, math.trunc, math.signround trunc sgn (extensions)
math.clamp(x, lo, hi), math.lerp(a, b, t)clamp, lerp
math.isnan(x), math.approx(a, b, tol?)snan, sap
math.rad(deg), math.deg(rad)mul by deg2rad / rad2deg
math.huge, -math.huge, math.nanpinf, ninf, nan
math.tau epsilon deg2rad rad2deg rgasIC10 constants (free)
bit.band bor bxor nor bnotand or xor nor not (also & | ~)
bit.sll srl sla sra rol rorshifts and rotations (also << >>)
bit.extract(x, off, len), bit.replace(x, v, off, len)ext, ins
str("HELLO")STR("HELLO"): up to 6 characters in one number (LED displays)
Color.Red, GasType.Oxygen, Sound.Alarm2, SlotClass.Ore…game enums as constants (26 groups, completion in the editor)
rmap(d0, hash("ItemIronOre"))rmap
clear(d0), clear(device(id))clr, clrd
select(cond, a, b)select (cond ? a : b)

Several chips

One script can describe several chips that work together, with shared variables:

shared = { temp = 0, alarm = 0 }

chip("atmos", function()
  local sensor = d0 --[[@as StructureGasSensor]]
  while true do
    shared.temp = sensor.Temperature
    shared.alarm = shared.temp > 310
    yield()
  end
end)

chip("display", { link = d5 }, function()
  local screen = d0 --[[@as StructureConsoleLED5]]
  while true do
    screen.Setting = shared.temp
    yield()
  end
end)

Differences from Lua

Cheat sheet

WantWrite
Name a devicelocal pump = d2 --[[@as StructureVolumePump]]
Read / writelocal x = pump.Setting / pump.On = 1
Main loopwhile true do … yield() end
Hysteresisif p < LOW then x.On = 1 elseif p > HIGH then x.On = 0 end
All panelsbatch.write(hash("StructureSolarPanel"), "Vertical", v)
Is it plugged in?if d3.exists then … end
Item in a slotd0.slots[0].Occupied
Waityield() (1 tick), sleep(seconds)