AI and Steering Behaviors

The game we have been writing is loosely based on the computer game Spacewar! If you are not familiar with Spacewar!, it was the first computer game ever written. It originally ran on a PDP-1 owned by MIT and was written by an MIT student named Steve Russel, in 1962. Back then, just getting a computer to display graphical output was difficult enough. Spacewar!, as well as many other early game systems such as Pong, were designed to be played by more than one person. That was because programming a computer to behave like a human was a very difficult thing. That is still somewhat true today, although more processing power and data allows modern Artificial Intelligence (AI) algorithms to behave much more intelligently than they have in the past.

Because our game is a single-player web game, we do not have the benefit of using a second human intelligence to power our enemy spaceship. Before this chapter, we used an AI stub to allow our enemy spaceship to move and shoot randomly around our gameplay area. That might have worked for us up to this point, but now we want our player to feel threatened by the enemy ship. It should be intelligent enough to fight and kill our player in one-on-one combat.

You will need to include several images in your build to make this project work. Make sure you include the /Chapter10/sprites/ folder from the project's GitHub. If you haven't yet downloaded the GitHub project, you can get it online at: https://github.com/PacktPublishing/Hands-On-Game-Development-with-WebAssembly.

In this chapter, we will be doing the following:

  • Introducing the concept of AI and Game AI
  • Adding obstacles to the game for avoidance AI (and increasing the canvas size)
  • Adding new collision detection for a line of sight
  • Introducing the concept of a Finite State Machine (FSM)
  • Introducing the concept of autonomous agents
  • Introducing the concept of steering behaviors
  • Adding force fields to our game
  • Using FSMs and steering behaviors to create an AI
  • Tuning our AI to allow the enemy spaceship to navigate obstacles