Yes, I know people have written path finding functions. I tried to make one myself before
here.
I have managed to fit pathfinding into 20 lines and it is capable of multiple paths at once. Change the "width" and "height" variables to change the width and height of the world.
Controls:
Left-click: Draw walls
Right-click: Draw beginning/end point
Enter(once drawn even number of points): begin to path find.
It will take a second to find the paths, especially with large worlds and many paths.
Also, don't make impossible situations. It doesn't check for that, so you will be stuck in a loop.
Compressed-
randomize timer() : sync on : sync rate 0 : autocam off : width=100 : height=100 : depth=32 : maxpoints=20
type node
g as integer : h as integer : pdx as integer : pdy as integer : state as integer
endtype
global dim blocks(maxpoints/2,width,height) as node
type position
x as integer : y as integer
endtype
dim pos(maxpoints,2) as position : make memblock 1,width*height*depth*4+12 : write memblock dword 1,0,width : write memblock dword 1,4,height : write memblock dword 1,8,depth
for x=1 to width-2 : for y=1 to height-2 : write memblock dword 1,(y*width+x)*4+12,rgb(100,200,0) : next y : next x : for x=0 to width-1 : write memblock dword 1,x*4+12,rgb(20,100,40) : write memblock dword 1,((height-1)*width+x)*4+12,rgb(20,100,40) : next x : for y=0 to height-1 : write memblock dword 1,y*width*4+12,rgb(20,100,40) : write memblock dword 1,(y*width+width-1)*4+12,rgb(20,100,40) : next y : make image from memblock 1,1 : sprite 1,0,0,1 : size sprite 1,screen width(),screen height() : repeat : sync : mclick=mouseclick() : px=x : py=y : x=mousex()*(width+0.0)/screen width() : y=mousey()*(height+0.0)/screen height() : if mclick=1 : if clicking=1
if px<>x : m#=(py-y+0.0)/(px-x+0.0) : if px>x : lowx=x : highx=px : else : lowx=px : highx=x : endif : for ix=lowx to highx : iy=m#*(ix-x)+y : write memblock dword 1,(iy*width+ix)*4+12,rgb(20,100,40) : next ix : else : if py<>y : m#=(px-x+0.0)/(py-y+0.0) : if py>y : lowy=y : highy=py : else : lowy=py : highy=y : endif : for iy=lowy to highy : ix=m#*(iy-y)+x
write memblock dword 1,(iy*width+ix)*4+12,rgb(20,100,40) : next ix : endif : endif : else : write memblock dword 1,(y*width+x)*4+12,rgb(20,100,40) : endif : clicking=1 : endif : if mclick=2 and clicking=0 : if checks/2=checks/2.0 : write memblock dword 1,(y*width+x)*4+12,rgb(0,0,100) : inc p : pos(p,1).x=x : pos(p,1).y=y : else : write memblock dword 1,(y*width+x)*4+12,rgb(0,255,0) : pos(p,2).x=x : pos(p,2).y=y : endif : inc checks : clicking=2 : endif : if mclick=0 : clicking=0
endif : make image from memblock 1,1 : if escapekey()=1 : end : endif : until returnkey()=1 and checks=>2 and checks/2.0=checks/2 : for x=0 to width-1 : for y=0 to height-1 : for i=1 to p : if memblock dword (1,(y*width+x)*4+12)=rgb(20,100,40) : blocks(i,x,y).state=1 : else : blocks(i,x,y).state=-1 : endif : next i : next y : next x : for i=1 to p : find_path(i,pos(i,1).x,pos(i,1).y,pos(i,2).x,pos(i,2).y,width,height) : next i : for x=0 to width-1 : for y=0 to height-1 : for i=1 to p : if blocks(i,x,y).state=2 : write memblock dword 1,(y*width+x)*4+12,rgb(0,255,255)
endif : next i : next y : next x : make image from memblock 1,1 : sync : wait key : end
function find_path(pathnum,startx,starty,endx,endy,width,height)
blocks(pathnum,startx,starty).state=0 : blocks(pathnum,startx,starty).g=0 : blocks(pathnum,startx,starty).h=(abs(startx-endx)+abs(starty-endy))*10 : blocks(pathnum,startx,starty).pdx=0 : blocks(pathnum,startx,starty).pdy=0 : repeat : lowestf=999999 : fx=0 : fy=0 : for x=1 to width : for y=1 to height : if blocks(pathnum,x,y).state=0 : if blocks(pathnum,x,y).g+blocks(pathnum,x,y).h<lowestf : lowestf=blocks(pathnum,x,y).g+blocks(pathnum,x,y).h : fx=x : fy=y
endif : endif : next y : next x : for a=-1 to 1 : for b=-1 to 1 : if abs(a)+abs(b)=0 : diagonal=2 : endif : if abs(a)+abs(b)=1 : diagonal=0 : mv=10 : endif : if abs(a)+abs(b)=2 : diagonal=1 : mv=14 : endif : possible=1 : if fx+a>-1 and fy+b>-1 and fx+a<width and fy+b<height : if (blocks(pathnum,fx+a,fy+b).state=1)+(blocks(pathnum,fx+a,fy+b).state=0 and blocks(pathnum,fx+a,fy+b).g<blocks(pathnum,fx,fy).g+mv)+(diagonal=1 and blocks(pathnum,fx+a,fy).state=1)+(diagonal=1 and blocks(pathnum,fx+a,fy).state=1 or blocks(pathnum,fx,fy+b).state=1)=0 : blocks(pathnum,fx+a,fy+b).state=0 : blocks(pathnum,fx+a,fy+b).h=(abs(fx+a-endx)+abs(fy+b-endy))*10
blocks(pathnum,fx+a,fy+b).g=blocks(pathnum,fx,fy).g+mv : blocks(pathnum,fx+a,fy+b).pdx=a : blocks(pathnum,fx+a,fy+b).pdy=b : endif : endif : next b : next a : blocks(pathnum,fx,fy).state=1 : until blocks(pathnum,endx,endy).state=0 : nx=endx : ny=endy : repeat : blocks(pathnum,nx,ny).state=2 : nx=nx-blocks(pathnum,nx,ny).pdx : ny=ny-blocks(pathnum,nx,ny).pdy : until nx=startx and ny=starty
endfunction
Uncompressed [not as good colo(u)rs]-
`Basic Setup
randomize timer()
sync on
sync rate 0
autocam off
`Variables
width=200
height=200
depth=32
maxpoints=20
`Setup for path finding
type node
g as integer
h as integer
pdx as integer
pdy as integer
state as integer
endtype
global dim blocks(maxpoints/2,width,height) as node
`Setup for storing positions
type position
x as integer
y as integer
endtype
dim pos(maxpoints,2) as position
`Setup for drawing
make memblock 1,width*height*depth*4+12
write memblock dword 1,0,width
write memblock dword 1,4,height
write memblock dword 1,8,depth
for x=1 to width-2
for y=1 to height-2
write memblock dword 1,(y*width+x)*4+12,rgb(255,255,255)
next y
next x
for x=0 to width-1
write memblock dword 1,x*4+12,rgb(0,0,0)
write memblock dword 1,((height-1)*width+x)*4+12,rgb(0,0,0)
next x
for y=0 to height-1
write memblock dword 1,y*width*4+12,rgb(0,0,0)
write memblock dword 1,(y*width+width-1)*4+12,rgb(0,0,0)
next y
make image from memblock 1,1
sprite 1,0,0,1
size sprite 1,screen width(),screen height()
`Draw
repeat
sync
mclick=mouseclick()
px=x
py=y
x=mousex()*(width+0.0)/screen width()
y=mousey()*(height+0.0)/screen height()
if mclick=1
if clicking=1
if px<>x
m#=(py-y+0.0)/(px-x+0.0)
if px>x
lowx=x
highx=px
else
lowx=px
highx=x
endif
for ix=lowx to highx
iy=m#*(ix-x)+y
write memblock dword 1,(iy*width+ix)*4+12,rgb(0,0,0)
next ix
else
if py<>y
m#=(px-x+0.0)/(py-y+0.0)
if py>y
lowy=y
highy=py
else
lowy=py
highy=y
endif
for iy=lowy to highy
ix=m#*(iy-y)+x
write memblock dword 1,(iy*width+ix)*4+12,rgb(0,0,0)
next ix
endif
endif
else
write memblock dword 1,(y*width+x)*4+12,rgb(0,0,0)
endif
clicking=1
endif
if mclick=2 and clicking=0
if checks/2=checks/2.0
write memblock dword 1,(y*width+x)*4+12,rgb(255,0,0)
inc p
pos(p,1).x=x
pos(p,1).y=y
else
write memblock dword 1,(y*width+x)*4+12,rgb(0,255,0)
pos(p,2).x=x
pos(p,2).y=y
endif
inc checks
clicking=2
endif
if mclick=0
clicking=0
endif
make image from memblock 1,1
if escapekey()=1
end
endif
until returnkey()=1 and checks=>2 and checks/2.0=checks/2
`Define world for path finding
for x=0 to width-1
for y=0 to height-1
for i=1 to p
if memblock dword (1,(y*width+x)*4+12)=rgb(0,0,0)
blocks(i,x,y).state=1
else
blocks(i,x,y).state=-1
endif
next i
next y
next x
`Find path
for i=1 to p
find_path(i,pos(i,1).x,pos(i,1).y,pos(i,2).x,pos(i,2).y,width,height)
next i
for x=0 to width-1
for y=0 to height-1
for i=1 to p
if blocks(i,x,y).state=2
write memblock dword 1,(y*width+x)*4+12,rgb(0,255,255)
endif
next i
next y
next x
make image from memblock 1,1
sync
wait key
end
function find_path(pathnum,startx,starty,endx,endy,width,height)
blocks(pathnum,startx,starty).state=0
blocks(pathnum,startx,starty).g=0
blocks(pathnum,startx,starty).h=(abs(startx-endx)+abs(starty-endy))*10
blocks(pathnum,startx,starty).pdx=0
blocks(pathnum,startx,starty).pdy=0
repeat
lowestf=999999
fx=0
fy=0
for x=1 to width
for y=1 to height
if blocks(pathnum,x,y).state=0
if blocks(pathnum,x,y).g+blocks(pathnum,x,y).h<lowestf
lowestf=blocks(pathnum,x,y).g+blocks(pathnum,x,y).h
fx=x
fy=y
endif
endif
next y
next x
for a=-1 to 1
for b=-1 to 1
n=abs(a)+abs(b)
select n
case 0
diagonal=2
endcase
case 1
diagonal=0
mv=10
endcase
case 2
diagonal=1
mv=14
endcase
endselect
possible=1
if fx+a>-1 and fy+b>-1 and fx+a<width and fy+b<height
if (blocks(pathnum,fx+a,fy+b).state=1)+(blocks(pathnum,fx+a,fy+b).state=0 and blocks(pathnum,fx+a,fy+b).g<blocks(pathnum,fx,fy).g+mv)+(diagonal=1 and blocks(pathnum,fx+a,fy).state=1)+(diagonal=1 and blocks(pathnum,fx+a,fy).state=1 or blocks(pathnum,fx,fy+b).state=1)=0
blocks(pathnum,fx+a,fy+b).state=0
blocks(pathnum,fx+a,fy+b).h=(abs(fx+a-endx)+abs(fy+b-endy))*10
blocks(pathnum,fx+a,fy+b).g=blocks(pathnum,fx,fy).g+mv
blocks(pathnum,fx+a,fy+b).pdx=a
blocks(pathnum,fx+a,fy+b).pdy=b
endif
endif
next b
next a
blocks(pathnum,fx,fy).state=1
until blocks(pathnum,endx,endy).state=0
nx=endx
ny=endy
repeat
blocks(pathnum,nx,ny).state=2
nx=nx-blocks(pathnum,nx,ny).pdx
ny=ny-blocks(pathnum,nx,ny).pdy
until nx=startx and ny=starty
endfunction