My Screeps AI

June 20, 2015

Last week I was presented with a new game, Screeps. The game is fairly simple to understand, it all boils down to harvesting energy, harvest as much as possible, use the energy to build units which in turn can be used to harvest even more energy. With the energy you can upgrade your units with more capabilities (for example attack, which you can use to conquer other people’s bases). So far you might think this sounds like any other RTS game, which it basically is, but the only difference here is that you need to control your units with nothing but code.

I started out building on the code that you receive from the tutorial, but it didn’t take all too long before I noticed that it wouldn’t be scaleable for my intentions. I kept going with the code for quite some time, mostly to get used to the API — which in all honesty is sometimes a bit non-consistent, but to be fair, it’s still just an early preview.

After I felt I had an idea on how to rebuild the AI, I basically put down around 8 hours just coding blindly, i.e. not running my code at all, just writing it. This is one of the things that I’d like to change in screeps, that you can have multiple codebases running at the same time. They do have a simulation room, but it’s only got one room, and it seems like you need to run the same code in the simulation that you’re using in the actual game, which means it’ll be hard to test out concepts (I could be wrong on this matter, but I haven’t yet gotten the simulation room to run without throwing a lot of errors).

Anyway, once I was fairly certain that my new version would be able to run, I pushed it to production. As soon as it was out, I noticed a huge performance boost from my earlier code, I then decided to try out the expandability of the AI, so I took over my adjacent room. Everything seemed like it was working correctly, which it did, but my neighbouring AI wasn’t as happy about me expanding, and seeing as he was tenfold stronger than me, he killed me within 10 minutes. But one of my goals were met, I could max out resource mining in a room with two resources within 300 seconds (the regeneration time of a energy source), which means that the code is pretty efficiently harvesting for max output.

The good part about restarting was that I got to test out the AI’s startup functionality, i.e. working from nothing to a functioning base, and to my surprise, small bugs to the side, it worked really well.

Screeps AI 2.4 flowchart
The basic layout for the code, not super up-to-date, but the basic architecture is still in place.

As earlier noted, my first version wasn’t really scalable, it was coded with only one room in mind, i.e. all of the decisions were made on a high level. This was the first thing I wanted to tackle with version two, treating each room as independently as possible. So for each room I control, I initialize a Room object, which in turn initializes it’s handlers for resources, deposits, constructions and population. These are then injected in to the CreepFactory, which both initializes existing creeps, loading in the appropriate class for their types, and handles the creation of new creeps.

The population class tracks how many creeps that exist in the room, and what kind of ratio that is needed for the room to survive (you can’t run a base with only soldiers or only miners, you need basically one of every type for the room to flourish). When there’s a sub-optimal distribution, the Room object will instruct the CreepFactory to create more of a certain type to even out the population.

Since conquering new rooms is a big part of the game, I made a RoomHandler, which acts as the broker for different rooms, a room can for instance, ask all other rooms for reinforcements, this will be sent through the RoomHandler, which in turn forwards the request to each room. If a room has enough resources to help out, it’ll send one creep of every type to the distressed room.

The new version is hardly complete, there’s still a lot to be worked out, for example, at this time the AI doesn’t build anything by itself, you need to mark out where you want the buildings to go, and the AI will start working on it. I also haven’t completed the expansion part, where a room starts sending troops to an adjacent room for taking over. I also haven’t really worked a lot on the defensive/offensive parts, meaning, my soldier creeps will just start attacking anything hostile, without any tactics.

It wouldn’t be fun if everything was easy, and if you could create a perfect AI in a couple of days, so I’m glad there’s still a lot for me to do on my AI, and lots of improvements to the functionality that I’ve already implemented. Anyway, if you fancy having a go at the game, let me know, I have a couple of invites that I can share. And if you want to try my AI out for a spin, check out my github repository, master-branch should, at least in theory, always contain stable code.

Update 22th January, 2017: This is a very old screeps AI build and will probably NOT work with the current screeps version

Tags