Who's Online:

  • [Bot]
Now online:
  • 3 guests
  • 1 robot

 

DBPRO CODE - 2D LINE OF SIGHT

 

This code works well for tile based maps or rogue-like rpg text adventures. You simply pick one point of the map and then a second point of the map to see if there is anything blocking the view in between the two points.

 

This code has been ported from c code off the internet over to dbpro code. The link to the c code is:

http://roguebasin.roguelikedevelopment.org/index.php?title=Simple_Line_of_Sight

 

This code snippet should be pretty easier for you to use in your own projects. There are two REPEAT/UNTIL blocks in the code. You will need to change the UNTIL condition to meet your code's requirements for what can block the line of sight view.

 

It's free. Use it however you like. Enjoy!

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
rem LIGN OF SIGHT FUNCTION
rem Ported From: http://roguebasin.roguelikedevelopment.org/index.php?title=Simple_Line_of_Sight
FUNCTION SGN(a)
    IF a<0 THEN EXITFUNCTION -1
ENDFUNCTION 1
 
FUNCTION LOS(StartX,StartY,EndX,EndY) `returns BOOL
    LOCAL t, x, y, ax, ay, sx, sy, dx, dy AS INTEGER
    
    IF StartX=EndX AND StartY=EndY THEN EXITFUNCTION 1
    
    dx = StartX - EndX
    dy = StartY - EndY
    ax = ABS(dx)*2
    ay = ABS(dy)*2
    sx = SGN(dx)
    sy = SGN(dy)
    x = EndX
    y = EndY
    rem The following if statement checks to see if the line
    rem is x dominate or y dominate and loops accordingly
    IF ax > ay
        rem X dominate loop
        t = ay - (ax/2)
        REPEAT
            IF t >= 0
                y = y+sy
                t = t-ax
            ENDIF
            x = x+sx
            t = t+ay
            rem check to see if we are at the player's position
            IF (x=StartX) AND (y=StartY)
                rem return that the player can see the specified map location
                EXITFUNCTION 1
            ENDIF
            rem keep looping until the specified map location is blocked
            rem by an object at the updated x,y coord
        UNTIL array_pfmap(x,y).object<>OBJ_EMPTY_FLOOR
        rem  Unable to see map location
        EXITFUNCTION 0
    ELSE
        rem Y dominate loop
        t = ax - (ay/2)
        REPEAT
            IF t >= 0
                x = x+sx
                t = t-ay
            ENDIF
            y = y+sy
            t = t+ax
            IF (x=StartX) AND (y=StartY)
                EXITFUNCTION 1
            ENDIF
        UNTIL array_pfmap(x,y).object<>OBJ_EMPTY_FLOOR
        EXITFUNCTION 0
    ENDIF
ENDFUNCTION 0