Game Development Community

dev|Pro Game Development Curriculum

Tactical AI Kit: Dozens of NPCs

by Bryce · 10/11/2009 (1:02 am) · 23 comments

I haven't posted a blog here since July. My goodness. Too long, eh?

Cool. From what I remember of the last blog (or was it the one before it...?), I touched a bit on the AI Manager system, which is a system that manages AI. I'm hoping you assumed that before I had to tell you. Anyway, it figures out which NPCs should be active and ready to fight based on the player's position. The idea is that in a single player game, you should be able to toss in dozens of NPCs and count on the AI Manager to keep only 10 or so awake at a time to avoid killing performance.

Inspired by a comment by Konrad Kiss, I decided to implement a zone system for the AI. Instead of searching for NPCs within a radius and deciding whether they should sleep or not based on LOS (which gets glitchy, as they oddly fade into view once the player turns a corner), the level is split up into triggers.

Some pictures to demonstrate:

img269.imageshack.us/img269/5174/basicmap.png
That's an overhead view of a theoretical mission.

img408.imageshack.us/img408/1971/basicmapzoned.png
There's the same mission, but with the zones represented as blue boxes. When you spawn a large number of NPCs in a mission, the AI Manager figures out which zone the human player is standing in. It then decides that any NPCs in zones that are not in or adjacent to the zone that the player is in, will be set to sleep. Sleeping NPCs don't perform any complex calculations (they don't look for enemies or other visible objects), are not processed by other NPCs, and they are set to be invisible to avoid the rendering performance hit.

img405.imageshack.us/img405/6828/basicmapzonedactive.png
The adjacent zones are specified on each zone trigger with a variable called adjacentZones. Here you type in the space-delimited list of the adjacent triggers (Zone_0 Zone_2 Zone_7...). The triggers don't even have to be physically adjacent: You could have the player be able to enter one zone, which activates a zone for a sniper tower far away. The idea is that a loud firefight in one zone should trip the NPCs in the next zone, so that they can move in/take cover and be ready for you.

Adding zones is very, very simple. You create a trigger with the datablock AIZone, and you scale it until it covers a little section. Then you copy+paste it, scale it, and repeat. Simple.

img12.imageshack.us/img12/7243/placezone.jpg
This cool thing about this? Because of the way the NPCs' alert system is designed, sleeping NPCs can still be alerted to things like explosions, shots in the distance, et cetera. They won't suck up performance by acting on these things until their zone is activated. This means that you can drop in a few dozen NPCs all over your mission, and when the player's cover is blown and they take part in a very audible firefight, the NPCs they encounter later on in the level will act like they heard the commotion earlier. This adds to level freedom. You can use silenced weapons, move quietly, and avoid alerting anybody and finish the level by stealth. Or, you can go Rambo and have everybody looking for you. Due to the randomness and hiding involved in the reaction processes, the NPCs show up in unpredictable locations, popping up when you least expect them.

Now, a video. The first part shows a run through the Linear MidEast mission, with the player's team set to -1 (enemies will not deliberately engage the player, but can react to the things he does), and using silenced weapons. The second part shows a run through the plane mission, with loud weapons and unlimited health. I apologize for the lag, that's just my computer. I can assure you that running 30 NPCs without the manager would be much, much worse.


I'll see if I can get another single player demo out soon. Now leave a comment or the terrorists win.
Page «Previous 1 2
#1
10/11/2009 (1:31 am)
Hey,

I have read all of your blogs about this "Kit" but am yet to work out whether this is a kit which is going to be released that has been in the works for a long time? or if it is just a project of your own?

Something like this would be awesome for the community, and the quality of FPS games coming out of Torque would be amazing! (not that they aren't already)
#2
10/11/2009 (1:49 am)
This is something that I'll hopefully be releasing soon, it's been in the works for a couple years now. I've pretty much run out of stuff to add...Now, it's just a matter of cleaning up the code, documenting it, and getting it up in the store.
#3
10/11/2009 (2:02 am)
Awesome Mr. Bryce!
#4
10/11/2009 (2:42 am)
I'd like to see more of the second part of the video :-)

This is always an interesting kit, AI can be so very important!
#5
10/11/2009 (2:45 am)
By the way what is your computer specs?
#6
10/11/2009 (2:48 am)
Great job, Bryce! Glad you got inspired, and great to see the system working for you!
#7
10/11/2009 (4:52 am)
Great work as usual Bryce, you've certainly got some passion for AI :)
#8
10/11/2009 (10:37 am)
Smart idea to zone off your AI, and well thought out to allow the ones in the next zones to reenable on loud events.

Any particular reason you've gone back to TGE? Coal powered laptop giving T3D gip?
#9
10/11/2009 (11:52 am)
@Edward: Yeah, the second part got cut off because I can only record 30 seconds at a time with Fraps, causing me to accidentally skip over most of the beginning.... I originally went upstairs, but all of them had already moved down to the lower deck except for one (who cowered in a little room way in the back), so I decided to just cut that out. I may try to re-record the plane level and make it into its own video.

And my specs: Dell Vostro 1510, 1.8GHz Core 2 Duo, no graphics card to my knowledge, 4G memory.

@Steve: I only own T3D so I can port over my work when it's finished, because a lot of people are switching to the cooler tech. I get about 1 frame per second under advanced lighting, and about 5 using basic...I prefer to code and experiment with TGE so that I can get some decent performance.

Thanks to everyone for the comments!
#10
10/11/2009 (2:21 pm)
So it was your coal powered laptop ...

It's probably got an old 965 chipset in there. I've got a laptop with something similar and ... yeah, T3D runs but doesn't likey.

At least after you release your kit you might get the cash to upgrade your hardware!
#11
10/11/2009 (2:35 pm)
but the problem is that if the player is in the middle you need get up 5 zones, right?

its not better use small triggers like traps?

can you please share the torquescript blood effect? (its a Particle Emitter right? ), thanks.
#12
10/11/2009 (5:11 pm)
@Steve: 965 sounds familiar. I'm too lazy to check.

@Javier: It all depends on your level design. Maybe don't put so many NPCs in the middle, or don't specify as many adjacent zones. If an NPC is attacking or being attacked, they won't sleep regardless of the zone they are in. So you could always make it one large zone.

This would work well with the small triggers, but those can be a pain to place. I'm just trying to make it a little less time consuming to set up a level. A combination of traps and zones would be the best solution in my opinion.

What's your email address? I'll see if I can send you a .zip with all the needed files.
#13
10/11/2009 (5:33 pm)
Looks awesome, can't wait to play the new demo.

LOL at the slow motion, you are that cool bryce! lmao
#14
10/11/2009 (9:31 pm)
Quote:A combination of traps and zones would be the best solution in my opinion
i second that...

my email is javier_canon [ * ] hotmail.com thanks a lot...
#15
10/12/2009 (6:58 am)
And here, after all this time, I thought you had fallen off the earth.
The videos look awesome. I can't wait to try what you have now. (I still play with the earlier beta)

#16
10/12/2009 (12:05 pm)
a new Beta? Id love to see the new one. I have the old one. my emial is racs333@hotmail.com
#17
10/12/2009 (5:37 pm)
Edward, the most recent demo can be found here. I hope you're not playing the one I released last October...
#18
10/12/2009 (6:02 pm)
Nice. Looks like a good way to optimise performance without compromising gameplay too much. Any idea how it fares in more open levels, like your Mid-East example with all the buildings to snipe from? I assume you'd want to activate a zone if, for example, the player could physically see it, or fires into it? Or would you set the sniper tower zones to have a ton more neighbours than just the adjacent zones?
#19
10/12/2009 (8:06 pm)
You want to have as few zones active as you can (fewer NPCs active), so you could give them more neighbors. However, you wouldn't have to activate all of them. Inactive NPCs go invisible, so if they aren't active, well then the player doesn't have to know that they exist ;]

It's all a matter of design. The zone system is much better off in generally linear, single-player missions without too many big open areas.

For example, I'm working on a single player map now that has the player fight through a town (one zone), the roads leading up a mountain (another zone), and down the mountain to another town again (a larger town with lots of buildings and enclosed spaces, so I'll use several zones). I don't need to set the zones for the town and the mountain road to have any neighboring zones, because the large scale of the map allows me to assume that they wouldn't move in to the neighboring areas anyway. But they can still hear the shots, and will go into their react state as soon as their zone is activated. Make any sense?

Basically, the small town, mountain road, and large town are sub-missions. It's just easier to spawn all NPCs at once, and to have them already cautious and watching for enemies as soon as they should be.
#20
10/13/2009 (2:25 am)
Quote:Example: zoning your game
To save CPU time and get better performance in your game, you divide the world into separate zones/mission regions/trigger areas. When the player enters and exits these areas, a message is send to the "zone information" message queue, saying "player entered/exited zone X". All the NPC AI's in the zone can now go from idle to active, and process input as they have subscribed to this message queue, and been informed of the arrival of a player. While the zone you just left can now be shut down. No need to have the parrots fly around, the NPC guards doing radius checks to see if you are in range or similar if the player is on the other side of the map.

You use this resource?:
www.garagegames.com/community/resources/view/5939

Page «Previous 1 2