Advertisement
FocusedWolf

Elite:Dangerous: Titan-Bomber Script

Apr 23rd, 2024 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.43 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. ##### What is this #####
  4.  
  5. # I made a simple python script to help with boosting away from interdiction's and bombing/rocketing the titan.
  6. # What this script does is very simple. It repeatedly presses your boost key while you are holding it down.
  7. # And if Scroll Lock is toggled on, then Right Mouse Button becomes full auto.
  8. #
  9. # If you are using an SCO FSD then i have an alternate version of the main() function for you to use.
  10. # It changes Scroll Lock to act as a global on/off key for the whole script.
  11. # Its a little more tedious to use since you need to turn the script on when using thrusters (if you want auto-boost)
  12. # and definitely off when using supercruise. I think this made SCO FSD even more annoying to run xD
  13. #
  14. # For reference here is my titan-bomber fire groups indicating what RMB is assigned to.
  15. # In firegroup C and D i use '(2)' for Nanite Torpedo Pylon and AX Missile Rack.
  16. # To prevent TG Pulse Neutraliser from discharging incorrectly prior to entering the titan cloud
  17. # keep Scroll Lock off until you are through the caustic cloud. After that you can keep Scroll Lock on.
  18. # The Nanite Torpedo Pylon will not fire until you have a lock,
  19. # and it feels like firing, so you can focus on flying + targeting with RMB held down and it will auto-click for you.
  20. # Also the AX Missile Rack is full auto now for when its time to shoot the exposed core :D
  21. #
  22. #                              A       B       C       D       E
  23. #    Beam Laser -------------------------------1-------1---------------
  24. #    AX Missile Rack ----------------------------------2---------------
  25. #    AX Missile Rack ----------------------------------2---------------
  26. #    AX Missile Rack ----------------------------------2---------------
  27. #    Nanite Torpedo Pylon ---------------------2-----------------------
  28. #    Repair Limpet --------------------------------------------1-------
  29. #    TG Pulse Neutraliser -------------2-------------------------------
  30. #    Enhanced Xeno Scanner ----2---------------------------------------
  31. #    Heatsink ---------------------------------------------------------
  32. #    Caustic Sink Launcher ------------1-------------------------------
  33. #    Data Link Scanner --------2---------------------------------------
  34. #    Comp. Scanner ------------2---------------------------------------
  35. #    D-Scanner ----------------1---------------------------------------
  36.  
  37. ##### Installation #####
  38. # To run this script you need Python installed. After that run cmd as administrator > [pip install pynput] because that library is also needed.
  39. # Save this script as a .py file, e.g. "Titan Script.py" and double click the file to run it.
  40. # Also, unless you are also using Caps Lock as a boost key then you'll probably want to mod this line "boost_button = Key.caps_lock".
  41.  
  42. ##### How to use this script when titan bombing #####
  43. # 1. When you jump into a titan system make sure Scroll Lock is toggled OFF (LED off).
  44. #    This disables the RMB autofire feature.
  45. #    We don't want to accidentally autofire whatever you have bound to mouse-2 right now, e.g. TG Pulse Neutraliser, Heatsinks, Caustic sinks etc.
  46. #
  47. # 2. When interdicted keep your boost key held down with all pips to engines.
  48. #    No need to spam the boost key like you're having a seizure, the script will handle it.
  49. #
  50. # 3. When you are through the malestrom cloud and at the titan, toggle ON Scroll Lock (LED on) to enable RMB autofire.
  51. #
  52. # 4. Switch to your Beam Laser + Nanite Torpedo fire group and commence to torpedo the thermal vents.
  53. #    Lock on to thermal vents and hold RMB. The Nanite Torpedo will only fire when you get close.
  54. #    No need to spam RMB.
  55. #
  56. # 5. Now switch to your Beam Laser + AX Missile Rack fire group and fly to the exposed core.
  57. #    Your rockets will fire as fast as the game allows, just hold down RMB and watch the fireworks.
  58. #
  59. # 6. Fly away to avoid the blue radiation attack, and repeat steps 4+.
  60.  
  61. import time
  62. def sleep(milliseconds):
  63.     time.sleep(milliseconds / 1000)
  64.  
  65. # Windows: $ pip install pynput
  66. # SOURCE: https://pypi.org/project/pynput/
  67. import pynput
  68. from pynput.keyboard import Key, KeyCode, Controller as KeyboardController
  69. from pynput.mouse import Button, Controller as MouseController
  70.  
  71. shoot_button = Button.right               # My Nanite Torpedo Pylon & AX Missile Rack button. If you use something else, then edit this line.
  72. boost_button = Key.caps_lock              # My boost key [Flight Miscellaneous > Engine boost]. If you use something else, then edit this line.
  73. #boost_button = Key.tab                   # If you use Tab to boost then uncomment this line.
  74. #boost_button = KeyCode.from_char('b')    # If you use a letter key like 'B' to boost then uncomment this line.
  75.  
  76. ##### Key status #####
  77.  
  78. pressed_inputs = {
  79.     shoot_button : False,
  80.     boost_button : False,
  81. }
  82.  
  83. import ctypes
  84. # GetKeyState(...) returns the status of the key. 1 if the key is toggled on, else 0.
  85. def is_capslock_on():
  86.     return ctypes.windll.user32.GetKeyState(0x14)
  87. def is_numlock_on():
  88.     return ctypes.windll.user32.GetKeyState(0x90)
  89. def is_scrolllock_on():
  90.     return ctypes.windll.user32.GetKeyState(0x91)
  91.  
  92. ##### Keyboard Hook #####
  93.  
  94. # This runs every time a key press is detected.
  95. # When you hold down a key, the operating system will repeat pressing the key at the key repeat rate which is why on_press is called multiple times.
  96. def on_press(key):
  97.     #  print('Pressed', key)
  98.     if key in pressed_inputs:
  99.         pressed_inputs[key] = True
  100.  
  101. # This runs every time a key release is detected.
  102. def on_release(key):
  103.     #  print('Released', key)
  104.     if key in pressed_inputs:
  105.         pressed_inputs[key] = False
  106.  
  107. LLKHF_INJECTED = 0x10
  108.  
  109. #  msg represents the Windows message associated with the event.
  110. #  data provides additional information related to the event. Its a MSLLHOOKSTRUCT. https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msllhookstruct
  111. def keyboard_win32_event_filter(msg, data):
  112.     # If this callback returns True, the event will be allowed to propagate normally throughout the system.
  113.     # If this callback returns False, the event will not be propagated to the listener callback.
  114.     # If listener.suppress_event() is called, the event is suppressed system wide. https://readthedocs.org/projects/pynput/downloads/pdf/latest/    https://pynput.readthedocs.io/en/latest/faq.html
  115.  
  116.     # Prevent simulated key presses from triggering keyboard-event callbacks.
  117.     return not data.flags & LLKHF_INJECTED
  118.  
  119. keyboard_listener = pynput.keyboard.Listener(
  120.     on_press=on_press,
  121.     on_release=on_release,
  122.     win32_event_filter=keyboard_win32_event_filter)
  123. keyboard_listener.start()
  124.  
  125. ##### Mouse Hook #####
  126.  
  127. # This runs when a mouse click or release is detected.
  128. def on_click(x, y, button, pressed):
  129.     #  print('Clicked' if pressed else 'Released', button)
  130.     if button in pressed_inputs:
  131.         pressed_inputs[button] = pressed
  132.  
  133. LLMHF_INJECTED = 0x01
  134.  
  135. #  msg represents the Windows message associated with the event.
  136. #  data provides additional information related to the event. Its a MSLLHOOKSTRUCT. https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msllhookstruct
  137. def mouse_win32_event_filter(msg, data):
  138.     # If this callback returns True, the event will be allowed to propagate normally throughout the system.
  139.     # If this callback returns False, the event will not be propagated to the listener callback.
  140.     # If listener.suppress_event() is called, the event is suppressed system wide. https://readthedocs.org/projects/pynput/downloads/pdf/latest/    https://pynput.readthedocs.io/en/latest/faq.html
  141.  
  142.     # Prevent simulated mouse clicks from triggering mouse-event callbacks.
  143.     return not data.flags & LLMHF_INJECTED
  144.  
  145. mouse_listener = pynput.mouse.Listener(
  146.     on_click=on_click,
  147.     win32_event_filter=mouse_win32_event_filter)
  148. mouse_listener.start()
  149.  
  150. ##### MNK Simulation #####
  151.  
  152. keyboard_controller = KeyboardController()
  153. mouse_controller = MouseController()
  154. def get_controller(mnkInput):
  155.     if isinstance(mnkInput, (Key, KeyCode)):
  156.         return keyboard_controller
  157.     if isinstance(mnkInput, Button):
  158.         return mouse_controller
  159.     print(f'Could not find a controller that accepts {type(mnkInput)} input.')
  160.  
  161. def simulate_press(mnkInput):
  162.     print(f'Simulate {mnkInput} press')
  163.     controller = get_controller(mnkInput)
  164.     controller.press(mnkInput)
  165.  
  166. def simulate_release(mnkInput):
  167.     print(f'Simulate {mnkInput} release')
  168.     controller = get_controller(mnkInput)
  169.     controller.release(mnkInput)
  170.  
  171. def simulate_tap(mnkInput, inputDelay = 50):
  172.     simulate_press(mnkInput)
  173.     sleep(inputDelay)
  174.     simulate_release(mnkInput)
  175.     sleep(inputDelay)
  176.  
  177. ##### Main #####
  178.  
  179. def main():
  180.     if is_scrolllock_on():
  181.         # Untoggle Scroll Lock (turn off LED).
  182.         # Force the initial state of Scroll Lock to OFF when starting this script.
  183.         simulate_tap(Key.scroll_lock)
  184.     while True:
  185.         for mnkInput,pressed in pressed_inputs.items():
  186.             if pressed:
  187.                 # If pressed input is not the autofire button, or Scroll Lock is toggled (LED is on).
  188.                 if mnkInput != shoot_button or is_scrolllock_on():
  189.                     simulate_tap(mnkInput)
  190.             else:
  191.                 # If capslock is released but still toggled (LED is on).
  192.                 if mnkInput == Key.caps_lock and is_capslock_on():
  193.                     # Untoggle Caps Lock (turn off LED).
  194.                     # Because i use Caps Lock for boost, i want the script to un-toggle the key when its not being held.
  195.                     simulate_tap(mnkInput)
  196.         sleep(1)
  197.  
  198. # WARNING: If you use an SCO FSD then uncomment this version of the main() function.
  199. #          It changes Scroll Lock to act as a global on/off hotkey for the entire script.
  200. #          With this version you will want to turn on Scroll Lock when using thrusters (to benefit from auto-boost),
  201. #          and turn off Scroll Lock when in supercruise to avoid triggering supercruise-overdrive with a full-auto boost key.
  202. # def main():
  203. #     if is_scrolllock_on():
  204. #         # Untoggle Scroll Lock (turn off LED).
  205. #         # Force the initial state of Scroll Lock to OFF when starting this script.
  206. #         simulate_tap(Key.scroll_lock)
  207. #     while True:
  208. #         if is_scrolllock_on():
  209. #             for mnkInput,pressed in pressed_inputs.items():
  210. #                 if pressed:
  211. #                     simulate_tap(mnkInput)
  212. #         sleep(1)
  213.  
  214. if __name__ == '__main__':
  215.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement