robocyclone

turtle miner 1

Feb 24th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local rotation = {"north", "east", "south", "west"}
  2.  
  3. if not fs.exists("/.persistance") then
  4.     print("Persistant variable folder not found. \n Creating...")
  5.     fs.makeDir("/.persistance")
  6.     print("Created")
  7. end
  8.  
  9. function store(sName, stuff)
  10.     local filePath = fs.combine("/.persistance", sName)
  11.     if stuff == nil then
  12.         return fs.delete(filePath)
  13.     end
  14.     local handle = fs.open(filePath, "w")
  15.     handle.write(textutils.serialize(stuff))
  16.     handle.close()
  17. end
  18.  
  19. function pull(sName)
  20.     local filePath = fs.combine("/.persistance", sName)
  21.     local handle = fs.open(filePath, "r")
  22.     local stuff = handle.readAll()
  23.     handle.close()
  24.     return textutils.unserialize(stuff)
  25. end
  26.  
  27. if not fs.open(".persistance/turtleCoords", "r") then
  28.     coords = { }
  29.     coords["x"] = 0
  30.     coords["y"] = 0
  31.     coords["z"] = 0
  32.     store("turtleCoords", coords)
  33. else
  34.     coords = pull("turtleCoords")
  35. end
  36. if not fs.open(".persistance/turtleDir", "r") then
  37.     direction = "north"
  38.     store("turtleDir", direction)
  39. else
  40.     direction = pull("turtleDir")
  41. end
  42.  
  43. if coords["x"] == 0 and coords["z"] == 0 and coords["y"] == 0 then
  44.     local originalDir = direction
  45.     local behindDir = turnAroundDir(checkRot(direction))
  46.     faceTo(behindDir)
  47.     local success, idtable = turtle.inspectDown()
  48.     if success and idtable.name == "minecraft:chest" then
  49.         print("Home chest found")
  50.         faceTo(originalDir)
  51.     else
  52.         error("Need chest behind turtle to start work!")
  53.         faceTo(originalDir)
  54.     end
  55. end
  56.  
  57. function checkRot(inDir)
  58.     for i = 1, 4 do
  59.         if rotation[i] == inDir then
  60.             return i
  61.         end
  62.     end
  63.     return false
  64. end
  65.  
  66. function turnAroundDir(numDir)
  67.     if numDir <= 2 then
  68.         return numDir + 2
  69.     else
  70.         return numDir - 2
  71.     end
  72.     return false
  73. end
  74.  
  75. function mineAround()
  76.     if turtle.inspectDown() then
  77.         turtle.digDown()
  78.     end
  79.     if turtle.inspectUp() then
  80.         turtle.digUp()
  81.     end
  82.     local returnLoc = direction
  83.     local numReverseLoc = turnAroundDir(checkRot(direction))
  84.     for f = 1, 4 do
  85.         local f2 = turnAroundDir(f)
  86.         if f2 ~= numReverseLoc then
  87.             faceTo(rotation[f])
  88.             local success, idtable = turtle.inspect()
  89.             if success and idtable.name ~= "minecraft:chest" then
  90.                 turtle.dig()
  91.             end
  92.             faceTo(returnLoc)
  93.         end
  94.     end
  95. end
  96.  
  97. function testForInv()
  98.     local invSpace = 16
  99.     for i = 1, 16 do
  100.         turtle.select(i)
  101.         if turtle.getItemDetail() then
  102.             invSpace = invSpace - 1
  103.         end
  104.     end
  105.     if invSpace <= 4 then
  106.         return false
  107.     else
  108.         return true
  109.     end
  110. end
  111.  
  112. function forward(dirToGo)
  113.     if dirToGo == "north" then
  114.         coords["z"] = coords["z"] + 1
  115.         store("turtleCoords", coords)
  116.         if direction == "north" then
  117.             turtle.forward()
  118.         elseif direction == "south" then
  119.             turtle.turnLeft()
  120.             turtle.turnLeft()
  121.             turtle.forward()
  122.         elseif direction == "west" then
  123.             turtle.turnRight()
  124.             turtle.forward()
  125.         elseif direction == "east" then
  126.             turtle.turnLeft()
  127.             turtle.forward()
  128.         end
  129.         direction = "north"
  130.         store("turtleDir", direction)
  131.     elseif dirToGo == "east" then
  132.         coords["x"] = coords["x"] + 1
  133.         store("turtleCoords", coords)
  134.         if direction == "north" then
  135.             turtle.turnRight()
  136.             turtle.forward()
  137.         elseif direction == "west" then
  138.             turtle.turnLeft()
  139.             turtle.turnLeft()
  140.             turtle.forward()
  141.         elseif direction == "east" then
  142.             turtle.forward()
  143.         elseif direction == "south" then
  144.             turtle.turnLeft()
  145.             turtle.forward()
  146.         end
  147.         direction = "east"
  148.         store("turtleDir", direction)
  149.     elseif dirToGo == "south" then
  150.         coords["z"] = coords["z"] - 1
  151.         store("turtleCoords", coords)
  152.         if direction == "south" then
  153.             turtle.forward()
  154.         elseif direction == "north" then
  155.             turtle.turnLeft()
  156.             turtle.turnLeft()
  157.             turtle.forward()
  158.         elseif direction == "east" then
  159.             turtle.turnRight()
  160.             turtle.forward()
  161.         elseif direction == "west" then
  162.             turtle.turnLeft()
  163.             turtle.forward()
  164.         end
  165.         direction = "south"
  166.         store("turtleDir", direction)
  167.     elseif dirToGo == "west" then
  168.         coords["x"] = coords["x"] - 1
  169.         store("turtleCoords", coords)
  170.         if direction == "west" then
  171.             turtle.forward()
  172.         elseif direction == "east" then
  173.             turtle.turnLeft()
  174.             turtle.turnLeft()
  175.             turtle.forward()
  176.         elseif direction == "south" then
  177.             turtle.turnRight()
  178.             turtle.forward()
  179.         elseif direction == "north" then
  180.             turtle.turnLeft()
  181.             turtle.forward()
  182.         end
  183.         direction = "west"
  184.         store("turtleDir", direction)
  185.     end
  186.     if dirToGo == "up" then
  187.         coords["y"] = coords["y"] + 1
  188.         store("turtleCoords", coords)
  189.         turtle.up()
  190.     elseif dirToGo == "down" then
  191.         coords["y"] = coords["y"] - 1
  192.         store("turtleCoords", coords)
  193.         turtle.down()
  194.     end
  195.     --fuelTestCount()
  196. end
  197.  
  198. function moveTo(x2, y2, z2)
  199.     if x2 > coords["x"] then
  200.         local x3 = x2 - coords["x"]
  201.         for f = 1, x3 do
  202.             forward("east")
  203.         end
  204.     elseif x2 < coords["x"] then
  205.         local x3 = math.abs((math.abs(x2)) - coords["x"])
  206.         for f = 1, x3 do
  207.             forward("west")
  208.         end
  209.     end
  210.     if y2 > coords["y"] then
  211.         local y3 = y2 - coords["y"]
  212.         for g = 1, y3 do
  213.             forward("up")
  214.         end
  215.     elseif y2 < coords["y"] then
  216.         local y3 = math.abs((math.abs(y2)) - coords["y"])
  217.         for g = 1, y3 do
  218.             forward("down")
  219.         end
  220.     end
  221.     if z2 > coords["z"] then
  222.         local z3 = z2 - coords["z"]
  223.         for h = 1, z3 do
  224.             forward("up")
  225.         end
  226.     elseif z2 < coords["z"] then
  227.         local z3 = math.abs(math.abs(z2) - coords["z"])
  228.         for h = 1, z3 do
  229.             forward("down")
  230.         end
  231.     end
  232. end
  233.  
  234. function faceTo(dirToTurn)
  235.     if dirToTurn == "north" then
  236.         if direction == "south" then
  237.             for i = 1, 2 do turtle.turnRight() end
  238.         elseif direction == "east" then
  239.             turtle.turnLeft()
  240.         elseif direction == "west" then
  241.             turtle.turnRight()
  242.         end
  243.         direction = "north"
  244.         store("turtleDir", direction)
  245.     elseif dirToTurn == "south" then
  246.         if direction == "north" then
  247.             for i = 1, 2 do turtle.turnRight() end
  248.         elseif direction == "east" then
  249.             turtle.turnRight()
  250.         elseif direction == "west" then
  251.             turtle.turnLeft()
  252.         end
  253.         direction = "south"
  254.         store("turtleDir", direction)
  255.     elseif dirToTurn == "east" then
  256.         if direction == "west" then
  257.             for i = 1, 2 do turtle.turnRight() end
  258.         elseif direction == "north" then
  259.             turtle.turnRight()
  260.         elseif direction == "south" then
  261.             turtle.turnLeft()
  262.         end
  263.         direction = "east"
  264.         store("turtleDir", direction)
  265.     elseif dirToTurn == "west" then
  266.         if direction == "east" then
  267.             for i = 1, 2 do turtle.turnRight() end
  268.         elseif direction == "north" then
  269.             turtle.turnLeft()
  270.         elseif direction == "south" then
  271.             turtle.turnRight()
  272.         end
  273.         direction = "west"
  274.         store("turtleDir", direction)
  275.     end
  276. end
  277.  
  278. function dropOff()
  279.     moveTo(0,0,0)
  280.     for i = 1, 16 do
  281.         turtle.select(i)
  282.         turtle.dropDown()
  283.     end
  284.     turtle.select(1)
  285. end
  286.  
  287. function mine()
  288.     for x = 0, 15 do
  289.         for z = 1, 15 do
  290.             for y = 0, -4, -1 do
  291.                 if not testForInv() then dropOff() end
  292.                 mineAround()
  293.                 moveto(x,y,z)
  294.             end
  295.         end
  296.     end
  297. end
Advertisement
Add Comment
Please, Sign In to add comment