Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created November 21, 2015 01:36
Show Gist options
  • Save dermotbalson/ae4dbccc00fb061e4376 to your computer and use it in GitHub Desktop.
Save dermotbalson/ae4dbccc00fb061e4376 to your computer and use it in GitHub Desktop.
LOS Tiles
--given a 2D table "tiles" with a cell for each tile on the ground, and each cell containing a list of objects
--in that cell,find all cells which are in line of sight between points A and T
--s is cellsize (square)
--tiles is a 2D table
--results will be passed back in a table
--so if tile[3][4] is in line of sight, then (whatever is in) tiles[3][4] will be added to the results
function GetTiles(T,A,s,tiles)
local x0,y0,x1,y1=T.x/s,T.z/s,A.x/s,A.z/s --tile positions of the two points
local dx = abs(x1-x0)
local dy = abs(y1-y0)
local huge=99999
local tt={}
local x = floor(x0)
local y = floor(y0)
local n = 1
local x_inc, y_inc, error
if dx == 0 then
x_inc = 0
error = huge
elseif x1 > x0 then
x_inc = 1
n = n + floor(x1) - x
error = (floor(x0) + 1 - x0) * dy
else
x_inc = -1
n = n + x - floor(x1)
error = (x0 - floor(x0)) * dy
end
if dy == 0 then
y_inc = 0
error =error - huge
elseif (y1 > y0) then
y_inc = 1
n = n + floor(y1) - y
error = error - (floor(y0) + 1 - y0) * dx
else
y_inc = -1;
n = n + y - floor(y1)
error = error - (y0 - floor(y0)) * dx
end
for i = n,1,-1 do
tt[#tt+1]=tiles[x][y]
if error > 0 then
y = y + y_inc
error = error - dx
else
x = x + x_inc
error = error + dy
end
end
return tt
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment