p100sch

Local GPS for turtles

Jun 30th, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | None | 0 0
  1. local args = {...}
  2. local path="bin/LGPS/"
  3. local data=path.."data"
  4. local current={x=nil,y=nil,z=nil,f=nil}
  5. local init=false
  6. local save=function()
  7.   local file=fs.open(data,"w")
  8.   file.write(current.x.."\n"..current.y.."\n"..current.z.."\n"..current.f.."\n")
  9.   file.close()
  10.   return true
  11. end
  12. start = function(x,y,z,f)
  13.   x=tonumber(x)
  14.   y=tonumber(y)
  15.   z=tonumber(z)
  16.   f=tonumber(f)
  17.   if x~=nil and y~=nil and z~=nil and f~=nil then
  18.     if f<0 or f>3 then return nil end
  19.     local file=fs.open(data,"w")
  20.     current.x=x
  21.     current.y=y
  22.     current.z=z
  23.     current.f=f
  24.     save()
  25.     init=true
  26.     return current
  27.   end
  28.   if gps.locate() then
  29.     local lx,ly,lz = gps.locate()
  30.     for i=1,4,1 do
  31.       if turtle.detect() then
  32.         turtle.turnLeft()
  33.       else
  34.         turtle.forward()
  35.         local x,y,z = gps.locate()
  36.         if lx==x and lz==z then return nil end
  37.         if lx==x then
  38.           if lz>z then
  39.             current.f=0
  40.           else
  41.             current.f=2
  42.           end
  43.         else
  44.           if lx>x then
  45.             current.f=3
  46.           else
  47.             current.f=1
  48.           end
  49.         end
  50.         local file=fs.open(data,"w")
  51.         current.x=x
  52.         current.y=y
  53.         current.z=z
  54.         save()
  55.         init=true
  56.         return current
  57.       end
  58.     end
  59.   end
  60.   if fs.exists(data) and not fs.isDir(data) then
  61.     local file = fs.open(data,"r")
  62.     for i=1,4,1 do
  63.       local l=tonumber(file.readLine())
  64.       if l==nil then break end
  65.       if i==1 then
  66.         current.x=l
  67.       elseif i==2 then
  68.         current.y=l
  69.       elseif i==3 then
  70.         current.z=l
  71.       elseif i==4 then
  72.         current.f=l
  73.         init=true
  74.         file.close()
  75.         return current
  76.       end
  77.     end
  78.     file.close()
  79.   end
  80.   return nil
  81. end
  82. getPos = function()
  83.   if init then return current
  84.   else return nil end
  85. end
  86. forward = function(i)
  87.   if not init then return nil end
  88.   if i==nil then i=1 end
  89.   i=tonumber(i)
  90.   if i==0 then return true,0
  91.   elseif i==nil then return false,0 end
  92.   for j=1,i,1 do
  93.     if turtle.forward() then
  94.       if current.f==0 then
  95.         current.z=current.z-1
  96.       elseif current.f==1 then
  97.         current.x=current.x+1
  98.       elseif current.f==2 then
  99.         current.z=current.z+1
  100.       else
  101.         current.x=current.x-1
  102.       end
  103.       save()
  104.     else
  105.       return false,j
  106.     end
  107.   end
  108.   return true,i
  109. end
  110. back = function(i)
  111.   if not init then return nil end
  112.   if i==nil then i=1 end
  113.   i=tonumber(i)
  114.   if i==0 then return true,0
  115.   elseif i==nil then return false,0 end
  116.   for j=1,i,1 do
  117.     if turtle.back() then
  118.       if current.f==0 then
  119.         current.z=current.z+1
  120.       elseif current.f==1 then
  121.         current.x=current.x-1
  122.       elseif current.f==2 then
  123.         current.z=current.z-1
  124.       else
  125.         current.x=current.x+1
  126.       end
  127.       save()
  128.     else
  129.       return false,j
  130.     end
  131.   end
  132.   return true,i
  133. end
  134. up = function(i)
  135.   if not init then return nil end
  136.   if i==nil then i=1 end
  137.   i=tonumber(i)
  138.   if i==0 then return true,0
  139.   elseif i==nil then return false,0 end
  140.   for j=1,i,1 do
  141.     if turtle.up() then
  142.       current.y=current.y+1
  143.       save()
  144.     else
  145.       return false,j
  146.     end
  147.   end
  148.   return true,i
  149. end
  150. down = function(i)
  151.   if not init then return nil end
  152.   if i==nil then i=1 end
  153.   i=tonumber(i)
  154.   if i==0 then return true,0
  155.   elseif i==nil then return false,0 end
  156.   for j=1,i,1 do
  157.     if turtle.down() then
  158.       current.y=current.y-1
  159.       save()
  160.     else
  161.       return false,j
  162.     end
  163.   end
  164.   return true,i
  165. end
  166. turnLeft = function(i)
  167.   if not init then return nil end
  168.   if i==nil then i=1 end
  169.   i=tonumber(i)
  170.   if i==0 then return true,0
  171.   elseif i==nil then return false,0 end
  172.   for j=1,i,1 do
  173.     if turtle.turnLeft() then
  174.       if current.f==0 then
  175.         current.f=3
  176.       else
  177.         current.f=current.f-1
  178.       end
  179.       save()
  180.     else
  181.       return false,j
  182.     end
  183.   end
  184.   return true,i
  185. end
  186. turnRight = function(i)
  187.   if not init then return nil end
  188.   if i==nil then i=1 end
  189.   i=tonumber(i)
  190.   if i==0 then return true,0
  191.   elseif i==nil then return false,0 end
  192.   for j=1,i,1 do
  193.     if turtle.turnRight() then
  194.       if current.f==3 then
  195.         current.f=0
  196.       else
  197.         current.f=current.f+1
  198.       end
  199.       save()
  200.     else
  201.       return false,j
  202.     end
  203.   end
  204.   return true,i
  205. end
  206.  
  207. local movements={["forward"]=forward,["back"]=back,["up"]=up,["down"]=down,["turnLeft"]=turnLeft,["turnRight"]=turnRight}
  208. if args[1]=="set" then
  209.   if tonumber(args[2])~=nil and tonumber(args[3])~=nil and tonumber(args[4])~=nil and tonumber(args[5])~=nil then
  210.     if tonumber(args[5])<0 or tonumber(args[5])>3 then
  211.       print("Invalid facing value!")
  212.       return 1
  213.     else
  214.       start(args[2],args[3],args[4],args[5])
  215.       return 0
  216.     end
  217.   else
  218.     print("Insuficient number of arguments or wrong types")
  219.     return 1
  220.   end
  221. elseif args[1]=="get" then
  222.   if start() then
  223.     local facing={"North","East","South","West"}
  224.     print("X: "..current.x.."\nY: "..current.y.."\nZ: "..current.z.."\nF: "..current.f.." "..facing[current.f+1])
  225.     return 0
  226.   else
  227.     print("No data available!")
  228.     return 1
  229.   end
  230. elseif movements[args[1]] then
  231.   if start() then
  232.     local i=1
  233.     if args[2]~=nil then
  234.       if tonumber(args[2]) then
  235.         i=args[2]
  236.       else
  237.         print("Invalid arguments!")
  238.         return 1
  239.       end
  240.     end
  241.     if movements[args[1]](i) then
  242.       return 0
  243.     else
  244.       print("Movement blocked!")
  245.       return 1
  246.     end
  247.   else
  248.     print("No data available!")
  249.     return 1
  250.   end
  251. end
Advertisement
Add Comment
Please, Sign In to add comment