AI trouble sdl 1.2

Started by
5 comments, last by Alberth 8 years, 2 months ago

Guys, I started working on this pacman game again. My ghost1.follow() function is so ugly, it's unreal. I need some pseudocode ideas on how the hell am I supposed to make my ghost follow pacman like a normal ghost. Any ideas are appreciated. ( if code is not too ugly )

Advertisement

http://mentalfloss.com/uk/games/31287/the-different-strategies-of-each-of-pac-mans-ghosts

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

Yeah, I know this. There is an even better link, you just click over the different ghosts and see what they do. http://pacman.shaunew.com/play/index.htm#learn
But how exactly to make the follow() and scan_tiles() codes pretty and efficient, this now is a completely different story.

So the key seems to be to find the shortest path to some point?

Then, given that path, a ghost has to follow it (for one junction, and then recompute the shortest path when you get at the next junction).

If you have that, you can just give the ghost a point to move to, and it will automagically work?


If the above makes sense to you, standard algorithms for finding a shortest path are Dijkstra (the simplest one), and A* (the more efficient one, although at your size of grid, it hardly matters). Resources are several articles here at gamedev.net, and "the Internet".
http://www.gamedev.net/page/resources/_/technical/artificial-intelligence/dijkstras-algorithm-shortest-path-r3872,
http://www.gamedev.net/page/resources/_/technical/artificial-intelligence/a-pathfinding-for-beginners-r2003 and http://www-cs-students.stanford.edu/~amitp/gameprog.html#Paths . Give it a try as separate program, people here know the algorithm, when you get stuck, they can give assistance.


Edit: Maybe you should also write some code to follow some road until a ghost hits a junction (then eg take a random direction). You will need such code, but it's a separate problem from finding the shortest path

What you need is a simple, generic pathfinder which you can feed each ghost. The pathfinder gets fed a ghost's current position, current direction of travel, and the position of its "target". That target is very clearly defined in each ghost's Scatter and Chase states; specific map coordinates, or locations relative to PacMan (including +0,+0 relative to PacMan).

The pathfinder has to respect the ghost's movement rules; they never reverse their direction, they only turn left, turn right, or carry on straight.

You only need to query the pathfinder for a given ghost when they reach a decision point. Once they're on a lane headed towards the next intersection, they can't change their mind anyways, so just let them carry on straight until they can choose to turn again.

Post some of your current ghost AI code, and we may be able to give you specific, pointed examples of how to improve what you're doing already.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

I can't.....Spent 2 days and nothing. I made another class Shade that has very fast speed ( 30 pixels in a frame ) and every time the ghost is in a junction, the shade gets the ghost's coordinates and tries all the possible ways ( it tries one way, then goes back to the junction and tries the other one ), but if there are more junctions it gets hard and I just made it like that: if( ghost is in a junction ) and if pacman is above ghost, go up ) and if pacman is below ghost, go down the junction and it kind of works, maybe its too early for me to go to this AI stuff, its too hard, but the good thing is that I kind of made progress in my rail system, so earlier when Pacman was in a junction I checked every single tile of the map with for loop, now I improved the code to actually check only the 4 tiles surrounding pacman, so now instead of 441 tiles being checked every frame, now only 4 tiles are checked every frame. My plan was to learn basic AI first, then make the game states and learn inheritance and stuff, maybe it's good to leave AI for later, what do you think?

EDIT: I mean I read all the articles Alberth gave me, and I can't really implement them in my game, maybe I should take it slower, learn something easier??

Sounds like good progress.

AI stuff is complicated, although the ghost behavior is not entirely what most people consider AI. It's a state machine with completely fixed (and not trivial) behavior.

Most people see AI as interaction with some form of "smart" knowledge about the surrounding (if a bridge is down, I can use that, else I have to walk around over a rope bridge), their own state (look for food if hungry, medicine if hurt, etc), terrain (don't sleep in flat unprotected land), and so on.

You can spend years to a life time on AI if you want. I agree with you however, for now stick something simple in that you can live with, and move on. You'll find both inheritance and game states everywhere, including in AI related studies, so it's a good foundation for a lot of things.

The Dijkstra and A* algorithms I gave you are quite simple in nature, the problem is likely that they are written in a way that is difficult to understand at first. You have to shift yourself into the mindset of the algorithm, and then all puzzle pieces fall into their place. The whole idea makes total sense, and you cannot imagine how you once failed to understand it :)

Revisiting these algorithms later is good I think, they are quite easy, and they are easy to see visually, which helps in understanding. They are also quite useful at lots of places.

This topic is closed to new replies.

Advertisement