Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Global Variables--
- min_x = 1
- max_x = 1
- mem_x = 1
- home_x = 1
- ret_x = 1
- x = 1
- min_y = 1
- max_y = 1
- mem_y = 1
- home_y = 1
- ret_y = 1
- y = 1
- min_z = 1
- max_z = 1
- mem_z = 1
- home_z = 1
- ret_z = 1
- z = 1
- f = 1
- mem_f = 1
- home_f = 1
- ret_f = 1
- continue = false
- x_forward = true
- y_forward = true
- new_floor = true
- turtle_objective = "mine"
- writeSchedulerCounter = 0
- function crash()
- print("turtle has gone wrong, r to reboot")
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "r" then
- os.restart()
- end
- end
- end
- function debugStop()
- print("in debug stop")
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "e" then os.restart() end
- end
- end
- function forward()
- local i = 0
- local operation_output = true
- turtle.dig()
- while turtle.forward() == false do
- turtle.dig()
- turtle.attack()
- i = i + 1
- if i == 10 then
- operation_output = false
- break
- end
- sleep(1)
- end
- if operation_output == true then
- if f == 1 then
- y = y + 1
- elseif f == 2 then
- x = x + 1
- elseif f == 3 then
- y = y - 1
- elseif f == 4 then
- x = x - 1
- else
- print("ERROR: turtle has no direction?!?!?")
- crash()
- end
- end
- writeScheduler()
- return operation_output
- end
- function forwardMine()
- local i = 0
- local operation_output = true
- turtle.dig()
- while turtle.forward() == false do
- turtle.dig()
- turtle.attack()
- i = i + 1
- if i == 10 then
- operation_output = false
- break
- end
- sleep(1)
- end
- if operation_output == true then
- turtle.digDown()
- if f == 1 then
- y = y + 1
- elseif f == 2 then
- x = x + 1
- elseif f == 3 then
- y = y - 1
- elseif f == 4 then
- x = x - 1
- else
- print("ERROR: turtle has no direction?!?!?")
- crash()
- end
- end
- writeScheduler()
- return operation_output
- end
- function up()
- local i = 0
- local operation_output = true
- turtle.digUp()
- while turtle.up() == false do
- turtle.digUp()
- turtle.attackUp()
- i = i + 1
- if i == 10 then
- operation_output = false
- break
- end
- sleep(1)
- end
- if operation_output == true then
- z = z + 1
- end
- writeScheduler()
- return operation_output
- end
- function down()
- local i = 0
- local operation_output = true
- turtle.digDown()
- while turtle.down() == false do
- turtle.digDown()
- turtle.attackDown()
- i = i + 1
- if i == 10 then
- operation_output = false
- break
- end
- sleep(1)
- end
- if operation_output == true then
- z = z - 1
- end
- writeScheduler()
- return operation_output
- end
- function turnLeft(i_end)
- for i = 1, i_end do
- turtle.turnLeft()
- f = f - 1
- if f == 0 then
- f = 4
- end
- end
- writeScheduler()
- end
- function turnRight(i_end)
- for i = 1, i_end do
- turtle.turnRight()
- f = f + 1
- if f == 5 then
- f = 1
- end
- end
- writeScheduler()
- end
- function turnRel(direction)
- local forward_f = home_f
- local right_f = home_f
- local backwards_f = home_f
- local left_f = home_f
- for i = 1,1 do
- right_f = right_f + 1
- if right_f == 5 then right_f = 1 end
- end
- for i = 1,2 do
- backwards_f = backwards_f + 1
- if backwards_f == 5 then backwards_f = 1 end
- end
- for i = 1,3 do
- left_f = left_f + 1
- if left_f == 5 then left_f = 1 end
- end
- if direction == "forwards" then
- while f ~= forward_f do
- if f < forward_f then
- if f == 1 and forward_f == 4 then
- turtle.turnLeft()
- f = 4
- else
- turtle.turnRight()
- f = f + 1
- end
- else
- if f == 4 and forward_f == 1 then
- turtle.turnRight()
- f = 1
- else
- turtle.turnLeft()
- f = f - 1
- end
- end
- end
- elseif direction == "right" then
- while f ~= right_f do
- if f < right_f then
- if f == 1 and right_f == 4 then
- turtle.turnLeft()
- f = 4
- else
- turtle.turnRight()
- f = f + 1
- end
- else
- if f == 4 and right_f == 1 then
- turtle.turnRight()
- f = 1
- else
- turtle.turnLeft()
- f = f - 1
- end
- end
- end
- elseif direction == "backwards" then
- while f ~= backwards_f do
- if f < backwards_f then
- if f == 1 and backwards_f == 4 then
- turtle.turnLeft()
- f = 4
- else
- turtle.turnRight()
- f = f + 1
- end
- else
- if f == 4 and backwards_f == 1 then
- turtle.turnRight()
- f = 1
- else
- turtle.turnLeft()
- f = f - 1
- end
- end
- end
- elseif direction == "left" then
- while f ~= left_f do
- if f < left_f then
- if f == 1 and left_f == 4 then
- turtle.turnLeft()
- f = 4
- else
- turtle.turnRight()
- f = f + 1
- end
- else
- if f == 4 and left_f == 1 then
- turtle.turnRight()
- f = 1
- else
- turtle.turnLeft()
- f = f - 1
- end
- end
- end
- else
- print("ERROR: incorrect reletive direction call")
- crash()
- end
- pcall(writeState)
- --print("forward_f:",forward_f," right_f:",right_f," backwards_f:",backwards_f," left_f:",left_f)
- --print("current_f:",f)
- end
- function turn(goto_f)
- while f ~= goto_f do
- if f < goto_f then
- if f == 1 and goto_f == 4 then
- turtle.turnLeft()
- f = 4
- else
- turtle.turnRight()
- f = f + 1
- end
- else
- if f == 4 and goto_f == 1 then
- turtle.turnRight()
- f = 1
- else
- turtle.turnLeft()
- f = f - 1
- end
- end
- end
- end
- function getLocation()
- rednet.open("right")
- local x1, y1, z1 = gps.locate(5,false)
- if x == nil then
- print("out of GPS range")
- crash()
- end
- if forward() == false then
- crash()
- end
- local x2, y2, z2 = gps.locate(5,false)
- turnRight(2)
- if forward() == false then
- crash()
- end
- turnRight(2)
- rednet.close("right")
- if y1 - y2 == -1 then
- f = 1
- elseif x1 - x2 == -1 then
- f = 2
- elseif y1 - y2 == 1 then
- f = 3
- elseif x1 - x2 == 1 then
- f = 4
- else
- print("ERROR: GPS wonkyness")
- crash()
- end
- x = x1
- y = y1
- z = z1
- end
- function readState() -- need to pcall this function so it doesn't break if it won't yield
- if fs.exists("MinerSavedData") == true then
- local file = fs.open("MinerSavedData","r")
- local data = file.readAll()
- file.close()
- local tablevar = {}
- tablevar = textutils.unserialize(data)
- min_x = tablevar["min_x"]
- max_x = tablevar["max_x"]
- mem_x = tablevar["mem_x"]
- home_x = tablevar["home_x"]
- ret_x = tablevar["ret_x"]
- min_y = tablevar["min_y"]
- max_y = tablevar["max_y"]
- mem_y = tablevar["mem_y"]
- home_y = tablevar["home_y"]
- ret_y = tablevar["ret_y"]
- min_z = tablevar["min_z"]
- max_z = tablevar["max_z"]
- mem_z = tablevar["mem_z"]
- home_z = tablevar["home_z"]
- ret_z = tablevar["ret_z"]
- mem_f = tablevar["mem_f"]
- home_f = tablevar["home_f"]
- ret_f = tablevar["ret_f"]
- continue = tablevar["continue"]
- x_forward = tablevar["x_forward"]
- y_forward = tablevar["y_forward"]
- turtle_objective = tablevar["turtle_objective"]
- new_floor = tablevar["new_floor"]
- end
- end
- function updateMemory()
- mem_x = x
- mem_y = y
- mem_z = z
- mem_f = f
- end
- function updateReturn()
- ret_x = x
- ret_y = y
- ret_z = z
- ret_f = f
- end
- function writeState() -- need to pcall this function so it doesn't break if it won't yield
- local tablevar = {}
- tablevar["min_x"] = min_x
- tablevar["max_x"] = max_x
- tablevar["mem_x"] = mem_x
- tablevar["home_x"] = home_x
- tablevar["ret_x"] = ret_x
- tablevar["min_y"] = min_y
- tablevar["max_y"] = max_y
- tablevar["mem_y"] = mem_y
- tablevar["home_y"] = home_y
- tablevar["ret_y"] = ret_y
- tablevar["min_z"] = min_z
- tablevar["max_z"] = max_z
- tablevar["mem_z"] = mem_z
- tablevar["home_z"] = home_z
- tablevar["ret_z"] = ret_z
- tablevar["mem_f"] = mem_f
- tablevar["home_f"] = home_f
- tablevar["ret_f"] = ret_f
- tablevar["continue"] = continue
- tablevar["x_forward"] = x_forward
- tablevar["y_forward"] = y_forward
- tablevar["turtle_objective"] = turtle_objective
- tablevar["new_floor"] = new_floor
- local file = fs.open("MinerSavedData","w")
- file.write(textutils.serialize(tablevar))
- file.close()
- end
- function writeScheduler()
- writeSchedulerCounter = writeSchedulerCounter + 1
- if writeSchedulerCounter == 10 then
- pcall(writeState)
- writeSchedulerCounter = 0
- end
- end
- function startup()
- term.clear()
- term.setCursorPos(1, 1)
- pcall(readState)
- if continue == true then
- print("existing instruction set found: (c)ontinue or (r)estart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "r" then
- continue = false
- print("resetting instruction set")
- break
- end
- if event == "char" and character == "c" then
- print("Continuing...")
- break
- end
- end
- end
- print("Using GPS to calculate location, please make sure nothing important is in front of me and press c to continue")
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "c" then break end
- end
- getLocation()
- if continue == false then
- term.clear()
- term.setCursorPos(1, 1)
- print("Please set me up at the bottom left corner of the excavation with a chest for spoil behind me and a chest for fuel to my left")
- print("")
- print("am I ready to continue? Press c to begin")
- while true do
- local event, character = os.pullEvent()
- if event == "char" and character == "c" then break end
- end
- term.clear()
- term.setCursorPos(1, 1)
- home_x = x
- home_y = y
- home_z = z
- home_f = f
- if home_f == 1 then
- print("Width to dig?")
- min_x = x
- max_x = min_x + (tonumber(read()) - 1) --because its already at the 1st block
- print("length to dig (how far back)?")
- min_y = y
- max_y = min_y + (tonumber(read()) - 1)
- print("hight to dig? Please use an even number")
- min_z = z
- max_z = min_z + (tonumber(read()) - 1)
- continue = true
- x_forward = true
- y_forward = true
- new_floor = true
- turtle_objective = "mine"
- elseif home_f == 2 then
- print("Width to dig?")
- max_y = y
- min_y = max_y - (tonumber(read()) - 1)
- print("length to dig (how far back)?")
- min_x = x
- max_x = min_x + (tonumber(read()) - 1)
- print("hight to dig? Please use an even number")
- min_z = z
- max_z = min_z + (tonumber(read()) - 1)
- continue = true
- x_forward = true
- y_forward = false
- new_floor = true
- turtle_objective = "mine"
- elseif home_f == 3 then
- print("Width to dig?")
- max_x = x
- min_x = max_x - (tonumber(read()) - 1)
- print("length to dig (how far back)?")
- max_y = y
- min_y = max_y - (tonumber(read()) - 1)
- print("hight to dig? Please use an even number")
- min_z = z
- max_z = min_z + (tonumber(read()) - 1)
- continue = true
- x_forward = false
- y_forward = false
- new_floor = true
- turtle_objective = "mine"
- elseif home_f == 4 then
- print("Width to dig?")
- min_y = y
- max_y = min_y + (tonumber(read()) - 1)
- print("length to dig (how far back)?")
- max_x = x
- min_x = max_x - (tonumber(read()) - 1)
- print("hight to dig? Please use an even number")
- min_z = z
- max_z = min_z + (tonumber(read()) - 1)
- continue = true
- x_forward = false
- y_forward = true
- new_floor = true
- turtle_objective = "mine"
- else
- print("ERROR: home_f produced an incorrect value")
- crash()
- end
- local skip_z = 0
- print("do you wish to skip to a certain hight?, even floors only and use 0 for no")
- skip_z = tonumber(read())
- if skip_z ~= 0 then
- local i = 0
- while i ~= skip_z do
- if up() == false then crash() end
- i = i + 1
- end
- end
- updateMemory()
- writeState()
- term.clear()
- term.setCursorPos(1, 1)
- print("starting operation")
- textutils.slowPrint("************************************GO!")
- sleep(0.5)
- term.clear()
- term.setCursorPos(1, 1)
- if up() == false then crash() end
- updateMemory()
- end
- end
- function checkFuel()
- local home_fuel = 20 -- bias added just to make sure
- if x > home_x then
- home_fuel = home_fuel + (x - home_x)
- else
- home_fuel = home_fuel + (home_x - x)
- end
- if y > home_y then
- home_fuel = home_fuel + (y - home_y)
- else
- home_fuel = home_fuel + (home_y - y)
- end
- if z > home_z then
- home_fuel = home_fuel + (z - home_z)
- else
- home_fuel = home_fuel + (home_z - z)
- end
- print("DEBUG home_fuel:",home_fuel)
- if turtle.getFuelLevel() < home_fuel then
- print("Fuel check failed")
- return false
- else
- return true
- end
- end
- function checkInventory()
- local space = false
- local i = 1
- repeat
- if turtle.getItemCount(i) == 0 then
- space = true
- end
- i = i + 1
- until i == 17
- if space == false then
- print("Inventory check failed")
- end
- return space
- end
- function unload()
- turnRel("backwards")
- local i = 1
- repeat
- turtle.select(i)
- if turtle.drop() == false then
- turtle.dropDown()
- end
- i = i + 1
- until i == 17
- turtle.select(1)
- if turtle.getFuelLevel() < 500 then
- turnRel("left")
- turtle.select(1)
- if turtle.suck() == false then
- print("ERROR: no Fuel")
- crash()
- end
- turtle.refuel()
- turnRel("backwards")
- turtle.select(1)
- if turtle.drop() == false then
- turtle.dropDown()
- end
- end
- end
- function mineXStrip()
- if x_forward == true then
- turn(2)
- repeat
- if forwardMine() == false then crash() end
- updateMemory()
- if checkFuel() == false or checkInventory() == false then
- updateReturn()
- turtle_objective = "home"
- gotoHome()
- unload()
- gotoReturn()
- turtle_objective = "mine"
- end
- until x == max_x
- x_forward = false
- return
- elseif x_forward == false then
- turn(4)
- repeat
- if forwardMine() == false then crash() end
- updateMemory()
- if checkFuel() == false or checkInventory() == false then
- updateReturn()
- turtle_objective = "home"
- gotoHome()
- unload()
- gotoReturn()
- turtle_objective = "mine"
- end
- until x == min_x
- x_forward = true
- return
- else
- print("ERROR: incorrect x_forward value")
- crash()
- end
- end
- function gotoMemory()
- while x ~= mem_x do
- if x > mem_x then
- turn(4)
- if forward() == false then crash() end
- elseif x < mem_x then
- turn(2)
- if forward() == false then crash() end
- end
- end
- while y ~= mem_y do
- if y > mem_y then
- turn(3)
- if forward() == false then crash() end
- elseif y < mem_y then
- turn(1)
- if forward() == false then crash() end
- end
- end
- while z ~= mem_z do
- if z > mem_z then
- if down() == false then crash() end
- elseif z < mem_z then
- if up() == false then crash() end
- end
- end
- turn(mem_f)
- end
- function gotoHome()
- while z ~= home_z do
- if z > home_z then
- if down() == false then crash() end
- elseif z < home_z then
- if up() == false then crash() end
- end
- end
- while y ~= home_y do
- if y > home_y then
- turn(3)
- if forward() == false then crash() end
- elseif y < home_y then
- turn(1)
- if forward() == false then crash() end
- end
- end
- while x ~= home_x do
- if x > home_x then
- turn(4)
- if forward() == false then crash() end
- elseif x < home_x then
- turn(2)
- if forward() == false then crash() end
- end
- end
- end
- function gotoReturn()
- while x ~= ret_x do
- if x > ret_x then
- turn(4)
- if forward() == false then crash() end
- elseif x < ret_x then
- turn(2)
- if forward() == false then crash() end
- end
- end
- while y ~= ret_y do
- if y > ret_y then
- turn(3)
- if forward() == false then crash() end
- elseif y < ret_y then
- turn(1)
- if forward() == false then crash() end
- end
- end
- while z ~= ret_z do
- if z > ret_z then
- if down() == false then crash() end
- elseif z < ret_z then
- if up() == false then crash() end
- end
- end
- turn(ret_f)
- end
- ------------------
- -- Main Program --
- ------------------
- local testmode = false
- if testmode == false then
- startup()
- if turtle_objective == "home" then
- gotoHome()
- unload()
- gotoMemory()
- turtle_objective = "mine"
- end
- gotoMemory()
- new_floor = true
- repeat
- if new_floor == false then
- if up() == false then crash() end
- if up() == false then crash() end
- new_floor = true
- updateMemory()
- end
- if y_forward == true then
- repeat
- if new_floor == false then
- turn(1)
- if forwardMine() == false then crash() end
- updateMemory()
- end
- mineXStrip()
- new_floor = false
- until y == max_y
- y_forward = false
- elseif y_forward == false then
- repeat
- if new_floor == false then
- turn(3)
- if forwardMine() == false then crash() end
- updateMemory()
- end
- mineXStrip()
- new_floor = false
- until y == min_y
- y_forward = true
- else
- print("ERROR incorrect y_forward value")
- crash()
- end
- until z == max_z
- gotoHome()
- continue = false
- pcall(writeState)
- term.clear()
- term.setCursorPos(1, 1)
- print("All Done")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement