Monday, November 7, 2011

Close Combat AI in Armed Assault 2

Have I mentioned that I really love working with AIs?  I had so much fun working on RCBOT 2 AI.  As much as I love them, they can also be frustrating.  The default AI in Armed Assault 2, a military simulator, had flaws that really bothered me.

For those of you who don't know what Armed Assault 2 is about:  It is a military simulation game developed by Bohemia Interactive Studios.  Their game engine has been under development for over a decade now and has gathered a large fan base over the years, thanks to their high mod-ability.  Its AI, however, has been its weak point since day one.  Although BIS has been improving the AI and is now quite intelligent, one problem still remains:  the AI is poor in close combat situations.

The following video is a demonstration of the default AI at work in close combat situations:


The reaction time is not that bad as it used to be.  The developers did a good job in making the AI smoother.  However, this is still too easy for experienced players to find near challenging.  Those players want AIs that can react EVEN FASTER.  They want to be kept on the edge instead of leaning back on their chairs and shooting down slow AIs.

My journey to create such an AI began there. 

It took me a day of brainstorming and crunching out the vector math and two days of testing prototypes.

My AI, which will be referred to as CQB AI from here on, first checks for hostile targets that are nearby.  Once a hostile target has been spotted, the CQB AI will lock onto that target unless another hostile target that has been spotted poses a greater threat than its current target (this is determined using a complicated formula that involves the distance to the target and the orientation of the target in relation to the AI).  Once a target has been acquired, the AI will continuously fire an invisible bullet toward this target.  If this bullet hits the target, then the AI has a line of sight to the target and will thus fire at the target.  Now, the AI doesn't actually fire this bullet.  This bullet, called the LOSShot, is spawned at the position of the AI and the physics engine applies a velocity of great magnitude toward the target.  Armed Assault 2 engine stores different types of bullets in the form of classes.  LOSShot is a bullet class on its own that, unlike the other bullet classes, does no damage and has no visual feedback.

The method through which the CQB AI acquires its target is the same as that of the default AI.  Enemy acquisition of the default AI is good enough to simulate the reaction of a human.  The problem of the default AI lay in the fact that the process after acquiring its target was not fast enough.  Naturally, I only had to improve that portion.  The mechanism through which I make the AI fire its weapon is not conventional.  As with the LOSShot, I do not make the AI fire the weapon.  Instead, I use what is called an event handler to simulate firing.  An event handler is a conditional code that is triggered every time a certain action occurs.  In my case, I added an event handler that handles firing.  Every time the AI fires, my event handler is called.  This event handler gets the projectile that was fired from the and redirects its velocity to the target (the engine lets me obtain the projectile and changes its velocity).  This portion required an extensive use of vector math because I had to make so many adjustments to the bullet's velocity as the game engine simulates bullet drop (which is dependent on the distance) and windage (which requires me to adjust the sway of the bullet).  Fortunately, the game engine already had many math functions such as the cross multiplication of the vectors already built in, so I did not have to create new math functions.  However, I still had the daunting and tedious tasks of finding the golden adjustment constant and calling the appropriate math functions throughout the code.

Now, why did I have to simulate the AI fire instead of making him actually fire his weapon?  There are limitations to the scripting engine of Armed Assault 2.  While I can manually adjust the AI's orientation as a whole, I cannot change the orientation of his weapon.  Therefore, while I can make the unit face its target, I cannot make his weapon face the target.  As a result, the bullets have a large chance of not hitting the target.  In order to fix this problem, I had to simulate firing and redirect the bullets' velocities toward the target.  It was an additional step I shouldn't have had to take, but it was a crucial one nonetheless for my purposes.

The most difficult part of this, however, was definitely figuring out a way to check for the AI's line of sight to his target.  Many modders were unable to figure out a way to do this, so I was left on my own.  I brainstormed for two days, and finally I decided on creating a bullet class.  Programming it was not that easy either.  The bullet traveled so fast that it was really hard to detect when it came close to its target.  But I persisted, and by the end of the day, I had a very reliable line of sight checker.  Designing and brainstorming was definitely the hardest part of this whole ordeal.

After three days of working on the CQB AI, my journey came to an end.  The result is a quite skilled and fast AI that is lethal enough to keep the players on their toes.  The following video is the CQB AI at work:


Now, that is pretty fast!  Note that in both situations, the AI was facing towards me before they spotted me.  At this rate, even the experienced players should be careful when they are going into close combat situations.  There is no more need for placing a bunch of enemies when one skilled AI will do.


No comments:

Post a Comment