Simple and accurate LOS function for BlitzMax

From RogueBasin
Revision as of 05:40, 12 June 2008 by Duerig (talk | contribs) (Added Gbox's function as a new article.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Simple and accurate LOS function

By Gbox

'// Code is [http://www.Blitzbasic.com BlitzMax]

Function UpdateLOS(m:tMonster, UpdateMap:Int = True)
	If m = Null Then RuntimeError("Null in LOScheck!")
	Local XX:Int, YY:Int '// Clear LOS flag from previous //
	For xx = 0 To MAPWIDTH - 1
		For yy = 0 To MAPHEIGHT - 1
			If Map[xx, yy].InLOS = True Then
				Map[xx, yy].HasSeen = True '// HasSeen cells are marked dark
				Map[xx, yy].InLOS = False
			EndIf
		Next
	Next

	For Local angle:Float = 1 To 360 Step 0.18	'// fractional amount gives better resolution [0.1 causes sight though doors, > 0.2 creates gaps. 0.18 seems the sweet spot
		Local dist:Int = 0
		Local x:Float = Float(m.X) + 0.5 '//M.x and M.y are player/monster current map location
		Local y:Float = Float(m.y) + 0.5 
		Local xmove:Float = Cos(angle)
		Local ymove:Float = Sin(angle)
	
		Repeat
			x = x + xmove
			y = y + ymove
			dist = dist + 1
			If dist >= m.Vision Then Exit  '// M.vision is the range of sight
			If X >= MAPWIDTH Then Exit
			If y >= MAPHEIGHT Then Exit
			If x < 0 Then Exit
			If y < 0 Then Exit
			Map[x, y].InLOS = True
			If Map[x, y].BlockVision Then Exit '// Map[,].Blockvision is for walls/closed doors ect
		Forever
	Next

End Function