Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Length
- local Width
- local Depth
- local X
- local Y
- local Z
- local direction
- local goingBack
- digheights = {}
- local dig_height
- local counter
- function digQuery()
- -- check if startposition or if digheight is greater than 3, 2 or 1
- for i=counter,#digheights do
- dig_height = tonumber(digheights[i])
- -- dig rows
- digRow(dig_height)
- if(i ~= #digheights) then
- -- reset robot to lowest point
- if(dig_height >= 2) then
- digDown(2)
- else
- digDown(1)
- end
- -- check if done with one plot
- if(Y == tonumber(Width)-1 and goingBack == "false" and Z ~= Depth) then
- turnLeft()
- turnLeft()
- goingBack = "true"
- local new_dig_height = digheights[i + 1]
- if(new_dig_height == 2) then
- digDown(1)
- digUp(1)
- elseif(new_dig_height == 3) then
- digDown(2)
- digUp(1)
- end
- elseif(Y == 0 and goingBack == "true" and Z ~= Depth) then
- turnRight()
- turnRight()
- goingBack = "false"
- local new_dig_height = digheights[i + 1]
- if(new_dig_height == 2) then
- digDown(1)
- digUp(1)
- elseif(new_dig_height == 3) then
- digDown(2)
- digUp(1)
- end
- end
- end
- local h = fs.open("query/counter", "w")
- h.writeLine(i)
- h.close()
- end
- doneMining()
- home()
- end
- -- [TURTLE FUNCTIONS]
- function turnLeft()
- turtle.turnLeft()
- if direction == "N" then
- direction = "W"
- elseif direction == "W" then
- direction = "S"
- elseif direction == "S" then
- direction = "E"
- elseif direction == "E" then
- direction = "N"
- end
- local h = fs.open("query/homePositions", "w")
- h.writeLine(X)
- h.writeLine(Y)
- h.writeLine(Z)
- h.writeLine(direction)
- h.writeLine(goingBack)
- h.close()
- end
- function turnRight()
- turtle.turnRight()
- if direction == "N" then
- direction = "E"
- elseif direction == "E" then
- direction = "S"
- elseif direction == "S" then
- direction = "W"
- elseif direction == "W" then
- direction = "N"
- end
- local h = fs.open("query/homePositions", "w")
- h.writeLine(X)
- h.writeLine(Y)
- h.writeLine(Z)
- h.writeLine(direction)
- h.writeLine(goingBack)
- h.close()
- end
- function digForward(height)
- if(height == 1) then
- while turtle.forward() == false do
- turtle.dig()
- end
- savePos()
- elseif(height == 2) then
- while turtle.forward() == false do
- turtle.dig()
- end
- savePos()
- turtle.digDown()
- elseif(height == 3) then
- while turtle.forward() == false do
- turtle.dig()
- end
- savePos()
- turtle.digUp()
- turtle.digDown()
- else
- print("Error: height given for function [digForward] is not 1, 2 or 3")
- end
- if(height == 3 or height == 2 or height == 1) then
- emptyInv()
- end
- end
- function digDown(amount)
- local _down = 0
- repeat
- while turtle.down() == false do
- turtle.digDown()
- end
- Z = Z + 1
- local h = fs.open("query/homePositions", "w")
- h.writeLine(X)
- h.writeLine(Y)
- h.writeLine(Z)
- h.writeLine(direction)
- h.writeLine(goingBack)
- h.close()
- _down = _down + 1
- until _down == amount
- end
- function digUp(amount)
- local _up = 0
- repeat
- while turtle.up() == false do
- turtle.digUp()
- end
- Z = Z - 1
- local h = fs.open("query/homePositions", "w")
- h.writeLine(X)
- h.writeLine(Y)
- h.writeLine(Z)
- h.writeLine(direction)
- h.writeLine(goingBack)
- h.close()
- _up = _up + 1
- until _up == amount
- end
- function goForward(amount)
- local _forward = 0
- repeat
- while turtle.forward() == false do
- turtle.dig()
- end
- _forward = _forward + 1
- until _forward == amount
- end
- -- [OTHER FUNCTIONS]
- function savePos()
- if direction == "N" then
- X = X + 1
- elseif direction == "S" then
- X = X - 1
- elseif direction == "E" then
- Y = Y - 1
- elseif direction == "W" then
- Y = Y + 1
- end
- local h = fs.open("query/homePositions", "w")
- h.writeLine(X)
- h.writeLine(Y)
- h.writeLine(Z)
- h.writeLine(direction)
- h.writeLine(goingBack)
- h.close()
- printInfo()
- end
- function isStartPosition()
- if(X == 0.0 and Y == 0.0 and Z == 0.0 and direction == "N") then
- print("start position = true, meaning we are at the begin of the query")
- return true
- else
- print("start position = false, meaning we are in the middle of the query")
- return false
- end
- end
- function digRow(my_dig_height)
- my_dig_height = tonumber(my_dig_height)
- if(goingBack == "false") then
- while(tonumber(Y) ~= (tonumber(Width) - 1)) do
- if(direction == "N") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) >= (tonumber(Length) - 1)
- turnLeft()
- digForward(my_dig_height)
- turnLeft()
- elseif(direction == "S") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) <= 0
- turnRight()
- digForward(my_dig_height)
- turnRight()
- elseif(tonumber(X) >= (tonumber(Length) - 1) and direction == "W") then
- turnLeft()
- elseif(tonumber(X) <= 0 and direction == "E") then
- turnRight()
- end
- end
- -- once more
- if(direction == "N") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) >= (tonumber(Length) - 1)
- elseif(direction == "S") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) <= 0
- end
- elseif(goingBack == "true") then
- while(tonumber(Y) ~= 0) do
- if(direction == "N") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) == (tonumber(Length) - 1)
- turnRight()
- digForward(my_dig_height)
- turnRight()
- elseif(direction == "S") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) == 0
- turnLeft()
- digForward(my_dig_height)
- turnLeft()
- elseif(tonumber(X) >= (tonumber(Length) - 1) and direction == "W") then
- turnRight()
- elseif(tonumber(X) <= 0 and direction == "E") then
- turnLeft()
- end
- end
- -- once more
- if(direction == "N") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) == (tonumber(Length) - 1)
- elseif(direction == "S") then
- repeat
- digForward(my_dig_height)
- until tonumber(X) == 0
- end
- end
- end
- function emptyInv()
- if turtle.getItemCount(16) > 0 then
- turnLeft()
- turnLeft()
- turtle.dig()
- turtle.select(1)
- turtle.place()
- local success, t = turtle.inspect()
- if success then
- print("found something")
- if string.find(t.name, "EnderStorage") == nil then
- print("error!")
- while true do
- sleep(5)
- end
- else
- for q=2,16 do
- turtle.select(q)
- turtle.drop()
- end
- turtle.select(1)
- turtle.dig()
- turnRight()
- turnRight()
- end
- else
- print("error!")
- while true do
- sleep(5)
- end
- end
- end
- end
- function home()
- if(Z > 0) then
- digUp(Z)
- end
- if(X > 0) then
- while(direction ~= "S") do
- turnRight()
- end
- goForward(X)
- end
- if(Y > 0) then
- while(direction ~= "E") do
- turnLeft()
- end
- goForward(Y)
- end
- if(X == 0 and Y == 0 and Z == 0) then
- while(direction ~= "N") do
- turnLeft()
- end
- turtle.select(1)
- turtle.place()
- local success, t = turtle.inspect()
- if success then
- print("found something")
- if string.find(t.name, "EnderStorage") == nil then
- print("error!")
- while true do
- sleep(5)
- end
- else
- for q=2,16 do
- turtle.select(q)
- turtle.drop()
- end
- turtle.select(1)
- turtle.dig()
- turnRight()
- turnRight()
- end
- else
- print("error!")
- while true do
- sleep(5)
- end
- end
- fs.delete("query")
- fs.delete("startup")
- clearScreen()
- print("Done and deleted /query & startup files")
- end
- end
- function doneMining()
- local h = fs.open("query/done", "w")
- h.writeLine(true)
- h.close()
- end
- function printInfo()
- clearScreen()
- print("--------------------------------")
- print("X: " .. X)
- print("Y: " .. Y)
- print("Z: " .. Z)
- print("--------------------------------")
- print("Direction: " .. direction)
- print("Current digheight: " .. dig_height)
- print("--------------------------------")
- if goingBack == true then
- print("Going back to home chest")
- end
- end
- function clearScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- -- [STARTUP AND INIT]
- function init_function()
- if fs.exists("query/done") then
- home()
- elseif fs.exists("query/firstInput") then
- --crashed
- print("reading first input, probably a crash occured")
- -- getting users input
- local h = fs.open("query/firstInput", "r")
- Length = tonumber(h.readLine())
- Width = tonumber(h.readLine())
- Depth = tonumber(h.readLine())
- h.close()
- -- getting his positions
- print("reading my positions and direction")
- local h = fs.open("query/homePositions", "r")
- X = tonumber(h.readLine())
- Y = tonumber(h.readLine())
- Z = tonumber(h.readLine())
- direction = h.readLine()
- goingBack = h.readLine()
- h.close()
- -- getting depth array
- local h = fs.open("query/depths", "r")
- line = h.readLine()
- i = 1
- while line do
- digheights[i] = tonumber(line) -- will make it true if the read line is true, else false.
- i = i + 1
- line = h.readLine()
- end
- h.close()
- -- getting current counter status (height)
- local h = fs.open("query/counter", "r")
- counter = tonumber(h.readLine())
- h.close()
- -- Check if crashed while empty inv.
- local data = turtle.getItemDetail(1)
- if(data == nil) then
- local second_data = turtle.inspect()
- if(second_data == nil) then
- repeat
- local data = turtle.getItemDetail(1)
- sleep(3)
- until data ~= nil and string.find(data.name, "EnderStorage")
- elseif(string.find(second_data.name, "EnderStorage")) then
- for q=2,16 do
- turtle.select(q)
- turtle.drop()
- end
- turtle.select(1)
- turtle.dig()
- turnRight()
- turnRight()
- end
- end
- -- Display given numers
- print(" " )
- print("Given numbers")
- print("Length: " .. Length .. " , Width: " .. Width .. " , Depth: " .. Depth)
- print("My numbers")
- print("X: " .. X .. " , Y: " .. Y .. " , Z: " .. Z .. " , direction: " .. direction)
- print(" " )
- print("Counter: " .. counter .. " / Times down: " .. #digheights)
- print("Starting..")
- if(digheights[counter] > 1) then
- turtle.digUp()
- turtle.digDown()
- end
- digQuery()
- else
- startup()
- end
- end
- function startup()
- print("V1 of the query bot with ender chest. Remember to put an ender chest in slot 1")
- term.write("Insert Length: ")
- Length = tonumber(read())
- print(" ")
- term.write("Insert Width: ")
- Width = tonumber(read())
- print(" ")
- term.write("Insert Depth: ")
- Depth = tonumber(read())
- print(" ")
- term.clear()
- term.setCursorPos(1,1)
- print("Is this the right input: ")
- print("Length: " .. Length)
- print("Width: " .. Width)
- print("Depth: " .. Depth)
- print("Press enter if it's correct, otherwhise: Ctrl+R for reboot")
- local rightInput = false
- while (rightInput == false) do
- local event, param = os.pullEvent()
- if event == "key" and param == 28 then -- key 28 is the 'enter' key
- rightInput = true
- end
- sleep(0.2)
- end
- local data = turtle.getItemDetail(1)
- if(data == nil) then
- print("Put an enderchest in slot 1")
- repeat
- local data = turtle.getItemDetail(1)
- sleep(0.2)
- until data ~= nil and string.find(data.name, "EnderStorage")
- end
- term.clear()
- term.setCursorPos(1,1)
- --Query folder, easy to delete
- print("making folder")
- fs.makeDir("query")
- print("saving first input")
- local h = fs.open("query/firstInput", "w")
- h.writeLine(Length)
- h.writeLine(Width)
- h.writeLine(Depth)
- h.close()
- print("making home positions")
- local h = fs.open("query/homePositions", "w")
- h.writeLine(0)
- h.writeLine(0)
- h.writeLine(0)
- h.writeLine("N")
- h.writeLine(false)
- h.close()
- X = 0
- Y = 0
- Z = 0
- direction = "N"
- goingBack = "false"
- print("Making startup file")
- if fs.exists("startup") then
- fs.delete("startup")
- local h = fs.open("startup", "w")
- h.writeLine("sleep(5)")
- h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
- h.close()
- else
- h.writeLine("sleep(5)")
- local h = fs.open("startup", "w")
- h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
- h.close()
- end
- -- calculate depths
- local depth_counter = 0
- while(depth_counter < Depth) do
- depth_counter = depth_counter + 3
- digheights[#digheights+1]=3
- end
- if(depth_counter > Depth) then
- digheights[#digheights]=3-(depth_counter-Depth)
- end
- local h = fs.open("query/depths", "w")
- for i = 1, #digheights do
- h.writeLine(digheights[i]) -- write to file
- end
- h.close()
- local h = fs.open("query/counter", "w")
- h.writeLine(1)
- h.close()
- counter = 1
- if(digheights[1] == 2) then
- digDown(1)
- digUp(1)
- elseif(digheights[1] == 3) then
- digDown(2)
- digUp(1)
- end
- digQuery()
- end
- -- start script
- init_function()
Advertisement
Add Comment
Please, Sign In to add comment