AI Controlled Ships

Pretty early on I realized that Space Frigates was pretty boring and hard to test with only one ship flying around. So I decided to create ships that were controlled by the computer.

I didn’t really have any idea how to do that but I was able to achieve the current results which I think are okay for now. At least when people pop by to check out the game there are other ships to blow up!

Abstracted from the Game Engine

The first thing I wanted to ensure was that the code the ran the game engine and the code that ran the AI was completely separate. My reasoning was:

  1. I wanted the flexibility to create different AIs at a later date
  2. I wanted the flexibility to scale the number of computer ships separately from the main game engine
  3. Some day, I think it would be cool for players to be able to script part or all of their ships functionality
  4. I knew that I would be running the game engine on both the client and the server and didn’t want the computer trying to control the same ships on both the client and the server

So all the code for the AI is contained in different classes. Then, during the game loop on the server, for each computer controlled ship I call a method that decides what that ship’s next command will be.

(Actually, what I do is only call that AI method a certain small percentage of the time. Otherwise the computer controlled ships would issue commands at about 60 times a second. Very hectic!)

The AI method emits a command event for each command it picks. These events are collected exactly as if they had come from a human player.

AI Logic

So that gave me a setup for ships controlled by the computer. But how does the computer actually decide what the ships are going to do? I have to confess, the commands are completely random. I just have a set of else if  statements and then randomly determine which decision path to take.

I did that as a starting point but found that it was actually pretty acceptable. The computer controlled ships are really just there for filler anyway and the real focus is on fighting other human ships when people play the game.

Eventually I intend to create smarter AI packages that track enemies, fight in formation, run away, etc. I think for me, this was one of those things were good enough was better than great because AI is a whole topic onto itself that would have stalled the whole project. But I look forward to doing a deep dive some day!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s