The3vilM0nk3y

Frame Controller

Aug 11th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1.  
  2. -- Movement Keys
  3. kForward = "w"
  4. kLeft = "a"
  5. kBack = "s"
  6. kRight = "d"
  7. kUp = "space"
  8. kDown = "leftShift"
  9. kQuit = "q"
  10. kMine = "r"
  11.  
  12. -- Maximum distance to move/mine in one command
  13. maxDistance = 50
  14. -- Cable colors
  15. --  Must be defined as colors.colorName
  16. --  See - http://computercraft.info/wiki/Colors_%28API%29  for color names
  17. up = colors.green
  18. down = colors.lightBlue
  19. left = colors.lightGray
  20. right = colors.orange
  21. forward = colors.white
  22. backward = colors.purple
  23.  
  24. -- Cable Sides
  25. aSide = "bottom"
  26. bSide = "top"
  27. power = "back"
  28.  
  29. -- Timer Settings (in seconds)
  30. chargeTime = .5
  31. mineTime = 8
  32. moveTime = .25
  33.  
  34.  
  35. -- Dont Change anything after this!
  36. w,h = term.getSize()
  37. manMine = false
  38. rs.setOutput(power,manMine)
  39. function center(s)
  40.   _,y = term.getCursorPos()
  41.   x = math.floor( (w/2) - (string.len(s)/2) )
  42.   term.setCursorPos(x,y)
  43.   print(s)
  44. end
  45.  
  46. function header()
  47.   term.clear()
  48.   term.setCursorPos(1,1)
  49.   center("####################")
  50.   center("# Funky Locomotion #")
  51.   center("#  Control  Panel  #")
  52.   center("####################")
  53.   print("")
  54. end
  55. function dotdot(str,amt)
  56.   header()
  57.   term.write(str)
  58.   if amt>=1 then
  59.     for i=1,math.ceil(amt) do
  60.       term.write(".")
  61.       sleep(1)
  62.     end
  63.   else
  64.     term.write(".")
  65.     sleep(amt)
  66.   end
  67. end
  68. function move(direction,doMine)
  69.   print("Preparing to Move")
  70.   sleep(1)
  71.   if doMine then
  72.     rs.setOutput(power,true)
  73.     rs.setBundledOutput(aSide,direction)
  74.     dotdot("Moving Part A",moveTime)
  75.     rs.setBundledOutput(aSide,0)
  76.     dotdot("Mine Cycle A",mineTime)
  77.     rs.setBundledOutput(bSide,direction)
  78.     dotdot("Moving Part B",moveTime)
  79.     rs.setBundledOutput(bSide,0)
  80.     dotdot("Mine Cycle B",mineTime)
  81.     print("\nMining Complete.")
  82.     rs.setOutput(power,false)
  83.   else
  84.     rs.setBundledOutput(aSide,direction)
  85.     dotdot("Moving Part A",moveTime)
  86.     rs.setBundledOutput(aSide,0)
  87.     dotdot("Charge Cycle",chargeTime)
  88.     rs.setBundledOutput(bSide,direction)
  89.     dotdot("Moving Part B",moveTime)
  90.     rs.setBundledOutput(bSide,0)
  91.   end
  92.   print("\nMove Complete")
  93.   sleep(1)
  94. end
  95. function instructions()
  96.   header()
  97.   if manual then
  98.     center("Manual Override Mode")
  99.     center("--------------------")
  100.     print(kForward .. " - forward")
  101.     print(kLeft .. " - left")
  102.     print(kBack .. " - back")
  103.     print(kRight .. " - right")
  104.     print(kUp .. " - up")
  105.     print(kDown .. " - down")
  106.     print(kQuit .. " - quit manual override")
  107.     print(kMine .. " - Toggle Mining Power")
  108.     print("\n\nMining Power - " .. tostring(manMine))
  109.   else
  110.     print("Commands - ")
  111.     print("  move direction amount")
  112.     print("  mine direction amount")
  113.     print("  manualoverride")
  114.     print("  exit")
  115.     print("\nValid Names: forward backward up down left right")
  116.     print("\nEnter Command")
  117.   end
  118. end
  119. function manualoverride()
  120.   doloop = true
  121.   while doloop do
  122.     instructions()
  123.     e,p1,p2,p3 = os.pullEvent()
  124.     if e == "key" then
  125.       key = keys.getName(p1)
  126.       if key == kForward and not p2 then
  127.         move(forward)
  128.       elseif key == kBack and not p2 then
  129.         move(backward)
  130.       elseif key == kLeft and not p2 then
  131.         move(left)
  132.       elseif key == kRight and not p2 then
  133.         move(right)
  134.       elseif key == kUp and not p2 then
  135.         move(up)
  136.       elseif key == kDown and not p2 then
  137.         move(down)
  138.       elseif key == kMine and not p2 then
  139.         if manMine then
  140.           manMine = false
  141.         else
  142.           manMine = true
  143.         end
  144.         rs.setOutput(power,manMine)
  145.       elseif key == kQuit then
  146.         doloop = false
  147.         print("Ending Program.")
  148.         sleep(1)
  149.       end
  150.     end
  151.   end
  152. end
  153. function checkDirection(dir)
  154.   if dir then
  155.     dir = string.lower(dir)
  156.     print("Checking " .. dir )
  157.     if dir == "forward" or dir == "backward" or dir == "left" or dir == "right" or dir == "up" or dir == "down" then
  158.       print("Match Found for direction")
  159.       return true
  160.     else
  161.       print(dir .. " is not a valid direction")
  162.     end
  163.   else
  164.     print("Please enter a valid direction")
  165.   end
  166. end
  167. function checkDirectionAmount(n)
  168.   if n then
  169.     num = tonumber(n)
  170.     if num then
  171.       if num >= 1 and num <= maxDistance then
  172.         return true
  173.       else
  174.         print("Distance must be between 1 and " .. tostring(maxDistance))
  175.       end
  176.     end
  177.   else
  178.     print("Distance Number is required")
  179.   end
  180. end
  181. function convertDirection(dir)
  182.   if dir == "forward" then
  183.     return forward
  184.   elseif dir == "backward" then
  185.     return backward
  186.   elseif dir == "left" then
  187.     return left
  188.   elseif dir == "right" then
  189.     return right
  190.   elseif dir == "up" then
  191.     return up
  192.   elseif dir == "down" then
  193.     return down
  194.   end
  195. end
  196. manual = false
  197. runController = true
  198. while runController do
  199.   instructions()
  200.   cmd_temp = io.read()
  201.   args = {}
  202.   for arg in cmd_temp:gmatch("%w+") do
  203.     table.insert(args, arg)
  204.   end
  205.   if args[1] then
  206.     cmd = string.lower(args[1])
  207.     if cmd == "move" or cmd == "mine" then
  208.       if checkDirection(args[2]) then
  209.         if checkDirectionAmount(args[3]) then
  210.           for i=1,math.floor(tonumber(args[3])) do
  211.             if cmd == "mine" then
  212.               move(convertDirection(string.lower(args[2])),true)
  213.             else
  214.               move(convertDirection(string.lower(args[2])))
  215.             end
  216.           end
  217.         end
  218.       end
  219.       sleep(1)
  220.      
  221.     elseif string.lower(args[1]) == "manualoverride" or string.lower(args[1]) == "man" then
  222.       manual = true
  223.       manualoverride()
  224.       manual = false
  225.     elseif string.lower(args[1]) == "exit" then
  226.       runController = false
  227.     else
  228.       print("Unknown Command")
  229.       sleep(1)
  230.     end
  231.   else
  232.     print("No Command Entered")
  233.   end
  234. end
Advertisement
Add Comment
Please, Sign In to add comment