C extensions

I’m playing around with an RTS. I’ve got most for the basic interface coded. The program uses pro-rsoft’s PGMM to generate a heightfield mesh- uses a different image to texture the mesh, then uses a third image as a grid for obstacles on that map. The grid information is used by a python based A* script (pathfinding through obstacles/mazes).

Everything works fine- units can be selected with a mouse click or a selection box- they can be told to move to any location on the map- the A* algorithm provides a path between the current position and the destination.

The problem is speed. The python based A* script is just too slow. The image grid is 64x64- so it’s not gigantic- but selecting multiple units each having to run their own A* routine to find a path between their individual position and the destination coordinates noticably stalls the computer (Core 2 Duo) until the A* script returns the path information.

I’m aware of a many C A* scripts on the internet but I don’t know a thing about C and I don’t know how to wrap them so that my python script could make reference to the C script instead of the python A* script I’m currently using.

Any ideas? Help?

Another way you can use is to make the a-star algorithm non-blocking, this way the ai-controlled objects stay still for a while (during calculation) but will not block the running game.

Another way is to use a different approach to path-finding. Walk in the direction of the target until you hit a wall, then follow the wall until you have a closer position then the one on the first collision.

But if you manage to make a c extension for panda calculating a-star for a python array, i and possibly other would be happy about it :slight_smile:

Panda3D provides everything you need to make C/C++ extensions to Panda3D.
Take PGMM, for example. In Python it wouldn’t be as fast as it is now, so I’ve implemented it into C++. You basically need to do the following steps:
[] Create a header file, source file and Inline functions file. (e.g. pgmm.h, pgmm.cxx, pgmm.I). Put in there the source you like.
] Interrogate your program. Interrogate is a Panda3D module which generates python bindings for your program. If you want, I can give you the shell script I use to generate interrogate wrappers for PGMM.
This process basically consists of two steps: interrogate, which generates e.g. a pgmm_igate.cxx, and the interrogate_module call, which generates a pgmm_module.cxx file.
[] Nextly, you compile all your .cxx files, including the two you got from interrogate. How? Check this or take a look at PGMM’s makefile.
] Finally, link your cxx files together to form a library. Put it somewhere in python’s site-packages directory, and off you go, you can import it from python and go use it.

I’ve heard about some sample cxx extension in the skel/ directory somewhere in the Panda3D source, you might find that useful as well.

Hypnos: Exactly how do you go about making the function non-blocking?

pro-rsoft: You’ve answered a lot of questions, unforunately I just don’t understand much about C++.

this is a implementation of a-star which is non-blocking:


Beware im not sure if i have implemented the a-star algorithm the correct way, but it look pretty fine.

You have to call astar2.py and it requires pygame for the output.

Unlike a “normal” a-star implementation it doesnt check the whole open-list in one turn, but has a maximum time it uses for checks (“step” function), until it returns and makes it possible to render a frame. next time the step function is called it’s continuing the calculation until it’s done, it’s finished once the result function doesnt return None…

This implementation is a bit more complicated then a usual a-star implementation, because of the non-blocking nature, and because it’s a 3d a-star version. If you run it you will see 3 images, the leftmost would be the lowest level, the middle the 2nd layer and the 3th the top layer.

You are free to use the code if it helps you…

I’ve used Cprogramming.com’s tutorials to get me started not so long time ago, to speed up my 3D slicer code to ~700x faster.

hello mavasher

im currently student. im new to panda3d and im implementing a simple RTS game for my final year project. im told to concentrate on the AI rather than on collision rays, selecting units etc.
however, i can see from your post that u already have your small RTS game which might help me a lot. can you please upload your project or link it to somewhere. i badly need help and im relying on you right now. i hope to hear from you soon
thanks in advance.