PDA

View Full Version : AStar Dll



MouseTrap
17-02-2009, 22:42
I created an Astar dll. and got it working with TB thanks to Eros. Its very fast and easy to use.
Here is the sample script I use to test it on a 100x100 map. It gives me about 100 paths a second.
Note: the array returned by 'GetRoute' is owned by the dll, don't try to free it. ;)


uses "CONSOLE"
randomize

DECLARE function SetupMap LIB "C:\Dev\path.DLL" ALIAS "SetupMap" (byval arrayhandle as long, byval Width as long, byval Height as long, byval Diag as long) as long
DECLARE function FindPath LIB "C:\Dev\path.DLL" ALIAS "FindPath" (byval StartX as long, byval StartY as long, byval DestX as long, byval DestY as long) as long
DECLARE function GetRoute LIB "C:\Dev\path.DLL" ALIAS "GetRoute" ( length as long) as long '// its byref

const _SIZE as long = 100
dim pMapArr as long
pMapArr = heap_alloc(_size * _size * sizeof(LONG)) '---<<<< ALLOCATE memory
dim i,j as integer
dim arr(_SIZE,_SIZE) as single at pMapArr
dim a,b,c,d,e as long
dim g as long = 1

a=_SIZE
b=_SIZE
c=2

'// Create test map with random val [0.0,1.0]
for i = 1 to _SIZE
for j = 1 to _SIZE
arr(i,j) = rnd '// a value of >=1 is considered a wall, < 1 is difficulty
next
next

print SetupMap(pMapArr,_SIZE,_SIZE,2) , $crlf '// pass the map

sleep(500)
for g=1 to 99999
FindPath(0,0, rnd(1,_SIZE-1), rnd(1,_SIZE-1) )
if mod(g, 100) = 0 then printl g & " findpath calls." '//
next

sleep(500)
d=GetRoute(e) '// [e] is length of the route, [d] is the array pointer
print e, $crlf

dim f(e) as long at d '// create an array of [e] size and point to addr [d]

waitkey

ErosOlmi
17-02-2009, 23:04
MouseTrap,

in order to avoid thread "invasion", I splitted you post (this one) to a new post. We usually prefer to have forum as easy as possible.
Michael posted a thread to have some feedback about a new module. If we post in the same thread another project we will have a mess.
I think it can be good also for your project.

I hope you understand.
Thanks a lot
Eros

MouseTrap
17-02-2009, 23:11
Hi,
I didn't mean to hijack his thread.
As his example was doing pathfinding, I was just giving away that dll I made.
I mistook the subject of the thread to be pathfinding rather than his module..

ErosOlmi
17-02-2009, 23:15
Ok, perfect.

I'm quite curious about your opinion on thinBasic. You seems very advanced so I feel you can give us a lot of precious feeling/indications about how to improve thinBasic.

In the meantime I'm testing your script.

Ciao
Eros

ErosOlmi
17-02-2009, 23:36
MouseTrap,

how can I get the path info?
GetRoute returns the length of the path and a pointer but how to get the exact path from A to B?

Thanks a lot
Eros

MouseTrap
17-02-2009, 23:38
Oops!
sorry i forgot to mention that. ;(
that path is returned as a 1d array in the format of:

X,y,X,y,X,y...

Also the beginning of the array is the *destination. so the array is reversed in the normal way of thinking..

Michael Hartlef
17-02-2009, 23:39
Hi,
I didn't mean to hijack his thread.
As his example was doing pathfinding, I was just giving away that dll I made.
I mistook the subject of the thread to be pathfinding rather than his module..


No problem. I just wanted to know if my module works ok so far.

MouseTrap
17-02-2009, 23:44
ya, it worked great and i couldnt get the framerate to drop below 60 [vsync]
is there a way to to do async rendering in the that graphics module?

btw: if anyone wants the source of the dll, just say so.

ErosOlmi
17-02-2009, 23:46
that path is returned as a 1d array in the format of:

X,y,X,y,X,y...

Also the beginning of the array is the *destination. so the array is reversed in the normal way of thinking..


Can you please post a little example on how to read path data back?
I was making a script to draw the initial map and than draw the path but accessing return pointer as array seems returning always zeroes. I'm sure I'm making some error but do not know where.

Thanks in advance.
Ciao
Eros

PS: as Petr suggested, better to avoid to use DECLARE with a fixed path into DLL name. To avoid DLL hell, thinBasic adopt a special DLL path finding. You can get info on DECLARE help at http://www.thinbasic.com/public/products/thinBasic/help/html/declare.htm
So just put your dll into script path or in mod\ directory or lib\ directory. thinBasic will find it.

MouseTrap
17-02-2009, 23:58
Ok, here is a modified script to show the path.

hit any key to show the path.


uses "CONSOLE"
randomize

DECLARE function SetupMap LIB "path.DLL" ALIAS "SetupMap" (byval arrayhandle as long, byval Width as long, byval Height as long, byval Diag as long) as long
DECLARE function FindPath LIB "path.DLL" ALIAS "FindPath" (byval StartX as long, byval StartY as long, byval DestX as long, byval DestY as long) as long
DECLARE function GetRoute LIB "path.DLL" ALIAS "GetRoute" ( length as long) as long '// its byref

Console_SetScreenBufferSize(80,25)

const _SIZE as long = 100
dim pMapArr as long
pMapArr = heap_alloc(_size * _size * sizeof(LONG)) '---<<<< ALLOCATE memory
dim i,j as integer
dim arr(_SIZE,_SIZE) as single at pMapArr
dim a,b,c,d,e as long
dim g as long = 1

a=_SIZE
b=_SIZE
c=2

'// Create test map with random val [0.0,1.0]
for i = 1 to _SIZE
for j = 1 to _SIZE
arr(i,j) = rndf(0,1.3) '// a value of >=1 is considered a wall, < 1 is difficulty
next
next

print SetupMap(pMapArr,_SIZE,_SIZE,2) , $crlf '// pass the map

sleep(500)
''for g=1 to 99999
FindPath(1,1, 50,20)'rnd(1,_SIZE-1), rnd(1,_SIZE-1) )
'' if mod(g, 100) = 0 then printl g & " findpath calls." '// call pathfinder
''next

sleep(500)
d=GetRoute(e) '// [e] is length of the route, [d] is the array pointer
print e, $crlf

dim f(e) as long at d '// create an array of [e] size and point to addr [d]

print f(1), f(2)

for i= 1 to _size
for j = 1 to _size
console_printat(".",i,j)
next
next
dim II as long = 1
while ii < e
console_printat("*",f(ii),f(ii+1))
ii+=2
sleep(300)
wend

waitkey

ErosOlmi
18-02-2009, 00:18
I made a little variation to have a visible layout trying to considering "difficulty".

MouseTrap
18-02-2009, 00:26
That looks much better. ;)

btw: the argument passed to 'SetupMap' is:

'0=no diagonal movement.
'1=diagonal movement and cutting corners allowed.
'2=diagonal movement but no cutting corners.

[EDIT]
And also: the Dll internally represents the map like a normal array with dimentions [0 to length-1]
so you can use the coords 0,0 when calling Findpath.

ErosOlmi
18-02-2009, 00:42
Great, thanks a lot

I will play more tomorrow with it. I'm going to have some rest now.

Ciao
Eros

Michael Hartlef
18-02-2009, 08:48
What language did you use to develop the DLL?

MouseTrap
18-02-2009, 09:07
Blitzmax blitzmax.com (http://blitzmax.com)

jack
19-02-2009, 04:58
btw: if anyone wants the source of the dll, just say so.

I am interested, i would like to look at your code :)

MouseTrap
19-02-2009, 06:23
Sent!