Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TODO continue after chunk reload
- function printUsage() --printing usage if mistakes are made
- print( "Usage:" )
- print( "Mine <forward> <right> <down>" )
- end
- resumeArgs = 13 --setting number of Args for resuming
- finishArgs = 9 --and finishing
- tArgs = { ... }
- for x=1, #tArgs do
- print(tArgs[x])
- end
- print("done printing args")
- if not (#tArgs == 3 or #tArgs == finishArgs or #tArgs == resumeArgs) then --making sure program is used correctly
- printUsage()
- return
- elseif tonumber(tArgs[1]) == nil or tonumber(tArgs[2]) == nil or tonumber(tArgs[3]) == nil then
- printUsage()
- return
- end
- print("args check 1 done")
- -- setting general parameters
- down = "down"
- up = "up"
- left = "left"
- right = "right"
- deep = 3
- remainingLine = 0
- remainingSquare = 0
- slot = 2
- turtle.select(slot)
- A = tonumber(tArgs[1])
- B = tonumber(tArgs[2])
- C = tonumber(tArgs[3])
- print("setting params done")
- if #tArgs == 3 or #tArgs == finishArgs then --setting paramaters if its first time usage
- print("args 3 or 9")
- locx = 1 --forward
- locy = 1 --right
- locz = 2 --down
- d = 0 --direction
- end
- if #tArgs == 3 then
- print("args 3")
- restarted = 0
- mined = 0
- attacked = 0
- dropped = 0
- turned = "right"
- left_to_right = 1
- end
- if #tArgs == finishArgs or #tArgs == resumeArgs then
- print("args 9 or 13")
- --restarted, mined, attacked, dropped, turned, left_to_right = tonumber(tArgs[4]), tonumber(tArgs[5]), tonumber(tArgs[6]), tonumber(tArgs[7]), tArgs[8], tArgs[9]
- restarted = tonumber(tArgs[4])
- mined = tonumber(tArgs[5])
- attacked = tonumber(tArgs[6])
- dropped = tonumber(tArgs[7])
- turned = tArgs[8]
- left_to_right = tArgs[9]
- end
- if #tArgs == resumeArgs then
- print("args 13")
- --locx, locy, locz, d = tonumber(tArgs[10]), tonumber(tArgs[11]), tonumber(tArgs[12]), tonumber(tArgs[13])
- locx = tonumber(tArgs[10])
- locy = tonumber(tArgs[11])
- locz = tonumber(tArgs[12])
- d = tonumber(tArgs[13])
- end
- if A < 1 or A > 512 then --making sure valid numbers are used
- print("forward must be between 1 and 512")
- error()
- elseif B < 1 or B > 512 then
- print("right must be between 1 and 512")
- error()
- elseif C < 1 or C > 256 then
- print("down must be between 1 and 256")
- error()
- end
- goForward = A - 1 --doing math on dimensions
- goRight = B - 1
- goDown = math.floor(C / 3) - 1 --square is 3 layers thick, floor because using remaining
- downRemaining = math.fmod(C,3) --for mining out the last 1 or 2 layers
- if goDown < 0 then --preventing negative numbers
- goDown = 0
- downRemaining = 0
- end
- function resume() --resuming mining if turtle got rebooted
- print("resume called")
- restarted = restarted + 1
- if turtle.getItemCount(1) == 0 then --dig up if slot 1 is empty, thus enderchest was placed
- print("getitemcount")
- turtle.select(1)
- dig(up)
- turtle.select(slot)
- else
- dig(up)
- end
- dig(down)
- --by now the turtle finished its last action
- if downRemaining == 0 then
- print("downremaining 0")
- if locz % 3 == 1 then --check if turtle is on the right level, or in the proces of moving down
- move(down)
- if left_to_right == 1 then --changing side to side direction as it failed to do so before unloading
- left_to_right = 0
- save()
- else
- left_to_right = 1
- save()
- end
- elseif locz % 3 == 0 then
- move(down)
- move(down)
- if left_to_right == 1 then --changing side to side direction as it failed to do so before unloading
- left_to_right = 0
- save()
- else
- left_to_right = 1
- save()
- end
- end
- elseif downRemaining == 2 then --check if turtle is busy with the last layers
- print("downremaining 2")
- if locz == C - 2 then --check if turtle is on the right level, or in the proces of moving down
- move(down)
- if left_to_right == 1 then --changing side to side direction as it failed to do so before unloading
- left_to_right = 0
- save()
- else
- left_to_right = 1
- save()
- end
- end
- end
- --by now turtle is on an actual mining level
- if (turned == "right" and left_to_right == 1) or (turned == "left" and left_to_right == 0) then
- print("turning right")
- while d ~= 0 do
- turn(right)
- end
- remainingLine = A - locx
- line(remainingLine)
- else
- print("turning left")
- while d ~= 2 do
- turn(left)
- end
- remainingLine = locx - 1
- line(remainingLine)
- end
- --by now turtle is at the end of a line
- if left_to_right == 1 then
- if locy < B then
- turning()
- end
- remainingSquare = B - locy
- square(remainingSquare)
- elseif left_to_right == 0 then
- if locy > 1 then
- turning()
- end
- remainingSquare = locy - 1
- square(remainingSquare)
- end
- --by now turtle is at the end of a square
- if locz ~= C - 1 then --if turtle is not at the last square, go down and turn to get in position for mining the rest
- if C - locz == 2 or C - locz == 3 then --if turtle is at the second to last layer ánd there is downremaining, move down less
- for x=1, C - locz - 1 do
- move(down)
- end
- else
- for x=1, 3 do
- move(down)
- end
- end
- dig(down)
- turn(right)
- turn(right)
- shell.run("mine", A, B, C, restarted, mined, attacked, dropped, turned, left_to_right)
- error() --laziness hayo
- end
- end
- function fuel() --fuel calculation
- if C < 3 then --calculating fuel usage for moving down
- movingDown = 0
- else
- movingDown = C - 3
- end
- fuelNeeded = (A * B - 1) * math.ceil(C / 3) + movingDown
- fuelLevel = turtle.getFuelLevel()
- fuelDifference = fuelNeeded - fuelLevel
- fuelCoal = math.ceil(fuelDifference / 80)
- if fuelLevel >= fuelNeeded then
- print("*Fuel usage: ".. fuelNeeded .." out of ".. fuelLevel .."")
- print("*Enough fuel to proceed")
- else
- print("*Need ".. fuelDifference .." more fuel (".. fuelCoal .." Coal)")
- while fuelNeeded > fuelLevel do
- C = C - 1
- if C < 3 then
- movingDown = 0
- else
- movingDown = C - 3
- end
- fuelNeeded = (A * B - 1) * math.ceil(C / 3) + movingDown
- end
- print("*Max area with current fuel: ".. A .." ".. B .." ".. C .."")
- print("*Shutting Down...")
- error()
- end
- end
- function full() --checking if inventory is full
- if turtle.getItemCount(16) > 0 then
- dropOff()
- end
- end
- function dropOff() --if inventory full, drop off in (ender)chest
- turtle.select(1)
- while turtle.placeUp() == false do
- if turtle.digUp() == false then
- turtle.attackUp()
- end
- end
- turtle.select(2)
- slot = 2
- for e=1, 15 do
- turtle.select(slot)
- turtle.dropUp()
- slot = slot + 1
- end
- turtle.select(1)
- turtle.digUp()
- slot = 2
- turtle.select(slot)
- dropped = dropped + 1
- end
- function mine() --mine up/down/forward and move forward
- move()
- dig(up)
- dig(down)
- end
- function line(distance) --mine a whole line
- for x=1, distance do
- mine()
- end
- end
- function square(distance) --mine a square
- for x=1, distance do
- line(goForward)
- turning()
- end
- line(goForward)
- end
- function turning() --check if this turn goes to the right or left
- if turned == "right" then
- turn(right)
- move()
- turned = "left"
- save()
- dig(up)
- dig(down)
- turn(right)
- else
- turn(left)
- move()
- turned = "right"
- save()
- dig(up)
- dig(down)
- turn(left)
- end
- end
- function cube() --mine the full cube using digCube()
- dig(down)
- dig(up)
- square(goRight)
- for x=1, goDown do --mine all full squares
- digCube(true)
- end
- if downRemaining ~= 0 then --mine the last 1-2 layers if needed
- deep = downRemaining
- digCube(true)
- end
- end
- function digCube(change) --mine squares and move into position for next one
- for x=1, deep do
- move(down)
- end
- if change == true then
- if left_to_right == 1 then
- left_to_right = 0
- save()
- else
- left_to_right = 1
- save()
- end
- end
- dig(down)
- turn(right)
- turn(right)
- square(goRight)
- end
- function dig(direction) --turtle digging
- full() --check if full
- if direction == "up" then
- if turtle.digUp() then
- mined = mined + 1
- return true
- else
- return false
- end
- elseif direction == "down" then
- if turtle.digDown() then
- mined = mined + 1
- return true
- else
- return false
- end
- else
- if turtle.dig() then
- mined = mined + 1
- return true
- else
- return false
- end
- end
- end
- function turn(direction) --turtle turning
- if direction == "right" then
- d = d + 1
- if d > 3 then d = d - 4 end
- save()
- turtle.turnRight()
- elseif direction == "left" then
- d = d - 1
- if d < 0 then d = d + 4 end
- save()
- turtle.turnLeft()
- end
- end
- function move(direction) --turtle moving
- if direction == "up" then
- while not turtle.up() do
- if not dig(up) then
- if turtle.attackUp() then
- attacked = attacked + 1
- end
- end
- end
- locz = locz - 1
- save()
- elseif direction == "down" then
- while not turtle.down() do
- if not dig(down) then
- if turtle.attackDown() then
- attacked = attacked + 1
- end
- end
- end
- locz = locz + 1
- save()
- else
- while not turtle.forward() do
- if not dig() then
- if turtle.attack() then
- attacked = attacked + 1
- end
- end
- end
- if d == 0 then --saving new location based on the direction its facing
- locx = locx + 1
- save()
- elseif d == 1 then
- locy = locy + 1
- save()
- elseif d == 2 then
- locx = locx - 1
- save()
- elseif d == 3 then
- locy = locy - 1
- save()
- end
- end
- end
- function save() --saving position
- file = fs.open("startup", "w") --open startup in writemode
- file.write('shell.run("mine", '..A..', '..B..', '..C..', '..restarted..', '..mined..', '..attacked..', '..dropped..', \''..turned..'\', '..left_to_right..', '..locx..', '..locy..', '..locz..', '..d..')')
- file.close()
- end
- --running main program
- print("running main program")
- if #tArgs == 3 then --if first time manual startup, interact
- fuel() --checking if there is enough fuel
- print("'Put an enderchest in slot 1'")
- print("'Turtle will mine forward, right, down'")
- print("'Type in anything to start'")
- io.read()
- end
- if #tArgs == resumeArgs then --call resume if resuming...
- resume()
- else
- cube() --start working
- end
- file = fs.open("startup", "w")
- file.write("") --removing startup persistence now that program is done
- file.close()
- dropOff() --final dropoff to empty inventory
- print ("mined "..mined.." blocks")
- if attacked > 0 then
- print ("attacked "..attacked.." times")
- end
- print ("dropped off "..dropped.." times")
- if restarted > 0 then
- print ("restarted "..restarted.." times")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement