karelvysinka

Turtle GPS goto 1

Feb 12th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.19 KB | None | 0 0
  1. dirString = { north=0, east=1, south=2, west=3}
  2. -- South= +z, East= +x
  3.  
  4. fuelSlot = 16
  5. x=0
  6. y=0
  7. z=0
  8. f=-1
  9.  
  10. local function saveAll()
  11.     save = fs.open(".persistLoc","w")
  12.     save.write(textutils.serialize({x,y,z,f,fuelSlot}))
  13.     save.close()
  14. end
  15.  
  16. local function loadAll()
  17.     if fs.exists(".persistLoc") then
  18.        load = fs.open(".persistLoc","r")
  19.        x,y,z,f,fuelSlot = unpack(textutils.unserialize(load.readAll()))
  20.        load.close()
  21.     end
  22. end
  23.  
  24. function findFuel()
  25.     for i=1,16 do
  26.         if i ~= fuelSlot then
  27.             turtle.select(i)
  28.             if turtle.refuel(0) then
  29.                 return i
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. function checkFuel()
  36.     if turtle.getFuelLevel() <= math.abs(2 * (100 - y)) then
  37.         turtle.select(fuelSlot)
  38.         if turtle.getItemCount(fuelSlot) == 0 or turtle.refuel(0) == false then
  39.             newFuel = findFuel()
  40.             if newFuel then
  41.                 turtle.select(newFuel)
  42.             else
  43.                 return false
  44.             end
  45.         end
  46.         turtle.refuel(1)
  47.         return true
  48.     end
  49. end
  50.  
  51. function setFuelSlot(slot)
  52.     if slot >= 1 and slot <= 16 then
  53.         fuelSlot = slot
  54.     else
  55.         error("Invalid slot designation: " .. slot, 2)
  56.     end
  57. end
  58.  
  59. function updateLoc(backward)
  60.     backward = backward or false
  61.     if backward then
  62.         factor = -1
  63.     else
  64.         factor = 1
  65.     end
  66.    
  67.     if f==0 then
  68.         z=z-factor
  69.     elseif f==1 then
  70.         x=x+factor
  71.     elseif f==2 then
  72.         z=z+factor
  73.     elseif f==3 then
  74.         x=x-factor
  75.     else
  76.         error("What direction again?")
  77.     end
  78.     saveAll()
  79. end
  80.  
  81. function initFacing()
  82.     fd(1)
  83.     fx,fy,fz = gps.locate(3)
  84.     if fx ~= nil then
  85.         if x ~= fx then
  86.             if fx > x then
  87.                 f = 1
  88.             else
  89.                 f = 3
  90.             end
  91.         elseif z ~= fz then
  92.             if fz > z then
  93.                 f = 2
  94.             else
  95.                 f = 0
  96.             end
  97.         end
  98.     end
  99.     bk(1)
  100. end
  101.  
  102. function initLoc()
  103.     rednet.open("right")
  104.     ix,iy,iz = gps.locate(3)
  105.     if ix ~= nil then
  106.         x,y,z = ix,iy,iz
  107.     else
  108.         print("t: Failed to get coordinates from GPS.")
  109.     end
  110.     if f == -1 then
  111.         initFacing()
  112.     end
  113.     saveAll()
  114.     rednet.close("right")
  115. end
  116.  
  117. function getLoc()
  118.     return x,y,z
  119. end
  120.  
  121. function getX()
  122.     return x
  123. end
  124.  
  125. function getY()
  126.     return y
  127. end
  128.  
  129. function getZ()
  130.     return z
  131. end
  132.  
  133. function rt(ct)
  134.     ct = ct or 1
  135.     for i=1,ct do
  136.         turtle.turnRight()
  137.         f = (f+1)%4
  138.         saveAll()
  139.     end
  140.     return true
  141. end
  142.  
  143. function lt(ct)
  144.     ct = ct or 1
  145.     for i=1,ct do
  146.         turtle.turnLeft()
  147.         f = (f+3)%4
  148.         saveAll()
  149.     end
  150.     return true
  151. end
  152.  
  153. function up(ct,force)
  154.     ct = ct or 1
  155.     force = force or false
  156.     local wait = 0
  157.     for i=1,ct do
  158.         checkFuel()
  159.         while not turtle.up() do
  160.             if wait == 5 then
  161.                 return i - 1
  162.             end
  163.             if force then
  164.                 turtle.digUp()
  165.                 turtle.attackUp()
  166.                 sleep(0.5)
  167.                 turtle.attackUp()
  168.             end
  169.             wait = wait + 1
  170.             sleep(3)
  171.         end
  172.         y = y + 1
  173.         saveAll()
  174.     end
  175.     return ct
  176. end
  177.  
  178. function dn(ct,force)
  179.     ct = ct or 1
  180.     force = force or false
  181.     local wait = 0
  182.     for i=1,ct do
  183.         checkFuel()
  184.         while not turtle.down() do
  185.             if wait == 5 then
  186.                 return i - 1
  187.             end
  188.             if force then
  189.                 turtle.digDown()
  190.                 turtle.attackDown()
  191.                 sleep(0.5)
  192.                 turtle.attackDown()
  193.             end
  194.             wait = wait + 1
  195.             sleep(3)
  196.         end
  197.         y = y - 1
  198.         saveAll()
  199.     end
  200.     return ct
  201. end
  202.  
  203. function fd(ct,force)
  204.     ct = ct or 1
  205.     force = force or false
  206.     local wait = 0
  207.     for i=1,ct do
  208.         checkFuel()
  209.         while not turtle.forward() do
  210.             if wait == 5 then
  211.                 return i - 1
  212.             end
  213.             if force then
  214.                 turtle.dig()
  215.                 turtle.attack()
  216.                 sleep(0.5)
  217.                 turtle.attack()
  218.             end
  219.             wait = wait + 1
  220.             sleep(3)
  221.         end
  222.         updateLoc()
  223.     end
  224.     return ct
  225. end
  226.  
  227. function bk(ct,force)
  228.     ct = ct or 1
  229.     force = force or false
  230.     local wait = 0
  231.     for i=1,ct do
  232.         checkFuel()
  233.         while not turtle.back() do
  234.             if wait == 5 then
  235.                 return i - 1
  236.             end
  237.             if force then
  238.                 rt(2)
  239.                 turtle.dig()
  240.                 turtle.attack()
  241.                 sleep(0.5)
  242.                 turtle.attack()
  243.                 rt(2)
  244.             end
  245.             wait = wait + 1
  246.             sleep(3)
  247.         end
  248.         updateLoc(true)
  249.     end
  250.     return ct
  251. end
  252.  
  253. function face(d)
  254.     if type(d) == "string" then
  255.         dirs = {north=0, east=1, south=2, west=3}
  256.         d = dirs[d]
  257.     elseif type(d) == "number" then
  258.         if d < 0 or d > 4 then
  259.             error("Directional number out of range (0-3).",2)
  260.         end
  261.     end
  262.     if d == nil then error("No direction specified.",2) end
  263. -- F 0=North, 1=East, 2=South, 3=West
  264. -- South= +z, East= +x
  265.     local dir = -1
  266.     if dirString[d] ~= nil then
  267.         dir = dirString[d]
  268.     else
  269.         dir = d
  270.     end
  271.  
  272.     if dir == 0 and f == 3 then
  273.         rt()
  274.     elseif dir == 3 and f == 0 then
  275.         lt()
  276.     elseif dir > f then
  277.         rt(dir - f)
  278.     elseif dir < f then
  279.         lt(f - dir)
  280.     end
  281. end
  282.  
  283. function gotoX(toX,force)
  284.     if toX == nil then error("No coordinate specified.",2) end
  285.     force = force or false
  286.     if x < toX then
  287.         face("east")
  288.     elseif x > toX then
  289.         face("west")
  290.     end
  291.    
  292.     while x ~= toX do
  293.         fd(1,force)
  294.     end
  295. end
  296.  
  297. function gotoY(toY,force)
  298.     if toY == nil then error("No coordinate specified.",2) end
  299.     force = force or false
  300.     if y < toY then
  301.         move = up
  302.         dig = turtle.digUp
  303.     elseif y > toY then
  304.         move = dn
  305.         dig = turtle.digDown
  306.     end
  307.    
  308.     while y ~= toY do
  309.         move(1,force)
  310.     end
  311. end
  312.  
  313. function gotoZ(toZ,force)
  314.     if toZ == nil then error("No coordinate specified.",2) end
  315.     force = force or false
  316.     if z < toZ then
  317.         face("south")
  318.     elseif z > toZ then
  319.         face("north")
  320.     end
  321.    
  322.     while z ~= toZ do
  323.         fd(1,force)
  324.     end
  325. end
  326.  
  327. function goto(toX, toY, toZ)
  328.     toX = toX or x
  329.     toY = toY or y
  330.     toZ = toZ or z
  331.    
  332.     gotoX(toX)
  333.     gotoZ(toZ)
  334.     gotoY(toY)
  335. end
  336.  
  337. loadAll()
  338. initLoc()
  339. print("Loaded t API. ID:" .. os.getComputerID())
Add Comment
Please, Sign In to add comment