Skip to content

Sorting and the cache

A radar answers not «who is there» but «who is best». And it does not answer afresh every time

mediumYou need: Finding a unit
tick 0
Radar0
target1target2target3sortradarsortOrderoutput
Radar1
target1target2target3sortradarsortOrderoutput
Radar2
target1target2target3sortradarsortOrderoutput
Radar3
target1target2target3sortradarsortOrderoutput
Sensor4
=
in
Sensor5
=
in
Stop6
@counter0number
nearnullnull
farnullnull
toughnullnull
weakestnullnull
nearHealthnullnull
weakHealthnullnull

Four radars, all with the same filters — only the measure and the order differ. The dagger is closer and healthier, the flare further away and weaker.

Measure What it compares
distance the distance to the block
health current health
maxHealth maximum health
shield shield
armor armour

Zero and one flip the choice:

Measure and order Who it finds
distance 1 the nearest
distance 0 the furthest
health 1 the toughest
health 0 the weakest

An easier way to remember it: one means “the best by the measure”, zero “the worst”. For distance, “best” means nearest, because that is usually who you are after.

tick 0
Radar0
target1target2target3sortradarsortOrderoutput
Control1
set of
unitshoot
Sensor2
=
in
Operation3
=
@counter0number
aimnullnull
deadnullnull
loopsnullnull

A radar does not search afresh every iteration. A block’s result is recomputed once every 30 ticks — half a second — and between recomputations the previous answer is handed back. This is done for speed: a dozen radars in a program would otherwise eat a frame.

Press “run” and watch dead. The turret finishes the flare off, and for a while aim still points at it — only now with @dead equal to one.

Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.