Skip to content

Commit

Permalink
Update Monkey-X - FloodFill Pathfinding - code example.monkey
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakz001 committed Jul 21, 2017
1 parent 5b9c4fe commit 4df639b
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions Monkey-X - FloodFill Pathfinding - code example.monkey
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Import mojo

' This is a class that holds x and y variables.
Class pathnode
Field x:Int,y:Int
Method New(x:Int,y:Int)
Expand All @@ -9,19 +10,29 @@ Class pathnode
End Class

Class MyGame Extends App
'Our map. it holds the walls(-1) and the distance
'from the start to end position.
Field cmap:Int[][]
' map width and height
Field mw:Int=20,mh:Int=20
' tile width and height
Field tw:Float=22,th:Float=18
' our start and end position
Field sx:Int,sy:Int,ex:Int,ey:Int
' this holds our path
Field path:List<pathnode>
Method OnCreate()
SetUpdateRate(1)
' create a new map array
cmap = New Int[mw][]
For Local i:=0 Until mw
cmap[i] = New Int[mh]
Next
' set the tile width and height.
tw = DeviceWidth() / Float(mw)
th = DeviceHeight() / Float(mh)
' get a random seed so every time we run
' this program we get a different result.
Seed = GetDate[4] + GetDate[5]
End Method
Method OnUpdate()
Expand All @@ -47,12 +58,15 @@ Class MyGame Extends App
Next
Next
Next

For Local y:=0 Until mh Step 5
For Local x:=0 Until mw
cmap[x][y] = 0
Next
Next



'find start and end position
Repeat
sx = Rnd(0,mw)
Expand All @@ -68,58 +82,101 @@ Class MyGame Extends App

'flood the map with distance from
'sx and sy
' Create a list with a class inside it (the class has
' the x and y variables)
Local ol:List<pathnode> = New List<pathnode>
' Add the start position on the list
ol.AddLast(New pathnode(sx,sy))
' set the cloes map at the start position to distance 1
cmap[sx][sy] = 1
' some helper arrays. We can determine the top,right,and bottom
' and left position cells with these numbers.
Local dx:Int[] = [0,1,0,-1]
Local dy:Int[] = [-1,0,1,0]
' While there is contents in the list
While ol.Count <> 0
' Get the current location
Local x1:Int=ol.First.x
Local y1:Int=ol.First.y
' Remove the current location from the list
ol.RemoveFirst
' Get 4 new positions around the current positions
For Local i:=0 Until 4
' Set new x and y
Local nx:Int=x1+dx[i]
Local ny:Int=y1+dy[i]
' If the coordinates are inside the map
If nx>=0 And ny>=0 And nx<mw And ny<mh
' If the closedmap is not written to yet
If cmap[nx][ny] = 0
' Set the new distance based on the current distance
cmap[nx][ny] = cmap[x1][y1] + 1
' Add new position to the list
ol.AddLast(New pathnode(nx,ny))
End If
End If
Next
Wend

' Make the path
' Make the path. Here we start at the end position
' and find the lowest value around our current position
' and so on until we are at the start position.
' Get the current position
Local x1:Int=ex,y1:Int=ey
' Reset our path list
path = New List<pathnode>
' Set the first path contents(end x and y)
path.AddLast(New pathnode(x1,y1))
' Little counter for if we enter an infinite loop
Local cnt:Int=0
' While we are not near the map distance of 1
While cmap[x1][y1] > 1
' Get tge current poditions distance
Local lowest:Int=cmap[x1][y1]
' Create 2 new variables that hold the new location
Local nx:Int,ny:Int
' If the position left of our current position is on the map
' and is not a wall and has a distance smaller that of our
' current position.
If x1-1>=0 And cmap[x1-1][y1]>-1 And cmap[x1-1][y1] < lowest Then
' set new position
nx=x1-1
ny=y1
' set new lowest variable
lowest = cmap[nx][ny]
End If
' If the position right of our current position is on the map
' and is not a wall and has a distance smaller that of our
' current position.
If x1+1<mw And cmap[x1+1][y1]>-1 And cmap[x1+1][y1] < lowest
nx=x1+1
ny=y1
lowest = cmap[nx][ny]
End If
' If the position top of our current position is on the map
' and is not a wall and has a distance smaller that of our
' current position.
If y1-1>=0 And cmap[x1][y1-1]>-1 And cmap[x1][y1-1] < lowest
nx=x1
ny=y1-1
lowest = cmap[nx][ny]
End If
' If the position at the bottom of our current position is on the map
' and is not a wall and has a distance smaller that of our
' current position.
If y1+1 < mh And cmap[x1][y1+1]>-1 And cmap[x1][y1+1] < lowest
nx=x1
ny=y1+1
lowest = cmap[nx][ny]
End If
' Here we should have a new position so we put it into
' our current position
x1 = nx
y1 = ny
' Add the next position to the bottom of our path list
path.AddLast(New pathnode(x1,y1))
' If we can not go anywhere then exit this loop if we have done
' a thousand loops.
cnt+=1
If cnt>1000 Then Exit
Wend
Expand All @@ -133,7 +190,6 @@ Class MyGame Extends App
If cmap[x][y] = -1 Then
DrawRect x*tw,y*th,tw,th
End If
' DrawText cmap[x][y],x*tw,y*th
Next
Next
'draw the path
Expand Down

0 comments on commit 4df639b

Please sign in to comment.