What you learn today
- Quantity and MaxQuantity
- Sum over many lockers
- Compute free space
The ore store holds six lockers full of iron ore. How much is left? And when do you have to build another locker? Instead of opening every locker you let the chip count.
Quantity and MaxQuantity
Every slot reports two amounts:
| LogicSlotType | Meaning |
|---|---|
Quantity | how many items sit in the slot |
MaxQuantity | how many fit on this stack at most |
A stack of iron ore with 37 of 50 items returns Quantity 37 and MaxQuantity 50. Things that do not stack, such as a tool, count as 1 of 1.
ls amount locker 0 Quantity # items in slot 0
ls room locker 0 MaxQuantity # fits on the stack at most
sub free room amount # still free on this stack
An empty slot reports Quantity 0 and MaxQuantity 0 as well. So you can only work out the free space of a stack where something already sits. Empty slots are counted via Occupied instead.
Across many lockers: lbs with Sum
lbs reads one slot of all devices of a type. Amounts really add up, so Sum is right here and not Average:
lbs total LOCKER 0 Quantity Sum # slot 0 of all lockers together
But a locker has more than one slot. The slot number may sit in a register, so a loop can run over all slots:
move total 0
move i 0
slot:
lbs value LOCKER i Quantity Sum
add total total value
add i i 1
blt i SLOTS slot
The small locker (StructureLockerSmall) has 4 slots, the normal locker (StructureStorageLocker) 30. A loop over 30 slots costs a good 120 instructions, almost a whole tick. In a store half a second does not matter.
Free slots
Occupied is 1 for a used slot. Summed over all lockers this gives the number of used slots. The number of lockers comes from Count:
lb lockers LOCKER PrefabHash Count
mul free lockers SLOTS
sub free free used
Count needs a LogicType the device can read, otherwise the chip reports IncorrectLogicType. Every device can read PrefabHash, so with it you are always right.
Only count with Count if all lockers of this type belong to the store. Count ignores names. We look at that more closely in week 14.
Diagram
Exercise
Task
Small lockers (StructureLockerSmall, 4 slots each) stand on the data network of the store. Count across all lockers:
- the total number of items in all slots → LED display “Bestand”
- the number of free slots → LED display “Frei”
If someone removes a locker, both numbers simply drop – without an error.
Hints
Hint 1
Reset total, used and i to 0 at the start of every pass.
Hint 2
Inside the loop use lbs … Sum twice: once Quantity, once Occupied.
Hint 3
Free slots = lockers × SLOTS − used slots. lb lockers LOCKER PrefabHash Count counts the lockers.
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