Advertisement
Eddlm

Hollywood Rollover

Jan 8th, 2016
2,801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.37 KB | None | 0 0
  1. using GTA;
  2. using GTA.Math;
  3. using GTA.Native;
  4. using System;
  5. using System.Windows.Forms;
  6.  
  7. enum WeatherType
  8. {
  9.     EXTRA_SUNNY = -1750463879,
  10.     CLEAR = 916995460,
  11.     NEUTRAL = -1530260698,
  12.     SMOG = 282916021,
  13.     FOGGY = -1368164796,
  14.     OVERCAST = -1148613331,
  15.     CLOUDS = 821931868,
  16.     CLEARING = 1840358669,
  17.     RAIN = 1420204096,
  18.     THUNDER = -1233681761,
  19.     SNOW = -273223690,
  20.     BLIZZARD = 669657108,
  21.     LIGHT_SNOW = 603685163,
  22.     X_MAS = -1429616491
  23. };
  24.  
  25. enum VehicleClass
  26. {
  27.     COMPACT=0,
  28.     SEDAN,
  29.     SUV,
  30.     COUPE,
  31.     MUSCLE,
  32.     SPORTCLASSIC,
  33.     SPORT,
  34.     SUPER,
  35.     MOTORCYCLE,
  36.     OFFROAD,
  37.     INDUSTRIAL,
  38.     UTILITY,
  39.     VAN_OR_PICKUP,
  40.     BICYCLE,
  41.     BOAT,
  42.     HELICOPTER,
  43.     AIRPLANE,
  44.     SERVICE,
  45.     EMERGENCY,
  46.     MILITARY,
  47.     COMMERTIAL,
  48. }
  49.  
  50.  
  51. public class HollywoodRollover : Script
  52. {
  53.     bool AffectPlayer = true;
  54.     bool AffectNPCs = true;
  55.     bool Debug = false;
  56.     bool CanRollover = true;
  57.  
  58.     int ReferenceTime=Game.GameTime;
  59.     int NPCBaseProb = 0;
  60.     int PlayerBaseProb =0;
  61.     int ExplosiveRolloverProb=0;
  62.     int RolloverCooldownTime = Game.GameTime;
  63.  
  64.     public HollywoodRollover()
  65.     {
  66.         Tick += OnTick;
  67.         KeyDown += OnKeyDown;
  68.         KeyUp += OnKeyUp;
  69.         LoadSettings();
  70.     }
  71.  
  72.  
  73.     void HandleRoll()
  74.     {
  75.         if (AffectPlayer)
  76.         {
  77.             if (CanWeUse(GetLastVehicle(Game.Player.Character)) && GetLastVehicle(Game.Player.Character).IsOnAllWheels  && Math.Abs(Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR, GetLastVehicle(Game.Player.Character), true).X) > 15f && Math.Abs(Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR, GetLastVehicle(Game.Player.Character), true).Y) < 15f)
  78.             {
  79.                 if (Debug)
  80.                 {
  81.                     DisplayHelpTextThisFrame(GetRolloverProb(GetLastVehicle(Game.Player.Character)).ToString() + "% prob rollover, " + (ExplosiveRolloverProb + (1000f - (int)GetLastVehicle(Game.Player.Character).BodyHealth) / 10) + "% prob explosive");
  82.                 }
  83.  
  84.                 if (GetRolloverProb(GetLastVehicle(Game.Player.Character)) > RandomInt(30,100))
  85.                 {
  86.                     RolloverCooldownTime = Game.GameTime;
  87.                     CanRollover = false;
  88.  
  89.                     float force = Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR, GetLastVehicle(Game.Player.Character), true).X;
  90.                     Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, 0f, 0f, 3f, 0f, 0f, 0f, 0, true, true, true, true, true);
  91.                     Script.Wait(20);
  92.                     Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, (force / 5), 0f, 0f, 0f, 0f, 30f, 0, true, true, true, true, true);
  93.                     Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, (force / 5) * -1, 0f, 0f, 0f, 0f, -30f, 0, true, true, true, true, true);
  94.                     if (ExplosiveRolloverProb > 0 && (ExplosiveRolloverProb + (1000f - GetLastVehicle(Game.Player.Character).BodyHealth) / 10) > RandomInt(50, 100))
  95.                     {
  96.                         Function.Call<int>(GTA.Native.Hash.SET_VEHICLE_OUT_OF_CONTROL, GetLastVehicle(Game.Player.Character),false,true);
  97.                         Script.Wait(40);
  98.                         Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, 0f, 0f, 2f, 0f, 0f, 0f, 0, false, true, true, true, true);
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.         if (AffectNPCs)
  104.         {
  105.             foreach(Vehicle veh in World.GetNearbyVehicles(Game.Player.Character, 30f))
  106.             {
  107.                 if(veh.Handle != GetLastVehicle(Game.Player.Character).Handle)
  108.                 {
  109.                     if (veh.IsOnAllWheels && Function.Call<Vector3>(Hash.GET_ENTITY_ROTATION_VELOCITY, veh).Z < 1f && Math.Abs(Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR, veh, true).X) > 10f && Math.Abs(Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR,veh, true).Y) < 14f)
  110.                     {
  111.                         if (Debug)
  112.                         {
  113.                             DisplayHelpTextThisFrame(GetRolloverProb(veh).ToString() + "% prob rollover, " + (ExplosiveRolloverProb + (1000f - (int)veh.BodyHealth) / 10) + "% prob explosive");
  114.                         }
  115.                         if (GetRolloverProb(veh) > RandomInt(30, 100))
  116.                         {
  117.                             RolloverCooldownTime = Game.GameTime;
  118.                             CanRollover = false;
  119.  
  120.                             float force = Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR, veh, true).X;
  121.                             Function.Call(Hash.APPLY_FORCE_TO_ENTITY, veh, 3, 0f, 0f, 3f, 0f, 0f, 0f, 0, true, true, true, true, true);
  122.                             Script.Wait(40);
  123.                             Function.Call(Hash.APPLY_FORCE_TO_ENTITY, veh, 3, (force / 5), 0f, 0f, 0f, 0f, 30f, 0, true, true, true, true, true);
  124.                             Function.Call(Hash.APPLY_FORCE_TO_ENTITY, veh, 3, (force / 5) * -1, 0f, 0f, 0f, 0f, -30f, 0, true, true, true, true, true);
  125.  
  126.                             if (ExplosiveRolloverProb >0 && (ExplosiveRolloverProb + (1000f - veh.BodyHealth) / 10) > RandomInt(50, 100))
  127.                             {
  128.                                 Function.Call<int>(GTA.Native.Hash.SET_VEHICLE_OUT_OF_CONTROL, veh, true, true);
  129.                                 Script.Wait(40);
  130.                                 Function.Call(Hash.APPLY_FORCE_TO_ENTITY, veh, 3, (force / 5), 0f, 0f, 0f, 0f, 30f, 0, true, true, true, true, true);
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.     }
  138.  
  139.     void OnTick(object sender, EventArgs e)
  140.     {
  141.         if (Game.GameTime > ReferenceTime + 500)
  142.         {
  143.             ReferenceTime = Game.GameTime;
  144.             if(CanRollover) HandleRoll();
  145.         }
  146.    
  147.         if(Game.GameTime > RolloverCooldownTime + 5000 && !CanRollover)
  148.         {
  149.             CanRollover = true;
  150.         }
  151.     }
  152.  
  153.     void OnKeyDown(object sender, KeyEventArgs e)
  154.     {
  155.  
  156.     }
  157.  
  158.     void OnKeyUp(object sender, KeyEventArgs e)
  159.     {
  160.         if (e.KeyCode == Keys.Enter)
  161.         {
  162.             LoadSettings();
  163.         }
  164.     }
  165.  
  166.     bool CanWeUse(Entity entity)
  167.     {
  168.         return entity != null && entity.Exists();
  169.     }
  170.  
  171.     int RandomInt(int min, int max)
  172.     {
  173.         max++;
  174.         return Function.Call<int>(GTA.Native.Hash.GET_RANDOM_INT_IN_RANGE, min, max);
  175.     }
  176.  
  177.  
  178.     Vehicle GetLastVehicle(Ped RecieveOrder)
  179.     {
  180.         Vehicle vehicle = null;
  181.         if (GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, true) != null)
  182.         {
  183.             vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, true);
  184.             if (vehicle.IsAlive)
  185.             {
  186.                 return vehicle;
  187.             }
  188.         }
  189.         else
  190.         {
  191.             if (GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, false) != null)
  192.             {
  193.                 vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, false);
  194.                 if (vehicle.IsAlive)
  195.                 {
  196.                     return vehicle;
  197.                 }
  198.             }
  199.         }
  200.         return vehicle;
  201.     }
  202.  
  203.  
  204.     void LoadSettings()
  205.     {
  206.         ScriptSettings config = ScriptSettings.Load(@"scripts\HollywoodRollover.ini");
  207.  
  208.         AffectNPCs = config.GetValue<bool>("SETTINGS", "AffectNPCs", true);
  209.         AffectPlayer = config.GetValue<bool>("SETTINGS", "AffectPlayer", true);
  210.         PlayerBaseProb = config.GetValue<int>("SETTINGS", "PlayerBaseProb", 0);
  211.         NPCBaseProb = config.GetValue<int>("SETTINGS", "NPCBaseProb", 0);
  212.         ExplosiveRolloverProb = config.GetValue<int>("SETTINGS", "ExplosiveRolloverProb", 0);
  213.         Debug = config.GetValue<bool>("SETTINGS", "Debug", true);
  214.  
  215.     }
  216.  
  217.     bool IsRaining()
  218.     {
  219.         int weather = Function.Call<int>(GTA.Native.Hash._0x564B884A05EC45A3); //get current weather hash
  220.         switch (weather)
  221.         {
  222.             case (int)WeatherType.BLIZZARD:
  223.                 {
  224.                     return true;
  225.                 }
  226.             case (int)WeatherType.CLEARING:
  227.                 {
  228.                     return true;
  229.                 }
  230.             case (int)WeatherType.FOGGY:
  231.                 {
  232.                     return true;
  233.                 }
  234.             case (int)WeatherType.RAIN:
  235.                 {
  236.                     return true;
  237.                 }
  238.             case (int)WeatherType.NEUTRAL:
  239.                 {
  240.                     return true;
  241.                 }
  242.             case (int)WeatherType.THUNDER:
  243.                 {
  244.                     return true;
  245.                 }
  246.             case (int)WeatherType.LIGHT_SNOW:
  247.                 {
  248.                     return true;
  249.                 }
  250.             case (int)WeatherType.SNOW:
  251.                 {
  252.                     return true;
  253.                 }
  254.             case (int)WeatherType.X_MAS:
  255.                 {
  256.                     return true;
  257.                 }
  258.         }
  259.         return false;
  260.     }
  261.  
  262.  
  263.     int GetRolloverProb(Vehicle veh)
  264.     {
  265.         int prob = 0;
  266.         if (veh.Handle == GetLastVehicle(Game.Player.Character).Handle)
  267.         {
  268.              prob = PlayerBaseProb;
  269.         }
  270.         else
  271.         {
  272.              prob = NPCBaseProb;
  273.         }
  274.         Vector3 min;
  275.         Vector3 max;
  276.         veh.Model.GetDimensions(out min, out max);
  277.  
  278.         prob += (int)((((max - min).X) * -3));
  279.         prob += (int)((((max - min).Y) * 3));
  280.         prob += (int)((((max - min).Z) * 3));
  281.         prob += (int)(1000f - veh.BodyHealth) / 10;
  282.         prob += (int)(veh.HeightAboveGround*3);
  283.  
  284.         if (Function.Call<bool>(Hash.IS_POINT_ON_ROAD, veh.Position.X, veh.Position.Y, veh.Position.Z, veh) == false)
  285.         {
  286.             prob += 20;
  287.         }
  288.  
  289.         if (IsRaining()) prob = prob / 2;
  290.         if (prob > 100) prob = 100;
  291.  
  292.         return prob;
  293.     }
  294.  
  295.  
  296.     void DisplayHelpTextThisFrame(string text)
  297.     {
  298.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  299.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  300.         Function.Call(Hash._0x238FFE5C7B0498A6, 0, 0, 1, -1);
  301.     }
  302.  
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement