Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tMap=setmetatable({
- "01000000000000000101",
- "01000000000000000101",
- "01111100000000000101",
- "00000111111111000101",
- "11110110000001000101",
- "00000110111101000101",
- "01111110001101000101",
- "01000011101101000101",
- "01000000101101000101",
- "01111111101101000101",
- "00000000001101000101",
- "11111111111101111101",
- "00000000000100001101",
- "00000000000101111101",
- "00000000000101111101",
- "00000000000100000001",
- "00000000000111111111"
- },{__call=function(self,x,y)
- if self[y] and x<=#self[y] then
- return string.sub(self[y],x,x)
- end
- end})
- local function fPath(map,targx,targy)
- --[VARS]
- local path={x=1,y=1,""}
- local target={x=targx or 1,y=targy or 1}
- local displacement=setmetatable({},{__index=function(self,index) if path[index] and target[index] then return path[index]-target[index] end end,__newindex=function() return nil end})
- local checked=setmetatable({{x=1,y=1}},{__call=function(self,x,y) for k,v in pairs(self) do if v.x==x and v.y==y then return true end end return false end})
- local directions_mt={__index=function(self,index) if index=="x" or index=="y" then return 0 end end}
- local directions=setmetatable({{x=1,"r"},{x=-1,"l"},{y=1,"d"},{y=-1,"u"}},{__call=function(self,direction)
- if direction then
- for k,v in pairs(self) do
- if v[1]==direction then
- return v
- end
- end
- else
- local x
- local y
- for k,v in pairs(self) do
- if (displacement.x>0 and v.x<0) or (displacement.x<0 and v.x>0) then
- x=v
- elseif (displacement.y>0 and v.y<0) or (displacement.y<0 and v.y>0) then
- y=v
- end
- end
- return x,y
- end
- end})
- for k,v in pairs(directions) do setmetatable(v,directions_mt) end
- --[FUNCTIONS]
- local render=function()
- term.clear()
- for k,v in pairs(map) do
- term.setCursorPos(1,k)
- print(({string.gsub(string.gsub(v,"0","."),"1","#")})[1])
- end
- print(path.x..", "..path.y)
- term.setCursorPos(path.x,path.y)
- write("*")
- end
- --[EXECUTION]
- while true do
- local x,y=directions()
- if x and map(path.x+x.x,path.y)=="0" and not checked(path.x+x.x,path.y) then
- path.x=path.x+x.x
- path[1]=path[1]..x[1]
- table.insert(checked,{x=path.x,y=path.y})
- elseif y and map(path.x,path.y+y.y)=="0" and not checked(path.x,path.y+y.y) then
- path.y=path.y+y.y
- path[1]=path[1]..y[1]
- table.insert(checked,{x=path.x,y=path.y})
- else
- local moved=false
- for k,v in pairs(directions) do
- if v~=x and v~=y and map(path.x+v.x,path.y+v.y)=="0" and not checked(path.x+v.x,path.y+v.y) then
- moved=true
- path.x=path.x+v.x
- path.y=path.y+v.y
- path[1]=path[1]..v[1]
- table.insert(checked,{x=path.x,y=path.y})
- break
- end
- end
- if not moved then
- if path.x==1 and path.y==1 then
- term.clear()
- term.setCursorPos(1,1)
- print("no way to get to target...")
- break
- end
- path.x=path.x-directions(string.sub(path[1],-1)).x
- path.y=path.y-directions(string.sub(path[1],-1)).y
- path[1]=string.sub(path[1],1,-2)
- end
- end
- term.setCursorPos(1,12)
- if path.x==target.x and path.y==target.y then
- term.clear()
- term.setCursorPos(1,1)
- print("target found, path:")
- print(path[1])
- break
- end
- sleep(0.1)
- render()
- end
- end
- fPath(tMap,19,1)
Advertisement
Add Comment
Please, Sign In to add comment