Advertisement
Eddlm

Singleplayer KERS

Aug 6th, 2016
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  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.Xml;
  9.  
  10. public class KERS : Script
  11. {
  12.     string ScriptName = "KERS for all cars";
  13.     string ScriptVer = "1.0";
  14.     float KERSEnergy;
  15.     int GameTimeRef = Game.GameTime;
  16.     bool BoostActive=false;
  17.     List<Model> Cars = new List<Model>();
  18.  
  19.     bool KERSNotified = false;
  20.     public KERS()
  21.     {
  22.         Tick += OnTick;
  23.  
  24.  
  25.  
  26.         string ConfigFile = @"scripts\\Kersconfig.xml";
  27.  
  28.         XmlDocument document = new XmlDocument();
  29.         document.Load(ConfigFile);
  30.         int pat = 0;
  31.         while (document == null && pat < 500)
  32.         {
  33.             document.Load(ConfigFile);
  34.             Script.Wait(0);
  35.         }
  36.  
  37.         if (document == null)
  38.         {
  39.             UI.Notify("~o~KERS couldn't find the xml file.");
  40.  
  41.             return;
  42.         }
  43.  
  44.         XmlElement root = document.DocumentElement;
  45.  
  46.         foreach (XmlElement e in root.SelectNodes("//model"))
  47.         {
  48.             Model car = e.InnerText;
  49.             if(car.IsValid && car.IsVehicle)
  50.             {
  51.                 Cars.Add(car);
  52.                 //UI.Notify("[KERS] Added "+car.ToString());
  53.             }
  54.         }
  55.     }
  56.     public static float ForwardSpeed(Entity ent)
  57.     {
  58.         return Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR, ent, true).Y;
  59.     }
  60.     void OnTick(object sender, EventArgs e)
  61.     {
  62.  
  63.         if (GameTimeRef < Game.GameTime)
  64.         {
  65.             GameTimeRef = Game.GameTime + 100;
  66.             if (BoostActive)
  67.             {
  68.                 if (KERSEnergy > 0)
  69.                 {
  70.                     KERSEnergy--;
  71.                     KERSEnergy--;
  72.                 }
  73.                 else
  74.                 {
  75.                     DisplayHelpTextThisFrame("~b~KERS~w~ discharged.");
  76.                     BoostActive = false;
  77.                 }
  78.             }
  79.         }
  80.         if (CanWeUse(Game.Player.Character.CurrentVehicle))
  81.         {            
  82.             Vehicle veh = Game.Player.Character.CurrentVehicle;
  83.             if (BoostActive)
  84.             {
  85.                 veh.EngineTorqueMultiplier = 4;
  86.  
  87.             }          
  88.             if (Cars.Contains(veh.Model))
  89.             {
  90.                 if (!KERSNotified)
  91.                 {
  92.                     DisplayHelpTextThisFrame("~b~KERS~w~ is available for this vehicle.");
  93.                     KERSNotified = true;
  94.                 }
  95.  
  96.                 if (Game.IsControlPressed(2,GTA.Control.VehicleBrake) && !Game.IsControlPressed(2,GTA.Control.VehicleAccelerate) && veh.IsOnAllWheels && ForwardSpeed(veh)>5)
  97.                 {
  98.                     if (KERSEnergy < 100) KERSEnergy += 0.2f;
  99.                 }
  100.                 if(Game.IsControlJustReleased(2,GTA.Control.VehicleBrake))
  101.                 {
  102.                     if(KERSEnergy<100) DisplayHelpTextThisFrame("~b~KERS~w~ charged [" + Math.Round(KERSEnergy, 0).ToString() + "%].");
  103.                     else DisplayHelpTextThisFrame("~b~KERS~w~ charged [~g~" + Math.Round(KERSEnergy, 0).ToString() + "%~w~].~n~Press ~INPUT_CONTEXT~ to release it.");
  104.                    
  105.                 }
  106.                 if (KERSEnergy>0 && Game.IsControlJustPressed(2, GTA.Control.VehicleHorn))
  107.                 {
  108.                     DisplayHelpTextThisFrame("~b~KERS~w~ injected.");
  109.                     BoostActive = true;
  110.                 }
  111.             }
  112.         }
  113.         else
  114.         {
  115.             if (KERSNotified) KERSNotified = false;
  116.             if (KERSEnergy!=0) KERSEnergy = 0;
  117.             if (BoostActive) BoostActive = false;
  118.         }
  119.     }  
  120.     bool CanWeUse(Entity entity)
  121.     {
  122.         return entity != null && entity.Exists();
  123.     }
  124.  
  125.     void DisplayHelpTextThisFrame(string text)
  126.     {
  127.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  128.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  129.         Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, false, true, -1);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement