Pathfinding

Pathfinding

In the Twitch Dungeon Game (which still needs a name, by the way), the player enters 18×10 tiled rooms. These rooms contain enemies, and these enemies’ need to move in a couple certain ways:

  • Melee enemies move towards the player until they are within reach.
  • Ranged enemies move towards the player until they are within shooting range. Additionally, they retreat if the player becomes too close.
  • Enemies do not collide into each other or other objects.

Let’s start with the melee enemies. Just make them move in the direction of the player’s position, right? Eh, it’s more than that. You see, what if there’s something in the way, like a wall? They’ll just end up bumping into the wall, never passing it, if the player is directly on the other side. We need our enemies to be smarter; give them some intelligence. Some artificial intelligence.

Last semester, I took Introduction to Artificial Intelligence. Didn’t really know what was going on towards the end. However! I do remember learning about the A* pathfinding algorithm (that was towards the beginning).

Honestly, it’s not too crazy. Essential you use a function:

f(n) = g(n) + h(n)

So, of a path, n would be the next node, g(n) would be the cost from the starting node to n, and h would be an estimate of the shortest path to the goal from n. Why not just use h to find the goal? Well, one reason is that nodes can share the same h, and that can create an infinite loop of going back and forth between those nodes with the same h values.

To implement this algorithm, I contacted my good buddy Sebastian Lague (AKA watched his A* tutorial series). I’m still going through it, but it’s been very helpful in learning A* again.

Once I get melee enemies done, ranged enemy following would of course be very similar, but I’d have to figure out a method for retreating. Maybe move towards the farthest node? And the collision issue- perhaps I’ll mark enemies the same way as inaccessible areas, but then that could block off paths that other enemies could take after the first enemy goes through them…

Anyways, a lot to think about and a lot to learn. Excited to see my progress (and my teammates’!) as the project goes on!

Leave a Reply

Your email address will not be published. Required fields are marked *