robocyclone

Turtle miner 6

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