BenDipp

mining_turtle

Jul 2nd, 2024 (edited)
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1. --TODO: better shape, put in chest, get fuel from chest
  2.  
  3. os.run({},"settingsSetter.lua")
  4.  
  5. local token = settings.get("discord.token")
  6. local ownerId = settings.get("discord.owner")
  7. local channel = settings.get("discord.channel")
  8. local exiting = false;
  9. local itemBlacklist = {"minecraft:cobbled_deepslate","minecraft:cobblestone","minecraft:tuff","minecraft:netherrack","minecraft:granite","minecraft:gravel","minecraft:andesite","minecraft:diorite","forbidden_arcanus:darkstone","twigs:pebble","minecraft:flint","twigs:rhyolite","create:orchrum"}
  10. local dropBlacklisted = true;
  11.  
  12. if os.getComputerLabel() == nil then
  13.     os.setComputerLabel("unnamed Mining Turtle")
  14. end
  15.  
  16. function notifyOwner(message)
  17.     message = "<@"..ownerId.."> "..os.getComputerLabel()..": "..message
  18.  
  19.     local headers={
  20.         ["Content-Type"]="application/json",
  21.         ["User-Agent"]="ComputerCraft Turtle",
  22.         ["Authorization"]="Bot "..token
  23.     }
  24.  
  25.     local response,error,errorResponse = http.post("https://discord.com/api/channels/"..channel.."/messages","{\"content\":\""..message.."\"}",headers)
  26.  
  27.     if(response == nil) then
  28.         write(error.."\n")
  29.         errorResponse.readAll();
  30.         errorResponse.close();
  31.     end
  32. end
  33.  
  34. function refuel()
  35.     turtle.select(1)
  36.     local level = turtle.getFuelLevel()
  37.     local ok, error = turtle.refuel()
  38.     if turtle.getFuelLevel() < 150 then
  39.         notifyOwner("Not enough fuel")
  40.         exiting = true;
  41.     end
  42. end
  43.  
  44. function refuelInternally()
  45.     turtle.select(2) -- see assumption
  46.     --turtle.dig()
  47.     local placed = turtle.place()
  48.  
  49.     local ateNonCombustable = false
  50.     local isFull = false
  51.     while not isFull and not ateNonCombustable do
  52.         turtle.suck()
  53.         ateNonCombustable = not turtle.refuel() -- returns false if not combustable
  54.         turtle.drop()
  55.         if(turtle.getFuelLimit() - turtle.getFuelLevel() < 1000) then -- fuellevl gained from one lava bucket
  56.             isFull = true
  57.         end
  58.     end
  59.    
  60.     --turtle.dig()
  61.     automataDigCollected()
  62.    
  63.     if ateNonCombustable then
  64.         notifyOwner("Ate Non Combustable")
  65.         exiting = true;
  66.     end
  67. end
  68.  
  69. function mineForward()
  70.     turtle.dig()
  71.     moveForward()
  72.     turtle.digUp()
  73.     turtle.digDown()
  74. end
  75.  
  76. function moveForward()
  77.     local moved = turtle.forward()
  78.     while not moved do
  79.         turtle.dig()
  80.         moved = turtle.forward()
  81.     end
  82. end
  83.  
  84. function mineChunk(x,y)
  85.     local swapback = false;
  86.     turtle.digUp()
  87.     turtle.digDown()
  88.     for j=1, x-1 do
  89.         for i=1, y-1 do
  90.             mineForward()
  91.         end
  92.  
  93.         if swapback then
  94.             turtle.turnRight()
  95.             mineForward()
  96.             turtle.turnRight()
  97.         else
  98.             turtle.turnLeft()
  99.             mineForward()
  100.             turtle.turnLeft()
  101.         end
  102.         swapback = not swapback
  103.     end
  104.  
  105.     for i=1, y-1 do
  106.         mineForward()
  107.     end
  108.     turtle.turnRight()
  109.     turtle.turnRight()
  110.     --placeTorch()
  111. end
  112.  
  113. function mineChunkTwoDeep(x,y)
  114.     mineChunk(x,y)
  115.     mineChunk(x,y)
  116. end
  117.  
  118. function placeTorch()
  119.     turtle.select(2)
  120.     local success = true;
  121.     if turtle.getItemDetail().name == "minecraft:torch" then
  122.         success = turtle.placeDown()
  123.         turtle.select(1)
  124.     else
  125.         sucess = false;
  126.     end
  127.  
  128.     if (not success) then
  129.         notifyOwner("Could not place torch")
  130.         exiting = true;
  131.     end
  132. end
  133.  
  134. function mineShaftSection()
  135.     mineForward()
  136.     turtle.turnLeft()
  137.     for i=1, 7 do
  138.         mineForward()
  139.     end
  140.     turtle.turnLeft()
  141.     turtle.turnLeft()
  142.  
  143.     for i=1, 7 do
  144.         moveForward()
  145.     end
  146.  
  147.     for i=1, 8 do
  148.         mineForward()
  149.     end
  150.  
  151.     turtle.turnLeft()
  152.     turtle.turnLeft()
  153.     for i=1, 8 do
  154.         moveForward()
  155.     end
  156.     turtle.turnRight()
  157.     mineForward()
  158.     mineForward()
  159. end
  160.  
  161. function mineMineshaft()
  162.     for i=1, 5 do
  163.         mineShaftSection()
  164.     end
  165. end
  166.  
  167. function walkUntilStorage()
  168.     while not peripheral.isPresent("front") and not exiting do
  169.         local result = turtle.forward()
  170.         if result == false then
  171.             notifyOwner("Ran out of fuel while walking to storage (or path is blocked)")
  172.             exiting = true
  173.         end
  174.     end
  175. end
  176.  
  177. function walkUpUntilStorage()
  178.     while not peripheral.isPresent("top") and not exiting do
  179.         local result = turtle.up()
  180.         if result == false then
  181.             notifyOwner("Ran out of fuel while walking to storage (or path is blocked)")
  182.             exiting = true
  183.         end
  184.     end
  185. end
  186.  
  187. function depositInventory()
  188.     for i=2, 16 do
  189.         turtle.select(i)
  190.         if not exiting and turtle.getItemDetail() ~= nil then
  191.             --if turtle.getItemDetail().name == "minecraft:cobbled_deepslate" then
  192.             --   turtle.dropDown()
  193.             --else
  194.                 local success = turtle.drop()
  195.                 if not success then
  196.                     notifyOwner("Storage is full")
  197.                     exiting = true
  198.                 end
  199.             --end
  200.         end
  201.     end
  202.     turtle.select(2)
  203. end
  204. function array_has_value(array, value)
  205.     for index, arr_val in ipairs(array) do
  206.         if arr_val == value then
  207.             return true
  208.         end
  209.     end
  210.     return false
  211. end
  212.  
  213. function depositInventoryUp()
  214.     for i=2, 16 do
  215.         turtle.select(i)
  216.         if not exiting and turtle.getItemDetail() ~= nil then
  217.             --if turtle.getItemDetail().name == "minecraft:cobbled_deepslate" then
  218.             --   turtle.dropDown()
  219.             --else
  220.            -- if depositInventoryUp and array_has_value(itemBlacklist, turtle.getItemDetail().name) then
  221.            --     turtle.dropDown()
  222.            -- else
  223.             local success = turtle.dropUp()
  224.             if not success then
  225.                 notifyOwner("Storage is full")
  226.                 exiting = true
  227.             end
  228.         end
  229.     end
  230. end
  231.  
  232. function walkUntilFrontOfMine()
  233.     while turtle.inspect() == false and not exiting do
  234.         local result = turtle.forward()
  235.         if result == false then
  236.             notifyOwner("Ran out of fuel while walking to front of mine")
  237.             exiting = true
  238.         end
  239.     end
  240. end
  241.  
  242. function walkDownUntilFrontOfMine()
  243.     while turtle.inspectDown() == false and not exiting do
  244.         local result = turtle.down()
  245.         if result == false then
  246.             notifyOwner("Ran out of fuel while walking to front of mine")
  247.             exiting = true
  248.         end
  249.     end
  250. end
  251.  
  252. function depositItems()
  253.     local sum = 0;
  254.     for i=1, 16 do
  255.         turtle.select(i)
  256.         if turtle.getItemDetail() ~= nil then
  257.             sum=sum+1;
  258.         end
  259.     end
  260.     if sum > 13 then
  261.         turtle.turnLeft()
  262.         turtle.turnLeft()
  263.         walkUntilStorage()
  264.         if not exiting then
  265.             depositInventory()  
  266.         end
  267.         turtle.turnLeft()
  268.         turtle.turnLeft()
  269.         if not exiting then
  270.             walkUntilFrontOfMine()
  271.         end
  272.     end
  273. end
  274.  
  275. function depositItemsVertically()
  276.     local sum = 0;
  277.     for i=1, 16 do
  278.         turtle.select(i)
  279.         if turtle.getItemDetail() ~= nil then
  280.             sum=sum+1;
  281.         end
  282.     end
  283.     if sum > 13 then
  284.         walkUpUntilStorage()
  285.         if not exiting then
  286.             depositInventoryUp()  
  287.         end
  288.         if not exiting then
  289.             walkDownpUntilFrontOfMine()
  290.         end
  291.     end
  292. end
  293.  
  294. function depositItemsInternally()
  295.     local sum = 0;
  296.     for i=1, 16 do
  297.         turtle.select(i)
  298.         if turtle.getItemDetail() ~= nil then
  299.             sum=sum+1;
  300.         end
  301.     end
  302.     turtle.select(1)
  303.     if sum > 13 then
  304.         turtle.select(2)
  305.         turtle.place()
  306.         depositInventory()
  307.         turtle.select(2)  
  308.         automataDigCollected()
  309.         turtle.select(1)
  310.     end
  311. end
  312.  
  313. function mineSnaked(length)
  314.     mineForward()
  315.     mineForward()
  316.     mineForward()
  317.  
  318.     turtle.turnLeft()
  319.     for i=1, length do
  320.         mineForward()
  321.     end
  322.     turtle.turnRight()
  323.  
  324.     mineForward()
  325.     mineForward()
  326.     mineForward()
  327.  
  328.     turtle.turnRight()
  329.  
  330.     for i=1, length do
  331.         mineForward()
  332.     end
  333.     turtle.turnLeft()
  334. end
  335.  
  336. local automata
  337. function automataDig()
  338.     while automata.getOperationCooldown("dig") > 0 do
  339.         sleep(0.1)
  340.     end
  341.     automata.digBlock()
  342. end
  343. function automataCollect()
  344.     while automata.getOperationCooldown("suck") > 0 do
  345.         sleep(0.1)
  346.     end
  347.     local result = automata.collectItems()
  348.     while result ~= nil do
  349.         while automata.getOperationCooldown("suck") > 0 do
  350.             sleep(0.1)
  351.         end
  352.         result = automata.collectItems()
  353.     end
  354. end
  355. function automataDigCollected()
  356.     automataDig()
  357.     automataCollect()
  358. end
  359. function automataDigForward()
  360.     automataDigCollected()
  361.     turtle.forward()
  362. end
  363. function automataStripMine(length)
  364.     for i=1,length do
  365.         automataDigForward()
  366.     end
  367. end
  368. local count = 0;
  369. --walkUntilFrontOfMine()
  370.  
  371. automata = peripheral.wrap("right")
  372. while not exiting do
  373.     --mineShaftSection()
  374.     --placeTorch()
  375.     --mineChunk(3,3)
  376.     automataStripMine(5)
  377.     depositItemsInternally()
  378.     if turtle.getFuelLevel() < 1000 then
  379.         refuelInternally()
  380.     end
  381.     count = count + 1;
  382.     if count > 1000 then
  383.         exiting = false
  384.         notifyOwner("Stopped due to limit reach")
  385.     end
  386. end
Advertisement
Add Comment
Please, Sign In to add comment