Search This Blog

Wednesday, July 16, 2014

Fighter AI

A word on game A.I.  Broadly, it means anything that governs the behavior of elements in your game.

My first try at a space game I had to make "AI" for the ships.   Here's what I did.

First make the ships move like you do.  That is to say: make them have the same rules you have:  rotation, acceleration, drift, etc.   This goes a long way toward making any ai more believable.

Early enemies
 The AI for my ships is based on this philosophy:

This might seem obvious, but it’s something to remember in general.  Half the battle of convincing AI enemies is to make sure the enemies appear to play by the same rules as the player.  A lot of what they do will then be determined by general rules and how they act will be more realistic.  (this is a rule that can be broken --but it’s a good place to start).

When possible, make them control like the player  (ie if the player has physics, gravity or “spaceship movement,” then so should the most player-like enemies), and make them subject to the same control limitations.

 Don’t let them get caught “cheating” --for example by always knowing where the player is --when they couldn’t, or turning on a dime when that would look unnatural with no explanation.

Make them subject to damage by the same things that would damage the player.   Nothing groin-kicks my suspension of disbelief like when enemies weaker than the player walk harmlessly over an electrified or lava floor or get hit by asteroids with total impunity.

“Friendly fire” among enemies is sometimes great (see Doom, Quake, Berzerk, etc) , but in this case none of them would survive  --so I’m willing to give them “friendly fire shields” or assume that they’re shooting “under or over” each other for the sake of gameplay.

Now that they control the way you do, I started with just 4 simple rules:

1.  If they were too far away from you --say beyond 800 units (or something) then they turn toward you.
2.  If they were too close to you -- say within 200 hundred or so, they turn away from you.
3.  When they’re in bullet range and pointing at you, they shoot.
4.  They thrust all the time.

That’s it.

When you have many instances of the same enemies following the exact same rules, they tend to bunch up and try to occupy the same spot.  To fix this, I added a little randomness to when they turn toward and when they turn away and gave them slight differences in speed.   These variables are set when the enemy is created and are unique to each instance of that enemy --so no more bunching.

Just with this mind-numbingly simple method in place, I let my friend test the game with several of these dumb 4-rule ships.  He came back with a surprising amount of “strategy.”   He talked about “tricking them” and “getting them riled up” as though they had much more sophisticated brains than they had.

He saw intelligence in these simple brained objects the same way a player sees a “tree” from a few simple blocks in Minecraft, or an “alien” from an 8 byte blocky character from Space Invaders.

The intelligence was projected from his imagination.

They are much more sophisticated now.  Back then they had only one "state" which was following those 4 rules, and another state which was "dying" in which they spun and blew up.   This is what is meant by "finite state machines" and describes the AI of most simple game enemies.  They have a few more states now and pick their targets based another simple set of rules.

Bottom line is they don't have to be smart at all, they only have to sort of "seem" like they are.





No comments:

Post a Comment