Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("right")
- con = "off"
- function topToBack() -- move turtle from top of controller to back
- turtle.forward()
- turtle.down()
- turtle.turnRight()
- turtle.turnRight()
- pos = "back"
- file = fs.open("pos", "w")
- file.write(pos)
- file:close()
- eng = peripheral.wrap("front")
- end
- function backToTop() -- move turtle from behind engine to top
- turtle.up()
- turtle.forward()
- turtle.turnRight()
- turtle.turnRight()
- pos = "top"
- file = fs.open("pos", "w")
- file.write(pos)
- file:close()
- eng = peripheral.wrap("bottom")
- end
- function goDown()
- dir = 29
- eng.move(0, false, false)
- turtle.down()
- yPos = yPos - 1
- file = fs.open("y", "w")
- file.write(yPos)
- file:close()
- sleep(1)
- end
- function goUp()
- dir = 42
- eng.move(1, false, false)
- turtle.up()
- yPos = yPos + 1
- file = fs.open("y", "w")
- file.write(yPos)
- file:close()
- sleep(1)
- end
- function goForward()
- dir = 17
- eng.move(2, false, false)
- os.sleep(1)
- zPos = zPos - 1
- file = fs.open("z", "w")
- file.write(zPos)
- file:close()
- turtle.forward()
- end
- function goBack()
- dir = 31
- eng.move(3, false, false)
- turtle.forward()
- zPos = zPos + 1
- file = fs.open("z", "w")
- file.write(zPos)
- file:close()
- sleep(1)
- end
- function goRight()
- dir = 32
- eng.move(5, false, false)
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- xPos = xPos + 1
- file = fs.open("x", "w")
- file.write(xPos)
- file:close()
- os.sleep(0.2)
- end
- function goLeft()
- eng.move(4, false, false)
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- xPos = xPos - 1
- file = fs.open("x", "w")
- file.write(xPos)
- file:close()
- os.sleep(0.2)
- end
- function display() -- screen display
- term.clear()
- x,y = term.getSize()
- term.setCursorPos(x/2, y/3-1)
- print("W")
- term.setCursorPos((x/5*4) - 2, y/3-1)
- print("Auto(R)")
- term.setCursorPos(x/2-3, y/3)
- print("Forward")
- term.setCursorPos(x/5*4, y/3)
- print(con)
- term.setCursorPos(x/5, y/3*2)
- print("A")
- term.setCursorPos(x/2, y/3*2)
- print("S")
- term.setCursorPos(x/5*4, y/3*2)
- print("D")
- term.setCursorPos(x/5-2, y/3*2+1)
- print("Left")
- term.setCursorPos(x/2-2, y/3*2+1)
- print("Back")
- term.setCursorPos(x/5*4-2, y/3*2+1)
- print("Right")
- term.setCursorPos(1, y-3)
- print("[Shift]")
- term.setCursorPos(3,y-2)
- print("Up")
- term.setCursorPos(1, y-1)
- print("[Ctrl]")
- term.setCursorPos(2, y)
- print("Down")
- end
- function waypoint() -- lets user set a waypoint to travel to
- term.clear()
- term.setCursorPos(1,1)
- write("Enter x coordinate of destination: ")
- xDest = io.read()
- xDest = tonumber(xDest)
- term.clear()
- term.setCursorPos(1,1)
- write("Enter y coordinate of destination: ")
- yDest = io.read()
- yDest = tonumber(yDest)
- term.clear()
- term.setCursorPos(1,1)
- write("Enter z coordinate of destination: ")
- zDest = io.read()
- zDest = tonumber(zDest)
- term.clear()
- term.setCursorPos(1,1)
- print("Traveling to "..xDest.." "..yDest.." "..zDest)
- if pos == "top" then
- topToBack()
- end
- if yPos < yDest then
- for i = 1, yDest - yPos do
- goUp()
- end
- elseif yPos > yDest then
- for i = 1, yPos - yDest do
- goDown()
- end
- end
- if xPos < xDest then
- if xDest - xPos < 0 then
- xDis = (xDest - xPos) * -1
- else
- xDis = xDest - xPos
- end
- for i = 1, xDis do
- goRight()
- end
- elseif xPos > xDest then
- if xPos - xDest < 0 then
- xDis = (xPos - xDest) * -1
- else
- xDis = xPos - xDest
- end
- for i = 1, xDis do
- goLeft()
- end
- end
- if zPos < zDest then
- if zPos - zDest < 0 then
- zDis = (zPos - zDest) * -1
- else
- --zDis = zDest - xPos
- zDis = zPos - zDest
- end
- if pos == "back" then
- backToTop()
- end
- for i = 1, zDis do
- goBack()
- end
- elseif zPos > zDest then
- if zDest - zPos < 0 then
- zDis = (zPos - zDest) * -1
- else
- zDis = zPos - zDest
- end
- for i = 1, xDis do
- goForward()
- end
- end
- end
- -- determine turtle position above or behind the engine
- result = fs.exists("pos")
- if result == true then
- file = io.open("pos", "r")
- pos = file.read()
- file:close()
- else
- pos = "back"
- file = fs.open("pos", "w")
- file.write(pos)
- file:close()
- end
- if pos == "back" then
- eng = peripheral.wrap("front")
- else
- eng = peripheral.wrap("bottom")
- end
- function coords()
- file = io.open("x", "r")
- xPos = file.read()
- xPos = tonumber(xPos)
- file:close()
- file = io.open("y", "r")
- yPos = file.read()
- yPos = tonumber(yPos)
- file:close()
- file = io.open("z", "r")
- zPos = file.read()
- zPos = tonumber(zPos)
- file:close()
- end
- result = fs.exists("x") --determine if turtle knows it's position
- if result == true then
- file = io.open("x", "r")
- xPos = file.read()
- xPos = tonumber(xPos)
- file:close()
- file = io.open("y", "r")
- yPos = file.read()
- yPos = tonumber(yPos)
- file:close()
- file = io.open("z", "r")
- zPos = file.read()
- zPos = tonumber(zPos)
- file:close()
- else
- term.clear()
- term.setCursorPos(1,1)
- write("Enter the Turtle's x coordinate: ")
- xPos = io.read()
- file = fs.open("x", "w")
- file.write(xPos)
- file:close()
- term.clear()
- xPos = tonumber(xPos)
- term.setCursorPos(1,1)
- write("Enter the Turtle's y coordinate: ")
- yPos = io.read()
- file = fs.open("y", "w")
- file.write(yPos)
- file:close()
- term.clear()
- yPos = tonumber(yPos)
- term.setCursorPos(1,1)
- write("Enter the Turtle's z coordinate: ")
- zPos = io.read()
- file = fs.open("z", "w")
- file.write(zPos)
- file:close()
- zPos = tonumber(zPos)
- term.clear()
- term.setCursorPos(1,1)
- end
- while true do
- coords()
- display()
- timeout = os.startTimer(0.5)
- event, param = os.pullEvent()
- if event == "timer" then
- if con == "on" then
- if dir ~= 31 then
- if pos == "top" then
- topToBack()
- end
- if dir == 17 then
- goForward()
- elseif dir == 32 then
- goRight()
- elseif dir == 30 then
- goLeft()
- elseif dir == 42 then
- goUp()
- elseif dir == 29 then
- goDown()
- end
- elseif dir == 31 then
- dir = 31
- if pos == "back" then
- backToTop()
- end
- goBack()
- end
- end
- end
- if param ~= 31 and event == "key" then
- if pos == "top" then
- topToBack()
- end
- if param == 17 then
- goForward()
- elseif param == 32 then
- goRight()
- elseif param == 30 then
- goLeft()
- elseif param == 42 then
- goUp()
- elseif param == 29 then
- goDown()
- elseif param == 19 then
- if con == "on" then
- con = "off"
- else
- con = "on"
- end
- os.sleep(0.5)
- dir = nil
- term.setCursorPos(x/5*4, y/3-1)
- print(con.." ")
- elseif param == 46 then
- waypoint()
- end
- --end
- elseif param == 31 then
- dir = 31
- if pos == "back" then
- backToTop()
- end
- goBack()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement