Precalculated line of sight

This code is useful for doing line of sight calculations. Rather than doing the line check every frame, you can run every possible combination once and save the results to a 4-dimensional table. This also includes an example implementation.



-- Use this function to perform your initial setup
function setup()
  shadows = firstshadegen()
iparameter("xxx",0,12)
iparameter("yyy",0,12)

end

-- This function gets called once every frame
function draw()
background(74, 74, 74, 255)
 map = makeshademap()
local merr = 0 --shadowcastmap[1][1]
if merr ~= 0 then map = merr end
map = shadows[xxx][yyy]
    for i = 0,16 do
        for j = 0,16 do
            if map[i][j]> 0 then
            ellipse(33+i*44,33+j*44,11+map[i][j]/4)
            end
       end 
    end
end

-- below this, i reccomend putting in a seperate tab    
        
function firstshadegen()

return genshade()
    end
    
function rou(inp)
    return math.floor(inp+0.5)
    end
    
function genshade()
local shadetable = {}

        local map = {}
    for i = 0,16 do map[i] = {} 
    for j = 0,16 do 
    if vec2(i,j):len()<16 then
    map[i][j] = makeshademap()  
    else
    map[i][j] = 0    
     end
    -- //you only actually need to do one quadrant and repeat it 
            --into the others
    end end
    
    for i = 0,12 do
        for j = 0,12 do
            if map[i][j] == 0 then else
                local testbox = vec2(i,j)
                local parentmap =  map[i][j]
                for k = 0,16 do
                for l = 0,16 do
                    
                     local diff = vec2(k,l):normalize()
                    local start = vec2(0,0)
                    for m = 0, vec2(k,l):len() do
                        start = start + diff
                        if vec2(rou(start.x),rou(start.y))== testbox
                        then
                        parentmap[k][l] = 0
                        break
                        
                        end
                    end
                end
                end
            end
        end
    end
return map
end

function makeshademap()
        local map = {}
    for i = 0,16 do map[i] = {} 
    for j = 0,16 do map[i][j] = (1-vec2(i,j):len()/16)*100
    end end
    return map
    end