kOS: Kerbal Operating System

Another game I enjoy playing is "Kerbal Space Program".  Similar to Space Engineers, the purpose of the game is to design, build, and fly space ships.  The key difference is that KSP relies on realistic physics and requires a basic grasp of orbital mechanics to succeed and has no vanilla mechanism for automating the spaceships using code.  There is, however, a mod that enables this functionality called "kOS" or "Kerbal Operating System".  The mod introduces a few new parts that allow players to write scripts in a unique kOS language to control various aspects of the attached ship.

One of the existing features in the KSP campaign mode is the requirement to unlock new parts by performing science experiments in different settings.  Limited science points are available around the starting space center with more points available from advanced experiments in orbit of or on the surface of different planets and moons.  The tedious grind comes when you fly a craft jam packed with experiments and experiment storage parts to a distant setting and then need to manage the interactions with all of them to exhaustively collect the science data and store it for transmission or return to the home planet.

While the kOS mod allows player to automate many of the functions of their space craft, when I first installed it there was no mechanism for "collecting" the science data from experiments into the storage containers.  I found this limiting so I cloned the source repository for the mod, spent some time learning the code base, and created a pull request to implement this functionality.  I was satisfied to learn that the pull request was eventually merged into the main codebase of the mod.

"JRRagon" Chess

First off, "JRR" are my daughter's initials and her nickname has been "the Dragon" since before she had a name, hence "JRRagon", and I built this after she first started getting into Chess.

The video is a quick demo of some of the functionality that had been completed as of the time of recording the video.  The first game is played automatically using the rudimentary "AI" that just chose legal moves at random.  "PERFT" is not quite an acronym used in Chess AI programming to mean "performance test" and "PERFT(1)" is looking ahead by one move, so when the window shows "PERFT(1)", it's showing all the legal moves the active player could make from that board position.  Since the AI is still in (very) early development here, the green highlight is the "best" move, which is evaluated based on nothing and is actually selected entirely at random.  The game is pretty long (151 moves) because it's hard for either side to gain a real advantage when it has no strategy at all.

The second game allows White to win by "Fool's Mate".  Black can win by "Fool's Mate" in only two moves while it takes White three, but for no particular reason I played the slightly longer game.

Several other features are hinted at without being demoed on the pause screen, including loading and saving; setting a custom board position using "FEN", a condensed means of storing a game state including board positions, active player, castling rights, etc; and setting up a network game.  This video was made prior to the establishment of the Node "Lobby" server, so network games at this point were constrained to clients running on directly accessible hosts.



The Law of Cosines

One of the fancy things I wanted to do in Space Engineers was build my own "autopilot" functionality.  There's an existing one based on the "Remote Control" block but it doesn't offer the kind of fine-tuning I wanted.  Specifically it uses too much fuel by getting up to speed and then keeping both the forward and backward thrusters active instead of treating space like space and getting up to speed and then coasting until it's time to slow down.

In this video I review the code I came up with for orienting the spacecraft towards the waypoint using the "Law of Cosines".  Most of us learned (or at least were taught) the Pythagorean Theorem in school, and so basically understand that when you have a right-angle triangle, the shorter sides a2 + b2 = c2, the longer side.  What most of us probably didn't learn is that there's a more generic version of this for other triangles that are NOT at right angles.  This means that by plugging in the X and Z coordinates of our current location relative to a waypoint, we can use this generic "Law of Cosines" to solve for the angle between our current heading and the waypoint and then turn the spacecraft (using the gyroscope blocks) until that angle reaches 0°.

All of that resulted in this C# script interfacing with the Space Engineers Scripting API to rotate a spacecraft to orient towards a waypoint, a critical element in any autopilot functionality.