KaoSDlanor

pathfinder API

Feb 7th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.21 KB | None | 0 0
  1. function edit(tMap)
  2.     tMap=tMap or {}
  3.     tMap.newLayer=function(self)
  4.         local wd,ht
  5.         local run=false
  6.         while not wd and not ht do
  7.             term.clear()
  8.             term.setCursorPos(1,1)
  9.             if run then
  10.                 print("Incorrect input\n")
  11.             end
  12.             print("Enter the dimensions of this layer")
  13.             write("x: ")
  14.             wd=tonumber(read())
  15.             write("y: ")
  16.             ht=tonumber(read())
  17.             run=true
  18.         end
  19.         local layer={}
  20.         for y=1,ht do
  21.             layer[y]=string.rep("1",wd)
  22.         end
  23.         self[#self+1]=layer
  24.         self.selection=self.selection or {}
  25.         self.selection.layer=#self
  26.         if not self(self.selection.x,self.selection.y,self.selection.layer) then
  27.             self.selection.x=1
  28.             self.selection.y=1
  29.         end
  30.     end
  31.     tMap.remLayer=function(self)
  32.         term.clear()
  33.         term.setCursorPos(1,1)
  34.         print("Are you sure you want to remove this layer? [y|n]")
  35.         while true do
  36.             local evts={os.pullEvent()}
  37.             if evts[1]=="key" and evts[2]==21 then
  38.                 table.remove(self,self.selection.layer)
  39.                 self.selection.layer=self.selection.layer-1
  40.                 if not self(self.selection.x,self.selection.y,self.selection.layer) then
  41.                     self.selection.x=1
  42.                     self.selection.y=1
  43.                 end
  44.                 break
  45.             elseif evts[1]=="key" and evts[2]==49 then
  46.                 break
  47.             end
  48.         end
  49.     end
  50.     setmetatable(tMap,{__call=function(self,x,y,z)
  51.             if self[z] and self[z][y] and x<=#self[z][y] then
  52.                     return string.sub(self[z][y],x,x)
  53.             end
  54.     end})
  55.  
  56.     if not tMap[1] then
  57.         tMap:newLayer()
  58.     end
  59.     if not tMap.selection then
  60.         tMap.selection={}
  61.         tMap.selection.layer=tMap.selection.layer or 1
  62.         tMap.selection.x=tMap.selection.x or 1
  63.         tMap.selection.y=tMap.selection.y or 1
  64.     end
  65.     local function render()
  66.         term.clear()
  67.         term.setCursorPos(1,1)
  68.         for y=1,#tMap[tMap.selection.layer] do
  69.             print(tMap[tMap.selection.layer][y])
  70.         end
  71.         term.setCursorPos(tMap.selection.x,tMap.selection.y)
  72.         write("X")
  73.         local scp=term.setCursorPos
  74.         term.setCursorPos=function(x,y)
  75.             return scp(#tMap[tMap.selection.layer][1]+1+x,y)
  76.         end
  77.         term.setCursorPos(1,1)
  78.         print("layer: "..tMap.selection.layer)
  79.         print("["..tMap.selection.x..","..tMap.selection.y.."] = "..tMap(tMap.selection.x,tMap.selection.y,tMap.selection.layer))
  80.         term.setCursorPos=scp
  81.     end
  82.  
  83.     while true do
  84.         render()
  85.         local evts={os.pullEvent()}
  86.         if evts[1]=="key" and evts[2]==200 and tMap.selection.y>1 then
  87.             tMap.selection.y=tMap.selection.y-1
  88.         elseif evts[1]=="key" and evts[2]==208 and tMap.selection.y<#tMap[tMap.selection.layer] then
  89.             tMap.selection.y=tMap.selection.y+1
  90.         elseif evts[1]=="key" and evts[2]==203 and tMap.selection.x>1 then
  91.             tMap.selection.x=tMap.selection.x-1
  92.         elseif evts[1]=="key" and evts[2]==205 and tMap.selection.x<#tMap[tMap.selection.layer][tMap.selection.y] then
  93.             tMap.selection.x=tMap.selection.x+1
  94.         elseif evts[1]=="key" and (evts[2]==11 or evts[2]==82) then
  95.             local str=tMap[tMap.selection.layer][tMap.selection.y]
  96.             tMap[tMap.selection.layer][tMap.selection.y]=string.sub(str,1,tMap.selection.x-1).."0"..string.sub(str,tMap.selection.x+1)
  97.         elseif evts[1]=="key" and (evts[2]==2 or evts[2]==79) then
  98.             local str=tMap[tMap.selection.layer][tMap.selection.y]
  99.             tMap[tMap.selection.layer][tMap.selection.y]=string.sub(str,1,tMap.selection.x-1).."1"..string.sub(str,tMap.selection.x+1)
  100.         elseif evts[1]=="key" and evts[2]==210 then
  101.             tMap:newLayer()
  102.         elseif evts[1]=="key" and evts[2]==211 then
  103.             tMap:remLayer()
  104.         elseif evts[1]=="key" and evts[2]==78 and tMap.selection.layer<#tMap then
  105.             tMap.selection.layer=tMap.selection.layer+1
  106.             if not tMap(tMap.selection.x,tMap.selection.y,tMap.selection.layer) then
  107.                 tMap.selection.x=1
  108.                 tMap.selection.y=1
  109.             end
  110.         elseif evts[1]=="key" and evts[2]==74 and tMap.selection.layer>1 then
  111.             tMap.selection.layer=tMap.selection.layer-1
  112.             if not tMap(tMap.selection.x,tMap.selection.y,tMap.selection.layer) then
  113.                 tMap.selection.x=1
  114.                 tMap.selection.y=1
  115.             end
  116.         elseif evts[1]=="key" and evts[2]==31 then
  117.             return tMap
  118.         end
  119.     end
  120. end
  121.  
  122. function path(map,targx,targy,targz)
  123.  
  124.     --[VARS]
  125.     local tPath={x=1,y=1,z=1,""}
  126.     local target={x=targx or 1,y=targy or 1,z=targz or 1}
  127.     local displacement=setmetatable({},{__index=function(self,index) if tPath[index] and target[index] then return tPath[index]-target[index] end end,__newindex=function() return nil end})
  128.     local checked=setmetatable({{x=1,y=1,z=1}},{__call=function(self,x,y,z) for k,v in pairs(self) do if v.x==x and v.y==y and v.z==z then return true end end return false end})
  129.     local directions_mt={__index=function(self,index) if index=="x" or index=="y" or index=="z" then return 0 end end}
  130.     local directions=setmetatable({{x=1,"r"},{x=-1,"l"},{y=1,"d"},{y=-1,"u"},{z=1,"f"},{z=-1,"b"}},{__call=function(self,direction)
  131.         if direction then
  132.             for k,v in pairs(self) do
  133.                 if v[1]==direction then
  134.                     return v
  135.                 end
  136.             end
  137.             local tDirs={}
  138.             for k,v in pairs(self) do
  139.                 if rawget(v,direction) then
  140.                     table.insert(tDirs,v)
  141.                 end
  142.             end
  143.             return tDirs
  144.         else
  145.             local x
  146.             local y
  147.             local z
  148.             for k,v in pairs(self) do
  149.                 if (displacement.x>0 and v.x<0) or (displacement.x<0 and v.x>0) then
  150.                     x=v
  151.                 elseif (displacement.y>0 and v.y<0) or (displacement.y<0 and v.y>0) then
  152.                     y=v
  153.                 elseif (displacement.z>0 and v.z<0) or (displacement.z<0 and v.z>0) then
  154.                     z=v
  155.                 end
  156.             end
  157.             return x,y,z
  158.         end
  159.     end})
  160.     for k,v in pairs(directions) do setmetatable(v,directions_mt) end
  161.  
  162.     --[FUNCTIONS]
  163.     local render=function()
  164.         term.clear()
  165.         for k,v in pairs(map[tPath.z]) do
  166.             term.setCursorPos(1,k)
  167.             print(({string.gsub(string.gsub(v,"0","."),"1","#")})[1])
  168.         end
  169.         print(tPath.x..", "..tPath.y..", "..tPath.z)
  170.         term.setCursorPos(tPath.x,tPath.y)
  171.         write("*")
  172.     end
  173.  
  174.     local list=function(...)
  175.         local tArgs={...}
  176.         return coroutine.wrap(function()
  177.             for i=1,#tArgs do
  178.                 coroutine.yield(tArgs[i])
  179.             end
  180.         end)
  181.     end
  182.     --[EXECUTION]
  183.  
  184.     while true do
  185.         local x,y,z=directions()
  186.         if x and map(tPath.x+x.x,tPath.y,tPath.z)=="0" and not checked(tPath.x+x.x,tPath.y,tPath.z) then
  187.             tPath.x=tPath.x+x.x
  188.             tPath[1]=tPath[1]..x[1]
  189.             table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
  190.         elseif y and map(tPath.x,tPath.y+y.y,tPath.z)=="0" and not checked(tPath.x,tPath.y+y.y,tPath.z) then
  191.             tPath.y=tPath.y+y.y
  192.             tPath[1]=tPath[1]..y[1]
  193.             table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
  194.         elseif z and map(tPath.x,tPath.y,tPath.z+z.z)=="0" and not checked(tPath.x,tPath.y,tPath.z+z.z) then
  195.             tPath.z=tPath.z+z.z
  196.             tPath[1]=tPath[1]..z[1]
  197.             table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
  198.         else
  199.             local moved=false
  200.             local axis,smallest
  201.             for item in list("x","y","z") do
  202.                 if not smallest or displacement[item]*(displacement[item]<0 and -1 or 1)<smallest or (displacement[item]==smallest and math.random(1,2)==1) then
  203.                         axis=item
  204.                         smallest=displacement[item]*(displacement[item]<0 and -1 or 1)
  205.                 end
  206.             end
  207.             local dirs=directions(axis)
  208.             for k,v in pairs(directions) do table.insert(dirs,v) end
  209.             for k,v in ipairs(dirs) do
  210.                 if v~=x and v~=y and v~=z and map(tPath.x+v.x,tPath.y+v.y,tPath.z+v.z)=="0" and not checked(tPath.x+v.x,tPath.y+v.y,tPath.z+v.z) then
  211.                     moved=true
  212.                     tPath.x=tPath.x+v.x
  213.                     tPath.y=tPath.y+v.y
  214.                     tPath.z=tPath.z+v.z
  215.                     tPath[1]=tPath[1]..v[1]
  216.                     table.insert(checked,{x=tPath.x,y=tPath.y,z=tPath.z})
  217.                     break
  218.                 end
  219.             end
  220.             if not moved then
  221.                 if tPath.x==1 and tPath.y==1 then
  222.                     return false
  223.                 end
  224.                 tPath.x=tPath.x-directions(string.sub(tPath[1],-1)).x
  225.                 tPath.y=tPath.y-directions(string.sub(tPath[1],-1)).y
  226.                 tPath.z=tPath.z-directions(string.sub(tPath[1],-1)).z
  227.                 tPath[1]=string.sub(tPath[1],1,-2)
  228.             end
  229.         end
  230.         if tPath.x==target.x and tPath.y==target.y and tPath.z==target.z then
  231.             return tPath[1]
  232.         end
  233.         --sleep(0.1)
  234.         --render()
  235.     end
  236. end
  237.  
  238. function follow(str,dir)
  239.     local res={}
  240.     local dir=dir or "d"
  241.     local tR={d="l",l="u",u="r",r="d"}
  242.     for i=1,#str do
  243.         local char=string.sub(str,i,i)
  244.         if char=="f" then
  245.             while not turtle.down() do end
  246.         elseif char=="b" then
  247.             while not turtle.up() do end
  248.         elseif char==dir then
  249.             while not turtle.forward() do end
  250.         else
  251.             while dir~=char do
  252.                 if turtle.turnRight() then
  253.                     dir=tR[dir]
  254.                 end
  255.             end
  256.             while not turtle.forward() do end
  257.         end
  258.     end
  259. end
Advertisement
Add Comment
Please, Sign In to add comment