MegiRicky

Y-20Pocket

Oct 23rd, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. local modem = peripheral.find("modem") or error("No modem attached", 0)
  2.  
  3. print("Input the controlChannel, default = 1200")
  4. local controlChannel = io.read()
  5. if controlChannel == "" then
  6. controlChannel = 1200
  7. end
  8. controlChannel = tonumber(controlChannel)
  9. modem.open(controlChannel) -- Open a channel to communicate
  10.  
  11. print("Remote control started. Use W S for pitch, a d for roll, e(right) q(left) for yaw, Space for cannon, shift and ctrl for throttle,Tab to switch cannon control mode.")
  12.  
  13. local controls = {
  14. pitchDown = false,
  15. pitchUp = false,
  16. rollLeft = false,
  17. rollRight = false,
  18. yawRight = false,
  19. yawLeft = false,
  20. throttleUp = false,
  21. throttleDown = false,
  22. fireCannon = false,
  23. cannonControlMode = "manual",
  24. hoverMode = false,
  25. cannonUp = false,
  26. cannonDown = false,
  27. cannonLeft = false,
  28. cannonRight = false
  29. }
  30.  
  31. local keyMap = {
  32. w = "pitchDown",
  33. s = "pitchUp",
  34. a = "rollLeft",
  35. d = "rollRight",
  36. e = "yawRight",
  37. q = "yawLeft",
  38. space = "fireCannon",
  39. capsLock = "hoverModeSwitch",
  40. leftShift = "throttleUp",
  41. leftCtrl = "throttleDown",
  42. tab = "switchMode",
  43. up = "cannonUp",
  44. down = "cannonDown",
  45. left = "cannonLeft",
  46. right = "cannonRight"
  47. }
  48.  
  49. -- Function to send the current controls state
  50. local function sendControls()
  51. modem.transmit(controlChannel, controlChannel, controls)
  52. end
  53.  
  54. while true do
  55. local event, param1, param2 = os.pullEvent()
  56.  
  57. if event == "key" then
  58. -- Key press event
  59. for k, control in pairs(keyMap) do
  60. if param1 == keys[k] then
  61. if control == "switchMode" then
  62. -- Switch cannon control mode when Tab is pressed
  63. if controls.cannonControlMode == "manual" then
  64. controls.cannonControlMode = "mouseAim"
  65. print("Switched to Mouse Aim mode")
  66. else
  67. controls.cannonControlMode = "manual"
  68. print("Switched to Manual Control mode")
  69. end
  70. elseif control == "hoverModeSwitch" then
  71. if controls.hoverMode then
  72. controls.hoverMode = false
  73. print("Switched to manual mode")
  74. else
  75. controls.hoverMode = true
  76. print("Switched to automatic hover mode")
  77. end
  78. else
  79. controls[control] = true
  80. end
  81. sendControls()
  82.  
  83. print("Key pressed:", k, control)
  84. end
  85. end
  86.  
  87. elseif event == "key_up" then
  88. -- Key release event
  89. for k, control in pairs(keyMap) do
  90. if param1 == keys[k] then
  91. if control ~= "switchMode" then
  92. controls[control] = false
  93. sendControls()
  94. print("Key released:", k, control)
  95. end
  96. end
  97. end
  98. end
  99. end
  100.  
Advertisement
Add Comment
Please, Sign In to add comment