After downloading a MsgBox Pluggin I managed to get it working. Although with problems.
Here is an example of what I mean:
sync on
`images
box 0,0,30,30
get image 1,0,0,32,32
cls
box 0,0,30,30,rgb(150,150,150),rgb(150,150,150),rgb(150,150,150),rgb(150,150,150)
get image 2,0,0,32,32
cls
box 0,0,30,30,rgb(190,190,190),rgb(190,190,190),rgb(190,190,190),rgb(190,190,190)
get image 3,0,0,32,32
cls
box 0,0,30,30,rgb(120,190,190),rgb(120,190,190),rgb(120,190,190),rgb(120,190,190)
get image 4,0,0,32,32
cls
`globals
global mX#
global mY#
Global StartX
Global FiniX
Global StartY
Global FiniY
global OpenListTop as integer
global TotalOpen
global ParentTop
global ClosedListTop as integer
global TotalClosed
global Path
type List_t
x as integer
y as integer
Gcost as integer
Hcost as integer
Cost as integer
endtype
type Parent_t
x as integer
y as integer
endtype
dim OpenList(10000) as List_t ` Prefer to make this big enough rather than try to grow it dynamically
dim ClosedList(1000) as List_t
dim Parents(1000) as Parent_t
dim ParentX(100,100)
dim ParentY(100,100)
type mgrid
xpos as integer
ypos as integer
tval as integer
endtype
dim map(15,15) as mgrid
`SetMap X and Y CoORDs
for mapy = 0 to 15
for mapx = 0 to 15
map(mapx,mapy).xpos=mapx * 32
map(mapx,mapy).ypos=mapy * 32
map(mapx,mapy).tval= 1
next mapx
next mapy
do
cls
`record mousex/y
mX#=mouseX():mY#=mouseY()
`MouseClick 1 - SetsStart Point
`Shift KEY -Sets End Pont
`ControlKey -DrawsWalls
`This example will fail if you set multiple start/finish points
if mouseclick()=1
SelectedX=int(mX#/32)
SelectedY=int(mY#/32)
map(SelectedX,SelectedY).tval=0
StartX=SelectedX
StartY=SelectedY
endif
if mouseclick()=2
SelectedX=int(mX#/32)
SelectedY=int(mY#/32)
map(SelectedX,SelectedY).tval=1
endif
if shiftkey()=1
SelectedX=int(mX#/32)
SelectedY=int(mY#/32)
map(SelectedX,SelectedY).tval=2
FiniX=SelectedX
FiniY=SelectedY
endif
if controlkey()=1
SelectedX=int(mX#/32)
SelectedY=int(mY#/32)
map(SelectedX,SelectedY).tval=10
endif
`Space KEY Works Out Path
if spacekey()= 1
GetPath(StartX,StartY,FiniX,FiniY)
endif
`DrawMap
for dmapy = 0 to 15
for dmapx = 0 to 15
if map(dmapx,dmapy).tval=0 then paste image 1,map(dmapx,dmapy).xpos,map(dmapx,dmapy).ypos
if map(dmapx,dmapy).tval=1 then paste image 2,map(dmapx,dmapy).xpos,map(dmapx,dmapy).ypos
if map(dmapx,dmapy).tval=2 then paste image 3,map(dmapx,dmapy).xpos,map(dmapx,dmapy).ypos
if map(dmapx,dmapy).tval=10 then paste image 4,map(dmapx,dmapy).xpos,map(dmapx,dmapy).ypos
text dmapx*32,dmapY*32,str$(ParentX(dmapx,dmapy))+":"+str$(ParentY(dmapx,dmapy))
next dmapx
next dmapy
sync
loop
Function GetPath(StartX as integer , StartY as integer , FiniX as integer , FiniY as integer )
`msgbox("Start X: "+str$(StartX))
`msgbox("Start Y: "+str$(StartY))
`msgbox("Fini X: "+str$(FiniX))
`msgbox("Fini Y: "+str$(FiniY))
`Reset Var
InitialiseOpenList()
` 1. Add start-node to the open list.
Gcost = 0
Hcost = getHCost(StartX, StartY, FiniX, FiniY)
AddToOpenList(StartX, StartY, Gcost, Hcost)
` REPEAT FOLLOWING:
While OpenListEmpty() = 0 or Path = 0
` 2. Look in the open list. Which has lowest estimated total cost value? This becomes your current node.
CurrentNode as List_t
CurrentRef = SearchOpenList()
`msgbox("CurrentRef: "+str$(currentref))
CurrentNode = OpenList(CurrentRef)
`msgbox("CurrentNode X: "+str$(currentNode.x))
`msgbox("CurrentNode Y: "+str$(currentNode.y))
if currentNode.x = finiX and currentNode.y = finiY then Path = 1 : exit
` 3. Remove the current node from the open list and add it to the closed list.
RemoveFromOpenList(CurrentRef)
`msgbox("After Remove, CurrentRef: "+str$(SearchOpenList())
AddToClosedList(CurrentNode.x, CurrentNode.y, CurrentNode.gcost, CurrentNode.hcost)
` 4. FOR all outgoing connections from this current node:
ParentX = CurrentNode.x
ParentY = CurrentNode.y
`msgbox("Parent X: "+str$(ParentX))
`msgbox("Parent Y: "+str$(ParentY))
For b = ParentY - 1 to ParentY + 1
For a = ParentX - 1 to ParentX + 1
`msgbox("Loop")
`msgbox("Parent X: "+str$(a))
`msgbox("Parent Y: "+str$(b))
` 1. IF NOT passable OR on closed list, THEN ignore
if a < 15 and a > 0 and b > 0 and b < 15
If Map(a,b).tVal <> 10 or InClosed(a,b) <> -1
` 2. IF NOT in open list, ADD to open list. Make the current node the parent of this node.
If InOpen(a,b) = 0
` msgbox("Not In Open")
cGcost =GetGCost(a,b,ParentX,ParentY)
cHcost= GetHCost(a, b, FiniX, FiniY)
AddToOpenList(a,b,cGcost,cHcost)
ParentX(a,b) = ParentX
ParentY(a,b) = ParentY
AddParentCoord(ParentX,ParentY)
` msgbox("Parents for this Node are X: "+str$(ParentX))
` msgbox("Parents for this Node are Y: "+str$(ParentY))
Endif
` 3. IF in open list already, check to see if the path to that node has a smaller cost-so-far.
` If so, that’s a better path. So make the current node the parent of this node.
OpenIndex = InOpen(a,b)
` msgbox("Open Index: "+str$(OpenIndex))
If OpenIndex > -1
`msgbox("Already In Open")
If AdditionalPath < OpenList(OpenIndex).Gcost
` msgbox("Set New Cost")
parentX(a,b) = ParentX
parentY(a,b) = ParentY
OpenList(OpenIndex).Gcost = AdditionalPath
`Update F Cost
For FindItem = 0 to TotalOpen
If OpenList(FindItem).x = a and OpenList(FindItem).y = b
OpenList(FindItem).Cost = OpenList(FindItem).Gcost + OpenList(FindItem).Hcost
Endif
Next
Endif
Endif
endif
EndIf
Next
Next
` 5. STOP if target node is added to the closed list (i.e. path found) OR open list has nothing in it
` anymore (i.e. no path found)
If OpenListEmpty() = 0 then path = 1
EndWhile
` 6. Get the path either by using the parent node method, or you would have a
` list with the path in it by the time you get to the target.
` 7. Reverse the list.
` 8. Finish: You now have a list with the nodes in correct order, from start to target.
EndFunction
function msgbox(Sstring as string)
x=displayMessage(Sstring,"Message",iconInfo(),btnOk())
endfunction
Function InOpen(oX as integer, oY as integer)
local aCurrentRef
For chk = 0 to OpenListTop
If OpenList(chk).x = oX and OpenList(Chk).y = oY then aCurrentRef = chk
Next
endfunction aCurrentRef
Function InClosed(cX as integer, cY as integer)
local bCurrentRef = -1
For chk = 0 to 1000
If ClosedList(chk).x = cX and ClosedList(Chk).y = cY then bCurrentRef = chk
Next
endfunction bCurrentRef
Function GetHCost(Xpos as integer ,Ypos as integer ,TargetX as integer ,TargetY as integer )
Local H
H = 10*(Abs(Xpos - TargetX) + Abs(Ypos - TargetY))
Endfunction H
Function GetGCost(Xpos,Ypos,ParentX,ParentY)
Local addedGCost
If Abs(Xpos-ParentX) = 1 And Abs(YPos-ParentY) = 1
addedGCost = 14
Else
addedGCost = 10
EndIf
Endfunction addedGCost
`OpenListTop set to -1 if there are no items so.......
function InitialiseOpenList()
OpenListTop = -1
ClosedListTop = -1
ParentTop = -1
endfunction
`It can be picked up by this function and return Empty as 1
function OpenListEmpty()
local Empty = 0
if OpenListTop < 0 then Empty = 1
endfunction Empty
`Increases Global OpenListTop then adds items
`another reason to start at -1
function AddToOpenList(x as integer, y as integer, Gcost as integer, Hcost as integer)
local NewItem as List_t
NewItem.x = x
NewItem.y = y
NewItem.Gcost = Gcost
NewItem.Hcost = Hcost
NewItem.Cost = Gcost + Hcost
inc OpenListTop
inc TotalOpen
OpenList(OpenListTop) = NewItem
endfunction
function AddParentCoord(cParentX,cParentY)
local NewItem as Parent_t
NewItem.x = cParentX
NewItem.y = cParentY
inc ParentTop
Parents(ParentTop) = NewItem
endfunction
function AddToClosedList(x as integer, y as integer, Gcost as integer, Hcost as integer)
local NewItem as List_t
NewItem.x = x
NewItem.y = y
NewItem.Gcost = Gcost
NewItem.Hcost = Hcost
NewItem.Cost = Gcost + Hcost
inc ClosedListTop
inc TotalClosed
ClosedList(ClosedListTop) = NewItem
endfunction
`Searches through OpenLists cost and returns Index of the smallest Cost through 'Current'
`Only increases Current if the OpenList cost being searched is higher than the CurrentCost (default first on array)
`Starting at 0 then through to OpenListTop (Total Used Items in array)
function SearchOpenList()
local Current as integer
local CurrentCost as integer
local i as integer
Current = 0
CurrentCost = OpenList(0).Cost
for i = 1 to OpenListTop
if OpenList(i).Cost < CurrentCost
Current = i
CurrentCost = OpenList(i).Cost
endif
next
endfunction Current
`If the Index given to the function is smaller than the index count of the Open Items then the
`OpenList(Index) value is set to the value of OpenList(global OpenlistTop)
`This Index count of OpenList is then reduced by 1 and in essence is deleted
function RemoveFromOpenList(Index as integer)
if Index < OpenListTop
OpenList(Index) = OpenList(OpenListTop)
endif
dec OpenListTop
endfunction
Use mouse click (1) to set a Start
Use ShiftKey to set an End point
Use ControlKey to draw walls
Use Space to draw path.
The Numbers on the grid are the X:Y of the Parent Nodes. Which if I understand right when thrown into reverse order will give me my path. For some reason when you exit from the program it will just crash. Might have something to do with no clearing/resetting some var. I will clean it up and post something cleaning in the future but for now this hopefully will be useful to anyone reading in on this post trying to learn from it.
Thank you IanM for your help as well.