Thursday, March 2, 2017

Monkey-X - Beginners - Vector Division Helper - code example


Import mojo

' This is the vector class. It has a 
' x and y float variable.
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float = 0,y:Float = 0)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    ' Here we create a 'a,b' variable using the
    ' vectorf class
    Field a:vectorf
    Field b:vectorf
    ' divide is used to divide a vector
    Field divide:Float
    ' cx and cy contain the center x and center y
    ' coordinates of the screen.
    Field cx:Int
    Field cy:Int
    ' time is a variable used to change(refresh)
    ' the information on the screen.
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        Seed = GetDate[4] + GetDate[5]
        cx = DeviceWidth()/2
        cy = DeviceHeight()/2
        ' Here we create a new vector in
        ' a with a new value(x and y)
        a = New vectorf(Rnd(-10,10),Rnd(10,10))
        ' here we create the scale (multiplier)
        divide = Rnd(1.5,3)
    End Method
    Method OnUpdate()        
        time+=1
        If time>5 Then
            time=0
            a = New vectorf(Rnd(-10,10),Rnd(-10,10))
            divide = Rnd(1.5,3)
        End If
    End Method
    Method OnRender()
        ' pointx and y hold the coordinates
        ' that are used to draw on the screen.
        Local pointx:Int
        Local pointy:Int
        Cls 0,0,0 
        SetColor 255,255,255
        ' Here we draw the helper screen part.
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
         DrawText "-X",0,cy
         DrawText "+X",DeviceWidth()-30,cy
         DrawText "-Y",cx,0
         DrawText "+Y",cx,DeviceHeight()-30
         DrawText "0,0",cx,cy,.5,.5
         DrawText "Origin",cx+5,cy-20
        '
        b = divide_vector(a,divide)
        '
        ' Here we draw the vector a.
        pointx = (a.x*13) + cx
        pointy = (a.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "a",pointx,pointy,.5,.5
        ' Here we draw the vector b.
        SetColor 255,255,0
        pointx = (b.x * 13) + cx
        pointy = (b.y * 13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "b = a / "+String(divide)[0..4],pointx,pointy
        '
        SetColor 255,255,255
        Scale 1.2,1.2
        SetAlpha .5
        DrawText "With vector division we divide",0,0
        DrawText "a vector's x and y by a value.",0,20
        DrawText "The scaling can only change the length and not",0,40
        DrawText "the direction.",0,60
        DrawText "Vector Division :",cx,DeviceHeight-200
        DrawText "b.x = a.x / divisor",cx,DeviceHeight-180
        DrawText "b.y = a.y / divisor",cx,DeviceHeight-160
    End Method
End Class

Function divide_vector:vectorf(v:vectorf,divisor:Float)
    Local r:vectorf = New vectorf()
    r.x = v.x / divisor
    r.y = v.y / divisor
    Return r
End Function

Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vector Multiply Helper - code example


Import mojo

' This is the vector class. It has a 
' x and y float variable.
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float = 0,y:Float = 0)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    ' Here we create a 'a,b' variable using the
    ' vectorf class
    Field a:vectorf
    Field b:vectorf
    ' Scale is used to scale a vector
    Field scale:Float
    ' cx and cy contain the center x and center y
    ' coordinates of the screen.
    Field cx:Int
    Field cy:Int
    ' time is a variable used to change(refresh)
    ' the information on the screen.
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        Seed = GetDate[4] + GetDate[5]
        cx = DeviceWidth()/2
        cy = DeviceHeight()/2
        ' Here we create a new vector in
        ' a with a new value(x and y)
        a = New vectorf(Rnd(-5,5),Rnd(-5,5))
        ' here we create the scale (multiplier)
        scale = Rnd(2,3)
    End Method
    Method OnUpdate()        
        time+=1
        If time>5 Then
            time=0
            a = New vectorf(Rnd(-5,5),Rnd(-5,5))
            scale = Rnd(2,3)
        End If
    End Method
    Method OnRender()
        ' pointx and y hold the coordinates
        ' that are used to draw on the screen.
        Local pointx:Int
        Local pointy:Int
        Cls 0,0,0 
        SetColor 255,255,255
        ' Here we draw the helper screen part.
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
         DrawText "-X",0,cy
         DrawText "+X",DeviceWidth()-30,cy
         DrawText "-Y",cx,0
         DrawText "+Y",cx,DeviceHeight()-30
         DrawText "0,0",cx,cy,.5,.5
         DrawText "Origin",cx+5,cy-20
        '
        b = multiply_vector(a,scale)
        '
        ' Here we draw the vector a.
        pointx = (a.x*13) + cx
        pointy = (a.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "a",pointx,pointy,.5,.5
        ' Here we draw the vector b.
        SetColor 255,255,0
        pointx = (b.x * 13) + cx
        pointy = (b.y * 13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "b = a * "+String(scale)[0..4],pointx,pointy
        '
        SetColor 255,255,255
        Scale 1.2,1.2
        SetAlpha .5
        DrawText "With vector multiplication we multiply",0,0
        DrawText "a vector's x and y by a value.",0,20
        DrawText "The scaling can only change the length and not",0,40
        DrawText "the direction.",0,60
        DrawText "Vector Multiplication :",cx,DeviceHeight-200
        DrawText "b.x = a.x * scale",cx,DeviceHeight-180
        DrawText "b.y = a.y * scale",cx,DeviceHeight-160
    End Method
End Class

Function multiply_vector:vectorf(v:vectorf,scalar:Float)
    Local r:vectorf = New vectorf()
    r.x = v.x * scalar
    r.y = v.y * scalar
    Return r
End Function

Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vector Subtraction Helper - code example


Import mojo

' This is the vector class. It has a 
' x and y float variable.
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float = 0,y:Float = 0)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    ' Here we create a 'a,b,c' variable using the
    ' vectorf class
    Field a:vectorf
    Field b:vectorf
    Field c:vectorf
    ' cx and cy contain the center x and center y
    ' coordinates of the screen.
    Field cx:Int
    Field cy:Int
    ' time is a variable used to change(refresh)
    ' the information on the screen.
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        Seed = GetDate[4] + GetDate[5]
        cx = DeviceWidth()/2
        cy = DeviceHeight()/2
        ' Here we create a new vector in
        ' a with a new value(x and y)
        a = New vectorf(Rnd(-10,10),Rnd(-10,10))
        b = New vectorf(Rnd(-10,10),Rnd(-10,10))
    End Method
    Method OnUpdate()        
        time+=1
        If time>5 Then
            time=0
            a = New vectorf(Rnd(-10,10),Rnd(-10,10))
            b = New vectorf(Rnd(-10,10),Rnd(-10,10))
        End If
    End Method
    Method OnRender()
        ' pointx and y hold the coordinates
        ' that are used to draw on the screen.
        Local pointx:Int
        Local pointy:Int
        Local point2x:Int
        Local point2y:Int
        Cls 0,0,0 
        SetColor 255,255,255
        ' Here we draw the helper screen part.
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
         DrawText "-X",0,cy
         DrawText "+X",DeviceWidth()-30,cy
         DrawText "-Y",cx,0
         DrawText "+Y",cx,DeviceHeight()-30
         DrawText "0,0",cx,cy,.5,.5
         DrawText "Origin",cx+5,cy-20
        '
        c = subtract_vector(a,b)
        '
        ' Here we draw the vector a.
        pointx = (a.x*13) + cx
        pointy = (a.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "a",pointx,pointy,.5,.5
        ' Here we draw the vector b.
        point2x = pointx + (b.x * 13)
        point2y = pointy + (b.y * 13)
        DrawCircle point2x,point2y,7
        DrawLine pointx,pointy,point2x,point2y
        DrawText "b",point2x,point2y,.5,.5
        ' Here we draw the vector c.
        SetColor 255,255,0
        pointx = (c.x*13) + cx
        pointy = (c.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "c",pointx,pointy,.5,.5
        SetColor 255,255,255
        '
        SetAlpha .3
        DrawLine pointx,pointy,point2x,point2y
        '
        Scale 1.2,1.2
        SetAlpha .5
        DrawText "With vector subtraction we have 2 vectors that",0,0
        DrawText "get subtracted from each other. vector b is drawn.",0,20
        DrawText "from vector a. Vector c is the line from the origin (0,0)",0,40
        DrawText "to the negative of the line from b To a.",0,60
        DrawText "Vector Subtraction :",cx,DeviceHeight-200
        DrawText "c.x = a.x - b.x",cx,DeviceHeight-180
        DrawText "c.y = a.y - b.y",cx,DeviceHeight-160
    End Method
End Class

Function subtract_vector:vectorf(a:vectorf,b:vectorf)
    Local r:vectorf = New vectorf()
    r.x = a.x - b.x
    r.y = a.y - b.y
    Return r
End Function

Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vector Addition Helper - code example


Import mojo

' This is the vector class. It has a 
' x and y float variable.
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    ' Here we create a 'a,b,c' variable using the
    ' vectorf class
    Field a:vectorf
    Field b:vectorf
    Field c:vectorf
    ' cx and cy contain the center x and center y
    ' coordinates of the screen.
    Field cx:Int
    Field cy:Int
    ' time is a variable used to change(refresh)
    ' the information on the screen.
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        Seed = GetDate[4] + GetDate[5]
        cx = DeviceWidth()/2
        cy = DeviceHeight()/2
        ' Here we create a new vector in
        ' a with a new value(x and y)
        a = New vectorf(Rnd(-10,10),Rnd(-10,10))
        b = New vectorf(Rnd(-10,10),Rnd(-10,10))
    End Method
    Method OnUpdate()        
        time+=1
        If time>3 Then
            time=0
            a = New vectorf(Rnd(-10,10),Rnd(-10,10))
            b = New vectorf(Rnd(-10,10),Rnd(-10,10))
        End If
    End Method
    Method OnRender()
        ' pointx and y hold the coordinates
        ' that are used to draw on the screen.
        Local pointx:Int
        Local pointy:Int
        Local point2x:Int
        Local point2y:Int
        Cls 0,0,0 
        SetColor 255,255,255
        ' Here we draw the helper screen part.
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
         DrawText "-X",0,cy
         DrawText "+X",DeviceWidth()-30,cy
         DrawText "-Y",cx,0
         DrawText "+Y",cx,DeviceHeight()-30
         DrawText "0,0",cx,cy,.5,.5
         DrawText "Origin",cx+5,cy-20
        '
        c = add_vector(a,b)
        '
        ' Here we draw the vector a.
        pointx = (a.x*13) + cx
        pointy = (a.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "a",pointx,pointy,.5,.5
        ' Here we draw the vector b.
        point2x = pointx + (b.x * 13)
        point2y = pointy + (b.y * 13)
        DrawCircle point2x-20,point2y-20,7
        DrawLine pointx,pointy,point2x,point2y
        DrawText "b",point2x-20,point2y-20,.5,.5
        ' Here we draw the vector c.
        SetColor 255,255,0
        pointx = (c.x*13) + cx
        pointy = (c.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "c",pointx,pointy,.5,.5
        SetColor 255,255,255
        '
        Scale 1.2,1.2
        SetAlpha .5
        DrawText "With vector addition we have 2 vectors that",0,0
        DrawText "get added up with each other. vector b is drawn.",0,20
        DrawText "from vector a. Vector c is the line from",0,40
        DrawText "the origin(0,0) to the vector b.",0,60
        DrawText "Vector addition is also known as vector translation.",0,80
        DrawText "Vector addition :",cx,DeviceHeight-200
        DrawText "c.x = a.x + b.x",cx,DeviceHeight-180
        DrawText "c.y = a.y + b.y",cx,DeviceHeight-160
    End Method
End Class

Function add_vector:vectorf(a:vectorf,b:vectorf)
    Local r:vectorf = New vectorf(0,0)
    r.x = a.x + b.x
    r.y = a.y + b.y
    Return r
End Function

Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vectors Helper - code example


Import mojo

' This is the vector class. It has a 
' x and y float variable.
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    ' Here we create a 'a' variable using the
    ' vectorf class
    Field a:vectorf
    ' cx and cy contain the center x and center y
    ' coordinates of the screen.
    Field cx:Int
    Field cy:Int
    ' time is a variable used to change(refresh)
    ' the information on the screen.
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        Seed = GetDate[4] + GetDate[5]
        cx = DeviceWidth()/2
        cy = DeviceHeight()/2
        ' Here we create a new vector in
        ' a with a new value(x and y)
        a = New vectorf(Rnd(-10,10),Rnd(-10,10))
    End Method
    Method OnUpdate()        
        time+=1
        If time>3 Then
            time=0
            a = New vectorf(Rnd(-10,10),Rnd(-10,10))
        End If
    End Method
    Method OnRender()
        ' pointx and y hold the coordinates
        ' that are used to draw on the screen.
        Local pointx:Int
        Local pointy:Int
        Cls 0,0,0 
        SetColor 255,255,255
        ' Here we draw the helper screen part.
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
         DrawText "-X",0,cy
         DrawText "+X",DeviceWidth()-30,cy
         DrawText "-Y",cx,0
         DrawText "+Y",cx,DeviceHeight()-30
         DrawText "0,0",cx,cy,.5,.5
         DrawText "Origin",cx+5,cy-20
        ' Here we draw the vector.
        pointx = (a.x*10) + cx
        pointy = (a.y*10) + cy
        DrawCircle pointx,pointy,10
        DrawLine cx,cy,pointx,pointy
        DrawText "a",pointx,pointy,.5,.5
        DrawText String(a.x)[0..4]+","+String(a.y)[0..4],pointx,pointy+10
        '
        Scale 1.2,1.2
        SetAlpha .5
        DrawText "A vector describes a straight movement in 2D space",0,0
        DrawText "The two dimensions are called X and Y.",0,20
        DrawText "The origin(starting point of vector) is always 0,0",0,40
        DrawText "The words point or vector are the same.",0,60
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Dot Product Helper - code example


Import mojo

' The dot product is used for instance
' in collision functions.
' The dot product is also known as
' a scalar product or inner product.
' It describes the relation between
' two vectors with a single number.

' A vector describes a straight movement in 2d
' space.
' Here with two dimensions (x and y) 
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    Field a:vectorf = New vectorf(8,-2)
    Field b:vectorf = New vectorf(-5,5)
    ' cx and cy contain the center x and y coordinates
    ' of the screen
    Field cx:Int
    Field cy:Int
    ' time is a variable that is used
    ' to see when we need to renew the
    ' screen with new info
    '
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        cx = DeviceWidth() / 2
        cy = DeviceHeight() / 2
    End Method
    Method OnUpdate()  
        time+=1
        If time>3
            time=0
                a = New vectorf(Rnd(-10,10),Rnd(-10,10))        
                b = New vectorf(Rnd(-10,10),Rnd(-10,10))
        End If      
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
        ' pointx and y are the vector coordinates
        ' multiplied with a value
        Local pointx:Int=(a.x*10)+cx
        Local pointy:Int=(a.y*10)+cy        
        DrawCircle pointx,pointy,10
        DrawLine cx,cy,pointx,pointy
         DrawText "a",pointx,pointy
        pointx = (b.x*10) + cx
        pointy = (b.y*10) + cy
        DrawCircle pointx,pointy,10
        DrawLine cx,cy,pointx,pointy
        DrawText "b",pointx,pointy
        ' dp is a variable that contains
        ' the dot product of a and b
        Local dp:Float=dot_product(a,b)
        Scale 1.2,1.2
        DrawText "Dot product of a and b = "+dp,0,0
        DrawText "If the value is 0 then the angle between",0,20
        DrawText "the vectors is 90 Degrees",0,40
        DrawText "When the value is positive then the",0,60
        DrawText "angle is less then 90 degrees",0,80
        DrawText "When the value is negative then the",0,100
        DrawText "angle is greater then 90 degrees",0,120    
        '
        DrawText "dot product is : ",cx,DeviceHeight()-180
        DrawText "a.x * b.x + a.y * b.y",cx,DeviceHeight()-160
    End Method
End Class

Function dot_product:Float(a:vectorf,b:vectorf)
    Return a.x * b.x + a.y * b.y 
End Function

Function Main()
    New MyGame()
End Function

Monday, February 20, 2017

Monkey-X - Wall Tracing on Random Maps - code example


' Wall Tracing from the book Ai For Game Developers.
' Random Maps based on code from Rogue Basin.
'

Import mojo

'
'this is the wall tracing entity
Class entity
    Field tilex:Int,tiley:Int
    Field direction:Int=8
    Field speed:Int
    Method New()
        findstartposition
    End Method
    Method update()
        walltrace
    End Method
    Method walltrace()
        If direction = 4
            If mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            Elseif mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            Elseif mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            Elseif mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            End If
        Elseif direction = 6
            If mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            Elseif mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            Elseif mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            Elseif mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            End If
        Elseif direction = 8
            If mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            Elseif mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            Elseif mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            Elseif mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            End If
        Elseif direction = 2
            If mymap.map[tilex][tiley-1] = 1
                tiley -= 1
                direction = 8
            Elseif mymap.map[tilex-1][tiley] = 1
                tilex -= 1
                direction = 2
            Elseif mymap.map[tilex][tiley+1] = 1
                tiley += 1
                direction = 4
            Elseif mymap.map[tilex+1][tiley] = 1
                tilex += 1
                direction = 6
            End If
        End If


    End Method
    Method findstartposition()
        ' for tracing the wall we need to be sure
        ' that the entity is spawned against a wall
        ' here we check for a random pos on the map
        ' on the floor(1) with a wall(2) at y+1
        Local exitloop:Bool=False
        While exitloop = False
            Local x:Int=Rnd(3,mymap.mapwidth-3)
            Local y:Int=Rnd(3,mymap.mapheight-3)
            If mymap.map[x][y] = 1
            If mymap.map[x][y+1] = 2 
                exitloop = True
                tilex = x
                tiley = y
            End If
            End If
        Wend
    End Method    
    Method draw()
        SetColor 255,0,0
        DrawOval     tilex*mymap.tilewidth,
                    tiley*mymap.tileheight,
                    mymap.tilewidth+1,
                    mymap.tileheight+1
    End Method
End Class

Class map
    Field tilewidth:Float
    Field tileheight:Float
    Field mapwidth:Int
    Field mapheight:Int
    Field screenwidth:Int
    Field screenheight:Int
    Field map:Int[][]
    Method New(    screenwidth:Int,
                screenheight:Int,
                mapwidth:Int,
                mapheight:Int)
        Self.screenwidth = screenwidth
        Self.screenheight = screenheight
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        Self.tilewidth = Float(screenwidth)/Float(mapwidth)
        Self.tileheight = Float(screenheight)/Float(mapheight)
        map = New Int[mapwidth][]
        For Local i=0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        map[mapwidth/2][mapheight/2] = 3 ' 3 is a door
        makemap
    End Method
    Method makemap()
        Local timeout:Int
        While timeout<(mapwidth*mapheight)*20
            timeout+=1
            Local x:Int=Rnd(11,mapwidth-11)
            Local y:Int=Rnd(11,mapheight-11)
            If map[x][y] = 3
                makeroom(x,y)
            End If
        Wend    
        'here we turn doors into walls
        'if they should be walls
        For Local y1=1 Until mapheight-1
        For Local x1=1 Until mapwidth-1
            If map[x1][y1] = 3
            Local cnt:Int=0
            For Local y2=y1-1 To y1+1
            For Local x2=x1-1 To x1+1
                If map[x2][y2] = 2 Then cnt+=1
            Next
            Next
            If cnt>3 Then map[x1][y1] = 2
            End If
        Next
        Next
        'here we turn doors into walls if they
        ' touch tiles that are nothing (0)
        For Local y1=1 Until mapheight-1
        For Local x1=1 Until mapwidth-1
            If map[x1][y1] = 3
            Local cnt:Int=0
            For Local y2=y1-1 To y1+1
            For Local x2=x1-1 To x1+1
                If map[x2][y2] = 0 Then cnt+=1
            Next
            Next
            If cnt>0 Then map[x1][y1] = 2
            End If
        Next
        Next        
        'here we turn the doors into floors
        For Local y1=0 Until mapheight
        For Local x1=0 Until mapwidth
            If map[x1][y1] = 3 Then map[x1][y1] = 1
        Next
        Next
    End Method
    Method makeroom(x:Int,y:Int)
        Local side:String
        If map[x][y-1] = 0
            side="up"
        Elseif map[x+1][y] = 0
            side="right"
        Elseif map[x][y+1] = 0
            side="down"
        Elseif map[x-1][y] = 0
            side="left"
        End If        
        Local w:Int=Rnd(5,10)
        Local h:Int=Rnd(5,10)
        If side="up"
            Local x1:Int=x-w/2
            Local y1:Int=y-h
            If roomfits(x1,y1,w,h)
                insertroom(x1,y1,w,h+1)
                'door up
                map[x1+Rnd(2,w-2)][y1] = 3
                ' door right
                map[x1+w-1][y1+Rnd(2,h-2)] = 3
                'door left
                map[x1][y1+Rnd(2,h-2)] = 3
            End If                        
            
        End If
        If side="right"
            Local x1:Int=x+1
            Local y1:Int=y-h/2
            If roomfits(x1,y1,w,h)
                insertroom(x1-1,y1,w,h)
                'door up
                map[x1+Rnd(2,w-2)][y1] = 3
                'door down
                map[x1+Rnd(2,w-2)][y1+h-1] = 3
                ' door right
                map[x1+w-2][y1+Rnd(2,h-2)] = 3                
            End If
        End If
        If side="left"
            Local x1:Int=x-w
            Local y1:Int=y-h/2
            If roomfits(x1,y1,w,h)
                insertroom(x1,y1,w+1,h)
                'door up
                map[x1+Rnd(2,w-2)][y1] = 3
                'door down
                map[x1+Rnd(2,w-2)][y1+h-1] = 3                
                'door left
                map[x1][y1+Rnd(2,h-2)] = 3

            End If            
        End If
        If side="down"
            Local x1:Int=x-w/2
            Local y1:Int=y+1
            If roomfits(x1,y1,w,h)
                insertroom(x1,y1-1,w,h)
                'door down
                map[x1+Rnd(2,w-2)][y1+h-2] = 3                
                'door left
                map[x1][y1+Rnd(2,h-2)] = 3                
                ' door right
                map[x1+w-1][y1+Rnd(2,h-2)] = 3                

            End If                        
        End If
    End Method
    Method insertroom(x,y,w,h)
        For Local y2=y Until y+h
        For Local x2=x Until x+w
            If map[x2][y2] <> 3 Then map[x2][y2] = 2
        Next
        Next

        For Local y2=y+1 Until y+h-1
        For Local x2=x+1 Until x+w-1
            map[x2][y2] = 1
        Next
        Next
    End Method
    Method roomfits(x:Int,y:Int,w:Int,h:Int)
        For Local y1=y Until y+h
        For Local x1=x Until x+w
            If map[x1][y1] = 1 Then Return False
        Next
        Next
        Return True
    End Method
    Method draw()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            Select map[x][y]
                Case 0
                Case 1'floor
                SetColor 100,100,100
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1
                Case 2'wall
                SetColor 200,200,200
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1
                Case 3'wall
                SetColor 244,244,0
                DrawRect     x*tilewidth,
                            y*tileheight,
                            tilewidth+1,
                            tileheight+1

            End Select
        Next
        Next
    End Method
End Class

Global mymap:map
Global myentity:List<entity> = New List<entity>

Class MyGame Extends App
    Field cnt:Int
    Method OnCreate()
        SetUpdateRate(5)
        Seed = GetDate[4]+GetDate[5]
        newmap
    End Method
    Method OnUpdate()    
        cnt+=1
        For Local i:=Eachin myentity
            i.update
        Next
        If KeyHit(KEY_SPACE) Or cnt>200
            cnt=0
            newmap
        End If    
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymap.draw
        For Local i:=Eachin myentity
            i.draw
        Next
        SetColor 255,255,255
        DrawText "Monkey-X - Wall Tracing on Random Maps",10,10
    End Method
End Class


Function Main()
    New MyGame()
End Function


Function newmap()
    Local s:Int=Rnd(40,100)
    mymap = New map(DeviceWidth,DeviceHeight,s,s)
    myentity = New List<entity>
    For Local i=0 Until Rnd(1,s/3)
        myentity.AddLast(New entity())
    Next
End Function