Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Length
- local Width
- local Depth
- local home_x
- local home_y
- local home_z
- local start_direction
- local home_direction_y
- local home_direction_x
- local dig_direction
- local total_forward
- local x, y, z = gps.locate(5)
- while not x do --If gps.locate didn't work, it won't return anything. So check to see if it did.
- error('No GPS!')
- sleep(5)
- local x, y, z = gps.locate(5)
- end
- if x then
- x = math.floor(x)
- y = math.floor(y)
- z = math.floor(z)
- end
- function checkCompas()
- print("function: checkCompas")
- local x, y, z = gps.locate(5)
- if turtle.back() then
- local new_x, new_y, new_z = gps.locate(5)
- new_x = math.floor(new_x)
- new_y = math.floor(new_y)
- if math.floor(new_y) ~= math.floor(y) then
- print("Y| " .. new_y .. " - " .. y)
- if math.floor(y) > math.floor(new_y) then
- dig_direction = "S"
- else
- dig_direction = "N"
- end
- elseif math.floor(new_x) ~= math.floor(x) then
- print("X| " .. math.floor(new_x) .. " - " .. math.floor(x))
- if math.floor(x) < math.floor(new_x) then
- dig_direction = "W"
- else
- dig_direction = "E"
- end
- end
- turtle.forward()
- else
- turtle.dig()
- turtle.dig()
- turtle.forward()
- local new_x, new_y, new_z = gps.locate(5)
- new_x = math.floor(new_x)
- new_y = math.floor(new_y)
- if math.floor(new_y) ~= math.floor(y) then
- print("Y| " .. new_y .. " - " .. y)
- if math.floor(y) < math.floor(new_y) then
- dig_direction = "S"
- else
- dig_direction = "N"
- end
- elseif math.floor(new_x) ~= math.floor(x) then
- print("X| " .. new_x .. " - " .. x)
- if math.floor(x) > math.floor(new_x) then
- dig_direction = "W"
- else
- dig_direction = "E"
- end
- end
- turtle.back()
- end
- print("direction = ")
- print(dig_direction)
- end
- function changeDir()
- -- print("Function: ChangeDir")
- if dig_direction == nil then
- checkCompas()
- end
- turtle.turnLeft()
- if dig_direction == "S" then
- dig_direction = "E"
- -- print("Facing east")
- elseif dig_direction == "E" then
- dig_direction = "N"
- -- print("Facing north")
- elseif dig_direction == "N" then
- dig_direction = "W"
- -- print("Facing west")
- elseif dig_direction == "W" then
- dig_direction = "S"
- -- print("Facing south")
- end
- end
- function changeDirRight()
- turtle.turnRight()
- if dig_direction == "S" then
- dig_direction = "W"
- -- print("Facing west")
- elseif dig_direction == "W" then
- dig_direction = "N"
- -- print("Facing north")
- elseif dig_direction == "N" then
- dig_direction = "E"
- -- print("Facing east")
- elseif dig_direction == "E" then
- dig_direction = "S"
- -- print("Facing south")
- end
- end
- function searchChest()
- print("Function: searchChest")
- local success, t = turtle.inspect()
- if success then
- print("found something")
- if string.find(t.name, "chest") == nil then
- changeDir()
- searchChest()
- end
- else
- changeDir()
- searchChest()
- end
- end
- function deleteOrNot()
- term.write("Delete all? y/n ")
- Delete = read()
- if Delete == "y" then
- print("Deleting query folder and startup file")
- fs.delete("startup")
- if fs.exists("startup") then
- print("startup was not deleted")
- end
- fs.delete("query")
- if fs.exists("query") then
- print("query folder not deleted")
- end
- elseif Delete == "n" then
- print("Ok.")
- else
- print("not a valid input")
- deleteOrNot()
- end
- end
- function goUp(amount)
- print("Function: goUp")
- if amount == 0 then
- print("nothing to go up")
- else
- if amount > 0 then
- local _up = 0
- repeat
- if turtle.up() then
- _up = _up + 1
- else
- turtle.digUp()
- end
- until _up == amount
- elseif amount < 0 then
- local _up = 0
- repeat
- if turtle.down() then
- _up = _up - 1
- else
- turtle.digDown()
- end
- until _up == amount
- end
- end
- end
- function crashOccured()
- print("Function: crashOccured")
- if fs.exists("query/crash_report") then
- print("deleting old crash report")
- fs.delete("query/crash_report")
- if fs.exists("query/crash_report") then
- print("crash_report not deleted")
- end
- end
- checkCompas()
- print(start_direction)
- if start_direction == "E" or start_direction == "W" then
- if dig_direction == "N" or dig_direction == "S" then
- print("Wrong direction")
- print(start_direction .. " - " .. dig_direction)
- changeDir()
- if turtle.detect() == false then
- changeDirRight()
- changeDirRight()
- end
- end
- elseif start_direction == "N" or start_direction == "S" then
- if dig_direction == "W" or dig_direction == "E" then
- print("Wrong direction")
- print(start_direction .. " - " .. dig_direction)
- changeDir()
- if turtle.detect() == false then
- changeDirRight()
- changeDirRight()
- end
- end
- end
- print("making a crash report file")
- local h = fs.open("query/crash_report", "w")
- h.writeLine(x)
- h.writeLine(y)
- h.writeLine(z)
- h.writeLine(dig_direction)
- h.close()
- sleep(1)
- if turtle.getItemCount(16) > 0 then
- goHome()
- searchChest()
- for q=1,16 do
- turtle.select(q)
- turtle.drop()
- end
- turtle.select(1)
- turtle.turnLeft()
- turtle.turnLeft()
- checkCompas()
- comeHome()
- end
- --mine further
- digQuery()
- end
- function goHome()
- print("function: goHome")
- local x, y, z = gps.locate(5)
- -- print(math.floor(x))
- if math.floor(home_x) == math.floor(x) and math.floor(home_y) == math.floor(y) and math.floor(home_z) == math.floor(z) then
- print("we already home")
- else
- checkCompas()
- local x_amount = math.floor(home_x) - math.floor(x)
- local y_amount = math.floor(home_y) - math.floor(y)
- local z_amount = math.floor(home_z) - math.floor(z)
- if y_amount < 0 then
- home_direction_y = "N"
- else
- home_direction_y = "S"
- end
- if x_amount < 0 then
- home_direction_x = "W"
- else
- home_direction_x = "E"
- end
- if z_amount ~= 0 then
- goUp(z_amount)
- end
- if dig_direction ~= home_direction_x then
- repeat
- changeDir()
- until dig_direction == home_direction_x
- end
- if x_amount ~= 0 then
- x_amount = math.floor(x_amount)
- forward(x_amount)
- end
- if dig_direction ~= home_direction_y then
- repeat
- changeDir()
- until dig_direction == home_direction_y
- end
- if y_amount ~= 0 then
- y_amount = math.floor(y_amount)
- forward(y_amount)
- end
- sleep(1)
- end
- end
- function comeHome()
- print("function: ComeHome")
- if fs.exists("query/crash_report") then
- print("yes")
- end
- local h = fs.open("query/crash_report", "r")
- local new_given_x = h.readLine()
- local new_given_y = h.readLine()
- local new_given_z = h.readLine()
- local end_direction = h.readLine()
- h.close()
- print(given_z)
- local x, y, z = gps.locate(5)
- -- print(math.floor(x))
- local x_amount = math.floor(new_given_x) - math.floor(x)
- local y_amount = math.floor(new_given_y) - math.floor(y)
- local z_amount = math.floor(new_given_z) - math.floor(z)
- -- print("XXX")
- -- print(new_given_x)
- -- print(new_given_y)
- -- print(new_given_z)
- -- print("YYYY")
- -- print(x_amount)
- -- print(y_amount)
- -- print(z_amount)
- -- print("XXX")
- sleep(1)
- if y_amount ~= 0 then
- if y_amount < 0 then
- home_direction_y = "N"
- else
- home_direction_y = "S"
- end
- end
- if x_amount ~= 0 then
- if x_amount < 0 then
- home_direction_x = "W"
- else
- home_direction_x = "E"
- end
- end
- print(home_direction_x)
- print(home_direction_y)
- sleep(1)
- if dig_direction ~= home_direction_y then
- repeat
- changeDir()
- until dig_direction == home_direction_y
- end
- if y_amount ~= 0 then
- y_amount = math.floor(y_amount)
- forward(y_amount)
- end
- if dig_direction ~= home_direction_x then
- repeat
- changeDir()
- until dig_direction == home_direction_x
- end
- if x_amount ~= 0 then
- x_amount = math.floor(x_amount)
- forward(x_amount)
- end
- if z_amount ~= 0 then
- print(z_amount)
- goUp(z_amount)
- end
- if dig_direction ~= end_direction then
- repeat
- changeDir()
- until dig_direction == end_direction
- end
- end
- function forward(amount)
- local _forward = 0
- local counter = 0
- if amount > 0 then
- repeat
- if turtle.forward() then
- _forward = _forward + 1
- else
- turtle.dig()
- end
- until _forward >= amount
- else
- repeat
- if turtle.forward() then
- _forward = _forward - 1
- else
- turtle.dig()
- end
- until _forward <= amount
- end
- end
- function dig(amount)
- local _dig_forward = 0
- if amount ~= 0 then
- if amount > 0 then
- repeat
- turtle.digDown()
- while turtle.forward() == false do
- turtle.dig()
- end
- turtle.digDown()
- _dig_forward = _dig_forward + 1
- checkEmpty()
- until _dig_forward == amount
- elseif amount < 0 then
- repeat
- turtle.digDown()
- while turtle.forward() == false do
- turtle.dig()
- end
- turtle.digDown()
- _dig_forward = _dig_forward - 1
- checkEmpty()
- until _dig_forward == amount
- end
- end
- end
- function checkEmpty()
- if turtle.getItemCount(16) > 0 then
- local x, y, z = gps.locate(5)
- print("making a crash report file")
- fs.delete("query/crash_report")
- if fs.exists("query/crash_report") then
- print("crash_report not deleted")
- end
- local h = fs.open("query/crash_report", "w")
- h.writeLine(x)
- h.writeLine(y)
- h.writeLine(z)
- h.writeLine(dig_direction)
- h.close()
- sleep(1)
- print("Gonna empty")
- goHome()
- searchChest()
- empty_all()
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.select(1)
- checkCompas()
- comeHome()
- end
- end
- function digQuery()
- turtle.select(1)
- print("Function: digQuery")
- local rows
- if math.floor(Depth) == 0 then
- print("Going to bedrock")
- repeat
- local x, y, z = gps.locate(5)
- repeat
- local x, y, z = gps.locate(5)
- local height_row = math.floor(home_z) - math.floor(z)
- -- print("height_row = " .. height_row)
- if start_direction == "W" or start_direction == "E" then
- if start_direction == "W" then
- if height_row % 4 == 2 then
- rows = (math.floor(y) - math.floor(home_y))
- else
- rows = (math.floor(home_y) - (math.floor(y - Width))-1)
- end
- else
- if height_row % 4 == 2 then
- rows = (math.floor(home_y) - math.floor(y))
- else
- rows = (math.floor(y) - (math.floor(home_y - Width))-1)
- end
- end
- print("rows: " .. rows)
- if dig_direction == start_direction then
- local x_amount = math.floor(x) - (math.floor(home_x - Length )+1)
- print(x_amount)
- dig(x_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDirRight()
- dig(1)
- changeDirRight()
- else
- changeDir()
- dig(1)
- changeDir()
- end
- end
- else
- local x_amount = math.floor(home_x) - math.floor(x)
- print(x_amount)
- dig(x_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDir()
- dig(1)
- changeDir()
- else
- changeDirRight()
- dig(1)
- changeDirRight()
- end
- end
- end
- elseif start_direction == "N" or start_direction == "S" then
- if start_direction == "N" then
- if height_row % 4 == 2 then
- rows = (math.floor(home_x) - math.floor(x))
- else
- rows = (math.floor(x) - (math.floor(home_x - Width))-1)
- end
- else
- if height_row % 4 == 2 then
- rows = (math.floor(x) - math.floor(home_x))
- else
- rows = (math.floor(home_x) - (math.floor(x - Width))-1)
- end
- end
- print("rows: " .. rows)
- if dig_direction == start_direction then
- local y_amount = math.floor(y) - (math.floor(home_y - Length)+1)
- print(y_amount)
- dig(y_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDirRight()
- dig(1)
- changeDirRight()
- else
- changeDir()
- dig(1)
- changeDir()
- end
- end
- else
- local y_amount = math.floor(home_y) - math.floor(y)
- print(y_amount)
- dig(y_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDir()
- dig(1)
- changeDir()
- else
- changeDirRight()
- dig(1)
- changeDirRight()
- end
- end
- end
- end
- until rows == 0
- local x, y, z = gps.locate(5)
- if (math.floor(z) - 2) > 5 then
- print(math.floor(z) + 2)
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- changeDir()
- changeDir()
- end
- until (math.floor(z) - 2) <= 5
- else
- if math.floor(z) > (math.floor(home_z) - math.floor(Depth)) then
- repeat
- local x, y, z = gps.locate(5)
- repeat
- local x, y, z = gps.locate(5)
- local height_row = math.floor(home_z) - math.floor(z)
- -- print("height_row = " .. height_row)
- if start_direction == "W" or start_direction == "E" then
- if start_direction == "W" then
- if height_row % 4 == 2 then
- rows = (math.floor(y) - math.floor(home_y))
- else
- rows = (math.floor(home_y) - (math.floor(y - Width))-1)
- end
- else
- if height_row % 4 == 2 then
- rows = (math.floor(home_y) - math.floor(y))
- else
- rows = (math.floor(y) - (math.floor(home_y - Width))-1)
- end
- end
- print("rows: " .. rows)
- if dig_direction == start_direction then
- local x_amount = math.floor(x) - (math.floor(home_x - Length )+1)
- print(x_amount)
- dig(x_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDirRight()
- dig(1)
- changeDirRight()
- else
- changeDir()
- dig(1)
- changeDir()
- end
- end
- else
- local x_amount = math.floor(home_x) - math.floor(x)
- print(x_amount)
- dig(x_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDir()
- dig(1)
- changeDir()
- else
- changeDirRight()
- dig(1)
- changeDirRight()
- end
- end
- end
- elseif start_direction == "N" or start_direction == "S" then
- if start_direction == "N" then
- if height_row % 4 == 2 then
- rows = (math.floor(home_x) - math.floor(x))
- else
- rows = (math.floor(x) - (math.floor(home_x - Width))-1)
- end
- else
- if height_row % 4 == 2 then
- rows = (math.floor(x) - math.floor(home_x))
- else
- rows = (math.floor(home_x) - (math.floor(x - Width))-1)
- end
- end
- print("rows: " .. rows)
- if dig_direction == start_direction then
- local y_amount = math.floor(y) - (math.floor(home_y - Length)+1)
- print(y_amount)
- dig(y_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDirRight()
- dig(1)
- changeDirRight()
- else
- changeDir()
- dig(1)
- changeDir()
- end
- end
- else
- local y_amount = math.floor(home_y) - math.floor(y)
- print(y_amount)
- dig(y_amount)
- if rows > 0 then
- if height_row % 4 == 2 then
- changeDir()
- dig(1)
- changeDir()
- else
- changeDirRight()
- dig(1)
- changeDirRight()
- end
- end
- end
- end
- until rows == 0
- local x, y, z = gps.locate(5)
- print(z)
- print((home_z - Depth) + 2)
- if math.floor(z) > ((home_z - Depth) + 2) then
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- changeDir()
- changeDir()
- end
- until math.floor(z) <= ((math.floor(home_z) - math.floor(Depth)) + 2)
- end
- end
- goHome()
- --end
- searchChest()
- for q=1,16 do
- turtle.select(q)
- turtle.drop()
- end
- turtle.select(1)
- turtle.turnLeft()
- turtle.turnLeft()
- local h = fs.open("query/firstInput", "r")
- local readline_1 = h.readLine()
- local readline_2 = h.readLine()
- local doneOrNot = tostring(h.readLine())
- local readline_4 = h.readLine()
- local readline_5 = h.readLine()
- h.close()
- fs.delete("query/firstInput")
- if fs.exists("query/firstInput") then
- print("file not deleted or edited")
- else
- local h = fs.open("query/firstInput", "w")
- h.writeLine(readline_1)
- h.writeLine(readline_2)
- h.writeLine("done")
- h.writeLine(readline_4)
- h.writeLine(readline_5)
- h.close()
- end
- print("Done!")
- deleteOrNot()
- end
- function empty_all()
- if turtle.drop() then
- for q=1,16 do
- turtle.select(q)
- turtle.drop()
- end
- else
- local one_up = 0
- local cannot_drop = 0
- local could_drop = 0
- print("chest is full!")
- repeat
- repeat
- turtle.up()
- sleep(1)
- if turtle.drop() then
- could_drop = 1
- print("Could drop?")
- end
- one_up = one_up + 1
- print(one_up)
- until could_drop == 1 or one_up == 10
- until could_drop == 1 or one_up == 10
- if one_up ~= 10 then
- for q=1,16 do
- turtle.select(q)
- turtle.drop()
- end
- else
- cannot_drop = 1
- end
- repeat
- turtle.down()
- one_up = one_up - 1
- until one_up == 0
- if cannot_drop == 1 then
- print("no storage!")
- term.write("Enter something if it's fixed")
- read()
- print("gonna try again")
- empty_all()
- end
- end
- end
- --start
- if fs.exists("query/firstInput") then
- --we zijn gecrasht...
- local h = fs.open("query/firstInput", "r")
- local given_length = h.readLine()
- local given_width = h.readLine()
- local doneOrNot = tostring(h.readLine())
- start_direction = h.readLine()
- local given_depth = h.readLine()
- h.close()
- sleep(1)
- local h = fs.open("query/home_pos", "r")
- local given_x = h.readLine()
- local given_y = h.readLine()
- local given_z = h.readLine()
- h.close()
- sleep(1)
- if tostring(doneOrNot) == "notDone" then
- Length = given_length
- Width = given_width
- Depth = given_depth
- home_x = given_x
- home_y = given_y
- home_z = given_z
- crashOccured()
- elseif tostring(doneOrNot) == 'done' then
- home_x = given_x
- home_y = given_y
- home_z = given_z
- print("Okey")
- if math.floor(home_x) == math.floor(x) and math.floor(home_y) == math.floor(y) and math.floor(home_z) == math.floor(z) then
- deleteOrNot()
- else
- goHome()
- deleteOrNot()
- end
- end
- else
- --eerste input
- term.write("Insert Length: ")
- Length = tonumber(read())
- print(" ")
- term.write("Insert Width: ")
- Width = tonumber(read())
- print(" ")
- term.write("Insert Depth (0 = to bedrock): ")
- Depth = tonumber(read())
- print(" ")
- --make it so we know first input was true
- print("making folder")
- fs.makeDir("query")
- checkCompas()
- start_direction = dig_direction
- print("making a first input file")
- local h = fs.open("query/firstInput", "w")
- h.writeLine(Length)
- h.writeLine(Width)
- h.writeLine("notDone")
- h.writeLine(start_direction)
- h.writeLine(Depth)
- h.close()
- local x, y, z = gps.locate(5)
- x = math.floor(x)
- y = math.floor(y)
- z = math.floor(z)
- print("Making home pos file")
- local h = fs.open("query/home_pos", "w")
- h.writeLine(x)
- h.writeLine(y)
- h.writeLine(z)
- h.close()
- 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
- Length = Length
- Width = Width
- home_x = x
- home_y = y
- home_z = z
- digQuery()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement