# simple enemy AI

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

You guys know of good resources for beginning AI? My first goal is just to get monsters moving around in a randomish way. I'm making a little tile-based Legend of Zelda project and I just want monsters to wander around.

##### Share on other sites
A good place to start with ai is with astar path finding. You probably want your monsters to avoid objects while they wander around. You can select random positions on a grid map and later have them finding food or enemies, etc.

##### Share on other sites
This is educational:
http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior

##### Share on other sites
To get that kind of movement its a simple algorithm:
void updateEnemyPosition(Enemy& e){ Direction direction = randomDirection(); if(e.canMove(direction)){//canMove simply checks if the current enemy position + direction has a valid walkable tile    e.moveUnit(direction); }}

And you simply update enemy position every now and then.

##### Share on other sites
Quote:
 Original post by ConcentrateTo get that kind of movement its a simple algorithm:void updateEnemyPosition(Enemy& e){ Direction direction = randomDirection(); if(e.canMove(direction)){//canMove simply checks if the current enemy position + direction has a valid walkable tile e.moveUnit(direction); }}And you simply update enemy position every now and then.
That algorithm is unlikely to produce suitable results. Unless there's more to what you have in mind that what's shown above, the algorithm will most likely produce jagged, discontinuous paths that tend not to get anywhere (or if they do, do so slowly and inefficiently).

@The OP: You're likely to have better luck with either a pathfinding-based solution, or with something like steering behaviors (e.g. wander + obstacle avoidance).

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by ConcentrateTo get that kind of movement its a simple algorithm:void updateEnemyPosition(Enemy& e){ Direction direction = randomDirection(); if(e.canMove(direction)){//canMove simply checks if the current enemy position + direction has a valid walkable tile e.moveUnit(direction); }}And you simply update enemy position every now and then.
That algorithm is unlikely to produce suitable results. Unless there's more to what you have in mind that what's shown above, the algorithm will most likely produce jagged, discontinuous paths that tend not to get anywhere (or if they do, do so slowly and inefficiently).

@The OP: You're likely to have better luck with either a pathfinding-based solution, or with something like steering behaviors (e.g. wander + obstacle avoidance).

Your right, I was just trying to answer the OP question with a basic algorithm to move randomly, but actually there is a slight modification that one can make on that algorithm to make it more stable like so :
void updateEnemyPosition(Enemy& e){ Direction direction = randomDirection();int step = random(1,MAX_STEP); for i = 0 to step   if( e.canMove(direction) )      e.moveUnit(direction);}

that should be a little better but there are algorithms out there that are much better than this. Above is just a starting point so you can have something on the screen.

##### Share on other sites

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984334

• 23
• 11
• 10
• 13
• 14