Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this