Simple Combat Logic

    In game development there are two kinds of problem solving. The first is on the programming side and the second is the logic of the MOB’s AI. When it comes to the logic of the MOB both are used but it is important to understand a few things prior to delving into developing a logical AI. The developer has to layout some logical paths for the mob. To do this the MOB must have an understanding if itself, the environment, and its goals. Most AI’s have a simple goal to survive by killing the opponent. But how the MOB achieves that goal is different depending on its own AI. Games will often use the same AI for every MOB which will simplify the development process however other games allow for different AI’s depending on the current state of the MOB or the MOB’s own personality. To illustrate how this process the following explanation will breakdown the basic combat model. There will be two different combat types used within the following method which will be a melee and a range attack. These are the most common ones seen within games however there may be others available depending on the game being designed. The following steps should be used regardless of how simple or complex the AI is since they lay the foundation for the eventual code. It is important to remember that rushing the development of a complex process, such as combat, can detract from the game’s appeal. So follow the steps and think them through and do not worry about spending a lot of time on each in the end it will be worth the effort.

  1. Define statistics
  2. Choose skills
  3. Select goals
  4. Write scenario cases
  5. Outline logic
  6. Develop test cases
  7. Write code

    For this example there will be two different MOBs created. The first is going to be a melee while the second is a ranged. The following table will breakdown their statistics skills and goals. The Attack (ATK) score is the unmodified damage that the MOB can inflict. Defense (DEF) score is how much damage can be negated. Health Points (HP) is how much damage the MOB can take. Attack Skill (AS) is the type of attack the MOB will try to make and the percent is the chance of success. Defense Skill (DS) will be used to evade or absorb the attack and the percent is the chance of success. The goal in this case is the range that the mob will attempt to maintain from the target. In the two examples below BOB will try to stay within bow range while MARRY will try to stay in sword range.

Statistic BOB (Melee MOB) MARRY (Ranged MOB)
Attack (ATK) 5 5
Defense (DEF) 3 4
Health Points (HP) 10 10
Attack Skill (AS) Bow (60%) Sword (30%)
Defense Skill (DS) Dodge (20%) Block (50%)
Goal to survive Keep target at a distance (range 5-10m) Rush into target ( range 0-5m)


    The next step is to define the scenarios for the different MOBs in some cases there are different things to take into consideration such as goals. However, there are also aspects that may be common to a larger group of MOBs. These are the first aspects that need to be developed because they will be common to all characters. Once they are defined then the variants can be created. To do this it is important to understand that mobs have similar statistics but will also have differences. The two MOBs detailed above are primitive by game design standards but they exhibit common statistics which will be used in most modern games.

    The first scenario that needs to be defined is how the combat system will work. For the example both mobs will have the same Rate of Fire (RoF) which is the time between attacks. So how is it determined if the target is hit or if the attack is dodged? To do this the random number function will be used to create a number from 00 to 99. This example will not use critical hit or miss modifiers which can be used in a more complex model. For now it is important to know they can exist. For the scenario the AS and DS specified on the table will be used at all times. For an attack the random number generated must be under the percentage provided in the table (60 for BOB, 30 for MARRY). If this is done then the attack is good enough to hit the target. At this point the defender must roll to see if the defensive skill is successful (20 for BOB, 50 for MARRY). If the defender successfully rolls under the DS percentage then the DEF rating will reduce the attackers AS damage and the remaining damage is deducted from the HP. If the defense roll fails then the defender deducts the whole amount from the HP. In basic combat models the two attackers will alternate between attacking and defending until one of them falls in combat. Once one of the two opponents reaches a HP score less than zero that MOB is declared the loser and the surviving MOB is the winner.

    This seems to be an overly simplified process however it is the basis for all combat within games. Some games rely on the computer’s ability to roll random numbers while others may rely on the player’s personal ability and skill. But for now the computer will remain in control of the combat and logic.

    The last thing that needs to be understood is the goals of the different MOBs. Since BOB is a ranged character he will try to keep the target at a distance. This improves his survivability since a melee target like MARRY cannot attack at a range. However MARRY will always try to get in close to use her sword. This seems like a simple thing however there is a bit of logic required for the MOB to know when to advance, retreat, or remain still. This is where the range distance comes into play. A ranged MOB in this scenario will try to keep its target between 5 and 10 meters while the melee MOB will try to keep the target between 0 and 5 meters. If the target mob is outside of these ranges the attacker will attempt to get them within the range before attacking. If the target is under the minimum the attacker will backup and likewise of the target is farther away than the maximum range the attacker will close the distance. The logic might be something like this for the target range.

  • Less than minimum – retreat
  • Greater than maximum – approach
  • Target in range – attack

    In a more complex solution there may be different options such as changing attack types, healing or using other abilities. However for this scenario the simple solution demonstrates how the scenarios can be created. Once the logic is to this point the scenarios, logic and test cases are defined. The only thing left is to write the code that will actually handle the above defined logic. The following psudocode can be translated to any language.

function attack(target) {
     if (target.range is between my.minimum and my.maximum) {
          hit equals random(0, 100)
          if (hit is less than my.AS) {
               def equals random(0, 100)
               if (def is less than target.DS) {
                    block equals target.DEF
               target.HP equals target.HP minus (my.ATK minus block)

function action(target) {
     if (target.range is less than my.minimum) then retreat()
     else if (target.range is greater than my.maximum) then advance()
     else attack(target)

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: