MegiRicky

VerticesMissilePocket

Oct 13th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. local modem = peripheral.find("modem") or error("No modem attached", 0)
  2.  
  3. print("Input the controlChannel, default = 500")
  4. local controlChannel = io.read()
  5. if controlChannel == "" then
  6. controlChannel = 500
  7. end
  8. controlChannel = tonumber(controlChannel)
  9. modem.open(controlChannel) -- Open a channel to communicate
  10.  
  11. print("Remote control started. Use WASD keys to control movement, e(up) q(down) for suspension, Space for cannon, Shift for autocannon, T for smoke grenades, Tab to switch cannon control mode.")
  12.  
  13. local controls = {
  14. accelerate = false,
  15. decelerate = false,
  16. turnLeft = false,
  17. turnRight = false,
  18. suspensionUp = false,
  19. suspensionDown = false,
  20. fireCannon = false,
  21. fireMissile = {},
  22. launchSmokeGrenade = false,
  23. cannonControlMode = "manual",
  24. cannonUp = false,
  25. cannonDown = false,
  26. cannonLeft = false,
  27. cannonRight = false,
  28. }
  29.  
  30. local keyMap = {
  31. w = "accelerate",
  32. s = "decelerate",
  33. a = "turnLeft",
  34. d = "turnRight",
  35. e = "suspensionUp",
  36. q = "suspensionDown",
  37. space = "fireCannon",
  38. leftShift = "fireMissile",
  39. t = "launchSmokeGrenade",
  40. tab = "switchMode",
  41. up = "cannonUp",
  42. down = "cannonDown",
  43. left = "cannonLeft",
  44. right = "cannonRight"
  45. }
  46.  
  47. local missileId = 1 -- Initial missile ID to be launched
  48. local missileLaunched = {} -- List to store launched missile IDs
  49.  
  50. -- Function to send the current controls state
  51. local function sendControls()
  52. modem.transmit(controlChannel, controlChannel, controls)
  53. end
  54.  
  55. -- Function to launch the next missile
  56. local function launchMissile()
  57. if type(controls.fireMissile) ~= "table" then
  58. controls.fireMissile = {}
  59. end
  60.  
  61. local currentMissileId = missileId
  62. -- Assign a table to this missile's ID in fireMissile, marking it as launched
  63. controls.fireMissile[currentMissileId] = {launch = true}
  64. missileLaunched[currentMissileId] = true -- Mark this missile as launched
  65. print("Launching missile:", currentMissileId)
  66.  
  67. missileId = missileId + 1 -- Increment missile ID for next launch
  68. end
  69.  
  70. while true do
  71. local event, param1, param2 = os.pullEvent()
  72.  
  73. if event == "key" then
  74. -- Key press event
  75. for k, control in pairs(keyMap) do
  76. if param1 == keys[k] then
  77. if control == "switchMode" then
  78. -- Switch cannon control mode when Tab is pressed
  79. if controls.cannonControlMode == "manual" then
  80. controls.cannonControlMode = "mouseAim"
  81. print("Switched to Mouse Aim mode")
  82. else
  83. controls.cannonControlMode = "manual"
  84. print("Switched to Manual Control mode")
  85. end
  86. elseif control == "fireMissile" then
  87. launchMissile()
  88. else
  89. controls[control] = true
  90. end
  91. sendControls()
  92. print("Key pressed:", k, control)
  93. end
  94. end
  95.  
  96. elseif event == "key_up" then
  97. -- Key release event
  98. for k, control in pairs(keyMap) do
  99. if param1 == keys[k] then
  100. if control ~= "switchMode" then
  101. controls[control] = false
  102. sendControls()
  103. print("Key released:", k, control)
  104. end
  105. end
  106. end
  107. end
  108. end
  109.  
Advertisement
Add Comment
Please, Sign In to add comment