RAGECOOP-V/Client/Entities/EntitiesPed.cs

909 lines
30 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
2021-07-07 13:36:25 +02:00
using GTA;
using GTA.Native;
using GTA.Math;
using LemonUI.Elements;
namespace CoopClient.Entities
2021-07-07 13:36:25 +02:00
{
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public class EntitiesPed
{
2021-12-12 19:31:06 +01:00
/// <summary>
/// 0 = Nothing
/// 1 = Character
/// 2 = Vehicle
/// </summary>
2021-12-12 22:00:06 +01:00
private byte ModelNotFound = 0;
2021-07-07 13:36:25 +02:00
private bool AllDataAvailable = false;
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool LastSyncWasFull { get; set; } = false;
/// <summary>
/// ?
/// </summary>
2021-11-19 22:08:15 +01:00
public ulong LastUpdateReceived { get; set; }
/// <summary>
/// ?
/// </summary>
public float Latency { get; set; }
2021-07-07 13:36:25 +02:00
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public Ped Character { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public int Health { get; set; }
private int LastModelHash = 0;
2021-12-12 19:31:06 +01:00
private int CurrentModelHash = 0;
/// <summary>
/// ?
/// </summary>
2021-12-12 19:31:06 +01:00
public int ModelHash
{
set
{
LastModelHash = LastModelHash == 0 ? value : CurrentModelHash;
CurrentModelHash = value;
}
}
2021-07-07 13:36:25 +02:00
private Dictionary<int, int> LastProps = new Dictionary<int, int>();
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public Dictionary<int, int> Props { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public Vector3 Position { get; set; }
#region -- ON FOOT --
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public Vector3 Rotation { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public Vector3 Velocity { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public byte Speed { get; set; }
private bool LastIsJumping = false;
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool IsJumping { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool IsRagdoll { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool IsOnFire { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public Vector3 AimCoords { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool IsAiming { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool IsShooting { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public bool IsReloading { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-07 13:36:25 +02:00
public int CurrentWeaponHash { get; set; }
#endregion
2021-07-07 13:36:25 +02:00
/// <summary>
/// ?
/// </summary>
2021-07-09 00:20:09 +02:00
public Blip PedBlip;
2021-07-07 13:36:25 +02:00
2021-07-10 10:52:43 +02:00
#region -- IN VEHICLE --
2021-11-19 22:08:15 +01:00
private ulong VehicleStopTime { get; set; }
2021-07-11 03:05:07 +02:00
/// <summary>
/// ?
/// </summary>
public bool IsInVehicle { get; set; }
2021-12-12 19:31:06 +01:00
private int LastVehicleModelHash = 0;
private int CurrentVehicleModelHash = 0;
/// <summary>
/// ?
/// </summary>
2021-12-12 19:31:06 +01:00
public int VehicleModelHash
{
set
{
LastVehicleModelHash = CurrentVehicleModelHash == 0 ? value : CurrentVehicleModelHash;
CurrentVehicleModelHash = value;
}
}
2021-08-15 13:17:57 +02:00
private int[] LastVehicleColors = new int[] { 0, 0 };
/// <summary>
/// ?
/// </summary>
2021-08-13 15:52:30 +02:00
public int[] VehicleColors { get; set; }
2021-08-22 13:59:15 +02:00
private Dictionary<int, int> LastVehicleMods = new Dictionary<int, int>();
/// <summary>
/// ?
/// </summary>
2021-08-22 13:59:15 +02:00
public Dictionary<int, int> VehicleMods { get; set; }
/// <summary>
/// ?
/// </summary>
public bool VehicleDead { get; set; }
/// <summary>
/// ?
/// </summary>
2021-08-16 16:51:33 +02:00
public float VehicleEngineHealth { get; set; }
/// <summary>
/// ?
/// </summary>
public int VehicleSeatIndex { get; set; }
/// <summary>
/// ?
/// </summary>
public Vehicle MainVehicle { get; set; }
/// <summary>
/// ?
/// </summary>
public Vector3 VehiclePosition { get; set; }
/// <summary>
/// ?
/// </summary>
public Quaternion VehicleRotation { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-11 00:51:46 +02:00
public Vector3 VehicleVelocity { get; set; }
2021-07-13 12:00:37 +02:00
private float LastVehicleSpeed { get; set; }
private float CurrentVehicleSpeed { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-13 12:00:37 +02:00
public float VehicleSpeed
{
set
{
LastVehicleSpeed = CurrentVehicleSpeed;
CurrentVehicleSpeed = value;
}
}
/// <summary>
/// ?
/// </summary>
2021-07-13 16:30:33 +02:00
public float VehicleSteeringAngle { get; set; }
private int LastVehicleAim;
/// <summary>
/// ?
/// </summary>
2021-08-17 15:04:50 +02:00
public bool VehIsEngineRunning { get; set; }
/// <summary>
/// ?
/// </summary>
public float VehRPM { get; set; }
/// <summary>
/// ?
/// </summary>
2021-08-17 15:04:50 +02:00
public bool VehAreLightsOn { get; set; }
/// <summary>
/// ?
/// </summary>
2021-08-17 15:04:50 +02:00
public bool VehAreHighBeamsOn { get; set; }
/// <summary>
/// ?
/// </summary>
2021-07-13 12:00:37 +02:00
public bool VehIsSireneActive { get; set; }
private VehicleDoors[] LastVehDoors;
/// <summary>
/// ?
/// </summary>
public VehicleDoors[] VehDoors { get; set; }
private int LastVehTires;
/// <summary>
/// ?
/// </summary>
public int VehTires { get; set; }
#endregion
internal void DisplayLocally(string username)
2021-07-07 13:36:25 +02:00
{
/*
* username: string
* string: null
* ped: npc
* string: value
* ped: player
*/
// Check beforehand whether ped has all the required data
if (!AllDataAvailable)
{
if (!LastSyncWasFull)
{
2021-08-26 17:01:32 +02:00
if (Position != null)
{
if (PedBlip != null && PedBlip.Exists())
{
PedBlip.Position = Position;
}
else
{
PedBlip = World.CreateBlip(Position);
PedBlip.Color = BlipColor.White;
PedBlip.Scale = 0.8f;
PedBlip.Name = username;
}
}
2021-07-07 13:36:25 +02:00
return;
}
AllDataAvailable = true;
}
2021-12-13 11:51:03 +01:00
if (ModelNotFound != 0)
{
if (ModelNotFound == 1)
{
if (CurrentModelHash != LastModelHash)
{
ModelNotFound = 0;
}
}
else
{
if (CurrentVehicleModelHash != LastVehicleModelHash)
{
ModelNotFound = 0;
}
}
}
#region NOT_IN_RANGE
2021-12-13 11:51:03 +01:00
if (ModelNotFound != 0 || !Game.Player.Character.IsInRange(Position, 500f))
2021-07-07 13:36:25 +02:00
{
2021-09-26 22:27:49 +02:00
if (Character != null && Character.Exists())
{
Character.Kill();
Character.MarkAsNoLongerNeeded();
Character.Delete();
Character = null;
}
if (MainVehicle != null && MainVehicle.Exists() && MainVehicle.IsSeatFree(VehicleSeat.Driver) && MainVehicle.PassengerCount == 0)
{
MainVehicle.MarkAsNoLongerNeeded();
MainVehicle.Delete();
MainVehicle = null;
}
2021-07-07 13:36:25 +02:00
if (username != null)
{
2021-08-26 17:01:32 +02:00
if (PedBlip != null && PedBlip.Exists())
{
PedBlip.Position = Position;
}
else
2021-07-07 13:36:25 +02:00
{
PedBlip = World.CreateBlip(Position);
PedBlip.Color = BlipColor.White;
PedBlip.Scale = 0.8f;
PedBlip.Name = username;
}
}
return;
}
#endregion
#region IS_IN_RANGE
bool characterExist = Character != null && Character.Exists();
if (!characterExist)
{
2021-08-18 11:47:59 +02:00
if (!CreateCharacter(username))
{
return;
}
2021-07-07 13:36:25 +02:00
}
else if (LastSyncWasFull)
{
2021-12-12 19:31:06 +01:00
if (CurrentModelHash != LastModelHash)
2021-07-07 13:36:25 +02:00
{
2021-08-18 11:47:59 +02:00
Character.Kill();
Character.Delete();
if (!CreateCharacter(username))
2021-07-07 13:36:25 +02:00
{
2021-08-18 11:47:59 +02:00
return;
2021-07-07 13:36:25 +02:00
}
}
else if (Props != LastProps)
{
foreach (KeyValuePair<int, int> prop in Props)
{
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, Character.Handle, prop.Key, prop.Value, 0, 0);
}
LastProps = Props;
}
}
if (username != null && Character.IsVisible && Character.IsInRange(Game.Player.Character.Position, 20f))
2021-07-07 13:36:25 +02:00
{
float sizeOffset;
if (GameplayCamera.IsFirstPersonAimCamActive)
{
Vector3 targetPos = Character.Bones[Bone.IKHead].Position + new Vector3(0, 0, 0.10f) + (Character.Velocity / Game.FPS);
Function.Call(Hash.SET_DRAW_ORIGIN, targetPos.X, targetPos.Y, targetPos.Z, 0);
sizeOffset = Math.Max(1f - ((GameplayCamera.Position - Character.Position).Length() / 30f), 0.30f);
}
else
{
Vector3 targetPos = Character.Bones[Bone.IKHead].Position + new Vector3(0, 0, 0.35f) + (Character.Velocity / Game.FPS);
Function.Call(Hash.SET_DRAW_ORIGIN, targetPos.X, targetPos.Y, targetPos.Z, 0);
sizeOffset = Math.Max(1f - ((GameplayCamera.Position - Character.Position).Length() / 25f), 0.25f);
}
new ScaledText(new PointF(0, 0), username, 0.4f * sizeOffset, GTA.UI.Font.ChaletLondon)
{
Outline = true,
Alignment = GTA.UI.Alignment.Center
}.Draw();
Function.Call(Hash.CLEAR_DRAW_ORIGIN);
}
if (Character.IsDead)
{
if (Health <= 0)
{
return;
}
Character.IsInvincible = true;
Character.Resurrect();
}
else if (Character.Health != Health)
{
Character.Health = Health;
if (Health <= 0 && !Character.IsDead)
{
Character.IsInvincible = false;
Character.Kill();
return;
}
}
if (IsInVehicle)
{
DisplayInVehicle();
}
else
{
DisplayOnFoot();
}
#endregion
}
private void DisplayInVehicle()
{
2021-12-12 19:31:06 +01:00
if (MainVehicle == null || !MainVehicle.Exists() || MainVehicle.Model.Hash != CurrentVehicleModelHash)
{
Vehicle targetVehicle = World.GetClosestVehicle(Position, 7f, new Model[] { CurrentVehicleModelHash });
bool vehFound = false;
2021-07-11 05:32:57 +02:00
if (targetVehicle != null)
2021-08-18 18:20:55 +02:00
{
if (targetVehicle.IsSeatFree((VehicleSeat)VehicleSeatIndex))
2021-07-11 05:32:57 +02:00
{
MainVehicle = targetVehicle;
2021-08-18 18:20:55 +02:00
vehFound = true;
2021-07-11 05:32:57 +02:00
}
2021-08-18 18:20:55 +02:00
}
2021-07-11 05:32:57 +02:00
2021-08-18 18:20:55 +02:00
if (!vehFound)
{
2021-12-12 19:31:06 +01:00
Model vehicleModel = CurrentVehicleModelHash.ModelRequest();
2021-08-18 18:20:55 +02:00
if (vehicleModel == null)
2021-08-18 11:47:59 +02:00
{
2021-12-12 22:00:06 +01:00
//GTA.UI.Notification.Show($"~r~(Vehicle)Model ({CurrentVehicleModelHash}) cannot be loaded!");
2021-12-12 19:31:06 +01:00
ModelNotFound = 2;
2021-08-18 18:20:55 +02:00
return;
2021-08-18 11:47:59 +02:00
}
2021-08-18 18:20:55 +02:00
MainVehicle = World.CreateVehicle(vehicleModel, VehiclePosition, VehicleRotation.W);
2021-12-12 22:52:57 +01:00
vehicleModel.MarkAsNoLongerNeeded();
}
}
2021-11-19 22:36:46 +01:00
if (!Character.IsInVehicle() || (int)Character.SeatIndex != VehicleSeatIndex || Character.CurrentVehicle.Handle != MainVehicle.Handle)
{
if (VehicleSeatIndex == -1 &&
Game.Player.Character.IsInVehicle() &&
2021-07-13 08:45:37 +02:00
(int)Game.Player.Character.SeatIndex == -1 &&
2021-11-19 22:36:46 +01:00
Game.Player.Character.CurrentVehicle.Handle == MainVehicle.Handle)
{
Game.Player.Character.Task.WarpOutOfVehicle(MainVehicle);
GTA.UI.Notification.Show("~r~Car jacked!");
}
2021-08-16 16:38:52 +02:00
Character.SetIntoVehicle(MainVehicle, (VehicleSeat)VehicleSeatIndex);
Character.IsVisible = true;
}
2021-07-10 23:41:28 +02:00
#region -- VEHICLE SYNC --
if (AimCoords != default)
{
if (MainVehicle.IsTurretSeat(VehicleSeatIndex))
{
int gameTime = Game.GameTime;
if (gameTime - LastVehicleAim > 30)
{
Function.Call(Hash.TASK_VEHICLE_AIM_AT_COORD, Character.Handle, AimCoords.X, AimCoords.Y, AimCoords.Z);
LastVehicleAim = gameTime;
}
}
}
if (MainVehicle.GetResponsiblePedHandle() != Character.Handle)
{
return;
}
if (VehicleColors != null && VehicleColors != LastVehicleColors)
{
Function.Call(Hash.SET_VEHICLE_COLOURS, MainVehicle, VehicleColors[0], VehicleColors[1]);
LastVehicleColors = VehicleColors;
}
2021-07-13 12:00:37 +02:00
if (Character.IsOnBike && MainVehicle.ClassType == VehicleClass.Cycles)
{
bool isFastPedaling = Function.Call<bool>(Hash.IS_ENTITY_PLAYING_ANIM, Character.Handle, PedalingAnimDict(), "fast_pedal_char", 3);
if (CurrentVehicleSpeed < 0.2f)
{
StopPedalingAnim(isFastPedaling);
}
else if (CurrentVehicleSpeed < 11f && !Function.Call<bool>(Hash.IS_ENTITY_PLAYING_ANIM, Character.Handle, PedalingAnimDict(), "cruise_pedal_char", 3))
{
StartPedalingAnim(false);
}
else if (CurrentVehicleSpeed >= 11f && !isFastPedaling)
{
StartPedalingAnim(true);
}
}
2021-08-17 15:04:50 +02:00
else
2021-07-10 23:41:28 +02:00
{
2021-08-22 13:59:15 +02:00
if (VehicleMods != null && VehicleMods != LastVehicleMods)
{
Function.Call(Hash.SET_VEHICLE_MOD_KIT, MainVehicle, 0);
foreach (KeyValuePair<int, int> mod in VehicleMods)
{
MainVehicle.Mods[(VehicleModType)mod.Key].Index = mod.Value;
}
2021-12-12 19:31:06 +01:00
LastVehicleMods = VehicleMods;
2021-08-22 13:59:15 +02:00
}
2021-08-17 15:04:50 +02:00
MainVehicle.EngineHealth = VehicleEngineHealth;
2021-07-10 23:41:28 +02:00
2021-08-17 15:04:50 +02:00
if (VehicleDead && !MainVehicle.IsDead)
{
MainVehicle.Explode();
}
else if (!VehicleDead && MainVehicle.IsDead)
{
MainVehicle.Repair();
}
2021-07-10 23:41:28 +02:00
2021-08-17 15:04:50 +02:00
if (VehIsEngineRunning != MainVehicle.IsEngineRunning)
{
MainVehicle.IsEngineRunning = VehIsEngineRunning;
}
2021-07-10 23:41:28 +02:00
MainVehicle.CurrentRPM = VehRPM;
2021-08-17 15:04:50 +02:00
if (VehAreLightsOn != MainVehicle.AreLightsOn)
{
2021-08-17 15:04:50 +02:00
MainVehicle.AreLightsOn = VehAreLightsOn;
}
if (VehAreHighBeamsOn != MainVehicle.AreHighBeamsOn)
{
MainVehicle.AreHighBeamsOn = VehAreHighBeamsOn;
}
if (VehIsSireneActive != MainVehicle.IsSirenActive)
{
MainVehicle.IsSirenActive = VehIsSireneActive;
}
2021-08-17 15:04:50 +02:00
Function.Call(Hash.SET_VEHICLE_BRAKE_LIGHTS, MainVehicle, CurrentVehicleSpeed > 0.2f && LastVehicleSpeed > CurrentVehicleSpeed);
2021-11-19 22:36:46 +01:00
if (LastSyncWasFull)
2021-08-17 15:04:50 +02:00
{
2021-11-19 22:36:46 +01:00
if (VehDoors != null && VehDoors != LastVehDoors)
2021-08-17 15:04:50 +02:00
{
2021-11-19 22:36:46 +01:00
int doorLength = VehDoors.Length;
if (VehDoors.Length != 0)
{
2021-11-19 22:36:46 +01:00
for (int i = 0; i < (doorLength - 1); i++)
{
2021-11-19 22:36:46 +01:00
VehicleDoor door = MainVehicle.Doors[(VehicleDoorIndex)i];
VehicleDoors aDoor = VehDoors[i];
if (aDoor.Broken)
2021-08-17 15:04:50 +02:00
{
2021-11-19 22:36:46 +01:00
if (!door.IsBroken)
{
door.Break();
}
continue;
}
else if (!aDoor.Broken && door.IsBroken)
{
// Repair?
//MainVehicle.Repair();
2021-08-17 15:04:50 +02:00
}
2021-11-19 22:36:46 +01:00
if (aDoor.FullyOpen)
2021-08-17 15:04:50 +02:00
{
2021-11-19 22:36:46 +01:00
if (!door.IsFullyOpen)
{
door.Open(false, true);
}
continue;
2021-08-17 15:04:50 +02:00
}
2021-11-19 22:36:46 +01:00
else if (aDoor.Open)
2021-08-17 15:04:50 +02:00
{
2021-11-19 22:36:46 +01:00
if (!door.IsOpen)
{
door.Open();
}
door.AngleRatio = aDoor.AngleRatio;
continue;
2021-08-17 15:04:50 +02:00
}
2021-11-19 22:36:46 +01:00
door.Close(true);
}
2021-08-17 15:04:50 +02:00
}
2021-11-19 22:36:46 +01:00
LastVehDoors = VehDoors;
}
2021-11-19 22:36:46 +01:00
if (VehTires != default && LastVehTires != VehTires)
{
2021-11-19 22:36:46 +01:00
foreach (var wheel in MainVehicle.Wheels.GetAllWheels())
{
2021-11-19 22:36:46 +01:00
if ((VehTires & 1 << (int)wheel.BoneId) != 0)
{
wheel.Puncture();
wheel.Burst();
}
}
2021-11-19 22:36:46 +01:00
LastVehTires = VehTires;
}
}
2021-07-12 23:50:24 +02:00
}
2021-07-13 16:30:33 +02:00
if (VehicleSteeringAngle != MainVehicle.SteeringAngle)
{
MainVehicle.CustomSteeringAngle((float)(Math.PI / 180) * VehicleSteeringAngle);
2021-07-13 16:30:33 +02:00
}
2021-07-13 12:00:37 +02:00
2021-07-11 03:05:07 +02:00
// Good enough for now, but we need to create a better sync
2021-08-26 17:01:32 +02:00
if (CurrentVehicleSpeed > 0.05f && MainVehicle.IsInRange(VehiclePosition, 7.0f))
2021-07-11 00:51:46 +02:00
{
2021-08-15 12:27:24 +02:00
int forceMultiplier = (Game.Player.Character.IsInVehicle() && MainVehicle.IsTouching(Game.Player.Character.CurrentVehicle)) ? 1 : 3;
2021-08-17 18:41:09 +02:00
MainVehicle.Velocity = VehicleVelocity + forceMultiplier * (VehiclePosition - MainVehicle.Position);
MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f);
2021-11-19 22:08:15 +01:00
VehicleStopTime = Util.GetTickCount64();
2021-07-11 03:05:07 +02:00
}
2021-11-19 22:08:15 +01:00
else if ((Util.GetTickCount64() - VehicleStopTime) <= 1000)
2021-07-11 03:05:07 +02:00
{
2021-11-19 22:08:15 +01:00
Vector3 posTarget = Util.LinearVectorLerp(MainVehicle.Position, VehiclePosition + (VehiclePosition - MainVehicle.Position), Util.GetTickCount64() - VehicleStopTime, 1000);
2021-08-17 18:41:09 +02:00
2021-07-11 03:05:07 +02:00
MainVehicle.PositionNoOffset = posTarget;
2021-08-17 18:41:09 +02:00
MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f);
2021-07-11 00:51:46 +02:00
}
else
{
2021-08-17 18:41:09 +02:00
MainVehicle.PositionNoOffset = VehiclePosition;
2021-07-11 03:05:07 +02:00
MainVehicle.Quaternion = VehicleRotation;
2021-07-11 00:51:46 +02:00
}
2021-07-10 23:41:28 +02:00
#endregion
}
2021-07-12 23:50:24 +02:00
#region -- PEDALING --
/*
* Thanks to @oldnapalm.
*/
private string PedalingAnimDict()
2021-07-12 23:50:24 +02:00
{
2021-12-12 19:31:06 +01:00
switch ((VehicleHash)CurrentVehicleModelHash)
2021-07-12 23:50:24 +02:00
{
case VehicleHash.Bmx:
return "veh@bicycle@bmx@front@base";
case VehicleHash.Cruiser:
return "veh@bicycle@cruiserfront@base";
case VehicleHash.Scorcher:
return "veh@bicycle@mountainfront@base";
default:
return "veh@bicycle@roadfront@base";
}
}
private string PedalingAnimName(bool fast)
{
return fast ? "fast_pedal_char" : "cruise_pedal_char";
}
private void StartPedalingAnim(bool fast)
{
2021-11-19 12:30:47 +01:00
Character.Task.PlayAnimation(PedalingAnimDict(), PedalingAnimName(fast), 8.0f, -8.0f, -1, AnimationFlags.Loop | AnimationFlags.AllowRotation, 1.0f);
2021-07-12 23:50:24 +02:00
}
private void StopPedalingAnim(bool fast)
{
Character.Task.ClearAnimation(PedalingAnimDict(), PedalingAnimName(fast));
}
#endregion
private void DisplayOnFoot()
{
if (Character.IsInVehicle())
{
Character.Task.LeaveVehicle();
}
2021-07-11 03:05:07 +02:00
if (MainVehicle != null)
{
MainVehicle = null;
}
2021-07-10 23:41:28 +02:00
if (IsOnFire && !Character.IsOnFire)
{
Character.IsInvincible = false;
Function.Call(Hash.START_ENTITY_FIRE, Character.Handle);
return;
}
else if (!IsOnFire && Character.IsOnFire)
{
Function.Call(Hash.STOP_ENTITY_FIRE, Character.Handle);
Character.IsInvincible = true;
if (Character.IsDead)
{
Character.Resurrect();
}
}
2021-07-07 13:36:25 +02:00
if (IsJumping && !LastIsJumping)
{
Character.Task.Jump();
}
LastIsJumping = IsJumping;
if (IsRagdoll && !Character.IsRagdoll)
{
Character.CanRagdoll = true;
Character.Ragdoll();
return;
}
else if (!IsRagdoll && Character.IsRagdoll)
{
Character.CancelRagdoll();
Character.CanRagdoll = false;
}
if (IsJumping || IsOnFire)
{
return;
}
if (IsReloading)
{
if (!Character.IsReloading)
{
Character.Task.ClearAll();
Character.Task.ReloadWeapon();
}
if (Character.IsInRange(Position, 0.5f))
{
return;
}
2021-07-07 13:36:25 +02:00
}
if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeaponHash)
{
Character.Weapons.RemoveAll();
Character.Weapons.Give((WeaponHash)CurrentWeaponHash, -1, true, true);
}
if (IsShooting)
{
if (!Character.IsInRange(Position, 0.5f))
{
Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, Position.X, Position.Y,
Position.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, Speed == 3 ? 3f : 2.5f, true, 0x3F000000, 0x40800000, false, 0, false,
unchecked((int)FiringPattern.FullAuto));
2021-07-07 13:36:25 +02:00
}
else
{
Function.Call(Hash.TASK_SHOOT_AT_COORD, Character.Handle, AimCoords.X, AimCoords.Y, AimCoords.Z, 1500, unchecked((int)FiringPattern.FullAuto));
2021-07-07 13:36:25 +02:00
}
}
else if (IsAiming)
{
if (!Character.IsInRange(Position, 0.5f))
{
Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, Position.X, Position.Y,
Position.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, Speed == 3 ? 3f : 2.5f, false, 0x3F000000, 0x40800000, false, 512, false,
unchecked((int)FiringPattern.FullAuto));
2021-07-07 13:36:25 +02:00
}
else
{
Character.Task.AimAt(AimCoords, 100);
2021-07-07 13:36:25 +02:00
}
}
else
{
WalkTo();
}
}
2021-08-18 11:47:59 +02:00
private bool CreateCharacter(string username)
2021-07-07 13:36:25 +02:00
{
2021-08-26 17:01:32 +02:00
if (PedBlip != null && PedBlip.Exists())
{
PedBlip.Delete();
PedBlip = null;
}
2021-07-07 13:36:25 +02:00
LastProps = Props;
2021-12-12 19:31:06 +01:00
Model characterModel = CurrentModelHash.ModelRequest();
2021-08-18 11:47:59 +02:00
if (characterModel == null)
{
2021-12-12 22:00:06 +01:00
//GTA.UI.Notification.Show($"~r~(Character)Model ({CurrentModelHash}) cannot be loaded!");
2021-12-12 19:31:06 +01:00
ModelNotFound = 1;
2021-08-18 11:47:59 +02:00
return false;
}
Character = World.CreatePed(characterModel, Position, Rotation.Z);
2021-12-12 22:52:57 +01:00
characterModel.MarkAsNoLongerNeeded();
2021-07-07 13:36:25 +02:00
Character.RelationshipGroup = Main.RelationshipGroup;
if (IsInVehicle)
{
Character.IsVisible = false;
}
2021-07-07 13:36:25 +02:00
Character.BlockPermanentEvents = true;
Character.CanRagdoll = false;
Character.IsInvincible = true;
Character.Health = Health;
if (username != null)
{
// Add a new blip for the ped
Character.AddBlip();
Character.AttachedBlip.Color = BlipColor.White;
Character.AttachedBlip.Scale = 0.8f;
Character.AttachedBlip.Name = username;
2021-07-09 00:20:09 +02:00
Function.Call(Hash.SET_PED_CAN_EVASIVE_DIVE, Character.Handle, false);
Function.Call(Hash.SET_PED_GET_OUT_UPSIDE_DOWN_VEHICLE, Character.Handle, false);
2021-07-07 13:36:25 +02:00
}
foreach (KeyValuePair<int, int> prop in Props)
{
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, Character.Handle, prop.Key, prop.Value, 0, 0);
}
2021-08-18 11:47:59 +02:00
2021-08-22 13:59:15 +02:00
Function.Call(Hash.SET_PED_CAN_BE_TARGETTED, Character, Game.Player, true);
2021-08-18 11:47:59 +02:00
return true;
2021-07-07 13:36:25 +02:00
}
private bool LastMoving;
private void WalkTo()
{
2021-07-12 09:24:31 +02:00
if (!Character.IsInRange(Position, 6.0f) && (LastMoving = true))
2021-07-07 13:36:25 +02:00
{
Character.Position = Position;
Character.Rotation = Rotation;
}
else
{
Vector3 predictPosition = Position + (Position - Character.Position) + Velocity;
float range = predictPosition.DistanceToSquared(Character.Position);
switch (Speed)
{
case 1:
if ((!Character.IsWalking || range > 0.25f) && (LastMoving = true))
{
float nrange = range * 2;
if (nrange > 1.0f)
{
nrange = 1.0f;
}
Character.Task.GoStraightTo(predictPosition);
Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, nrange);
2021-07-07 13:36:25 +02:00
}
break;
case 2:
if ((!Character.IsRunning || range > 0.50f) && (LastMoving = true))
{
Character.Task.RunTo(predictPosition, true);
Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f);
2021-07-07 13:36:25 +02:00
}
break;
case 3:
if ((!Character.IsSprinting || range > 0.75f) && (LastMoving = true))
{
Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character.Handle, predictPosition.X, predictPosition.Y, predictPosition.Z, 3.0f, -1, 0.0f, 0.0f);
Function.Call(Hash.SET_RUN_SPRINT_MULTIPLIER_FOR_PLAYER, Character.Handle, 1.49f);
Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character.Handle, 1.0f);
2021-07-07 13:36:25 +02:00
}
break;
default:
if (!Character.IsInRange(Position, 0.5f))
{
Character.Task.RunTo(Position, true, 500);
}
else if (LastMoving && (LastMoving = false))
{
2021-07-12 09:24:31 +02:00
Character.Task.StandStill(1000);
2021-07-07 13:36:25 +02:00
}
break;
}
}
}
}
}