LoginLogin

enemy AI which would go through a maze and find the player

Root / Programming Questions / [.]

ChaseCZCreated:
Yeah i probably said everything in the title... I have an idea of how it should work but no idea of how to code it lol. Any help super appreciated

What you are referencing to is a path finder. While I've never successfully made one myself, I do know some articles that can explain the process. Like this one http://www.cokeandcode.com/main/tutorials/path-finding/ It is much easier if you are using a grid, but if you aren't, then you are in for a long ride.

What you are referencing to is a path finder. While I've never successfully made one myself, I do know some articles that can explain the process. Like this one http://www.cokeandcode.com/main/tutorials/path-finding/ It is much easier if you are using a grid, but if you aren't, then you are in for a long ride.
Yes, that's a good explanation, now i know how it basically works, but to make it work in Smilebasic, i'll need to start up my brain lmao Thanks

Also how would i take wall collision into consideration?

For the pathfinder or the player?

For the pathfinder, so it doesn't bump into walls and actually finds at least some logical path to the player

Are you using a grid? If so, then you would detect if the space is empty or not. If it is empty, advance, else, find somewhere else.

What do you mean by grid? Lol

Like this:
@grid
Data "11111"
Data "10101"
Data "10101"
Data "10001"
Data "11111"
[/copde]

Oooh you mean a data map. Yeah, i won't be using that.

What are you using then? An open area? Streets?

I'm thinking about a top down (not with tile movement though) or a 2d map. Haven't made my mind yet but i think i'll be going with a 2d "room-to-room" game where (as the name suggests) you're gonna go from room to room, from door to door and i want the enemy to find a path and go through the doors too. I think it's actually gonna be easier than the top down game, which would be similar but with bigger rooms. I don't really wanna spoil anything yet.

I'm not sure how to help you. Time-based maps pretty much the easiest(and least expensive to ram) to do.

I don't understand what you are trying to mean. Do you have a map top down where you can see the maze around the player and walk around in it? How will this maze work?

If you post a picture, we can better understand what you are asking

If you post a picture, we can better understand what you are asking
yes that's a good idea

It sounds like you want to investigate the A Star or A* algorithm. It works well on a grid but is really more of a graph traversal algorithm. Moving from point to point has a cost so you can make going up a hill more expensive than a flat area. If you want impassable areas don't mark a position as a traversable node on the graph. It starts out like Djikstra's search where you recursively evaluate nodes until you find a path to the target. However in A Star you have a heuristic function, so that you explore nodes that are better possibilities first. Once you find the target you backtrack your path. If your heuristic never overestimates you should have an optimal path. With a junk heuristic you devolve into Djikstras search performance. This isn't a cheap operation so you should only really run it sparingly, not every frame. As for being followed from room to room, you may just want to put the enemy on a timer and fake it. Only doing another search when the time says they should reach the room the user is in.

It sounds like you want to investigate the A Star or A* algorithm. It works well on a grid but is really more of a graph traversal algorithm. Moving from point to point has a cost so you can make going up a hill more expensive than a flat area. If you want impassable areas don't mark a position as a traversable node on the graph. It starts out like Djikstra's search where you recursively evaluate nodes until you find a path to the target. However in A Star you have a heuristic function, so that you explore nodes that are better possibilities first. Once you find the target you backtrack your path. If your heuristic never overestimates you should have an optimal path. With a junk heuristic you devolve into Djikstras search performance. This isn't a cheap operation so you should only really run it sparingly, not every frame. As for being followed from room to room, you may just want to put the enemy on a timer and fake it. Only doing another search when the time says they should reach the room the user is in.
Yes, the timer would be a good solution... But not the best, because what if there are multiple doors in the room where the player is, how would it determine at which door it would appear?
If you post a picture, we can better understand what you are asking
yes that's a good idea
And as for the pictures, i have prepared some poorly-made paintings on my phone but my mobile data are slow af and it won't upload, so i'll send it here as soon as i connect to wifi somewhere

Store the door in a variable.

I would calculate the door to enter from with the Pythagorean Theorem. For a right triangle with sides a, and b and hypotenuse c it states that a^2 + b^2 = c^2. Let x1, y1 be the location the monster was (globally) at when you left them. Then for each door in the current room let x2, y2 be the (global) location of the door. dx = x2 - x1, and dy = y2 - y1 so calculate dx*dx + dy*dy and use the door that has the smallest resulting c^2 value. All that being said, having a monster chasing you from room to room is pretty non-standard game behavior. I can't think of many games that do it, as it is pretty brutal. So I would of course use it very sparingly.