Roguelike - Implementing Bresenham's Line Algorithm

fthmdg

Member
Joined
Dec 23, 2007
Messages
6
Programming Experience
1-3
So I'm working on a little roguelike as a side project. If you don't know what a rougelike is, it's basically an RPG / adventure type game, displayed all in ASCII characters and text.

I want to implement Bresenham's Line Algorithm in order to determine Line of Sight for the player and enemies, basically to determine what the player can see, and whether or not enemies can see the player.

I have a [I believe] working form of the algorithm, but the original code was written in Visual Basic 6.0, and was used to print a line. What I want to do is plot a line between a players X & Y positions, and an enemies X & Y positions. Then walk along the map in that line, and check for anything that would obstruct vision, like walls.

Here's the code I've got right now.
VB.NET:
    Private Sub BresLine(ByVal InitialX As Long, ByVal InitialY As Long, ByVal FinalX As Long, ByVal FinalY As Long)
        ' Bresenham's line algorithm for Microsoft Visual Basic 6.0
        ' Implementation by Robert Lee  July, 2002 Public Domain

        Dim Steep As Boolean
        Dim DeltaX, DeltaY, Delta, StepX, StepY, Coord As Long

        Steep = False
        DeltaX = (FinalX - InitialX)

        If (FinalX - InitialX) > 0 Then
            StepX = 1
        Else
            StepX = -1
        End If

        DeltaY = (FinalY - InitialY)

        If (FinalY - InitialY) > 0 Then
            StepY = 1
        Else
            StepY = -1
        End If

        If DeltaY > DeltaX Then
            Steep = True
            Swap(InitialX, InitialY)
            Swap(DeltaX, DeltaY)
            Swap(StepX, StepY)
        End If

        Delta = (DeltaY * 2) - DeltaX

        For Coord = 0 To DeltaX - 1

            If Steep = True Then
                'VB 6 CODE
                'Me.PSet(InitialY, InitialX)
            Else
                'VB 6 CODE
                'Me.PSet(InitialX, InitialY)
            End If

            While Delta >= 0
                InitialY = InitialY + StepY
                Delta = Delta - (DeltaX * 2)
            End While

            InitialX = InitialX + StepX
            Delta = Delta + (DeltaY * 2)

        Next

        'VB 6 CODE
        'Coord(Me.PSet(FinalX, FinalY))

    End Sub

I've got a swap(int1,int2) function that works, I just don't know how to take this from the algorithm and implement it in my game.

I have an array MapFloor(0 to 79, 0 to 20) that holds the tile information, so what I need it to do is check tile by tile between the two position for any walls.

Any ideas? Thanks in advance
 
Back
Top