User:RA2lover/Sandbox/Printer Automation Notes
From Stationeers Community Wiki
More actions
Printer Stack Processing
All Printers inherit from SimpleFabricatorBase, which does the following actions on each logic tick:
- Check if the printer is usable and not busy, skipping all processing for the tick otherwise.
- A usable printer is built, turned on, and provided with power.
- Any of these make the printer count as busy:
- Printer is Activated (LogicType.Activate is 1)
- Printer purge handle is open and there are still reagents in its list
- Printer has an occupied export slot
- The check appears to fail to check if the printer is submerged.
- Check if the printer has been instructed to print something. If so, read the current print instruction.
- If its quantity field is 0, clear the missing reagents from its stack memory and advance its stack pointer once, then do nothing for the rest of the logic tick.
- Otherwise, check if the printer is *not* Activated, then do the following:
- If the printer has enough materials to continue printing, clear missing reagent memory and activate the printer, skipping the rest of the tick.
- If the printer's current print instruction has no invalid handler address, clear missing reagent memory, advance its stack pointer once, then do nothing for the rest of the tick.
- If the printer's current print instruction has an invalid handler address, clear missing reagent memory and current print instruction, jump to the invalid handler address, and do nothing for the rest of the tick.
- If the printer has been instructed to wait until the next valid instruction, populate the missing reagent memory with missing reagents and do nothing for the rest of the tick.
- If there's no current print instruction to process, set the invalid handler to int.MinValue and read the address pointed by the stack pointer 54 times, doing the following processing depending on opcode:
- Opcode 0 (Undocumented, "NOP"): Advance the stack pointer.
- Opcode 2 (ExecuteRecipe):
- Read the opcode's quantity field. If >0, try to create a print instruction from the provided hash (and existing invalid handler if one is available).
- Opcode 3 (WaitUntilNextValid):
- Set the invalid handler to -1 (as opposed to int.MinValue) and advance the stack pointer.
- Opcode 4 (JumpIfNextInvalid):
- Set the invalid handler to the address within the opcode (clamped between 0 and 53) and advance the stack pointer.
- Opcode 5 (JumpToAddress):
- Set the stack pointer to the address within the opcode (clamped between 0 and 53), then stop processing instructions for the rest of the tick.
- Opcode 6 (DeviceSetLock):
- Set the device's Lock based on the instruction's contents, erase the instruction's contents, and advance the stack pointer.
- Opcode 7 (EjectReagent):
- Check if printer contains the reagent in the instruction's contents.
- if so, stop processing instructions for the rest of the tick while moving the reagent to the output port (up to 500g at a time) once it's unoccupied.
- Once the printer no longer contains the instruction's reagent, erase the instruction's contents and advance the stack pointer.
- Check if printer contains the reagent in the instruction's contents.
- Opcode 8 (EjectAllReagents):
- Open the ejection handle and stop processing instructions for the current tick while reagents are still on the printer. Once empty, close ejection handle, erase instruction conents and advance the stack pointer.
- Any other opcode:
- Set invalid handler to int.MinValue and advance the stack pointer.
Side Effects
On exporting an item:
- If printer is activated and has a current print instruction, decrement its quantity and update stack memory contents.
- If this results in a quantity of 0, erase the instruction's contents and advance the stack pointer.
On printer writing to stack memory:
- (UNTESTED) Clear unused bits
On advancing the stack pointer:
- If the incremented stack pointer goes past address 53, move stack pointer to 0 instead.
- Write to the stack pointer address (63)
On reading/writing to out-of-bounds addresses:
- Throw a stack underflow / stack overflow exception