Battler/Troops AI

Hi guys,

I'm into making some of the battles in my game more interesting. I want to make some troops strategy or individual battler's behaviour.

I would need some command like:

certain battler attacks a hero with...

be it:

<enemy 1> attacks <hero with lowest healt> with <spell 1>

or

<enemy 2> defends if <certain hero> is in pary

or

<enemy 2> attacks <random hero> with <normal attack> if <enemy 1> is alive

I cannot remember to have found a script for this while browsing the script list a month ago. Is there a script which can provide me with commands like those and if there isn't how can I do those things without a script?
First and last try to bump it...
You could use troop events, switches, or skills that call common events with force actions to do things like that.

For example, let's say you have two enemies, and you only want Enemy A to use Skill A while Enemy B is alive - let's say Rat only uses Scratch while Slime is alive. In Rat's behavior, you give his Scratch skill the condition "Switch: SlimeAlive is ON." Then, in the troop where you fight Rat and Slime, you make an event page that's set to "turn 0," once per battle, that turns the switch SlimeAlive on. Then, you have a troop event page whose condition is, "Slime's health is 0% or less," set to check every turn. On that page, you turn the switch SlimeAlive off.

What this will do is check every turn if Slime is still alive - if so, then Rat is capable of using Scratch. If you want Rat to ALWAYS use Scratch while Slime is alive, then just make his skill a 10 rating and have none of his other skills higher than 7. Once Slime dies, the switch is turned off, and Scratch is no longer available.

You can do the same thing for checking if an actor is parrying. Make the defend skill conditioned by the switch "ActorParry," then have a troop event page that, every turn, checks if the actor has the Parry state on them, and if so, turn on the switch. If not, turn off the switch. If you want them to still occasionally defend sometimes, then give them the defend skill twice - one is conditioned on the ActorParry switch, but is rated at 10 - the other one is rated at 7 or lower, with no condition.

For attacking a specific target, you could have the skill call a common event that forces the enemy to use a particular skill. So, let's say you have an enemy called Executioner who uses a skill called Execute that always goes after the lowest-health party member. First, make two skills - CallExecute, and Execute. CallExecute will do nothing except call a common event, and Execute will do whatever you want the skill to do. You will give the enemy the skill CallExecute, but not Execute - it's simply a skill that will be called via Force Action.

Create a common event called Execute. Then have a script call that looks like the following:

actors = $game_party.battle_membersactors.sort! { |x,y| x.hp <=> y.hp }if actors[0].hp>0$game_variables[1]=actors[0].indexelse if actors[1].hp>0$game_variables[1]=actors[1].indexelse$game_variables[1]=actors[2].indexend;endActors is just a variable, so you could call that anything, but what the first line will do is store all the actors currently in battle. The second line will then call an array of all those actors and sort them according to their HP, organizing them from lowest to highest.

One thing to consider, however, is that if someone is dead, they'll come back as having 0 HP, and you don't want your enemy to try and target a dead party member. So, you'll want to add some extra ifs to that script call equal to the number of party members you can have. In the example above, there are a possible 3 party members you could have. So, once you've established who has the lowest HP that's still greater than 0 (or, who has the lowest HP that's still alive, in other words), it will store their index as a variable - in this case, variable 1, which we'll call LowestHP.

Now you do a conditional tree that checks if variable 1 (LowestHP) is equal to each possible index in your party. Within each, you then have the Executioner use a Force Action of the Execute skill on the appropriate index. So, for example, let's say your party can have up to 3 members:

conditional branch: LowestHP == 1Force Action: Executioner, Execute, Index 1elseconditional branch: LowestHP == 2Force Action: Executioner, Execute, Index 2elseForce Action: Executioner, Execute, Index 3So the skill will call your common event, check who has the lowest HP, then make your enemy use the skill on the target with the lowest HP.
Great advices, Zevia.

I think I may have used some of those techniques but for other purposes. I'll gladly try to make some harder battles and if they work, it will be awesome. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/battler-troops-ai.45140/