Show HN: Andromeda Invaders Autopilot Demo

susam | 2 points

The autopilot algorithm is about a 100 lines of code contained in a single function here:

https://github.com/susam/invaders/blob/0.9.0/invaders.html#L...

Although the code looks long, the essence of the algorithm is pretty simple. At first, it scans the neighbourhood of the player to find all the falling boulders that are too close to it (determined by SCAN_WIDTH). These boulders are the threats to the player.

If the threat is coming directly from then top, then it decides to move either left or right to avoid the falling boulder. To do so, it first determines if it can safely move in either direction without being hit by other boulders in the neighbourhood. Based on the result, it moves either left or right.

If the player is blocked on both sides, i.e., it can neither move left nor right without being hit by a falling boulder, as a last resort, it positions itself to directly aim at the boulders.

Finally, if there is no immediate threat, then the autopilot seizes the opportunity to aim directly at the invading ships.

susam | 14 days ago

Nice work!

jprd | 14 days ago