Search code examples
c#path-finding

game character find fastest way to target (pathfind)


I am programming a little game, like dwarf fortress in C#. I have an array, with objects, these do have a property, indicating wheter the object is passable or not, meaning you have to walk around it. I want now to give the player the oppertunity to say that a dwarf should walk from the Position x,y to x,y. But this dwarf has to find the fastest (or at least one) way to go from the current location to the desired location. I just don't know how to implement this feature, that dwarfs will automatically find a way from their current location to a desired location. Because it could be that they have to pass a long hallway with many curves, and this hallway maybe also goes back and then forward again and after the hallway there is maybe free land, but then it has to pass again a difficult hallway to the desired location. I know it is not described very well, because english is not my mature language and it is really difficult to explain what I mean. But just ask me if you want some more information, i will do my best.

So now again summarized: I want to have a method which finds a way from one location to another, no matter how strong the way ist, even if it is a maze, it has to do it. I mean, this i programmed in most of the games, like settlers and dwarf fortress etc.


Solution

  • The Answer is to use the A* (A Star) Algorithm