SHARE
TWEET

Heli AI weapon handling from Bodyguard Squads

Eddlm Oct 18th, 2015 (edited) 198 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This function allows any ped on an attack heli to use its weapons against the enemies the ped is fighting.
  3. Pasted here as used in https://www.gta5-mods.com/scripts/bodyguard-squads.
  4. Call on tick, providing the ped that is on an heli that has weapons.
  5. Lacks multiweapon handling for now, they only use the default weapon (Minigun for buzzard/annihilator, explosive rounds for Valk/Savage).
  6. */
  7.     void HandleAirCombat(Ped RecieveOrder)
  8.     {
  9.         if (CanWeUse(RecieveOrder) && RecieveOrder.IsSittingInVehicle())
  10.         {
  11.             if (RecieveOrder.CurrentVehicle.Model == (Model)VehicleHash.Savage)
  12.             {
  13.                 if (RecieveOrder.IsInCombat && GetRandomInt(1, 30) == 1)
  14.                 {
  15.                     foreach (Ped target in World.GetAllPeds())
  16.                     {
  17.                         if (target.IsAlive && GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_COMBAT, RecieveOrder, target) && GTA.Native.Function.Call<Vector3>(GTA.Native.Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, RecieveOrder, target.Position.X, target.Position.Y, target.Position.Z).Y > -10)
  18.                         {
  19.                             Vector3 Velocity = new Vector3(GetRandomInt(-2, 2), GetRandomInt(-2, 2), 0);
  20.                             if (target.Velocity.Length() > 10f)
  21.                             {
  22.                                 Velocity = Velocity + RecieveOrder.Velocity - target.Velocity;
  23.                             }
  24.                             GTA.Native.Function.Call(GTA.Native.Hash.SET_VEHICLE_SHOOT_AT_TARGET, RecieveOrder, 0, target.Position.X + Velocity.X, target.Position.Y + Velocity.Y, target.Position.Z);
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.             if (RecieveOrder.CurrentVehicle.Model == (Model)VehicleHash.Annihilator)
  30.             {
  31.                 if (RecieveOrder.IsInCombat)
  32.                 {
  33.                     foreach (Ped target in World.GetAllPeds())
  34.                     {
  35.                         if (target.IsAlive && GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_COMBAT, RecieveOrder, target) && GTA.Native.Function.Call<Vector3>(GTA.Native.Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, RecieveOrder, target.Position.X, target.Position.Y, target.Position.Z).Y > 30)
  36.                         {
  37.                             Vector3 Velocity = new Vector3(GetRandomInt(-2, 2), GetRandomInt(-2, 2), 0);
  38.                             if (target.Velocity.Length() > 10f)
  39.                             {
  40.                                 Velocity = Velocity + RecieveOrder.Velocity - target.Velocity;
  41.                             }
  42.                             GTA.Native.Function.Call(GTA.Native.Hash.SET_VEHICLE_SHOOT_AT_TARGET, RecieveOrder, 0, target.Position.X + Velocity.X, target.Position.Y + Velocity.Y, target.Position.Z);
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.             if (RecieveOrder.CurrentVehicle.Model == (Model)VehicleHash.Buzzard)
  48.             {
  49.                 if (RecieveOrder.IsInCombat)
  50.                 {
  51.                     foreach (Ped target in World.GetAllPeds())
  52.                     {
  53.                         if (target.IsAlive && GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_COMBAT, RecieveOrder, target) && GTA.Native.Function.Call<Vector3>(GTA.Native.Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, RecieveOrder, target.Position.X, target.Position.Y, target.Position.Z).Y > 30)
  54.                         {
  55.                             Vector3 Velocity = new Vector3(GetRandomInt(-2, 2), GetRandomInt(-2, 2), 0);
  56.                             if (target.Velocity.Length() > 10f)
  57.                             {
  58.                                 Velocity = Velocity + RecieveOrder.Velocity - target.Velocity;
  59.                             }
  60.                             GTA.Native.Function.Call(GTA.Native.Hash.SET_VEHICLE_SHOOT_AT_TARGET, RecieveOrder, 0, target.Position.X + Velocity.X, target.Position.Y + Velocity.Y, target.Position.Z);
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.             if (RecieveOrder.CurrentVehicle.Model == (Model)VehicleHash.Valkyrie)
  66.             {
  67.                 if (RecieveOrder.IsInCombat && GetRandomInt(1, 30) == 1)
  68.                 {
  69.                     if (CanWeUse(RecieveOrder.CurrentVehicle.GetPedOnSeat(VehicleSeat.RightFront)))
  70.                     {
  71.                         foreach (Ped target in World.GetAllPeds())
  72.                         {
  73.                             if (target.IsAlive && GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_COMBAT, RecieveOrder, target))
  74.                             {
  75.                                 Vector3 Velocity = new Vector3(GetRandomInt(-5, 5), GetRandomInt(-5, 5), 0);
  76.                                 if (target.Velocity.Length() > 10f)
  77.                                 {
  78.                                     Velocity = Velocity + Squad1Leader.Velocity - target.Velocity;
  79.                                 }
  80.                                 GTA.Native.Function.Call(GTA.Native.Hash.SET_VEHICLE_SHOOT_AT_TARGET, RecieveOrder.CurrentVehicle.GetPedOnSeat(VehicleSeat.RightFront), 0, target.Position.X + Velocity.X, target.Position.Y + Velocity.Y, target.Position.Z);
  81.                             }
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89. //Used by HandleAirCombat.
  90.     bool CanWeUse(Entity entity)
  91.     {
  92.         return entity != null && entity.Exists();
  93.     }
  94.     Random rnd = new Random();
  95.     protected int GetRandomInt(int min, int max)
  96.     {
  97.         return rnd.Next(min, max);
  98.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top