Skip to content

Packing data

There are few slots in memory, and a unit has exactly one flag. Several numbers have to be folded into one

tick 0
Set0
=
Set1
=
Operation2
=
Operation3
=
Operation4
=
Operation5
=
Stop6
@counter0number
xnullnull
ynullnull
packednullnull
backXnullnull
backYnullnull
op mul packed x 1000
op add packed packed y
op idiv backX packed 1000
op mod backY packed 1000

The coordinates 37 and 12 turned into a single number 37012 — and came apart again. That is packing: multiply the high part by the place value, add the low one.

Action With what
pack op mul and op add
take out the high part op idiv — integer division
take out the low part op mod — the remainder

There are few places in mlog where a number can be put:

  • a unit’s flag — exactly one number per unit;
  • a memory cell — 64 slots, a memory bank — 512;
  • a neighbour’s variable, which it reads by name.

When you have to pass “unit number three is carrying copper to the store at point 37, 12”, it has to be folded into one number. That is usually just what is done: the low digits are the coordinate, the high ones the task.

The second way is bitwise operations: shift left, add, then shift back and apply a mask.

op shl packed x 10
op or packed packed y
op shr backX packed 10
op and backY packed 1023

That is easier to reason about in terms of “how much fits”: ten bits are a thousand and twenty-four values, and the bounds are exact. The number reads worse in the variables, though: 37900 instead of the legible 37012.

  • pack integers. A fraction breaks in packing: op idiv throws part of the number away, and it will not come back;
  • watch out for negatives. -5 in packing turns into rubbish: if you need a sign, add an offset (always +500, say) and subtract it when unpacking;
  • do not pack what need not be packed. A cell has 64 slots — usually enough to put the numbers in separately and spare yourself the trouble.

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.