Advertisement
VGToolBox

Turtle Movement Recorder

Oct 3rd, 2012
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.28 KB | None | 0 0
  1. version = "0.1"
  2.  
  3. filename = "directions.loc"
  4.  
  5. local iBearing = 1
  6. local tlocalCoordinates = {x = 0, y = 0, z = 0}
  7.  
  8. local tInstructions = {}
  9.  
  10. local tDirections = {left = "left", right = "right", up = "up", down = "down", forward = "forward", back = "back", redstone = "redstone"}
  11.  
  12. local tArgs = {...}
  13.  
  14. local function printUsage()
  15.     print("Usage:")
  16.     print("movement record")
  17.     print("movement play")
  18. end
  19.  
  20. if #tArgs < 1 then
  21.     printUsage()
  22.     return
  23. end
  24.  
  25. function undo()
  26.  
  27.     if tInstructions[#tInstructions] == tDirections.left then
  28.         turtle.turnRight()
  29.         table.remove(tInstructions, #tInstructions)
  30.     elseif tInstructions[#tInstructions] == tDirections.right then
  31.         turtle.turnLeft()
  32.         table.remove(tInstructions, #tInstructions)
  33.     elseif tInstructions[#tInstructions] == tDirections.forward then
  34.         turtle.back()
  35.         table.remove(tInstructions, #tInstructions)
  36.     elseif tInstructions[#tInstructions] == tDirections.back then
  37.         turtle.forward()
  38.         table.remove(tInstructions, #tInstructions)
  39.     elseif tInstructions[#tInstructions] == tDirections.up then
  40.         turtle.down()
  41.         table.remove(tInstructions, #tInstructions)
  42.     elseif tInstructions[#tInstructions] == tDirections.down then
  43.         turtle.up()
  44.         table.remove(tInstructions, #tInstructions)
  45.     end
  46.  
  47. end
  48.  
  49. function turnLeft(recording)
  50.     turtle.turnLeft()
  51.     if recording then
  52.         table.insert(tInstructions, tDirections.left)
  53.     end
  54.     iBearing = iBearing - 1
  55.     normaliseBearing()
  56. end
  57.  
  58. function turnRight(recording)
  59.     turtle.turnRight()
  60.     if recording then
  61.         table.insert(tInstructions, tDirections.right)
  62.     end
  63.     iBearing = iBearing + 1
  64.     normaliseBearing()
  65. end
  66.  
  67. function moveDown(recording)
  68.     if turtle.down() and recording then
  69.         table.insert(tInstructions, tDirections.down)
  70.     end
  71.     tlocalCoordinates.z = tlocalCoordinates.z - 1
  72. end
  73.  
  74. function moveUp(recording)
  75.     if turtle.up() and recording then
  76.         table.insert(tInstructions, tDirections.up)
  77.     end
  78.     tlocalCoordinates.z = tlocalCoordinates.z + 1
  79. end
  80.  
  81. function moveForward(recording)
  82.     if turtle.forward() and recording then
  83.         table.insert(tInstructions, tDirections.forward)
  84.     end
  85.     if iBearing == 1 then
  86.         tlocalCoordinates.x = tlocalCoordinates.x + 1
  87.     elseif iBearing == 2 then
  88.         tlocalCoordinates.y = tlocalCoordinates.y + 1
  89.     elseif iBearing == 3 then
  90.         tlocalCoordinates.x = tlocalCoordinates.x - 1
  91.     elseif iBearing == 4 then
  92.         tlocalCoordinates.y = tlocalCoordinates.y - 1
  93.     end
  94. end
  95.  
  96. function moveBack(recording)
  97.     if turtle.back() and recording then
  98.         table.insert(tInstructions, tDirections.back)
  99.     end
  100.     if iBearing == 1 then
  101.         tlocalCoordinates.x = tlocalCoordinates.x - 1
  102.     elseif iBearing == 2 then
  103.         tlocalCoordinates.y = tlocalCoordinates.y - 1
  104.     elseif iBearing == 3 then
  105.         tlocalCoordinates.x = tlocalCoordinates.x + 1
  106.     elseif iBearing == 4 then
  107.         tlocalCoordinates.y = tlocalCoordinates.y + 1
  108.     end
  109. end
  110.  
  111. function setRedstone(recording)
  112.     rs.setOutput("top",true)
  113.     sleep(1)
  114.     rs.setOutput("top",false)
  115.     sleep(1.2)
  116.     table.insert(tInstructions, tDirections.redstone)
  117. end
  118.  
  119.  
  120.  
  121. function normaliseBearing()
  122.     if iBearing < 1 then
  123.         iBearing = iBearing + 4
  124.     elseif iBearing > 4 then
  125.         iBearing = iBearing - 4
  126.     end
  127. end
  128.  
  129. function writeDirections()
  130.  
  131.     local hWrite = fs.open(filename, "w")
  132.     for i = 1, #tInstructions do
  133.         hWrite.write(tInstructions[i].."\n")
  134.     end
  135.     hWrite.close()
  136. end
  137.  
  138. function readDirections()
  139.     local hRead = assert(fs.open(filename, "r")) --Will crash the program if the file cannot be opened
  140.     while true do
  141.         local temp = hRead.readLine()
  142.         table.insert(tInstructions, temp)
  143.         if temp == nil then
  144.             break
  145.         end
  146.     end
  147. end
  148.  
  149.  
  150. function main()
  151.  
  152.     if tArgs[1] == "record" then
  153.  
  154.         repeat
  155.  
  156.             term.clear()
  157.             term.setCursorPos(1,1)
  158.             print("Movement Controls")
  159.             print("        W               O")
  160.             print("      A   D             L")
  161.             print("        S               R")
  162.             print("\nRecording controls:\n")
  163.             print("  Z - UNDO   M - COMPLETE")
  164.             print("\nLocation")
  165.             print("x: "..tlocalCoordinates.x.." y: "..tlocalCoordinates.y.." z: "..tlocalCoordinates.z.."  Bearing: "..iBearing)
  166.  
  167.             e, k = os.pullEvent("char")
  168.  
  169.             if k == "w" or key == "W" then
  170.                 moveForward(true)
  171.             elseif k == "s" or key == "S" then
  172.                 moveBack(true)
  173.             elseif k == "o" or key == "O" then
  174.                 moveUp(true)
  175.             elseif k == "l" or key == "L" then
  176.                 moveDown(true)
  177.             elseif k == "a" or k == "A" then
  178.                 turnLeft(true)
  179.             elseif k == "d" or k == "D" then
  180.                 turnRight(true)
  181.             elseif k == "z" or k == "Z" then
  182.                 undo()
  183.             elseif k == "r" or k == "R" then
  184.                 setRedstone(true)
  185.             end
  186.  
  187.             --sleep(0.8)
  188.  
  189.         until k == "m" or k == "M"
  190.  
  191.             writeDirections()
  192.  
  193.     elseif tArgs[1] == "play" then
  194.         readDirections()
  195.         if #tInstructions > 0 then
  196.             i = 1
  197.             while true do
  198.                 v = tInstructions[i]
  199.  
  200.                 if v == tDirections.left then
  201.                     turnLeft(false)
  202.                 elseif v == tDirections.right then
  203.                     turnRight(false)
  204.                 elseif v == tDirections.up then
  205.                     moveUp(false)
  206.                 elseif v == tDirections.down then
  207.                     moveDown(false)
  208.                 elseif v == tDirections.forward then
  209.                     moveForward(false)
  210.                 elseif v == tDirections.back then
  211.                     moveBack(false)
  212.                 elseif v == tDirections.redstone then
  213.                     setRedstone(false)
  214.                 end
  215.  
  216.                 i = i + 1
  217.                 if i > #tInstructions then
  218.                     i = 1
  219.                 end
  220.  
  221.                 --sleep(0.8)
  222.  
  223.             end
  224.         end
  225.     end
  226. end
  227.  
  228. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement