这是一个测试正方形网格上被阻挡的瓦片视线的函数。
你需要自己编写以下函数:
ValidSquare(x,y)(如果瓦片在地图范围内则返回true)
CheckSquare(y,x)(如果瓦片可见则返回true)
Function CheckSquareLOS(ByVal srow, ByVal scol, ByVal trow, ByVal tcol) As Boolean
Dim sx As Long, sy As Long, tx As Long, ty As Long, ex As Long, ey As Long
Dim x As Double, y As Double
' First check if the values are in range
If Not ValidSquare(P(scol, srow)) Then Stop ' : Exit Function
If Not ValidSquare(P(tcol, trow)) Then Stop ' : Exit Function
sx = scol * 3780: sy = srow * 3780
tx = tcol * 3780: ty = trow * 3780
tx = tx - sx: ty = ty - sy: sx = 0: sy = 0: x = scol: y = srow
' Repeat the following until we reach the target square or we are blocked
While (srow <> trow) Or (scol <> tcol)
If ty = 0 Then
' NPrint "Horizontal straight line"
scol = scol + 1 * Sgn(tx)
Else
If tx = 0 Then
' NPrint "Vertical straight line"
srow = srow + 1 * Sgn(ty)
Else
ey = 1890 * Sgn(ty)
ex = sx + (ey - sy) * tx / ty
If Abs(ex) < 1890 Then
sx = ex: sy = -ey: srow = srow + Sgn(ty)
Else
ex = 1890 * Sgn(tx)
ey = sy + (ex - sx) * ty / tx
If Abs(ey) < 1890 Then
sx = -ex: sy = ey: scol = scol + Sgn(tx)
Else
' We must be going through a corner
If Not CheckSquare(srow + Sgn(ty), scol) And Not CheckSquare(srow, scol + Sgn(tx)) Then
CheckSquareLOS = False: Exit Function
End If
sx = -ex: sy = -ey: srow = srow + Sgn(ty): scol = scol + Sgn(tx)
End If
End If
End If
End If
If (srow <> trow) Or (scol <> tcol) Then
If CheckSquare(srow, scol) = False Then
CheckSquareLOS = False: Exit Function
End If
End If
Wend
' If view hasn't been blocked up until now, it must be a clear LOS
CheckSquareLOS = True
End Function