Advertisement
djst3rios

Mining script 1

Oct 24th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | Gaming | 0 0
  1. os.loadAPI("inv.lua")
  2. os.loadAPI("t.lua")
  3.  
  4. local x = 0
  5. local y = 0
  6. local z = 0
  7. local max = 16
  8. local deep = 64
  9. local facingfw = true
  10.  
  11. local OK = 0
  12. local ERROR = 1
  13. local LAYERCOMPLETE = 2
  14. local OUTOFFUEL = 3
  15. local FULLINV = 4
  16. local BLOCKEDMOV = 5
  17. local USRINTERRUPT = 6
  18.  
  19. local USEMODEM = false
  20.  
  21.  
  22. -- Arguments
  23. local tArgs = {...}
  24. for i=1,#tArgs do
  25.     local arg = tArgs[i]
  26.     if string.find(arg, "-") == 1 then
  27.         for c=2,string.len(arg) do
  28.             local ch = string.sub(arg,c,c)
  29.             if ch == 'm' then
  30.                 USEMODEM = true
  31.             else
  32.                 write("Invalid flag '")
  33.                 write(ch)
  34.                 print("'")
  35.             end
  36.         end
  37.     end
  38. end
  39.  
  40.  
  41. function out(s)
  42.  
  43.     s2 = s .. " @ [" .. x .. ", " .. y .. ", " .. z .. "]"
  44.            
  45.     print(s2)
  46.     if USEMODEM then
  47.         rednet.broadcast(s2, "miningTurtle")
  48.     end  
  49. end
  50.  
  51. function dropInChest()
  52.     turtle.turnLeft()
  53.    
  54.     local success, data = turtle.inspect()
  55.    
  56.     if success then
  57.         if data.name == "minecraft:chest" then
  58.        
  59.             out("Dropping items in chest")
  60.            
  61.             for i=1, 16 do
  62.                 turtle.select(i)
  63.                
  64.                 data = turtle.getItemDetail()
  65.                
  66.                 if data ~= nil and
  67.                         (data.name == "minecraft:coal" or
  68.                          data.name == "minecraft:charcoal" or
  69.                          data.name == "modern_industrialization:lignite_coal") == false then
  70.                     turtle.drop()
  71.                 end
  72.             end
  73.         end
  74.     end
  75.    
  76.     turtle.turnRight()
  77.    
  78. end
  79.  
  80. function goDown()
  81.     while true do
  82.         if turtle.getFuelLevel() <= fuelNeededToGoBack() then
  83.             if not refuel() then
  84.                 return OUTOFFUEL
  85.             end
  86.         end
  87.    
  88.         if not turtle.down() then
  89.             turtle.up()
  90.             z = z+1
  91.             return
  92.         end
  93.         z = z-1
  94.     end
  95. end
  96.  
  97. function fuelNeededToGoBack()
  98.     return -z + x + y + 2
  99. end
  100.  
  101. function refuel()
  102.     for i=1, 16 do
  103.         -- Only run on Charcoal
  104.         turtle.select(i)
  105.        
  106.         item = turtle.getItemDetail()
  107.         if item and
  108.                 (item.name == "minecraft:coal" or
  109.                  item.name == "minecraft:charcoal" or
  110.                  item.name == "modern_industrialization:lignite_coal") and
  111.                 turtle.refuel(1) then
  112.             return true
  113.         end
  114.     end
  115.    
  116.     return false
  117. end
  118.  
  119. function moveH()
  120.     if inv.isInventoryFull() then
  121.         out("Dropping thrash")
  122.         inv.dropThrash()
  123.        
  124.         if inv.isInventoryFull() then
  125.             out ("Stacking items")
  126.             inv.stackItems()
  127.         end
  128.        
  129.         if inv.isInventoryFull() then
  130.             out("Full inventory!")
  131.             return FULLINV  
  132.         end
  133.     end
  134.    
  135.     if turtle.getFuelLevel() <= fuelNeededToGoBack() then
  136.         if not refuel() then
  137.             out("Out of fuel!")
  138.             return OUTOFFUEL
  139.         end
  140.     end
  141.    
  142.     if facingfw and y<max-1 then
  143.     -- Going one way
  144.         local dugFw = t.dig()
  145.         if dugFw == false then
  146.             out("Hit bedrock, can't keep going")
  147.             return BLOCKEDMOV
  148.         end
  149.         t.digUp()
  150.         t.digDown()
  151.    
  152.         if t.fw() == false then
  153.             return BLOCKEDMOV
  154.         end
  155.        
  156.         y = y+1
  157.    
  158.     elseif not facingfw and y>0 then
  159.     -- Going the other way
  160.         t.dig()
  161.         t.digUp()
  162.         t.digDown()
  163.        
  164.         if t.fw() == false then
  165.             return BLOCKEDMOV
  166.         end
  167.        
  168.         y = y-1
  169.        
  170.     else
  171.         if x+1 >= max then
  172.             t.digUp()
  173.             t.digDown()
  174.             return LAYERCOMPLETE -- Done with this Y level
  175.         end
  176.        
  177.         -- If not done, turn around
  178.         if facingfw then
  179.             turtle.turnRight()
  180.         else
  181.             turtle.turnLeft()
  182.         end
  183.        
  184.         t.dig()
  185.         t.digUp()
  186.         t.digDown()
  187.        
  188.         if t.fw() == false then
  189.             return BLOCKEDMOV
  190.         end
  191.        
  192.         x = x+1
  193.        
  194.         if facingfw then
  195.             turtle.turnRight()
  196.         else
  197.             turtle.turnLeft()
  198.         end
  199.        
  200.         facingfw = not facingfw
  201.     end
  202.    
  203.     return OK
  204. end
  205.  
  206. function digLayer()
  207.    
  208.     local errorcode = OK
  209.  
  210.     while errorcode == OK do
  211.         if USEMODEM then
  212.             local msg = rednet.receive(1)
  213.             if msg ~= nil and string.find(msg, "return") ~= nil then
  214.                 return USRINTERRUPT
  215.             end
  216.         end
  217.         errorcode = moveH()
  218.     end
  219.    
  220.     if errorcode == LAYERCOMPLETE then
  221.         return OK
  222.     end
  223.    
  224.     return errorcode  
  225. end
  226.  
  227. function goToOrigin()
  228.    
  229.     if facingfw then
  230.        
  231.         turtle.turnLeft()
  232.        
  233.         t.fw(x)
  234.        
  235.         turtle.turnLeft()
  236.        
  237.         t.fw(y)
  238.        
  239.         turtle.turnRight()
  240.         turtle.turnRight()
  241.        
  242.     else
  243.        
  244.         turtle.turnRight()
  245.        
  246.         t.fw(x)
  247.        
  248.         turtle.turnLeft()
  249.        
  250.         t.fw(y)
  251.        
  252.         turtle.turnRight()
  253.         turtle.turnRight()
  254.        
  255.     end
  256.    
  257.     x = 0
  258.     y = 0
  259.     facingfw = true
  260.    
  261. end
  262.  
  263. function goUp()
  264.  
  265.     while z < 0 do
  266.        
  267.         t.up()
  268.        
  269.         z = z+1
  270.        
  271.     end
  272.    
  273.     goToOrigin()
  274.    
  275. end
  276.  
  277. function mainloop()
  278.  
  279.     while true do
  280.  
  281.         local errorcode = digLayer()
  282.    
  283.         if errorcode ~= OK then
  284.             goUp()
  285.             return errorcode
  286.         end
  287.        
  288.         goToOrigin()
  289.        
  290.         for i=1, 3 do
  291.             t.digDown()
  292.             success = t.down()
  293.        
  294.             if not success then
  295.                 goUp()
  296.                 return BLOCKEDMOV
  297.             end
  298.  
  299.             z = z-1
  300.             out("Z: " .. z)
  301.  
  302.         end
  303.     end
  304. end
  305.  
  306. if USEMODEM then
  307.     rednet.open("right")
  308. end
  309.  
  310. out("\n\n\n-- WELCOME TO THE MINING TURTLE --\n\n")
  311.  
  312. while true do
  313.  
  314.     goDown()
  315.  
  316.     local errorcode = mainloop()
  317.     dropInChest()
  318.    
  319.     if errorcode ~= FULLINV then
  320.         break
  321.     end
  322. end
  323.  
  324. if USEMODEM then
  325.     rednet.close("right")
  326. end
  327.  
Tags: Quarry
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement