AI for emergent gameplay
I thought I'd write a piece about AI and how I'm coding it up with relevance to emergent game-play.
You may not want to read on if you prefer to think its all done by magic, but if you want to take a glimpse behind the curtain read on.
I like my ai's to react meaningfully to stimulus, which basically means giving them the ability to see and hear.
That may sound obvious, but a lot of ai's in games don't react to anything, they are simply pre-placed in the world, told to go to sleep and are woken up when the player enters a specific trigger box, perhaps halfway down a corridor they are in. So they wake up and start executing their pre given command shoot player.
You can get away with this in linear games and sometimes its actually the best way if you want to have total control of your game, everything tightly scripted, almost like the ai's are following a movie script. Zombie dog jump through window 3 seconds after player steps into this invisible box. It can make games atmospheric, but the downside is its totally the same every time you play through, you die and restart the level, same guy triggers, same dog jumps through window, Yawn!
I much prefer games that take a behavior driven approach, you basically code up multiple behaviors make them react to stimulus and give them a bit of decision making capability.
So lets take a standard behavior, Guard. You can imagine this as a typical security guard, night after night given the duty to guard an area. Lets give him an area he is going to guard some kind of compound, nobody should be entering this secure area.
The guard has 2 main senses we need to worry about sight and sound, sight has a field of view (FOV), say 150 degrees and a range, the range might be a km, but he also might not care about what anyone is doing 100 meters outside the volume he is guarding. Hearing has no FOV but has a range much shorter than sight lets say 100 meters.Sound travels slow so would take about 1/3rd of a second to travel 100 meters. Then we factor in reaction times too.
Events, events are the means by which we communicate things happening that ai's need to know about.
Fire a gun, broadcast that event to everyone within 500 meters say, here's the key though, the sound of the event goes into a queue for everyone within range. They will receive the event when the sound has correctly reached them, the visual of the event will reach them instantly, but if its outside their FOV or something is blocking their line of sight (LOS) they won't react to the visual cue. So really 2 events are created and are propagated at different rates, Gunshot audio event and visual event. Audio Events also are less precise they may only provide the listener with a possible volume for the source of the sound and the volume will be bigger the further away the sound is, and again if there's no LOS maybe the sound had to bounce off a few surfaces to reach the listener so its even less accurate where the source is.
How people react to events is decided by their current behavior as well as various stats they may have.So back to our guard, a gun shot outside his guarded area is probably going to raise his attention if its close enough, what's some of his basic responses we may have actions for.
- Go investigate
- Take Cover
- Sound an Alarm
- Run Away and hide
- Simply turn and look in that direction and wait
- Ignore it
With a bit of fuzzy logic we can decide which of these options the guard is going to take, we might factor in his boredom levels/ tiredness/ bravery perhaps its a violent area where gun shots are frequently heard outside his compound.
To decide which action we take, we look at a set of essential properties that the AI is looking to maximize such as Financial, Safety, Food, Sex, Excitement, Sleep, Respect.
These properties generally decay over time, people get hungrier,sleepier,hornier..Doing something heroic might increase your respect levels and excitement levels.Running away might decrease respect a little but increase your safety but decrease finance (you are not paid to run away).
Now each of the 6 actions above can have a +/- value assigned to each of the 7 essential properties for each of the type of events that might happen.
Investigating a gunshot might increase heroic and excitement and decrease safety a little.
The amount these essential properties changes can be scaled based on distance of the event, and also base stats such as how brave the ai is, or intelligence, or monetary greed etc.
So now this Guard can compare the scores for all his choices choose the highest scoring option, compare it perhaps against his current activity and decide what to do.
For extra dynamics the essential properties feed into each other, too much excitement can be detrimental to safety, sleep and hunger.
Too much food can make him sleepy. We can factor sleepiness into reactions
Everything feeds into everything else causing a chaotic system, but also a believable system, a systems where the Guard will go get some food because his hunger level got too high, or he fell asleep on the job because he got to sleepy, or he decided to run away from a shoot out because he’s basically a bit of a coward and the financial gains of defending the compound didn't out-way the risk of being killed.
These stats can also factor into taking a bribe, or being distracted by an attractive member of the opposite sex, or what the guard is going to do when his work shift finishes.
Of course the event might not be a gunshot, it might be a car crashing through a gate, or having a gun aimed at you, or being offered a bribe, or an explosion, or a bullet ricochet, or a near miss from a laser beam, or a muffled grunt or a soft foot fall behind you, the lights cutting out, a siren blaring, discovering a dead body...
The bottom line is by giving AI’s behaviors, and a behavior being set up with lots of possible reactions to lots of different events, and factoring in base genetics of each AI with their current state and needs, we can make the world react to the player in a believable way, and can remove the scripted feel to the game as the AI reacts different depending on how you break into the compound to steal the computer equipment, by stealth, by explosives, by sheer gun power, perhaps you just offer the guard enough money, perhaps one of your female agents seduces him.