robocyclone

Turtle miner 2

Feb 24th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. function checkRot(inDir)
  2.     for i = 1, 4 do
  3.         if rotation[i] == inDir then
  4.             return i
  5.         end
  6.     end
  7.     return false
  8. end
  9.  
  10. function turnAroundDir(numDir)
  11.     if numDir <= 2 then
  12.         return numDir + 2
  13.     else
  14.         return numDir - 2
  15.     end
  16.     return false
  17. end
  18.  
  19. local rotation = {"north", "east", "south", "west"}
  20.  
  21. if not fs.exists("/.persistance") then
  22.     print("Persistant variable folder not found. \n Creating...")
  23.     fs.makeDir("/.persistance")
  24.     print("Created")
  25. end
  26.  
  27. function store(sName, stuff)
  28.     local filePath = fs.combine("/.persistance", sName)
  29.     if stuff == nil then
  30.         return fs.delete(filePath)
  31.     end
  32.     local handle = fs.open(filePath, "w")
  33.     handle.write(textutils.serialize(stuff))
  34.     handle.close()
  35. end
  36.  
  37. function pull(sName)
  38.     local filePath = fs.combine("/.persistance", sName)
  39.     local handle = fs.open(filePath, "r")
  40.     local stuff = handle.readAll()
  41.     handle.close()
  42.     return textutils.unserialize(stuff)
  43. end
  44.  
  45. if not fs.open(".persistance/turtleCoords", "r") then
  46.     coords = { }
  47.     coords["x"] = 0
  48.     coords["y"] = 0
  49.     coords["z"] = 0
  50.     store("turtleCoords", coords)
  51. else
  52.     coords = pull("turtleCoords")
  53. end
  54. if not fs.open(".persistance/turtleDir", "r") then
  55.     direction = "north"
  56.     store("turtleDir", direction)
  57. else
  58.     direction = pull("turtleDir")
  59. end
  60.  
  61. if coords["x"] == 0 and coords["z"] == 0 and coords["y"] == 0 then
  62.     local originalDir = direction
  63.     local behindDir = turnAroundDir(checkRot(direction))
  64.     faceTo(behindDir)
  65.     local success, idtable = turtle.inspectDown()
  66.     if success and idtable.name == "minecraft:chest" then
  67.         print("Home chest found")
  68.         faceTo(originalDir)
  69.     else
  70.         error("Need chest behind turtle to start work!")
  71.         faceTo(originalDir)
  72.     end
  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