Earlier today I worked on putting together a basic combat AI for my rpg engine. It was a fun process, though a bit more involved than I thought it would be…

The first step was to determine the basic “thought process” involved in deciding what to do in a combat situation. The results of that brainstorming can be found in a markdown file on my GitHub, though I may delete the file later, so here are the important contents:

When to run

  • When a single attack does damage equal to X% of the maximum HP
    • X should be somewhere in the 33-75% range. Enemies shouldn’t run away too often, but farming weak enemies isn’t encouraged
  • When the opponent’s level is a certain number greater than this one’s?
    • The number should allow for the player to be a bit stronger, but not able to one-shot everything
    • Ideally this level will correspond to a player with some focus on doing the most damage and has a slightly above-average weapon
      • Or calculate the number based on the current state of the player and where points are distributed
      • Calculating would be difficult, considering game devs could include a god weapon…

When to use a restore ______

  • Prioritize items over spells so MP can be used on attacks
  • If HP <= 1.5 enemy’s average damage amount
    • Need a way to make sure the average calculation is not abused…
    • Ignore damage amount if within a certain percentage of a previous attack?
      • Also replace that saved amount with the average of it and the saved amount?
      • Doing so would require keeping track of how often that particular attack has been received

When to use an attack spell

  • If there are no healing items, reserve enough MP for a healing spell or two
  • Otherwise, determine which spell to use based on:
    • Total damage - to kill the opponent in one shot
    • Damage/MP ratio - most damage for the MP spent
    • Doing more damage than the average/base attack (not including the random multiplier)

When to just attack

  • The rest of the time :)

The intent was for this to be similar to my own combat strategies in games and something sophisticated enough to make the player have to strategize somewhat while in combat with an enemy of approximately equal level - while also discouraging annoying gimmicks like farming for XP. Even though this is being created as an engine that others can use, I have a particular vision for what kinds of games will be made for it: games that tell a story, not constantly force the player to remove themselves from the story to focus on leveling up enough to survive.

There are also plans for developers to be able to override some of this behavior, particularly the running away part. Some enemies would not run away from a battle, even if it means certain death, and that should be able to be specified. It has not been implemented yet, but it will be.

The next step is to test this “AI” out in various combat scenarios and make sure that it makes the best decision for the situation it is in.

If you’ve got any ideas or improvements on this model, stop by the project’s issue tracker and let me know what you’re thinking, as well as why it is an improvement. Make sure to use the “Combat” and “enhancement” or “idea” labels if you submit an idea. You can also look at which issues have those labels - please do, so you can make sure you aren’t adding a duplicate. If you want to add to the argument for an existing improvement/idea, add a comment to the existing issue instead - even if the issue was closed as “wontfix.” I am willing to revisit closed ideas if there are improved arguments.

Comments