Advertisement
Eddlm

Racer.cs class

Aug 30th, 2016
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GTA;
  7. using GTA.Native;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using GTA.Math;
  11. using NativeUI;
  12. namespace Street_Races
  13. {
  14.     public class Racer
  15.     {
  16.         public Vehicle car;
  17.         public Ped driver;
  18.  
  19.         //Waypoint & Position
  20.         public int CurrentWaypoint = 0;
  21.         public float DistanceToWaypoint = 0;
  22.         public int PositionInRace = 1; //Handled by StreetRacing.HandlePositions();
  23.         public int CurrentLap = 0;
  24.         public int CurrentCheckpoint = 0;
  25.         public string Name ="Racer";
  26.  
  27.         bool OutOfRace = false;
  28.  
  29.         //Nitro
  30.         float NitroBar = 100;
  31.         int NitroSafety = 0;
  32.         int IntervalBetweenNitros = 2000;
  33.  
  34.         int NitroTimeLimit=Game.GameTime;
  35.  
  36.         public Racer(Vehicle model, Ped racer, string name)
  37.         {
  38.             if (Util.CanWeUse(racer))
  39.             {
  40.                 Name = name;
  41.                 car = racer.CurrentVehicle;
  42.                 driver = racer;
  43.                 driver.SetIntoVehicle(car, VehicleSeat.Driver);
  44.  
  45.                 if (!driver.IsPlayer)
  46.                 {
  47.                     Function.Call(GTA.Native.Hash.SET_DRIVER_ABILITY, driver, 100.0);
  48.                     Function.Call(GTA.Native.Hash.SET_DRIVER_AGGRESSIVENESS, driver, 0);
  49.                     driver.AlwaysKeepTask = true;
  50.                     driver.BlockPermanentEvents = true;
  51.  
  52.                     Blip blip = car.AddBlip();
  53.                     blip.Color = BlipColor.Blue;
  54.  
  55.                     Function.Call(GTA.Native.Hash._0x0DC7CABAB1E9B67E, driver, true); //Load Collision
  56.                     Function.Call(GTA.Native.Hash._0x0DC7CABAB1E9B67E, car, true); //Load Collision
  57.  
  58.                     car.EngineRunning = true;
  59.                 }
  60.  
  61.             }
  62.         }
  63.  
  64.         public void ProcessRace()
  65.         {
  66.  
  67.                 switch (StreetRaces.RaceStatus)
  68.                 {
  69.                     case RacePhase.NotRacing:
  70.                         {
  71.  
  72.                             break;
  73.                         }
  74.                     case RacePhase.RaceSetup:
  75.                         {
  76.  
  77.                             break;
  78.                         }
  79.                 case RacePhase.RaceCountDown:
  80.                     {
  81.                         if (!driver.IsPlayer && car.IsConvertible)
  82.                         {
  83.                             if (Util.IsRaining() && car.RoofState == VehicleRoofState.Opened) car.RoofState = VehicleRoofState.Closing;
  84.                             else if(car.RoofState == VehicleRoofState.Closed) car.RoofState = VehicleRoofState.Opening;
  85.                         }
  86.                         break;
  87.                     }
  88.                 case RacePhase.RaceInProgress:
  89.                         {
  90.                             HandleRacing();
  91.                             break;
  92.                         }
  93.  
  94.             }
  95.         }
  96.         public void Delete()
  97.         {
  98.             if (!driver.IsPlayer)
  99.             {
  100.                 if (car.CurrentBlip != null) car.CurrentBlip.Remove();
  101.                 driver.MarkAsNoLongerNeeded();
  102.                 car.MarkAsNoLongerNeeded();
  103.             }
  104.         }
  105.  
  106.  
  107.         public void HandleRacing()
  108.         {
  109.             //Debug notifications
  110.             bool shouldnotify = false;
  111.             if (StreetRaces.Debug && car.IsNearEntity(Game.Player.Character, new Vector3(30, 30, 30))) shouldnotify = true;
  112.  
  113.             string notification = "Lap: "+CurrentLap+"/"+StreetRaces.Laps+" | Waypoint: " +CurrentWaypoint;
  114.  
  115.             if (!driver.IsPlayer)
  116.             {
  117.                 //AI Helpers
  118.                if(StreetRaces.AICurvePathing.Checked)  Util.HandleAIOutOfPath(car,true);
  119.                 if (StreetRaces.AIRearEnd.Checked) Util.HandleAIRearEndHelp(car);
  120.                 if (StreetRaces.AISpinout.Checked) Util.HandleAISpinoutHelp(car);
  121.                 if (StreetRaces.AISlamToWall.Checked) Util.HandleAISlamToWall(car);
  122.  
  123.                 Vector3 playerpos = Game.Player.Character.Position;
  124.                 if (!driver.IsPlayer && !car.IsOnScreen && Util.IsPlayerParticipating() && this.PositionInRace > StreetRaces.Racers[StreetRaces.Racers.Count - 1].PositionInRace && Function.Call<Vector3>(Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, driver, playerpos.X, playerpos.Y, playerpos.Z).Y > 20f) //
  125.                 {
  126.                     if (!this.car.IsInvincible) this.car.IsInvincible = true;
  127.                     Util.HandleAICatchUp(car);
  128.                 }else if (this.car.IsInvincible) this.car.IsInvincible = false;
  129.  
  130.                 //Vehicle stuck handler
  131.                 if (car.IsUpsideDown && car.IsStopped) car.PlaceOnGround();
  132.  
  133.  
  134.                 if (StreetRaces.UseNitro.Checked)
  135.                 {
  136.  
  137.                     if (NitroTimeLimit > Game.GameTime)
  138.                     {
  139.                         //Calculate safety when nitroing
  140.                         if (Util.IsSliding(car, 1f) || Util.IsPotentiallySliding(car, 1f))
  141.                         {
  142.                             if (NitroSafety > 40) NitroSafety = NitroSafety - 5;
  143.                         }
  144.                     }
  145.                     else
  146.                     {
  147.                         //Calculate safety when not nitroing
  148.                         if (Util.IsSliding(car, 0.2f) || Util.IsPotentiallySliding(car, 0.3f))
  149.                         {
  150.                             if (NitroSafety > 40) NitroSafety = NitroSafety - 5;
  151.                         }
  152.                         else if (NitroSafety < 100)
  153.                         {
  154.                             if (car.CurrentGear < 4) NitroSafety = NitroSafety + 2;
  155.                             else NitroSafety++;
  156.                         }
  157.                     }
  158.  
  159.                     //dev info
  160.                     if (shouldnotify)
  161.                     {
  162.                         notification = notification + "~n~Nitro bar: " + NitroBar + "%";
  163.                         string safety = "~r~" + NitroSafety + "%";
  164.                         if (NitroSafety > 10) safety = "~y~" + NitroSafety + "%";
  165.                         if (NitroSafety > 50) safety = "~g~" + NitroSafety + "%";
  166.                         notification = notification + "~n~Nitro Safety: " + safety + "~w~";
  167.                     }
  168.  
  169.  
  170.                     //Nitro cancel conditions
  171.                     if (NitroBar < 0 || Util.IsSliding(car, 2f) || Util.IsPotentiallySliding(car, 1f)) NitroTimeLimit = Game.GameTime;
  172.  
  173.  
  174.                     //Apply the nitro
  175.                     if (NitroTimeLimit > Game.GameTime && car.CurrentGear > 1 && car.Acceleration > 0 && NitroBar > 0)
  176.                     {
  177.                         notification = notification + "~n~Nitroing";
  178.                         Util.ForceNitro(car);
  179.                         NitroBar--;
  180.                     }//Nitro bar recharge
  181.                     else if (NitroBar < 100) NitroBar = NitroBar + 0.2f;
  182.  
  183.                     if (NitroSafety > 50 && NitroTimeLimit + IntervalBetweenNitros < Game.GameTime) NitroTimeLimit = Game.GameTime + (Util.GetRandomInt(5, 10) * 1000);
  184.                 }
  185.                 else shouldnotify = false;
  186.  
  187.             }
  188.  
  189.  
  190.             //Waypoint following
  191.             if (driver.IsAlive && driver.IsInVehicle(car))
  192.             {
  193.                 if (StreetRaces.RaceWaypoints.Count > 0 && CurrentLap <= StreetRaces.Laps)
  194.                 {
  195.                     if ((!driver.IsPlayer && !Util.IsDriving(driver)) || (driver.IsPlayer && driver.Position.DistanceTo(StreetRaces.RaceWaypoints[CurrentWaypoint]) < 20f))
  196.                     {
  197.                         int DrivingStyle = 4 + 8 + 512 + 262144;
  198.                         float speed = 120f;
  199.                         CurrentCheckpoint++;
  200.                         if (CurrentWaypoint == StreetRaces.RaceWaypoints.Count - 1)
  201.                         {
  202.                             CurrentWaypoint = 0;
  203.                             if (!driver.IsPlayer)
  204.                             {
  205.                                 Vector3 pos = StreetRaces.RaceWaypoints[CurrentWaypoint];
  206.                                 //driver.Task.DriveTo(car, pos, 15f, speed, DrivingStyle);
  207.                                 Function.Call(Hash.TASK_VEHICLE_DRIVE_TO_COORD_LONGRANGE, driver, car, pos.X, pos.Y, pos.Z, speed, DrivingStyle, 15f);
  208.                             }
  209.                         }
  210.                         else
  211.                         {
  212.                             if ((driver.Position.DistanceTo(StreetRaces.RaceWaypoints[CurrentWaypoint]) < 20f || (CurrentLap == 0 && CurrentWaypoint == 0)))
  213.                             {
  214.                                 //Add a lap
  215.                                 if (CurrentWaypoint == 0)
  216.                                 {
  217.                                     CurrentCheckpoint = 0;
  218.                                     CurrentLap++;
  219.                                     if (CurrentLap > StreetRaces.Laps) StreetRaces.Winner = this; else if (driver.IsPlayer && CurrentLap>1) BigMessageThread.MessageInstance.ShowColoredShard(CurrentLap.ToString(), "", HudColor.HUD_COLOUR_BLACK, HudColor.HUD_COLOUR_MENU_YELLOW, 800);
  220.                                 }
  221.                                 CurrentWaypoint++;
  222.                                 if (Util.CarCanSeePos(car, StreetRaces.RaceWaypoints[CurrentWaypoint])) DrivingStyle = 4 + 8 + 512 + 262144 + 4194304;
  223.                                 if (Util.ForwardSpeed(car) > 20f && StreetRaces.RaceDangerousWaypoints.Contains(StreetRaces.RaceWaypoints[CurrentWaypoint])) speed = car.Speed / 1.5f;
  224.  
  225.                                 if (!driver.IsPlayer)
  226.                                 {
  227.                                     Vector3 pos = StreetRaces.RaceWaypoints[CurrentWaypoint];
  228.                                     //driver.Task.DriveTo(car, pos, 15f, speed, DrivingStyle);
  229.                                     Function.Call(Hash.TASK_VEHICLE_DRIVE_TO_COORD_LONGRANGE, driver, car, pos.X, pos.Y, pos.Z, speed, DrivingStyle, 15f);
  230.                                 }
  231.                             }
  232.                         }
  233.                        //if(driver.IsPlayer) Util.FlareUpNextCheckpoint(StreetRaces.RaceWaypoints[CurrentWaypoint]);
  234.                     }
  235.                 }
  236.             }
  237.             else
  238.             {
  239.                 if (!OutOfRace)
  240.                 {
  241.                     car.LeftIndicatorLightOn = true;
  242.                     car.RightIndicatorLightOn = true;
  243.                     Util.WarnPlayer(StreetRaces.ScriptName, "RACER OUT", Name + " is out of the race.");
  244.                     OutOfRace = true;
  245.                 }
  246.  
  247.             }
  248.             DistanceToWaypoint = (float)Math.Round(car.Position.DistanceTo(StreetRaces.RaceWaypoints[CurrentWaypoint]));
  249.  
  250.             if (driver.IsPlayer)
  251.             {
  252.                 shouldnotify = false;
  253.             }
  254.             if (shouldnotify) Util.DisplayHelpTextThisFrame(notification); else if (driver.IsPlayer) Util.DisplayHelpTextThisFrame("Lap: "+ CurrentLap+"/"+StreetRaces.Laps+" | Waypoint: "+CurrentWaypoint+ " | Pos: " + PositionInRace);
  255.         }
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement