Report article RSS Feed The basics of AI in Half Life 2

Have a look at the methods to create a convincing fight against real threats in your levels.

Posted by RavenITA on May 31st, 2009
Intermediate Other.

Most of the problems a beginner has when thinking of creating a good fight scene rely on his conviction that the AI is good enough by itself. I saw a lot of people simply put some soldiers here and there and start their mod, which fails badly in offering a good fighting experience.
So, how are we going to handle this?

First of all, we must understand that the Half Life 2 AI cant see anything by itself. Soldiers and monsters dont have any data about the environment around them, so we need to provide them those data.
The only thing they're good at when compiling a map without providing them data is chasing the player wherever he is regardless of the consequences.
To help them out, there are some special entities, such as:

-AI_battle_line
-AI_changehintgroup
-AI_changetarget
-AI_goal
-AI_goal_follow
-AI_goal_assault

and so on. Those entities can be found in Hammer and have their unique propieties. I'm not going to explain what each of them does, but I'll do suggest some of them and their use.

PART 1 - Navigation
The first thing a monster needs is knowing what is going on around them. Barrels, crates, walls, everything. With those information, he will follow a path "thinking" where he's going.
For this, we will use info_nodes. An info_node is a special entity used for local navigation that tells the NPCs where they can go.

So, assuming that you have something simple, like this:


You'll start placing info_nodes around to help soldiers and monsters find their way through the level. Another common error I saw often was this:

 
Now, you may think "Wooah, now they are super smart and can go everywhere!" wrong. Placing a lot of info_nodes around the area with no logic will make your soldiers even more stupid than before!

To check this in game, simply compile your map. Then enter into the console (if you dont know how to do it, create a desktop shortcut to your game and type -toconsole at the end of the "path" field) and use the command: ai_show_connect.

With this command, you can see directly into the game how things are going.


See the green lines? Those areas the NPC can travel to with no problems. The red lines indicates that the area is unreachable and something bad has occurred. Examine the image and you'll find out that:

1 - the green lines are placed in total disorder
2 - some of the info_nodes lead to dead areas because of bad placement

This means that the NPC will follow a strange route, and where he wont find nodes any more, he'll just try to reach the player. So, you have to think before placing info_nodes. Use them carefully, only place them in groups where a NPC must do a choice between two or more paths, and make sure they're always connected to each other.


An example of good node placement on a corner. Notice that, info_nodes can be also info_node_climb and info_node_air. There's no difference between these classes, except that the first is for climbing NPCs and the second is for flying NPCs. Just place the air nodes above the ground where you want your NPC to fly to, with the same method as the ground nodes.

PART II - HINTS
Hints are the essentials of the NPC AI. They provide extra data on the environment and can help them perform a lot of tasks.

Examine the Hint node.


As you can see, Hints work a little differently from info_nodes. There are two types of hints:

- info_hint
- info_node_hint (and info_node_air_hint - which is the same thing)

The info_hint entity only provides information on what's going on around the NPC, and wont serve as node for local navigation. An inf_node_hint instead will do both: will provide useful informations, and serve as a node for the NPC to walk to.
Is up to you to set the first or the second, I generally prefer to have info_node_hints only for basic actions such as cover and entrance\exit pinch.

The Hint propieties are:

Hint Activity - an activity associated with this hint
Target node - this hint can be linked to another node
Maximum and Minimum state - the NPC wont use this hint if he's not in the specified state
Hint - what information this hint will provide to the NPC

There are a variety of actions the NPC can perform with this entity, but the most common are crouch cover medium \ low, entrance \ exit pinch, and enemy disadvantage point.

Remember that certain hints wont work if the player can see the enemy and \ or is behind the NPC.
As for normal nodes, place hint nodes carefully: its no use having a "enemy disadvantage point" in the middle of an open area with no cover: the enemy will try to kill the player directly, becoming cannon fodder for his shotgun. Always try to flank the player or push him into a corner.

Another interesting and very useful aspect of hint nodes is that you can give them a name and divide them in groups, then assign a certain group to your desired NPCs. Even if this wont affect local navigation, they will try to follow the specified route you gave them via hints, so you can create realistic tactics that will make the goal more interesting to reach.

PART III - Brushwork!
Even with those informations and hints, NPCs still needs something that can help them out.
For example, a good map should always have NPC CLIPs around.  The npc clip is not an entity, is just a texture with some special proprieties that will block the NPC anywhere you want. Select the npc clip texture, and make a block covered with that. In game this texture will be invisible, and the player wont be affected by it. Try to build boxes and shapes with this texture to prevent NPCs to fall \ get stuck in a corner \ try to reach the player from an unreachable position. With this and the data from nodes and hints, your soldiers and monsters will be real war machines. But there's still a problem to be solved: physics! Yes, physics are the real threat to NPCs, because:

1 - NPCs wont bump into physics even if they're small and easily pushable, instead, they'll avoid them like normal geometry
2 - info_nodes and hint nodes wont be blocked by physics objects, so the NPCs wont know how to reach a certain place if there's a physic object in the middle of the path

To avoid this (very annoying indeed) problem, you must use another entity: logic_navigation.
The logic_navigation entity will tell a desired NPC to push certain physics objects instead of avoiding them. Unfortunately, it appears that you cant use classnames to use this entity, so you'll be forced to do this trick only with certain NPCs with the same name. Physics objects affected by this entity have the same problem, so you'll want to build your levels around this issue when thinking of fighting soldiers.

PART IV - Code
The most important aspect of AI is, of course, the code.
Now I'm going to be a little more specific, just to give you a general idea.

NOTE: I assume you know the basics of C++

Go into your mod foldier\ src \ game \ server \ hl2 \ npc_combine.cpp (if you have Orange Box) and take a look at this line, under the Spawn function:

cpp code:
CapabilitiesAdd( bits_CAP_TURN_HEAD | bits_CAP_MOVE_GROUND | bits_CAP_MOVE_JUMP );

This defines what the NPC can do. He can turn his head, move on the ground, and jump.

cpp code:
CapabilitiesAdd( bits_CAP_AIM_GUN );
 // Innate range attack for grenade CapabilitiesAdd(bits_CAP_INNATE_RANGE_ATTACK2 );
 // Innate range attack for kicking CapabilitiesAdd(bits_CAP_INNATE_MELEE_ATTACK1 );
 // Can be in a squad
CapabilitiesAdd( bits_CAP_SQUAD);
CapabilitiesAdd( bits_CAP_USE_WEAPONS );
CapabilitiesAdd( bits_CAP_DUCK );
 // In reloading and cover
CapabilitiesAdd( bits_CAP_NO_HIT_SQUADMATES );

This defines that the NPC can have a squad, he cant hit squadmates, can duck etc etc. Adding things an NPC is capable of can improve very quickly his reactions and give him new  abilities. Just experiment it. Removing CapabilitiesADD ( bits_CAP_MOVE_SHOOT ), from npc_combine_s for example, will result in taking away the possibility to shoot and move at the same time, so they'll be moving a lot more to avoid the player, but will shoot less, like HL1 grunts. Also, adding CapabilitiesADD( bits_CAP_MOVE_CLIMB ) with the proper animations from your modeler, will let the NPC climb ladders and other objects via info_node_climb. There's a lot that can be done to make your NPCs the best ever.

Now, you'll be asking "ok, this covers the moves he can perform, but what about the actual "thinking" ?" You're right, I almost forgot. NPCs think about what they'll have to do next with a combination of TASKS, CONDITIONS and SCHEDULES. To make it simple, a series of Tasks (such as wait_for_movement, get_path_to_target, and so on) are performed if certain conditions are met.

Example:
If the enemy (the player) is facing me (NPC), then do the following tasks, otherwise, do another one

A series of tasks compose a schedule. Schedules are marked as valid only if every task within the specified schedule is done. If only a single task is not done due to its conditions to be valid, then the schedule is immediately discarded. Lets have a look at the schedules that the Combine soldiers use in battle:

Search for int CNPC_Combine::SelectCombatSchedule()
 

cpp code:
  // If I'm scared of this enemy run away
  if ( IRelationType( GetEnemy() ) == D_FR )
  {
    if (HasCondition( COND_SEE_ENEMY )  ||
      HasCondition( COND_SEE_FEAR ) ||
      HasCondition( COND_LIGHT_DAMAGE ) ||
      HasCondition( COND_HEAVY_DAMAGE ))
    {
      FearSound();
      //ClearCommandGoal();
      return SCHED_RUN_FROM_ENEMY;
    }

Ok, this is a simple one. Basically it checks if the NPC sees an enemy, and if this enemy is someone he fears (relationship is equal to D_FR - FR stands for Fear), has taken light or heavy damage, then it starts the schedule run_away_from_enemy. Pretty simple huh? Knowing this and the specific tasks \ schedules \ conditions of each monster (usually stored in the Header file) and shared ones, you'll be able to modify their behaviour and make your NPCs true beasts in battle.

Have fun and experiment with your NPCs!

Post comment Comments
Jokerme
Jokerme Jun 1 2009, 12:05am says:

Wow, that's some detailed tutorial. Nice job. I'm sure it will be helpful.

+2 votes     reply to comment
RavenITA
RavenITA Jun 1 2009, 1:58am says:

Thanks

+2 votes     reply to comment
Skrell
Skrell Jun 1 2009, 2:49pm says:

I would love to see more of these type tutorials!! also, please fix the grammar mistakes as it does make the reading slightly more confusing. Btw, i can see <br /> statements used in your second C++ code example.

+2 votes     reply to comment
RavenITA
RavenITA Jun 1 2009, 5:04pm replied:

Thank you, I fixed that error. Sorry about the mistakes, I'll try to be more careful next time

+1 vote     reply to comment
Skrell
Skrell Jun 1 2009, 5:37pm says:

honestly, i think you have a talent with explaining this stuff. You did a great job relating all the ideas with concrete examples(C++ code). You should consider making a website where you could post this tutorial(possibly expanded) along with other tutorials. Then you could have a Q&A section(maybe separate into noob/expert section) at the bottom where people can post questions about specific things in the tutorial and other people can answer them.

+3 votes     reply to comment
Marvin_hun
Marvin_hun Jun 10 2009, 3:20pm says:

Thanks for your tutorial, it gave to me a good idea...

So, I'm working on a HL2 mod project (Wolfenstein 3D + Spear of Destiny remake - you can find the mod's profile here: Moddb.com), but I haven't got any knowledge about the coding. So I created a few enemy custom models, but I cannot coding them. The valve's developer site isn't includes enough information about the coding.

Can you make a tutorial about creating a simple NPC's coding? Nothing special, just the NPC could Idle, Patrol-walk, Realize the player, Run, Shoot, Melee, Reload, and die.

I need only a simple npc.cpp file, what I can customize for all enemies.

Thanks!

+2 votes     reply to comment
crazy90
crazy90 Jul 6 2009, 2:24pm says:

nice tut.

+2 votes     reply to comment
aNewb
aNewb Aug 5 2009, 10:17pm says:

Wow cool tutorial I want to make a bot 4 gmod but I don't know where the src folder is (probably need to decompile the dll to c it[I'm a noob to c++]), and I want to take a look at the game ai before trying to make my own.
Nice tutorial a must read for noobs that want to make modify ai or just make a map.

+1 vote     reply to comment
RavenITA
RavenITA Aug 7 2009, 2:32pm says:

I'm afraid that Gmod uses its own dlls so you wont be able to create a bot for it, if I'm not mistaken

+3 votes     reply to comment
aNewb
aNewb Aug 10 2009, 3:22pm says:

Dan's snpc pack is an addon for Gmod with bots that he said he made customly. And I want to try to make a bot like that. The npc addons use lua instead of dlls but I don't realy know if I'm even looking at the right files.

+2 votes     reply to comment
RavenITA
RavenITA Aug 25 2009, 1:42pm says:

Listen to me, learn some programming language first, or ask to someone that can actually solve your problems. Most Gmod NPCs are just replacements for standard models with the same animations \ sounds

+2 votes     reply to comment
confridin
confridin Sep 30 2009, 11:21am says:

This tuto has been translated to french, with the author's Agreement.

The french version is on this website : play-mod.rochmedia.com

+1 vote     reply to comment
Themodderofmods
Themodderofmods Oct 5 2009, 10:46pm says:

Thanks you very much!

+2 votes     reply to comment
Necromunger
Necromunger Oct 28 2009, 7:26am replied:

yeah this tuts awesome

+2 votes     reply to comment
SGritter
SGritter Nov 9 2009, 1:11pm says:

Much appreciated -- this provides all the initial need to know and go-tos to really dive in with the ai. Cheers.

+2 votes     reply to comment
NOMaDHUNteR
NOMaDHUNteR Apr 22 2011, 3:16pm says:

good;)

+2 votes     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
Half-Life 2
Half-Life 2 Single & Multiplayer First Person Shooter
Related Engines
Source
Source Commercial Released Oct 31, 2004
Related Groups
Valve
Valve Developer & Publisher