Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My attempt at a LOS routine
#1
Here's my attempt at making a LOS routine...turned out ok Big Grin.

Green square = you
White squares = visible area
Red squares = not visible area/walls

Arrow keys to move around, ESC exits.

Try playing around with the MAX_DISTANCE constant...

Code:
Declare Sub GetSight ()
Declare Sub DrawVisibleMap ()
Declare Sub ClearVisibleMap ()
Declare Function Collide (argX As Integer, argY As Integer) As Byte


Const TRUE = -1
Const FALSE = 0
Const VISIBLE = 1
Const NOT_VISIBLE = 0
Const R2D = 3.141593 / 180
Const MAX_DISTANCE = 1024
Const PRECISION = 32

Screen 21, 32,, 1

Type ViewerType
    X As Integer
    Y As Integer
End Type

Type EngineType
    MapSizeX As Integer
    MapSizeY As Integer
End Type

Dim Shared VisibleMap(99, 99) As Integer
Dim Shared Map(99, 99) As Integer
Dim Shared Viewer As ViewerType
Dim Shared Engine As EngineType

Viewer.X = 100
Viewer.Y = 100

Read Engine.MapSizeX, Engine.MapSizeY
For tempY = 0 To Engine.MapSizeY - 1
    For tempX = 0 To Engine.MapSizeX - 1
        Read Map(tempX, tempY)
    Next
Next

Do
    If (Multikey(72)) Then
        If (Collide(Viewer.X, Viewer.Y - 1) = FALSE) Then
            If (Collide(Viewer.X + 63, Viewer.Y - 1) = FALSE) Then
                Viewer.Y -= 1
            End If
        End If
    End If
    If (Multikey(80)) Then
        If (Collide(Viewer.X, Viewer.Y + 64) = FALSE) Then
            If (Collide(Viewer.X + 63, Viewer.Y + 64) = FALSE) Then
                Viewer.Y += 1
            End If
        End If
    End If
    If (Multikey(77)) Then
        If (Collide(Viewer.X + 64, Viewer.Y) = FALSE) Then
            If (Collide(Viewer.X + 64, Viewer.Y + 63) = FALSE) Then
                Viewer.X += 1
            End If
        End If
    End If
    If (Multikey(75)) Then
        If (Collide(Viewer.X - 1, Viewer.Y) = FALSE) Then
            If (Collide(Viewer.X - 1, Viewer.Y + 63) = FALSE) Then
                Viewer.X -= 1
            End If
        End If
    End If
    ClearVisibleMap
    GetSight
    DrawVisibleMap
    line (Viewer.X, Viewer.Y)-(Viewer.X + 63, Viewer.Y + 63), rgb(0, 255, 0), bf
    If (Timer - tempStartTime >= 1) Then
     tempStartTime = Timer
     FPS = tempFrames
     tempFrames = 0
    END IF
    tempFrames = tempFrames + 1
    locate 1, 1: print FPS;  " fps"
Loop Until Multikey(1)

Sub GetSight ()
    Dim tempAngle As Integer
    Dim tempDistance As Integer
    Dim tempTileX As Integer, tempTileY As Integer
    
    For tempAngle = 0 To 360
        For tempDistance = 0 To MAX_DISTANCE Step PRECISION
            tempTileX = int((Viewer.X + 32 + cos(tempAngle * r2d) * tempDistance) / 64)
            tempTileY = int((Viewer.Y + 32 + sin(tempAngle * r2d) * tempDistance) / 64)
            If (Map(tempTileX, tempTileY) = 1) Then Exit For
            If (Map(tempTileX, tempTileY) = 0) Then
                VisibleMap(tempTileX, tempTileY) = VISIBLE
            End If
        Next
    Next
End Sub

Sub ClearVisibleMap ()
    Dim tempX As Integer, tempY As Integer
    
    For tempX = 0 To 99
        For tempY = 0 To 99
            VisibleMap(tempX, tempY) = NOT_VISIBLE
        Next
    Next
End Sub

Sub DrawVisibleMap ()
    Dim tempX As Integer, tempY As Integer
    
    For tempY = 0 To Engine.MapSizeY - 1
        For tempX = 0 To Engine.MapSizeX - 1
            Select Case VisibleMap(tempX, tempY)
            Case NOT_VISIBLE
                Line (tempX * 64, tempY * 64)-((tempX * 64) + 63, (tempY * 64) + 63), rgb(255, 0, 0), bf
            Case VISIBLE
                Line (tempX * 64, tempY * 64)-((tempX * 64) + 63, (tempY * 64) + 63), rgb(255, 255, 255), bf
            End Select
        Next
    Next
End Sub

Function Collide (argX As Integer, argY As Integer) As Byte
    If (Map(int(argX / 64), int(argY / 64)) = 1) Then Return True
    Return False
End Function

Data 20, 15
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Data 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1
Data 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
It's the difference between asking someone how much flour goes into pancakes, and handing them a sorry mix of oozing green goo and asking them to fix it." - Deleter

-Founder & President of the No More Religion Threads movement-
Reply
#2
not bad. but its very flickery. Like I go down and certain spots go visible/invisible/visible a bunch of times. This is probably because you aren't casting enough rays to cover every area. Its pretty good though.
[Image: freebasic.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)