caucow

ComputerCraft Advanced Turtles Basic Movement Generator

Nov 25th, 2015 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.63 KB | None | 0 0
  1. local instructions = {}
  2. local index = 1
  3. local scroll = 0
  4. local saved = true
  5.  
  6. local function pause()
  7.   print("Press a key or click to continue.")
  8.   while true do
  9.     name = os.pullEvent()
  10.     if name == "key" or name == "mouse_click" then
  11.       break
  12.     end
  13.   end
  14. end
  15.  
  16. local function hideCursor()
  17.   term.setCursorPos(24, index + 1 - scroll)
  18.   term.write(" ")
  19. end
  20.  
  21. local function showCursor()
  22.   term.setCursorPos(24, index + 1 - scroll)
  23.   term.write(">")
  24. end
  25.  
  26. local function renderControls()
  27.   term.setCursorPos(1, 1)
  28.   print("+---------+-------+---+---------------+")
  29.   print("|for i=1,x|while 1|end|               |")
  30.   print("+---------+-------+---+               |")
  31.   print("| UP      |PLACE UP   |               |")
  32.   print("| DOWN    |PLACE DOWN |               |")
  33.   print("| FORWARD |PLACE FRONT|               |")
  34.   print("| BACK    |DIG UP     |               |")
  35.   print("| LEFT    |DIG DOWN   |               |")
  36.   print("| RIGHT   |DIG FRONT  |               |")
  37.   print("+---------+-----------+               |")
  38.   print("|[^][DEL][CLEAR][EXIT]|               |")
  39.   print("|[v][COMP][LOAD][SAVE]|               |")
  40.   term.write("+---------------------+---------------+")
  41. end
  42.  
  43. local function renderInstructions()
  44.   length = table.getn(instructions)
  45.   for i = 1, 11 do
  46.     term.setCursorPos(26, i + 1)
  47.     term.write("             ")
  48.     if i + scroll <= length then
  49.       term.setCursorPos(26, i + 1)
  50.       term.write(instructions[i + scroll])
  51.     end
  52.   end
  53.   term.setCursorPos(29, 1)
  54.   term.write("-----")
  55.   term.setCursorPos(29, 13)
  56.   term.write("-----")
  57.   if scroll > 0 then
  58.     term.setCursorPos(29, 1)
  59.     term.write("  ^  ")
  60.   end
  61.   if length - 11 > scroll then
  62.     term.setCursorPos(29, 13)
  63.     term.write("  v  ")
  64.   end
  65. end
  66.  
  67. local function renderAll()
  68.   renderControls()
  69.   renderInstructions()
  70.   showCursor()
  71. end
  72.  
  73. local function update()
  74.   len = table.getn(instructions)
  75.   if len + 1 < index then
  76.     index = len + 1
  77.   end
  78.   if index < 1 then
  79.     index = 1
  80.   end
  81.   if index - 1 < scroll then
  82.     scroll = index - 1
  83.   end
  84.   if index - scroll > 11 then
  85.     scroll = index - 11
  86.   end
  87. end
  88.  
  89. local function insert(inst)
  90.   length = table.getn(instructions)
  91.   if index == length then
  92.     table.insert(instructions, inst)
  93.   else
  94.     table.insert(instructions, index, inst)
  95.   end
  96.   hideCursor()
  97.   index = index + 1
  98.   update()
  99.   renderInstructions()
  100.   showCursor()
  101. end
  102.  
  103. local function del()
  104.   length = table.getn(instructions)
  105.   if index > length then
  106.     table.remove(instructions, length)
  107.     index = length + 1
  108.   else
  109.     table.remove(instructions, index)
  110.   end
  111.   hideCursor()
  112.   update()
  113.   renderInstructions()
  114.   showCursor()
  115. end
  116.  
  117. local function getFileName()
  118.   term.setCursorPos(1, 3)
  119.   print("                                       ")
  120.   print("=======================================")
  121.   print("   Type name of file and press ENTER   ")
  122.   print("---------------------------------------")
  123.   print("                                       ")
  124.   print("                                       ")
  125.   print("           Type a blank line to cancel ")
  126.   print("=======================================")
  127.   print("                                       ")
  128.   term.setCursorPos(1, 7)
  129.   return read()
  130. end
  131.  
  132. local function confirm(message)
  133.   term.clear()
  134.   term.setCursorPos(1, 1)
  135.   print(message)
  136.   print("")
  137.   print("+-----------------+  +----------------+")
  138.   print("|                 |  |                |")
  139.   print("|                 |  |                |")
  140.   print("|                 |  |                |")
  141.   print("|                 |  |                |")
  142.   print("|       YES       |  |       NO       |")
  143.   print("|                 |  |                |")
  144.   print("|                 |  |                |")
  145.   print("|                 |  |                |")
  146.   print("|                 |  |                |")
  147.   term.write("+-----------------+  +----------------+")
  148.   while true do
  149.     name, button, x, y = os.pullEvent("mouse_click")
  150.     if y > 2 then
  151.       if x < 20 then
  152.         term.clear()
  153.         return true
  154.       elseif x > 21 then
  155.         term.clear()
  156.         return false
  157.       end
  158.     end
  159.   end
  160. end
  161.  
  162. local function b_while()
  163.   insert("while true")
  164.   saved = false
  165. end
  166.  
  167. local function getFor()
  168.   term.setCursorPos(1, 3)
  169.   print("                                       ")
  170.   print("=======================================")
  171.   print("    Type loop limit and press ENTER    ")
  172.   print("---------------------------------------")
  173.   print("                                       ")
  174.   print("                                       ")
  175.   print(" 1 < X < 100000       Type 0 to cancel ")
  176.   print("=======================================")
  177.   print("                                       ")
  178.   term.setCursorPos(1, 7)
  179.   num = tonumber(read())
  180.   if num == nil or num == 0 then
  181.     return nil
  182.   end
  183.   if num > 99999 then
  184.     num = 99999
  185.   end
  186.   if num < 2 then
  187.     num = 2
  188.   end
  189.   return num
  190. end
  191.  
  192. local function b_for()
  193.   num = getFor()
  194.   if not (num == nil) then
  195.     insert("for i=1," .. num)
  196.   end
  197.   term.clear()
  198.   renderAll()
  199.   saved = false
  200. end
  201.  
  202. local function b_end()
  203.   insert("end")
  204. end
  205.  
  206. local function b_mUp()
  207.   insert("MOVE_UP")
  208.   saved = false
  209. end
  210.  
  211. local function b_mDown()
  212.   insert("MOVE_DOWN")
  213.   saved = false
  214. end
  215.  
  216. local function b_mForward()
  217.   insert("MOVE_FORWARD")
  218.   saved = false
  219. end
  220.  
  221. local function b_mBack()
  222.   insert("MOVE_BACK")
  223.   saved = false
  224. end
  225.  
  226. local function b_mLeft()
  227.   insert("TURN_LEFT")
  228.   saved = false
  229. end
  230.  
  231. local function b_mRight()
  232.   insert("TURN_RIGHT")
  233.   saved = false
  234. end
  235.  
  236. local function b_pUp()
  237.   insert("PLACE_UP")
  238.   saved = false
  239. end
  240.  
  241. local function b_pDown()
  242.   insert("PLACE_DOWN")
  243.   saved = false
  244. end
  245.  
  246. local function b_pFront()
  247.   insert("PLACE_FRONT")
  248.   saved = false
  249. end
  250.  
  251. local function b_dUp()
  252.   insert("DIG_UP")
  253.   saved = false
  254. end
  255.  
  256. local function b_dDown()
  257.   insert("DIG_DOWN")
  258.   saved = false
  259. end
  260.  
  261. local function b_dFront()
  262.   insert("DIG_FRONT")
  263.   saved = false
  264. end
  265.  
  266. local function b_cursorUp()
  267.   hideCursor()
  268.   index = index - 1
  269.   update()
  270.   renderInstructions()
  271.   showCursor()
  272. end
  273.  
  274. local function b_cursorDown()
  275.   hideCursor()
  276.   index = index + 1
  277.   update()
  278.   renderInstructions()
  279.   showCursor()
  280. end
  281.  
  282. local function b_del()
  283.   del()
  284.   saved = false
  285. end
  286.  
  287. local function b_clear()
  288.   if confirm("Clear the instruction list?") then
  289.     instructions = {}
  290.     index = 1
  291.     scroll = 0
  292.     saved = true
  293.   end
  294.   renderAll()
  295. end
  296.  
  297. local function b_compile()
  298.   name = getFileName()
  299.   if name == "" then
  300.     renderAll()
  301.     return
  302.   end
  303.   term.clear()
  304.   term.setCursorPos(1, 1)
  305.   print("Compiling to " .. name)
  306.   file = fs.open(name, "w")
  307.   movement = {movement = false, up = false, down = false, forward = false, back = false, left = false, right = false}
  308.   place = {up = false, down = false, front = false}
  309.   print("Scanning for movement...")
  310.   for i = table.getn(instructions), 1, -1 do
  311.     sub1 = string.sub(instructions[i], 1, 4)
  312.     sub2 = nil
  313.     if sub1 == "MOVE" then
  314.       movement.movement = true
  315.       sub2 = string.sub(instructions[i], 6, 6)
  316.       if sub2 == "U" then
  317.         movement.up = true
  318.       elseif sub2 == "D" then
  319.         movement.down = true
  320.       elseif sub2 == "F" then
  321.         movement.forward = true
  322.       elseif sub2 == "B" then
  323.         movement.back = true
  324.       end
  325.     end
  326.     if sub1 == "TURN" then
  327.       movement.movement = true
  328.       sub2 = string.sub(instructions[i], 6, 6)
  329.       if sub2 == "L" then
  330.         movement.left = true
  331.       elseif sub2 == "R" then
  332.         movement.right = true
  333.       end
  334.     end
  335.   end
  336.   print("Scanning for block placing...")
  337.   for i = table.getn(instructions), 1, -1 do
  338.     sub1 = string.sub(instructions[i], 1, 5)
  339.     sub2 = nil
  340.     if sub1 == "PLACE" then
  341.       sub2 = string.sub(instructions[i], 7, 7)
  342.       if sub2 == "U" then
  343.         place.up = true
  344.       elseif sub2 == "D" then
  345.         place.down = true
  346.       elseif sub2 == "F" then
  347.         place.front = true
  348.       end
  349.     end
  350.   end
  351.   print("Generating required functions...")
  352.   if movement.movement then
  353.     file.writeLine("local function tryMove()")
  354.     file.writeLine("  while turtle.getFuelLevel() == 0 do")
  355.     file.writeLine("    for i = 1, 16 do")
  356.     file.writeLine("      turtle.select(i)")
  357.     file.writeLine("      if turtle.refuel(1) then")
  358.     file.writeLine("        break")
  359.     file.writeLine("      end")
  360.     file.writeLine("    end")
  361.     file.writeLine("  end")
  362.     file.writeLine("end")
  363.     file.writeLine("")
  364.     if movement.up then
  365.       file.writeLine("local function tryMoveUp()")
  366.       file.writeLine("  tryMove()")
  367.       file.writeLine("  turtle.up()")
  368.       file.writeLine("end")
  369.       file.writeLine("")
  370.     end
  371.     if movement.down then
  372.       file.writeLine("local function tryMoveDown()")
  373.       file.writeLine("  tryMove()")
  374.       file.writeLine("  turtle.down()")
  375.       file.writeLine("end")
  376.       file.writeLine("")
  377.     end
  378.     if movement.forward then
  379.       file.writeLine("local function tryMoveForward()")
  380.       file.writeLine("  tryMove()")
  381.       file.writeLine("  turtle.forward()")
  382.       file.writeLine("end")
  383.       file.writeLine("")
  384.     end
  385.     if movement.back then
  386.       file.writeLine("local function tryMoveBack()")
  387.       file.writeLine("  tryMove()")
  388.       file.writeLine("  turtle.back()")
  389.       file.writeLine("end")
  390.       file.writeLine("")
  391.     end
  392.     if movement.left then
  393.       file.writeLine("local function tryMoveLeft()")
  394.       file.writeLine("  turtle.turnLeft()")
  395.       file.writeLine("end")
  396.       file.writeLine("")
  397.     end
  398.     if movement.right then
  399.       file.writeLine("local function tryMoveRight()")
  400.       file.writeLine("  turtle.turnRight()")
  401.       file.writeLine("end")
  402.       file.writeLine("")
  403.     end
  404.   end
  405.   if place.up then
  406.     file.writeLine("local function tryPlaceUp()")
  407.     file.writeLine("  while not turtle.placeUp() do")
  408.     file.writeLine("    local i = turtle.getSelectedSlot()")
  409.     file.writeLine("    i = i + 1")
  410.     file.writeLine("    if i > 16 then")
  411.     file.writeLine("      i = 1")
  412.     file.writeLine("    end")
  413.     file.writeLine("    turtle.select(i)")
  414.     file.writeLine("  end")
  415.     file.writeLine("end")
  416.     file.writeLine("")
  417.   end
  418.   if place.down then
  419.     file.writeLine("local function tryPlaceDown()")
  420.     file.writeLine("  while not turtle.placeDown() do")
  421.     file.writeLine("    local i = turtle.getSelectedSlot()")
  422.     file.writeLine("    i = i + 1")
  423.     file.writeLine("    if i > 16 then")
  424.     file.writeLine("      i = 1")
  425.     file.writeLine("    end")
  426.     file.writeLine("    turtle.select(i)")
  427.     file.writeLine("  end")
  428.     file.writeLine("end")
  429.     file.writeLine("")
  430.   end
  431.   if place.front then
  432.     file.writeLine("local function tryPlace()")
  433.     file.writeLine("  while not turtle.place() do")
  434.     file.writeLine("    local i = turtle.getSelectedSlot()")
  435.     file.writeLine("    i = i + 1")
  436.     file.writeLine("    if i > 16 then")
  437.     file.writeLine("      i = 1")
  438.     file.writeLine("    end")
  439.     file.writeLine("    turtle.select(i)")
  440.     file.writeLine("  end")
  441.     file.writeLine("end")
  442.     file.writeLine("")
  443.   end
  444.   print("Parsing instruction list...")
  445.   indent = 0
  446.   length = table.getn(instructions)
  447.   mapping = {MOVE_UP = "tryMoveUp()", MOVE_DOWN = "tryMoveDown()", MOVE_FORWARD = "tryMoveForward()", MOVE_BACK = "tryMoveBack()", TURN_LEFT = "tryMoveLeft()", TURN_RIGHT = "tryMoveRight()", PLACE_UP = "tryPlaceUp()", PLACE_DOWN = "tryPlaceDown()", PLACE_FRONT = "tryPlace()", DIG_UP = "turtle.digUp()", DIG_DOWN = "turtle.digDown()", DIG_FRONT = "turtle.dig()"}
  448.   varnames = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  449.   mapped = nil
  450.   for i = 1, length do
  451.     mapped = mapping[instructions[i]]
  452.     if mapped == nil then
  453.       if instructions[i] == "while true" then
  454.         file.writeLine(string.format("%" .. indent .. "s%s", "", "while true do"))
  455.         indent = indent + 2
  456.       elseif instructions[i] == "end" then
  457.         indent = indent - 2
  458.         if indent < 0 then
  459.           break
  460.         end
  461.         file.writeLine(string.format("%" .. indent .. "s%s", "", "end"))
  462.       else
  463.         indent = indent + 2
  464.         file.writeLine(string.format("%" .. indent .. "s%s", "", "for " .. varnames[indent / 2] .. " = 1, " .. string.sub(instructions[i], 9) .. " do"))
  465.       end
  466.     else
  467.       file.writeLine(string.format("%" .. indent .. "s%s", "", mapped))
  468.     end
  469.   end
  470.   file.close()
  471.   if indent > 0 then
  472.     print("Error: Unclosed loop.")
  473.   elseif indent < 0 then
  474.     print("Error: end without loop.")
  475.   else
  476.     print("Finished compiling without errors.")
  477.   end
  478.   pause()
  479.   renderAll()
  480. end
  481.  
  482. local function b_load()
  483.   name = getFileName()
  484.   if name == "" then
  485.     renderAll()
  486.     return
  487.   end
  488.   term.clear()
  489.   term.setCursorPos(1, 1)
  490.   print("Loading " .. name .. "...")
  491.   if not fs.exists(name) then
  492.     print("Error: File " .. name .. " does not exist!")
  493.     pause()
  494.     renderAll()
  495.     return
  496.   end
  497.   file = fs.open(name, "r")
  498.   instructions = {}
  499.   line = file.readLine()
  500.   i = 0
  501.  
  502.   while not (line == nil) do
  503.     i = i + 1
  504.     instructions[i] = line
  505.     line = file.readLine()
  506.   end
  507.   file.close()
  508.   print("Loaded.")
  509.   pause()
  510.   renderAll()
  511.   saved = true
  512. end
  513.  
  514. local function b_save()
  515.   name = getFileName()
  516.   if name == "" then
  517.     renderAll()
  518.     return
  519.   end
  520.   term.clear()
  521.   term.setCursorPos(1, 1)
  522.   print("Saving to " .. name .. "...")
  523.   file = fs.open(name, "w")
  524.   length = table.getn(instructions)
  525.   for i = 1, length do
  526.     file.writeLine(instructions[i])
  527.   end
  528.   file.close()
  529.   print("File saved.")
  530.   pause()
  531.   renderAll()
  532.   saved = true
  533. end
  534.  
  535. local function b_exit()
  536.   if confirm("Are you sure you want to exit?") then
  537.     if not saved and confirm("Save instruction list before quitting?") then
  538.       b_save()
  539.     end
  540.     return false
  541.   end
  542.   renderAll()
  543.   return true
  544. end
  545.  
  546. local function handleEvents()
  547.   name, button, x, y = os.pullEvent("mouse_click")
  548.   if y == 2 then
  549.     if x >= 2 and x <= 10 then
  550.       b_for()
  551.     elseif x >= 12 and x <= 18 then
  552.       b_while()
  553.     elseif x >= 20 and x <= 22 then
  554.       b_end()
  555.     end
  556.   elseif y == 4 then
  557.     if x >= 2 and x <= 10 then
  558.       b_mUp()
  559.     elseif x >= 12 and x <= 22 then
  560.       b_pUp()
  561.     end
  562.   elseif y == 5 then
  563.     if x >= 2 and x <= 10 then
  564.       b_mDown()
  565.     elseif x >= 12 and x <= 22 then
  566.       b_pDown()
  567.     end
  568.   elseif y == 6 then
  569.     if x >= 2 and x <= 10 then
  570.       b_mForward()
  571.     elseif x >= 12 and x <= 22 then
  572.       b_pFront()
  573.     end
  574.   elseif y == 7 then
  575.     if x >= 2 and x <= 10 then
  576.       b_mBack()
  577.     elseif x >= 12 and x <= 22 then
  578.       b_dUp()
  579.     end
  580.   elseif y == 8 then
  581.     if x >= 2 and x <= 10 then
  582.       b_mLeft()
  583.     elseif x >= 12 and x <= 22 then
  584.       b_dDown()
  585.     end
  586.   elseif y == 9 then
  587.     if x >= 2 and x <= 10 then
  588.       b_mRight()
  589.     elseif x >= 12 and x <= 22 then
  590.       b_dFront()
  591.     end
  592.   elseif y == 11 then
  593.     if x >= 2 and x <= 4 then
  594.       b_cursorUp()
  595.     elseif x >= 5 and x <= 9 then
  596.       b_del()
  597.     elseif x >= 10 and x <= 16 then
  598.       b_clear()
  599.     elseif x >= 17 and x <= 22 then
  600.       return b_exit()
  601.     end
  602.   elseif y == 12 then
  603.     if x >= 2 and x <= 4 then
  604.       b_cursorDown()
  605.     elseif x >= 5 and x <= 10 then
  606.       b_compile()
  607.     elseif x >= 11 and x <= 16 then
  608.       b_load()
  609.     elseif x >= 17 and x <= 22 then
  610.       b_save()
  611.     end
  612.   end
  613.   return true
  614. end
  615.  
  616. term.setCursorBlink(false)
  617. renderAll()
  618. while handleEvents() do
  619. end
  620. term.clear()
  621. term.setCursorPos(1, 1)
Add Comment
Please, Sign In to add comment