Construct 2 RPG Series #6 – Mages AI

Construct 2 RPG Series by Aryadi Subagio (@AryadiPS):

Welcome to my tutorial of making an RPG. This is the second part of designing enemy AI, the mages AI. Making AI for mages isn’t really much different than AI for physical attacker, there’s only little changes. First we need to remember that there are two kinds of mages, the offensive mage that attacks enemies with destructive magic, and the supportive mage that gives healing or any other supportive magic to allies. Both have their own logics.

First, let’s design the AI for offensive mages, the logic flow is like this:

  1. Select target
  2. If the SP is enough to cast at least one magic, choose a magic
  3. Enter casting time
  4. If attacked by enemy, the magic is cancelled
  5. If casting time is finished, reduce the SP and cast the magic
  6. If target is still alive, back to no. 2
  7. If target is dead, back to no. 1
  8. If don’t have enough SP, attack physically.

The last point is needed so that the magician don’t just stand helplessly on the battle screen. The logic for selecting target is the same as physical attacker, we’ll be using instance variables enemySelection and targetName. If you forget what those are, please see my previous tutorial, it’s written there so I’m not going to explain it again.

Next, the mages will select which magic he/she will cast. For now I’m making the enemy select the magic randomly from the list of magics that are available to him in one battle. Because I don’t know how to make the AI to choose a skill/magic over the other, when I know how to make it, I’ll write a tutorial about it. After choosing their magic, the mages will enter a casting time, while casting the mages can’t walk or attack or do other things. If you still remember the state instance variable, we will change the value of the state variable while the mages are casting the magic. All magics have an instance variable called castTime that determines how long the casting time is in seconds. If the mages are attacked, we will change the state so we know that he’s not currently casting his magic. If the casting is finished uninterrupted we will reduce the SP and then execute the function that will cast the magic. I’m planning of making a family that contains all available magics and have the necessary instance variables and functions, including the function that will cast the magic.

Now on to the supportive mages AI. These mages have two jobs: to heal the wounded party member, and to give magics to temporarily boost stats. So the flow of logic is: if there’s a friend needing healing heal him, if not then give supportive magics.

  1. Look other party member’s current HP, find if there’s someone need healing
  2. If there is, choose a healing magic
  3. Enter casting time
  4. If attacked by enemy, casting time will end, and magic won’t be casted
  5. If manage to cast the magic until finished, reduce current SP and cast the magic
  6. Back to Point 1
  7. If noone need healing, choose a supportive magic
  8. Choose a target for the magic
  9. Enter casting time
  10. If attacked, casting time will end, and the magic cancelled
  11. If manage to cast the magic, reduce current SP and cast the magic
  12. Back to point 1

Before executing this AI, we need to check whether the current SP is enough to cast at least one magic, if unable then the mage will do physical attack.

The definition of  “a party member needs healing” is interesting, and we can customize this to determine how often a magician will heal his or her party member. I’m gonna divide this depending on how much is the other party member’s current HP. Just like usual I’m gonna make an instance variable of it, this time I’m naming it healMethod.

  1. Heals when other party member’s current HP is below 75% = “worries”
  2. Heals when other party member’s current HP is below 50% = “moderate”
  3. Heals when other party member’s current HP is below 25% = “critical”

with this we can customize party member’s (or enemy’s) healing AI for the healer, because I want the AI customization to be a little bit advance. One more note about healing magic: there will be healing magic that heals more than one party member, and to use it healers must know whether the wounded party member is only one, or more than one. For this reason we’re gonna make an instance variable called woundedNumber. If the value of woundedNumber is more than one then healers will use healing magics that heals multiple party members at once. This value will be set back to 0 after each use of healing magics.

For supportive magics, we need to mark whether a character’s (or enemy’s) party member has already casted by a supportive magic or not, so that the magician will focus on supporting a character that hasn’t been casted by a supportive spell. We’re gonna make two kinds of slots, for buff magic and for debuff magic. These slots are instance variables in the name of: slot1Buff, slot2Buff, slot1Debuff, slot2Debuff which contain the string of name of buff/debuff magics casted on the player.

That’s it for now, next time we’ll start on implementing this AI.