ravneravn

Mining 0.1

Aug 3rd, 2021 (edited)
187
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- arg 1 = y start
  2. -- arg 2 =
  3. -- arg 3 = "manual" / boolean for ender chest eller normal chest
  4.  
  5. term.clear()
  6. term.setCursorPos(1,1)
  7.  
  8. local area = 16
  9. local turtleId = os.getComputerLabel()
  10.  
  11. local INVENTORY_SIZE = 16
  12.  
  13. local isRunning
  14. local isModem
  15. local isEnderchest
  16.  
  17.  
  18.  
  19. local args = {...}
  20. local bot = {}
  21. bot.yStart = 0
  22. bot.currY = 0
  23. bot.currX = 1
  24. bot.currZ = 1
  25. bot.direction = 1
  26. bot.currLayer = 0
  27. bot.valuable = 0
  28. bot.resumeY = 0
  29. bot.resumeX = 0
  30. bot.resumeZ = 0
  31. bot.resumeDirection = 1
  32. bot.isRunning = false
  33. bot.isModem = false
  34. bot.isEnderchest = false
  35. bot.tilesToMine = 0
  36. bot.tilesMined = 0
  37.  
  38. local mine_length
  39. local mine_width
  40.  
  41.  
  42. -------------------------------
  43.  
  44. if peripheral.isPresent("right") then
  45.   bot.isModem = true
  46. end
  47.  
  48.  
  49. if #arg == 3 then
  50.     bot.yStart = tonumber(arg[1])
  51.     bot.currY = tonumber(arg[1])
  52.     mine_length = tonumber(arg[2])
  53.     mine_width = tonumber(arg[3])
  54.  
  55.     if mine_width % 2 ~= 0 or mine_length % 2 ~= 0 then
  56.         print("Both the height and width arguments must be an even number")
  57.         return
  58.     elseif mine_width == 1 or mine_length == 1 then
  59.         print("Both the height and width arguments must be greater and 1")
  60.         return
  61.     end
  62. else
  63.     print("Please enter the correct arguments when executing this script. The turtle's current Y height, width and length of mine are required (e.g. mining 64 16 16)")
  64.     return
  65. end
  66.  
  67.  
  68.  
  69.  
  70.  
  71. -------------------------------
  72.  
  73.  
  74. local function textOutput(output_message, x_screen_pos, z_screen_pos, clear_area)
  75.   term.setCursorPos(x_screen_pos,z_screen_pos)
  76.    if clear_area == 0 then
  77.        clear_area = string.len(output_message)
  78.    end
  79.    write(output_message..string.rep(" ", (clear_area - string.len(output_message))))
  80. end
  81.  
  82.  
  83. local function textInput()
  84.  
  85.  
  86. end
  87.  
  88.  
  89.  
  90. local function printPos()
  91.   term.clear()
  92.   term.setCursorPos(1,1)
  93.   print("------------")
  94.   print("x"..bot.currX)
  95.   print("z"..bot.currZ)
  96.   print("y"..bot.currY)
  97.   print("direction "..bot.direction)
  98.   print("layer "..bot.currLayer)
  99.   print("------------")
  100. end
  101.  
  102.  
  103. -- ## WRITE FILE ##
  104. local function savePos()
  105.   handle = fs.open("enderMine/savedFile", "w")
  106.   handle.write(textutils.serialize(bot))
  107.   handle.close()
  108. end
  109.  
  110. local function loadPos()
  111.   handle = fs.open("enderMine/savedFile", "r")
  112.   readFile = handle.readLine()
  113.   if readFile ~= nil then
  114.     bot = textutils.unserialize(readFile)
  115.   end
  116.   handle.close()
  117. end
  118.  
  119.  
  120.  
  121. local function sendBroadcast(message)
  122.   if bot.isModem == true then
  123.     rednet.open("right")
  124.     rednet.broadcast("["..turtleId.."] "..message)
  125.   end
  126. end
  127.  
  128.  
  129.  
  130. function refuel(slot_number)
  131.     print("[TURTLE] Refueling... ")
  132.     turtle.select(slot_number)
  133.     turtle.refuel()
  134.     print("[TURTLE] Nom. Nom. Nom.")
  135. end
  136.  
  137.  
  138. local function checkFuel()
  139.   layers = math.floor(bot.yStart / 4)
  140.   -- mass = area * area * layers
  141.   mass = mine_length * mine_width * layers
  142.   fuel = turtle.getFuelLevel()
  143.   if fuel < mass then
  144.    term.clear()
  145.    term.setCursorPos(1,1)
  146.    print("Not enought fuel")
  147.    print("Fuel needed: "..mass)
  148.    print("Current fuel: "..fuel)
  149.    sleep(1)
  150.    print("")
  151.    print("Place fuel in inventory and press a key to continue")
  152.    event = os.pullEvent("key")
  153.    for i = 1, INVENTORY_SIZE do
  154.     local currentItem = turtle.getItemDetail(i)
  155.  
  156.     if currentItem ~= nil then
  157.          refuel(i)
  158.     end
  159.    end
  160.   end
  161. end
  162.  
  163. local function inventorySort()
  164.     for i = 1, INVENTORY_SIZE do
  165.         local currentItem = turtle.getItemDetail(i)
  166.  
  167.         if currentItem ~= nil then
  168.             turtle.select(i)
  169.  
  170.             for j = i, INVENTORY_SIZE do
  171.                 if turtle.compareTo(j) then
  172.                     turtle.select(j)
  173.                     turtle.transferTo(i)
  174.                     turtle.select(i)
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. local function moveForward()
  182.   while not turtle.forward() do
  183.     turtle.dig()
  184.   end
  185.   savePos()
  186.   printPos()
  187. end
  188.  
  189. local function moveUp()
  190.   while not turtle.up() do
  191.     turtle.digUp()
  192.   end
  193.   bot.currY = bot.currY+1
  194.   savePos()
  195.   printPos()
  196. end
  197.  
  198. local function moveDown()
  199.   while not turtle.down() do
  200.     turtle.digDown()
  201.   end
  202.   bot.currY = bot.currY-1
  203.   savePos()
  204.   printPos()
  205. end
  206.  
  207. local function turnRight()
  208.   turtle.turnRight()
  209.   bot.direction = bot.direction +1
  210.   if bot.direction > 4 then
  211.     bot.direction = 1
  212.   end
  213.   savePos()
  214.   printPos()
  215. end
  216.  
  217. local function turnLeft()
  218.   turtle.turnLeft()
  219.   bot.direction = bot.direction -1
  220.   if bot.direction < 1 then
  221.     bot.direction = 4
  222.   end
  223.   savePos()
  224.   printPos()
  225. end
  226.  
  227.  
  228.  
  229. local function gotoShaft() -- finder punktet hvor den gik ned "skakten"
  230.   while bot.direction ~= 3 do
  231.    turnRight()
  232.   end
  233.  
  234.   while bot.currX > 1 do
  235.    moveForward()
  236.    bot.currX = bot.currX -1
  237.    savePos()
  238.   end
  239.  
  240.   while bot.direction ~= 4 do
  241.    turnRight()
  242.   end
  243.  
  244.   while bot.currZ > 1 do
  245.    moveForward()
  246.    bot.currZ = bot.currZ -1
  247.    savePos()
  248.   end
  249.  
  250.   while bot.direction ~= 1 do
  251.    turnRight()
  252.   end
  253. end
  254.  
  255.  
  256.  
  257. --#### COMPARE UP ####
  258. local function compareUp()
  259.   for i = 1, 3 do
  260.     turtle.select(i)
  261.     turtle.compareUp()
  262.     if turtle.compareUp() == true then
  263.       notinuse = 1
  264.     else
  265.       turtle.digUp()
  266.       bot.valuable = bot.valuable +1
  267.     end
  268.   end
  269. end
  270.  
  271.  
  272. --#### COMPARE DOWN ####
  273. local function compareDown()
  274.   for i = 1, 3 do
  275.     turtle.select(i)
  276.     turtle.compareDown()
  277.     if turtle.compareDown() == true then
  278.       notinuse = 1
  279.     else
  280.       turtle.digDown()
  281.       bot.valuable = bot.valuable +1
  282.     end
  283.   end
  284. end
  285.  
  286.  
  287.  
  288.  
  289.  
  290. local function dropOff() -- vender tilbage til overfladen og smider alt af
  291.  stone = turtle.getItemCount(4)
  292.  turtle.select(4)
  293.  turtle.drop(stone-1)
  294.  gotoShaft()
  295.  bot.resumeY = tonumber(bot.currY)
  296.  toSurface()
  297.  turnRight()
  298.  turnRight()
  299.  for i = 5, 15 do
  300.   turtle.select(i)
  301.   turtle.drop()
  302.  end
  303.  turnRight()
  304.  turnRight()
  305.  while bot.currY > bot.resumeY do
  306.   moveDown()
  307.   printPos()
  308.   savePos()
  309.  end
  310. end
  311.  
  312. local function dropCobble() -- dropper alt cobble stone i slot 4
  313.   local cobble = turtle.getItemCount(4)
  314.   if cobble ~= 0 then
  315.     turtle.select(4)
  316.     turtle.drop(cobble-1)
  317.   end
  318. end
  319.  
  320.  
  321.  
  322.  
  323.  
  324. local function mineRowXPlus() -- miner en række frem
  325.   while bot.currX < mine_length do
  326.     moveForward()
  327.     bot.currX = bot.currX +1
  328.     bot.tilesMined = bot.tilesMined +1
  329.     savePos()
  330.     printPos()
  331.     compareUp()
  332.     compareDown()
  333.   end
  334.   turnRight()
  335.   moveForward()
  336.   bot.currZ = bot.currZ +1
  337.   savePos()
  338.   compareUp()
  339.   compareDown()
  340.   turnRight()
  341. end
  342.  
  343. local function mineRowXMinus() -- mine en række tilbage
  344.   while bot.currX > 1 do
  345.     moveForward()
  346.     bot.currX = bot.currX -1
  347.     bot.tilesMined = bot.tilesMined +1
  348.     savePos()
  349.     printPos()
  350.     compareUp()
  351.     compareDown()
  352.   end
  353.   turnLeft()
  354.   moveForward()
  355.   bot.currZ = bot.currZ +1
  356.   savePos()
  357.   compareUp()
  358.   compareDown()
  359.   turnLeft()
  360.   end
  361.  
  362. local function mineLayer() -- miner et lag og dropper det af
  363.   while bot.currZ < mine_width do
  364.     mineRowXPlus()
  365.     mineRowXMinus()
  366.     dropCobble()
  367.     inventorySort()
  368.   end
  369.   gotoShaft()
  370.   dropOff()
  371. end
  372.  
  373.  
  374. local function newLayer()
  375.   for i = 1, 3 do
  376.     moveUp()
  377.     printPos()
  378.   end
  379.   bot.currLayer = bot.currLayer + 1
  380.   savePos()
  381.   sendBroadcast("Starting layer number: "..bot.currLayer..", Y:"..bot.currY)
  382.   sendBroadcast("current valuables: "..bot.valuable)
  383. end
  384.  
  385.  
  386.  
  387. local function toBedrock()
  388.   local mineStart = bot.yStart - 6
  389.   for i = 1, mineStart do
  390.     moveDown()
  391.   end
  392.   bot.currLayer = 1
  393.   savePos()
  394.   sendBroadcast("Bottom reached")
  395.   printPos()
  396. end
  397.  
  398.  
  399.  
  400. function toSurface()
  401.   gotoShaft()
  402.   sendBroadcast("Going up")
  403.   --for i = 1, bot.yStart - bot.currY do
  404.   while tonumber(bot.currY) < tonumber(bot.yStart) do
  405.           moveUp()
  406.   end
  407.   --if tonumber(bot.currY) > tonumber(bot.yStart) then
  408.   while tonumber(bot.currY) > tonumber(bot.yStart) do
  409.    turtle.down()
  410.    bot.currY = bot.currY -1
  411.    savePos()
  412.    printPos()
  413.   end
  414.   bot.status = "off"
  415.   bot.isRunning = false
  416. end
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428. local function resume()
  429. gotoShaft()
  430. sendBroadcast("Resuming, Y:"..bot.currY)
  431. turtle.select(16)
  432. turtle.dig()
  433. layersToGo = math.floor((bot.yStart - bot.currY)/4)
  434. for i = 1, layersToGo do
  435.  mineLayer()
  436.  newLayer()
  437.  if bot.currY > 50 then
  438.   break
  439.  end
  440. end
  441. toSurface()
  442. end
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451. --------------- LOOP
  452.  
  453. local function startMining()
  454.   if bot.isRunning == true then
  455.     loadPos()
  456.     gotoShaft()
  457.     resume()
  458.     bot.status = "off"
  459.     os.reboot()
  460.   else
  461.     checkFuel()
  462.  
  463.     bot.isRunning = true
  464.  
  465.     if turtle.getItemCount(15) ~= 0 then -- ender chest
  466.       isEnderchest = true
  467.     else -- stationær chest
  468.       isEnderchest = false
  469.     end
  470.  
  471.     savePos()
  472.     bot.status = "on"
  473.     layers = math.floor(bot.yStart / 4)
  474.     bot.currY = bot.yStart
  475.     toBedrock()
  476.     for i = 1, layers do
  477.       mineLayer()
  478.       newLayer()
  479.       if bot.currY > 50 then
  480.        break
  481.       end
  482.     end
  483.     bot.status = "off"
  484.     toSurface()
  485.   end
  486. end
  487.  
  488. startMining()
  489.  
RAW Paste Data