Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTA;
- using GTA.Math;
- using GTA.Native;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Windows.Forms;
- using System.Runtime.Serialization;
- using System.Xml;
- public class AutoEngineSoundSwap : Script
- {
- Dictionary<Model, SwapInfo> SoundRelationships = new Dictionary<Model, SwapInfo>();
- string ScriptName = "Auto Engine Sound Swap";
- string ScriptVer = "1.0";
- Dictionary<Vehicle, int> SwapWatcher = new Dictionary<Vehicle, int>();
- Vehicle[] Traffic;
- bool Debug = false;
- public AutoEngineSoundSwap()
- {
- Tick += OnTick;
- LoadSettings();
- }
- public static bool WasCheatStringJustEntered(string cheat)
- {
- return Function.Call<bool>(Hash._0x557E43C447E700A8, Game.GenerateHash(cheat));
- }
- int Timer = 0;
- int TrafficListCounter = 0;
- int LastEngineUpgrade = 0;
- void OnTick(object sender, EventArgs e)
- {
- if (WasCheatStringJustEntered("assdebug"))
- {
- Debug = !Debug;
- if (Debug) UI.Notify(ScriptName + " debug ~g~enabled~w~."); else UI.Notify(ScriptName + " debug ~b~disabled~w~.");
- }
- if (Timer < Game.GameTime)
- {
- Timer = Game.GameTime + 500;
- HandleSwaps();
- }
- // Sets the car top speed. Its not engine limited, seems to be force-based.
- // Function.Call((Hash)0xBAA045B4E42F3C06, Game.Player.Character.CurrentVehicle, Game.Player.Character.CurrentVehicle.Velocity.Length());
- // Modifies the engine top speed.
- //Function.Call((Hash)0x93A3996368C94158, Game.Player.Character.CurrentVehicle, 20 - Game.Player.Character.CurrentVehicle.Velocity.Length());
- }
- int HandleTuningSwaps(Vehicle v, int lastMod)
- {
- if (CanWeUse(v) && SoundRelationships.ContainsKey(v.Model))
- {
- int m = v.GetMod((VehicleMod)SoundRelationships[v.Model].ModCategory);
- if (m != lastMod)
- {
- if (SoundRelationships[v.Model].ModIDtoEngineSound.ContainsKey(m))
- {
- SwapEngineSound(v, SoundRelationships[v.Model].ModIDtoEngineSound[m]);
- if (Debug) UI.Notify("~b~" + v.FriendlyName + "~w~'s engine its swapped for a ~b~" + SoundRelationships[v.Model].ModIDtoEngineSound[m] + "~w~ engine.");
- if (m == -1)
- {
- Function.Call((Hash)0x93A3996368C94158, v,0f);
- v.EnginePowerMultiplier = 1f;
- }
- else
- {
- float myMaxSpeed = Function.Call<float>(Hash._0x53AF99BAA671CA47, v);
- float maxSpeed = Function.Call<float>(Hash._0xF417C2502FFFED43, Game.GenerateHash(SoundRelationships[v.Model].ModIDtoEngineSound[m]));
- float bonusPower = (Function.Call<float>(Hash._0x8C044C5C84505B6A, Game.GenerateHash(SoundRelationships[v.Model].ModIDtoEngineSound[m])))*100; //- Function.Call<float>(Hash._0x5DD35C8D074E57AE, v)
- if (bonusPower > 0f) v.EnginePowerMultiplier = bonusPower; else v.EnginePowerMultiplier = -bonusPower;
- }
- }
- /*
- if (SoundRelationships[v.Model].ModIDtoSpeedPlus.ContainsKey(m))
- {
- Function.Call((Hash)0x93A3996368C94158, v, (float)SoundRelationships[v.Model].ModIDtoSpeedPlus[m]);
- if (Debug) UI.Notify(v.FriendlyName + "'s engine upgraded by " + SoundRelationships[v.Model].ModIDtoSpeedPlus[m] + "");
- }
- else
- {
- Function.Call((Hash)0x93A3996368C94158, v, 0f);
- }
- */
- return m;
- }
- //DisplayHelpTextThisFrame("Max speed: " + Function.Call<int>(Hash._0x90A38E9838E0A8C1, Game.Player.Character.CurrentVehicle, 0, 0).ToString());
- }
- return -2;
- }
- void HandleSwaps()
- {
- foreach (Vehicle v in SwapWatcher.Keys)
- {
- if (CanWeUse(v))
- {
- int n = HandleTuningSwaps(v, SwapWatcher[v]);
- if (n > -2)
- {
- SwapWatcher[v] = n;
- break;
- }
- if (NoRadioOnPolice && IsPoliceVehicle(v) && Function.Call<bool>(Hash._0x5F43D83FD6738741)) v.RadioStation = RadioStation.RadioOff;
- }
- else
- {
- SwapWatcher.Remove(v);
- break;
- }
- }
- Vehicle PlayerVeh = Game.Player.Character.CurrentVehicle;
- if (CanWeUse(PlayerVeh) && !SwapWatcher.ContainsKey(PlayerVeh) && SoundRelationships.ContainsKey(PlayerVeh.Model))
- {
- SwapWatcher.Add(PlayerVeh, -2);
- }
- if (Traffic == null)
- {
- if (TrafficToo) Traffic = World.GetAllVehicles();
- }
- else
- {
- for (int i = TrafficListCounter; i < TrafficListCounter + 5; i++)
- {
- if (i > Traffic.Length - 1)
- {
- Traffic = null;
- TrafficListCounter = 0;
- break;
- }
- else
- {
- Vehicle veh = Traffic[TrafficListCounter];
- if (CanWeUse(veh) && !SwapWatcher.ContainsKey(veh) && SoundRelationships.ContainsKey(veh.Model))
- {
- SwapWatcher.Add(veh, -2);
- }
- TrafficListCounter++;
- }
- }
- }
- }
- public static float MPHtoMS(float mph)
- {
- return (float)Math.Round(mph * 0.44704f, 1);
- }
- void SwapEngineSound(Vehicle v, string sound)
- {
- Function.Call(Hash._0x4F0C413926060B38, v, sound);
- if (NoRadioOnPolice && IsPoliceVehicle(v))
- {
- v.RadioStation = RadioStation.RadioOff;
- if (Debug) UI.Notify(v.FriendlyName + " is a cop car, Sound Swapper will keep its radio Off.");
- }
- }
- public static bool IsPoliceVehicle(Vehicle veh)
- {
- Vector3 vehpos = veh.Position;
- float radius = veh.Model.GetDimensions().Y / 2;
- 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);
- }
- protected override void Dispose(bool dispose)
- {
- base.Dispose(dispose);
- }
- bool NoRadioOnPolice = false;
- bool TrafficToo = false;
- void LoadSettings()
- {
- if (File.Exists(@"scripts\AutoEngineSoundSwap.ini"))
- {
- ScriptSettings config = ScriptSettings.Load(@"scripts\AutoEngineSoundSwap.ini");
- TrafficToo = config.GetValue<bool>("SETTINGS", "ApplyToTraffic", false);
- NoRadioOnPolice = config.GetValue<bool>("SETTINGS", "NoRadioOnCopCars", false);
- Debug = config.GetValue<bool>("SETTINGS", "Debug", false);
- }
- foreach (string t in Directory.GetFiles(@"scripts\SoundSwaps"))
- {
- XmlDocument file = new XmlDocument();
- file.Load(t);
- foreach (XmlElement swapset in file.DocumentElement.ChildNodes)
- {
- SwapInfo s = new SwapInfo();
- s.ModCategory = int.Parse(swapset.GetAttribute("ModType"));
- foreach (XmlElement swap in swapset)
- {
- if (swap.HasAttribute("EngineSound")) s.ModIDtoEngineSound.Add(int.Parse(swap.GetAttribute("ModID")), swap.GetAttribute("EngineSound"));
- if (swap.HasAttribute("PowerMult")) s.ModIDtoSpeedPlus.Add(int.Parse(swap.GetAttribute("ModID")), int.Parse(swap.GetAttribute("PowerMult")));
- }
- SoundRelationships.Add(new Model(System.IO.Path.GetFileNameWithoutExtension(t)), s);
- }
- }
- }
- void WarnPlayer(string script_name, string title, string message)
- {
- Function.Call(Hash._SET_NOTIFICATION_TEXT_ENTRY, "STRING");
- Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, message);
- Function.Call(Hash._SET_NOTIFICATION_MESSAGE, "CHAR_SOCIAL_CLUB", "CHAR_SOCIAL_CLUB", true, 0, title, "~b~" + script_name);
- }
- bool CanWeUse(Entity entity)
- {
- return entity != null && entity.Exists();
- }
- void DisplayHelpTextThisFrame(string text)
- {
- Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
- Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
- Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, false, true, -1);
- }
- }
- public class SwapInfo
- {
- public int ModCategory;
- public Dictionary<int, string> ModIDtoEngineSound = new Dictionary<int, string>();
- public Dictionary<int, int> ModIDtoSpeedPlus = new Dictionary<int, int>();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement