Advertisement
Guest User

GTA V Selective Fire mod 1.2 by Kryo4lex

a guest
Mar 27th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 30.82 KB | None | 0 0
  1. //By Kryo4lex
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using GTA;
  6. using GTA.Native;
  7. using GTA.Math;
  8. using System.Drawing;
  9. using System.Runtime.InteropServices;
  10. using NativeUI;
  11. using System.IO;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using System.Linq;
  14. using XInputDotNetPure;
  15.  
  16. public class Selective_Fire : Script
  17. {
  18.  
  19.     public const string version_num = "1.2";
  20.     public const string version = "Selective Fire" + " " + version_num;
  21.     public const string settingsFilePath = @"scripts//Selective_Fire//Selective_Fire_settings.bin";
  22.  
  23.     private UIMenu mainMenu;
  24.     private MenuPool _menuPool;
  25.     private UIMenuItem UIMI_FIREMODE_KEY;
  26.     private UIMenuItem UIMI_FIREMODE_CGP;
  27.     private UIMenuCheckboxItem UIMCBI_MOD_ENABLED;
  28.  
  29.     private UIMenuCheckboxItem UIMCBI_SafetyLock;
  30.     private UIMenuCheckboxItem UIMCBI_FullAutoDefault;
  31.     private UIMenuCheckboxItem UIMCBI_Semi;
  32.     private UIMenuCheckboxItem UIMCBI_BurstRelease;
  33.     private UIMenuCheckboxItem UIMCBI_BurstDelay;
  34.  
  35.     private UIMenuListItem UIMLI_BurstCount;
  36.     private UIMenuListItem UIMLI_NotficationType;
  37.  
  38.     private UIMenuItem savesettings;
  39.  
  40.     [Serializable]
  41.     public struct SF_Settings
  42.     {
  43.         public bool MOD_ENABLED;
  44.         public bool SAFETY_LOCK_ENABLED;
  45.         public bool FULL_AUTO_DEFAULT_ENABLED;
  46.         public bool SEMI_ENABLED;
  47.         public bool BURST_RELEASE_ENABLED;
  48.         public bool BURST_DELAY_ENABLED;
  49.         public int N_BURST;
  50.         public List<Keys> KEYS_FIRING_MODE_SWITCH;
  51.         public int CGP_FIRING_MODE_SWITCH;
  52.         public int NOTIFICATION_TYPE;
  53.     }
  54.  
  55.     private SF_Settings settings = new SF_Settings();
  56.  
  57.     private List<dynamic> burstShots
  58.     {
  59.         get; set;
  60.     }
  61.  
  62.     private List<dynamic> notificationTypes
  63.     {
  64.         get; set;
  65.     }
  66.  
  67.     public Selective_Fire()
  68.     {
  69.         if (!File.Exists(settingsFilePath))
  70.         {
  71.             settings.MOD_ENABLED = true;
  72.             settings.SAFETY_LOCK_ENABLED = true;
  73.             settings.FULL_AUTO_DEFAULT_ENABLED = true;
  74.             settings.SEMI_ENABLED = true;
  75.             settings.BURST_RELEASE_ENABLED = true;
  76.             settings.BURST_DELAY_ENABLED = true;
  77.             settings.N_BURST = 3;
  78.             settings.KEYS_FIRING_MODE_SWITCH = new List<Keys>();
  79.             settings.KEYS_FIRING_MODE_SWITCH.Add(Keys.CapsLock);
  80.             settings.CGP_FIRING_MODE_SWITCH = 528;
  81.             settings.NOTIFICATION_TYPE = 0;
  82.         }
  83.         else
  84.         {
  85.             loadSettings();
  86.         }
  87.  
  88.         this.Tick += this.OnTick;
  89.         this.KeyDown += this.OnKeyDown;
  90.         this.KeyUp += this.OnKeyUp;
  91.  
  92.         ControllerGamepadNames = new List<dynamic>
  93.         {
  94.         "A",
  95.         "B",
  96.         "X",
  97.         "Y",
  98.         "Left Shoulder",
  99.         "Right Shoulder",
  100.         "Left Stick",
  101.         "Right Stick",
  102.         "DPad Up",
  103.         "DPad Down",
  104.         "DPad Left",
  105.         "DPad Right",
  106.         "LS Up",
  107.         "LS Down",
  108.         "LS Right",
  109.         "LS Left",
  110.         "RS Up",
  111.         "RS Down",
  112.         "RS Right",
  113.         "RS Left",
  114.         };
  115.  
  116.         setupUIMenu();
  117.     }
  118.  
  119.     private void setupUIMenu()
  120.     {
  121.  
  122.     burstShots = new List<dynamic>
  123.     {
  124.         1,
  125.         2,
  126.         3,
  127.         4,
  128.         5
  129.     };
  130.  
  131.     notificationTypes = new List<dynamic>
  132.     {
  133.         "Subtitle",
  134.         "Map Notification",
  135.         "Help text",
  136.         "None"
  137.     };
  138.  
  139.         _menuPool = new MenuPool();
  140.  
  141.         mainMenu = new UIMenu(version, "~b~By Kryo4lex");
  142.         //mainMenu.SetBannerType("scripts\\Selective_Fire\\customBanner.png");
  143.         _menuPool.Add(mainMenu);
  144.  
  145.         UIMI_FIREMODE_KEY = new UIMenuItem("Firing mode switch keyboard: " + getStringKeys(settings.KEYS_FIRING_MODE_SWITCH), "Select the key for the firing mode switch.");
  146.         UIMI_FIREMODE_CGP = new UIMenuItem("Firing mode switch controller: " + stringControlPattern(settings.CGP_FIRING_MODE_SWITCH), "Select the controller/gamepad controls for the firing mode switch.");
  147.  
  148.         mainMenu.AddItem(UIMCBI_MOD_ENABLED = new UIMenuCheckboxItem("Mod enabled", settings.MOD_ENABLED, "Use this to toggle the Selective Fire mod."));
  149.         mainMenu.AddItem(UIMI_FIREMODE_KEY);
  150.         mainMenu.AddItem(UIMI_FIREMODE_CGP);
  151.         mainMenu.AddItem(UIMCBI_SafetyLock = new UIMenuCheckboxItem("Safety/Lock", settings.SAFETY_LOCK_ENABLED, "Check this option if you want to have the Safety/Lock firing mode available."));
  152.         mainMenu.AddItem(UIMCBI_FullAutoDefault = new UIMenuCheckboxItem("Full auto/Default", settings.FULL_AUTO_DEFAULT_ENABLED, "Check this option if you want to have the Full auto/Default firing mode available."));
  153.         mainMenu.AddItem(UIMCBI_Semi = new UIMenuCheckboxItem("Single fire/Semi", settings.SEMI_ENABLED, "Check this option if you want to have the Single fire/Semi firing mode available."));
  154.         mainMenu.AddItem(UIMCBI_BurstRelease = new UIMenuCheckboxItem(settings.N_BURST.ToString() + "-Burst-release", settings.BURST_RELEASE_ENABLED, "Check this option if you want to have the Burst-release firing mode available. Release means, that you have to release the trigger to fire again."));
  155.         mainMenu.AddItem(UIMCBI_BurstDelay = new UIMenuCheckboxItem(settings.N_BURST.ToString() + "-Burst-delay", settings.BURST_DELAY_ENABLED, "Check this option if you want to have the Burst-delay firing mode available. Delay means, that yo will shoot a burst and then it will fire again with a delay, if you keep holding the trigger."));
  156.         mainMenu.AddItem(UIMLI_BurstCount = new UIMenuListItem("Burst-shot count:", burstShots, (settings.N_BURST-1) , "Change the count of the shots of a burst."));
  157.         mainMenu.AddItem(UIMLI_NotficationType = new UIMenuListItem("Notification type:", notificationTypes, settings.NOTIFICATION_TYPE, "Change the way the change of the firing mode is indicated."));
  158.  
  159.  
  160.         savesettings = new UIMenuItem("Save settings", "Save your settings, to play with the same settings on the next startup of the game.");
  161.  
  162.         mainMenu.AddItem(savesettings);
  163.         mainMenu.RefreshIndex();
  164.         mainMenu.OnItemSelect += OnItemSelect;
  165.         mainMenu.OnCheckboxChange += OnCheckboxChange;
  166.         mainMenu.OnListChange += OnListChange;
  167.  
  168.     }
  169.  
  170.     public void OnListChange(UIMenu sender, UIMenuListItem list, int index)
  171.     {
  172.         if (list == UIMLI_BurstCount)
  173.         {
  174.             settings.N_BURST = UIMLI_BurstCount.Index + 1;
  175.             UIMCBI_BurstRelease.Text = settings.N_BURST.ToString() + "-Burst-release";
  176.             UIMCBI_BurstDelay.Text = settings.N_BURST.ToString() + "-Burst-delay";
  177.         }
  178.  
  179.         if (list == UIMLI_NotficationType)
  180.         {
  181.             settings.NOTIFICATION_TYPE = UIMLI_NotficationType.Index;
  182.         }
  183.     }
  184.  
  185.     public void OnItemSelect(UIMenu sender, UIMenuItem selectedItem, int index)
  186.     {
  187.         if (selectedItem == savesettings)
  188.         {
  189.             saveSettings();
  190.             UI.Notify("~b~Selective Fire settings saved!");
  191.         }
  192.         else if (selectedItem == UIMI_FIREMODE_KEY)
  193.         {
  194.             ChangeKeyAssignment_active = true;
  195.         }
  196.         else if(selectedItem == UIMI_FIREMODE_CGP)
  197.         {
  198.             ChangeControllerAssignment = true;
  199.         }
  200.     }
  201.  
  202.     public void OnCheckboxChange(UIMenu sender, UIMenuCheckboxItem checkbox, bool Checked)
  203.     {
  204.  
  205.         if (checkbox == UIMCBI_SafetyLock)
  206.         {
  207.             settings.SAFETY_LOCK_ENABLED = UIMCBI_SafetyLock.Checked;
  208.         }
  209.         else if(checkbox == UIMCBI_FullAutoDefault)
  210.         {
  211.             settings.FULL_AUTO_DEFAULT_ENABLED = UIMCBI_FullAutoDefault.Checked;
  212.         }
  213.         else if (checkbox == UIMCBI_Semi)
  214.         {
  215.             settings.SEMI_ENABLED = UIMCBI_Semi.Checked;
  216.         }
  217.         else if (checkbox == UIMCBI_BurstRelease)
  218.         {
  219.             settings.BURST_RELEASE_ENABLED = UIMCBI_BurstRelease.Checked;
  220.         }
  221.         else if (checkbox == UIMCBI_BurstDelay)
  222.         {
  223.             settings.BURST_DELAY_ENABLED = UIMCBI_BurstDelay.Checked;
  224.         }
  225.         else if (checkbox == UIMCBI_MOD_ENABLED)
  226.         {
  227.             settings.MOD_ENABLED = UIMCBI_MOD_ENABLED.Checked;
  228.         }
  229.  
  230.         //Protect from selecting no firing mode at all
  231.         /*
  232.         if (!(settings.SAFETY_LOCK_ENABLED || settings.FULL_AUTO_DEFAULT_ENABLED || settings.SEMI_ENABLED || settings.BURST_RELEASE_ENABLED || settings.BURST_DELAY_ENABLED))
  233.         {
  234.             checkbox.
  235.         }
  236.         */
  237.     }
  238.  
  239.     private string getStringKeys(List<Keys> listkeys)
  240.     {
  241.         string str = "";
  242.         int i = 0;
  243.         foreach (var key in listkeys)
  244.         {
  245.             str += key.ToString();
  246.             if (i != (listkeys.Count - 1))
  247.             {
  248.                 str += " + ";
  249.             }
  250.             i++;
  251.         }
  252.         return str;
  253.     }
  254.  
  255.     private void saveSettings()
  256.     {
  257.         FileStream fs = new FileStream(settingsFilePath, FileMode.Create);
  258.         BinaryFormatter formatter = new BinaryFormatter();
  259.         formatter.Serialize(fs, settings);
  260.         fs.Close();
  261.     }
  262.  
  263.     private void loadSettings()
  264.     {
  265.         FileStream fs = new FileStream(settingsFilePath, FileMode.Open);
  266.         BinaryFormatter formatter = new BinaryFormatter();
  267.         settings = (SF_Settings)formatter.Deserialize(fs);
  268.         fs.Close();
  269.     }
  270.  
  271.     private List<Keys> newKeys = new List<Keys>();
  272.     bool ChangeKeyAssignment_active = false;
  273.     int KeyAssignmentStepIndex = 0;
  274.     bool s1_started = false;
  275.     long t_s1 = 0;
  276.     bool s2_started = false;
  277.     long t_s2 = 0;
  278.     private List<Keys> pressedKeys = new List<Keys>();
  279.  
  280.     bool switchBef = false;
  281.     bool switchNow = false;
  282.  
  283.     void OnTick(object sender, EventArgs e)
  284.     {
  285.         if (settings.MOD_ENABLED)
  286.         {
  287.             processKeyboardInput();
  288.  
  289.             processControllerInput();
  290.  
  291.             featureSelectiveFire();
  292.  
  293.             if(displayHelpTextThisFrameActive)
  294.             {
  295.                 if(displayHelpTextThisFrameActiveStartTime + 2500 >= Environment.TickCount)
  296.                 {
  297.                     DisplayHelpTextThisFrame(displayHelpTextThisFrameMessage);
  298.                 }
  299.                 else
  300.                 {
  301.                     displayHelpTextThisFrameActive = false;
  302.                 }
  303.             }
  304.  
  305.         }
  306.         _menuPool.ProcessMenus();
  307.     }
  308.  
  309.     private void processKeyboardInput()
  310.     {
  311.         if (ChangeKeyAssignment_active)
  312.         {
  313.  
  314.             switch (KeyAssignmentStepIndex)
  315.             {
  316.                 case 0:
  317.                     mainMenu.Visible = false;
  318.                     UI.ShowSubtitle("~b~In 3 seconds, you have 4 seconds, to hold the new key(s), for the firing mode switch.", 3000);
  319.  
  320.                     if (!s1_started)
  321.                     {
  322.                         s1_started = true;
  323.                         t_s1 = Environment.TickCount + 3000;
  324.                     }
  325.  
  326.                     if (Environment.TickCount >= t_s1)
  327.                         KeyAssignmentStepIndex++;
  328.                     break;
  329.                 case 1:
  330.                     newKeys.Clear();
  331.                     newKeys = new List<Keys>(pressedKeys);
  332.                     UI.ShowSubtitle("~b~Key(s):\n" + getStringKeys(newKeys), 4000);
  333.  
  334.                     if (!s2_started)
  335.                     {
  336.                         s2_started = true;
  337.                         t_s2 = Environment.TickCount + 4000;
  338.                     }
  339.  
  340.                     if (Environment.TickCount >= t_s2)
  341.                         KeyAssignmentStepIndex++;
  342.                     break;
  343.                 case 2:
  344.                     s1_started = false;
  345.                     s2_started = false;
  346.                     ChangeKeyAssignment_active = false;
  347.                     KeyAssignmentStepIndex = 0;
  348.                     if (newKeys.Count != 0)
  349.                     {
  350.                         settings.KEYS_FIRING_MODE_SWITCH.Clear();
  351.                         settings.KEYS_FIRING_MODE_SWITCH = new List<Keys>(newKeys);
  352.                         UIMI_FIREMODE_KEY.Text = "Firing mode switch key: " + getStringKeys(settings.KEYS_FIRING_MODE_SWITCH);
  353.                         UI.ShowSubtitle("~b~Successfully assinged firing mode switch key to " + getStringKeys(pressedKeys), 3000);
  354.                     }
  355.                     else
  356.                     {
  357.                         UI.ShowSubtitle("~b~No key(s) have been changed!", 3000);
  358.                     }
  359.                     break;
  360.             }
  361.         }
  362.         else
  363.         {
  364.             switchNow = LinqHelper.ContainsAll(pressedKeys, settings.KEYS_FIRING_MODE_SWITCH);
  365.             if (switchNow && !switchBef)
  366.             {
  367.                 if (!Game.Player.Character.IsInVehicle())
  368.                 {
  369.                     changeFiringMode();
  370.                 }
  371.             }
  372.             switchBef = switchNow;
  373.         }
  374.     }
  375.  
  376.     //used to count the shots for bursts
  377.     int shotCounter = 0;
  378.  
  379.     //number of desired shots fired with a burst
  380.     //int n_burst = 3;
  381.  
  382.     //used to count shots --> edge detection
  383.     bool shots_bef = false;
  384.  
  385.     //fire delay in ms for 4th firing mode (n-burst delay)
  386.     long fireDelay = 500;
  387.     long lastFire = 0;
  388.  
  389.     private void featureSelectiveFire()
  390.     {
  391.         if (!Game.Player.Character.IsInVehicle())
  392.         {
  393.             //edge detection to count shots
  394.             if (Game.Player.Character.IsShooting && shots_bef == false)
  395.             {
  396.                 shotCounter++;
  397.             }
  398.  
  399.             shots_bef = Game.Player.Character.IsShooting;
  400.  
  401.             bugfixes();
  402.  
  403.             switch (firingMode)
  404.             {
  405.                 case 0://lock weapon
  406.                     disableAttack();
  407.                     break;
  408.                 case 1://everything is enabled, like default
  409.                     break;
  410.                 case 2://single fire
  411.                     //if 1 shot has been fired, suppress (disable) the fire key                
  412.                     if (shotCounter >= 1)
  413.                     {
  414.                         disableAttack();
  415.                     }
  416.                     //if the suppressed fire key (=disabled control) is pressed, re-enable it and reset the shot counter
  417.                     if (Game.IsDisabledControlJustPressed(0, GTA.Control.Attack))
  418.                     {
  419.                         Game.EnableControlThisFrame(0, GTA.Control.Attack);
  420.                         shotCounter = 0;
  421.                     }
  422.                     break;
  423.                 case 3://burst fire release
  424.                     //same as case 1, but only effective if it counts 3 shotCounter = burst
  425.                     //necesssary to release the fire key
  426.                     if (shotCounter >= settings.N_BURST)
  427.                     {
  428.                         disableAttack();
  429.                     }
  430.                     //this code branch part simulates the trigger,
  431.                     //even if the player did bot hit the key.
  432.                     //Without that code, the weapon would only fire while the player pressed the trigger,
  433.                     //but not automatically 3 times (with n_burst = 3), if he only pressed it once
  434.                     else
  435.                     {
  436.                         if (Game.IsControlPressed(0, GTA.Control.Attack))
  437.                         {
  438.                             Game.SetControlNormal(0, GTA.Control.Attack, 1.0f);
  439.                         }
  440.                     }
  441.  
  442.                     if (Game.IsDisabledControlJustPressed(0, GTA.Control.Attack))
  443.                     {
  444.                         Game.EnableControlThisFrame(0, GTA.Control.Attack);
  445.                         shotCounter = 0;
  446.                     }
  447.                     break;
  448.                 case 4://burst fire delay
  449.                        //same as case 3, but no need to release the fire key
  450.  
  451.                     if (shotCounter >= settings.N_BURST)
  452.                     {
  453.                         lastFire = Environment.TickCount;
  454.                         shotCounter = 0;
  455.                     }
  456.                     //this code branch part simulates the trigger,
  457.                     //even if the player did bot hit the key.
  458.                     //Without that code, the weapon would only fire while the player pressed the trigger,
  459.                     //but not automatically 3 times (with n_burst = 3), if he only pressed it once
  460.                     else
  461.                     {
  462.                         if(lastFire + fireDelay >= Environment.TickCount)
  463.                         {
  464.                             disableAttack();
  465.                             break;
  466.                         }
  467.                         if (Game.IsControlPressed(0, GTA.Control.Attack))
  468.                         {
  469.                             Game.SetControlNormal(0, GTA.Control.Attack, 1.0f);
  470.                         }
  471.                     }
  472.                    
  473.                     break;
  474.                 default:
  475.  
  476.                     break;
  477.             }
  478.         }
  479.     }
  480.  
  481.     //suppress fire key
  482.     void disableAttack()
  483.     {
  484.         Game.DisableControlThisFrame(0, GTA.Control.Attack);
  485.         Game.DisableControlThisFrame(0, GTA.Control.Attack2);
  486.     }
  487.  
  488.     Weapon playerWeapon;
  489.     void bugfixes()
  490.     {
  491.         //shooting after weapon switch
  492.         if (Game.Player.Character.Weapons.Current != playerWeapon)
  493.         {
  494.             shotCounter = settings.N_BURST;
  495.         }
  496.  
  497.         playerWeapon = Game.Player.Character.Weapons.Current;
  498.  
  499.         //Check if the wepaon itself exists, for example when you climb and etc., there would be this bug where it continues shooting after interrupted
  500.         //Don't use Game.Player.Character.Weapons.CurrentWeaponObject.Exists() --> Null Reference Exception, and try catch is inefficient
  501.         if (Game.Player.Character.Weapons.CurrentWeaponObject == null)
  502.         {
  503.             //no weapon object in the memory (=not existing in game) --> reset shotCounter
  504.             shotCounter = settings.N_BURST;
  505.             //UI.ShowSubtitle("WEAP NO MEM",100);
  506.         }
  507.         else
  508.         {
  509.             if (Game.Player.Character.Weapons.CurrentWeaponObject.Exists())
  510.             {
  511.                 //the weapon object exists in the game so do nothing --> player isn't climbing etc. ...
  512.                 //UI.ShowSubtitle("WEAP USED",100);
  513.             }
  514.             else
  515.             {
  516.                 //the weapon object exists in the memory, but not in the game --> weapon put away --> reset shotCounter
  517.                 shotCounter = settings.N_BURST;
  518.                 //UI.ShowSubtitle("WEAP PUT AWAY",100);
  519.             }
  520.         }
  521.  
  522.         //shooting when ragdoll --> ragdoll occurs when shooting is interrupted
  523.         if (Game.Player.Character.IsRagdoll)
  524.         {
  525.             shotCounter = settings.N_BURST;
  526.         }
  527.  
  528.         //is in air, like jumping
  529.         if (Game.Player.Character.IsInAir)
  530.         {
  531.             shotCounter = settings.N_BURST;
  532.         }
  533.  
  534.         //if the player pressed the cover control, imagine that you shoot with burst, then go into cover, and and then it continus firing
  535.         if (Game.IsControlJustPressed(0, GTA.Control.Cover))
  536.         {
  537.             shotCounter = settings.N_BURST;
  538.         }
  539.  
  540.         //shooting after reloading
  541.         if (Game.Player.Character.IsReloading)
  542.         {
  543.             shotCounter = settings.N_BURST;
  544.         }
  545.  
  546.     }
  547.  
  548.     int firingMode = 1;
  549.  
  550.     int MenuToggleStep = 0;
  551.  
  552.     private void OnKeyDown(object sender, KeyEventArgs e)
  553.     {
  554.         if (!(pressedKeys.Contains(e.KeyCode)))
  555.             pressedKeys.Add(e.KeyCode);
  556.         /*
  557.         //change fire mode with x key
  558.         if (e.KeyCode == Keys.CapsLock)
  559.         {
  560.             if (!Game.Player.Character.IsInVehicle())
  561.             {
  562.                 changeFiringMode();
  563.                 Audio.PlaySoundFrontend("TOGGLE_ON", "HUD_FRONTEND_DEFAULT_SOUNDSET");
  564.             }
  565.         }
  566.         */
  567.         /*
  568.         if (e.KeyCode == Keys.X)
  569.         {
  570.             mainMenu.Visible = !mainMenu.Visible;
  571.         }
  572.         */
  573.  
  574.         if (e.KeyCode == Keys.F && MenuToggleStep == 0)
  575.         {
  576.             MenuToggleStep++;
  577.         }
  578.         else if (e.KeyCode == Keys.I && MenuToggleStep == 1)
  579.         {
  580.             MenuToggleStep++;
  581.         }
  582.         else if (e.KeyCode == Keys.R && MenuToggleStep == 2)
  583.         {
  584.             MenuToggleStep++;
  585.         }
  586.         else if (e.KeyCode == Keys.E && MenuToggleStep == 3)
  587.         {
  588.             MenuToggleStep++;
  589.         }
  590.         else if (e.KeyCode == Keys.M && MenuToggleStep == 4)
  591.         {
  592.             mainMenu.Visible = !mainMenu.Visible;
  593.             MenuToggleStep = 0;
  594.         }
  595.         else
  596.         {
  597.             MenuToggleStep = 0;
  598.         }
  599.            
  600.        
  601.     }
  602.  
  603.     private void OnKeyUp(object sender, KeyEventArgs e)
  604.     {
  605.         pressedKeys.Remove(e.KeyCode);
  606.  
  607.         //printPressedKeys(pressedKeys);
  608.     }
  609.  
  610.     //change fire mode and show current fire mode
  611.     void changeFiringMode()
  612.     {
  613.         //firingMode++;
  614.         bool[] boolFiringModes = { settings.SAFETY_LOCK_ENABLED, settings.FULL_AUTO_DEFAULT_ENABLED, settings.SEMI_ENABLED, settings.BURST_RELEASE_ENABLED, settings.BURST_DELAY_ENABLED };
  615.  
  616.         int i = firingMode;
  617.         int j = 0;
  618.         while(j<=5)
  619.         {
  620.             i++;
  621.             j++;
  622.             if (i >= 5)
  623.             {
  624.                 i = 0;
  625.             }
  626.             if(boolFiringModes[i]==true)
  627.             {
  628.                 firingMode = i;
  629.                 break;
  630.             }
  631.  
  632.         }
  633.  
  634.         /*
  635.         if (firingMode >= 5)
  636.         {
  637.             firingMode = 0;
  638.         }
  639.         */
  640.  
  641.         switch (firingMode)
  642.         {
  643.             case 0:
  644.                 changeFiringModeMessage("Safety/Lock");
  645.                 break;
  646.             case 1:
  647.                 changeFiringModeMessage("Firing mode: Full Auto (Default)");
  648.                 break;
  649.             case 2:
  650.                 changeFiringModeMessage("Firing mode: Semi (Single Fire)");
  651.                 break;
  652.             case 3:
  653.                 changeFiringModeMessage("Firing mode: " + settings.N_BURST.ToString() + "-Burst Release");
  654.                 break;
  655.             case 4:
  656.                 changeFiringModeMessage("Firing mode: " + settings.N_BURST.ToString() + "-Burst Delay");
  657.                 break;
  658.             default:
  659.  
  660.                 break;
  661.         }
  662.         Audio.PlaySoundFrontend("TOGGLE_ON", "HUD_FRONTEND_DEFAULT_SOUNDSET");
  663.     }
  664.  
  665.     int notificationHandle;
  666.     bool displayHelpTextThisFrameActive = false;
  667.     long displayHelpTextThisFrameActiveStartTime = 0;
  668.     string displayHelpTextThisFrameMessage;
  669.  
  670.     void changeFiringModeMessage(string message)
  671.     {
  672.         if(settings.NOTIFICATION_TYPE == 0)//Subtitle
  673.         {
  674.             UI.ShowSubtitle(message);
  675.         }
  676.         else if(settings.NOTIFICATION_TYPE == 1)// Map notification
  677.         {
  678.             Function.Call(Hash._REMOVE_NOTIFICATION, new InputArgument[] { notificationHandle });
  679.             notificationHandle = ShowNotification(message);
  680.         }
  681.         else if (settings.NOTIFICATION_TYPE == 2)//Help text
  682.         {
  683.             displayHelpTextThisFrameActive = true;
  684.             displayHelpTextThisFrameActiveStartTime = Environment.TickCount;
  685.             displayHelpTextThisFrameMessage = message;
  686.         }
  687.         else if (settings.NOTIFICATION_TYPE == 3)//None
  688.         {
  689.  
  690.         }
  691.     }
  692.  
  693.     public int ShowNotification(string message)
  694.     {
  695.         Function.Call(Hash._SET_NOTIFICATION_TEXT_ENTRY, new InputArgument[] { "STRING" });
  696.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, new InputArgument[] { message });
  697.         int haNotificationDeletitionLastTimee = Function.Call<int>(Hash._DRAW_NOTIFICATION, new InputArgument[] { false, false });
  698.         return haNotificationDeletitionLastTimee;
  699.     }
  700.  
  701.    
  702.     void DisplayHelpTextThisFrame(string text)
  703.     {
  704.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  705.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  706.         Function.Call(Hash._0x238FFE5C7B0498A6, 0, 0, 1, -1);
  707.     }
  708.    
  709.     #region Controller/GamePad Input
  710.  
  711.     public void setBit(ref int value, int index)
  712.     {
  713.         value = (value | (1 << index));
  714.     }
  715.  
  716.     bool isBitSet(int value, int index)
  717.     {
  718.         return (value & (1 << index)) != 0;
  719.     }
  720.  
  721.     int createControlPattern()
  722.     {
  723.         int controlpattern = 0;
  724.         GamePadState state = GamePad.GetState(PlayerIndex.One);
  725.  
  726.         //A
  727.         if (state.Buttons.A == XInputDotNetPure.ButtonState.Pressed)
  728.         {
  729.             setBit(ref controlpattern, 0);
  730.         }
  731.  
  732.         //B
  733.         if (state.Buttons.B == XInputDotNetPure.ButtonState.Pressed)
  734.         {
  735.             setBit(ref controlpattern, 1);
  736.         }
  737.  
  738.         //X
  739.         if (state.Buttons.X == XInputDotNetPure.ButtonState.Pressed)
  740.         {
  741.             setBit(ref controlpattern, 2);
  742.         }
  743.  
  744.         //Y
  745.         if (state.Buttons.Y == XInputDotNetPure.ButtonState.Pressed)
  746.         {
  747.             setBit(ref controlpattern, 3);
  748.         }
  749.  
  750.         //Left Shoulder
  751.         if (state.Buttons.LeftShoulder == XInputDotNetPure.ButtonState.Pressed)
  752.         {
  753.             setBit(ref controlpattern, 4);
  754.         }
  755.  
  756.         //Right Shoulder
  757.         if (state.Buttons.RightShoulder == XInputDotNetPure.ButtonState.Pressed)
  758.         {
  759.             setBit(ref controlpattern, 5);
  760.         }
  761.  
  762.         //Left Stick
  763.         if (state.Buttons.LeftStick == XInputDotNetPure.ButtonState.Pressed)
  764.         {
  765.             setBit(ref controlpattern, 6);
  766.         }
  767.  
  768.         //Right Stick
  769.         if (state.Buttons.RightStick == XInputDotNetPure.ButtonState.Pressed)
  770.         {
  771.             setBit(ref controlpattern, 7);
  772.         }
  773.  
  774.         //DPad Up
  775.         if (state.DPad.Up == XInputDotNetPure.ButtonState.Pressed)
  776.         {
  777.             setBit(ref controlpattern, 8);
  778.         }
  779.  
  780.         //DPad Down
  781.         if (state.DPad.Down == XInputDotNetPure.ButtonState.Pressed)
  782.         {
  783.             setBit(ref controlpattern, 9);
  784.         }
  785.  
  786.         //DPad Left
  787.         if (state.DPad.Left == XInputDotNetPure.ButtonState.Pressed)
  788.         {
  789.             setBit(ref controlpattern, 10);
  790.         }
  791.  
  792.         //DPad Right
  793.         if (state.DPad.Right == XInputDotNetPure.ButtonState.Pressed)
  794.         {
  795.             setBit(ref controlpattern, 11);
  796.         }
  797.  
  798.         //LS Up
  799.         if (state.ThumbSticks.Left.Y >= 0.5)
  800.         {
  801.             setBit(ref controlpattern, 12);
  802.         }
  803.  
  804.         //LS Down
  805.         if (state.ThumbSticks.Left.Y <= -0.5)
  806.         {
  807.             setBit(ref controlpattern, 13);
  808.         }
  809.  
  810.         //LS Right
  811.         if (state.ThumbSticks.Left.X >= 0.5)
  812.         {
  813.             setBit(ref controlpattern, 14);
  814.         }
  815.  
  816.         //LS Left
  817.         if (state.ThumbSticks.Left.X <= -0.5)
  818.         {
  819.             setBit(ref controlpattern, 15);
  820.         }
  821.  
  822.         //RS Up
  823.         if (state.ThumbSticks.Right.Y >= 0.5)
  824.         {
  825.             setBit(ref controlpattern, 16);
  826.         }
  827.  
  828.         //RS Down
  829.         if (state.ThumbSticks.Right.Y <= -0.5)
  830.         {
  831.             setBit(ref controlpattern, 17);
  832.         }
  833.  
  834.         //RS Right
  835.         if (state.ThumbSticks.Right.X >= 0.5)
  836.         {
  837.             setBit(ref controlpattern, 18);
  838.         }
  839.  
  840.         //RS Left
  841.         if (state.ThumbSticks.Right.X <= -0.5)
  842.         {
  843.             setBit(ref controlpattern, 19);
  844.         }
  845.  
  846.         return controlpattern;
  847.  
  848.     }
  849.  
  850.     private List<dynamic> ControllerGamepadNames
  851.     {
  852.         get; set;
  853.     }
  854.  
  855.     string stringControlPattern(int value)
  856.     {
  857.         string s = "";
  858.  
  859.         for (int i = 0; i < ControllerGamepadNames.Count; i++)
  860.         {
  861.             if (isBitSet(value, i))
  862.                 s += ControllerGamepadNames[i] + "+";
  863.         }
  864.  
  865.         if (s.Length > 1)
  866.             s = s.Remove(s.Length - 1);
  867.  
  868.         return s;
  869.     }
  870.  
  871.     int change_cgp_control_idx = 0;
  872.  
  873.     int newcontrols = 0;
  874.  
  875.     bool state_CGP_FIRING_MODE_SWITCH = false;
  876.  
  877.     bool state_CGP_FIRING_MODE_SWITCH_bef = false;
  878.  
  879.     bool ChangeControllerAssignment = false;
  880.  
  881.     void processControllerInput()
  882.     {
  883.         int currentcontrolpattern = createControlPattern();
  884.  
  885.         state_CGP_FIRING_MODE_SWITCH = (currentcontrolpattern & settings.CGP_FIRING_MODE_SWITCH) == settings.CGP_FIRING_MODE_SWITCH;
  886.  
  887.         if (ChangeControllerAssignment)
  888.         {
  889.  
  890.             switch (change_cgp_control_idx)
  891.             {
  892.                 case 0:
  893.  
  894.                     UI.ShowSubtitle("~b~In 3 seconds, you have 4 seconds, to hold the control(s), for the firing mode switch.", 3000);
  895.  
  896.                     if (!s1_started)
  897.                     {
  898.                         s1_started = true;
  899.                         t_s1 = Environment.TickCount + 3000;
  900.                     }
  901.  
  902.                     if (Environment.TickCount >= t_s1)
  903.                         change_cgp_control_idx++;
  904.                     break;
  905.                 case 1:
  906.                     newcontrols = createControlPattern();
  907.                     UI.ShowSubtitle("~b~Controls:\n" + stringControlPattern(newcontrols), 4000);
  908.  
  909.                     if (!s2_started)
  910.                     {
  911.                         s2_started = true;
  912.                         t_s2 = Environment.TickCount + 4000;
  913.                     }
  914.  
  915.                     if (Environment.TickCount >= t_s2)
  916.                         change_cgp_control_idx++;
  917.                     break;
  918.                 case 2:
  919.  
  920.                     s1_started = false;
  921.                     s2_started = false;
  922.                     ChangeControllerAssignment = false;
  923.                     change_cgp_control_idx = 0;
  924.                     if (newcontrols != 0)
  925.                     {
  926.                         settings.CGP_FIRING_MODE_SWITCH = newcontrols;
  927.                         UIMI_FIREMODE_CGP.Text = "Firing mode switch controller: " + stringControlPattern(settings.CGP_FIRING_MODE_SWITCH);
  928.  
  929.                         UI.ShowSubtitle("~b~Successfully assinged firing mode switch key to " + stringControlPattern(settings.CGP_FIRING_MODE_SWITCH), 3000);
  930.                     }
  931.                     else
  932.                     {
  933.                         UI.ShowSubtitle("~b~No control(s) have been changed!", 3000);
  934.  
  935.                     }
  936.                     break;
  937.             }
  938.         }
  939.         else
  940.         {
  941.  
  942.             if(state_CGP_FIRING_MODE_SWITCH && !state_CGP_FIRING_MODE_SWITCH_bef)
  943.             {
  944.                 changeFiringMode();
  945.             }
  946.  
  947.             state_CGP_FIRING_MODE_SWITCH_bef = state_CGP_FIRING_MODE_SWITCH;
  948.  
  949.         }
  950.     }
  951.  
  952.     #endregion
  953. }
  954.  
  955. public static class LinqHelper
  956. {
  957.     public static bool ContainsAll<T>(this IEnumerable<T> containingList, IEnumerable<T> lookupList)
  958.     {
  959.         return !lookupList.Except(containingList).Any();
  960.     }
  961.  
  962.     public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
  963.     {
  964.         return listToClone.Select(item => (T)item.Clone()).ToList();
  965.     }
  966. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement