382 lines
12 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using GTA;
2022-05-22 15:55:26 +08:00
using GTA.Math;
2022-07-20 17:50:01 +08:00
using GTA.Native;
2022-05-22 15:55:26 +08:00
using RageCoop.Core;
2022-07-20 17:50:01 +08:00
using System;
using System.Collections.Generic;
2022-07-17 19:19:23 +08:00
using System.Diagnostics;
2022-08-16 19:51:56 +08:00
using System.Drawing;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
2022-07-01 13:54:18 +08:00
/// <summary>
/// A synchronized vehicle instance
/// </summary>
2022-08-18 20:44:39 +08:00
public partial class SyncedVehicle : SyncedEntity
2022-05-22 15:55:26 +08:00
{
#region -- CONSTRUCTORS --
/// <summary>
/// Create a local entity (outgoing sync)
/// </summary>
2022-07-01 13:54:18 +08:00
/// <param name="v"></param>
internal SyncedVehicle(Vehicle v)
2022-05-22 15:55:26 +08:00
{
ID=EntityPool.RequestNewID();
2022-05-22 15:55:26 +08:00
MainVehicle=v;
MainVehicle.CanPretendOccupants=false;
OwnerID=Main.LocalPlayerID;
2022-08-18 20:44:39 +08:00
SetUpFixedData();
}
private void SetUpFixedData(){
IsAircraft = MainVehicle.IsAircraft;
IsMotorcycle = MainVehicle.IsMotorcycle;
HasRocketBoost = MainVehicle.HasRocketBoost;
HasParachute = MainVehicle.HasParachute;
HasRoof = MainVehicle.HasRoof;
IsSubmarineCar=MainVehicle.IsSubmarineCar;
IsDeluxo=MainVehicle.Model==1483171323;
2022-05-22 15:55:26 +08:00
}
/// <summary>
/// Create an empty VehicleEntity
/// </summary>
internal SyncedVehicle()
2022-05-22 15:55:26 +08:00
{
}
internal SyncedVehicle(int id)
2022-05-22 15:55:26 +08:00
{
ID=id;
LastSynced=Main.Ticked;
}
#endregion
/// <summary>
/// VehicleSeat,ID
/// </summary>
2022-08-18 20:44:39 +08:00
internal override void Update()
2022-05-22 15:55:26 +08:00
{
2022-08-16 19:51:56 +08:00
#if DEBUG_VEH
foreach(var s in _predictedTrace)
{
World.DrawMarker(MarkerType.DebugSphere, s, default, default, new Vector3(0.3f, 0.3f, 0.3f), Color.AliceBlue);
}
foreach (var s in _orgTrace)
{
World.DrawMarker(MarkerType.DebugSphere, s, default, default, new Vector3(0.3f, 0.3f, 0.3f), Color.Orange);
}
#endif
2022-07-20 17:50:01 +08:00
2022-05-22 15:55:26 +08:00
// Check if all data avalible
2022-08-18 20:44:39 +08:00
if (!IsReady || Owner == null) { return; }
// Check existence
if ((MainVehicle == null) || (!MainVehicle.Exists()) || (MainVehicle.Model != Model))
2022-05-22 15:55:26 +08:00
{
if (!CreateVehicle())
{
return;
}
2022-05-22 15:55:26 +08:00
}
2022-08-22 19:25:03 +08:00
DisplayVehicle();
2022-07-17 19:19:23 +08:00
// Skip update if no new sync message has arrived.
2022-08-18 20:44:39 +08:00
if (!NeedUpdate)
{
return;
2022-07-21 09:53:07 +08:00
}
2022-07-30 12:39:09 +08:00
2022-05-22 15:55:26 +08:00
if (SteeringAngle != MainVehicle.SteeringAngle)
{
MainVehicle.CustomSteeringAngle((float)(Math.PI / 180) * SteeringAngle);
}
MainVehicle.ThrottlePower=ThrottlePower;
MainVehicle.BrakePower=BrakePower;
2022-08-18 20:44:39 +08:00
2022-07-17 12:22:11 +08:00
if (IsDead)
{
if (MainVehicle.IsDead)
{
return;
}
2022-08-11 14:48:19 +02:00
MainVehicle.Explode();
2022-07-17 12:22:11 +08:00
}
else
{
if (MainVehicle.IsDead)
{
2022-07-21 22:42:29 +08:00
Main.Delay(() =>
{
if (MainVehicle.IsDead && !IsDead)
{
MainVehicle.Repair();
}
},1000);
2022-07-17 12:22:11 +08:00
}
}
2022-07-17 20:12:25 +08:00
if (MainVehicle.IsOnFire)
{
if (!Flags.HasVehFlag(VehicleDataFlags.IsOnFire))
{
Function.Call(Hash.STOP_ENTITY_FIRE, MainVehicle);
}
}
else if (Flags.HasVehFlag(VehicleDataFlags.IsOnFire))
{
Function.Call(Hash.START_ENTITY_FIRE, MainVehicle);
}
2022-07-17 12:22:11 +08:00
if (EngineRunning != MainVehicle.IsEngineRunning)
{
MainVehicle.IsEngineRunning = EngineRunning;
}
if (LightsOn != MainVehicle.AreLightsOn)
{
MainVehicle.AreLightsOn = LightsOn;
}
if (HighBeamsOn != MainVehicle.AreHighBeamsOn)
{
MainVehicle.AreHighBeamsOn = HighBeamsOn;
}
2022-08-18 20:44:39 +08:00
2022-07-17 12:22:11 +08:00
2022-08-18 20:44:39 +08:00
if (IsAircraft)
2022-07-17 12:22:11 +08:00
{
if (LandingGear != (byte)MainVehicle.LandingGearState)
{
MainVehicle.LandingGearState = (VehicleLandingGearState)LandingGear;
}
}
else
{
if (MainVehicle.HasSiren && SireneActive != MainVehicle.IsSirenActive)
{
MainVehicle.IsSirenActive = SireneActive;
}
if (HornActive)
{
if (!_lastHornActive)
{
_lastHornActive = true;
MainVehicle.SoundHorn(99999);
}
}
else if (_lastHornActive)
{
_lastHornActive = false;
MainVehicle.SoundHorn(1);
}
2022-08-18 20:44:39 +08:00
if (HasRoof && MainVehicle.RoofState!=RoofState)
2022-07-17 12:22:11 +08:00
{
MainVehicle.RoofState=RoofState;
}
2022-08-18 20:44:39 +08:00
if(HasRocketBoost && Flags.HasFlag(VehicleDataFlags.IsRocketBoostActive) != MainVehicle.IsRocketBoostActive()){
MainVehicle.SetRocketBoostActive(Flags.HasFlag(VehicleDataFlags.IsRocketBoostActive));
}
if(HasParachute && Flags.HasFlag(VehicleDataFlags.IsParachuteActive) != MainVehicle.IsParachuteActive()){
MainVehicle.SetParachuteActive(Flags.HasFlag(VehicleDataFlags.IsParachuteActive));
}
if (IsSubmarineCar)
{
if (Transformed)
2022-08-17 23:18:06 +08:00
{
2022-08-18 20:44:39 +08:00
if (!_lastTransformed)
{
_lastTransformed = true;
Function.Call(Hash._TRANSFORM_VEHICLE_TO_SUBMARINE, MainVehicle.Handle, false);
2022-08-17 23:18:06 +08:00
}
}
2022-08-18 20:44:39 +08:00
else if (_lastTransformed)
{
_lastTransformed = false;
Function.Call(Hash._TRANSFORM_SUBMARINE_TO_VEHICLE, MainVehicle.Handle, false);
2022-08-17 23:18:06 +08:00
}
}
2022-08-23 13:45:45 +08:00
else if(IsDeluxo)
{
2022-08-18 20:44:39 +08:00
MainVehicle.SetDeluxoHoverState(IsDeluxoHovering);
2022-08-23 13:45:45 +08:00
if (IsDeluxoHovering)
{
2022-08-18 20:44:39 +08:00
MainVehicle.SetDeluxoWingRatio(DeluxoWingRatio);
2022-08-18 08:25:15 +08:00
}
}
2022-08-17 23:18:06 +08:00
2022-07-17 12:22:11 +08:00
Function.Call(Hash.SET_VEHICLE_BRAKE_LIGHTS, MainVehicle.Handle, BrakeLightsOn);
2022-08-22 11:36:52 +08:00
2022-07-17 12:22:11 +08:00
}
MainVehicle.LockStatus=LockStatus;
if (LastFullSynced>=LastUpdated)
2022-05-22 15:55:26 +08:00
{
if (Flags.HasVehFlag(VehicleDataFlags.Repaired))
{
MainVehicle.Repair();
}
2022-05-22 15:55:26 +08:00
if (Colors != null && Colors != _lastVehicleColors)
{
Function.Call(Hash.SET_VEHICLE_COLOURS, MainVehicle, Colors[0], Colors[1]);
_lastVehicleColors = Colors;
}
MainVehicle.EngineHealth=EngineHealth;
if (Mods != null && !Mods.Compare(_lastVehicleMods))
{
Function.Call(Hash.SET_VEHICLE_MOD_KIT, MainVehicle, 0);
foreach (KeyValuePair<int, int> mod in Mods)
{
MainVehicle.Mods[(VehicleModType)mod.Key].Index = mod.Value;
}
_lastVehicleMods = Mods;
}
2022-06-20 10:27:45 +08:00
if (Function.Call<string>(Hash.GET_VEHICLE_NUMBER_PLATE_TEXT, MainVehicle)!=LicensePlate)
{
2022-07-20 17:50:01 +08:00
Function.Call(Hash.SET_VEHICLE_NUMBER_PLATE_TEXT, MainVehicle, LicensePlate);
2022-06-20 10:27:45 +08:00
}
2022-07-09 22:18:00 +08:00
if (_lastLivery!=Livery)
{
Function.Call(Hash.SET_VEHICLE_LIVERY, MainVehicle, Livery);
_lastLivery=Livery;
}
2022-08-22 11:36:52 +08:00
MainVehicle.SetDamageModel(DamageModel);
2022-05-22 15:55:26 +08:00
}
LastUpdated=Main.Ticked;
}
2022-08-22 19:25:03 +08:00
void DisplayVehicle()
2022-07-17 19:19:23 +08:00
{
2022-08-22 21:59:01 +08:00
_predictedPosition = Predict(Position);
var current = MainVehicle.ReadPosition();
2022-08-18 22:08:06 +08:00
var dist = current.DistanceTo(_predictedPosition);
var cali = dist * (_predictedPosition - current);
if (Velocity.Length() < 0.1) { cali *= 10; }
if (dist > 10)
2022-07-17 19:19:23 +08:00
{
2022-08-18 22:08:06 +08:00
MainVehicle.Position = _predictedPosition;
2022-08-10 20:42:47 +08:00
MainVehicle.Velocity = Velocity;
2022-08-11 14:48:19 +02:00
MainVehicle.Quaternion = Quaternion;
return;
}
2022-08-22 19:27:25 +08:00
else if (dist > 0.03)
2022-08-22 19:25:03 +08:00
{
2022-08-22 19:27:25 +08:00
MainVehicle.Velocity = Velocity + cali;
2022-08-22 19:25:03 +08:00
}
2022-08-18 22:08:06 +08:00
2022-08-20 22:36:48 +08:00
Vector3 calirot;
if (IsFlipped || (calirot = GetCalibrationRotation()).Length()>50)
2022-08-11 14:48:19 +02:00
{
MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.ReadQuaternion(), Quaternion, 0.5f);
MainVehicle.RotationVelocity = RotationVelocity;
return;
}
2022-07-17 19:19:23 +08:00
else
{
2022-08-20 22:36:48 +08:00
MainVehicle.RotationVelocity = RotationVelocity + calirot * 0.2f;
2022-07-17 19:19:23 +08:00
}
}
2022-05-22 15:55:26 +08:00
private Vector3 GetCalibrationRotation()
{
2022-07-20 17:50:01 +08:00
var rot = Quaternion.LookRotation(Quaternion*Vector3.RelativeFront, Quaternion*Vector3.RelativeTop).ToEulerAngles();
var curRot = Quaternion.LookRotation(MainVehicle.ReadQuaternion()*Vector3.RelativeFront, MainVehicle.ReadQuaternion()*Vector3.RelativeTop).ToEulerAngles();
2022-07-20 17:50:01 +08:00
2022-06-17 15:26:27 +08:00
var r = (rot-curRot).ToDegree();
2022-05-22 15:55:26 +08:00
if (r.X>180) { r.X=r.X-360; }
2022-07-20 17:50:01 +08:00
else if (r.X<-180) { r.X=360+r.X; }
2022-05-22 15:55:26 +08:00
if (r.Y>180) { r.Y=r.Y-360; }
else if (r.Y<-180) { r.Y=360+r.Y; }
if (r.Z>180) { r.Z=r.Z-360; }
else if (r.Z<-180) { r.Z=360+r.Z; }
return r;
}
private bool CreateVehicle()
2022-05-22 15:55:26 +08:00
{
MainVehicle?.Delete();
MainVehicle = Util.CreateVehicle(Model, Position);
if (!Model.IsInCdImage)
{
// GTA.UI.Notification.Show($"~r~(Vehicle)Model ({CurrentVehicleModelHash}) cannot be loaded!");
return false;
}
else if (MainVehicle==null)
2022-05-22 15:55:26 +08:00
{
Model.Request();
return false;
2022-05-22 15:55:26 +08:00
}
lock (EntityPool.VehiclesLock)
{
EntityPool.Add(this);
2022-05-22 15:55:26 +08:00
}
2022-06-03 14:40:41 +08:00
MainVehicle.Quaternion = Quaternion;
2022-05-22 15:55:26 +08:00
if (MainVehicle.HasRoof)
{
2022-06-19 13:36:23 +08:00
MainVehicle.RoofState=RoofState;
2022-05-22 15:55:26 +08:00
}
2022-08-05 16:13:40 +08:00
foreach(var w in MainVehicle.Wheels)
{
w.Fix();
}
2022-07-02 17:14:56 +08:00
if (IsInvincible) { MainVehicle.IsInvincible=true; }
2022-08-18 20:44:39 +08:00
SetUpFixedData();
Model.MarkAsNoLongerNeeded();
return true;
2022-05-22 15:55:26 +08:00
}
2022-08-16 19:51:56 +08:00
#region -- PEDALING --
2022-05-22 15:55:26 +08:00
/*
* Thanks to @oldnapalm.
*/
private string PedalingAnimDict()
{
switch ((VehicleHash)Model)
2022-05-22 15:55:26 +08: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)
{
MainVehicle.Driver?.Task.PlayAnimation(PedalingAnimDict(), PedalingAnimName(fast), 8.0f, -8.0f, -1, AnimationFlags.Loop | AnimationFlags.AllowRotation, 1.0f);
}
private void StopPedalingAnim(bool fast)
{
MainVehicle.Driver.Task.ClearAnimation(PedalingAnimDict(), PedalingAnimName(fast));
}
2022-08-16 19:51:56 +08:00
#endregion
2022-05-22 15:55:26 +08:00
2022-05-22 15:55:26 +08:00
}
2022-08-17 01:54:14 +08:00
}