Yea, I know, I'm just happy I got a basic form of it working. I never really understood it and used to just try and force my character in the general direction of the goal. But with my character getting stuck and stuff. it got a little annoying lol
just for a little notice, i tried fixing the trap and wall glitch but got a little lazy. ill fix it in a bit.
EDIT:
Fixed it now it only moves up down left and right and you can trap it, but it won't freeze up.
Example:
`+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
`---------------------------------------------------------------------------------------------------------------------
`*********************************************************************************************************************
`Title: A* Pathfinding
`Created by: Pictionaryjr
`Date: Friday, March 12, 2010
`*********************************************************************************************************************
`---------------------------------------------------------------------------------------------------------------------
`+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
`*********************************************************************************************************************
`Controls: Left click to place blocks
` Right click to erase blocks
` Enter key to randomly place blocks
` Control key to clear all blocks
` Spacekey to activate path finding
`*********************************************************************************************************************
`*********************************************************************************************************************
`Colors: Red = End Point
` Green = Start Point
` Yellow = Explored Point
` Cyan = Seen Point
` Magenta = Path
` Blue = Block
`*********************************************************************************************************************
rem setup manual sync and clear screen for default flash
sync on : sync rate 0 : cls
rem setup up all arrays
`openg and shutg are the distances from the start point to the pending point
`openh and shuth are the distances from the end point to the pending point
`openf and shutf are the total distances from the start to the pending point to the end point
`openp and shutp are the parent points of the pending point
`open and shut establish if the pending point is closed or open
`blocks specifies if a block is there
`path is the resulting path after pathfinding is finished
dim openg(64,48) : dim openh(64,48) : dim openf(64,48) : dim openp(64,48,2) : dim open(64,48)
dim shutg(64,48) : dim shuth(64,48) : dim shutf(64,48) : dim shutp(64,48,2) : dim shut(64,48)
dim blocks(64,48) : dim path(64,48)
rem variables
`cury and curx are the current points being used
`endx and endy are the end points
`startx and starty are the starting points
curx=16 : cury=24 : endx=48 : endy=24 : startx=16 : starty=24
rem create the grid
`draw the grid
ink rgb(255,255,255),0
for x=0 to 630 step 10 : line x,0,x,479 : next x
for y=0 to 470 step 10 : line 0,y,639,y : next y
`get an image of the grid
get image 1,0,0,640,479
rem draw the starting and ending points
ink rgb(0,255,0),0
box startx*10,starty*10,(startx*10)+9,(starty*10)+9
ink rgb(255,0,0),0
box endx*10,endy*10,(endx*10)+9,(endy*10)+9
rem begin main loop
do
rem obtain mouse coordinates
mx=mousex() : my=mousey()
rem check for left mouse click to place a new block
if mouseclick()=1
for x=0 to 64
for y=0 to 48
if mx>=x*10 and mx<=(x*10)+9 and my>=y*10 and my<=(y*10)+9
if x=startx and y=starty
else
if x=endx and y=endy
else
if rnd(3)=2
blocks(x,y)=1
ink rgb(0,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
endif
endif
endif
next y
next x
endif
rem check for right mouse click to eras an existing block
if mouseclick()=2
for x=0 to 64
for y=0 to 48
if mx>=x*10 and mx<=(x*10)+9 and my>=y*10 and my<=(y*10)+9
blocks(x,y)=0
ink rgb(255,255,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
ink 0,0
box (x*10)+1,(y*10)+1,(x*10)+9,(y*10)+9
endif
next y
next x
endif
rem this is used to generate random blocks all over the screen
if keyed=0
if returnkey()=1
keyed=1
for x=0 to 64
for y=0 to 48
if x=startx and y=starty
else
if x=endx and y=endy
else
if rnd(3)=2
blocks(x,y)=1
ink rgb(0,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
endif
endif
next y
next x
endif
rem this is used to clear all existing blocks
if controlkey()=1
keyed=1
for x=0 to 64
for y=0 to 48
if blocks(x,y)=1
blocks(x,y)=0
ink rgb(255,255,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
ink 0,0
box (x*10)+1,(y*10)+1,(x*10)+9,(y*10)+9
endif
next y
next x
endif
rem you press this when you want to activate pathfinding (I'm going to attempt to explain the pathfinding)
if spacekey()=1
keyed=1
cls
paste image 1,0,0
curx=startx : cury=starty
rem reset all variables and arrays
for x=0 to 64
for y=0 to 48
openg(x,y)=0 : openh(x,y)=0 : openf(x,y)=0 : openp(x,y,1)=0 : openp(x,y,2)=0 : open(x,y)=0
shutg(x,y)=0 : shuth(x,y)=0 : shutf(x,y)=0 : shutp(x,y,1)=0 : shutp(x,y,2)=0 : shut(x,y)=0
path(x,y)=0
next y
next x
rem begin sub loop for pathfinding
repeat
rem close the current point being used, so you do not return to it and reuse it
shut(curx,cury)=1 : open(curx,cury)=0 : shutg(curx,cury)=openg(curx,cury) : shuth(curx,cury)=openh(curx,cury) : shutf(curx,cury)=openf(curx,cury) : shutp(curx,cury,1)=openp(curx,cury,1) : shutp(curx,cury,2)=openp(curx,cury,2)
rem check if block is within array
if curx-1>-1
`check if the point in question is a block or was already used
if blocks(curx-1,cury)=0 and shut(curx-1,cury)=0
`if it hasn't been used open it
open(curx-1,cury)=1
`get the distance from the startpoint to the point in question
openg(curx-1,cury)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx-1,cury)=(abs((curx-1)-endx)*10)+(abs((cury)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx-1,cury)=openg(curx-1,cury)+openh(curx-1,cury)
`Get the parent point coordinates
openp(curx-1,cury,1)=curx : openp(curx-1,cury,2)=cury
endif
endif
if curx+1<65
`check if the point in question is a block or was already used
if blocks(curx+1,cury)=0 and shut(curx+1,cury)=0
`if it hasn't been used open it
open(curx+1,cury)=1
`get the distance from the startpoint to the point in question
openg(curx+1,cury)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx+1,cury)=(abs((curx+1)-endx)*10)+(abs((cury)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx+1,cury)=openg(curx+1,cury)+openh(curx+1,cury)
`Get the parent point coordinates
openp(curx+1,cury,1)=curx : openp(curx+1,cury,2)=cury
endif
endif
if cury-1>-1
`check if the point in question is a block or was already used
if blocks(curx,cury-1)=0 and shut(curx,cury-1)=0
`if it hasn't been used open it
open(curx,cury-1)=1
`get the distance from the startpoint to the point in question
openg(curx,cury-1)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx,cury-1)=(abs((curx)-endx)*10)+(abs((cury-1)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx,cury-1)=openg(curx,cury-1)+openh(curx,cury-1)
`Get the parent point coordinates
openp(curx,cury-1,1)=curx : openp(curx,cury-1,2)=cury
endif
endif
if cury+1<49
`check if the point in question is a block or was already used
if blocks(curx,cury+1)=0 and shut(curx,cury+1)=0
`if it hasn't been used open it
open(curx,cury+1)=1
`get the distance from the startpoint to the point in question
openg(curx,cury+1)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx,cury+1)=(abs((curx)-endx)*10)+(abs((cury+1)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx,cury+1)=openg(curx,cury+1)+openh(curx,cury+1)
`Get the parent point coordinates
openp(curx,cury+1,1)=curx : openp(curx,cury+1,2)=cury
endif
endif
blocked=1
for x=0 to 64
for y=0 to 48
if open(x,y)=1 then blocked=0
next y
next x
rem make the new current point the point close by with the lowest total distance
starter=1
for x=0 to 64
for y=0 to 48
if open(x,y)=1
if starter=1
lowx=x : lowy=y
starter=0
else
if openf(x,y)<openf(lowx,lowy)
lowx=x : lowy=y
endif
endif
endif
next y
next x
rem setup new current point
curx=lowx : cury=lowy
rem end sub loop
until curx=endx and cury=endy or blocked=1
rem sub loop to find the path after finally reaching it by exploring
repeat
rem find path through parents
pathx=curx : pathy=cury
curx=openp(pathx,pathy,1) : cury=openp(pathx,pathy,2)
path(curx,cury)=1
shut(curx,cury)=0
open(curx,cury)=0
rem end sub loop
until curx=startx and cury=starty
rem draw path on screen
for x=0 to 64
for y=0 to 48
`blocks
if blocks(x,y)=1
ink rgb(0,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
`closed
if shut(x,y)=1
ink rgb(255,255,0),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
`open
if open(x,y)=1
ink rgb(0,255,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
`path
if path(x,y)=1
ink rgb(255,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
next y
next x
`start point
ink rgb(0,255,0),0
box startx*10,starty*10,(startx*10)+9,(starty*10)+9
`end point
ink rgb(255,0,0),0
box endx*10,endy*10,(endx*10)+9,(endy*10)+9
endif
else
if spacekey()=0 and controlkey()=0 and returnkey()=0 then keyed=0
endif
rem refresh screen
sync
rem end main loop
loop
-EDIT:
I found out if you enclose just the green brick it freezes, but if there are any other open points enclosed with the green brick, it will work.
--EDIT:
Alrighty, I finally got it completely working. I just had to check if there were blocks all around the green block and if so just skip pathfinding.
Here's the final (I hope):
`+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
`---------------------------------------------------------------------------------------------------------------------
`*********************************************************************************************************************
`Title: A* Pathfinding
`Created by: Pictionaryjr
`Date: Friday, March 12, 2010
`*********************************************************************************************************************
`---------------------------------------------------------------------------------------------------------------------
`+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
`*********************************************************************************************************************
`Controls: Left click to place blocks
` Right click to erase blocks
` Enter key to randomly place blocks
` Control key to clear all blocks
` Spacekey to activate path finding
`*********************************************************************************************************************
`*********************************************************************************************************************
`Colors: Red = End Point
` Green = Start Point
` Yellow = Explored Point
` Cyan = Seen Point
` Magenta = Path
` Blue = Block
`*********************************************************************************************************************
rem setup manual sync and clear screen for default flash
sync on : sync rate 0 : cls
rem setup up all arrays
`openg and shutg are the distances from the start point to the pending point
`openh and shuth are the distances from the end point to the pending point
`openf and shutf are the total distances from the start to the pending point to the end point
`openp and shutp are the parent points of the pending point
`open and shut establish if the pending point is closed or open
`blocks specifies if a block is there
`path is the resulting path after pathfinding is finished
dim openg(63,47) : dim openh(63,47) : dim openf(63,47) : dim openp(63,47,2) : dim open(63,47)
dim shutg(63,47) : dim shuth(63,47) : dim shutf(63,47) : dim shutp(63,47,2) : dim shut(63,47)
dim blocks(63,47) : dim path(63,47)
rem variables
`cury and curx are the current points being used
`endx and endy are the end points
`startx and starty are the starting points
curx=16 : cury=24 : endx=48 : endy=24 : startx=16 : starty=24
rem create the grid
`draw the grid
ink rgb(255,255,255),0
for x=0 to 630 step 10 : line x,0,x,479 : next x
for y=0 to 470 step 10 : line 0,y,639,y : next y
`get an image of the grid
get image 1,0,0,640,479
rem draw the starting and ending points
ink rgb(0,255,0),0
box startx*10,starty*10,(startx*10)+9,(starty*10)+9
ink rgb(255,0,0),0
box endx*10,endy*10,(endx*10)+9,(endy*10)+9
rem begin main loop
do
rem obtain mouse coordinates
mx=mousex() : my=mousey()
rem check for left mouse click to place a new block
if mouseclick()=1
for x=0 to 63
for y=0 to 47
if mx>=x*10 and mx<=(x*10)+9 and my>=y*10 and my<=(y*10)+9
if x=startx and y=starty
else
if x=endx and y=endy
else
if rnd(3)=2
blocks(x,y)=1
ink rgb(0,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
endif
endif
endif
next y
next x
endif
rem check for right mouse click to eras an existing block
if mouseclick()=2
for x=0 to 63
for y=0 to 47
if mx>=x*10 and mx<=(x*10)+9 and my>=y*10 and my<=(y*10)+9
blocks(x,y)=0
ink rgb(255,255,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
ink 0,0
box (x*10)+1,(y*10)+1,(x*10)+9,(y*10)+9
endif
next y
next x
endif
rem this is used to generate random blocks all over the screen
if keyed=0
if returnkey()=1
keyed=1
for x=0 to 63
for y=0 to 47
if x=startx and y=starty
else
if x=endx and y=endy
else
if rnd(3)=2
blocks(x,y)=1
ink rgb(0,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
endif
endif
next y
next x
endif
rem this is used to clear all existing blocks
if controlkey()=1
keyed=1
for x=0 to 63
for y=0 to 47
if blocks(x,y)=1
blocks(x,y)=0
ink rgb(255,255,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
ink 0,0
box (x*10)+1,(y*10)+1,(x*10)+9,(y*10)+9
endif
next y
next x
endif
rem you press this when you want to activate pathfinding (I'm going to attempt to explain the pathfinding)
if spacekey()=1
keyed=1
cls
paste image 1,0,0
rem reset all variables and arrays
for x=0 to 63
for y=0 to 47
openg(x,y)=0 : openh(x,y)=0 : openf(x,y)=0 : openp(x,y,1)=0 : openp(x,y,2)=0 : open(x,y)=0
shutg(x,y)=0 : shuth(x,y)=0 : shutf(x,y)=0 : shutp(x,y,1)=0 : shutp(x,y,2)=0 : shut(x,y)=0
path(x,y)=0
next y
next x
curx=startx : cury=starty : openp(startx,starty,1)=startx : openp(startx,starty,2)=starty
if blocks(startx+1,starty)=1 and blocks(startx-1,starty)=1 and blocks(startx,starty+1)=1 and blocks(startx,starty-1)=1
skip=1
else
skip=0
endif
if skip=0
rem begin sub loop for pathfinding
repeat
rem close the current point being used, so you do not return to it and reuse it
shut(curx,cury)=1 : open(curx,cury)=0 : shutg(curx,cury)=openg(curx,cury) : shuth(curx,cury)=openh(curx,cury) : shutf(curx,cury)=openf(curx,cury) : shutp(curx,cury,1)=openp(curx,cury,1) : shutp(curx,cury,2)=openp(curx,cury,2)
rem check if block is within array
if curx-1>-1
`check if the point in question is a block or was already used
if blocks(curx-1,cury)=0 and shut(curx-1,cury)=0
`if it hasn't been used open it
open(curx-1,cury)=1
`get the distance from the startpoint to the point in question
openg(curx-1,cury)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx-1,cury)=(abs((curx-1)-endx)*10)+(abs((cury)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx-1,cury)=openg(curx-1,cury)+openh(curx-1,cury)
`Get the parent point coordinates
openp(curx-1,cury,1)=curx : openp(curx-1,cury,2)=cury
endif
endif
if curx+1<64
`check if the point in question is a block or was already used
if blocks(curx+1,cury)=0 and shut(curx+1,cury)=0
`if it hasn't been used open it
open(curx+1,cury)=1
`get the distance from the startpoint to the point in question
openg(curx+1,cury)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx+1,cury)=(abs((curx+1)-endx)*10)+(abs((cury)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx+1,cury)=openg(curx+1,cury)+openh(curx+1,cury)
`Get the parent point coordinates
openp(curx+1,cury,1)=curx : openp(curx+1,cury,2)=cury
endif
endif
if cury-1>-1
`check if the point in question is a block or was already used
if blocks(curx,cury-1)=0 and shut(curx,cury-1)=0
`if it hasn't been used open it
open(curx,cury-1)=1
`get the distance from the startpoint to the point in question
openg(curx,cury-1)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx,cury-1)=(abs((curx)-endx)*10)+(abs((cury-1)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx,cury-1)=openg(curx,cury-1)+openh(curx,cury-1)
`Get the parent point coordinates
openp(curx,cury-1,1)=curx : openp(curx,cury-1,2)=cury
endif
endif
if cury+1<48
`check if the point in question is a block or was already used
if blocks(curx,cury+1)=0 and shut(curx,cury+1)=0
`if it hasn't been used open it
open(curx,cury+1)=1
`get the distance from the startpoint to the point in question
openg(curx,cury+1)=shutg(curx,cury)+10
`get the distance from the end point to the point in question
openh(curx,cury+1)=(abs((curx)-endx)*10)+(abs((cury+1)-endy)*10)
`Get the total distance from the start point to the point in question to the end point
openf(curx,cury+1)=openg(curx,cury+1)+openh(curx,cury+1)
`Get the parent point coordinates
openp(curx,cury+1,1)=curx : openp(curx,cury+1,2)=cury
endif
endif
blocked=1
for x=0 to 63
for y=0 to 47
if open(x,y)=1 then blocked=0
next y
next x
rem make the new current point the point close by with the lowest total distance
starter=1
for x=0 to 63
for y=0 to 47
if open(x,y)=1
if starter=1
lowx=x : lowy=y
starter=0
else
if openf(x,y)<openf(lowx,lowy)
lowx=x : lowy=y
endif
endif
endif
next y
next x
rem setup new current point
curx=lowx : cury=lowy
rem end sub loop
until curx=endx and cury=endy or blocked=1
rem sub loop to find the path after finally reaching it by exploring
repeat
rem find path through parents
pathx=curx : pathy=cury
curx=openp(pathx,pathy,1) : cury=openp(pathx,pathy,2)
path(curx,cury)=1
shut(curx,cury)=0
open(curx,cury)=0
rem end sub loop
until curx=startx and cury=starty
endif
rem draw path on screen
for x=0 to 63
for y=0 to 47
`blocks
if blocks(x,y)=1
ink rgb(0,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
`closed
if shut(x,y)=1
ink rgb(255,255,0),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
`open
if open(x,y)=1
ink rgb(0,255,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
`path
if path(x,y)=1
ink rgb(255,0,255),0
box x*10,y*10,(x*10)+9,(y*10)+9
endif
next y
next x
`start point
ink rgb(0,255,0),0
box startx*10,starty*10,(startx*10)+9,(starty*10)+9
`end point
ink rgb(255,0,0),0
box endx*10,endy*10,(endx*10)+9,(endy*10)+9
endif
else
if spacekey()=0 and controlkey()=0 and returnkey()=0 then keyed=0
endif
rem refresh screen
sync
rem end main loop
loop