Game Development Community

Simple Terrain-Only A-Star for TGE

by Ricky Taylor · 07/03/2006 (11:20 am) · 6 comments

Download Code File

These two files should be extracted to engine/ai.

Disclaimer

Well, this isnt the best pathfinding, and I suggest you look into the AI pack for torque that is going to be released at some stage if you want to do anything complex.

I have done quite a chunk of code C++ only, so it'd be a good idea to read and rewrite the code as you see fit. =)

Reference

After a recompile you have access to the following commands:

pathInit(); -- Starts the pathfinder, required to do any pathfinding.

pathCreate(); -- Forces the NavMap to be recreated (usually done by pathInit).

pathWrite(%file); -- Writes the NavMap to %file.

pathGetNextPoint(%start, %goal, %closed); -- The actual pathfinding command, start is a 3D location in the format of "x y z", so is goal. %closed is a NodePath that is populated by passing to this function. For a successful path, the same closed list must be passed each time.

pathSetMaxSlope(%slope) -- Sets the maximum climb, squared. The recommended value is 100.

Implementation

For a successful implementation, you must call pathInit and pathSetMaxSlope at the start of the game, and then call pathGetNextPoint for every pathfinding operation. It should be called at the start, and after each point is reached, other than the goal.

You should also edit aiPathfinding.cc if you arent using the RTS starter kit.

Check line 72 and read the comment.

Example

Here is a good example how to read all the points of a path:

pathInit();
pathSetMaxSlope(100);

%closed = new NodePath();

%start = "0 0 0";

%dest = "500 0 0";

%point = pathGetNextPoint(%start, %dest, %closed);

while(%point !$= %dest)
{
    echo(%point);
    %point = pathGetNextPoint(%point, %dest, %closed);
}

About the author

Recent Blogs

• The mystery maze of A-Star!

#1
07/02/2006 (12:52 pm)
I just dropped this into my tse project and replaced the rendering code. But it kept crashing so I left out the rendering code but it still crashs. Any idea why?

edit1:
I have isolated the error to somwhere in here in the createmap function

this line i think
F32 gridSize = Con::getFloatVariable("Server::gridSize", 10.0f);
#2
07/03/2006 (12:50 pm)
I doubt that's it, the second part of the Con::get methods gives you a default value if the variable isn't found. Are you using the legacy TGE terrain or Atlas terrain? This probably doesn't work with Atlas, and anything referencing TGE terrain in TSE is bound to be wonky.

He also says you should read line 72 in in aiPathFinding.cc if you aren't using the RTS starter kit, to fix some incompatibilities there.
#3
07/03/2006 (12:57 pm)
Yea im using the legacy terrain, Im using a RTS TSE port. It seems im one of those people who doesnts read the instructions and wonders why everything goes wrong :p. But that line is ok because I have the RTSUnit class. Is there a good way of debuging c++ source, im using vc 2005 and at the mo im just using con:errorprintf.

Got it to work, it just wont work in a debug version
#4
09/04/2006 (4:17 am)
I'm using the RTS kit, no TSE, and getting the error:

isNodeBlocked: Unknown Command.

In the console when pathGetNextPoint() is called. Where is this command supposed to have come from? I can't find it declared anywhere, only referenced in the aiPathFinding.cc that's supplied here. Cheers.
#5
11/01/2006 (3:30 pm)
What is the setup for the Units(code)? I am not sure how to attach to each unit...do you have some example code for a single unit?

sorry for all the questions :)
#6
03/06/2007 (6:21 pm)