Creating a Simple Bot

or Building a Bot in Bash

I was taking a break from my normal weekend routine and decided to start playing Oolite. I loved playing its predecessor, Elite, on my first computer, a C-64. It’s come a long way since then. If you haven’t tried it, I highly recommend it.

Anyway, I was working the merchant grind and it was starting to get boring. What better way to spice it up than with some coding?

So, let’s create the simplest bot we can to reduce the grind…

After some research, I decided that I didn’t want to write an OXP (Oolite eXpansion Package) – and even if I did, it doesn’t look like there’s an easy way to manipulate the player (probably to prevent botting). Ah, a challenge! How can we control the game from outside of the program? Enter xdotool.

Already read this installment? Here are all the articles in this series:
Part 1 – Establish control of Oolite
Part 2 – Automating a trading route
Part 3 – Reading Oolite’s state
Part 4 – Adding polish

My Setup

I’m running Ubuntu 14.04 64-bit. If you’re running other versions of Ubuntu or Debian, you should be able to follow along. I’ll be implementing this using a Bash script.

xdotool

xdotool is an X-Windows command line program for controlling GUI based applications in ‘nix. Let’s see what we can do with it.

Install it from the command line with

The xdotool man page provides a lot of info

Oolite

You can install Oolite by following instructions at oolite.org. I’d recommend playing it for awhile to get the hang of it.

Two days later…

Map Out What We Need To Automate

In the game, as a merchant, we buy furs at an agricultural world, sell them at an industrial world, buy computers, and return to the ag world to sell them and repeat. If we walk through this process manually, and note the exact keystrokes/steps we get something like this:

  • Refuel our ship:
    • 3 to go to the Ship Outfitting page
    • Enter/Return to select the fuel option (it’s the first option). Note that it’s not available if the ship’s fuel has been topped off.
  • Buy furs (assuming we’re starting on an ag world):
    • 8 to go to the market page
    • go down 11 times to Furs
    • and hit enter to buy the max amount of furs
  • Mark the destination planet
    • go to the system chart: 6
    • click the mouse on the destination system
  • Save our progress
    • go to the save screen: 2
    • the default option is Quick Save so hit enter
  • Launch from the space station
    • F1
  • Maneuver away from the station
    • down for a couple of seconds
    • use our injectors for a few more seconds: i
  • Hyperspace
    • h
  • After coming out of hyper, we want to get off of the main path to avoid pirates and such
    • down for a couple of seconds
    • use the injectors for awhile to make sure there’s no other ships causing a mass-lock: i
    • use the jump drive for 10-15 more seconds: j
  • Head to the destination planet
    • up for a few seconds
  • When we’re in range of the station, dock
    • ‘Shift-c’
  • Sell our cargo
    • Market page: 8
    • Select Furs: down * 11
    • Enter to sell
  • Repeat from the top except we’ll buy computers and select the original system this time

Phew! Quite a list.

Will xdotool Work?

With no xdotool experience, we need to see if it can work for us.

Open up a terminal window and let’s see what xdotool offers.

Looks like there might be some commands we can use… based on our list of actions, windowactivate, getmouselocation, key, keydown, keyup, mousemove all look useful.

Start up Oolite so we have something to play with and create a new player if you haven’t played the game yet.

Testing Control of the Game

Our first step is refueling the ship, but we can only refuel once (can’t refuel a full tank) so let’s pick a different step to play with.

Buying and Selling Furs looks simple and we can repeat it over and over again if we like so we’ll go with that.

Create a file to contain our script and make it executable:

Using your favorite editor (mine’s vim) edit the script to look as follows (the title of my version of Oolite says Oolite v1.80)

Running this on my machine results only in the window being brought into focus. It doesn’t change to the Market screen as expected.

Doing some more research (reading man xdotool), I notice a --delay option for the key command. Looks like the default is 12ms.
Let’s adjust this and see if it makes a difference.

Ah, now we’re in business!

Select the furs:

When I run the script this time, it selects Firearms and purchases them. We wanted Furs. Also, I have to keep manually hitting 5 before I run the script.

We’ll add the ‘initialization’ to go to the status page up front, and extend the delay for the Down keys. The script should now look like this:

Now the script consistently buys and/or sells Furs.

Review

We’ve determined (so far) that xdotool and bash seem to provide enough access to control Oolite programatically. We’ve created a script that allows us to find the Oolite window, go to the Market screen, and buy or sell Furs.

We still have many steps to automate and I can already see potential for some refactoring. In the next article we’ll work on some more steps and refactor what we have currently.

What would you do to refactor this script? Have you implemented a bot for Oolite in some other fashion? Tell us in the comments.

One thought on “Creating a Simple Bot

Leave a Reply

Your email address will not be published. Required fields are marked *