robocyclone

turtle miner 5

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