Ubidibity

boxmining.lua

Apr 20th, 2025 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Attempt at a variable width, length, and depth mining program
  2. -- initially basic 2x1 mine down to depth, advance then 2x1 up and repeat length number of times (for width number of times)
  3. -- attempt to handle falling blocks (sand/gravel) as well as I can.  Nothing for fluids at this time.  Need to detect stelarite
  4. -- so it doesn't blow up the turtle
  5. -- Current draft is attempting to add offsets to allow return to chests when full ('heading' is relative, not absolute, so it shouldn't
  6. -- matter what direction the turtle is facing to begin, the chest should be immediately behind the turtle.
  7.  
  8. heading=0 -- right to left 1,2,3 left to right 3,2,1
  9. xpos=0 -- starting x position (relative to turtle, not literal x/y world orientation)
  10. zpos=0 -- starting z position (relative to turtle)
  11. ypos=0 -- ditto
  12.  
  13. flip=0 -- manages left or right u-turn at end of column
  14.  
  15. width=1
  16. -- depth=37 passed by parameter now.  I might add this as a variable, I might not.
  17. length=12 -- 2 or 4 is reasonable as these are 4x longer than specified
  18.  
  19. blocklag=0 -- .5 if you're in a sand heavy area this can help allow sand to fall into place to avoid false forward movement
  20.  
  21. local args = {...}
  22. if #args < 1 then
  23.   print ("Usage: downup # (where depth=#)")
  24.   return
  25. end
  26.  
  27. local depth = tonumber(args[1])
  28.  
  29. if not depth or depth < 1 then
  30.   print("error: absolute positive offsets only.")
  31.   return
  32. end
  33.  
  34. if turtle.getFuelLevel()<(depth+(length*2))*width+(width*4) then  -- close the u-turns should be accounted for with the *4 addition
  35.   print("Refuel or I can't get there and back.")
  36.   return
  37. end
  38.  
  39. local function pauseMe()
  40.   print("Press any key...")
  41.   os.pullEvent("key")
  42. end
  43.  
  44. local function digForward()
  45.   while turtle.detect() do
  46.     local has_block, data = turtle.inspect()
  47.     if has_block then
  48.       if data.name == "forbidden_arcanus:stella_arcanum" or data.name == "allthemodium:allthemodium_slate_ore" or data.name == "lootr:lootr_chest" then
  49.         print("Special exception ore detected! Press any key to continue...")
  50.         os.pullEvent("key")
  51.       end
  52.     end
  53.   turtle.dig()
  54.   end
  55.   sleep(blocklag)
  56. end
  57.  
  58. local function goUp()
  59.    while not turtle.up() do
  60.       local has_block, data = turtle.inspectUp()
  61.       if data.name == "forbidden_arcanus:stella_arcanum" or data.name == "allthemodium:allthemodium_slate_ore" or data.name == "lootr:lootr_chest" then
  62.         print("Special exception ore detected! Press any key to continue...")
  63.         os.pullEvent("key")
  64.       end
  65.  
  66.      turtle.digUp()
  67.    end
  68.   zpos=zpos-1
  69. end
  70.  
  71. local function goDown()
  72.       local has_block, data = turtle.inspectDown()
  73.       if data.name == "forbidden_arcanus:stella_arcanum" or data.name == "allthemodium:allthemodium_slate_ore" or data.name == "lootr:lootr_chest" then
  74.         print("Special exception ore detected! Press any key to continue...")
  75.         os.pullEvent("key")
  76.       end
  77.   turtle.digDown()
  78.   turtle.down()
  79.   zpos=zpos+1
  80. end
  81.  
  82. local function goForward()
  83.   while not turtle.forward() do
  84.     digForward()
  85.   end
  86.   -- xpos and ypos offsets depend on heading, presumably 1,2 + and 3,4 -
  87.   if heading == 0 then
  88.     ypos=ypos+1
  89.   elseif heading == 1 then
  90.     xpos=xpos+1
  91.   elseif heading == 2 then
  92.     ypos=ypos-1
  93.   elseif heading == 3 then
  94.     xpos=xpos-1
  95.     -- Code for case 3
  96.     print("Value is 3")
  97.   end
  98.   print("heading=",heading,"x=",xpos,",y=",ypos,",z=",zpos)
  99.   if xpos<-1 or ypos<-1 then
  100.      print("ERROR, ERROR, DANGER, WILL PARISH, DANGER!")
  101.      pauseMe()
  102.   end
  103. end
  104.  
  105. local function turnRight()
  106.   -- xpos=xpos+1 position only changes with movement, not with turning.
  107.   heading=heading+1
  108.   if heading==4 then heading=0 end
  109.   turtle.turnRight()
  110. end
  111.  
  112. local function turnLeft()
  113.   heading=heading-1
  114.   if heading==-1 then heading=0 end
  115.   turtle.turnLeft()
  116. end
  117.  
  118. -- first stab testing of a Grok routine to dump inventory to a chest
  119. local function returnToStartAndBack(heading, posx, posy, posz)
  120.     -- Step 1: Copy parameters to working variables to avoid modifying originals
  121.     local currentHeading = heading
  122.     local currentX = posx
  123.     local currentY = posy
  124.     local currentZ = posz
  125.  
  126.     -- Verify we're at the top (z = 0)
  127.     if currentZ ~= 0 then
  128.         print("Error: Turtle must be at z=0 to return to start (",currentZ,")")
  129.         return
  130.     end
  131.  
  132.     -- Step 2: Navigate to (0, 0, 0)
  133.  
  134.     -- Helper function to turn to a specific heading
  135. local function turnTo(targetHeading)
  136.   while currentHeading~=targetHeading do
  137.     if currentHeading<targetHeading then
  138.       turnLeft()
  139.       currentHeading=currentHeading+1
  140.     else
  141.       turnRight()
  142.       currentHeading=currentHeading-1
  143.     end
  144.     print("Correcting discrepancy between ",currentHeading," and ",targetHeading)
  145.   end
  146. end
  147.  
  148. -- Move along x-axis to get posx to 0
  149.  
  150. -- two valid states, turtle is on the side of the chest at 0, or it's 'length' away.  In the 0 state we'll turn right,
  151. -- in the 'length' state we'll turn left.  Actually thinking that through heading 0 will always be away from the chest, so 2 will be
  152. --  towards it.  heading 1 is away, and 0 is not toward, but 'near side' so I'll use heading instead.
  153. -- It zig zags back and forth so there are only those two valid states (pointed towards chest at a distance, or to the right)
  154. -- y may not be dug yet, so come back on x first, either way it should be clear back to 0.
  155.  
  156. print("flip==",flip," should be moving from currentX ",currentX," to 0 from heading ",currentHeading,". posx=",posx,",posy=",posy)
  157.  
  158. if flip==1 then
  159.   turnRight()
  160. else
  161.   turnLeft()
  162. end
  163.  
  164. if currentX > 0 then
  165.   for i = 1, currentX do
  166.     turtle.forward()
  167.     currentX = currentX - 1
  168.   end
  169. end
  170.  
  171. if flip==1 then --  ...actually lets see if it's always a left turn? and near side doesn't turn?
  172.   turtle.turnLeft()
  173. else
  174.   turtle.turnLeft()
  175. end
  176. print("ready to move ",posy," y blocks at current heading.")
  177.  
  178. if currentY > 0 then
  179.   for i = 1, currentY do
  180.     print("Moving in -y direction",currentY)
  181.     turtle.forward()
  182.     currentY = currentY-1
  183.   end
  184. end
  185.  
  186. print("Checking for fuel...")
  187. for i = 1, 16 do -- loop through the slots
  188.   turtle.select(i) -- change to the slot
  189.   if turtle.refuel(0) then -- if it's valid fuel
  190.     turtle.refuel(turtle.getItemCount(i)) -- consume IT ALL!
  191.     print("Refueling...")
  192.   end
  193. end
  194.  
  195.  
  196. -- Step 3: Face the chest
  197. -- seems to work okay to here
  198. print("Should be looking at chest...currentHeading ",currentHeading," flip ",flip," posx=",posx," posy=",posy)
  199. print("Dig through issues with new frequency returns so next movement is 0=L, 1=R, H0,F0=U")
  200.  
  201. -- validated that heading=0, flip=0 should u-turn
  202.     -- Step 4: Unload inventory into the chest
  203.     print("Unloading...")
  204.     for slot = 1, 16 do
  205.         turtle.select(slot)
  206.         turtle.drop()
  207.     end
  208.     turtle.select(1) -- Reset to slot 1
  209.  
  210.     print("Return to work.")
  211.  
  212.    if flip==1 then
  213.       turnLeft() -- confirmed if flip==1 then turn left and head back y posx=1,posy=3 in test
  214.       turnLeft() -- so then turn to go back and hook back into nook
  215.    elseif flip==0 and currentHeading==0 then
  216.      print("(u-turn/two R) Should be aligning currentHeading ",currentHeading," with true heading of ",heading)
  217.      turtle.turnRight()
  218.      turtle.turnRight()
  219.    end
  220.  
  221.    if posy > 0 then
  222.      for i = 1, posy do
  223.        turtle.forward()
  224.      end
  225.    end
  226.  
  227. -- it gets more complicated now if I call it at the top of the col., it was post-flip where I'd turn right or left based on end-of-row
  228. -- (0 or length*4 essentially)
  229. -- now most 'top of columns' return and either don't turn at all, or possibly turn right.
  230.   -- flip0, heading0 wants a right turn (at least if x=0/first pass out)
  231.    if flip==0 and currentHeading==1 then
  232.      turtle.turnLeft()
  233.    elseif flip==0 and currentHeading==0 then
  234.      print("resuming in forward direction")
  235.    else -- presumably ...
  236.      turtle.turnRight() -- opposite of before
  237.    end
  238.   print("should be at end of return ",posy, " if 0,0 shouldn't turn.  flip==",flip)
  239.   -- pauseMe()
  240.  
  241.   if posx > 0 then
  242.     for i = 1, posx do
  243.       turtle.forward()
  244.     end
  245.   end
  246.  
  247.   -- point back down mining row
  248.   if flip==1 then  -- ok if flip==0 otherwise u-turn
  249.     turtle.turnRight()
  250.   elseif flip==0 and currentHeading==0 then
  251.     print("Opt out of turns.")
  252.   else
  253.     turtle.turnLeft()
  254.   end
  255.  
  256.   print("flip=",flip,"heading=",currentHeading,", ready to go?")
  257.   --pauseMe()
  258. end
  259.  
  260. -- begin the original/actual box miner
  261. -- length is actually 4 times longer as it's 2 2 column passes
  262. for x=1,width do
  263.   for y=1,length do
  264.     -- depth down
  265.     for z=1,depth do
  266.       digForward()
  267.       goDown()
  268.     end
  269.     goForward()
  270.     goForward()
  271.     -- end of 'down' in position for 'up'
  272.     -- depth back up with while insurance
  273.     for x=1,depth do
  274.       digForward()
  275.       goUp()
  276.     end
  277.     goForward()
  278.     goForward()
  279. -- for deep digs
  280. --    sleep(5)
  281.     -- top of first step to length
  282.   end
  283.  
  284.   if turtle.getItemCount(13)>0 then -- return earlier for deeper depths
  285.     -- legacy item... os.pullEvent("key")
  286.     -- Call the function to return to start, unload, and come back
  287.     print("Inventory full heading back")
  288.     returnToStartAndBack(heading, xpos, ypos, zpos)
  289.     print("back in position, how do I look, Boss?")
  290.    -- pauseMe()
  291.   end
  292.  
  293.   -- shouldn't be needed any longer  sleep(5) -- more moderate depths or widths  
  294.   if flip==0 then
  295.     flip=1
  296.     turnRight()
  297.     goForward()
  298.     turnRight()
  299.   else
  300.     flip=0
  301.     turnLeft()
  302.     goForward()
  303.     turnLeft()
  304.   end
  305.   -- in position for next lane of width
  306.   -- step forward to avoid offset
  307.   goForward()
  308. end
  309.  
  310.  
Add Comment
Please, Sign In to add comment