BlockFactory:Signals and Programmable Interface

From MutigerAffe Wiki
Jump to navigation Jump to search

This page covers two kinds of signals. Rail signals split a rail network into blocks so that several trains can share it. The signal network lets your factory react to itself: chests report their contents, tanks their fill level, and you switch Grabbers, pumps, boilers, lamps and Train Stops on and off. Instead of logic gates you write small programs in a Programmable Interface.

Rail Signal and Chain Signal

Name Epoch Unlocked by Size Recipe
Rail Signal 2 Rail Signals 1 × 1 5 Steel Plate + 1 Electronic Circuit
Chain Signal 2 Rail Signals 1 × 1 5 Steel Plate + 1 Electronic Circuit

Without signals the whole rail network is a single block, and only one train can move through it safely. Signals cut the network into blocks, and only one train may be in a block at a time. A train never drives into rail cells that another train occupies, signals or not.

  • Place a signal right beside the track, with its lenses facing the oncoming train. It guards the rail cell next to it, for trains coming from the side its lenses face. Trains going the other way pass it unchecked. Place it so that it touches only the rail it should guard.
  • Rail Signal: green when the block behind it is free, red when another train is in it or has reserved it. A train waits in front of a red signal.
  • Chain Signal: lets a train through only if every block along its way is free, up to and including the block behind the next Rail Signal. Put Chain Signals in front of crossings and junctions, so that trains never stop in the middle of one.
  • Signals update their lights once per second.

For trains, stops and schedules see Railways.

Signal network

Name Epoch Unlocked by Size Recipe
Signal Cable 3 Circuit Network tool 1 Electronic Circuit + 5 Copper Cable
Programmable Interface 3 Programmable Interface 1 × 1 5 Advanced Circuit + 5 Electronic Circuit + 5 Steel Plate

Wiring

  • Hold the Signal Cable and right-click a building, then a second one. A green wire connects them. The cable is a tool and is not used up.
  • A wire reaches at most 10 blocks.
  • Right-click the same building twice to remove all its wires.
  • You can only wire buildings of your own team.

All buildings joined by wires form one network. Each tick, a network adds up the signals of all its members.

What buildings report

Signal Who reports it Value
an item ID, e.g. BF_Iron_Plate chests and other buildings that store items, such as Gun Towers how many of that item it holds
a fluid name, e.g. Crude_Oil Storage Tanks and every building with fluid tanks units in its tanks
power every building on a power grid how well that grid is supplied, in percent (100 = enough power)
train Train Stop 1 while a train is halted there, otherwise 0
unrest the world, once per network the current unrest in percent (see Enemies)
your own names Programmable Interfaces whatever their program sets with out
  • Values of the same name add up. Every wired building on a grid adds its own power value, so wire only one of them if you want a plain percentage.
  • Item signals use the item's ID: BF_ plus the name with underscores, for example BF_Coal or BF_Advanced_Circuit. The Items page lists all IDs.
  • Fluid signals are Water, Steam, Crude_Oil, Heavy_Oil, Light_Oil, Petroleum_Gas, Lubricant and Sulfuric_Acid.
  • Machines that burn fuel do not report their slots, only their fluid tanks.

What you can switch

Building Listens to
Coal Grabber, Grabber T1, T2, T3, Mass Grabber, Long Grabber grabber
Water Boiler boiler
Water Pump, Oil Pumpjack pump
Train Stop stop (trains do not head for a stop that is off)
Lamp lamp
Nuclear Reactor, Heat Exchanger on only

A wired building runs only while its signal is greater than 0. If its own signal is missing from the network, it listens to on instead, which switches everything at once. If neither signal is in the network, it runs as if it had no wire.

Programmable Interface

Use the interface to open its editor: a code field, a Save button, three example programs, and a live view of the network's signals and of the interface's outputs. Save checks the program; if something is wrong, the status line names the line and the problem. The interface itself shows whether its program runs or has stopped with an error.

The program runs once per tick (30 times per second) with the signals its network had in the previous tick. Its outputs enter the network in the next tick.

Statements

One statement per line. Everything after # is a comment. Empty lines are ignored, and indentation does not matter.

Statement Meaning
name = expression set a variable
out name = expression send the signal name into the network
if expression … elif expression … else … end run the lines below only if the value is not 0; elif and else are optional, end is required. Blocks can be nested.

Values and names

  • All values are whole numbers. / drops the remainder, % gives it. Dividing by 0 is an error.
  • $name reads a network signal; a missing signal reads as 0.
  • A plain name reads a variable. Variables start at 0 and keep their value from tick to tick, until you save the program again or the server restarts.
  • Outputs are set fresh in every run: an out line that does not run in a tick sends nothing in that tick. If several out lines set the same name, the last one counts.
  • Names start with a letter or _ and may contain letters, digits and _. They are case sensitive. The keywords if, elif, else, end, and, or, not and out are not, and cannot be used as variable names.

Operators

From strongest to weakest binding:

Operators Notes
( ) grouping
-x negative
* / % multiply, divide, remainder
+ - add, subtract
== != < <= > >= give 1 (true) or 0 (false). Only one comparison per term: write a < b and b < c, not a < b < c.
not 1 if the value is 0, otherwise 0
and 1 if both sides are not 0
or 1 if at least one side is not 0

Because not binds weaker than comparisons, not $train == 1 means "not (train equals 1)".

There are no other functions, loops or text values. A program may have at most 200 lines. An error while running (division by zero, too many steps in one tick) stops the program until you save it again.

Examples

Run the boilers only while the wired coal chest holds more than 20 Coal (one of the built-in examples):

# Boilers by coal in the chest
out boiler = $BF_Coal > 20

Open a station only when the iron chest next to it runs low. Wire the interface to the chest and the Train Stop; trains heading for that stop name go elsewhere while it is closed:

# Station on demand
if $BF_Iron_Plate < 200
  out stop = 1
else
  out stop = 0
end

Keep an oil tank between 5,000 and 20,000 units. Wire the interface to the Storage Tank and the Oil Pumpjacks. The variable remembers whether the pumps are filling, so they do not flicker on and off at a single threshold:

if $Crude_Oil < 5000
  filling = 1
elif $Crude_Oil > 20000
  filling = 0
end
out pump = filling

A counter that sends a one-tick pulse every 30 ticks (once per second):

t = t + 1
if t >= 30
  t = 0
  out pulse = 1
end

Light the wired lamps as an alarm once the unrest reaches 20 %:

out lamp = $unrest >= 20

Wirecutter

The Wirecutter (Start, 2 Iron Plate + 1 Wood) is for power poles, not for signal wires. Right-click a power pole, then a second one, to cut the cable between them or put it back. This is only for looks: the poles stay connected to the grid. See Power.

See also

Block Factory
Start Installation · Getting Started
Basics Controls and Building · Gathering and Crafting · Field Manual and HUD · The World
Progression Epochs · Research · Portal of the Ancient · Great Portal
Factory Belts · Grabbers · Storage · Mining · Smelting · Fabricators · Hunting and Farming
Power and fluids Power · Fluids · Oil Processing · Nuclear Power · Rune Modules
Trains and logic Railways · Signals and Programmable Interface · Vehicles
Workers Goblins · Goblin Planner · Golems · Outlanders
Danger Enemies · Defense · Weapons and Armor · Death and Respawn
Multiplayer and servers Teams and Raids · Commands · World Settings
Reference Buildings · Items · Recipes · Research Tree
Project page on mutigeraffe.com · Download on CurseForge