Eddlm

Sound Swapper 2.0 - Source

Feb 3rd, 2020
1,527
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using GTA;
  2. using GTA.Math;
  3. using GTA.Native;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using System.Runtime.Serialization;
  9. using System.Xml;
  10.  
  11. public class AutoEngineSoundSwap : Script
  12. {
  13.     Dictionary<Model, SwapInfo> SoundRelationships = new Dictionary<Model, SwapInfo>();
  14.     string ScriptName = "Auto Engine Sound Swap";
  15.     string ScriptVer = "1.0";
  16.     Dictionary<Vehicle, int> SwapWatcher = new Dictionary<Vehicle, int>();
  17.     Vehicle[] Traffic;
  18.  
  19.     bool Debug = false;
  20.  
  21.     public AutoEngineSoundSwap()
  22.     {
  23.         Tick += OnTick;
  24.  
  25.  
  26.         LoadSettings();
  27.     }
  28.  
  29.     public static bool WasCheatStringJustEntered(string cheat)
  30.     {
  31.         return Function.Call<bool>(Hash._0x557E43C447E700A8, Game.GenerateHash(cheat));
  32.     }
  33.     int Timer = 0;
  34.  
  35.     int TrafficListCounter = 0;
  36.     int LastEngineUpgrade = 0;
  37.     void OnTick(object sender, EventArgs e)
  38.     {
  39.  
  40.         if (WasCheatStringJustEntered("assdebug"))
  41.         {
  42.             Debug = !Debug;
  43.             if (Debug) UI.Notify(ScriptName + " debug ~g~enabled~w~."); else UI.Notify(ScriptName + " debug ~b~disabled~w~.");
  44.         }
  45.         if (Timer < Game.GameTime)
  46.         {
  47.             Timer = Game.GameTime + 500;
  48.             HandleSwaps();
  49.         }
  50.  
  51.  
  52.         // Sets the car top speed. Its not engine limited, seems to be force-based.
  53.         // Function.Call((Hash)0xBAA045B4E42F3C06, Game.Player.Character.CurrentVehicle, Game.Player.Character.CurrentVehicle.Velocity.Length());    
  54.  
  55.         // Modifies the engine top speed.
  56.         //Function.Call((Hash)0x93A3996368C94158, Game.Player.Character.CurrentVehicle, 20 - Game.Player.Character.CurrentVehicle.Velocity.Length());
  57.  
  58.     }
  59.     int HandleTuningSwaps(Vehicle v, int lastMod)
  60.     {
  61.         if (CanWeUse(v) && SoundRelationships.ContainsKey(v.Model))
  62.         {
  63.             int m = v.GetMod((VehicleMod)SoundRelationships[v.Model].ModCategory);
  64.             if (m != lastMod)
  65.             {
  66.                 if (SoundRelationships[v.Model].ModIDtoEngineSound.ContainsKey(m))
  67.                 {
  68.                     SwapEngineSound(v, SoundRelationships[v.Model].ModIDtoEngineSound[m]);
  69.                     if (Debug) UI.Notify("~b~" + v.FriendlyName + "~w~'s engine its swapped for a ~b~" + SoundRelationships[v.Model].ModIDtoEngineSound[m] + "~w~ engine.");
  70.                     if (m == -1)
  71.                     {
  72.                         Function.Call((Hash)0x93A3996368C94158, v,0f);
  73.                         v.EnginePowerMultiplier = 1f;
  74.                     }
  75.                     else
  76.                     {
  77.                         float myMaxSpeed = Function.Call<float>(Hash._0x53AF99BAA671CA47, v);
  78.                         float maxSpeed = Function.Call<float>(Hash._0xF417C2502FFFED43, Game.GenerateHash(SoundRelationships[v.Model].ModIDtoEngineSound[m]));
  79.                         float bonusPower = (Function.Call<float>(Hash._0x8C044C5C84505B6A, Game.GenerateHash(SoundRelationships[v.Model].ModIDtoEngineSound[m])))*100; //- Function.Call<float>(Hash._0x5DD35C8D074E57AE, v)
  80.                         if (bonusPower > 0f) v.EnginePowerMultiplier = bonusPower; else v.EnginePowerMultiplier = -bonusPower;
  81.                     }
  82.                 }
  83.                 /*
  84.                 if (SoundRelationships[v.Model].ModIDtoSpeedPlus.ContainsKey(m))
  85.                 {
  86.                     Function.Call((Hash)0x93A3996368C94158, v, (float)SoundRelationships[v.Model].ModIDtoSpeedPlus[m]);
  87.                     if (Debug) UI.Notify(v.FriendlyName + "'s engine upgraded by " + SoundRelationships[v.Model].ModIDtoSpeedPlus[m] + "");
  88.                 }
  89.                 else
  90.                 {
  91.                     Function.Call((Hash)0x93A3996368C94158, v, 0f);
  92.                 }
  93.                 */
  94.                 return m;
  95.             }
  96.             //DisplayHelpTextThisFrame("Max speed: " + Function.Call<int>(Hash._0x90A38E9838E0A8C1, Game.Player.Character.CurrentVehicle, 0, 0).ToString());
  97.         }
  98.         return -2;
  99.     }
  100.  
  101.     void HandleSwaps()
  102.     {
  103.  
  104.         foreach (Vehicle v in SwapWatcher.Keys)
  105.         {
  106.             if (CanWeUse(v))
  107.             {
  108.                 int n = HandleTuningSwaps(v, SwapWatcher[v]);
  109.                 if (n > -2)
  110.                 {
  111.                     SwapWatcher[v] = n;
  112.                     break;
  113.                 }
  114.  
  115.                 if (NoRadioOnPolice && IsPoliceVehicle(v) && Function.Call<bool>(Hash._0x5F43D83FD6738741)) v.RadioStation = RadioStation.RadioOff;
  116.  
  117.             }
  118.             else
  119.             {
  120.                 SwapWatcher.Remove(v);
  121.                 break;
  122.             }
  123.         }
  124.  
  125.         Vehicle PlayerVeh = Game.Player.Character.CurrentVehicle;
  126.         if (CanWeUse(PlayerVeh) && !SwapWatcher.ContainsKey(PlayerVeh) && SoundRelationships.ContainsKey(PlayerVeh.Model))
  127.         {
  128.             SwapWatcher.Add(PlayerVeh, -2);
  129.         }
  130.  
  131.  
  132.  
  133.         if (Traffic == null)
  134.         {
  135.             if (TrafficToo) Traffic = World.GetAllVehicles();
  136.         }
  137.         else
  138.         {
  139.  
  140.             for (int i = TrafficListCounter; i < TrafficListCounter + 5; i++)
  141.             {
  142.                 if (i > Traffic.Length - 1)
  143.                 {
  144.                     Traffic = null;
  145.                     TrafficListCounter = 0;
  146.                     break;
  147.                 }
  148.                 else
  149.                 {
  150.                     Vehicle veh = Traffic[TrafficListCounter];
  151.                     if (CanWeUse(veh) && !SwapWatcher.ContainsKey(veh) && SoundRelationships.ContainsKey(veh.Model))
  152.                     {
  153.                         SwapWatcher.Add(veh, -2);
  154.                     }
  155.  
  156.                     TrafficListCounter++;
  157.  
  158.                 }
  159.             }
  160.         }
  161.  
  162.  
  163.     }
  164.     public static float MPHtoMS(float mph)
  165.     {
  166.         return (float)Math.Round(mph * 0.44704f, 1);
  167.     }
  168.     void SwapEngineSound(Vehicle v, string sound)
  169.     {
  170.         Function.Call(Hash._0x4F0C413926060B38, v, sound);
  171.  
  172.         if (NoRadioOnPolice && IsPoliceVehicle(v))
  173.         {
  174.             v.RadioStation = RadioStation.RadioOff;
  175.             if (Debug) UI.Notify(v.FriendlyName + " is a cop car, Sound Swapper will keep its radio Off.");
  176.         }
  177.     }
  178.  
  179.     public static bool IsPoliceVehicle(Vehicle veh)
  180.     {
  181.         Vector3 vehpos = veh.Position;
  182.         float radius = veh.Model.GetDimensions().Y / 2;
  183.         return Function.Call<bool>(Hash.IS_COP_VEHICLE_IN_AREA_3D, vehpos.X + radius, vehpos.Y + radius, vehpos.Z + radius, vehpos.X - radius, vehpos.Y - radius, vehpos.Z - radius);
  184.     }
  185.  
  186.     protected override void Dispose(bool dispose)
  187.     {
  188.  
  189.  
  190.         base.Dispose(dispose);
  191.     }
  192.  
  193.  
  194.  
  195.     bool NoRadioOnPolice = false;
  196.     bool TrafficToo = false;
  197.  
  198.  
  199.     void LoadSettings()
  200.     {
  201.  
  202.  
  203.         if (File.Exists(@"scripts\AutoEngineSoundSwap.ini"))
  204.         {
  205.             ScriptSettings config = ScriptSettings.Load(@"scripts\AutoEngineSoundSwap.ini");
  206.             TrafficToo = config.GetValue<bool>("SETTINGS", "ApplyToTraffic", false);
  207.             NoRadioOnPolice = config.GetValue<bool>("SETTINGS", "NoRadioOnCopCars", false);
  208.             Debug = config.GetValue<bool>("SETTINGS", "Debug", false);
  209.         }
  210.  
  211.         foreach (string t in Directory.GetFiles(@"scripts\SoundSwaps"))
  212.         {
  213.             XmlDocument file = new XmlDocument();
  214.             file.Load(t);
  215.             foreach (XmlElement swapset in file.DocumentElement.ChildNodes)
  216.             {
  217.                 SwapInfo s = new SwapInfo();
  218.                 s.ModCategory = int.Parse(swapset.GetAttribute("ModType"));
  219.                 foreach (XmlElement swap in swapset)
  220.                 {
  221.                     if (swap.HasAttribute("EngineSound")) s.ModIDtoEngineSound.Add(int.Parse(swap.GetAttribute("ModID")), swap.GetAttribute("EngineSound"));
  222.                     if (swap.HasAttribute("PowerMult")) s.ModIDtoSpeedPlus.Add(int.Parse(swap.GetAttribute("ModID")), int.Parse(swap.GetAttribute("PowerMult")));
  223.                 }
  224.                 SoundRelationships.Add(new Model(System.IO.Path.GetFileNameWithoutExtension(t)), s);
  225.  
  226.             }
  227.         }
  228.  
  229.     }
  230.  
  231.     void WarnPlayer(string script_name, string title, string message)
  232.     {
  233.         Function.Call(Hash._SET_NOTIFICATION_TEXT_ENTRY, "STRING");
  234.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, message);
  235.         Function.Call(Hash._SET_NOTIFICATION_MESSAGE, "CHAR_SOCIAL_CLUB", "CHAR_SOCIAL_CLUB", true, 0, title, "~b~" + script_name);
  236.     }
  237.  
  238.     bool CanWeUse(Entity entity)
  239.     {
  240.         return entity != null && entity.Exists();
  241.     }
  242.  
  243.     void DisplayHelpTextThisFrame(string text)
  244.     {
  245.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  246.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  247.         Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, false, true, -1);
  248.     }
  249.  
  250.  
  251. }
  252.  
  253. public class SwapInfo
  254. {
  255.     public int ModCategory;
  256.     public Dictionary<int, string> ModIDtoEngineSound = new Dictionary<int, string>();
  257.     public Dictionary<int, int> ModIDtoSpeedPlus = new Dictionary<int, int>();
  258. }
RAW Paste Data