stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 284

Day 284 of 365 · Expert · Month 10: Bits and data

Enum values as numbers

Behind every name like LogicType.Temperature or Color.Red there is a number: reading namespaces as numbers, translating values back and building translation tables in the stack.

Lesson 16 min Week 41

What you learn today

  • Namespaces as numbers
  • Translate values back
  • Build tables

The battery reports Mode 2. What does that mean? The display should not show a number but a colour: red, orange, yellow, green. For that you need to know which numbers hide behind the names – and how to translate the other way round.

Namespaces are numbers

Every name with a prefix is a constant. The preprocessor does not replace it, but the chip reads it as a number:

NameNumber
LogicType.Temperature6
Color.Red4
Color.Green2
GasType.Oxygen1
GasType.Nitrogen2
GasType.Air3
SortingClass.Ores9

Notice something about GasType? The gases are bit values: Oxygen 1, Nitrogen 2, CarbonDioxide 4, Methane 8. GasType.Air = 3 is oxygen and nitrogen – two bits set. That is bit week, just hidden in the game.

Warning

Bare names are only valid at their typed position: l r0 d0 Temperature is right, move r0 Temperature is an error. At number positions you need the prefix: move r0 LogicType.Temperature.

Translating values back

Devices often report a state as a number. The meaning is in the list of modes, for the battery for example:

ModeMeaning
0Empty
1Critical
2VeryLow
3Low
4Medium
5High
6Full

For a few values a select chain is enough. For many a table is better: the value read is the index, the translation sits at that address in the stack.

Building tables

Before start: you set up the table once with poke. In the loop get … db index reads the matching entry:

poke 0 Color.Red       # Empty
poke 1 Color.Red       # Critical
poke 2 Color.Orange    # VeryLow
start:
yield
lbn mode BATT HASH("Haupt") Mode Average
get colour db mode     # mode is the index
Warning

A NaN as an index is dangerous: converted into an integer it gives no valid address, and get halts the chip. Check the value with snan first and set a safe index before you use get.

Tip

The table costs seven lines. A select chain for seven values would be twice as long – and you would have to hunt for every colour change in the chain instead of in a list.

Diagram

The stack with its pointer slot 0 10 slot 1 20 slot 2 30 slot 3 slot 4 sp = 3 next free slot push 40 pop r0 r0 = 30 last in, first out
The battery's mode is the index, the table in the stack returns the matching colour.

Exercise

Task

The table mode → colour is already in the stack. Read the Mode of the battery “Haupt” and output on the display “Akku”:

  • Setting = mode, −1 without a battery
  • Color = colour from the table, Color.Gray without a battery

With NaN, get must not run with NaN as the address.

Hints

Hint 1

snanz ok mode, then select mode ok mode 0 – that way the index is always valid.

Hint 2

get colour db mode reads the colour.

Hint 3

Finally replace both values for the failure case with select.

Quiz

4 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere