Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = table.pack(...)
- local totalMoves = 100000
- -- print the usage, along with a small information thing if the inputs are wrong
- local function printUsage(i)
- print("Usage:")
- print("dig <right:number> <down:number> <forward:number>")
- if i then
- local char = i == 1 and 'right' or i == 2 and 'down' or i == 3 and 'forward'
- print(string.format("Input %s is not a number", char))
- end
- end
- -- If there wasn't three inputs (x y z), print the usage
- if tArgs.n ~= 3 then
- printUsage()
- return
- end
- -- Grab the inputs, convert to numbers
- local x, y, z = table.unpack(tArgs, 1, n)
- x, y, z = tonumber(x), tonumber(y), tonumber(z)
- -- if one is not a number, print error
- if not x or not y or not z then
- printUsage(not x and 1 or not y and 2 or not z and 3)
- return
- end
- local function ensure(func, ...)
- local args = table.pack(...)
- while not func() do
- for i = 1, args.n do
- args[i]()
- end
- end
- end
- local mx, my = term.getSize()
- local line = string.rep(' ', mx)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1, 1)
- io.write(line)
- term.setCursorPos(1, 1)
- print("Information")
- local infoWindow = window.create(term.current(), 1, 2, mx, 6)
- term.setCursorPos(1, 8)
- io.write(line)
- term.setCursorPos(1, 8)
- io.write("Positioning")
- local posWindow = window.create(term.current(), 1, 9, mx, 1)
- term.setCursorPos(1, 10)
- io.write(line)
- term.setCursorPos(1, 10)
- io.write("Fuel")
- local fuelWindow = window.create(term.current(), 1, 11, mx, 1)
- term.setCursorPos(1, 12)
- io.write(line)
- term.setCursorPos(1, 12)
- io.write("Completion")
- local compWindow = window.create(term.current(), 1, 13, mx, 1)
- local origTerm = term.redirect(infoWindow)
- local imx, imy = term.getSize()
- local count = 1
- local function print(...)
- term.redirect(infoWindow)
- local args = table.pack(...)
- local out = table.concat(args, " ")
- local _, y = term.getCursorPos()
- if y == imy then
- term.scroll(1)
- term.setCursorPos(1, y)
- else
- term.setCursorPos(1, y + 1)
- end
- write(string.format("[%d] %s", count, out))
- count = count + 1
- end
- term.setBackgroundColor(colors.black)
- -- mini pathfinding "algorithm". Doesn't save location, just uses it for returning
- -- to base.
- local homePos = {0,0,0}
- local pos = {0, 0, 0, f = 0}
- local function posu()
- term.redirect(posWindow)
- write(string.format("\nx: %d | y: %d | z: %d | facing: %s", pos[1], pos[2], pos[3],
- pos.f == 0 and "forward" or pos.f == 1 and "right"
- or pos.f == 2 and "back" or pos.f == 3 and "left"))
- end
- local function fuelu()
- term.redirect(fuelWindow)
- write(string.format("\n%d / %d", turtle.getFuelLevel(), turtle.getFuelLimit()))
- end
- fuelu()
- local moves = 0
- local function compu(x)
- term.redirect(compWindow)
- moves = moves + (x or 1)
- local perc = moves / totalMoves
- perc = math.floor(perc * mx)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 1)
- local space = string.rep(' ', mx)
- write(space)
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(1, 1)
- space = string.rep(' ', perc)
- write(space)
- local toWrite = string.format("%d / %d", moves, totalMoves)
- if toWrite:len() > perc then
- term.setBackgroundColor(colors.black)
- term.setCursorPos(mx - toWrite:len() + 1, 1)
- else
- term.setCursorPos(1, 1)
- end
- write(toWrite)
- term.setBackgroundColor(colors.black)
- end
- -- "overrides" for turtle commands
- local function forward()
- local ok, err = turtle.forward()
- if ok then
- if pos.f == 0 then
- -- forward on z axis (forward)
- pos[3] = pos[3] + 1
- elseif pos.f == 1 then
- -- forward on x axis (right)
- pos[1] = pos[1] + 1
- elseif pos.f == 2 then
- -- back on z axis (back)
- pos[3] = pos[3] - 1
- elseif pos.f == 3 then
- -- back on x axis (left)
- pos[1] = pos[1] - 1
- end
- end
- posu()
- fuelu()
- return ok, err
- end
- local function up()
- local ok, err = turtle.up()
- if ok then
- -- up on y axis
- pos[2] = pos[2] + 1
- end
- posu()
- fuelu()
- return ok, err
- end
- local function down()
- local ok, err = turtle.down()
- if ok then
- -- down on y axis
- pos[2] = pos[2] - 1
- end
- posu()
- fuelu()
- return ok, err
- end
- local function left()
- local ok, err = turtle.turnLeft()
- if ok then
- pos.f = pos.f - 1
- if pos.f < 0 then pos.f = 3 end
- end
- posu()
- return ok, err
- end
- local function right()
- local ok, err = turtle.turnRight()
- if ok then
- pos.f = (pos.f + 1) % 4
- end
- posu()
- return ok, err
- end
- -- face a certain direction.
- local function face(x)
- local lf = ((x + 1) % 4 == pos.f)
- local rf = (x - 1 == pos.f) or (x - 1 + 4 == pos.f)
- if lf then
- ensure(left)
- elseif rf then
- ensure(right)
- else
- ensure(left)
- ensure(left)
- end
- end
- -- calculate the fuel required using the same "movements" as the actual run func
- local function calcFuel()
- local fuelNeeded = 0
- totalMoves = 0
- for i = 1, y, 2 do
- for j = 1, x do
- for k = 1, j ~= 1 and z - 1 or i == 1 and z or z - 1 do
- if i ~= y then
- totalMoves = totalMoves + 1
- end
- totalMoves = totalMoves + 2
- if i ~= y and k == z - 1 then
- totalMoves = totalMoves + 1
- end
- fuelNeeded = fuelNeeded + 1
- end
- if j ~= x then
- if i ~= y then
- totalMoves = totalMoves + 1
- end
- fuelNeeded = fuelNeeded + 1
- totalMoves = totalMoves + 4
- end
- end
- if i < y - 1 then
- fuelNeeded = fuelNeeded + 2
- totalMoves = totalMoves + 6
- end
- end
- -- estimation of the fuel needed for returning to the base when inventory is full
- fuelNeeded = fuelNeeded + ((x + y + z) * 2)
- return fuelNeeded
- end
- local fuelReq = calcFuel()
- local fuelCalc = fuelReq
- local current = turtle.getFuelLevel()
- print(string.format("fuel: %d/%d required", current, fuelReq))
- if current < fuelReq then
- print()
- error(string.format("Not enough fuel. Need %d more.", fuelReq - current), -1)
- else
- print(string.format("We're good to go. We'll have %d fuel left after this.",
- current - fuelReq))
- end
- local turnDir = right
- local facing = true
- local function dropItems(flerp)
- local cpos = {pos[1], pos[2], pos[3], f = pos.f}
- print(string.format("Returning home from x:%d, y:%d, z:%d.", pos[1], pos[2], pos[3]))
- -- get home
- -- y axis
- while pos[2] < 0 do
- ensure(up, turtle.digUp, turtle.attackUp)
- end
- -- z axis
- face(3)
- while pos[1] > 0 do
- ensure(forward, turtle.dig, turtle.attack)
- end
- -- x axis
- face(2)
- while pos[3] > 0 do
- ensure(forward, turtle.dig, turtle.attack)
- end
- -- drop items
- print("Dropping items.")
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- turtle.select(1)
- if not flerp then
- -- return to last location
- -- x axis
- print(string.format("Returning to x:%d, y:%d, z:%d.", cpos[1], cpos[2], cpos[3]))
- face(0)
- while pos[3] < cpos[3] do
- ensure(forward, turtle.dig, turtle.attack)
- end
- -- z axis
- face(1)
- while pos[1] < cpos[1] do
- ensure(forward, turtle.dig, turtle.attack)
- end
- -- y axis
- while pos[2] > cpos[2] do
- ensure(down, turtle.digDown, turtle.attackDown)
- end
- -- restore original facing
- face(cpos.f)
- end
- end
- local function checkItems()
- local sum = 0
- for i = 1, 16 do
- sum = sum + (turtle.getItemCount(i) > 0 and 1 or 0)
- end
- return sum
- end
- local function swap(t)
- turnDir = turnDir == left and right or left
- facing = not facing
- end
- local function run()
- for i = 1, y, 2 do
- for j = 1, x do
- for k = 1, j ~= 1 and z - 1 or i == 1 and z or z - 1 do
- -- only mine the second block if we aren't on the block we need to stop on
- if i ~= y then
- turtle.digDown()
- compu(1)
- end
- -- mine and move
- ensure(forward, turtle.dig, turtle.attack)
- compu(2)
- if i ~= y and k == z - 1 then
- turtle.digDown()
- compu(1)
- end
- if k % 5 == 0 then
- if checkItems() > 10 then
- dropItems(false)
- end
- end
- end
- if j ~= x then
- -- turn around and start the next line
- if i ~= y then
- turtle.digDown()
- compu(1)
- end
- ensure(turnDir)
- ensure(forward, turtle.dig, turtle.attack)
- ensure(turnDir)
- compu(4)
- end
- -- swap the way we turn
- swap()
- end
- -- move up two spots
- if i < y - 1 then
- ensure(down, turtle.digDown, turtle.attackUp)
- ensure(down, turtle.digDown, turtle.attackUp)
- ensure(turnDir)
- ensure(turnDir)
- compu(6)
- swap()
- end
- end
- dropItems(true)
- print("Digging complete.")
- end
- local ok, err = pcall(run)
- if not ok then
- term.redirect(origTerm)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- printError(err)
- return
- end
- term.redirect(origTerm)
- os.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement