Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --load moveAPI's as neeeded
- os.loadAPI("moveAPI")
- --[[TODO
- automatic fuel search
- wireless integration through API
- --]]
- --[[Features
- variable quarry length, depth, width
- automatic inventory emptying
- automatic refueling (as long as fuel is found)
- maintance built into the program, e.g. fuel empty -> return to chest, waits for command
- accepts any chest
- --]]
- --CONSTANTS
- LEFT, RIGHT = 1, 2
- function welcomeScreen()
- local okParameter = false
- local x, y, z, face
- while not okParameter do
- --clear terminal TODO
- print("Welcome to Quarry 2.0")
- print("I will stand in the left-down corner")
- term.write("Length: ")
- x = tonumber(read())
- print()
- term.write("Width: ")
- z = tonumber(read())
- print()
- term.write("Depth(optional): ")
- y = read()
- if y == "" then y = 255 else y = tonumber(y) end --check for optional
- term.write("Direction(optional) N/S/W/E: ")
- face = read()
- if face == "" then face = nil else face = moveAPI.stringToCompass(face) end
- if face == -1 then face = nil end
- --check if parameters are okay
- if x > 0 and z > 0 then
- okParameter = true
- end
- print("-----------------")
- end
- return x, y, z, face
- end
- function printSettings(x, y, z)
- y = y ~= 255 and y or "Bedrock" --Bedrock or Real-Number
- --term.write Settings
- print("Settings: ")
- print("Length: " .. x)
- print("Width : " .. z)
- print("Depth : " .. y)
- end
- function checkForChest()
- print("-----------------")
- print("Ensuring that chest is behind me...")
- moveAPI.turnLeft(2)
- repeat
- os.sleep(0.5)
- _, data = turtle.inspect()
- data = data or ""
- until data.name ~= nil and string.find(data.name, "chest")
- print("Chest found!")
- moveAPI.turnLeft(2)
- print("-----------------")
- end
- function waitForMuhi(why)
- why = why or "NIL"
- print("-----------------")
- print("Error: " .. why)
- print("Decide: continue(c)/refuel(r)")
- term.write("/end(en)/empty(em): ")
- local str = read()
- print("-----------------")
- if str == "c" then return
- elseif str == "r" then checkFuel(true)
- elseif str == "en" then returnToChest() ; emptyInv() ; os.reboot()
- elseif str == "em" then returnToChest() ; emptyInv()
- else print("Wrong command") ; waitForMuhi()
- end
- end
- function checkInv()
- for i=1, 16 do
- turtle.select(i)
- if turtle.getItemDetail() == nil then turtle.select(1) ; return false
- end
- end
- -- arriving here means inv is full
- turtle.select(1)
- return true
- end
- function checkFuel(force)
- force = force or false
- --idea: turtle should always be able to drive home (chest)
- -- if turtle-inv does not contain fuel, return home
- -- check the chest for fuel (how?),
- -- if it does not contain fuel, throw error
- local minLevel =
- math.abs(limit.x) +
- math.abs(limit.y) +
- math.abs(limit.z);
- if turtle.getFuelLevel() < minLevel or force then
- for i=1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel(turtle.getItemCount(i))
- end
- end
- turtle.select(1)
- end
- --check if refueled
- if turtle.getFuelLevel() < minLevel then
- print("-----------------")
- print("Fuel is too low to continue")
- print("Needed: " .. minLevel .. " / Is: " .. turtle.getFuelLevel())
- --return to chest check for coal
- local x, y, z = moveAPI.getXYZ()
- local face = moveAPI.getFace()
- returnToChest()
- emptyInv()
- waitForMuhi("fuel")
- returnToMine(x, y, z, face)
- end
- end
- function readyUpLayer()
- moveAPI.turnRight(2)
- moveAPI.down()
- end
- function readyUpRow(turnSide)
- if turnSide == RIGHT then
- moveAPI.turnRight()
- moveAPI.forward()
- moveAPI.turnRight()
- else
- moveAPI.turnLeft()
- moveAPI.forward()
- moveAPI.turnLeft()
- end
- end
- function digRow()
- --for i=2, limit.x do
- -- moveAPI.forward()
- --end
- moveAPI.forward(limit.x - 1)
- end
- function returnToChest()
- print("Returning to chest")
- while moveAPI.getY() ~= 0 do
- moveAPI.up()
- end
- moveAPI.turnTo(moveAPI.west())
- while moveAPI.getZ() ~= 0 do
- moveAPI.forward()
- end
- moveAPI.turnTo(moveAPI.south())
- while moveAPI.getX() ~= 0 do
- moveAPI.forward()
- end
- end
- function returnToMine(x, y, z, face)
- print("Returning to mine")
- moveAPI.turnTo(moveAPI.north())
- while moveAPI.getX() ~= x do
- moveAPI.forward()
- end
- moveAPI.turnTo(moveAPI.east())
- while moveAPI.getZ() ~= z do
- moveAPI.forward()
- end
- while moveAPI.getY() ~= y do
- moveAPI.down()
- end
- moveAPI.turnTo(face)
- end
- function emptyInv()
- for i=1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- function main()
- --Start of Script
- x, y, z, face = welcomeScreen()
- printSettings(x, y, z)
- moveAPI.initLocation(face)
- checkForChest()
- limit = {x=x, y=y, z=z} --Record
- turnSide = RIGHT --determines first rotation
- --[[
- First go through X
- turn right, forward once
- turn right
- repeat (but change turn side each time)
- --]]
- repeat
- for j=1, limit.z do
- checkFuel()
- digRow()
- if j ~= limit.z then readyUpRow(turnSide) end
- --manditory checks
- if checkInv() then
- local x, y, z = moveAPI.getXYZ() --save current pos
- local face = moveAPI.getFace() --save face
- returnToChest()
- emptyInv()
- returnToMine(x, y, z, face)
- end
- turnSide = (turnSide ~= 2) and (turnSide+1) or 1 --change turn
- end
- readyUpLayer()
- turnSide = (turnSide ~= 2) and (turnSide+1) or 1 --change turn
- until -limit.y == moveAPI.getY()
- --Go back to chest
- returnToChest()
- emptyInv()
- moveAPI.turnLeft(2)
- turtle.select(1)
- print("-----------------")
- print("Done :)")
- end
- limit = {}
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement