## Implementing State Machines

The two states for the imp's brain form a very simple state machine. A state generally defines two things:

• What the NPC is doing at that moment

• At what point it should switch to another state

The condition to get from the exploring state to the seeking state is self.can_see(player)—in other words, "Can I (the imp) see the player?" The opposite condition (not self.can_see(player)) is used to get back from seeking to exploring. Figure 7-1 is a diagram of the imp's state machine, which is effectively its brain. The arrows define the links between the states and the conditions that must be satisfied to switch states. Links in a state machine are always one-way, but there may be another link that returns to the original state. There may also be several intermediate states before returning to the original state, depending on the complexity of the NPC's behavior.

In addition to the current behavior and conditions, states may also contain entry actions and exit actions. An entry action is something that is done prior to entering a new state, and is typically used to perform one-time actions needed by the state to run. For the seeking state in the imp's state machine, an entry action might calculate a heading toward the player and play a noise to indicate that it has seen the player—or anything else required to ready the imp for battle. Exit actions, the opposite of entry actions, are performed when leaving a state.

Let's create a slightly more interesting state machine so we can put this into practice. We are going to create a simulation of an ant's nest. Insects are often used when experimenting with AI because they have quite simple behaviors that are easy to model. In our simulation universe, we are going to have three entities: leaves, spiders, and the ants themselves. The leaves will grow in random spots on the screen and will be harvested by the ants and returned to the nest. Spiders wander over the screen, and are tolerated by the ants as long as they don't come near the nest. If a spider enters the nest, it will be chased and bitten until it either dies or manages to get far enough away.

■Note Even though we are using an insect theme for this simulation, the AI code we will be writing is applicable to many scenarios. If we were to replace the ants, spiders, and leaves with giant "mech" robots, tanks, and fuel drops, then the simulation would still make sense.