Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = "0.1"
- filename = "directions.loc"
- local iBearing = 1
- local tlocalCoordinates = {x = 0, y = 0, z = 0}
- local tInstructions = {}
- local tDirections = {left = "left", right = "right", up = "up", down = "down", forward = "forward", back = "back", redstone = "redstone"}
- local tArgs = {...}
- local function printUsage()
- print("Usage:")
- print("movement record")
- print("movement play")
- end
- if #tArgs < 1 then
- printUsage()
- return
- end
- function undo()
- if tInstructions[#tInstructions] == tDirections.left then
- turtle.turnRight()
- table.remove(tInstructions, #tInstructions)
- elseif tInstructions[#tInstructions] == tDirections.right then
- turtle.turnLeft()
- table.remove(tInstructions, #tInstructions)
- elseif tInstructions[#tInstructions] == tDirections.forward then
- turtle.back()
- table.remove(tInstructions, #tInstructions)
- elseif tInstructions[#tInstructions] == tDirections.back then
- turtle.forward()
- table.remove(tInstructions, #tInstructions)
- elseif tInstructions[#tInstructions] == tDirections.up then
- turtle.down()
- table.remove(tInstructions, #tInstructions)
- elseif tInstructions[#tInstructions] == tDirections.down then
- turtle.up()
- table.remove(tInstructions, #tInstructions)
- end
- end
- function turnLeft(recording)
- turtle.turnLeft()
- if recording then
- table.insert(tInstructions, tDirections.left)
- end
- iBearing = iBearing - 1
- normaliseBearing()
- end
- function turnRight(recording)
- turtle.turnRight()
- if recording then
- table.insert(tInstructions, tDirections.right)
- end
- iBearing = iBearing + 1
- normaliseBearing()
- end
- function moveDown(recording)
- if turtle.down() and recording then
- table.insert(tInstructions, tDirections.down)
- end
- tlocalCoordinates.z = tlocalCoordinates.z - 1
- end
- function moveUp(recording)
- if turtle.up() and recording then
- table.insert(tInstructions, tDirections.up)
- end
- tlocalCoordinates.z = tlocalCoordinates.z + 1
- end
- function moveForward(recording)
- if turtle.forward() and recording then
- table.insert(tInstructions, tDirections.forward)
- end
- if iBearing == 1 then
- tlocalCoordinates.x = tlocalCoordinates.x + 1
- elseif iBearing == 2 then
- tlocalCoordinates.y = tlocalCoordinates.y + 1
- elseif iBearing == 3 then
- tlocalCoordinates.x = tlocalCoordinates.x - 1
- elseif iBearing == 4 then
- tlocalCoordinates.y = tlocalCoordinates.y - 1
- end
- end
- function moveBack(recording)
- if turtle.back() and recording then
- table.insert(tInstructions, tDirections.back)
- end
- if iBearing == 1 then
- tlocalCoordinates.x = tlocalCoordinates.x - 1
- elseif iBearing == 2 then
- tlocalCoordinates.y = tlocalCoordinates.y - 1
- elseif iBearing == 3 then
- tlocalCoordinates.x = tlocalCoordinates.x + 1
- elseif iBearing == 4 then
- tlocalCoordinates.y = tlocalCoordinates.y + 1
- end
- end
- function setRedstone(recording)
- rs.setOutput("top",true)
- sleep(1)
- rs.setOutput("top",false)
- sleep(1.2)
- table.insert(tInstructions, tDirections.redstone)
- end
- function normaliseBearing()
- if iBearing < 1 then
- iBearing = iBearing + 4
- elseif iBearing > 4 then
- iBearing = iBearing - 4
- end
- end
- function writeDirections()
- local hWrite = fs.open(filename, "w")
- for i = 1, #tInstructions do
- hWrite.write(tInstructions[i].."\n")
- end
- hWrite.close()
- end
- function readDirections()
- local hRead = assert(fs.open(filename, "r")) --Will crash the program if the file cannot be opened
- while true do
- local temp = hRead.readLine()
- table.insert(tInstructions, temp)
- if temp == nil then
- break
- end
- end
- end
- function main()
- if tArgs[1] == "record" then
- repeat
- term.clear()
- term.setCursorPos(1,1)
- print("Movement Controls")
- print(" W O")
- print(" A D L")
- print(" S R")
- print("\nRecording controls:\n")
- print(" Z - UNDO M - COMPLETE")
- print("\nLocation")
- print("x: "..tlocalCoordinates.x.." y: "..tlocalCoordinates.y.." z: "..tlocalCoordinates.z.." Bearing: "..iBearing)
- e, k = os.pullEvent("char")
- if k == "w" or key == "W" then
- moveForward(true)
- elseif k == "s" or key == "S" then
- moveBack(true)
- elseif k == "o" or key == "O" then
- moveUp(true)
- elseif k == "l" or key == "L" then
- moveDown(true)
- elseif k == "a" or k == "A" then
- turnLeft(true)
- elseif k == "d" or k == "D" then
- turnRight(true)
- elseif k == "z" or k == "Z" then
- undo()
- elseif k == "r" or k == "R" then
- setRedstone(true)
- end
- --sleep(0.8)
- until k == "m" or k == "M"
- writeDirections()
- elseif tArgs[1] == "play" then
- readDirections()
- if #tInstructions > 0 then
- i = 1
- while true do
- v = tInstructions[i]
- if v == tDirections.left then
- turnLeft(false)
- elseif v == tDirections.right then
- turnRight(false)
- elseif v == tDirections.up then
- moveUp(false)
- elseif v == tDirections.down then
- moveDown(false)
- elseif v == tDirections.forward then
- moveForward(false)
- elseif v == tDirections.back then
- moveBack(false)
- elseif v == tDirections.redstone then
- setRedstone(false)
- end
- i = i + 1
- if i > #tInstructions then
- i = 1
- end
- --sleep(0.8)
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement