simple enemy AI

Started by
4 comments, last by Concentrate 13 years, 3 months ago
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.
Advertisement
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.
This is educational:
http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior
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.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Quote:Original post by Concentrate
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.
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).
Quote:Original post by jyk
Quote:Original post by Concentrate
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.
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.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

This topic is closed to new replies.

Advertisement