Jump to content

  • Log In with Google      Sign In   
  • Create Account

Like
0Likes
Dislike

Knowing the Path

By Richard Fine | Published Jun 17 2002 06:33 PM in Game Design

nbsp node #39 route network move environment algorithm npc
If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

The player is presented with a new environment, having just battled his way past a hoard of enemies. The new environment is a network of corridors and rooms - and throughout the environment are enemies, bonuses, and other interactive items. The player explores the environment, taking out the enemies, collecting the bonuses, and discovering the interactive items.

Now, take two.

The NPC is presented with a new environment, having just been generated by the engine. The new environment is a network of corridors and rooms - and throughout the environment are enemies, bonuses, and other interactive items. The NPC heads straight for the nearest room, because it can see from the nodegraph that there is a bonus there, and then takes a route through the environment that avoids and evades all enemies. The framerate drops a little as the route is recalculated each step, to account for enemy movements. The player watches in disbelief.

Sound familiar? This article is going to discuss a method for avoiding this: something I call an 'expandable path table.' It allows NPCs to both navigate through an environment, and to explore and learn it, rather than being presented with the entire thing. It's also faster than constantly recalculating a route.


Before we start

You'll need to know a pathfinding algorithm. I used Dijikstra's algorithm when researching - but the A* algorithm, or any other algorithm for finding a path through a network of nodes, will do fine.


The Problem

Attached Image: ktp_image001.jpg

The above network represents my level. The nodes are significant points - such as junctions, rooms, or corners - and the lines represent the routes between them. There are no weights on this network, but they could easily be added in a real situation (i.e. the length of the routes between the nodes).

The NPC is my enemy and needs to track me through the level. I'm moving around, and so is it, so it needs to recalculate the route to my position each step. (For this article, we will assume that the NPC and I can only be at a node at any given time - in a real situation though, it could be assumed that a node will have a line-of-sight to all nodes that it connects to, so the NPC could move to the node nearest to me and be able to shoot at me).

Now, while it will depend on your pathfinding algorithm, recalculating a path each step isn't fast. Especially with an algorithm such as Dijikstra's, which sometimes involves labeling the entire network. Ideally, we need a method to store all the routes between nodes beforehand.


A concept

If the shortest route from node A to node D is ABCD, then the shortest route from A to C must be ABC.

I haven't yet found a case where this isn't true. For Dijikstra's, the shortest distance (and therefore, route) to each node in the network is found, and the algorithm depends on it being true. I'm not going to try and disprove it. :-)


How does this help?

As I said, we want some kind of way of pre-calculating all possible routes. Some nice data structure that we can look stuff up in - such as a tree - or, rather, a matrix. Here's the matrix we're going to use for the problem I described earlier:

<table width="400" border="1" cellpadding="1" cellspacing="0"><tbody><tr><td>&nbsp;</td><td class="tblhdr" colspan="9" align="center">To</td></tr><tr><td rowspan="9" class="tblhdr" width="1%" align="center">From</td><td align="center">&nbsp;</td><td align="center"><b>A</b></td><td align="center"><b>B</b></td><td align="center"><b>C</b></td><td align="center"><b>D</b></td><td align="center"><b>E</b></td><td align="center"><b>F</b></td><td align="center"><b>G</b></td><td align="center"><b>H</b></td></tr><tr><td align="center"><b>A</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>B</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>C</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>D</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>E</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>F</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>G</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr><tr><td align="center"><b>H</b></td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td><td align="center">&nbsp;</td></tr></tbody></table>That'll be nice, won't it? Look up where we are, where we're going, and get... what? The answer: the next node we need to move to. I'll fill out the matrix for the network above, quickly.

<table width="400" border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td>&nbsp;</td> <td class="tblhdr" colspan="9" align="center">To</td> </tr> <tr> <td rowspan="9" class="tblhdr" width="1%" align="center">From</td> <td align="center">&nbsp;</td> <td align="center"><b>A</b></td> <td align="center"><b>B</b></td> <td align="center"><b>C</b></td> <td align="center"><b>D</b></td> <td align="center"><b>E</b></td> <td align="center"><b>F</b></td> <td align="center"><b>G</b></td> <td align="center"><b>H</b></td> </tr> <tr> <td align="center"><b>A</b></td> <td align="center">-</td> <td align="center">B</td> <td align="center">B</td> <td align="center">E</td> <td align="center">E</td> <td align="center">F</td> <td align="center">F</td> <td align="center">F</td> </tr> <tr> <td align="center"><b>B</b></td> <td align="center">A</td> <td align="center">-</td> <td align="center">C</td> <td align="center">C</td> <td align="center">A</td> <td align="center">A</td> <td align="center">C</td> <td align="center">A</td> </tr> <tr> <td align="center"><b>C</b></td> <td align="center">B</td> <td align="center">B</td> <td align="center">-</td> <td align="center">D</td> <td align="center">D</td> <td align="center">G</td> <td align="center">G</td> <td align="center">G</td> </tr> <tr> <td align="center"><b>D</b></td> <td align="center">E</td> <td align="center">C</td> <td align="center">C</td> <td align="center">-</td> <td align="center">E</td> <td align="center">G</td> <td align="center">G</td> <td align="center">G</td> </tr> <tr> <td align="center"><b>E</b></td> <td align="center">A</td> <td align="center">A</td> <td align="center">D</td> <td align="center">D</td> <td align="center">-</td> <td align="center">F</td> <td align="center">F</td> <td align="center">F</td> </tr> <tr> <td align="center"><b>F</b></td> <td align="center">A</td> <td align="center">A</td> <td align="center">G</td> <td align="center">G</td> <td align="center">E</td> <td align="center">-</td> <td align="center">G</td> <td align="center">H</td> </tr> <tr> <td align="center"><b>G</b></td> <td align="center">F</td> <td align="center">C</td> <td align="center">C</td> <td align="center">D</td> <td align="center">F</td> <td align="center">F</td> <td align="center">-</td> <td align="center">H</td> </tr> <tr> <td align="center"><b>H</b></td> <td align="center">F</td> <td align="center">F</td> <td align="center">G</td> <td align="center">G</td> <td align="center">F</td> <td align="center">F</td> <td align="center">G</td> <td align="center">-</td> </tr> </tbody></table>Does that make sense? Trace the route from any one node to another, quickly. For example, B to H.

B->H: Move to A
A->H: Move to F
F->H: Move to H
GOAL

That demonstrates that we've got a very simple algorithm on our hands. The matrix is also easy to build, too - you just calculate the route from the 'from' node to the 'start' node, and store the first node in the route (excluding the start node itself).

This network is also a little special because there are no dead ends - that is, nodes with only one connection. If there were - and in real situations, there would be - that row in the matrix is easy to do, as there's only one option for each cell.

Now, what about the original problem of trying to track me? I'll show a sample 'game,' with the NPC's moves on the left, and mine on the right. Let's say that the NPC starts at node A, and I at node C. I don't know the level very well, so I may make some bad moves from time to time, but it demonstrates the AI.

<table border="1" cellpadding="2" cellspacing="0"> <tbody><tr> <td>A-&gt;C: Move to B</td> <td>C-&gt;D</td> </tr> <tr> <td>B-&gt;D: Move to C</td> <td>D-&gt;E</td> </tr> <tr> <td>C-&gt;E: Move to D</td> <td>E-&gt;F</td> </tr> <tr> <td>D-&gt;F: Move to G</td> <td>F-&gt;H</td> </tr> <tr> <td>G-&gt;H: Move to H</td> <td>Aargh!</td> </tr> </tbody></table>In each case, all the NPC needs to do is to know my position, which it can use to look up where to move in the table. It's fast, no?

Sure, if I'd played a little better, I could have had it running around in circles for as long as I felt like. That's partly because of the level, and partly because the bot doesn't have the option to 'rest.' If the bot stopped to rest (or, for that matter, if I did) then the outcome would have been different. Also, if there were other goals, the bot may have chosen to take a different route to pick up a bonus on the way. Ultimately, I guess, it's just a version of the Terminator algorithm (or 'tracker,' but I like Terminator better :-) ) applied to a network. Still, it's useful.


Cheating

Let's return to the scenario I originally presented you with, of the NPC entering a new environment. Rather than exploring, it uses a pre-calculated network to find the optimal route and reach its goal immediately, rather than having to explore.

The matrix method that I've just demonstrated can be expanded for education. The mind is a little more complicated, but it's still understandable.


Learning the path

Another level:

/reference/programming/features/fastpathfinding/ktp_image003.png
Attached Image: ktp_image003.png

This one's a little more complex. The blue lines represent walls.

We can build two extra tables from this that we'll need later. The first one is one that I will call our VIS table - it contains information about which nodes can 'see' which other nodes. Our bot will only 'learn' about the existence of a node when it 'sees' it.

Sight Table - T: TRUE, BLANK: FALSE

<table border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td width="1%">&nbsp;</td> <td colspan="17" class="tblhdr" align="center">To</td> </tr> <tr> <td rowspan="17" class="tblhdr">From</td> <td>&nbsp;</td> <td align="center"><b>A</b></td> <td align="center"><b>B</b></td> <td align="center"><b>C</b></td> <td align="center"><b>D</b></td> <td align="center"><b>E</b></td> <td align="center"><b>F</b></td> <td align="center"><b>G</b></td> <td align="center"><b>H</b></td> <td align="center"><b>I</b></td> <td align="center"><b>J</b></td> <td align="center"><b>K</b></td> <td align="center"><b>L</b></td> <td align="center"><b>M</b></td> <td align="center"><b>N</b></td> <td align="center"><b>O</b></td> <td align="center"><b>P</b></td> </tr> <tr> <td align="center"><b>A</b></td> <td align="center">-</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> </tr> <tr> <td align="center"><b>B</b></td> <td align="center">T</td> <td align="center">-</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> </tr> <tr> <td align="center"><b>C</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">-</td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>D</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>E</b></td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> <td align="center">T</td> <td align="center">-</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>F</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">-</td> <td align="center">T</td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>G</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>H</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>I</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> <td align="center">T</td> <td align="center">-</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>J</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">-</td> <td align="center">T</td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> </tr> <tr> <td align="center"><b>K</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>L</b></td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>M</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">T</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>N</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>O</b></td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">-</td> <td align="center">T</td> </tr> <tr> <td align="center"><b>P</b></td> <td align="center">T</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">T</td> <td align="center">-</td> </tr> </tbody></table>(Yes, it is symmetrical along the To=From line - because we're working on the premise that if A can see B, B can see A. I can't think of a practical application where this isn't true, but if you can, kudos to you. :-) )

The second table we can build - a much smaller one - is a magnitude table. It lists the number of connections each node has. I won't do it yet, as we don't pre-build it, we build it as we go along.

OK. Let's say that our bot starts at node A. He knows only node A, and nothing more. His route table looks like this:

<table width="100" border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td width="1%">&nbsp;</td> <td colspan="2" class="tblhdr" align="center">To</td> </tr> <tr> <td rowspan="2" class="tblhdr">From</td> <td>&nbsp;</td> <td align="center"><b>A</b></td> </tr> <tr> <td align="center"><b>A</b></td> <td>&nbsp;</td> </tr> </tbody></table> can build the magnitude table I mentioned above:

<table width="100" border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td class="tblhdr" align="center">Node</td> <td class="tblhdr" align="center">Magnitude</td> </tr> <tr> <td align="center">A</td> <td align="center">0</td> </tr> </tbody></table>Nice! B-) But, somehow, kinda useless. So, as our bot cannot move and has no higher priority goal (that is achievable - if he is hungry, for example, his limited network will not have any food in it), he decides to look around.

Now, we are going to cheat a little here, but if we didn't, it'd look strange. Node A can see nodes B, L, O, and P. So, we pick the first of those, and turn the bot to face it.

In actual fact, B, O, and P are all in a line. So, when the bot's field of view picks up node B, it will pick up nodes O and P as well.

The first move is to add the newly discovered nodes to the route and magnitude tables, and to update the magnitude table:

<table width="200" border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td>&nbsp;</td> <td class="tblhdr" colspan="5" align="center">To</td> </tr> <tr> <td rowspan="5" class="tblhdr" width="1%" align="center">From</td> <td align="center">&nbsp;</td> <td align="center"><b>A</b></td> <td align="center"><b>B</b></td> <td align="center"><b>O</b></td> <td align="center"><b>P</b></td> </tr> <tr> <td align="center"><b>A</b></td> <td align="center">-</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>B</b></td> <td align="center">&nbsp;</td> <td align="center">-</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>O</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">-</td> <td align="center">&nbsp;</td> </tr> <tr> <td align="center"><b>P</b></td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">&nbsp;</td> <td align="center">-</td> </tr> </tbody></table><br> <table width="150" border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td class="tblhdr" align="center">Node</td> <td class="tblhdr" align="center">Magnitude</td> </tr> <tr> <td align="center">A</td> <td align="center">1</td> </tr> <tr> <td align="center">B</td> <td align="center">2</td> </tr> <tr> <td align="center">O</td> <td align="center">1</td> </tr> <tr> <td align="center">P</td> <td align="center">2</td> </tr> </tbody></table>Now, recalculate all new rows and columns in the route table:

<table width="200" border="1" cellpadding="1" cellspacing="0"> <tbody><tr> <td>&nbsp;</td> <td class="tblhdr" colspan="5" align="center">To</td> </tr> <tr> <td rowspan="5" class="tblhdr" width="1%" align="center">From</td> <td align="center">&nbsp;</td> <td align="center"><b>A</b></td> <td align="center"><b>B</b></td> <td align="center"><b>O</b></td> <td align="center"><b>P</b></td> </tr> <tr> <td align="center"><b>A</b></td> <td align="center">-</td> <td align="center">B</td> <td align="center">B</td> <td align="center">B</td> </tr> <tr> <td align="center"><b>B</b></td> <td align="center">A</td> <td align="center">-</td> <td align="center">P</td> <td align="center">O</td> </tr> <tr> <td align="center"><b>O</b></td> <td align="center">P</td> <td align="center">P</td> <td align="center">-</td> <td align="center">P</td> </tr> <tr> <td align="center"><b>P</b></td> <td align="center">B</td> <td align="center">B</td> <td align="center">O</td> <td align="center">-</td> </tr> </tbody></table>And that's it!

There are a couple of extra things to mention. Firstly, it's a good idea to keep a separate, pre-calculated magnitude table for the entire network, so you can see when a node has been 'fully explored' (by comparing its value in the bot's magnitude table to that in the map's magnitude table). Exploration of the network is achieved by finding the nearest node that hasn't been 'fully explored' and looking at each of its links in turn to add them to the tables.

Incidentally, see why I said we had to cheat? If we didn't use the VIS table to look at visible nodes in turn, rather than just rotating the bot's head and seeing what it could see, what would have happened at, say, corridor corners? The bot would have stopped, done a complete 360-degree scan, and then continued. A human player would just see that the corridor bends too the right, and continue. Of course, there could be access from above - which the bot would see, but the player would probably miss. Still, it's better than stopping at each node to perform a full scan.

Secondly, what do you do when the addition of a node affects other routes? If, for example, node A was fully explored, then B, then P, the route from node L to node J would be LABPJ. However, if J was then fully explored, the route should become LKJ. How do you detect this?

I haven't been able to find a nice, neat pattern that can be expressed with a single rule yet. Perhaps there isn't one. The best I can come up with is:

After calculating values for the new cells, recalculate all rows for nodes directly connected to the new node, and then recalculate all rows for nodes with a magnitude of 3 or higher.

So, with my previous example, after adding node K to the tables and calculating the values for column K and row K, you need to recalculate rows L and J, and then nodes B and P (which would each have magnitudes of 3, but would not have already been recalculated).


Conclusion

Right, that's all. This method has plenty of room for expansion - off the top of my head, assigning 'goodness' values to each node - for strategic importance, safety, proximity to health/weapons, etc - might be useful to break out of the loop situations I mentioned earlier.

Oof. I'm going to go and eat some toast, I think. Questions, comments, etc, you can email me at rfine@lycos.com, post about this in the forums (I usually check) or catch me in #gamedev. Happy brainstorming! =]





Comments

Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.




PARTNERS