Path finding algorithm


function setup()
    iparameter("speed",3,1000,110)
setupspace()
guy = vec2(math.random(2,15),math.random(2,9))
goal = vec2(9,6)
noSmooth()
watch("sparky.karet.x")
watch("sparky.karet.y")
watch("directionnames[sparky.directs[sparky.order]]")
sparky = pathmake(space,guy,goal)
rectMode(CENTER)
end


function draw()
ortho(0,#space*32,0,#space[1]*32)
    background(255, 255, 255, 255)
    fill(84, 84, 84, 255)
   
    
    for i = 1,#space do for j = 1, #space[1] do
        local me = vec3((i-1/2)*32,(j-1/2)*32,sparky.gridbuffer[i][j])
    text(me.z,me.x,me.y)
    if sparky.grid[i][j] == 0 then
        noStroke()
        me2 = vec2((i-1/2)*32,(j-1/2)*32)
        rect(me2.x,me2.y,27,27)
        end
        
        if me.z >= 1 and me.z <= 4 then
            stroke(255, 0, 0, 255)
            strokeWidth(3)
            local meoff = dirchecktable[me.z]*30 + vec2(me.x,me.y)
            line(me.x,me.y,meoff.x,meoff.y)
        end
    end end
    sparky = pathfind(sparky,speed)

end



function setupspace()
local e = -1 -- this is only because letters show up black and its easier to see the map that way
space = {
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,e,e,0,0,0,0,e,e,e,e,0},
{0,0,e,e,e,e,e,e,0,0,e,0},
{0,0,e,e,e,e,e,e,e,e,e,0},
{0,0,e,e,e,e,e,e,0,e,0,0},
{0,e,0,0,0,e,e,0,0,e,0,0},
{0,e,0,0,0,0,e,e,e,e,0,0},
{0,e,e,e,e,0,e,0,e,0,e,0},
{0,0,0,0,e,e,e,e,e,0,0,0},
{0,0,e,e,e,e,e,e,0,e,0,0},
{0,0,e,e,e,e,e,e,0,e,0,0},
{0,e,0,0,0,0,e,0,0,e,0,0},
{0,e,0,0,0,0,e,e,e,e,0,0},
{0,e,e,e,e,0,0,0,e,0,e,0},
{0,0,0,0,e,e,e,e,e,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
}

end


dirchecktable = {vec2(1,0),vec2(0,1),vec2(-1,0),vec2(0,-1)}
directionnames ={"east", "north", "west", "south"}
randorders = {
{1,2,3,4},
{4,3,2,1},
{2,1,4,3},
{3,4,1,2},
}
    
function pathmake(spacey,startq,goalq)
 local spac = {}
spac.grid = spacey
spac.goal = goalq
spac.grid[startq.x][startq.y] = 7

spac.gridbuffer = {}
for i = 1,#spacey do spac.gridbuffer[i] = {}
for j = 1,#spacey[1] do 

spac.gridbuffer[i][j] = 0


end
end

spac.paths = {}
spac.karet = vec2(1,1)
spac.order = 1
spac.directs = randorders[math.random(1,4)]


    return spac
end

function pathfind(spac,limitof)
    if limitof == nil then limitof = 300 end
    local count = limitof
    while 1 do
        local karet = spac.karet
        local mdir = dirchecktable[sparky.directs[sparky.order]]
        
        if spac.grid[karet.x][karet.y] < 0 or spac.grid[karet.x][karet.y] == -2 then
            if spac.grid[karet.x+mdir.x][karet.y+mdir.y] > 0 then
                if spac.grid[karet.x][karet.y] == -2 then 
                    
                        else
                spac.gridbuffer[karet.x][karet.y] = sparky.directs[sparky.order]
                end
                end
        end
        
        
        spac.karet = spac.karet + vec2(0,1)
    if spac.karet.y > #spac.grid[1] then
        spac.karet.y = 1
        spac.karet.x = spac.karet.x + 1
         if spac.karet.x > #spac.grid then
            spac.karet = vec2(1,1)
            spac.order = spac.order + 1
    if spac.order == 5 then
    spac.order = 1
 spac.directs = randorders[math.random(1,4)]
local grid = spac.gridbuffer
for l = 1,#grid do for m = 1,#grid[1] do
    if grid[l][m] >= 1 and grid[l][m] <= 4 then
 spac.grid[l][m] = grid[l][m] end
    end
end 
    end
    end
    end         
        
        count = count -1
        if count == 0 or count == -9999 then break end
    end
    
    return spac 
end


Awesome. I have been thinking of making a tower defense game and this will save me a lot of effort. Thanks.