StackTips
 25 minutes

Learn to Shoot Game Artificial Intelligence – Part I

By Editorial @stacktips, On Sep 17, 2023 Blog Posts 2.21K Views

This part of tutorial  explains, how to shoot—as true action game bots should. The shooting behaviors are split up into sub-tasks that can be handled independently. A combination of physics and neural network technology produces the ideal balance between effectiveness and realism. This proves to be a surprisingly challenging problem (although equally entertaining).

Motivation

Given a satisfactory solution for movement, the next step to building up a death-match bot is to give it weapon skills. The non-player character (NPC) can handle a variety of lethal weapons, ranging from slow rockets to instantaneous railguns.

After forward and side movement, turning is the most primitive capability of animats. Turning has already been put to use for movement, but tight control wasn’t necessary. Shooting, on the other hand, emphasizes the capability to turn precisely. Together with other simple actions to control the weapon, this will provide an interesting challenge for the AI.

The shooting behavior can be split up into different tasks, including anticipation, prediction, target selection, aiming, and firing. Unlike the previous examples, the shooting behavior requires more than just one function. This behavior will give us a feel for simple architectures using a single-level decomposition.

Combat Settings

Key Topics

  • In the Armory
  • Weapon Requirements
  • Environment Conditions
  • Training Zone

When designing animats to deal with combat, it’s essential to analyze what’s involved in the task generally. Moreover, it’s crucial for the developer to have a solid grasp of the game design and its consequences on the AI.

No matter how great the battle, warfare takes place at an individual level. This chapter provides an informal description of the concept of combat on a personal level, where the objective is to inflict the most possible damage upon the opponent. AI design ideas are built by looking into challenges faced by soldiers throughout the centuries, and investigating the approach taken within existing computer games to model these activities.

In the course of this tutorial we will learn the following topics:

  • The varieties of weapons used in warfare
  • Different weapon types (to abstract out the essence of combat: shooting skill)
  • The role of the environment in individual fights, revealing its importance
  • Design of a test bed where we can train and evaluate animats at one-on-one combat

As game players, principles of combat in games are familiar to us. As game developers, it’s important to understand the dynamics of the design well enough to implement the AI.

In the Armory

There are two fundamentally different types of weapons: melee (or contact) weapons and range (or projectile) weapons. They are generally designed for only one of these purposes—but sometimes both, such as the spear or even the musket and bayonet. Of course, one can physically use any type of weapon outside of its intended purpose, but it will not be too effective!

Instead of discussing the abilities required to use such weapons, this chapter focuses on the physical requirements for them to serve their purpose—and hence what the AI needs for shooting. This section discusses existing computer games and how they model weapons as objects. Different levels of weapon modeling are presented, including conceptual and physical levels.

Melee Weapons

Hand weapons, such as axes and swords, have the advantage of being reusable, a logical progress from throwing rocks or bits of tree. The important aspect of such weapons is the physical contact (naturally)—the impact of the weapon on its target causes harm. Therefore, any concepts that affect this collision (such as velocity, momentum, or pressure) need to be understood by the AI, too.

Computer games can capture the complexity of such contact weapons relatively well, although low-level details of limb control are often abstracted out for playability. This means the player cannot manipulate the weapon freely. Select attacking behaviors are instead provided; clicking the mouse is a poke action, and mouse movement is a slash of the sword—as in role-playing games or first-person adventures (for instance, Jedi Knight 2 and Die by the Sword). The alternative is to press combinations of buttons to control the weapon behavior, as in popular beat ’em up games (for instance, Soul Blade and Caliber). These design decisions can be reused directly to create interfaces to the AI.

Such games require a good model of weapons, because the gameplay relies on them heavily. Generally, however, contact weapons in first-person games do give the impression of desperate fighting, as a last measure before the fatal fall. Because sophisticated combinations of key presses reminiscent of beat ’em ups are quite rare, the skill often boils down to raw clicking speed or unpredictable slashes.

The modeling of the weapon itself is always simplified in games, because full physical simulations are rarely performed in great detail. This is typically just an animation of a weapon moving. The collision detection between the weapon and the target remains crude, for efficiency and simplicity purposes. Some games handle the contact as a Boolean fact; if the weapon hit the bounding box around a character, reduce its health. Simpler models such as these require lower levels of understanding from the AI, resulting in simpler behaviors.

Range Weapons

Rocks and sticks are often popular choices, although technology can significantly improve projectiles. The properties of projectiles (materials, shape) and the way they are thrown affect their trajectory the most. Therein lies the key concept: The object’s flight through the air determines its impact on the target.

Fundamentally, all the computer model needs to do is simulate the trajectory of the projectile. When checking the targets, a simple Boolean hit test is enough, although variable injury models (where individual limbs can get hurt) are becoming increasingly popular (for instance, Soldier of Fortune). The AI needs to be aware of such concepts to perform realistically.

Fairly simple models of projectile weapons tend to work very well (that is, linear flight with no gravity). It’s easier to develop non player characters (NPCs) with an understanding of projectiles—or ballistics. Parameters are easy to tweak for playability by the designer (for instance, precision and velocity). As such, range weapons often prove much more fun than melee weapons. The process of aiming at a distance is both challenging and rewarding, and therefore interesting from the AI’s perspective, too.

As for the process of aiming and firing, the simulation also contains different levels of realism. Depending on the weapon, the player’s view can include wobble or angle restrictions. The timing for the release the projectile can also influence power and accuracy (for instance, arrows or grenades). Finally, the player may have to reload manually. The combination of these properties can contribute to the immersiveness of the game, but conversely can steepen the learning curve. Games tend to handle this very differently, so various restrictions may be imposed on the AI, too.

Weapon Requirements

Modern first-person games usually include many different kinds of weapons. This adds to the players’ entertainment by varying their experience, providing them with more challenges. Different weapons require different abilities from the players—whether human or AI. Obviously, contact weapons require movement abilities to get into proximity of the target, but direct control of the weapon is also required to inflict the damage. On the other hand, projectiles require being fired from a distance while aiming for the target.

Beyond these rough categorizations, each variety of weapon requires different skills—each of which may need handling separately by the AI. For example, a weapon with fast projectiles behaves very differently from a slow one. Each alternate firing mode also requires different abilities. The essence of the combat is the common denominator: the ability to damage the enemy by shooting. This basic ability may be modeled as a reusable component in the AI, from which specific skills are derived.

Other tactical decisions, such as weapon selections and combat strategies just attempt to make the most of these shooting behaviors.

Environment Conditions

Skill with the weapon itself is only part of the story. The layout of the environment significantly influences close combat. Not only does it constrain player movement, but also the weapons and the blast resulting from the explosion (a.k.a. splash damage). Both participants in the fight have to deal with the environment. It affects the attacker and the defender in different ways. The defender can exploit the layout of the environment to hide temporarily, or secure an escape. In general, however, defenders have little room for error; they depend on the terrain, which can make them vulnerable if a wrong decision is made.

The attackers are in the favorable position, because the traps of the environment play in their favor. Attackers can take advantage of splash damage by aiming for nearby walls, and constrain enemy movement with targeted fire (for instance, in dead ends). Interestingly, the roles are almost never as clear-cut. Firing often happens simultaneously, which means each player is partially an attacker and a defender. That said, there is usually a dominant party, so the descriptions apply as trends. This can be especially challenging when the parties involved are in close proximity. Local fire is both very important and extremely dangerous. Both players therefore have to look out for backfire. It can be quite damaging—and embarrassing—to accidentally shoot into a nearby wall. Such pitfalls are quite common in close combat, and need to be prevented by the AI, too.

Training Zone

Having described the important aspects of combat such as the weapons and conditions, we can now define a test bed to develop the AI behaviors. The chosen environment needs to be representative of real in-game situations, because skills developed and learned here should be transferable. By making the training zone a real game level, the chances of this happening successfully are significantly increased.

The focus of the training zone will be medium-range combat, emphasizing aiming skills, weapon handling, and immediate decisions. To improve these conditions, the environment needs to be designed with care:

  1. Frequent targets— The bots learning to aim need to find cannon fodder as often as possible, which will speed up the training process.
  2. Variety of weapons— To offer opportunities for more than one weapon, they need to be scattered throughout the training zone.
  3. Abundance of ammo— In addition, so as not to limit the bot to particular weapons, ammunition needs to be provided at regular intervals in space.

Also, paying attention to such little details will improve the development process and the learning of computer agents. For this, we’ll use a fairly small deathmatch level, with good connectivity and plenty of weapons.

stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.