Advertisement
mah17

Pilot

Apr 10th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. -- Pilot: A simple program to manipulate a turtle with the keyboard
  2. --
  3.  
  4. if term.isColor() then
  5.   HEADER_COLOR = colors.blue
  6.   HEADER_TEXT_COLOR = colors.white
  7. else
  8.   HEADER_COLOR = colors.white
  9.   HEADER_TEXT_COLOR = colors.black
  10. end
  11.  
  12. local keytable = {
  13.   ['17'] = 'f', ['200'] = 'f',
  14.   ['30'] = 'l', ['203'] = 'l',
  15.   ['31'] = 'b', ['208'] = 'b',
  16.   ['32'] = 'r', ['205'] = 'r',
  17.   ['42'] = 'd', ['54']  = 'd',
  18.   ['57'] = 'u',
  19.   ['16'] = 'q'
  20. }
  21.  
  22. function lAndRAligned(left, right)
  23.   local maxx, maxy = term.getSize()
  24.   local padding = maxx - (string.len(left) + string.len(right))
  25.   return left .. string.rep(' ', padding) .. right
  26. end
  27.  
  28. function printUsage()
  29.   term.clear()
  30.   term.setBackgroundColor(HEADER_COLOR)
  31.   term.setTextColor(HEADER_TEXT_COLOR)
  32.   term.setCursorPos(1, 1)
  33.   term.write(lAndRAligned('Pilot', '0.2-alpha'))
  34.   term.setBackgroundColor(colors.black)
  35.   term.setTextColor(colors.white)
  36.   term.setCursorPos(1, 2)
  37.   print('W or Up Arrow:    Forward')
  38.   print('A or Left Arrow:  Turn left')
  39.   print('S or Down Arrow:  Back')
  40.   print('D or Right Arrow: Turn right')
  41.   print('Space:            Go up')
  42.   print('LShift or RShift: Go down')
  43.   if term.isColor() then
  44.     print('Mouse scroll:     Change selected slot')
  45.     print('Left click:       Break/Attack front')
  46.     print('Right click:      Place selected block')
  47.   end
  48.   print('Q:                Quit')
  49. end
  50.  
  51. printUsage()
  52. while true do
  53.   local e, v1, v2, v3 = os.pullEvent()
  54.  
  55.   if e == 'key' then
  56.     local dir = keytable[tostring(v1)]
  57.  
  58.     if     dir == 'f' then turtle.forward()
  59.     elseif dir == 'l' then turtle.turnLeft()
  60.     elseif dir == 'b' then turtle.back()
  61.     elseif dir == 'r' then turtle.turnRight()
  62.     elseif dir == 'u' then turtle.up()
  63.     elseif dir == 'd' then turtle.down()
  64.     elseif dir == 'q' then
  65.       os.queueEvent('tmp')
  66.       os.pullEvent() --Clear leftover events
  67.       break
  68.     end
  69.   elseif e == 'mouse_scroll' then
  70.     if     turtle.getSelectedSlot() + v1 == 0 then v1 = v1 + 16
  71.     elseif turtle.getSelectedSlot() + v1 == 17 then v1 = v1 - 16
  72.     end
  73.     turtle.select(turtle.getSelectedSlot() + v1)
  74.   elseif e == 'mouse_click' then
  75.     if v1 == 1 then turtle.attack(); turtle.dig()
  76.     elseif v1 == 2 then turtle.place()
  77.     end
  78.   end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement