====== Grid: Enemy AI ====== This section explains how the enemy decide their action. ===== Prelude ===== The enemy's artificial intelligence is always a challenging part of any system design. We can probably design a good system with many features, yet it may fall apart because the AI is not that good. This grid battle system is no exception. Especially when the goal is to make this script is used for the public while not introducing a complex AI setup. Over the course of the years, I finally nailed the basic design of the AI that (hopefully) not feel underwhelming. [{{https://i.imgur.com/BgYu3Gi.gif|Choosing a target be like}}] ===== Default RPG Maker: Enemy action ===== To make it less complicated, I decided to keep the default action in RPG Maker while integrating my own AI. But if you don't know how the default action works, I will try to explain how it works under the hood. ==== How action is filtered ==== Consider you have this list in your enemy action pattern list [{{https://i.imgur.com/m8IHul1.jpg|Default action}}] Let's assume that three of those skills are conditionally valid. And by valid, it means: * The enemy has a sufficient resource (for example, have enough MP to cast the skill) * It is conditionally valid (turn, HP trigger, MP trigger, switch, etc) * It has a target to attack (the range reached at least one target or allow to target an empty grid) With those conditions, let's also assume that turned out these three skills met the criteria above. [{{https://i.imgur.com/HBTGrui.jpg|Filtered action}}] Respectively, they have their own rating in 1, 4, and 5. The system recognized that 5 is the highest rating. Then the system ruled out anything beyond 5 subtracted by 2, which is 3, which means that we only have an action that has a rating from 3 to 5. Now we only have two actions from the list. Both have rating 4 and rating 5 respectively. How the system decides to pick one of those actions is by using a [[https://en.wikipedia.org/wiki/Fitness_proportionate_selection|roulette selection]]. And we will be using the same algorithm to select a target later. If Wikipedia is too complicated for you, here is the simplified version. In short, the chance of each action is taken is formulated in this formula Chance = Individual Rating / Total Rating As you can see since we only have two valid actions. it makes that the total rating = 9. That makes each of the actions has ''4/9'' and ''5/9'' chance to proc. Pretty straightforward. ==== How target is picked ==== This page is under construction