TGC Codebase Backup



[Tier 1] Tile based line of sight by Cliff Mellangard 3DEGS

20th Feb 2013 17:48
Summary

I have only converted this from dbp by another author todd riggins.



Description

I neaded this for my dungeon game and converted it.
Simply change the map array to the array name you use.
My map used true as a open segment while the original used false.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    FUNCTION LOS(StartX,StartY,EndX,EndY) `returns BOOL
   IF StartX=EndX then if StartY=EndY THEN EXITFUNCTION 1
   dx = StartX - EndX
   dy = StartY - EndY
   ax = ABS(dx)*2
   ay = ABS(dy)*2
   sx = losSGN(dx)
   sy = losSGN(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 GridMap[x,y].BlockType=0
      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 GridMap[x,y].BlockType=0
      EXITFUNCTION 0
   ENDIF
ENDFUNCTION 0

FUNCTION losSGN(a)
   IF a<0 THEN EXITFUNCTION -1
ENDFUNCTION 1