robocyclone

turtle miner 4

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