Mindustry Logic Documentation
Mindustry Logic Documentation
Table of contents
Foreword (Please read)
Introduction
Glossary
Basic concept
Processors
How Processors run its code
Links
Variables
Built in variables
Constants
Buffers
Blocks
Instructions
● Read
● Draw
● Draw flush
● Print
● Print flush
● Get Link
● Control
● Radar
● Sensor
● Set
● Operation
● Lookup
● Pack color
● Wait
● Stop
● End
● Jump
Controlling Units
● Unit bind
● Unit control
● Unit radar
● Unit locate
Simple Logic Examples
Universal Switch
Shuttle logic
Advanced (WIP)
Writing in text editor
World logic (WIP)
Get block
Set Block
Spawn Unit
Apply Status
Spawn Wave (WIP)
Set Rule (WIP)
Flush Message
Cut Scene (WIP)
Effect (WIP)
Explosion
Set Rate
Fetch
Sync (WIP)
Get Flag (WIP)
Set Flag (WIP)
Set Prop
Bleeding edge (WIP)
Self promotion
Appendix
Built in variables:
Global:
Environment
Sensors
Blocks and items
Units
Text form instruction
Foreword (Please read)
This documentation was created solely by me, so please understand that there
may be inaccuracies or omissions. Additionally, as English is not my first language,
please forgive any spelling or grammar errors.
If you have any corrections, additions or suggestion, feel free to reach out to
me on discord (Yruei) to discuss such topics,
Or if you just want to ask questions, I’ll be happy to help
you can DM or Ping me on the Mindustry Discord Server
Or reddit (u/Far_Kale558)
Fun fact, it is turing complete (meaning you can literally do anything a real computer can, although the
practicality is questionable),
it is often used for more advanced automation (telling units what to do, disabling/enabling blocks, and
more),
For more advanced people, they like to mess around just for fun making stuff that are not actually useful to
campaign like games, emulators, renderings, etc.
mlog is not necessary for the completion of campaign , it is very optional, often the time you spent coding
will be more than if you just do it manually.
Glossary
Data types
Integers
Integers are whole numbers, meaning no decimal, 1,2,3, are integers, 1.001, are not.
Float
Opposite of integers, decimal numbers, 1.001, are float.
Boolean
A data type that can have 1 of 2 states, true or false, 0 or 1.
Strings
a sequence of characters, different from variables, in mlog strings are enclosed by “ (quotation mark)
(“Hello, world” is a string).
Reference
Unit or building reference, is what processors use, examples are Links, Sensor.
Basic concept
Processors
There are 3 types of Logic Processors, Micro processor, Logic processor, and Hyper
processor, each have different speeds and range (Information can be found on the game’s info
menu)
With jumps:
When the processor reach the end of its instruction it will go back to 0, repeating the loop indefinitely
Links
You can link a building to the processor by clicking the processor (you can see a circle, this is
the processor range) and clicking the building
Variables
Variables are the fundamental of programming, to put it simply, they are just named
“containers” to store data to, for example the Set instruction sets a variable:
This will make “x” a variable, this variable can be used for all sort of different things, for example, the
Operation instruction
Because “x” is 10 this operation means 10 + 5, and the result of it is written to the “result” variable,
meaning “result” is now 15
Of course it doesn’t have to be “result”, you can write anything for it, different people write their
variable differently, some chooses to write arbitrary words/letters like “peach” and others simply write
“x+5”, whatever you choose is up to you, just keep in mind you have to remember them.
Incrementing variable
Increment is the act or process of increasing
Since processor loops, this will increase “x” by 1 every loop, as time goes on x will get bigger and
bigger. This is what’s called incrementing a variable
Built in variables
Built in variables are variables that have their value determined by the game, but can also
change. they are preceded by “@”, here are lists of built in variables (if you’re just starting out you
don’t have to read all of this you can always come back):
● @counter
@counter shows the current line the processor runs at, other than giving you crucial
information while debugging you can also use them, usage example:
Since this set instruction is at line 10, the “x” variable will be 11
● @this
@this shows the building reference for that processor, example: anything that uses
building reference can be replaced by this, so instead of ‘processor1’ you can write “@this”
for this processor
● @unit
@unit shows the current unit that is stored in the processor as the result of the unit
bind instruction. For more information go Controlling Units
● @links
@links shows the number of links the processor has, example: if you have 3
buildings linked to the processor ‘display1’ , ‘display2’ , ‘display3’ , @links will be 3
● @ipt
ipt stands for instruction per tick, this shows how many instruction a processor run in
a single tick
1 second is 60 ticks
Constants
Constant, they are like variable but you can’t write to them, their value are predetermined, for
example “@pi”, like the name suggest it is pi ( Π , 3.14159….), here are the list of constant (if you’re just
starting out you don’t have to read all of this, you can always come back):
● Null
null means none, it has no value
● true and false
true and false are booleans, they are interpreted to 1 and 0
● @pi
π, 3.14159….
● @e
Mathematical constant e (2,71828…)
● @degToRad
To convert degrees to radian, multiply the number with this
● @radToDeg
Opposite of @degToRad
Buffers
Internally there is a Text buffer and a Draw buffer in each processor, when you use print or draw,
when the processor runs it, it will store it in the buffer.
Example: if you print “frog” the word “frog” will be stored in the text buffer, then to get that word to a
message block you have to use Print Flush which transfer the items in the text buffer to the message block
It's the same case with draw
The draw buffer has 256 item buffer, meaning you can only draw 256 times before needing to flush,
otherwise everything after 256 just gets thrown out,
displays also have a buffer limit, 1024 / tick to be exact, this means if you flush 1025 items to 1 display in 1
tick 1 item will be lost.
Blocks
● Message
Is used with Print and Print Flush, can also be manually written by clicking it and click the
pencil icon
● Switch
Has 2 state, enabled and disabled / 1 and 0, state can be read using Sensor instruction, and
can also be controlled with Control instruction
Instructions
● Read
Read a value from a cell
Write
Write a value to a cell
● Draw
Adds a command to the draw buffer
○ Clear
Sets the entire screen to a specific color
● Print
Prints the specified value to the text buffer, can print both text and variables
(there might be more printing codes, you can help expanding it)
● \n
Writing \n will write the next text to a new line, example:
● Printing colors “ [] “
You can change printed colors with [], example
● Print flush
Flush the queued print instruction(s) from the text buffer to the specified message block with
its building reference
● Get Link
Get link gets the link of the processor linked building
example : you have 2 buildings linked to the processor, ‘duo1’ and ‘duo2’
getlink “result” #0 will get you ‘duo1’ and
getlink “result” #1 will get you ‘duo2’
The order is based on the order you link the buildings
The buildings reference is then “result”
Using it with numbers is pretty useless, they are usually used with incrementing variables as its
index, this way you can get multiple buildings without writing every buildings reference
(message1,2,3….)
● Control
Controls a building
● Enabled
Enabled or disable a building, can be 0 or 1 or true and false
● Shoot
Shoot at a coordinate position, usually used with turret
can be 0 or 1 / true or false
● Shootp
Shoot at a unit/building but with velocity prediction
● Color
Change the color of Illuminator block
Usually used with pack color
● Radar
Radar a unit in range of the building’s range
● Sensor
Sensor / get / read data from a building
This will get the amount of copper in ‘vault1’, the number will be stored in the variable ‘result’, let’s
say the vault has 10 coppers, therefore ‘result’ will be 10
● Set
Set a variable, either with numbers or with another variable (including unit or block reference)
● Operation
Perform a single operation on 2 variables, or just numbers directly, example:
Let’s say “a” is 10, therefore this operation means 10 + 5
“result” will then be 15
─ +
Addition
─ -
Subtraction
─ *
Multiplication
─ /
Division
─ //
Integer division, like division but it will round down the results
─ %
Modulo operation, like division but it returns the remainder
─ ^
Power / exponentiation,
─ Sqrt
Square root
─ ==
Check if the 2 variables are equal to each other
Returns 1 or 0 / true or false
─ Not
Logical not, check if the 2 variables are not equal to each other
Returns 1 or 0 / true or false
─ And
Logical AND gate
Inputs and output are boolean
─ <
Less than, check if the first variable is less than the second variable
Returns 1 or 0 / true or false
─ <=
Less than equal, check if the first variable is less than equal the second variable
Returns 1 or 0 / true or false
─ >
Greater than, check if the first variable is greater than the second variable
Returns 1 or 0 / true or false
─ >=
Greater than equal, check if the first variable is greater than equal the second variable
Returns 1 or 0 / true or false
─ log
Logarithm, return the exponent given the base and the result
─ log 10
Logarithm in base 10
─ abs
Absolute number
─ floor
Floor , Round down the specified number
─ ceil
Ceiling, Round up the specified number
─ Or
Bitwise or
─ B-and
Bitwise and
─ Xor
Bitwise XOR
─ Flip
Bitwise flip
─ Max
Maximum of 2 numbers
─ Min
Minimum of 2 numbers
─ Angle
Angle of vector in degrees,usually used like: Angle = ((x1-x2)(y1-y2))
─ Angle diff
Absolute distance between to angles in degrees, usually used like: angdiff = (ang1)(ang2)
─ Len
Length of vector, usually used like: len = ((x1-x2)(y1-y2))
─ Noise
2d simplex noise
─ Rand
Random decimal in range of [0,value]
─ Sin
Sine, in degrees
─ Cos
Cosine, in degrees
─ Tan
Tangent, in degrees
─ Asin
Arc sine, in degrees
─ Acos
Arc cosine, in degrees
─ Atan
Arc tangent, in degrees
● Lookup
Look up an item/liquid/unit/block type by ID.
Total counts of each type can be accessed with:
@unitcount / @itemCount / @liquidCount / @blockCount
For the inverse operation, sensor @id of the object
Example:
This operation is looking up item with an id of 1, which is lead, therefore “result will then be lead,
lead is NOT a string, its an object, same as ‘@lead’, which can be used for things like sensor
● Pack color
Packs RGBA color into a single number, is usually used with draw col and control color
Since it's a single number it can be stored to 1 address on cells/banks, instead of 4 addresses with
the usual RGBA, making reading and writing a lot easier too.
RGBA is their respective color, can also be directly written with a number, value is from 0 to 1
● Wait
Wait the specified number of seconds
● Stop
Completely stops the processor from running, cannot be resumed in anyway other than
rebuilding or refreshing the code
● End
End the process, when a processor run this line it will go back to 0 as if it reach the end of
the instruction, similar to jump always 0
● Jump
Jump to a line number on the processor with a condition
● ==
Check if the 2 variables are equal to each other
If true then jumps, if false continue to the instruction below it
● Not
Logical not, check if the 2 variables are not equal to each other
If true then jumps, if false continue to the instruction below it
● <
Less than, check if the first variable is less than the second variable
If true then jumps, if false continue to the instruction below it
● <=
Less than equal, check if the first variable is less than equal the second variable
If true then jumps, if false continue to the instruction below it
● >
Greater than, check if the first variable is greater than the second variable
If true then jumps, if false continue to the instruction below it
● >=
Greater than equal, check if the first variable is greater than equal the second variable
If true then jumps, if false continue to the instruction below it
● ===
Strict equal, usually used to check for null, example:
0 == null is true, while 0 === null is false
● Always
Always jump regardless
Controlling Units
Controlling a unit is a bit complicated, the controls are all very specific, it is very literal, therefore you
need to write code in great detail to get the unit to do what you want.
Things you need to know:
● @unit
Can only store 1 unit at any given time, stores the unit reference, like building
reference, unit reference are different from each other, even the same type
● Binding
unlike the name say, unit bind instruction itself does not bind, unit control, unit radar,
unit locate are what binds unit
If you hover your mouse on either the unit or the processor you will see a yellow box showing
what unit the processor is binding or what processor the unit is binded to, this is called
Binded, meaning the unit is now in full control of the processor, you cannot give it commands
anymore using the RTS Commands, the only way to get the unit back is either
control(posses) the unit or unbind it which can be done with either destroying the processor,
or altering the processor code, units will also be automatically un-binded after 5 seconds with
no unit control, unit radar, and unit locate instruction ran
A player unit can also be "bound," but only to retrieve data from it; controlling is not
possible.
● Boosting
Some ground units can boost, boosting allows them to fly over anything air units can,
albeit slower, these units are: Nova, Pulsar, Quasar, and Vela
● Payload
Payload is a block/unit that is not physically placed in the world, they are either on a
conveyor or picked up by unit, unit’s payload capacity can be found in-game’s info menu
Example: 3x3 means the unit can carry 9 of 1x1 blocks, 2 of 2x2 blocks and, 1 of 3x3 blocks
● Unit bind
Bind 1 unit and put its reference in the @unit variable,
When binding a unit @[type] it will bind in an order
the order of bind is determined by how old it is, the first unit alive will be binded first and the most
recent unit is last
When looped (meaning the unit bind instruction is run again) it will bind the next unit in the order,
when it reach the end it will go back and bind the first order
Here’s a visualization :
Don’t worry about instruction line 1,2,3,they are there just to make the code not 1 instruction, to more
easily visualize it.
Let’s say the first mega reference “mega#1”, when “mega#1” is binded it store the reference in @unit
meaning @unit is now “mega#1”, and so on with “mega#2 , 3 , 4”
● Unit control
Unit control, controls one unit in the @unit variable, a common misconception is that it
controls all binded unit, this is wrong, and a second common misconception is that Unit control wait
for completion before running the next unit control, this is also wrong,
Like the visualization you see above, the processor doesn’t care what instruction you put in it, it will
just run it regardless, it is not smart, you have to specify it yourself.
● Move
Move to unit to the specified coordinates in a straight line
Once the instruction is run it will move to that coordinates even if you immediately remove
the instruction, its similar to RTS controls (shift clicking and left click to move unit to the
cursor location)
● Approach
Similar to move but it has a radius, the unit will stop at the radius
● pathFind
Path find to the specified location
Usually used with grounded units as Move moves the unit in a straight line and grounded
units can get stuck in buildings
If the specified location is inside a building or invalid the unit will not move, they will stay in
place
● autoPathFind
Automatically pathfinds to the nearest enemy core or drop point.
This is the same as standard wave enemy pathfinding
● Idle
Stop moving, but keep building/mining, the default state
● Stop
Stop moving, mining, and building
● Boost
Enable boosting, boolean, 1 = boost, 0 = don’t boost
● Target
Shoot a coordinate position, boolean, 1 = shoot, 0 = don’t shoot
● TargetP
Same as target but shoot a unit reference instead and with velocity prediction
● ItemDrop
Drops items the unit is currently carrying to the specified location
Unit have to be near the specified building to drop its item, else it does nothing
● ItemTake
Takes items from the specified building to the unit, unit’s item capacity can be found
in-game’s info menu
The unit have to be near the specified building to take the items, else it does nothing
Unit has an interaction limit of 1,5 seconds, meaning it can only take/drop item every 1,5
seconds
● PayDrop
Drop payload at current location.
● PayTake
Pick up payload at current location
● PayEnter
Land/enter on a building that accept payload, like reconstructor
● Mine
Mine a coordinate position, if out of range will do nothing
● Build
Build a block at specified location, if out of range will do nothing
x,y is build location
block is what block to build
rotation is what orientation the block is built, 0 is right, 1 is up, 2 is left, and 3 is down
Config is
● getBlock
Gets a block at a specified location, outputs will be null if out of range
Is this two scenario the picture on the left will have the variable “result” true because the unit
is within the circle, while the right picture is false because it is outside the circle
● Unbind
Immediately unbind unit
● Flag
Gives the unit a numeric flag, essentially assigning a number to a unit, this number
serves no purpose other than being read, what you do with the number is up to you
● Unit radar
Unit radar locates other unit around the @unit unit within the unit’s range (information can be
found in-game’s info menu)
target is a filter of what category of unit it will radar, enemy means it will radar enemy units
Order is sorting order, ascending or descending, can only be 0 or 1
sort is a metric to sort results by, distance means it will get unit by distance
Since order is 1, the unit gotten is the closest to that @unit’s unit
Will output the unit reference to “result”
● Unit locate
Locates a building/ore/spawn without needing to be in the unit’s range, but limited to what it
can locates
● Building
● Ore
Will locate an ore of the selected type, in this case @copper
● Spawn
Will locate an enemy spawn point, may be a core or a position
● Damaged
Will locate an ally damaged buildings
line 0 : check for @enabled in ‘switch1’, stores the state in the ‘switch’ variable
line 1 : getlink with ‘link’, which is an incrementing variable
line 2 : sensor for @type in ‘switch1’ for later
line 3 : increment the variable ‘link’
line 4 : reset the ‘link’ variable when it reached @links using the modulo operator
line 5 : jump if ‘type’ is @switch, this ensures that the switch building does not get controlled
line 6 : control the enabled of ‘block’, which is the results of getlink
Shuttle logic
The key is to not put 2 or more move that the processor can run “simultaneously”, in other words
don’t make the unit move to 2 location or more “simultaneously”
Either move to core or move to vault based on a condition
From this point forward, omissions and inaccuracies might increase, you can help expanding it
Advanced (WIP)
Labels
Labels are replacement for jumps number, this way you can write freely without needing to worry about
jump numbers
Comments
Combining lines
Get block
Set Block
Spawn Unit
Apply Status
currentWaveTimer
waveTimer
waves
wave
waveSpacing
waveSending
attackMode
enemyCoreBuildRadius
set how large the radius of the no build zone around the enemy core
dropZoneRadius
set how large the annihilation radius around the enemy spawn point
unitCap
set the cap of unit that can be constructed
mapArea
set
lightning
ambientLight
solarMultiplier
set the multiplier solar panels produce power
unitBuildSpeed
set the build speed of units
unitCost
set multiplier of unit construction cost
unitDamage
set the multiplier of unit damage
blockHealth
set the multiplier of block health
blockDamage
set the multiplier of block damage (turrets, explosion, etc)
rtsMinWeight
rtsMinSquad
Flush Message
display a message on player’s screen from the text buffer, will wait until the previous message
finishes
`toast`
`mission`
Effect (WIP)
Explosion
create an explosion
`team` what team the explosion belong to, explosion cannot harm ally blocks
`x` `y` where to create the explosion
`radius` size of explosion, block outside this radius does not take damage
`damage` the damage of the explosion
`air` whether to damage air or nor, true means it will damage air (air units, boosted units, etc)
`ground` whether to damage ground or nor, true means it will damage air (ground unit, blocks, etc)
`pierce` whether to pierce buildings or not, if false, it will need a line of sight from the explosion to the
block, if something is in the way that block will not be damaged
Set Rate
Fetch
get unit, player, core, building, and outputs their reference to “result”, by index
`team` what team to fetch the object
`#` index, starts at 0, 0 being the first object
unitCount, playerCount, coreCount, buildCount, gets the total count of currently alive/available object
in the world
Sync (WIP)
Get Flag (WIP)
Set Prop
Built in variables:
Information of each might be added in the future
Global
@waveNumber
number of wave currently
@waveTime
current countdown time for next wave
@derelict
@sharded
@crux
@malis
@green
@blue
@solid
@block
@manifold
@assembly-drone
@turret-unit-build-tower
@blockCount
@unitCount
@itemCount
@liquidCount
Environment
Sensors
@totalItems
total items the object is currently holding; returns number
@firstItem
the first item the object is currently holding
returns item names, eg, @copper, @lead
@totalLiquids
total liquid units the object is currently holding; returns number
@totalPower
@itemCapacity
the maximum item capacity an object can hold, for vault it is 1000 even though it can store each item
for 1000; returns number, information can also be found on ingame’s info menu
@liquidCapacity
the maximum liquid units capacity an object can hold; returns number, information can also be found
on ingame’s info menu
@powerCapacity
the maximum power unit a battery can hold; returns number, information can also be found on
ingame’s info menu
@powerNetStored
the power currently stored in the net (all connected batteries); returns number
@powerNetCapacity
the maximum power unit a net of battery can hold; returns number
@powerNetIn
the gross power of net generators (anything that generate power); returns number
@powerNetOut
the load power of net (anything that uses power); returns number
@ammo
@ammoCapacity
the maximum amount of items (ammo) a turret can hold; returns number
@health
the current health of object, returns number
@maxHealth
the maximum health of object, returns number
@heat
@efficiency
@progress
progress of an object, eg, launch pad when on their cooldown, foreshadow when on their cooldown;
returns number, 0 - 1
@timescale
multipliers of boosted object (overdrived with overdrive projector or overdrive dome), returns number,
1 is normal value, 1.5 is when overdrive with overdrive projector, 2.5 is dome
@rotation
rotation of object, returns number in degrees, 360/0 is right, 90 is up, 180 is left, 270 is down
@x
the x coordinate value location of object; returns number
@y
the y coordinate value location of object; returns number
@shootX
the x coordinate value of what that object is targeting, returns number, if used on a player will return
their cursor position
@shootY
the y coordinate value of what that object is targeting, returns number, if used on a player will return
their cursor position
@size
the size of object; returns number, eg, foreshadow is 4 because it is 4x4, information can also be
found on ingame’s info menu
@dead
whether the object is dead or not, returns booelan, 1 is dead, 0 is not dead
@range
range of object; returns number, information can also be found on ingame’s info menu
@shooting
whether the object is shooting or not, returns boolean, 1 is shooting, 0 is not shooting
@boosting
whether the object is boosting or not; returns boolean, 1 is boosting, 0 is not boosting
@mineX
if unit is mining returns the x coordinate value of that unit mining location, else returns -1
@mineY
if unit is mining returns the y coordinate value of that unit mining location, else returns -1
@mining
whether the unit is mining or not; returns boolean, 1 is mining, 0 is not mining
@speed
the object maximum speed, in tiles/second; returns number, information can also be found on
ingame’s info menu, there is currently no variable for current speed
@team
the object team; returns integers, 1 is sharded, 2 is crux, ….
@type
the object’s name (not reference), eg, for router returns `@router` also not a string
@flag
the object’s flags; returns number, unit can be flagged with `ucontrol flag`
@controlled
check if unit is controlled, returns :
0 , if unit is not controlled by anything
@ctrlProcessor or 1, if unit is controlled by processor
@ctrlPlayer or 2, if unit is controlled by a player
@ctrlCommad or 3, if unit is controlled by a player command
@controller
returns the controller reference, if processor will returns that processor building reference, else
returns the unit reference itself
@name
name of player, returns string
@payloadCount
number of payload the unit is currently carrying, returns number
@payloadType
name of payload, will return only the last picked up payload, returns name
@id
returns the object ID, returns integer
@enabled
whether the object is enabled or not, returns boolean; 1 is enabled, 0 is disabled
@config
configuration of object, eg, sorter item names
@color
color of illuminator, returns packed color
Blocks and items
draw clear r g b
color r b g a
col n
stroke n
line x y x2 y2
rect x y width height
lineRect x y width height
poly x y sides radius rotation
linePoly x y sides radius rotation
triangle x y x2 y2 x3 y3
image x y @copper size rotation
print "frog"
drawflush display1
printflush message1
set result 0
op add result a b
sub
mul
div
idiv
mod
pow
equal
notEqual
land
lessThan
lessThanEq
greaterThan
greaterThanEq
strictEqual
shl
shr
or
and
xor
not
max
min
angle
angleDiff
len
noise
abs
log
log10
floor
ceil
sqrt
rand
sin
cos
tan
asin
acos
atan
packcolor result r g b a
wait 0.5
stop
end
jump -1 equal v1 v2
notEqual
lessThan
lessThanEq
greaterThan
greaterThanEq
strictEqual
always
ubind @poly
ucontrol idle
stop
move x y
approach x y radius
pathfind x y
autoPathfind
boost enable
target x y shoot
targetp unit shoot
itemDrop to amount
itemTake from amount
payDrop
payTake takeUnits
payEnter
mine x y
flag value
build x y block rotation config
getBlock x y type building floor
within x y radius result
unbind