Tactical AI Kit: Reacting to Threats
by Bryce · 10/15/2008 (9:25 pm) · 23 comments
Hello Community!
I have finally accomplished something that was much needed for the Tactical AI Kit--reacting to threats. So far, it's pretty basic, but I'm quite glad that I implemented in two hours what many games today don't offer with AI.
After a while, I grew tired of having my AI Players getting stupidly killed because they weren't facing the right way and couldn't figure out that he was being shot at. My original idea was to make the AI Players cheat; when they come under fire, they know where the threat came from and will immediately react.
Problem: I HATE cheating AI. Nothing bothers me more that being unable to have fun sniping in a game because you instantly come under fire from enemies who shouldn't know you exist. I was playing co-op Chaos Theory at a friend's house not too long ago, and was sniping an enemy from across a system of docks who was blazing away at my friend. I hit him in the leg from 100 meters or so, with a silenced weapon, and I suddenly came under fire. There is no way he heard that shot across the docks while his assault rifle was going off. That made me mad, not just because I was promptly killed, but because a game that relatively new needed to resort to cheating AI.
So, I looked for a solution that would allow AI Players to move intelligently (while also looking like they are panicking) when they come under fire while not engaging an enemy. At that same friend's house, I was playing the first Ghost Recon game and noticed something interesting. At the bottom of the screen, there is a little compass-thingy that looks like this:

Whenever the player comes under fire and is close enough to hear the shot, the section that points closest to the location of the gunshot turns red. This gave me an idea. Every 25 milliseconds, the AI Player performs a quick radius search and sees if there are any enemy players firing their weapon (okay, okay, some cheating is employed here to get the firer's team, but without it, they would mistake friendly players firing as possible enemies, which looks pretty bad). This is not as performance-costly as it sounds. Now we check the distance of the shot and the datablock of the weapon that was fired. In the weapon datablock, a field needs to be added that specifies how close an AI Player has to be to hear it firing (rifles could have a sound radius of around 400, lower distances around 40 or so could be used for silenced weapons, etc). If a gunshot is heard, the location of the shot is altered from 0 to 90 degrees based on the distance of the shot, so that there is some guess error.
If at any time we notice a projectile sail by us or a bullet hit somewhere close to us, we enter the REACT state. The NPC checks if we have heard any gunshots, and if we have, we move to cover from that position while watching it (he has a look around behind him every once in a while, too), while occasionaly wandering back into the open cautiously to find the shooter. If the NPC hasn't heard any enemy gunfire, he basically runs around while constantly searching in every direction (panic). So far, this is working extremely well. There aren't many things more fun than watching these little guys scramble like ants and run for cover through a rifle scope.
Rate and comment!
I have finally accomplished something that was much needed for the Tactical AI Kit--reacting to threats. So far, it's pretty basic, but I'm quite glad that I implemented in two hours what many games today don't offer with AI.
After a while, I grew tired of having my AI Players getting stupidly killed because they weren't facing the right way and couldn't figure out that he was being shot at. My original idea was to make the AI Players cheat; when they come under fire, they know where the threat came from and will immediately react.
Problem: I HATE cheating AI. Nothing bothers me more that being unable to have fun sniping in a game because you instantly come under fire from enemies who shouldn't know you exist. I was playing co-op Chaos Theory at a friend's house not too long ago, and was sniping an enemy from across a system of docks who was blazing away at my friend. I hit him in the leg from 100 meters or so, with a silenced weapon, and I suddenly came under fire. There is no way he heard that shot across the docks while his assault rifle was going off. That made me mad, not just because I was promptly killed, but because a game that relatively new needed to resort to cheating AI.
So, I looked for a solution that would allow AI Players to move intelligently (while also looking like they are panicking) when they come under fire while not engaging an enemy. At that same friend's house, I was playing the first Ghost Recon game and noticed something interesting. At the bottom of the screen, there is a little compass-thingy that looks like this:

Whenever the player comes under fire and is close enough to hear the shot, the section that points closest to the location of the gunshot turns red. This gave me an idea. Every 25 milliseconds, the AI Player performs a quick radius search and sees if there are any enemy players firing their weapon (okay, okay, some cheating is employed here to get the firer's team, but without it, they would mistake friendly players firing as possible enemies, which looks pretty bad). This is not as performance-costly as it sounds. Now we check the distance of the shot and the datablock of the weapon that was fired. In the weapon datablock, a field needs to be added that specifies how close an AI Player has to be to hear it firing (rifles could have a sound radius of around 400, lower distances around 40 or so could be used for silenced weapons, etc). If a gunshot is heard, the location of the shot is altered from 0 to 90 degrees based on the distance of the shot, so that there is some guess error.
If at any time we notice a projectile sail by us or a bullet hit somewhere close to us, we enter the REACT state. The NPC checks if we have heard any gunshots, and if we have, we move to cover from that position while watching it (he has a look around behind him every once in a while, too), while occasionaly wandering back into the open cautiously to find the shooter. If the NPC hasn't heard any enemy gunfire, he basically runs around while constantly searching in every direction (panic). So far, this is working extremely well. There aren't many things more fun than watching these little guys scramble like ants and run for cover through a rifle scope.
Rate and comment!
#2
10/15/2008 (10:32 pm)
I suppose performing a firing sound detection everytime a trigger is pulled would be more costly on the CPU side?
#3
Really nice progress youre having with this bryce, keep it up!
10/16/2008 (12:42 am)
As most 'gangs' most likely is to use different weapons (different sounds to the shots) this system of 'slight cheating' dont seem a morale problem to me ;)Really nice progress youre having with this bryce, keep it up!
#4
10/16/2008 (12:54 am)
Given the frequency of firing changes vs having to scan the radius and the costs of each, I'd recommend going the other way and alerting anyone in radius when a player fires thier weapon, rather than constantly polling (unless the polling is doing something else as well).
#5
Sound is a particularly tricky thing to get right, I think. I mean, you're modelling bullet trajectories already, so it's easy to detect when you're being fired at. But it's difficult to actually detect when somebody is firing, because sounds aren't processed in the same way.
I'd tend to agree with Phil that it seems smarter to have the weapon cause a sound event, rather than for players to go looking for sound events, but I haven't tried either method. I guess both have their merits (like Phil said, your 'go looking' scan can do other things).
10/16/2008 (7:41 am)
You're definitely filling a noticable gap here, so props to you. It's great to see the AI becoming even better; really, it does astound you when you look at the cop-outs some games use, when there just isn't an excuse (like the bit you describe in Chaos Theory).Sound is a particularly tricky thing to get right, I think. I mean, you're modelling bullet trajectories already, so it's easy to detect when you're being fired at. But it's difficult to actually detect when somebody is firing, because sounds aren't processed in the same way.
I'd tend to agree with Phil that it seems smarter to have the weapon cause a sound event, rather than for players to go looking for sound events, but I haven't tried either method. I guess both have their merits (like Phil said, your 'go looking' scan can do other things).
#6
Whilst my brain is currently in modelling-mode, I had been giving a little thought to this concept. I was wondering if it'd be cheaper (CPU-wise) to have all AI and players given a flag/value based on stealth, range, angle and weapon. So in the end you'd have a formula for integrating into an AI's standard scan for enemies.
Say not engaging the enemy gives you a stealth rating of 1, and that halves the distance of a 90 degree scan by the opposition, and quarters it for the side, and makes it real small for the rear quadrant, with a certain small radius that the AI will always pick you up on (hearing etc, like Killer Kork).
And you can give the AI a rating for how switched on they are, so you could stalk them if they are unaware of a hostile presence, and once they notice a threat they go into action mode.
If you engage (press that big shooty shooty button - and you've got a loud weapon, not silenced) you lose that stealth rating and then the AI gets full scanning capability on you. Full from the front, half from the sides, quarter from the rear.
You could also throw in some sort of cover value, the KK resource does multiple LOS checks against parts of the target to see how much is in view when determining whether to engage or not. Personally I think that might be a bit CPU heavy, but you could have triggers situated around certain objects (like bushes) which give an improved stealth rating even when fired from.
People tend to catagorize threats based on distance, what's closest is what preoccupies them. So when getting sniped at by someone from half a mile away, they're still going to be more concerned about the opposition they can see much closer.
I think AI programming is a bit of an amusing paradox because it's obviously best to keep it streamlined, but trying to mimick human behaviour just makes it all the more complicated!
That was probably all a bit over the place as I haven't got my programming head on, but you the get my gist. Returns to modelling....
Edit: Also if your looking for a sound event thing, there's a resource of subscribing to events like in Unreal which you might want to have a look at.www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13632
I also think there might be a more up to date version - though that might be for TGEA.
10/16/2008 (7:54 am)
I'd agree with Phil on this one Bryce, all these scans do sound costly to the CPU, especially when you scale it up from a few AI.Whilst my brain is currently in modelling-mode, I had been giving a little thought to this concept. I was wondering if it'd be cheaper (CPU-wise) to have all AI and players given a flag/value based on stealth, range, angle and weapon. So in the end you'd have a formula for integrating into an AI's standard scan for enemies.
Say not engaging the enemy gives you a stealth rating of 1, and that halves the distance of a 90 degree scan by the opposition, and quarters it for the side, and makes it real small for the rear quadrant, with a certain small radius that the AI will always pick you up on (hearing etc, like Killer Kork).
And you can give the AI a rating for how switched on they are, so you could stalk them if they are unaware of a hostile presence, and once they notice a threat they go into action mode.
If you engage (press that big shooty shooty button - and you've got a loud weapon, not silenced) you lose that stealth rating and then the AI gets full scanning capability on you. Full from the front, half from the sides, quarter from the rear.
You could also throw in some sort of cover value, the KK resource does multiple LOS checks against parts of the target to see how much is in view when determining whether to engage or not. Personally I think that might be a bit CPU heavy, but you could have triggers situated around certain objects (like bushes) which give an improved stealth rating even when fired from.
People tend to catagorize threats based on distance, what's closest is what preoccupies them. So when getting sniped at by someone from half a mile away, they're still going to be more concerned about the opposition they can see much closer.
I think AI programming is a bit of an amusing paradox because it's obviously best to keep it streamlined, but trying to mimick human behaviour just makes it all the more complicated!
That was probably all a bit over the place as I haven't got my programming head on, but you the get my gist. Returns to modelling....
Edit: Also if your looking for a sound event thing, there's a resource of subscribing to events like in Unreal which you might want to have a look at.www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13632
I also think there might be a more up to date version - though that might be for TGEA.
#7
This is excellent stuff Bryce, keep it up.
10/16/2008 (7:55 am)
Quote:Given the frequency of firing changes vs having to scan the radius and the costs of each, I'd recommend going the other way and alerting anyone in radius when a player fires thier weapon, rather than constantly polling (unless the polling is doing something else as well).I was thinking exactly the same thing. Give every weapon some sort of "sound radius" value and every time the weapon is fired you do a radius search in the given sound radius and then notify any potential listeners in range. From there the AI code can take over a decide what to do about it. If the AI gets damaged but doesn't "hear" the shot, they should still react in some way, but perhaps not the same as if they "heard" the shot.
This is excellent stuff Bryce, keep it up.
#8
10/16/2008 (10:01 am)
Okay, I made the alert come from the weapon firing, not a check every 25ms. Works a lot more efficiently--I had not yet stress tested the system with more than 5 players before...
#9
10/16/2008 (10:08 am)
Quote:I had not yet stress tested the system with more than 5 players before...It sucks to have to make AI as efficient as possible. My target is supporting hundreds of AI agents - we'll see how efficiently I can do that... :P I think every AI system needs to have its target range of simultaneous bots. if your system is intended to provide very realistic AI for just a handful of bots, then go ahead with things that make it easier to achieve that goal, even if they may be more computationally expensive.
#10
One idea I had was to plug each AI Player into the AI Manager. The AI Manager checks each bot every 2 seconds or so, and if two AI Players on opposing teams are outside some radius of each other or do not have any line of sight to each other, they are set to "sleep" and will only think every 5 seconds. If they are close enough and have line of sight, they may want to attack each other and will think every 1/2 second. This way, developers with vast maps can stick in all their AI Players at once and each NPC will only impact game play when an opposing team member is able to do something to affect them.
10/16/2008 (10:15 am)
On a relatively slow computer, I can get 8 bots all shooting at me and taking cover without much of a hit. Past that, it starts to slow down. There are some things that I could do to reduce the performance hit, but I'm going to wait until just before I'm ready to ship to make those changes so that it doesn't interfere with anything I may do later.One idea I had was to plug each AI Player into the AI Manager. The AI Manager checks each bot every 2 seconds or so, and if two AI Players on opposing teams are outside some radius of each other or do not have any line of sight to each other, they are set to "sleep" and will only think every 5 seconds. If they are close enough and have line of sight, they may want to attack each other and will think every 1/2 second. This way, developers with vast maps can stick in all their AI Players at once and each NPC will only impact game play when an opposing team member is able to do something to affect them.
#11
10/16/2008 (2:40 pm)
I want to buy it NOW! :)
#12
10/16/2008 (2:53 pm)
@Alienforce--You sound just a little eager...When I'm ready to release beta, would you like to test for me?
#14
Just as a note to you, ive been deployed two times to Iraq now (Infantry Hooah) and personally you generally dont know that you are being engaged until you see the puff of dirt on the ground or pieces of a wall fragmenting. Now im in no way saying your wrong in what your doing, just think of this as positive criticism =) i am actually very intrigued and excited that somebody is making realistic AI for a change.
So im just going to lay down the facts, in reality from my experiences, unless (like i said before) you see the rounds hitting around you or you hear the snap you dont know your being shot at. Not only that as odd as it may seem rounds as they go past you let of a slight ripple effect that you can feel in your body (its an earie feeling) and on larger rounds like .50 cals if they even come within a foot of you they generally rip a limb off or tear some flesh and a possible your your clothes.
Now this is just a though, remember im not a programmer im a 3d artist. If a player shoots at a bot, wouldnt it make more sense instead of making the system do checks to just make the AI aware that he has been shot at? for instance if say rounds hit within 10 meters of him he starts his investigation, or if he witnesses an impact in his field of vision, or say if a round comes within three feet of the bot he does a check? triggering effects more than checks i guess you could say. Anyhow though i could tell ya just about anything you want to know about bullets and combat so feel free to email me if you have a question, note though that im still in Iraq right now (w00t! month 13 two more to go! emails on the profile).
But you need to hurry up and finish so i can use this in my project =) props to ya.
10/17/2008 (12:59 am)
@Bryce-Just as a note to you, ive been deployed two times to Iraq now (Infantry Hooah) and personally you generally dont know that you are being engaged until you see the puff of dirt on the ground or pieces of a wall fragmenting. Now im in no way saying your wrong in what your doing, just think of this as positive criticism =) i am actually very intrigued and excited that somebody is making realistic AI for a change.
So im just going to lay down the facts, in reality from my experiences, unless (like i said before) you see the rounds hitting around you or you hear the snap you dont know your being shot at. Not only that as odd as it may seem rounds as they go past you let of a slight ripple effect that you can feel in your body (its an earie feeling) and on larger rounds like .50 cals if they even come within a foot of you they generally rip a limb off or tear some flesh and a possible your your clothes.
Now this is just a though, remember im not a programmer im a 3d artist. If a player shoots at a bot, wouldnt it make more sense instead of making the system do checks to just make the AI aware that he has been shot at? for instance if say rounds hit within 10 meters of him he starts his investigation, or if he witnesses an impact in his field of vision, or say if a round comes within three feet of the bot he does a check? triggering effects more than checks i guess you could say. Anyhow though i could tell ya just about anything you want to know about bullets and combat so feel free to email me if you have a question, note though that im still in Iraq right now (w00t! month 13 two more to go! emails on the profile).
But you need to hurry up and finish so i can use this in my project =) props to ya.
#15
10/17/2008 (6:27 am)
@Mikey: That makes sense. I'll see if I can implement a visibility check in the part of the code that detects the impacts and see how it goes. Thank you for your service, I hope you come home safely!
#16
First, thanks for the service! From someone who makes some of the products you probably use:) Be safe...
I am also a certified firearms instructor and even though I have never been in the service, I have been shot at! Sitting in a boat on the river and hearing the crack of a rifle and the plop of a bullet next to the boat, I can pretty much tell what the reaction is as well, and would love to talk to you more once I get my AI going - again (lost on hard drive crash). You know you were shot at, but you have no idea where from, and the first reaction (if trained in self defense), is to get down and seek cover.
If you have ever played GRAW2, you know that if you shoot a NPC, that is their first reaction, to find cover and start looking for you. Just like in real life, the cover they choose may not cover them from all directions and leave them open to engagement from behind the cover. Which is why I do not use the 360 check. I physically rotate the NPC so that you always know the way he is looking. There is no overhead in the game, because they are only looking in the normal FOV that a human would probably have. In fact, if you are in stealth mode, you could sneak right up behind the player and stab him and he would never know it (well, until he died). Just be careful not to step on anything to trigger the noise alert:)
This is indeed a very interesting topic. Curious Bryce, I have not read all of your other blogs, but hace played your demo. are you incorporating this in code or script?
10/17/2008 (10:09 am)
@Mikey,First, thanks for the service! From someone who makes some of the products you probably use:) Be safe...
I am also a certified firearms instructor and even though I have never been in the service, I have been shot at! Sitting in a boat on the river and hearing the crack of a rifle and the plop of a bullet next to the boat, I can pretty much tell what the reaction is as well, and would love to talk to you more once I get my AI going - again (lost on hard drive crash). You know you were shot at, but you have no idea where from, and the first reaction (if trained in self defense), is to get down and seek cover.
If you have ever played GRAW2, you know that if you shoot a NPC, that is their first reaction, to find cover and start looking for you. Just like in real life, the cover they choose may not cover them from all directions and leave them open to engagement from behind the cover. Which is why I do not use the 360 check. I physically rotate the NPC so that you always know the way he is looking. There is no overhead in the game, because they are only looking in the normal FOV that a human would probably have. In fact, if you are in stealth mode, you could sneak right up behind the player and stab him and he would never know it (well, until he died). Just be careful not to step on anything to trigger the noise alert:)
This is indeed a very interesting topic. Curious Bryce, I have not read all of your other blogs, but hace played your demo. are you incorporating this in code or script?
#17
10/17/2008 (10:19 am)
@Randy--To preserve flexibility, most of this is in script, but I have the pathfinding code in C++ to prevent heavy performance hits. If you are familiar with the Killer Kork structure, my AI code is build that way. All code is built into an "algorithm," and that is assigned to the AI player object in-game. It uses a similar state machine, similar visibility testing function, and I borrowed some math functions from it (::getAngle(), ::rotateAngleAz(), ::doYaw(), etc).
#18
Thanks for the quick reply!
Not familiar with the Killer Kork. That makes sense to let the code handle the rougher stuff... Just for an FYI, for me I had taken the AIGuard/AIPatrol examples and started ripping them though and developing what I needed. My background is in engineering design, but by hobby I am a hacker so right now I am in the process of taking classes to hone that part of my skills. So it will be a year or so before I have exactly what I need.
Good Job so far.. Go info on reading. I have read in the other links where people have already addressed some questions I had on the players crowding me in a corner and know you are already looking into that:)
I like the fact you are open minded with the input from the community - Good Job!
(I am old and set in my ways sometimes:)
10/17/2008 (11:08 am)
@Bryce,Thanks for the quick reply!
Not familiar with the Killer Kork. That makes sense to let the code handle the rougher stuff... Just for an FYI, for me I had taken the AIGuard/AIPatrol examples and started ripping them though and developing what I needed. My background is in engineering design, but by hobby I am a hacker so right now I am in the process of taking classes to hone that part of my skills. So it will be a year or so before I have exactly what I need.
Good Job so far.. Go info on reading. I have read in the other links where people have already addressed some questions I had on the players crowding me in a corner and know you are already looking into that:)
I like the fact you are open minded with the input from the community - Good Job!
(I am old and set in my ways sometimes:)
#19
10/17/2008 (11:15 am)
@Randy: Thanks :-)
#20
10/17/2008 (1:58 pm)
bryce - on a slightly unrelated note, how are you handling squad movement with AIPlayers? Do you run an update function to decide on move destinations for all of them? 
Torque Owner Randy Hearn - Magnum
Default Studio Name
Also you may already have it, but a percentage of time they will react to a shot changes game play a lot. If alert 30% chance of telling where it came from and increases each time a shot is fired, giving the impression they are looking for you.
I also hate the accuracy of NPC shots increasingly getting better with each shot. If he is a lousy shot, then he should stay a lousy shot. If he is a veteran sniper, well then you are dead.
Just a few of the things that bug me during gameplay as well. Plus the fact they can see/start shooting at you before you top a hill:)