Mindustry

Mindustry

Not enough ratings
Logic 6.0
By Panterich
Listing of processor instructions
   
Award
Favorite
Favorited
Unfavorite
INTRO
Main reason for this guide is to give easy access to necessary instruction reference material for people new to logic in mindustry, but reasonably acquainted with coding in general. So no handholding is originally planned (yet still possible). In accordance with trello board[trello.com]this information should be available inside the game at some point, and there is also some coverage at other places, but nothing in Steam guides yet. I know this would`ve helped me a LOT earlier, and same questions keep popping up now and then in discussions, so here it is.

It is highly advised to first make yourself acquainted with information in official wiki[mindustrygame.github.io](4 pages atm).
read / write
# read <result> = <link> at <address> read <result> <link> <address> # write <data> to <link> at <address> write <data> <link> <address>
Allows to store or retrieve a number to/from memory block. Memory block should be linked to processor and specified in <link> parameter. Memory blocks can store several numbers, so <address> parameter specifies which one you want, starting from 0. <data> parameter will be written to memory, while <result> will contain read number. Texts and other objects can`t be stored that way.

Consult core database or wiki for memory blocks stats.
draw / print / flush
draw clear 0 0 0 drawflush <link> print <var> print "text\nmore text" printflush <link>
Draw and print instructions will put specified data into respective buffer, that can later be flushed by respective flush instruction into persistent storage of suitable linked block - displays for drawing and messages for printing. Flushing clears the buffer in both cases. Both buffers are limited in size as well as both target blocks.

Drawing
Flushing doesn`t initialize display`s state by itself. Add clearing instruction if needed.
I was never interested in drawing myself, so there will be no details here, at least for now.
You can find some in reddit guide linked at the end.

Printing
Flushing will clear block`s storage before populating it with new text. Use '\n' as a newline.
Output typed text by using double quotes. You can supply a variable with number or text.
You can also supply other objects. In that case their name parameter or object name will be printed in most cases. Consult source for more details.

You can look up actual limits for messages here[github.com]
public int maxTextLength = 220; public int maxNewlines = 24;

Single instruction can have only one parameter and there are no inline resolutions.
print "Processor coords\n " print @thisy print ":" print @thisy printflush message1
control
# set enabled of <link> to <state> control enabled <link> <state> # set shoot of <link> x <x> y <y> shoot <state> control shoot <link> <x> <y> <state> # set shootp of <link> unit <target> shoot <state> control shootp <link> <target> <state> # set configure of <link> to <value> control configure <link> <value>
Control instruction can be used to change specified property of a <link>, which should be a building. Only linked buildings can be used. An object can be used for <value> or <target> parameters. Parameter <state> should be conversible to number, where 0 would mean false.

enabled
to enable/disable linked buildings. See wiki for more info
shoot
shootp
to shoot at either specified coordinates or target
configure
to setup such buildings as unloaders and sorters. Works with numbers and objects both nicely, in variables and constants. Value of 0 acts as 'unset'.

#4291[github.com]
Originally posted by Anuken:
targetp doesn't support allied blocks
(will further need to check if this is related to control as well and if there were any changes)

Additional attention should be paid when running Control instruction in a loop, as its 'cooldown' is actionable in that case, and will fail if executed multiple times in quick succession.
radar (uradar)
# from <radar> target <t1> and <t2> and <t3> order <order> sort <sorting> output <result> radar <t1> <t2> <t3> <sorting> <radar> <order> <result>
Radar instruction allows to find an instance of specified targets according to specified <sorting> rules and <order>, using specified object <radar> as a base point of search. Target scope is a logical conjunction of <t1> and <t2> and <t3>. Parameter <order> specifies if sorting is done in ascending or descending order. Parameter <sorting> specifies upon which property should targets be sorted. Parameter <result> will contain reference to first object in sorted list of found targets, or null if nothing was found (or if <radar> is not valid). An object reference should be used as <radar> which either has a range or can be controlled.

It is important to account that search is done exclusively in the range of <radar>. So <radar> must be able to shoot and have a range. Thus a common question "are there enemies present anywhere on the map?" becomes quite complicated atm, because nothing has a range that covers whole map.

Execution of single radar instruction multiple times in quick succession will produce same result regardless of its parameters, as it will return cached result.
sensor
# <result> = <property> in <object> sensor <result> <object> <property>
Retrieves specified <property> of <object>. Parameter <result> will contain the result, either a number or an object.

Properties
(will need to add some ambiguous properties descriptions there later)

Examples
# amount of copper in linked container sensor result container1 @copper # item carried by bound unit .. sensor item @unit @firstItem # .. and its amount sensor amount @unit item # also amount of carried items sensor result @unit @totalItems # health, to further check if target is still "alive" sensor health target @health
end / jump
The only reason this section exists is because several sources mention a difference between using end instruction and naturally reaching the last instruction.

The only thing end instruction does is it puts total instruction count value into @counter variable. That means execution jumps to the next instruction after the very last one, practically meaning the same as naturally reaching the end of instructions list.

I`ll also note that there were bugfixes in comparison implementation within jump instruction where type convertions were needed. So it might be safer to avoid ambiguous comparisons.
unit *
The idea with unit instructions is that you need to bind a unit first, and can then use other instructions upon bound unit.
@unit constant contains bound unit reference (or null). Can be conveniently used with sensor instruction.
Both unit control and unit locate instructions will set 'controlled' property of bound unit for this duration (source[github.com])
public static final float logicControlTimeout = 10f * 60f;

Binding
# bind specified unit or unit type ubind <unit> ubind <type> # bind nothing (unbind) ubind null
Only one unit can be bound to single processor at the same time, but a single unit can be bound to multiple processors. There is no unbind instruction, so you will need to bind something else in case you need that, and it doesn`t have to be anything valid - you can bind null or 80085.

If <type> is provided, first unit of that type will be bound (if available). Every next bind with the same <type> will pick next unit of that type, so running that instruction in a loop will iterate over all units of that type.

Controlling
# getBlock x <x> y <y> type <type> building <building> ucontrol getBlock <x> <y> <type> <building>
This will set <type> and <building> variables in accordance with what is present at specified coordinates IF it is in range, or null otherwise.

stop
is to interrupt movement, mining and building
approach
is to keep specified distance to target
pathfind
s to follow closest command center
target
targetp
same as with control instruction
itemTake
itemDrop
is to transfer items to/from cargo, works within 9 tiles (same for mining)
payTake
payDrop
is to transport units and buildings by carriers
flag
is to assign a number to persistent storage of bound unit
within
will set last parameter in accordance with bound unit being within other parameters

locating
Allows searching for closest (to bound unit) specified ore, enemy spawn point, allied damaged building or closest building of specified type.
Examples
OUTRO
Links to reddit logic guides:
https://www.reddit.com/r/Mindustry/comments/ic9wrm/logic_in_60/
https://www.reddit.com/r/Mindustry/comments/kfea1e/an_overly_indepth_logic_guide/
(it seems some info is either incorrect, or outdated)

Link to source:
https://github.com/Anuken/Mindustry/blob/master/core/src/mindustry/logic/LExecutor.java


Feel free to submit usefull short examples of either specific instructions use, or of logic in general. Make sure to use code formatting and provide descriptions and comments.

4 Comments
antalopeguy Apr 17, 2023 @ 10:49am 
i used to play this game when you could control factory units with a command center. do not understand microprocessors and no guide ive seen so far has even basic examples of how to use factory units minimally.
Bilib Jan 24, 2023 @ 9:48am 
Hi, do you know of a way to iterate through multiple objects with the RADAR command? In case I want to control multiple units in range not just the first in order.
CDV Quailman Oct 25, 2022 @ 12:33pm 
This amazing and so necessary. I'm playing bleeding edge logic V7, and am struggling to no end. Not having a language reference has been really challenging. Thank you for this great work.
Corvus Jun 19, 2021 @ 6:57am 
Woah.