ENEMY AI IN SCORPION ENGINE PART 1

Over the weekend I completed a first iteration of the enemy movement and attacks. Coding this in the Scorpion Engine is very different from doing it in C, but many of the concepts remain the same.

The whole thing works as one big code loop, so the enemy is always thinking about their next move.

Initilaise

First I initialise the enemy, in here I simply spawn their legs (see previous post about this) and then run the “Idle” code block.

Idle

This is where we return the enemy back to their default settings, in our case we set their Var1 (which controls whether they are attacking, on a ladder, being hit etc..) We set this back to normal which is 0.

And then we trigger the AI codeblock.

AI

In here we first check that we are in a state to make an AI move (eg. if the enemy is being hit they do not need to run this code)

We then check the distance on the X axis to the target (the player).

Check Distance X

First we check if the enemy is standing too close and should reposition.

If not, then we check if the enemy is outside of the attack range, if so we move the enemy using “Approach”.

And if they are in the perfect attack range we then “Check Distance Y”

Check Distance Y

If they are too far away we let the enemy “Approach”.

Else the enemy is within range on both X and Y axis, meaning they have a chance to attack.

Attack Chance

We now grab a random value, in Scorpion it’s called rolling the dice. If it lands on 1 I say we can “Attack”, otherwise the enemy is told to wait 10 frames (0.2 seconds) before being sent back to the “AI” codeblock above.

Attack

Here we can tell the enemy to select an attack type, again we can do a probablity roll of the dice to determine whether they should prefer one attack over another.

Approach

As mentioned above sometimes the enemy is asked to “Approach”, we can use the Scorpions inbuilt “move_atplayer” which looks really good.

We then ask the enemy to go back to “AI” after 5 frames (0.1 seconds).

To Conclude

So by returning to “AI” the enemy is constantly reassessing it’s choices, which makes for some nice AI. This iteration is not perfect, but it gets us pretty close to an enjoyable game fairly quickly.

Things to do next will be to get the enemies to consider which side they want to attack as right now they choose the nearest side. And if approaching from below they always place themselves ontop before moving to the attack range, so this will need fixing too.