[BEHAVIOR] Simple Enemy AI (Update: was Ghost AI)

Post your work in progress addons and get feedback

Post » Sat Jun 24, 2017 7:56 am

Update:
I changed the name to 'Simple Enemy AI'. I think that is more appropriate.

Description:
A behavior with 6 different modes for chasing or following a target. It based on the 4 different Ghosts of PacMan, so it's a very simple AI. There are only a few things you can adjust.


Enable or Disable the behavior.
There are no stop or move actions, you have to use 'set enable' as an action.

Speed
How many pixels in one second the object moves.

reset direction
This is only an action. Normally the sprite (enemy) would never move backwards. The only exception is if it stuck on a dead end. This should avoid that the enemy is jitter from one direction to another.
With reset direction you can force to recalculate the directions inclusive backwards. This can be helpful if your enemy should flee for something (like the PacMan Ghost when PacMan gets a pill). It could be very bad if the enemy have to move to the next cross-way , before change the direction.

get direction
The direction the sprite is moving at this moment.
1= up; 2=right; 3=down; 4=left;
Helpful for the animation of the sprite.

getpx, getpy are for debug ignore them.

chasing mode
For each chasing modes you have to set different links to other objects. This are all actions that you should put in 'On start of Layout'.
All need a Tilemap with a path of tiles to follow. It's up to you how the path looks and the size of the tiles, but this has a big influence of how good the AI works. The tiles have not to be transparent or on the top layer. You can also put the layer as bottom layer. There is no need that this layer is visible at runtime. If you put at at the bottom this could have a positive effect to the performance if you have a really high resolution and a big tilemap.

The other objects are all sprites:
Target - The one that should be attacked.
Hideoutt - It's like the second Target, but more for retreat.
Cohunter - It's only needed for the encircling mode, best is to use a sprite with the follow mode.
I recommend to use not an instance of the main enemy sprite but rather a new sprite-object.
It will only choose the first instance of objects, so it could be possible that the cohunter is the same instance like the main hunter (enemy).

The modes are:
retreat and follow
This are practical the same.The enemy follows direct their target. The difference is that you can give each mode a different target, then you can switch targets at runtime. You could do the same if you switch the target in one of the modes at runtime. It's up to you how you do that.
Needs:
A Tilemap and a Target.

ambush
Tries to get in front of the target.
Needs:
A Tilemap and a Target.

encircle
Needs a second sprite as a 2nd hunter and tries to encircle the target.
Needs:
A Tilemap, a Target and CoHunter.

shy away
Will follow the target, but if it get too near (6 tiles in every direction) it will flee to his second target.
Needs:
A Tilemap, a Target and hideout.

guard
The opposite of 'shy away'. Guards a target by circling around it. If an sprite comes too near (6 tiles in every direction) it will attack this sprite as long as the sprite is in 6 tiles range.

This behavior allows only 90° turns.
If you forget to link a needed object for the mode the sprite won't move.

This is a first beta. It's highly possible that this behavior causes crashes. So be very careful and don't use it in big projects. It's more for testing at the moment.

In the example you can see how to use the behavior.
Put the behavior (only 'simpleenemyai') in: Program Files\Construct 2\exporters\html5\behaviors

https://drive.google.com/uc?export=down ... 0pJWUstaGs











obsolet:

A behavior with the 4 AIs of the 4 different Ghosts of PacMan: Blinky (red), Pinky(pink), Inky(light blue/turquois), and Clyde (brown).

It uses a tilemap as path for chasing a target. Each Ghost has a different way to chase his target.

Blinky:
Chases his target direct.
Pinky:
Tries to perform an ambush.
Inky:
Inky uses Blinky to encircle his target.
Clyde:
Chases his target like Blinky. If he comes to close to his targets, he flees.

This based all on the work of Toru Iwatani in 1980. It's not really an AI. All Ghost have more or less a simple behavior to chase PacMan. So it looked more like chaos than really an intelligent chase. The advantage is the very low cpu usage.
A made a little demo to show how the ghost are chasing PacMan. PacMan hasn't really an intelligent behavior. This will only show how the ghost chases. So no collision detection is implemented.
https://www.scirra.com/arcade/other-gam ... test-19126

At the moment it's in a very early stage, so no download link. If is there any interest in this behavior I will made a usable version.
Last edited by Asmodean on Sat Jul 01, 2017 9:18 am, edited 1 time in total.
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Sat Jun 24, 2017 10:17 pm

This is very cool. I'm looking forward for this
B
31
S
5
G
3
Posts: 32
Reputation: 4,446

Post » Sun Jun 25, 2017 10:57 am

Thanks,
I recognize that the name is a little bit misleading- This behavior will not only for a PacMan-Game. It should work for all maze like games where you can build a closed path with a tilemap and enemies who chases you.
It has not even a top down game. A game with a side view, what has ladders or something like that would do also work.
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Sun Jun 25, 2017 11:05 am

Many users ask for help with AI so this would be a much welcomed addition. Nice work.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
134
S
30
G
84
Posts: 5,387
Reputation: 58,456

Post » Mon Jun 26, 2017 7:45 am

Will it be able to detect sprites as obstacles or is it only for tilemap?
B
31
S
5
G
3
Posts: 32
Reputation: 4,446

Post » Mon Jun 26, 2017 9:34 am

Will it be able to detect sprites as obstacles or is it only for tilemap?


At the moment there is no detection of sprites or obstacles. You have to make a tilemap with tiles as a path for the sprites and they will only move on this path depending on the mode you have chosen.
Here is the tilemap from the PacMan-Example. This are all transparent tiles. The red boxes are only the collision polygon.

Image

So you can simply make a tilemap with transparent tiles and put it above your game. That the enemies behave more or less intelligent also depends how good you chose the path not only one the Behavior-plugin.
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Thu Jun 29, 2017 3:11 pm

Oh yeah, I'm ashamed I didn't think of that in the first place. So you mean that they actually follow the tiles and not going around them? So, maybe if I have a layout full of static objects, I will be able to generate an invisible functional tilemap that will erase all the tiles at object positions? Will that work?
B
31
S
5
G
3
Posts: 32
Reputation: 4,446

Post » Thu Jun 29, 2017 4:53 pm

RadioMars wrote:So you mean that they actually follow the tiles and not going around them?


Exactly, you made a path out of tiles and the enemy will follow this path on the tiles. Independent of where the target of the enemy is, it will always follow the target on the tiles. If the enemy is not on a tile it won't move.

So, maybe if I have a layout full of static objects, I will be able to generate an invisible functional tilemap that will erase all the tiles at object positions? Will that work?


I don't know if I understand this question correct.
If you would fill a complete tilemap with tiles and only delete on the position of the obstacles the tiles it should work. But the enemies would turn only in 90° steps. So it would look a litte silly and I don't know if there are other side-effects.

Maybe I do a 'Pro'-version with 45° degree steps and more individual settings, but at the moment I would be happy if I get this behavior to work. :)
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Sat Jul 01, 2017 4:14 am

Oh yeah, I see. Sign me up for pro verson :mrgreen:
B
31
S
5
G
3
Posts: 32
Reputation: 4,446

Post » Sat Jul 01, 2017 9:23 am

Try the first beta. :)
Link in first posting.
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Next

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 1 guest