Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //v1.2.2 (8/6/2015 9:11AM)
- //Script by Bilago
- //Original VB script written by 5mith
- //https://www.gta5-mods.com/scripts/off-road-traffic
- //changes from 1.2.1 -> 1.2.2
- //Minor code change to prevent issues adding more locations
- //overhaul on vehicle/pedestrian generation
- //Prevents vehicles from spawning in front of a user.
- //
- //changes from 1.2 -> 1.2.1
- //Fixed a typo that may cause the script to no longer function
- //
- //changes from 1.1 -> 1.2
- //Added 3 more vehicles (Jeeps) Thanks tall70
- //Added BF Injection and Dune vehicles Thanks JWillMac51!
- //Removed hard coded distance and use per Location distance for check
- //Made the tick rate lower for driving faster than 40f (was 3 seconds now is 2)
- //Added a check to make sure the pedestrian spawned before trying to give weapons
- //Added configurable setting for spawn speed when driving fast and slow
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using GTA;
- using GTA.Math;
- using GTA.Native;
- using System.Drawing;
- public class Traffic : Script
- {
- /// <summary>
- /// Change this to increase/decrease the delay between spawning vehicles when driving
- /// This is measured in seconds
- /// SpawnSpeedDrivingFast is used when going over 40MPH
- /// SpawnSpeedDrivingSlow is used when going less than 40MPH.
- /// </summary>
- private int SpawnSpeedDrivingFast = 2; //seconds
- private int SpawnSpeedDrivingSlow = 3; //seconds
- /// <summary>
- /// List of vehicles to spawn randomly
- /// </summary>
- private List<string> GeneralVehicles = new List<string>()
- {
- "granger", "sanchez2", "bmx", "tractor", "sanchez", "landstalker", "rebel", "bodhi", "bjxl", "blazer", "bobcatxl", "asea", "rancherxl", "sandking",
- "patriot", "blazer3", "asterope", "gburrito2", "scorcher", "brawler", "fugitive", "baller", "enduro", "rhapsody", "youga",
- "serrano", "sandking2", "dukes", "sheriff", "sheriff2", "fbi2", "pranger", "cavalcade", "dloader", "mesa", "mesa2", "mesa3", "bfinjection", "dune"
- };
- /// <summary>
- /// List of pedestrian types you want to spawn (leave blank to spawn any random pedestrians)
- /// Putting pedestrians in this list will only spawn from this list!
- /// </summary>
- private List<string> GeneralDrivers = new List<string>()
- {
- //currently blank so all pedestrians have a chance to spawn
- };
- /// <summary>
- /// List of Military Only Vehicles
- /// </summary>
- private List<string> MarineVehicles = new List<string>()
- {
- "crusader", "barracks", "barracks2", "barracks3", "insurgent", "insurgent2", "technical", "sanchez"
- };
- /// <summary>
- /// List of Marines to spawn randomly
- /// </summary>
- private List<string> MarineDrivers = new List<string>()
- {
- "S_M_M_Marine_01"
- };
- /// <summary>
- /// List of Police Only Vehicles
- /// </summary>
- private List<string> PoliceVehicles = new List<string>()
- {
- "sheriff", "sheriff2", "fbi2", "pranger"
- };
- /// <summary>
- /// List of Police officers to spawn randomly if a police vehicle is chosen
- /// </summary>
- private List<string> PoliceDrivers = new List<string>()
- {
- "S_M_Y_Ranger_01", "S_M_Y_Sheriff_01"
- };
- /// <summary>
- /// Weapons to give randomly to regular Police officers
- /// </summary>
- private List<WeaponHash> policeWeapons = new List<WeaponHash>()
- {
- WeaponHash.Pistol, WeaponHash.StunGun, WeaponHash.VintagePistol, WeaponHash.Nightstick, WeaponHash.Pistol50, WeaponHash.HeavyShotgun, WeaponHash.SNSPistol
- };
- /// <summary>
- /// Weapons to give randomly to Marines
- /// </summary>
- private List<WeaponHash> marinesWeapons = new List<WeaponHash>()
- {
- WeaponHash.SMG, WeaponHash.AdvancedRifle, WeaponHash.SpecialCarbine, WeaponHash.HeavyShotgun, WeaponHash.APPistol
- };
- /// <summary>
- /// Weapons to give randomly to pedestrians
- /// Multiple Unarmed entries increases chances the pedestrian is weaponless
- /// </summary>
- private List<WeaponHash> pedestrianWeapons = new List<WeaponHash>()
- {
- WeaponHash.Bat, WeaponHash.Pistol, WeaponHash.Knife, WeaponHash.Crowbar, WeaponHash.Unarmed, WeaponHash.Unarmed, WeaponHash.Unarmed, WeaponHash.Unarmed
- };
- /// <summary>
- /// Seats to place pedestrians when spawning more than one
- /// </summary>
- private List<VehicleSeat> availableSeats = new List<VehicleSeat>() {
- VehicleSeat.Driver//, VehicleSeat.RightFront
- };
- //Entry Function
- public Traffic()
- {
- setupLocations();
- //Converting seconds to milliseconds
- SpawnSpeedDrivingFast = SpawnSpeedDrivingFast * 1000;
- SpawnSpeedDrivingSlow = SpawnSpeedDrivingSlow * 1000;
- Tick += general_tick;
- this.Interval = 8000;
- }
- //Per tick function
- private void general_tick(object sender, EventArgs e)
- {
- bool atLocation = false;
- //Iterate Through all Locations stored
- for (int i = 0; i < Locations.Count; i++)
- {
- if (Game.Player.Character.IsInRangeOf(Locations[i].Location, Locations[i].Distance))
- {
- atLocation = true;
- //lets spawn a vehicle
- SpawnRandomVehicle(Locations[i]);
- //if the player is driving and is going over 40, lets increase the script speed
- if (Game.Player.Character.IsInVehicle())
- {
- if (Game.Player.Character.CurrentVehicle.Speed > 40f)
- changeInterval(SpawnSpeedDrivingFast);
- else if (Game.Player.Character.CurrentVehicle.Speed < 5f)
- changeInterval(8000);
- else
- changeInterval(SpawnSpeedDrivingSlow);
- }
- else
- changeInterval(8000);
- break;
- }
- }
- //if we get here that means we're not near any specified location, let's increase the interval delay
- if(!atLocation)
- changeInterval(8000);
- }
- /// <summary>
- /// Changes the tick interval of this mod
- /// </summary>
- /// <param name="interval">delay between ticks (milliseconds)</param>
- private void changeInterval(int interval)
- {
- if (this.Interval != interval)
- this.Interval = interval;
- }
- private string log = "OffRoad-TrafficERRORS.log";
- private Vehicle v;
- private Ped d;
- private static Random r = new Random();
- private List<LocationInfo> Locations = new List<LocationInfo>();
- /// <summary>
- /// Stores the spawn locations
- /// </summary>
- public class LocationInfo
- {
- public string ID { get; set; }
- public Vector3 Location;
- public List<string> Vehicles { get; set; }
- public List<string> DriverTypes = new List<string>();
- public List<WeaponHash> Weapons { get; set; }
- public float Distance { get; set; }
- }
- /// <summary>
- /// This sets up all the spawn location information, should only run once in the startup method
- /// </summary>
- private void setupLocations()
- {
- LocationInfo Zancudo = new LocationInfo()
- {
- ID = "Zancudo",
- Location = new Vector3(-1524.5f, 3402.34f, 100f),
- DriverTypes = MarineDrivers,
- Vehicles = MarineVehicles,
- Weapons = marinesWeapons,
- Distance = 1100f
- };
- Locations.Add(Zancudo);
- LocationInfo Chapparral = new LocationInfo()
- {
- ID = "Chapparral",
- Location = new Vector3(-184.18f, 2347.42f, 102f),
- DriverTypes = GeneralDrivers,
- Vehicles = GeneralVehicles,
- Weapons = pedestrianWeapons,
- Distance = 1000f
- };
- Locations.Add(Chapparral);
- LocationInfo Paleto = new LocationInfo()
- {
- ID = "Paleto",
- Location = new Vector3(-617.55f, 5844.59f, 24f),
- DriverTypes = GeneralDrivers,
- Vehicles = GeneralVehicles,
- Weapons = pedestrianWeapons,
- Distance = 1000f
- };
- Locations.Add(Paleto);
- LocationInfo NatPark = new LocationInfo()
- {
- ID = "NatPark",
- Location = new Vector3(-947.53f, 4615.99f, 240f),
- DriverTypes = GeneralDrivers,
- Vehicles = GeneralVehicles,
- Weapons = pedestrianWeapons,
- Distance = 1000f
- };
- Locations.Add(NatPark);
- LocationInfo Dam = new LocationInfo()
- {
- ID = "Dam",
- Location = new Vector3(1862.24f, 159.23f, 176.15f),
- DriverTypes = GeneralDrivers,
- Vehicles = GeneralVehicles,
- Weapons = pedestrianWeapons,
- Distance = 600f
- };
- Locations.Add(Dam);
- LocationInfo Molinos = new LocationInfo()
- {
- ID = "Molinos",
- Location = new Vector3(2293.53f, 1938.54f, 127.58f),
- DriverTypes = GeneralDrivers,
- Vehicles = GeneralVehicles,
- Weapons = pedestrianWeapons,
- Distance = 600f
- };
- Locations.Add(Molinos);
- }
- /// <summary>
- /// This spawns a random vehicle based on the location data
- /// </summary>
- /// <param name="location">Where the player is currently close to</param>
- private void SpawnRandomVehicle(LocationInfo location)
- {
- if (location.Vehicles.Count == 0)
- {
- writeLog("Vehicle list specified is empty.. cannot spawn");
- return;
- }
- string Vehicle = location.Vehicles[GetRandomNumber(location.Vehicles.Count)];
- List<string> drivers = new List<string>();
- List<WeaponHash> weaponList = new List<WeaponHash>();
- bool isPedestrian = false;
- if (PoliceVehicles.Contains(Vehicle))
- {
- drivers = PoliceDrivers;
- weaponList = policeWeapons;
- }
- else
- {
- isPedestrian = !MarineVehicles.Contains(Vehicle);
- drivers = location.DriverTypes;
- weaponList = location.Weapons;
- }
- createVehicle(Vehicle, drivers, weaponList, isPedestrian);
- }
- /// <summary>
- /// Creates a vehicle with the specified vehicle name and driver
- /// </summary>
- /// <param name="Vehicle">The type of vehicle to spawn</param>
- /// <param name="driver">the type of driver to spawn. Empty string will create a random pedestrian</param>
- private void createVehicle(string Vehicle, List<string> drivers, List<WeaponHash> weapons, bool isPedestrian)
- {
- try
- {
- //spawns a vehicle with a random distance between 200 and 500
- v = World.CreateVehicle(Vehicle, Game.Player.Character.Position.Around((float)GetRandomNumber(150, 300)));
- if (v == null)
- {
- writeLog(string.Format("{0} is not a valid vehicle", Vehicle));
- return;
- }
- //This helps but makes traffic way too sparse for the amount that's spawned.
- //while (!v.IsOccluded)
- //{
- // v.Position = Game.Player.Character.Position.Around(90);
- // v.PlaceOnNextStreet();
- // Wait(10);
- //}
- for (int i = 0; i < availableSeats.Count; i++)
- {
- VehicleSeat seat = availableSeats[i];
- int rand =0;
- if (v.IsSeatFree(seat))
- {
- if (drivers.Count == 0)
- d = v.CreateRandomPedOnSeat(seat);
- else
- {
- rand = GetRandomNumber(drivers.Count);
- d = v.CreatePedOnSeat(seat, drivers[rand]);
- }
- if (d == null)
- {
- writeLog("An Invalid Pedestrian Type was used! Check your spawn list!");
- writeLog(string.Format("Random number :{0} List value: {1}", rand, drivers[rand]));
- v.MarkAsNoLongerNeeded();
- v.Delete();
- return;
- }
- d.Task.WarpIntoVehicle(v, seat);
- //Lets generate two weapons for the NPC we're about to create
- WeaponHash weaponSlot0 = WeaponHash.Unarmed;
- WeaponHash weaponSlot1 = WeaponHash.Unarmed;
- GenerateWeaponSlots(ref isPedestrian, ref weapons, ref weaponSlot0, ref weaponSlot1);
- d.Weapons.Give(weaponSlot0, 1000, true, true);
- d.Weapons.Give(weaponSlot1, 1000, true, true);
- d.MarkAsNoLongerNeeded();
- }
- }
- v.MarkAsNoLongerNeeded();
- //Lets make sure someone made it into the driver seat.. if not lets delete the car
- if (v.IsSeatFree(VehicleSeat.Driver))
- v.Delete();
- //else
- // writeLog(string.Format("Vehicle {0} created with pedestrian {1}", v.DisplayName, d.Handle));
- }
- catch
- {
- writeLog("ERROR Creating vehicle!");
- return;
- }
- }
- /// <summary>
- /// This will generate two weapons
- /// </summary>
- /// <param name="isPedestrian">if this is true, there is a 50% chance that they will not be part of the 'weapon lottery'</param>
- /// <param name="weapons">the list of weapons to choose from</param>
- /// <param name="a">weapon 1</param>
- /// <param name="b">weapon 2</param>
- private void GenerateWeaponSlots(ref bool isPedestrian, ref List<WeaponHash> weapons, ref WeaponHash a, ref WeaponHash b)
- {
- try
- {
- if (weapons.Count == 0)
- return;
- if (isPedestrian)
- {
- if (r.NextDouble() > 0.5)
- return;
- }
- a = weapons[GetRandomNumber(weapons.Count)];
- b = weapons[GetRandomNumber(weapons.Count)];
- }
- catch
- {
- a = WeaponHash.Unarmed;
- b = WeaponHash.Unarmed;
- }
- }
- /// <summary>
- /// Generates a random number
- /// </summary>
- /// <param name="max">The exclusive maximum of the range</param>
- /// <returns>a number from 0 to (max - 1)</returns>
- private int GetRandomNumber(int max)
- {
- return GetRandomNumber(0, max);
- }
- /// <summary>
- /// Generates a random number
- /// </summary>
- /// <param name="min">The inclusive minimum of the range</param>
- /// <param name="max">The exclusive maximum of the range</param>
- /// <returns>a number from min to (max - 1) </returns>
- private int GetRandomNumber(int min, int max)
- {
- //dont bother getting a random number if the max is 1
- if (max == 1)
- return 0;
- try
- {
- return r.Next(0, max);
- }
- catch
- {
- return 0;
- }
- }
- private void writeLog(string text)
- {
- string message = string.Empty;
- if (!string.IsNullOrEmpty(text))
- message = string.Format("[{0}] - {1}", DateTime.Now, text);
- using (System.IO.StreamWriter write = new System.IO.StreamWriter(log, true))
- {
- write.WriteLine(message);
- }
- //System.IO.File.AppendAllText(log, message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment