Advertisement
eytixis

Controller

May 15th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. -- configuration section --
  2. turtle = 2044
  3. rednet.open("back")
  4.  
  5. -- common with the pair --
  6. commands = {}
  7. commands.move = {}
  8. commands.place = {}
  9. commands.interact = {}
  10. commands.turn = {}
  11. commands.inventory = {}
  12. commands.utilities = {}
  13. commands.inventory.read = {}
  14. -- inventory commands --
  15. commands.inventory.select = "select slot"
  16. commands.inventory.read.slot = "get slot"
  17. commands.inventory.read.all = "get all items"
  18. commands.inventory.getSlot = "get slot selected"
  19. -- move commands --
  20. commands.move.forward = "move forward"
  21. commands.move.back = "move back"
  22. commands.move.up = "go up"
  23. commands.move.down = "go down"
  24. -- turn commands --
  25. commands.turn.left = "turn left"
  26. commands.turn.right = "turn right"
  27. -- interact commands --
  28. commands.interact.dig = "dig front"
  29. commands.interact.digUp = "dig up"
  30. commands.interact.digDown = "dig down"
  31. commands.interact.place = "place front"
  32. commands.interact.placeUp = "place up"
  33. commands.interact.placeDown = "place down"
  34. commands.interact.inspect = "inspect front"
  35. commands.interact.inspectUp = "inspect up"
  36. commands.interact.inspectDown = "inspect down"
  37. commands.interact.suck = "suck items"
  38. commands.interact.suckUp = "suck from up"
  39. commands.interact.suckDown = "suck from down"
  40. commands.interact.drop = "drop items"
  41. commands.interact.dropUp = "drop items up"
  42. commands.interact.dropDown = "drop items down"
  43. -- utilities commands --
  44. commands.utilities.fuel = "get fuel level"
  45. commands.utilities.refuel = "refuel from slot"
  46.  
  47.  
  48. -- keys --
  49. keys = {}
  50. keys.move = {}
  51. keys.interact = {}
  52. keys.turn = {}
  53. keys.control = {}
  54. -- move keys --
  55. keys.move.forward = 17
  56. keys.move.back = 31
  57. keys.move.up = 200
  58. keys.move.down = 208
  59. -- turn keys --
  60. keys.turn.left = 30
  61. keys.turn.right = 32
  62. -- interact keys --
  63. keys.interact.place = 75
  64. keys.interact.placeUp = 71
  65. keys.interact.placeDown = 79
  66. keys.interact.dig = 77
  67. keys.interact.digUp = 73
  68. keys.interact.digDown = 81
  69. keys.interact.inspect = 76
  70. keys.interact.inspectUp = 72
  71. keys.interact.inspectDown = 80
  72. -- control keys --
  73. keys.control.main = 19
  74. keys.control.fuel = 33
  75.  
  76.  
  77.  
  78. function termClear()
  79.     term.clear()
  80.     term.setCursorPos(1,1)
  81. end
  82.  
  83. function help()
  84.     print("Help: ")
  85.     print("help, remote, get item")
  86.     print("get inventory, clear")
  87.     commandListener()
  88. end
  89.  
  90. function remote()
  91.     event, key, d = os.pullEvent("key")
  92.     remoteKeyAct(key)
  93. end
  94.  
  95. function remoteKeyAct(key)
  96.     if key == keys.move.forward then
  97.         commandPack = {}
  98.         commandPack.command = commands.move.forward
  99.         msg = textutils.serialise(commandPack)
  100.         rednet.send(turtle, msg)
  101.     elseif key == keys.move.back then
  102.         commandPack = {}
  103.         commandPack.command = commands.move.back
  104.         msg = textutils.serialise(commandPack)
  105.         rednet.send(turtle, msg)
  106.     elseif key == keys.move.up then
  107.         commandPack = {}
  108.         commandPack.command = commands.move.up
  109.         msg = textutils.serialise(commandPack)
  110.         rednet.send(turtle, msg)
  111.     elseif key == keys.move.down then
  112.         commandPack = {}
  113.         commandPack.command = commands.move.down
  114.         msg = textutils.serialise(commandPack)
  115.         rednet.send(turtle, msg)
  116.     elseif key == keys.turn.left then
  117.         commandPack = {}
  118.         commandPack.command = commands.turn.left
  119.         msg = textutils.serialise(commandPack)
  120.         rednet.send(turtle, msg)
  121.     elseif key == keys.turn.right then
  122.         commandPack = {}
  123.         commandPack.command = commands.turn.right
  124.         msg = textutils.serialise(commandPack)
  125.         rednet.send(turtle, msg)
  126.     elseif key == keys.control.main then
  127.         startScreen()
  128.     elseif key == keys.control.fuel then
  129.         commandPack = {}
  130.         commandPack.command = commands.utilities.fuel
  131.         msg = textutils.serialise(commandPack)
  132.         rednet.send(turtle, msg)
  133.         receiveMessage()
  134.         recTab = textutils.unserialise(message)
  135.         print("Fuel Level: "..recTab.fuel)
  136.     elseif key == keys.interact.dig then
  137.         commandPack = {}
  138.         commandPack.command = commands.interact.dig
  139.         msg = textutils.serialise(commandPack)
  140.         rednet.send(turtle, msg)
  141.     elseif key == keys.interact.digUp then
  142.         commandPack = {}
  143.         commandPack.command = commands.interact.digUp
  144.         msg = textutils.serialise(commandPack)
  145.         rednet.send(turtle, msg)
  146.     elseif key == keys.interact.digDown then
  147.         commandPack = {}
  148.         commandPack.command = commands.interact.digDown
  149.         msg = textutils.serialise(commandPack)
  150.         rednet.send(turtle, msg)   
  151.     elseif key == keys.interact.place then
  152.         commandPack = {}
  153.         commandPack.command = commands.interact.place
  154.         msg = textutils.serialise(commandPack)
  155.         rednet.send(turtle, msg)
  156.     elseif key == keys.interact.placeUp then
  157.         commandPack = {}
  158.         commandPack.command = commands.interact.placeUp
  159.         msg = textutils.serialise(commandPack)
  160.         rednet.send(turtle, msg)
  161.     elseif key == keys.interact.placeDown then
  162.         commandPack = {}
  163.         commandPack.command = commands.interact.placeDown
  164.         msg = textutils.serialise(commandPack)
  165.         rednet.send(turtle, msg)   
  166.     elseif key == keys.interact.inspect then
  167.         commandPack = {}
  168.         commandPack.command = commands.interact.inspect
  169.         msg = textutils.serialise(commandPack)
  170.         rednet.send(turtle, msg)   
  171.         receiveMessage()
  172.         recTab = textutils.unserialise(message)
  173.         print("front: "..recTab.name)
  174.     elseif key == keys.interact.inspectUp then
  175.         commandPack = {}
  176.         commandPack.command = commands.interact.inspectUp
  177.         msg = textutils.serialise(commandPack)
  178.         rednet.send(turtle, msg)   
  179.         receiveMessage()
  180.         recTab = textutils.unserialise(message)
  181.         print("up: "..recTab.name)
  182.     elseif key == keys.interact.inspectDown then
  183.         commandPack = {}
  184.         commandPack.command = commands.interact.inspectDown
  185.         msg = textutils.serialise(commandPack)
  186.         rednet.send(turtle, msg)   
  187.         receiveMessage()
  188.         recTab = textutils.unserialise(message)
  189.         print("down: "..recTab.name)
  190.     end
  191.     remote()
  192. end
  193.  
  194. function getItems()
  195.     termClear()
  196.     commandPack = {}
  197.     commandPack.command = commands.inventory.read.all
  198.     msg = textutils.serialise(commandPack)
  199.     rednet.send(turtle, msg)
  200.     receiveMessage()
  201.     tab = textutils.unserialise(message)
  202.     print("turtle inventory")
  203.     for i=1,16 do
  204.         x = tab[i].name
  205.         y = tab[i].amount
  206.         print(i..": "..y.."x"..x)
  207.     end
  208.     commandListener()
  209. end
  210.  
  211. function getItem()
  212.     termClear()
  213.     print("enter slot")
  214.     slot = read()
  215.     commandPack = {}
  216.     commandPack.command = commands.inventory.read.slot
  217.     commandPack.slot = slot
  218.     msg = textutils.serialise(commandPack)
  219.     rednet.send(turtle, msg)
  220.     receiveMessage()
  221.     recTab = textutils.unserialise(message)
  222.     termClear()
  223.     print(slot..": "..recTab.amount.."x"..recTab.name)
  224.     commandListener()
  225. end
  226.  
  227. function receiveMessage()
  228.     id, msg = rednet.receive()
  229.     if id == turtle then
  230.         message = msg
  231.     else
  232.         receiveMessage()
  233.     end
  234. end
  235.  
  236. function commandListener()
  237.     command = read()
  238.     if command == "remote" then
  239.         termClear()
  240.         remote()
  241.     elseif command == "help" then
  242.         help()
  243.     elseif command == "clear" then
  244.         startScreen()
  245.     elseif command == "select" then
  246.         print("enter the number of slot")
  247.         slot = read()
  248.         commandPack = {}
  249.         commandPack.command = commands.inventory.select
  250.         commandPack.slotNum = slot
  251.         msg = textutils.serialise(commandPack)
  252.         rednet.send(turtle, msg)
  253.         commandListener()
  254.     elseif command == "get item" then
  255.         getItem()
  256.     elseif command == "get inventory" then
  257.         getItems()
  258.     elseif command  == "get slot" then
  259.         commandPack = {}
  260.         commandPack.command = commands.inventory.getSlot
  261.         x = textutils.serialise(commandPack)
  262.         rednet.send(turtle, x)
  263.         receiveMessage()
  264.         msg = textutils.unserialise(message)
  265.         print("selected slot: "..msg.slot)
  266.         commandListener()
  267.     elseif command == "suck up" then
  268.         commandPack = {}
  269.         commandPack.command = commands.interact.suckUp
  270.         x = textutils.serialise(commandPack)
  271.         rednet.send(turtle, x)
  272.     elseif command == "suck down" then
  273.         commandPack = {}
  274.         commandPack.command = commands.interact.suckDown
  275.         x = textutils.serialise(commandPack)
  276.         rednet.send(turtle, x)
  277.     elseif command == "suck" then
  278.         commandPack = {}
  279.         commandPack.command = commands.interact.suck
  280.         x = textutils.serialise(commandPack)
  281.         rednet.send(turtle, x)
  282.     elseif command == "drop up" then
  283.         commandPack = {}
  284.         commandPack.command = commands.interact.dropUp
  285.         x = textutils.serialise(commandPack)
  286.         rednet.send(turtle, x)
  287.     elseif command == "drop down" then
  288.         commandPack = {}
  289.         commandPack.command = commands.interact.dropDown
  290.         x = textutils.serialise(commandPack)
  291.         rednet.send(turtle, x)
  292.     elseif command == "drop" then
  293.         commandPack = {}
  294.         commandPack.command = commands.interact.drop
  295.         x = textutils.serialise(commandPack)
  296.         rednet.send(turtle, x)
  297.     end
  298. end
  299.  
  300. function startScreen()
  301. termClear()
  302. print("welcome to the controller")
  303. print("enter your commands")
  304. commandListener()
  305. end
  306.  
  307.  
  308.  
  309. startScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement