Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using GTA;
- using GTA.Native;
- using System.IO;
- using System.Windows.Forms;
- using GTA.Math;
- using NativeUI;
- namespace Street_Races
- {
- public class Racer
- {
- public Vehicle car;
- public Ped driver;
- //Waypoint & Position
- public int CurrentWaypoint = 0;
- public float DistanceToWaypoint = 0;
- public int PositionInRace = 1; //Handled by StreetRacing.HandlePositions();
- public int CurrentLap = 0;
- public int CurrentCheckpoint = 0;
- public string Name ="Racer";
- bool OutOfRace = false;
- //Nitro
- float NitroBar = 100;
- int NitroSafety = 0;
- int IntervalBetweenNitros = 2000;
- int NitroTimeLimit=Game.GameTime;
- public Racer(Vehicle model, Ped racer, string name)
- {
- if (Util.CanWeUse(racer))
- {
- Name = name;
- car = racer.CurrentVehicle;
- driver = racer;
- driver.SetIntoVehicle(car, VehicleSeat.Driver);
- if (!driver.IsPlayer)
- {
- Function.Call(GTA.Native.Hash.SET_DRIVER_ABILITY, driver, 100.0);
- Function.Call(GTA.Native.Hash.SET_DRIVER_AGGRESSIVENESS, driver, 0);
- driver.AlwaysKeepTask = true;
- driver.BlockPermanentEvents = true;
- Blip blip = car.AddBlip();
- blip.Color = BlipColor.Blue;
- Function.Call(GTA.Native.Hash._0x0DC7CABAB1E9B67E, driver, true); //Load Collision
- Function.Call(GTA.Native.Hash._0x0DC7CABAB1E9B67E, car, true); //Load Collision
- car.EngineRunning = true;
- }
- }
- }
- public void ProcessRace()
- {
- switch (StreetRaces.RaceStatus)
- {
- case RacePhase.NotRacing:
- {
- break;
- }
- case RacePhase.RaceSetup:
- {
- break;
- }
- case RacePhase.RaceCountDown:
- {
- if (!driver.IsPlayer && car.IsConvertible)
- {
- if (Util.IsRaining() && car.RoofState == VehicleRoofState.Opened) car.RoofState = VehicleRoofState.Closing;
- else if(car.RoofState == VehicleRoofState.Closed) car.RoofState = VehicleRoofState.Opening;
- }
- break;
- }
- case RacePhase.RaceInProgress:
- {
- HandleRacing();
- break;
- }
- }
- }
- public void Delete()
- {
- if (!driver.IsPlayer)
- {
- if (car.CurrentBlip != null) car.CurrentBlip.Remove();
- driver.MarkAsNoLongerNeeded();
- car.MarkAsNoLongerNeeded();
- }
- }
- public void HandleRacing()
- {
- //Debug notifications
- bool shouldnotify = false;
- if (StreetRaces.Debug && car.IsNearEntity(Game.Player.Character, new Vector3(30, 30, 30))) shouldnotify = true;
- string notification = "Lap: "+CurrentLap+"/"+StreetRaces.Laps+" | Waypoint: " +CurrentWaypoint;
- if (!driver.IsPlayer)
- {
- //AI Helpers
- if(StreetRaces.AICurvePathing.Checked) Util.HandleAIOutOfPath(car,true);
- if (StreetRaces.AIRearEnd.Checked) Util.HandleAIRearEndHelp(car);
- if (StreetRaces.AISpinout.Checked) Util.HandleAISpinoutHelp(car);
- if (StreetRaces.AISlamToWall.Checked) Util.HandleAISlamToWall(car);
- Vector3 playerpos = Game.Player.Character.Position;
- 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) //
- {
- if (!this.car.IsInvincible) this.car.IsInvincible = true;
- Util.HandleAICatchUp(car);
- }else if (this.car.IsInvincible) this.car.IsInvincible = false;
- //Vehicle stuck handler
- if (car.IsUpsideDown && car.IsStopped) car.PlaceOnGround();
- if (StreetRaces.UseNitro.Checked)
- {
- if (NitroTimeLimit > Game.GameTime)
- {
- //Calculate safety when nitroing
- if (Util.IsSliding(car, 1f) || Util.IsPotentiallySliding(car, 1f))
- {
- if (NitroSafety > 40) NitroSafety = NitroSafety - 5;
- }
- }
- else
- {
- //Calculate safety when not nitroing
- if (Util.IsSliding(car, 0.2f) || Util.IsPotentiallySliding(car, 0.3f))
- {
- if (NitroSafety > 40) NitroSafety = NitroSafety - 5;
- }
- else if (NitroSafety < 100)
- {
- if (car.CurrentGear < 4) NitroSafety = NitroSafety + 2;
- else NitroSafety++;
- }
- }
- //dev info
- if (shouldnotify)
- {
- notification = notification + "~n~Nitro bar: " + NitroBar + "%";
- string safety = "~r~" + NitroSafety + "%";
- if (NitroSafety > 10) safety = "~y~" + NitroSafety + "%";
- if (NitroSafety > 50) safety = "~g~" + NitroSafety + "%";
- notification = notification + "~n~Nitro Safety: " + safety + "~w~";
- }
- //Nitro cancel conditions
- if (NitroBar < 0 || Util.IsSliding(car, 2f) || Util.IsPotentiallySliding(car, 1f)) NitroTimeLimit = Game.GameTime;
- //Apply the nitro
- if (NitroTimeLimit > Game.GameTime && car.CurrentGear > 1 && car.Acceleration > 0 && NitroBar > 0)
- {
- notification = notification + "~n~Nitroing";
- Util.ForceNitro(car);
- NitroBar--;
- }//Nitro bar recharge
- else if (NitroBar < 100) NitroBar = NitroBar + 0.2f;
- if (NitroSafety > 50 && NitroTimeLimit + IntervalBetweenNitros < Game.GameTime) NitroTimeLimit = Game.GameTime + (Util.GetRandomInt(5, 10) * 1000);
- }
- else shouldnotify = false;
- }
- //Waypoint following
- if (driver.IsAlive && driver.IsInVehicle(car))
- {
- if (StreetRaces.RaceWaypoints.Count > 0 && CurrentLap <= StreetRaces.Laps)
- {
- if ((!driver.IsPlayer && !Util.IsDriving(driver)) || (driver.IsPlayer && driver.Position.DistanceTo(StreetRaces.RaceWaypoints[CurrentWaypoint]) < 20f))
- {
- int DrivingStyle = 4 + 8 + 512 + 262144;
- float speed = 120f;
- CurrentCheckpoint++;
- if (CurrentWaypoint == StreetRaces.RaceWaypoints.Count - 1)
- {
- CurrentWaypoint = 0;
- if (!driver.IsPlayer)
- {
- Vector3 pos = StreetRaces.RaceWaypoints[CurrentWaypoint];
- //driver.Task.DriveTo(car, pos, 15f, speed, DrivingStyle);
- Function.Call(Hash.TASK_VEHICLE_DRIVE_TO_COORD_LONGRANGE, driver, car, pos.X, pos.Y, pos.Z, speed, DrivingStyle, 15f);
- }
- }
- else
- {
- if ((driver.Position.DistanceTo(StreetRaces.RaceWaypoints[CurrentWaypoint]) < 20f || (CurrentLap == 0 && CurrentWaypoint == 0)))
- {
- //Add a lap
- if (CurrentWaypoint == 0)
- {
- CurrentCheckpoint = 0;
- CurrentLap++;
- 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);
- }
- CurrentWaypoint++;
- if (Util.CarCanSeePos(car, StreetRaces.RaceWaypoints[CurrentWaypoint])) DrivingStyle = 4 + 8 + 512 + 262144 + 4194304;
- if (Util.ForwardSpeed(car) > 20f && StreetRaces.RaceDangerousWaypoints.Contains(StreetRaces.RaceWaypoints[CurrentWaypoint])) speed = car.Speed / 1.5f;
- if (!driver.IsPlayer)
- {
- Vector3 pos = StreetRaces.RaceWaypoints[CurrentWaypoint];
- //driver.Task.DriveTo(car, pos, 15f, speed, DrivingStyle);
- Function.Call(Hash.TASK_VEHICLE_DRIVE_TO_COORD_LONGRANGE, driver, car, pos.X, pos.Y, pos.Z, speed, DrivingStyle, 15f);
- }
- }
- }
- //if(driver.IsPlayer) Util.FlareUpNextCheckpoint(StreetRaces.RaceWaypoints[CurrentWaypoint]);
- }
- }
- }
- else
- {
- if (!OutOfRace)
- {
- car.LeftIndicatorLightOn = true;
- car.RightIndicatorLightOn = true;
- Util.WarnPlayer(StreetRaces.ScriptName, "RACER OUT", Name + " is out of the race.");
- OutOfRace = true;
- }
- }
- DistanceToWaypoint = (float)Math.Round(car.Position.DistanceTo(StreetRaces.RaceWaypoints[CurrentWaypoint]));
- if (driver.IsPlayer)
- {
- shouldnotify = false;
- }
- if (shouldnotify) Util.DisplayHelpTextThisFrame(notification); else if (driver.IsPlayer) Util.DisplayHelpTextThisFrame("Lap: "+ CurrentLap+"/"+StreetRaces.Laps+" | Waypoint: "+CurrentWaypoint+ " | Pos: " + PositionInRace);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement