Nodegraphs: The Key to Intelligent NPCs
(September 27, 2017)
If you're creating levels with NPCs, a good nodegraph is important. It's the way NPCs see the world; they don't know good places to hide and where to go unless you tell them where, and you do that through their nodegraph. Even if you're just making a sandbox map for Garry's Mod, if you don't have a nodegraph, the people who like to spawn NPCs will be disappointed when Combine soldiers simply stand there and run at them. Not every kind of NPC uses the Hammer-based nodegraph, but the base Half-Life 2 NPCs do, and that's important enough.
Wait, what's a nodegraph?
A nodegraph is the network of AI nodes (position entities, essentially) that NPCs such as citizens and Combine soldiers will use to get around your level. Half-Life: Source NPCs also use the nodegraph. There's three main types of nodes; navigation nodes (
info_hint), hint nodes (
info_node_hint), and air nodes (
Navigation nodes are your basic type of AI node, and they'll make up the vast majority of nodes in your level.
info_node defines a walkable point on the ground. Any nodes around it will be connected to one another in a big network, and NPCs will use the network to find a path to what they need (cover, a better shot, and so on).
info_node entities won't work if you place them too close to the ground, inside brushwork, or over brush entities. (A good idea is to select all of your nodes and move them a certain height above the ground, between 32 and 128 units, to make sure they're functional.) You can see their locations in-game by entering
ai_nodes into the console, and see their connections with
ai_show_connect. Green nodes are functional, while red ones are not, even if they display in a location where they look functional. Same goes for the network lines.
Believe it or not, the vast majority of the AI scripting in a level is already done for you, provided you have these nodes. Of course, I'm leaving out one crucial part: where do you put the nodes? Do you just scatter them about loosely, or do you make a big, dense network across everything? Too few, and the NPCs will trip over each other's feet. Too many, and the AI becomes inefficient.
The rule of thumb is to keep the nodegraph moderately populated, but cluster tightly around cover points and intricate geometry and keep sparse in areas where NPCs shouldn't linger. Nodes should also subconsciously enforce the behavior of the NPC in question, like humans being rather imprecise and scattered and the genetically-altered Combine soldiers being strict and exact in their positioning. Here's a few examples of Valve's nodegraphs, to give you a better idea of how best to craft yours.
Valve noding, example one: City 17
The fourth map of the "Anticitizen One" chapter has AI nodes scattered about randomly, around 100 units from one another. Note the cluster of nodes around the window area; this is a cover point and also allows NPCs to get out of the breakable shop windows without getting too caught up on the frames.
Behind the Combine wall, you'll notice the ground is very sparse of nodes, but the walkway behind the wall itself has them rather densely clustered. Remember to cluster nodes where the action is hot, and make them more sparse when you want NPCs to keep moving.
Valve noding, example two: Nova Prospekt
This view from the second map of the "Nova Prospekt" chapter shows the way Valve noded the tight hallways of the prison. Unlike the City 17 map, the prison has a largely symmetrical and sparse nodegraph. Here, the soldiers also have to keep moving, but the straight lines emphasize the prison architecture, give the enemy movement a rather robotic feeling, and keep them from bumping into one another on the walkways.
There's not a lot of cover in these maps, so the nodegraph doesn't really account for it. The action stays out in the open. There's extra nodes around the concrete debris in the corner of the Nova Prospekt washroom so NPCs don't stumble around it. This is important to remember; if there's a chance that NPCs can get caught up on a part of your map, place nodes tightly around that part in case your level geometry breaks part of the network.
The second node type, hint nodes, have the ability to suggest things to NPCs that look for them. A hint node can suggest to an NPC to look at it when idle, so as to appear that the NPC is doing more than it really is. Birds will use hint nodes to find places to perch when frightened. NPCs are not forced to use hint nodes, and not all hint types will be usable by all NPCs.
You can set the type of hint in the entity settings, and the above Valve Developer Community link has a list of all available hint types and which NPCs respond to the one in question.
info_node_hint are differenciated only by the fact that the former is not a navigation node, and NPCs won't walk to it.
info_node_hint acts as a proper navigation node as well as a hint node.
Air nodes, our final type of node, are functional for NPCs marked as air NPCs, such as helicopters, striders, and city scanners, but not much else. Any NPCs that work underwater will also use air nodes. Noding your map with air nodes is only useful if the map is big enough to sustain them. They're difficult to place and not many NPCs will make use of them, so whether or not you node your map for them is up to you. If you know you'll be dealing with scanners or helicopters, however, they're practically required. Your nodegraph for these can be much sparser than the ground nodes, and air nodes have to be at least 64 units away from all solid geometry in order to be functional.
A good nodegraph should work with your level design and aid enemies and allies into keeping the action interesting. Keep your nodegraph varied and interesting, and the NPCs will act varied and interesting.