What you learn today
- Collect the harvest
- Count storage
- Avoid overflow
The Harvie puts every harvest into its export slot. From there it goes on through a chute, in the simplest case into a fridge. If that fills up, everything backs up to the Harvie, and the next ripe plant cannot be harvested.
The way of the harvest
- The Harvie harvests, the produce lands in slot 1 (export).
- Through the export port it goes into a chute.
- At the end there is a store: fridge (
StructureFridgeBig, 15 slots) or locker (StructureStorageLocker, 30 slots).
A fridge slows down the decay of food. That makes it the better choice for the harvest.
Counting the store
Every slot reports Quantity (number of items) and Occupied. For the sum over all slots you walk through the slot numbers with a loop. lbns with Sum adds each number over all fridges of the same name. Here Sum is right, because quantities really add up.
move total 0
move used 0
move idx 0
count:
lbns tmp FRIDGE HASH("Vorrat") idx Quantity Sum
add total total tmp
lbns tmp FRIDGE HASH("Vorrat") idx Occupied Sum
add used used tmp
add idx idx 1
blt idx SLOTS count
15 passes with six instructions are 90 instructions, which fits into one tick. With a locker of 30 slots it would be 180. Then the loop spreads over two ticks by itself, because the chip pauses after 128 instructions. So always evaluate the result only after the loop.
Avoiding overflow
Occupied with Sum counts the occupied slots. If all are occupied, the store is full – more precisely: almost full, because a stack can still grow. For the Harvie a simple rule is enough:
- store full or Harvie export slot occupied → Harvie off, light on.
- An occupied export slot means: the chute takes nothing more.
sge stop used CAPACITY # all occupied
lbns tmp HARVIE HASH("Ernter") 1 Occupied Average # export full?
sgtz tmp tmp
or stop stop tmp
Sum returns 0 without a matching device, not NaN. A removed fridge then looks like an empty one – and the Harvie harvests into nowhere. Check presence separately, for example with PrefabHash and Average: without a device that gives NaN.
Two fridges of the same name have 30 slots together, but the loop still only runs over the numbers 0 to 14 – lbns adds each number over both. That is why there are two defines: SLOTS for the loop, CAPACITY for the total.
Diagram
Exercise
Task
The Harvie “Ernter” delivers into the fridge “Vorrat” (15 slots). Write a chip that every tick:
- counts all 15 slots and shows the total number of items on the display “Vorrat”
- switches the Harvie off and the light “Lager” on when all slots are occupied, the Harvie's export slot 1 is occupied or the fridge is missing
- otherwise switches the Harvie on and the light off
- shows −1 without the fridge
Hints
Hint 1
Loop with idx from 0 to 14, inside two lbns … Sum for Quantity and Occupied.
Hint 2
Full: sge stop used CAPACITY. Jam: lbns on slot 1 of the Harvie.
Hint 3
Presence: lbn tmp FRIDGE HASH("Vorrat") PrefabHash Average and snan.
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