Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Attempt at a variable width, length, and depth mining program
- -- initially basic 2x1 mine down to depth, advance then 2x1 up and repeat length number of times (for width number of times)
- -- attempt to handle falling blocks (sand/gravel) as well as I can. Nothing for fluids at this time. Need to detect stelarite
- -- so it doesn't blow up the turtle
- -- Current draft is attempting to add offsets to allow return to chests when full ('heading' is relative, not absolute, so it shouldn't
- -- matter what direction the turtle is facing to begin, the chest should be immediately behind the turtle.
- heading=0 -- right to left 1,2,3 left to right 3,2,1
- xpos=0 -- starting x position (relative to turtle, not literal x/y world orientation)
- zpos=0 -- starting z position (relative to turtle)
- ypos=0 -- ditto
- flip=0 -- manages left or right u-turn at end of column
- width=1
- -- depth=37 passed by parameter now. I might add this as a variable, I might not.
- length=12 -- 2 or 4 is reasonable as these are 4x longer than specified
- 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
- local args = {...}
- if #args < 1 then
- print ("Usage: downup # (where depth=#)")
- return
- end
- local depth = tonumber(args[1])
- if not depth or depth < 1 then
- print("error: absolute positive offsets only.")
- return
- end
- if turtle.getFuelLevel()<(depth+(length*2))*width+(width*4) then -- close the u-turns should be accounted for with the *4 addition
- print("Refuel or I can't get there and back.")
- return
- end
- local function pauseMe()
- print("Press any key...")
- os.pullEvent("key")
- end
- local function digForward()
- while turtle.detect() do
- local has_block, data = turtle.inspect()
- if has_block then
- if data.name == "forbidden_arcanus:stella_arcanum" or data.name == "allthemodium:allthemodium_slate_ore" or data.name == "lootr:lootr_chest" then
- print("Special exception ore detected! Press any key to continue...")
- os.pullEvent("key")
- end
- end
- turtle.dig()
- end
- sleep(blocklag)
- end
- local function goUp()
- while not turtle.up() do
- local has_block, data = turtle.inspectUp()
- if data.name == "forbidden_arcanus:stella_arcanum" or data.name == "allthemodium:allthemodium_slate_ore" or data.name == "lootr:lootr_chest" then
- print("Special exception ore detected! Press any key to continue...")
- os.pullEvent("key")
- end
- turtle.digUp()
- end
- zpos=zpos-1
- end
- local function goDown()
- local has_block, data = turtle.inspectDown()
- if data.name == "forbidden_arcanus:stella_arcanum" or data.name == "allthemodium:allthemodium_slate_ore" or data.name == "lootr:lootr_chest" then
- print("Special exception ore detected! Press any key to continue...")
- os.pullEvent("key")
- end
- turtle.digDown()
- turtle.down()
- zpos=zpos+1
- end
- local function goForward()
- while not turtle.forward() do
- digForward()
- end
- -- xpos and ypos offsets depend on heading, presumably 1,2 + and 3,4 -
- if heading == 0 then
- ypos=ypos+1
- elseif heading == 1 then
- xpos=xpos+1
- elseif heading == 2 then
- ypos=ypos-1
- elseif heading == 3 then
- xpos=xpos-1
- -- Code for case 3
- print("Value is 3")
- end
- print("heading=",heading,"x=",xpos,",y=",ypos,",z=",zpos)
- if xpos<-1 or ypos<-1 then
- print("ERROR, ERROR, DANGER, WILL PARISH, DANGER!")
- pauseMe()
- end
- end
- local function turnRight()
- -- xpos=xpos+1 position only changes with movement, not with turning.
- heading=heading+1
- if heading==4 then heading=0 end
- turtle.turnRight()
- end
- local function turnLeft()
- heading=heading-1
- if heading==-1 then heading=0 end
- turtle.turnLeft()
- end
- -- first stab testing of a Grok routine to dump inventory to a chest
- local function returnToStartAndBack(heading, posx, posy, posz)
- -- Step 1: Copy parameters to working variables to avoid modifying originals
- local currentHeading = heading
- local currentX = posx
- local currentY = posy
- local currentZ = posz
- -- Verify we're at the top (z = 0)
- if currentZ ~= 0 then
- print("Error: Turtle must be at z=0 to return to start (",currentZ,")")
- return
- end
- -- Step 2: Navigate to (0, 0, 0)
- -- Helper function to turn to a specific heading
- local function turnTo(targetHeading)
- while currentHeading~=targetHeading do
- if currentHeading<targetHeading then
- turnLeft()
- currentHeading=currentHeading+1
- else
- turnRight()
- currentHeading=currentHeading-1
- end
- print("Correcting discrepancy between ",currentHeading," and ",targetHeading)
- end
- end
- -- Move along x-axis to get posx to 0
- -- 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,
- -- 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
- -- towards it. heading 1 is away, and 0 is not toward, but 'near side' so I'll use heading instead.
- -- It zig zags back and forth so there are only those two valid states (pointed towards chest at a distance, or to the right)
- -- y may not be dug yet, so come back on x first, either way it should be clear back to 0.
- print("flip==",flip," should be moving from currentX ",currentX," to 0 from heading ",currentHeading,". posx=",posx,",posy=",posy)
- if flip==1 then
- turnRight()
- else
- turnLeft()
- end
- if currentX > 0 then
- for i = 1, currentX do
- turtle.forward()
- currentX = currentX - 1
- end
- end
- if flip==1 then -- ...actually lets see if it's always a left turn? and near side doesn't turn?
- turtle.turnLeft()
- else
- turtle.turnLeft()
- end
- print("ready to move ",posy," y blocks at current heading.")
- if currentY > 0 then
- for i = 1, currentY do
- print("Moving in -y direction",currentY)
- turtle.forward()
- currentY = currentY-1
- end
- end
- print("Checking for fuel...")
- for i = 1, 16 do -- loop through the slots
- turtle.select(i) -- change to the slot
- if turtle.refuel(0) then -- if it's valid fuel
- turtle.refuel(turtle.getItemCount(i)) -- consume IT ALL!
- print("Refueling...")
- end
- end
- -- Step 3: Face the chest
- -- seems to work okay to here
- print("Should be looking at chest...currentHeading ",currentHeading," flip ",flip," posx=",posx," posy=",posy)
- print("Dig through issues with new frequency returns so next movement is 0=L, 1=R, H0,F0=U")
- -- validated that heading=0, flip=0 should u-turn
- -- Step 4: Unload inventory into the chest
- print("Unloading...")
- for slot = 1, 16 do
- turtle.select(slot)
- turtle.drop()
- end
- turtle.select(1) -- Reset to slot 1
- print("Return to work.")
- if flip==1 then
- turnLeft() -- confirmed if flip==1 then turn left and head back y posx=1,posy=3 in test
- turnLeft() -- so then turn to go back and hook back into nook
- elseif flip==0 and currentHeading==0 then
- print("(u-turn/two R) Should be aligning currentHeading ",currentHeading," with true heading of ",heading)
- turtle.turnRight()
- turtle.turnRight()
- end
- if posy > 0 then
- for i = 1, posy do
- turtle.forward()
- end
- end
- -- 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
- -- (0 or length*4 essentially)
- -- now most 'top of columns' return and either don't turn at all, or possibly turn right.
- -- flip0, heading0 wants a right turn (at least if x=0/first pass out)
- if flip==0 and currentHeading==1 then
- turtle.turnLeft()
- elseif flip==0 and currentHeading==0 then
- print("resuming in forward direction")
- else -- presumably ...
- turtle.turnRight() -- opposite of before
- end
- print("should be at end of return ",posy, " if 0,0 shouldn't turn. flip==",flip)
- -- pauseMe()
- if posx > 0 then
- for i = 1, posx do
- turtle.forward()
- end
- end
- -- point back down mining row
- if flip==1 then -- ok if flip==0 otherwise u-turn
- turtle.turnRight()
- elseif flip==0 and currentHeading==0 then
- print("Opt out of turns.")
- else
- turtle.turnLeft()
- end
- print("flip=",flip,"heading=",currentHeading,", ready to go?")
- --pauseMe()
- end
- -- begin the original/actual box miner
- -- length is actually 4 times longer as it's 2 2 column passes
- for x=1,width do
- for y=1,length do
- -- depth down
- for z=1,depth do
- digForward()
- goDown()
- end
- goForward()
- goForward()
- -- end of 'down' in position for 'up'
- -- depth back up with while insurance
- for x=1,depth do
- digForward()
- goUp()
- end
- goForward()
- goForward()
- -- for deep digs
- -- sleep(5)
- -- top of first step to length
- end
- if turtle.getItemCount(13)>0 then -- return earlier for deeper depths
- -- legacy item... os.pullEvent("key")
- -- Call the function to return to start, unload, and come back
- print("Inventory full heading back")
- returnToStartAndBack(heading, xpos, ypos, zpos)
- print("back in position, how do I look, Boss?")
- -- pauseMe()
- end
- -- shouldn't be needed any longer sleep(5) -- more moderate depths or widths
- if flip==0 then
- flip=1
- turnRight()
- goForward()
- turnRight()
- else
- flip=0
- turnLeft()
- goForward()
- turnLeft()
- end
- -- in position for next lane of width
- -- step forward to avoid offset
- goForward()
- end
Add Comment
Please, Sign In to add comment