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
|
1 2 3 |
sudo apt-get install xdotool |
The xdotool man page provides a lot of info
|
1 2 3 |
man xdotool |
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:
3to go to the Ship Outfitting pageEnter/Returnto 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):
8to go to the market page- go
down11 times to Furs - and hit
enterto buy the max amount of furs
- Mark the destination planet
- go to the system chart:
6 - click the mouse on the destination system
- go to the system chart:
- Save our progress
- go to the save screen:
2 - the default option is Quick Save so hit
enter
- go to the save screen:
- Launch from the space station
F1
- Maneuver away from the station
downfor 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
downfor 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
upfor a few seconds
- When we’re in range of the station, dock
- ‘Shift-c’
- Sell our cargo
- Market page:
8 - Select Furs:
down * 11 Enterto sell
- Market page:
- 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.
|
1 2 3 |
xdotool --help |
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:
|
1 2 3 4 |
touch bot chmod +x ./bot |
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)
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/env bash # Find the window and store its ID WID=$(xdotool search --name "Oolite v1.80" | head -1) # Activate the window (focus) xdotool windowactivate --sync $WID # Go to the Market screen xdotool key 8 |
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.
|
1 2 3 4 |
# Go to the Market screen xdotool key --delay 50 8 |
Ah, now we’re in business!
Select the furs:
|
1 2 3 4 5 |
# Select Furs xdotool key --delay 50 Down Down Down Down Down Down Down Down Down Down Down xdotool key --delay 50 Return |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/usr/bin/env bash # Find the window WID=$(xdotool search --name "Oolite v1.80" | head -1) # Activate the window (focus) xdotool windowactivate --sync $WID # Always start from the Status screen xdotool key --delay 50 5 # Go to the Market screen xdotool key --delay 50 8 # Select furs xdotool key --delay 80 Down Down Down Down Down Down Down Down Down Down Down xdotool key --delay 80 Return |
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.
Hello there, You have done a great job. I’ll certainly digg it
and personally recommend to my friends. I am sure they will be benefited from this website.