Last week I implemented some rudimentary AI for bots in my game. Currently, they only have two modes of operation. Attack mode, where they beeline towards the closest enemy player, and a wandering mode where they pick a random point on the map and beeline towards that. Of course, I want to make some improvements to these behaviors, but first things first – an explanation of how the bots currently behave.

If I am in attack mode, find the closest enemy player, turn towards that player

If I am hit by an enemy, switch to attack mode.

If I am in attack mode, but I haven’t been damaged in a “long” (5 seconds or so) time, switch to wander mode.

If I’ve been wandering for a “long” time, switch to attack mode.

If I am very close to my target, move backward.

If I am very far away my target, move forward.

Even with these very simple rules, the bots are surprisingly fun to play against. One big issue, though, is that they frequently run into walls that they cannot pass while attempting to seek towards an enemy. Fortunately, they will in fairly short order switch over into wandering mode, and will inevitably bumble around the map and eventually find an enemy to fight. But still, it makes the bots look pretty darn stupid! So, the next step is to add some smarts to the bots so that they would be able to recognize the fact that there might be a wall in between them and their current target.

Any game developer worth their salt knows about the trusty A* search algorithm, which is an optimized / generalized version of Dijkstra’s algorithm. I’m not going to go into the specifics of how to implement this or anything, but it’s important to note that it operates on a graph of interconnected nodes, and provides an optimal route between a given starting and ending node. The point of this post to mostly to go into my method of creating this graph for my game.

After some reflection upon the problem, I realized that I wanted automatically create these pathfinding nodes, since the map I’m currently using is certainly not final – plus, I might want to create new maps (or have players create them themselves) in the future, and I don’t want to have to generate them by hand. This is not as straight-forward as it might be in some games, as my game is not grid based, and many entities are not axis aligned… Heck, there are even some turnstiles that I think I want the bots to avoid like the plague (for now)! So without further ado, here’s how I’m planning on creating my graph:

When the game simulation starts up, have it create the map (as usual). For each x/y coordinate (or perhaps a higher resolution than that), add a “pathing monster” entity that dies whenever it collides with a wall Simulate approximately 5 seconds worth of time using a few synchronous tick() calls… This will end up destroying the pathing entities that were inside of (or too close to) a wall. For each of the remaining pathing entities, find its neighboring entities (only entities that are less than a certain distance away… I will make certain that this distance is less than the minimum width of a wall). Take this information and put it into some sort of pathing node manager object. Omit all nodes that have no neighbors. Destroy all of the pathing entities.

Anyhoo, that’s how I’m approaching the problem. Any suggestions as far as easier methods of doing this? I’m all ears!