Chapter 10. Advanced NPC Scripting

In the previous chapter, we learned how easy it was to give some life to the NPCs in our levels. The Half-Life series of games are classified as First-Person Shooters; what's the fun if we can't get some bullets flying? This chapter will guide you through setting up scripted battles with your NPCs. We could script nearly every footstep of every NPC if we wanted to, but that would take a long time, and the results wouldn't be great unless we spent even more time tweaking every detail. The Source engine has a fantastic AI system that we can manipulate through the use of assaults, squads, and hint nodes. Let's begin.

In this chapter, we will cover the following topics:

  • Using hint nodes to guide battles
  • Using assaults to assign battle goals
  • Using squads
  • Setting up lines of defense

Using nodes

The AI (Artificial Intelligence) is clever, but it frequently needs some coercing to really do what we want it to do. That's where the nodes come into play. Nodes come in many varieties, but we'll be focusing on two nodes in this chapter: info nodes define the basic nav-mesh that the NPCs use to get around and hint nodes give additional meaning to the nav-mesh and help with NPC battles.

Using info nodes

Info nodes, when placed, are short yellow boxes and have Ground Node written on them, as shown in the following image:

Using info nodes

The info_node entity is the most basic piece that makes up the navigation mesh, but they have a significant impact on how the AI works. To really see how necessary these are, let's start by making a simple map where we can see the difference between having them and not having them. Make a 1024 x 1024 room, place some combine soldiers in it, give yourself a gun, and place some boxes for cover. I have included an example map, that is, ch10_example01 that you can refer to if needs be.

Using info nodes

Place a light, compile, then run the map, and fight the combine. Try the map a few times changing tactics every time. You'll notice that the soldiers are pretty static; they don't find cover, they don't chase after you, and they generally seem pretty disorganized.

Using info nodes

The battle is easy. You can peek out and shoot them one at a time. The soldiers rarely move, and if they do, it's straight at you, making it easier to pick them off. Go back into the map and, with your entity tool, place some info_node entities on the ground at strategic locations. Place them near cover, behind walls, and spread them out in open areas to help guide the soldiers between cover.

The soldiers will use the nodes to navigate around the map. They will travel in a straight line between each node in order to get around. The more info_node entities you place, the more options the soldiers have to walk around.

Using info nodes

Open the ch10_example02 example map to check out one way to position the nodes. Compile again and then run the map to check out the soldier's new behavior.

Using info nodes

This map just got a bit harder, didn't it? The soldiers are now more aggressive. They don't really give you a chance to breathe, and they're always moving. Not only do they always move around, but also they now take cover behind boxes while reloading and will chase you around a corner if you try to hide.

All we did was place some info_nodes entities; how about that? The AI knows to take cover while reloading, and it knows when it can't see you; so, it will try to chase you down. The info_node entity gives information about the map to the soldiers, and they allow the AI to really live up to its potential.

Using hint nodes

We can further enhance the effectiveness of the AI by using hint nodes. Hint nodes give some sort of meaning to the area around an NPC and make battles a bit more realistic.

Using hint nodes

Hint nodes come in two varieties:

  • info_node_hint: This is a ground node that acts like a regular node but with special properties. An NPC can navigate to this node and will act a certain way once there.
  • info_hint: This is not a navigation node, so NPCs won't move to their location, but they can be used to draw attention to certain areas of the map.

The ch10_example03 example map is just like the previous map; only this one has hint nodes stationed behind boxes. These hint nodes will tell the soldiers to crouch behind boxes to take cover.

Using hint nodes

We've used the same hint nodes for the ActBusy scripts in the previous chapter, but here we're using them a bit differently. Place one down and open the properties, as shown in the following screenshot:

Using hint nodes

Set the Hint parameter to Crouch Cover Medium. Any NPC using this node will know that this location is an ideal place to crouch. The Node FOV property is a useful property as well. NPCs in the area surrounding this node will only use it if the node can see the target NPC. Take the following image as an example:

Using hint nodes

This info_node_hint property has an FOV of 180 degrees (90 degrees on either side of the angle it's facing). The NPC will only use this node if it's within the shaded cone.

The MaximumState property and MinimumState property helps define whether or not an NPC can use a specific node. In this context, a state is defined as Idle, Alert, or Combat. Here, Idle is normal behavior; an idle NPC will stand around. An Alert NPC has either just exited combat or sensed combat nearby. A Combat state is set when there are bullets flying.

For instance, if the minimum state is combat, and an NPC is not in combat, the NPC will ignore the node. Once we compile and play the game, we can see the effectiveness of the hint nodes. The soldiers will actively seek cover if you're shooting at them, making them harder to hit.

Using hint nodes

That just covers info nodes and hint nodes. We have proven how useful they are for player versus AI battles. But what about AI versus AI battles? We can do this in AI versus AI battles too.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.55.18