Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Line Detection Function


Octohunter
 Share

Recommended Posts

In the spirit of Christmas, I've decided to release this.  Paste it wherever you want (mine is in modCombat).  This little snippet of code essentially detects blocked tiles between the player and the target, making it useful for stopping players from casting spells through walls.
I ripped this straight from one of my projects, so feel free to comment out the "Subscript Out of Range Blocked" text, it was for testing purposes.
Please also note that "range" in this case would be the length of the line.
```
Public Function collisionLine(ByVal index As Long, ByVal target As Long, ByVal range As Long) As Boolean

Dim Run As Long
Dim Rise As Long
Dim r As Long
Dim i As Long
Dim mapNum As Long
Dim pX As Long
Dim pY As Long
Dim tX As Long
Dim tY As Long
Dim nVal As Long

mapNum = GetPlayerMap(index)
collisionLine = True
pX = GetPlayerX(index)
pY = GetPlayerY(index)
    If TempPlayer(index).targetType = TARGET_TYPE_NPC Then
        nVal = Sqr((GetPlayerX(index) - MapNpc(mapNum).Npc(target).x) ^ 2 + (GetPlayerY(index) - MapNpc(mapNum).Npc(target).y) ^ 2)
        Run = (MapNpc(mapNum).Npc(target).x - GetPlayerX(index)) / nVal
        Rise = (MapNpc(mapNum).Npc(target).y - GetPlayerY(index)) / nVal
        tX = MapNpc(mapNum).Npc(target).x
        tY = MapNpc(mapNum).Npc(target).y
    Else
        If TempPlayer(index).target = index Then Exit Function
        nVal = Sqr((GetPlayerX(target) - GetPlayerX(index)) ^ 2 + (GetPlayerY(target) - GetPlayerY(index)) ^ 2)
        Run = (GetPlayerX(target) - GetPlayerX(index)) / nVal
        Rise = (GetPlayerY(target) - GetPlayerY(index)) / nVal
        tX = GetPlayerX(target)
        tY = GetPlayerY(target)
    End If
    If Not isInRange(range, pX, pY, tX, tY) Then
        PlayerMsg index, "Target not in range", BrightRed
    End If
For r = 1 To Int(nVal)
    If pX > tX And pY > tY Then
        If pX + (Run * r) < tX Or pY + (Rise * r) < tY Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX > tX And pY < tY Then
        If pX + (Run * r) < tX Or pY + (Rise * r) > tY Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX < tX And pY > tY Then
        If pX + (Run * r) > tX Or pY + (Rise * r) < tY Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX < tX And pY < tY Then
        If pX + (Run * r) > tX Or pY + (Rise * r) > tY Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX < tX And pY = tY Then
        If pX + (Run * r) > tX Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX > tX And pY = tY Then
        If pX + (Run * r) < tX Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX = tX And pY < tY Then
        If pY + (Rise * r) > tY Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    ElseIf pX = tX And pY > tY Then
        If pY + (Rise * r) < tY Then
            PlayerMsg index, "Subscript Out of Range Blocked!", BrightRed
            Exit For
        End If
    End If

    If Map(mapNum).Tile(pX + (Run * r), pY + (Rise * r)).Type = TILE_TYPE_BLOCKED Then
        PlayerMsg index, "Your attack hit the wall!", BrightRed
        Exit Function
    End If
Next

collisionLine = False

End Function
```
No credit required, it's Christmas :D
Please do comment if this doesn't work.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...