lua - Attempting to find the shortest path -
so have following code, thought recurse through nodes , once reaches goal, return shortest. i'm getting stack overflow.
note: getconnectedparts method of part, current setup returns each accessible node.
note2: there path start finish, multiple actually.
my code here, , i'd thankful can provide:
function findpath(start, finish, path) --define table hold paths local paths = {} --make default argument path = path or {start} --loop through connected nodes i,v in ipairs(start:getconnectedparts()) --overflow occurs here --determine if backtracking local loop = false i,vv in ipairs(path) if v == vv loop = true end end if not loop --make path clone local npath = {unpack(path)} if v == finish --if reach end add path paths[#paths+1] = npath else --otherwise add shortest part extending node paths[#paths+1] = findpath(v, finish, npath) end end end --find , return shortest path local lengths = {} i,v in ipairs(paths) lengths[#lengths+1] = #v end local least = math.min(unpack(lengths)) i,v in ipairs(paths) if #v == least return v end end end
oh wow. problem solved. wasn't putting parts inside path itself. sorry confusion.
Comments
Post a Comment