Skip to content

Instantly share code, notes, and snippets.

@David-Lor
Created May 3, 2018 19:02
Show Gist options
  • Save David-Lor/19b90dd11ac726bd22337dfb8098e4ca to your computer and use it in GitHub Desktop.
Save David-Lor/19b90dd11ac726bd22337dfb8098e4ca to your computer and use it in GitHub Desktop.
GTA V Freemode MP Cops WIP
using GTA;
using GTA.Native;
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class FreemodeMP_Cops : Script
{
private int relationship_player=0, relationship_cop_wanted=0, relationship_cop_unwanted=0, prevWantedlevel = 0;
private List<Ped> cops = new List<Ped>();
private List<Vehicle> cop_vehicles = new List<Vehicle>();
private List<Ped> cops_drivers = new List<Ped>();
private Random r = new Random(DateTime.Now.Millisecond);
private int[] head_list_male = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
private int[] head_list_female = { 45, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 21 };
/*private int[] hair_list = { 0, 1, 2, 3, 4 };
private int[] hair_colours = { 1, 2, 3, 4, 5 };*/
private VehicleHash[] copcars = { VehicleHash.Police, VehicleHash.Police2, VehicleHash.Police3, VehicleHash.Police4 };
private WeaponHash[] copweapons_main = { WeaponHash.CarbineRifle, WeaponHash.SMG, WeaponHash.AssaultSMG, WeaponHash.SpecialCarbine };
private WeaponHash[] copweapons_second = { WeaponHash.Pistol, WeaponHash.CombatPistol, WeaponHash.PumpShotgun, WeaponHash.Revolver };
private int maxcops = 12;
public FreemodeMP_Cops()
{
Tick += OnTick;
Interval = 100;
relationship_player = World.AddRelationshipGroup("fmc_player");
relationship_cop_wanted = World.AddRelationshipGroup("fmc_cop_wanted");
relationship_cop_unwanted = World.AddRelationshipGroup("fmc_cop_unwanted");
World.SetRelationshipBetweenGroups(Relationship.Respect, relationship_cop_wanted, relationship_cop_wanted);
World.SetRelationshipBetweenGroups(Relationship.Respect, relationship_cop_unwanted, relationship_cop_unwanted);
World.SetRelationshipBetweenGroups(Relationship.Respect, relationship_cop_wanted, relationship_cop_unwanted);
World.SetRelationshipBetweenGroups(Relationship.Dislike, relationship_cop_unwanted, relationship_player);
World.SetRelationshipBetweenGroups(Relationship.Hate, relationship_cop_wanted, relationship_player);
Game.Player.Character.RelationshipGroup = relationship_player;
/*GTA.Blip[] activeblips = GTA.World.GetActiveBlips();
foreach (Blip b in activeblips) {
b.Remove();
}*/
}
void OnTick(object sender, EventArgs e)
{
int wantedlevel = Game.Player.WantedLevel;
if (wantedlevel > 1 && cops.Count <= maxcops) {
VehicleHash carhash = copcars[ r.Next( copcars.Length ) ];
Vehicle car = GTA.World.CreateVehicle( carhash, Game.Player.Character.Position.Around (100f * (r.Next(1, 6) ) ) );
car.PlaceOnNextStreet();
car.AddBlip();
car.CurrentBlip.Sprite = BlipSprite.PoliceOfficer;
car.CurrentBlip.Scale = 0.2f;
cop_vehicles.Add(car);
VehicleSeat[] seats = { VehicleSeat.Driver, VehicleSeat.Passenger, VehicleSeat.LeftRear, VehicleSeat.RightRear };
Dictionary<Ped, int> cops_now = new Dictionary<Ped, int>(); //cops_now = Ped, Gender
for (int i=0; i < seats.Length; i++) {
//Male=0, Female=1
int gen = r.Next(2);
PedHash ph;
if (gen == 0) ph = PedHash.FreemodeMale01;
else ph = PedHash.FreemodeFemale01;
Ped p = car.CreatePedOnSeat(seats[i], ph);
cops_now.Add(p, gen);
}
foreach (KeyValuePair<Ped, int> kv in cops_now) {
Ped p = kv.Key;
int gen = kv.Value;
p.RelationshipGroup = relationship_cop_wanted;
Function.Call(Hash.SET_PED_AS_COP, p, true);
cops.Add(p);
p.Accuracy = 10;
p.Health = 80;
p.MaxHealth = 80;
p.Armor = 100;
WeaponHash w1 = copweapons_main[r.Next(copweapons_main.Length)];
WeaponHash w2 = copweapons_second[r.Next(copweapons_second.Length)];
p.Weapons.Give(w2, 420, false, true);
p.Weapons.Give(w1, 420, false, true);
int tid = 0;
int head;
if (gen == 0) {
head = head_list_male[ r.Next( head_list_male.Length ) ];
} else {
head = head_list_female[ r.Next( head_list_female.Length ) ];
}
int hair = r.Next(1,8); //USAR LISTAS TAMBIÉN
int hair_colour = r.Next(6);
if (gen == 0) {
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 0, head, 0, tid); //head
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 1, 0, 0, tid); //beard (masks)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 2, hair, hair_colour, tid); //hair
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 3, 52, 0, tid); //torso (variar según aux.torso y color de piel - también da guantes)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 4, 34, 0, tid); //legs
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 5, 0, 0, tid); //hands (paracaídas)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 6, 24, 0, tid); //foot
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 7, 0, 0, tid); //teeth
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 8, 58, 0, tid); //accesories1 (
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 9, 0, 0, tid); //accesories2 (armaduras)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 10, 0, 0, tid); //decals
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 11, 55, 0, tid); //aux.torso (partes de arriba reales)
} else { //Female
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 0, head, 0, tid); //head
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 1, 0, 0, tid); //beard (masks)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 2, hair, hair_colour, tid); //hair
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 3, 44, 0, tid); //torso (variar según aux.torso y color de piel - también da guantes)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 4, 34, 0, tid); //legs
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 5, 0, 0, tid); //hands (paracaídas)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 6, 25, 0, tid); //foot
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 7, 83, 2, tid); //teeth
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 8, 35, 0, tid); //accesories1 (
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 9, 0, 0, tid); //accesories2 (armaduras)
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 10, 0, 0, tid); //decals
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, p, 11, 48, 0, tid); //aux.torso (partes de arriba reales)
}
}
Ped cop_driver = car.Driver;
cop_driver.Task.DriveTo(car, Game.Player.Character.Position, 5f, 100f, 512);
cop_driver.AlwaysKeepTask = true;
cop_driver.DrivingStyle = DrivingStyle.AvoidTrafficExtremely;
cops_drivers.Add(cop_driver);
UI.ShowSubtitle("New freemode cops group spawned!");
Wait(30000);
} else if (wantedlevel == 0) {
foreach (Ped p in cops) {
p.RelationshipGroup = relationship_cop_unwanted;
}
}
//Make drivers stop driving when being close to the player
List<Ped> rmvCopsDrivers = new List<Ped>();
foreach (Ped p in cops_drivers) {
if (p.Position.DistanceTo(Game.Player.Character.Position) < 25f) {
rmvCopsDrivers.Add(p);
if (p.IsInVehicle()) {
p.Task.ClearAllImmediately();
p.Task.LeaveVehicle(LeaveVehicleFlags.LeaveDoorOpen);
}
}
}
foreach (Ped p in rmvCopsDrivers) {
cops_drivers.Remove(p);
}
//Remove old cops
List<Ped> rmvCops = new List<Ped>();
foreach (Ped p in cops) {
if (p.IsDead || !p.Exists() || wantedlevel == 0) {
p.MarkAsNoLongerNeeded();
rmvCops.Add(p);
p.CurrentBlip.Remove();
}
}
foreach (Ped p in rmvCops) {
cops.Remove(p);
if (cops_drivers.Contains(p)) cops_drivers.Remove(p);
}
//Remove old vehicles
List<Vehicle> rmvVehicles = new List<Vehicle>();
foreach (Vehicle v in cop_vehicles) {
Ped[] ps = v.Passengers;
bool ps_dead = false;
foreach (Ped p in ps) {
if (!p.IsAlive || !p.Exists() || Function.Call<int>(Hash.GET_PED_TYPE) != 6) { //Ped type 6 = COP
ps_dead = true;
break;
}
}
if (v.PassengerCount == 0 || v.Health == 0 || !v.Exists() || ps_dead) {
v.MarkAsNoLongerNeeded();
rmvVehicles.Add(v);
v.CurrentBlip.Remove();
}
}
foreach (Vehicle v in rmvVehicles) {
cop_vehicles.Remove(v);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment