I have seen that some people have already made perfectly funcioning A* pathfinding functions. I still decided to try to make a simple one on my own. It kind of works.
To begin with draw a world with the mouse. Right mouseclick in two areas for the start and the end. Once you have finished making the world for them hit enter and it will try to find a path between 1 point to the other.
randomize timer() : sync on : sync rate 0 : autocam off : width=100 : height=100 : depth=32 : 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=2 to width-1 : for y=2 to height-1 : write memblock dword 1,((y-1)*width+x-1)*4+12,rgb(255,255,255) : next y : next x : for x=1 to width : write memblock dword 1,((1-1)*width+x-1)*4+12,rgb(0,0,0) : write memblock dword 1,((height-1)*width+x-1)*4+12,rgb(0,0,0) : next x : for y=1 to height : write memblock dword 1,((y-1)*width+1-1)*4+12,rgb(0,0,0) : write memblock dword 1,((y-1)*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() : checks=0 : repeat : sync : x=mousex()*(width+0.0)/screen width() : y=mousey()*(height+0.0)/screen height() : if mouseclick()=1 : write memblock dword 1,((y)*width+x)*4+12,rgb(0,0,0) : endif : if mouseclick()=2 : if checks=1 : if memblock dword(1,((y)*width+x)*4+12)<>rgb(255,0,0) : write memblock dword 1,((y)*width+x)*4+12,rgb(0,255,0) : inc checks : endx=x : endy=y : endif : endif : if checks=0 : write memblock dword 1,((y)*width+x)*4+12,rgb(255,0,0) : inc checks : startx=x : starty=y
endif : endif : make image from memblock 1,1 : until returnkey()+checks=3
type node
g as integer : h as integer : pdx as integer : pdy as integer : state as integer
endtype
global dim blocks(width,height) as node : for x=1 to width : for y=1 to height : if memblock dword (1,((y-1)*width+x-1)*4+12)=rgb(0,0,0) : blocks(x,y).state=1 : else : blocks(x,y).state=-1 : endif : next y : next x : blocks(startx,starty).state=0 : blocks(startx,starty).g=0 : blocks(startx,starty).h=(abs(startx-endx)+abs(starty-endy))*10 : blocks(startx,starty).pdx=0 : blocks(startx,starty).pdy=0 : lp=find_path(startx,starty,endx,endy,width,height) : for x=1 to width : for y=1 to height : if blocks(x,y).state=2 : write memblock dword 1,((y-1)*width+x-1)*4+12,rgb(0,255,255)
endif : next y : next x : make image from memblock 1,1 : exit prompt "Length of path was "+str$(lp)+" spaces","Tada" : sync : wait key : end
function find_path(startx,starty,endx,endy,width,height)
repeat : lowestf=999999 : fx=0 : fy=0 : for x=1 to width : for y=1 to height : if blocks(x,y).state=0 : if blocks(x,y).g+blocks(x,y).h<lowestf : lowestf=blocks(x,y).g+blocks(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)=1 : diagonal=0 : mv=10 : endif : if abs(a)+abs(b)=2 : diagonal=1 : mv=14
endif : if abs(a)+abs(b)=0 : diagonal=2 : endif : possible=1 : if fx+a>-1 and fy+b>-1 and fx+a<width and fy+b<height : if blocks(fx+a,fy+b).state=1 : possible=0 : endif : if blocks(fx+a,fy+b).state=0 : if blocks(fx+a,fy+b).g<blocks(fx,fy).g+mv : possible=0 : endif : endif : if diagonal=1 : if blocks(fx+a,fy).state=1 or blocks(fx,fy+b).state=1 : possible=0 : endif : endif : else : possible=0 : endif : if possible=1 : blocks(fx+a,fy+b).state=0 : blocks(fx+a,fy+b).h=(abs(fx+a-endx)+abs(fy+b-endy))*10
blocks(fx+a,fy+b).g=blocks(fx,fy).g+mv : blocks(fx+a,fy+b).pdx=a : blocks(fx+a,fy+b).pdy=b : endif : next b : next a : blocks(fx,fy).state=1 : until blocks(endx,endy).state=0 : nx=endx : ny=endy : repeat : blocks(nx,ny).state=2 : inc lp : nx=nx-blocks(nx,ny).pdx : ny=ny-blocks(nx,ny).pdy : until nx=startx and ny=starty
endfunction lp
Problems I still need to fix:
-It can't handle either the start or end being in the top left corner.
-It almost never goes the fastest route.
-Path sometimes doesn't quite touch the start.