--- Title: ACNH Printer - a writeup! Date: 2020-11-05 Category: dev Tags: writeup, gaming, technical, python Ad: cheat at animal crossing('s pattern designer) anfscd: A robot that draws patterns --- This is a writeup of a project I did in April but never released. Well, I've [definitely released it now](https://github.com/GiovanH/ACNH-Printer), if you want to give it a try! Instead of a real introduction, here's a video demo, with camcorder LP technology from 2005: ![Printer Demo](https://www.youtube.com/embed/GFba91hnWyo) *I am not going to buy a capture card* Ever since Wild World, Animal Crossing has had a pattern system, where players can design their own textures and use them as clothes or decoration. New Horizons has one, but since it doesn't have a stylus you have to either use the directional pad to mark individual pixels or draw with your fingertip. I thought it would be fun to find a way to automate that. Now, granted, it takes a while, but it's still much faster than trying to copy pixels over by hand. ## The USB bit Using work done by shinyquagsire23's [Switch Flightstick](https://github.com/shinyquagsire23/Switch-Fightstick/) project, you can use a Teensy board to simulate a controller and send commands programatically. The Teensy only has a tiny amount of memory, though, so any program program has to be pretty small. This rules out, say, loading a 32x32 png file and some commands to the board; you need to generate a compressed instruction set on a computer and compile that. The engine I wrote for the Teensy is fairly simple: I define a series of steps in a large array, and then iterate through that: ```c #include "types.h" uint8_t step[] = { 8, 8, 1, // X (SetDrawingF) x 1 0, 0, 1, // HAT_UP x 1 4, 8, 1, // A (SetTool) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 8, 1, // NONE x 1 4, 8, 1, // A (ResetPalette) x 1 0, 8, 1, // NONE x 1 0, 2, 1, // HAT_RIGHT x 1 4, 8, 1, // A (SetTool) x 1 ... ``` (It also appends some boilerplate for setting up the controller, like pressing the triggers together.) My code treats each group of 3 `uint8`s as a logical frame, and sends one frame every 12 cycles. This times to just about 30 fps. The logical frame itself is extremely compressed: I never need to touch the analog stick, just the buttons and the hat. The first number is the data set for the hat, the second number is the data sent for the buttons, and the last number is the number of times that frame is repeated. Button data and hat data here are, of course, bitmasks, using the descriptors in `types.h`. When the Teensy is flashed and plugged in to the Switch via USB, it starts pressing buttons, until it has pressed all the buttons, and then it stops. That's enough for the Teensy to run simple programs, so long as I can write and compile them. ## Generating the program So, with that working, you still need to do all the logical work of figuring out what buttons to press, and in what order. First, a breakdown of how patterns actually work: The canvas[^canvassize] is 32x32, and you can draw on it with a number of simple, MSPaint style tools. For this project, I use the pencil and the global fill tool, along with the "mirror" option. There are likely more optimizations you could do by using more tools, but I haven't done that yet. [^canvassize]: For standard patterns, which is all I've done so far In the New Horizons pattern designer, you can define your own color palette. You get 15 color slots to work with, and each color is defined by hue, saturation, and value. There are 30 hue increments, and 15 increments each for saturation and value. Now, the first thing you need to do, strictly speaking, is generate a 32x32 image file with only 15 unique colors. I don't automate that, I just use Paint.NET. Then you open that file with [`makesteps.py`](https://github.com/GiovanH/ACNH-Printer/blob/master/makesteps.py), which is where all the logic for the program lives. The general system for generating the instruction set is thus: 1. Load an image 2. Create a virtual 32x32 canvas and set it to the defaults the game uses 3. Generate a game-ready palette 4. Incrementally edit the virtual canvas using the tools available in the game, recording each step 5. Convert that series of steps to button presses 6. Flash the Teensy and let it run ### 1. Load an image When I load the image, I validate it. It needs to be 32x32, it needs to have a palette of 15 colors or fewer, etc. ### 2. Create a virtual canvas In order to make sure I draw the image correctly -- and so it's easy to check for errors -- I make a virtual canvas and simulate the entire drawing process. The actual virtual canvas is a simple python object: ```python class ACCanvas(): def __init__(self, width, height): super(ACCanvas, self).__init__() self.w = width self.h = height self.canvas = [[None for x in range(width)] for y in range(height)] self.palette = None self.steps = [] ``` Here, the pixel values are stored in `self.canvas`, which is just a two-dimentional list initialized to all `None`s. The `ACCanvas` class is also where I store helper functions that work on the canvas array, like `genPalette` to determine what colors are on the canvas, or `toImage` to create a `PIL.Image` version I can save as a png. This is especially nice because it lets me export the finished image locally. Because of the color limitations of the game, the colors in the original image will be clamped somewhat, and the virtual canvas lets me test what that looks like. ACNH also uses the [6xBRZ](https://sourceforge.net/projects/xbrz/) scaling algorithm for displaying patterns in-game. 6xBRZ is a pixel-scaling algorithm, so instead of pixel art, the final pattern is made of curves and triangles. The pixels are replaced with lines and shapes that are meant to better represent the meaning of the shape. Here is an example, where the input file is pixel art of a horse fighting a football player: ![Without xBRZ scaling](./horse.png){: style="width: 192px;image-rendering: pixelated;"} ![With xBRZ scaling](./horseScale.png) {: .side-by-side .align-top} ### 3. Generate a game-ready palette Now I take all the colors in the input image and put together a palette that can be used in the game. The first and most obvious step is converting the colors: as mentioned before, ACNH has 30 possible hues and 15 possible saturations and values, whereas the input image has 256 possible states for each. The first order of business, then, is reducing the bits on these. Here's what that looks like: ```python import colorsys # Zero index HUES = 30 - 1 SATS = 15 - 1 VALS = 15 - 1 def percToInt(max): def c(val): return round(val * max) return c def rgbaToACColor(pixel): if pixel[3] == 0: # Transparent, bad # raise NotImplementedError return (-1, -1, -1) # Get RGB values from pixel tuple r, g, b, *_ = pixel # Convert to HSV h, s, v = colorsys.rgb_to_hsv(r, g, b) # Colorsys returns V as an int from 0-255, so turn that into a percentage. # s and v are already floats from 0.0-1.0, so no correction needed. v /= 255 tup = (percToInt(HUES)(h), percToInt(SATS)(s), percToInt(VALS)(v)) return tup ``` This gives us a nice hsv tuple with the game's drastically reduced domain. (The steps for this, by the way, are hideous: hundreds of individual ticks to configure the sliders exactly right for each color.) This isn't good yet, though, because there are a number of compression steps to do: - Set all colors with V=0 to the same hue (they're white/grey/black) - Set all colors with S=0 to the same hue and vividness (they're black) Otherwise, we might waste multiple color slots on identical colors. In the future, it might be possible to some sort of optimization by putting colors that frequently appear adjacent to each other in the image closer together on the palette, to reduce the number of color swaps. ### 4. Incrementally edit the virtual canvas This is, by far, the most complicated part of the program, because we don't just have to mark each pixel correctly, we need to do it *efficiently.* Otherwise it'll take a very long time to run, or else it might not fit on the Teensy at all. All our drawing methods are stored in a class called `Printer`; every actual printer is a class that inheirits from this. It has functions like `moveTo` to move the cursor to a pixel, `setColor` to change the selected color, `setTool` to set the selected tool, etc. More on the printer implementation in section 5. The very simplest implementation of this is `NaivePrinter`, which simply visits every pixel on the canvas and marks it: ```python class NaivePrinter(Printer): def drawImageCustom(self, source): new_steps = [] for x, y in self.smartTraverse(source): new_steps += self.markPixel(x, y, source.canvas[x][y]) return new_steps ``` This isn't quite as bad as it sounds, because `smartTraverse` tries to be somewhat sensible in how it traverses the canvas: by default it snakes around, so as to traverse the full canvas in one continuous line without wasting time by covering any spots it already visited: (4x4 canvas to demonstrate): ``` 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 ``` Now, I'll be honest with you, I'm lousy[^lousy] with algorithms. Algorithms are obtuse, horrible little things that should be buried away in functions and never looked directly at. So, instead of doing any incredibly robust analysis of the source image to determine what method would correctly print the full image in the fewest number of steps, I just try a *lot* of different methods and check afterwards to see which one is fastest. Because each step takes a logical frame, it's easy to quickly time this. [^lousy]: "Lousy" here meaning "poor" (re: performance) rather than "overrun with", although an argument could be made for either. In addition to the `NaivePrinter` shown above, here are some other methodologies I use: `ScreenPrinter` prints one color at a time: ```python class ScreenPrinter(Printer): def drawImageCustom(self, source): new_steps = [] for color in source.palette: for x, y in self.smartTraverse(source): if source.canvas[x][y] == color: new_steps += self.markPixel(x, y, color) return new_steps ``` This can save a significant amount of time depending on the source image, as changing color midway through the image can be expensive. `SpiralPrinter` starts in the middle of the canvas (where your cursor defaults to) and traverses the canvas in a spiral outward. There are also other settings I permute through, like `smartTravers`ing vertically instead of horizontally, or setting the game's "Mirror" mode to print two spaces at once, or using the "Fill All" tool at the beginning of the print. ### 5. Convert that series of steps to button presses Each of these functions that edits the virtual canvas also yields a list of steps. In the python script, steps are stored as enums that represent button presses, with their values being (button, hat) tuples, as such: ```python class Step(enum.Enum): NONE = (0x0, 0x08) Y = (0x01, 0x08) B = (0x02, 0x08) A = (0x04, 0x08) ... HAT_UP = (0, 0x00) HAT_RIGHT = (0, 0x02) ... ``` As I edit the canvas, I collect those movements into a list, and at the end of the process I serialize that list to the teensy steps format, as described earlier. ```python class Printer(): ... def toSteps(self): new_steps = [] self.source.genPalette() new_steps += self.setPalette(self.source.palette) new_steps.append(Step.NONE) new_steps += self.drawImage(self.source) return new_steps ``` Except I don't *just* do that, because that would be extremely slow. If you did every step sequentially, you would end up wasting a ton of time, because there are a lot of steps that can run in parallel. By the time you actually press the A button and write the pixel, everything needs to be set properly. However, before then, multiple steps can be folded together. Take, as an example, needing to mark a pixel that is 4 pixels away and 5 colors away. Moving the cursor uses the hat only, and changing the color uses the trigger buttons. Instead of doing - Right x4 (move cursor) - LR x5 (change color) - A x1 (mark pixel) sequentially, you can collapse the steps into - Right + LR x4 (move cursor & hange color) - LR x1 (change color) - A x1 (mark pixel) See how this dramatically improves performance: ```mermaid gantt axisFormat %S section Sequential Set color :a1, 0, 5s Move cursor :a2, after a1, 4s Mark spot :a3, after a1 a2, 1s section Parallel Set color :b1, 0, 5s Move cursor :b2, 0, 4s Mark spot :b3, after b1 b2, 1s ``` This algorithm actually implements step folding: ```python from itertools import zip_longest def foldSteps(step1, step2): if step1.value[0] != 0x0 and step2.value[0] != 0x0: # Both steps contain a button press, cannot fold return [step1, step2] if step1.value[0] == Step.LR.value[0] or step2.value[0] == Step.LR.value[0]: # At least one step contains LR, cannot fold return [step1, step2] if step1.value[1] != 0x8 and step2.value[1] != 0x8: # Steps contain different hat directions, cannot fold return [step1, step2] else: return [PseudoStep(step1).plus(step2)] def foldStepSeqs(steps1, steps2): if not steps1: return steps2 if not steps2: return steps1 new_steps_compressed = [] for xstep, ystep in zip_longest(steps1, steps2, fillvalue=Step.NONE): new_steps_compressed += foldSteps(xstep, ystep) return new_steps_compressed ``` In addition to this postprocessing, there's additional compression and optimization throughout the step generation process. `Printer.moveCursorTo(x, y)` uses `zip` to group steps together and use diagonal hat movement when possible. Setting commands like `setMirrored` and `setTool` use state to keep track of the setting, and don't require a step if the option is already set correctly. ### 6. Flash the Teensy and let it run And I'm done! Just plug the teensy into the switch at the right moment and it will print the pattern autonomously. --- That's, uh, all I've got, really. If you've got a Teensy or $20 and want to print your own patterns, or you just want to take a closer look at how this was done, you're welcome to [grab the code](https://github.com/GiovanH/ACNH-Printer/). ## Technical Demo The program itself runs something like this: ``` $ python3 makesteps.py --help usage: makesteps.py [-h] [--dump | --no-dump] [--gen | --no-gen] [--preview | --no-preview] [--bogo | --no-bogo] [--make | --no-make] infile positional arguments: infile Input pattern file optional arguments: -h, --help show this help message and exit --dump Save pattern (Default: True) --no-dump Save pattern (Default: True) --gen Generate steps file (Default: True) --no-gen Generate steps file (Default: True) --preview Generate scaled preview file (Default: True) --no-preview Generate scaled preview file (Default: True) --bogo Find fastest solution (Default: True) --no-bogo Find fastest solution (Default: True) --make Automatically run make after completion (Default: False) --no-make Automatically run make after completion (Default: False) ``` And, running it: (this is the configuration used in the demo video, with `SpiralPrinter` temporarily disabled) ``` $ python3 makesteps.py s/despair.png --make (32, 32) Generated preview at scalepreview.png (00100) printed pattern in 1582 steps (593.25 bytes) (~5m 16s runtime) BEST! (00101) printed pattern in 1582 steps (593.25 bytes) (~5m 16s runtime) (00110) printed pattern in 1106 steps (414.75 bytes) (~3m 41s runtime) BEST! (00111) printed pattern in 1110 steps (416.25 bytes) (~3m 42s runtime) (01000) printed pattern in 1512 steps (567.0 bytes) (~5m 2s runtime) (01001) printed pattern in 1512 steps (567.0 bytes) (~5m 2s runtime) (01010) printed pattern in 1047 steps (392.625 bytes) (~3m 29s runtime) BEST! (01011) printed pattern in 1047 steps (392.625 bytes) (~3m 29s runtime) (10100) printed pattern in 1246 steps (467.25 bytes) (~4m 9s runtime) (10101) printed pattern in 1253 steps (469.875 bytes) (~4m 10s runtime) (10110) printed pattern in 933 steps (349.875 bytes) (~3m 6s runtime) BEST! (10111) printed pattern in 941 steps (352.875 bytes) (~3m 8s runtime) (11000) printed pattern in 1469 steps (550.875 bytes) (~4m 53s runtime) (11001) printed pattern in 1469 steps (550.875 bytes) (~4m 53s runtime) (11010) printed pattern in 1031 steps (386.625 bytes) (~3m 26s runtime) (11011) printed pattern in 1031 steps (386.625 bytes) (~3m 26s runtime) (00100) printed pattern in 1641 steps (615.375 bytes) (~5m 28s runtime) (00101) printed pattern in 1634 steps (612.75 bytes) (~5m 26s runtime) (00110) printed pattern in 1115 steps (418.125 bytes) (~3m 43s runtime) (00111) printed pattern in 1108 steps (415.5 bytes) (~3m 41s runtime) (01000) printed pattern in 1749 steps (655.875 bytes) (~5m 49s runtime) (01001) printed pattern in 1735 steps (650.625 bytes) (~5m 47s runtime) (01010) printed pattern in 1085 steps (406.875 bytes) (~3m 37s runtime) (01011) printed pattern in 1071 steps (401.625 bytes) (~3m 34s runtime) (10100) printed pattern in 1283 steps (481.125 bytes) (~4m 16s runtime) (10101) printed pattern in 1277 steps (478.875 bytes) (~4m 15s runtime) (10110) printed pattern in 980 steps (367.5 bytes) (~3m 16s runtime) (10111) printed pattern in 974 steps (365.25 bytes) (~3m 14s runtime) (11000) printed pattern in 1697 steps (636.375 bytes) (~5m 39s runtime) (11001) printed pattern in 1691 steps (634.125 bytes) (~5m 38s runtime) (11010) printed pattern in 1063 steps (398.625 bytes) (~3m 32s runtime) (11011) printed pattern in 1057 steps (396.375 bytes) (~3m 31s runtime) <__main__.NaivePrinter object at 0x000001F4576CCE20> {'startfill': True, 'usemirror': True, 'vertical': True, 'horizontal': False, 'adjpalette': False} makefile:59: warning: overriding recipe for target 'clean' ../LUFA/LUFA/Build/DMBS/DMBS/gcc.mk:182: warning: ignoring old recipe for target 'clean' [GCC] : Compiling C file "nextreport.c" avr-gcc -c -pipe -gdwarf-2 -g2 -mmcu=at90usb1286 -fshort-enums -fno-inline-small-functions -fpack-struct -Wall -fno-strict-aliasing -funsigned-char -funsigned-bitfields -ffunction-sections -I. -DARCH=ARCH_AVR8 -DF_CPU=16000000UL -mrelax -fno-jump-tables -x c -Os -std=gnu99 -Wstrict-prototypes -DUSE_LUFA_CONFIG_HEADER -IConfig/ -I. -I../LUFA/LUFA/.. -DARCH=ARCH_AVR8 -DBOARD=BOARD_NONE -DF_USB=16000000UL -MMD -MP -MF obj/nextreport.d nextreport.c -o obj/nextreport.o [LNK] : Linking object files into "Joystick.elf" avr-gcc obj/nextreport.o obj/Joystick.o obj/Descriptors.o obj/HIDParser.o obj/Device_AVR8.o obj/EndpointStream_AVR8.o obj/Endpoint_AVR8.o obj/Host_AVR8.o obj/PipeStream_AVR8.o obj/Pipe_AVR8.o obj/USBController_AVR8.o obj/USBInterrupt_AVR8.o obj/ConfigDescriptors.o obj/DeviceStandardReq.o obj/Events.o obj/HostStandardReq.o obj/USBTask.o -o Joystick.elf -lm -Wl,-Map=Joystick.map,--cref -Wl,--gc-sections -Wl,--relax -mmcu=at90usb1286 [OBJCPY] : Extracting HEX file data from "Joystick.elf" avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature Joystick.elf Joystick.hex g++ -c steps.c g++ -c nextreport.c g++ -o test.exe nextreport.o test.o [INFO] : Begin compilation of project "Joystick"... avr-gcc.exe (GCC) 4.8.2 20131010 (prerelease) Copyright (C) 2013 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [OBJCPY] : Extracting EEP file data from "Joystick.elf" avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings Joystick.elf Joystick.eep || exit 0 [OBJCPY] : Extracting BIN file data from "Joystick.elf" avr-objcopy -O binary -R .eeprom -R .fuse -R .lock -R .signature Joystick.elf Joystick.bin [OBJDMP] : Extracting LSS file data from "Joystick.elf" avr-objdump -h -d -S -z Joystick.elf > Joystick.lss [NM] : Extracting SYM file data from "Joystick.elf" avr-nm -n Joystick.elf > Joystick.sym [SIZE] : Determining size of "Joystick.elf" avr-size --mcu=at90usb1286 --format=avr Joystick.elf AVR Memory Usage ---------------- Device: at90usb1286 Program: 4858 bytes (3.7% Full) (.text + .data + .bootloader) Data: 1495 bytes (18.2% Full) (.data + .bss + .noinit) [INFO] : Finished building project "Joystick". Waiting for AT90USB1286... ``` (the bitmasks in parenthesis are permutations of settings) And the generated `steps.c` file, for those curious: ::: spoiler Bad ```c #include "types.h" uint8_t step[] = { 8, 8, 1, // X (SetDrawingF) x 1 0, 0, 1, // HAT_UP x 1 4, 8, 1, // A (SetTool) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 8, 1, // NONE x 1 4, 8, 1, // A (ResetPalette) x 1 0, 8, 1, // NONE x 1 0, 2, 1, // HAT_RIGHT x 1 4, 8, 1, // A (SetTool) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 6, 12, // HAT_LEFT x 12 0, 4, 1, // HAT_DOWN x 1 0, 6, 9, // HAT_LEFT x 9 0, 4, 1, // HAT_DOWN x 1 32, 8, 1, // R (ColorR1) x 1 0, 8, 1, // NONE x 1 0, 4, 2, // HAT_DOWN x 2 0, 6, 14, // HAT_LEFT x 14 0, 4, 1, // HAT_DOWN x 1 32, 8, 1, // R (ColorR2) x 1 0, 8, 1, // NONE x 1 0, 6, 1, // HAT_LEFT x 1 0, 4, 1, // HAT_DOWN x 1 0, 2, 1, // HAT_RIGHT x 1 0, 4, 2, // HAT_DOWN x 2 32, 8, 1, // R (ColorR3) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 6, 12, // HAT_LEFT x 12 0, 4, 1, // HAT_DOWN x 1 0, 6, 7, // HAT_LEFT x 7 0, 4, 1, // HAT_DOWN x 1 32, 8, 1, // R (ColorR4) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 6, 12, // HAT_LEFT x 12 0, 4, 1, // HAT_DOWN x 1 0, 2, 2, // HAT_RIGHT x 2 0, 4, 1, // HAT_DOWN x 1 32, 8, 1, // R (ColorR5) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 6, 11, // HAT_LEFT x 11 0, 4, 1, // HAT_DOWN x 1 0, 6, 1, // HAT_LEFT x 1 0, 4, 1, // HAT_DOWN x 1 32, 8, 1, // R (ColorR6) x 1 0, 8, 1, // NONE x 1 0, 4, 2, // HAT_DOWN x 2 0, 6, 5, // HAT_LEFT x 5 0, 4, 1, // HAT_DOWN x 1 32, 8, 1, // R (ColorR7) x 1 0, 8, 1, // NONE x 1 0, 4, 1, // HAT_DOWN x 1 0, 6, 11, // HAT_LEFT x 11 0, 4, 2, // HAT_DOWN x 2 32, 8, 1, // R (ColorR8) x 1 0, 8, 1, // NONE x 1 0, 2, 7, // HAT_RIGHT x 7 0, 4, 1, // HAT_DOWN x 1 0, 2, 2, // HAT_RIGHT x 2 0, 4, 1, // HAT_DOWN x 1 0, 6, 1, // HAT_LEFT x 1 0, 4, 1, // HAT_DOWN x 1 4, 8, 1, // A x 1 8, 8, 1, // X (SetDrawingF) x 1 0, 6, 1, // HAT_LEFT x 1 0, 4, 1, // HAT_DOWN x 1 4, 8, 1, // A (SetTool) x 1 0, 8, 2, // NONE x 2 16, 8, 6, // L (ColorL2) x 6 8, 8, 1, // X (SetDrawingF) x 1 0, 4, 4, // HAT_DOWN x 4 4, 8, 2, // A (SetTool) x 2 8, 8, 1, // X (SetDrawingF) x 1 0, 0, 4, // HAT_UP x 4 4, 8, 1, // A (SetTool) x 1 32, 7, 4, // HAT_UP_LEFT+R (ColorR6) x 4 128, 7, 1, // HAT_UP_LEFT+ZR (SetMirrorT) x 1 0, 7, 10, // HAT_UP_LEFT x 10 4, 7, 1, // HAT_UP_LEFT+A x 1 4, 4, 10, // HAT_DOWN+A x 10 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 10, // HAT_UP+A x 10 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 10, // HAT_DOWN+A x 10 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 10, // HAT_UP+A x 10 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 9, // HAT_DOWN+A x 9 128, 4, 1, // HAT_DOWN+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 1, // HAT_UP+A x 1 128, 0, 1, // HAT_UP+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 4, 0, 8, // HAT_UP+A x 8 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 8, // HAT_DOWN+A x 8 128, 4, 1, // HAT_DOWN+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 4, 2, 1, // HAT_RIGHT+A x 1 128, 0, 1, // HAT_UP+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 4, 0, 9, // HAT_UP+A x 9 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 10, // HAT_DOWN+A x 10 16, 4, 2, // HAT_DOWN+L (ColorL4) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 4, 4, 2, // HAT_DOWN+A x 2 32, 4, 1, // HAT_DOWN+R (ColorR5) x 1 4, 8, 1, // A x 1 16, 4, 1, // HAT_DOWN+L (ColorL4) x 1 0, 4, 1, // HAT_DOWN x 1 4, 4, 1, // HAT_DOWN+A x 1 0, 3, 1, // HAT_DOWN_RIGHT x 1 0, 4, 1, // HAT_DOWN x 1 4, 4, 1, // HAT_DOWN+A x 1 4, 0, 1, // HAT_UP+A x 1 32, 0, 1, // HAT_UP+R (ColorR5) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL4) x 1 4, 8, 1, // A x 1 4, 0, 1, // HAT_UP+A x 1 32, 0, 1, // HAT_UP+R (ColorR5) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR7) x 1 32, 8, 1, // NONE+R (ColorR7) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL4) x 1 16, 8, 2, // NONE+L (ColorL4) x 2 4, 8, 1, // A x 1 4, 0, 5, // HAT_UP+A x 5 32, 0, 1, // HAT_UP+R (ColorR6) x 1 32, 8, 1, // NONE+R (ColorR6) x 1 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 4, 0, 8, // HAT_UP+A x 8 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 6, // HAT_DOWN+A x 6 16, 4, 1, // HAT_DOWN+L (ColorL3) x 1 16, 8, 2, // NONE+L (ColorL3) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 4, 8, 1, // A x 1 4, 4, 4, // HAT_DOWN+A x 4 16, 4, 1, // HAT_DOWN+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR7) x 1 32, 8, 3, // NONE+R (ColorR7) x 3 4, 8, 1, // A x 1 16, 4, 1, // HAT_DOWN+L (ColorL5) x 1 16, 8, 1, // NONE+L (ColorL5) x 1 4, 8, 1, // A x 1 4, 4, 2, // HAT_DOWN+A x 2 16, 4, 1, // HAT_DOWN+L (ColorL4) x 1 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 32, 4, 1, // HAT_DOWN+R (ColorR5) x 1 4, 8, 1, // A x 1 16, 4, 1, // HAT_DOWN+L (ColorL4) x 1 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 1, // HAT_UP+A x 1 16, 0, 1, // HAT_UP+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR4) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR4) x 1 4, 8, 1, // A x 1 4, 0, 10, // HAT_UP+A x 10 16, 0, 1, // HAT_UP+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR6) x 1 32, 8, 2, // NONE+R (ColorR6) x 2 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 4, 0, 6, // HAT_UP+A x 6 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 5, // HAT_DOWN+A x 5 16, 4, 1, // HAT_DOWN+L (ColorL3) x 1 16, 8, 2, // NONE+L (ColorL3) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 4, 8, 1, // A x 1 4, 4, 5, // HAT_DOWN+A x 5 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 3, // NONE+L (ColorL0) x 3 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 32, 8, 3, // NONE+R (ColorR4) x 3 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 16, 4, 1, // HAT_DOWN+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 4, 8, 1, // A x 1 4, 4, 3, // HAT_DOWN+A x 3 16, 4, 1, // HAT_DOWN+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 4, 8, 1, // A x 1 4, 4, 2, // HAT_DOWN+A x 2 4, 3, 1, // HAT_DOWN_RIGHT+A x 1 16, 0, 1, // HAT_UP+L (ColorL3) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR4) x 1 4, 8, 1, // A x 1 4, 0, 6, // HAT_UP+A x 6 32, 0, 1, // HAT_UP+R (ColorR7) x 1 32, 8, 2, // NONE+R (ColorR7) x 2 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL4) x 1 16, 8, 2, // NONE+L (ColorL4) x 2 4, 8, 1, // A x 1 4, 0, 8, // HAT_UP+A x 8 16, 0, 1, // HAT_UP+L (ColorL3) x 1 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR6) x 1 32, 8, 2, // NONE+R (ColorR6) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 128, 0, 1, // HAT_UP+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 4, 0, 4, // HAT_UP+A x 4 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 4, // HAT_DOWN+A x 4 16, 4, 1, // HAT_DOWN+L (ColorL3) x 1 16, 8, 2, // NONE+L (ColorL3) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 32, 8, 3, // NONE+R (ColorR4) x 3 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 4, 4, 3, // HAT_DOWN+A x 3 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 3, // NONE+L (ColorL0) x 3 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 32, 8, 3, // NONE+R (ColorR4) x 3 4, 8, 1, // A x 1 4, 4, 2, // HAT_DOWN+A x 2 32, 4, 1, // HAT_DOWN+R (ColorR7) x 1 32, 8, 2, // NONE+R (ColorR7) x 2 4, 8, 1, // A x 1 16, 4, 1, // HAT_DOWN+L (ColorL5) x 1 16, 8, 1, // NONE+L (ColorL5) x 1 4, 8, 1, // A x 1 16, 4, 1, // HAT_DOWN+L (ColorL4) x 1 4, 8, 1, // A x 1 4, 4, 3, // HAT_DOWN+A x 3 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 3, // NONE+L (ColorL0) x 3 4, 8, 1, // A x 1 128, 4, 1, // HAT_DOWN+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 128, 4, 1, // HAT_DOWN+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR4) x 1 32, 8, 3, // NONE+R (ColorR4) x 3 4, 8, 1, // A x 1 4, 2, 1, // HAT_RIGHT+A x 1 16, 0, 1, // HAT_UP+L (ColorL3) x 1 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL1) x 1 16, 8, 1, // NONE+L (ColorL1) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR3) x 1 32, 8, 1, // NONE+R (ColorR3) x 1 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 128, 0, 1, // HAT_UP+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR4) x 1 32, 8, 3, // NONE+R (ColorR4) x 3 4, 8, 1, // A x 1 4, 0, 1, // HAT_UP+A x 1 32, 0, 1, // HAT_UP+R (ColorR5) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL4) x 1 4, 8, 1, // A x 1 4, 0, 2, // HAT_UP+A x 2 16, 0, 1, // HAT_UP+L (ColorL3) x 1 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 4, 0, 2, // HAT_UP+A x 2 16, 0, 1, // HAT_UP+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR3) x 1 32, 8, 2, // NONE+R (ColorR3) x 2 128, 8, 1, // NONE+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 4, 8, 1, // A x 1 4, 0, 1, // HAT_UP+A x 1 32, 0, 1, // HAT_UP+R (ColorR3) x 1 32, 8, 2, // NONE+R (ColorR3) x 2 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 128, 0, 1, // HAT_UP+ZR (SetMirrorT) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR6) x 1 32, 8, 2, // NONE+R (ColorR6) x 2 4, 8, 1, // A x 1 4, 0, 4, // HAT_UP+A x 4 16, 3, 1, // HAT_DOWN_RIGHT+L (ColorL0) x 1 16, 4, 5, // HAT_DOWN+L (ColorL0) x 5 128, 8, 1, // NONE+ZR (SetMirrorF) x 1 4, 8, 1, // A x 1 32, 4, 3, // HAT_DOWN+R (ColorR3) x 3 4, 4, 1, // HAT_DOWN+A x 1 0, 4, 3, // HAT_DOWN x 3 4, 4, 2, // HAT_DOWN+A x 2 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR3) x 1 32, 8, 2, // NONE+R (ColorR3) x 2 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR1) x 1 4, 8, 1, // A x 1 0, 4, 1, // HAT_DOWN x 1 4, 4, 1, // HAT_DOWN+A x 1 32, 4, 2, // HAT_DOWN+R (ColorR3) x 2 4, 4, 1, // HAT_DOWN+A x 1 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 1, // HAT_UP+A x 1 16, 0, 2, // HAT_UP+L (ColorL1) x 2 4, 8, 1, // A x 1 16, 0, 1, // HAT_UP+L (ColorL0) x 1 4, 8, 1, // A x 1 4, 0, 2, // HAT_UP+A x 2 32, 0, 1, // HAT_UP+R (ColorR1) x 1 4, 8, 1, // A x 1 4, 0, 3, // HAT_UP+A x 3 16, 0, 1, // HAT_UP+L (ColorL0) x 1 4, 8, 1, // A x 1 32, 0, 1, // HAT_UP+R (ColorR3) x 1 32, 8, 2, // NONE+R (ColorR3) x 2 4, 8, 1, // A x 1 4, 0, 4, // HAT_UP+A x 4 16, 0, 1, // HAT_UP+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 4, 8, 1, // A x 1 0, 0, 1, // HAT_UP x 1 4, 0, 2, // HAT_UP+A x 2 4, 2, 1, // HAT_RIGHT+A x 1 32, 4, 2, // HAT_DOWN+R (ColorR3) x 2 32, 8, 1, // NONE+R (ColorR3) x 1 4, 8, 1, // A x 1 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR3) x 1 32, 8, 2, // NONE+R (ColorR3) x 2 4, 8, 1, // A x 1 4, 4, 4, // HAT_DOWN+A x 4 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 16, 8, 2, // NONE+L (ColorL0) x 2 4, 8, 1, // A x 1 4, 4, 5, // HAT_DOWN+A x 5 32, 4, 1, // HAT_DOWN+R (ColorR1) x 1 4, 8, 1, // A x 1 4, 4, 1, // HAT_DOWN+A x 1 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 4, 8, 1, // A x 1 4, 4, 3, // HAT_DOWN+A x 3 32, 1, 1, // HAT_UP_RIGHT+R (ColorR1) x 1 4, 8, 1, // A x 1 4, 0, 11, // HAT_UP+A x 11 32, 0, 1, // HAT_UP+R (ColorR3) x 1 32, 8, 1, // NONE+R (ColorR3) x 1 4, 8, 1, // A x 1 4, 0, 1, // HAT_UP+A x 1 16, 0, 1, // HAT_UP+L (ColorL1) x 1 16, 8, 1, // NONE+L (ColorL1) x 1 4, 8, 1, // A x 1 4, 0, 1, // HAT_UP+A x 1 32, 0, 1, // HAT_UP+R (ColorR3) x 1 32, 8, 1, // NONE+R (ColorR3) x 1 4, 8, 1, // A x 1 4, 0, 1, // HAT_UP+A x 1 16, 0, 1, // HAT_UP+L (ColorL1) x 1 16, 8, 1, // NONE+L (ColorL1) x 1 4, 8, 1, // A x 1 4, 3, 1, // HAT_DOWN_RIGHT+A x 1 4, 4, 9, // HAT_DOWN+A x 9 16, 4, 1, // HAT_DOWN+L (ColorL0) x 1 4, 8, 1, // A x 1 32, 4, 1, // HAT_DOWN+R (ColorR1) x 1 4, 8, 1, // A x 1 4, 4, 2, // HAT_DOWN+A x 2 0, 4, 1, // HAT_DOWN x 1 4, 4, 3, // HAT_DOWN+A x 3 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 3, // HAT_UP+A x 3 0, 0, 4, // HAT_UP x 4 4, 0, 7, // HAT_UP+A x 7 32, 0, 1, // HAT_UP+R (ColorR6) x 1 32, 8, 4, // NONE+R (ColorR6) x 4 4, 8, 1, // A x 1 4, 0, 2, // HAT_UP+A x 2 0, 3, 1, // HAT_DOWN_RIGHT x 1 4, 4, 2, // HAT_DOWN+A x 2 16, 4, 2, // HAT_DOWN+L (ColorL1) x 2 16, 8, 3, // NONE+L (ColorL1) x 3 4, 8, 1, // A x 1 4, 4, 3, // HAT_DOWN+A x 3 0, 4, 5, // HAT_DOWN x 5 4, 4, 3, // HAT_DOWN+A x 3 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 3, // HAT_UP+A x 3 32, 1, 1, // HAT_UP_RIGHT+R (ColorR8) x 1 32, 0, 6, // HAT_UP+R (ColorR8) x 6 0, 0, 2, // HAT_UP x 2 4, 0, 1, // HAT_UP+A x 1 16, 4, 7, // HAT_DOWN+L (ColorL1) x 7 0, 4, 2, // HAT_DOWN x 2 4, 4, 4, // HAT_DOWN+A x 4 4, 1, 1, // HAT_UP_RIGHT+A x 1 4, 0, 2, // HAT_UP+A x 2 32, 0, 7, // HAT_UP+R (ColorR8) x 7 0, 0, 2, // HAT_UP x 2 4, 0, 2, // HAT_UP+A x 2 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 1, // HAT_DOWN+A x 1 16, 4, 7, // HAT_DOWN+L (ColorL1) x 7 0, 4, 1, // HAT_DOWN x 1 4, 4, 4, // HAT_DOWN+A x 4 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 3, // HAT_UP+A x 3 32, 0, 7, // HAT_UP+R (ColorR8) x 7 0, 0, 1, // HAT_UP x 1 4, 0, 1, // HAT_UP+A x 1 16, 3, 1, // HAT_DOWN_RIGHT+L (ColorL1) x 1 16, 4, 6, // HAT_DOWN+L (ColorL1) x 6 0, 4, 1, // HAT_DOWN x 1 4, 4, 3, // HAT_DOWN+A x 3 4, 2, 1, // HAT_RIGHT+A x 1 4, 0, 3, // HAT_UP+A x 3 4, 2, 1, // HAT_RIGHT+A x 1 4, 4, 3, // HAT_DOWN+A x 3 4, 1, 1, // HAT_UP_RIGHT+A x 1 4, 0, 2, // HAT_UP+A x 2 16, 7, 1, // HAT_UP_LEFT+L (ColorL0) x 1 0, 7, 1, // HAT_UP_LEFT x 1 0, 6, 13, // HAT_LEFT x 13 0 }; static int numsteps = 476; ```