From c20118aac146fd8de6419a0fda088f39a9413b24 Mon Sep 17 00:00:00 2001 From: Sardelka9515 Date: Thu, 18 Aug 2022 20:44:39 +0800 Subject: [PATCH] Vehicle cleanup --- RageCoop.Client/Networking/Send.cs | 4 +- RageCoop.Client/RageCoop.Client.csproj | 1 + .../Sync/Entities/SyncedVehicle.Members.cs | 85 +++++++++ .../Sync/Entities/SyncedVehicle.cs | 177 +++++------------- RageCoop.Client/Util/VehicleExtensions.cs | 15 +- 5 files changed, 147 insertions(+), 135 deletions(-) create mode 100644 RageCoop.Client/Sync/Entities/SyncedVehicle.Members.cs diff --git a/RageCoop.Client/Networking/Send.cs b/RageCoop.Client/Networking/Send.cs index e4d48818..386dc612 100644 --- a/RageCoop.Client/Networking/Send.cs +++ b/RageCoop.Client/Networking/Send.cs @@ -121,7 +121,7 @@ public static void SendVehicle(SyncedVehicle v, bool full) var packet = SendPackets.VehicelPacket; packet.ID =v.ID; packet.OwnerID=v.OwnerID; - packet.Flags = veh.GetVehicleFlags(); + packet.Flags = v.GetVehicleFlags(); packet.SteeringAngle = veh.SteeringAngle; packet.Position = veh.ReadPosition(); packet.Velocity=veh.Velocity; @@ -129,9 +129,7 @@ public static void SendVehicle(SyncedVehicle v, bool full) packet.RotationVelocity=veh.RotationVelocity; packet.ThrottlePower = veh.ThrottlePower; packet.BrakePower = veh.BrakePower; - if (v.LastVelocity==default) {v.LastVelocity=packet.Velocity; } v.LastSentStopWatch.Restart(); - v.LastVelocity= packet.Velocity; if (packet.Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering)) { packet.DeluxoWingRatio=v.MainVehicle.GetDeluxoWingRatio(); } if (full) { diff --git a/RageCoop.Client/RageCoop.Client.csproj b/RageCoop.Client/RageCoop.Client.csproj index 88568f76..4d78bf10 100644 --- a/RageCoop.Client/RageCoop.Client.csproj +++ b/RageCoop.Client/RageCoop.Client.csproj @@ -61,6 +61,7 @@ + diff --git a/RageCoop.Client/Sync/Entities/SyncedVehicle.Members.cs b/RageCoop.Client/Sync/Entities/SyncedVehicle.Members.cs new file mode 100644 index 00000000..61362460 --- /dev/null +++ b/RageCoop.Client/Sync/Entities/SyncedVehicle.Members.cs @@ -0,0 +1,85 @@ +using System; +using RageCoop.Core; +using GTA; +using System.Diagnostics; +using System.Collections.Generic; +using GTA.Math; +using GTA.Native; + +namespace RageCoop.Client{ + public partial class SyncedVehicle{ + public Vehicle MainVehicle { get; internal set; } + public Stopwatch LastSyncedStopWatch = new Stopwatch(); + + + #region -- SYNC DATA -- + internal Vector3 RotationVelocity { get; set; } + internal float SteeringAngle { get; set; } + internal float ThrottlePower { get; set; } + internal float BrakePower { get; set; } + internal float DeluxoWingRatio { get; set; } = -1; + + + internal byte LandingGear { get; set; } + internal VehicleRoofState RoofState { get; set; } + internal VehicleDamageModel DamageModel { get; set; } + internal byte[] Colors { get; set; } + internal Dictionary Mods { get; set; } + internal float EngineHealth { get; set; } + internal VehicleLockStatus LockStatus { get; set; } + internal byte RadioStation = 255; + internal string LicensePlate { get; set; } + internal int Livery { get; set; } = -1; + internal VehicleDataFlags Flags { get; set; } + + #endregion + + #region FLAGS + + internal bool EngineRunning { get => Flags.HasVehFlag(VehicleDataFlags.IsEngineRunning); } + internal bool Transformed { get => Flags.HasVehFlag(VehicleDataFlags.IsTransformed); } + internal bool HornActive { get => Flags.HasVehFlag(VehicleDataFlags.IsHornActive); } + internal bool LightsOn { get => Flags.HasVehFlag(VehicleDataFlags.AreLightsOn); } + internal bool BrakeLightsOn { get => Flags.HasVehFlag(VehicleDataFlags.AreBrakeLightsOn); } + internal bool HighBeamsOn { get => Flags.HasVehFlag(VehicleDataFlags.AreHighBeamsOn); } + internal bool SireneActive { get => Flags.HasVehFlag(VehicleDataFlags.IsSirenActive); } + internal bool IsDead { get => Flags.HasVehFlag(VehicleDataFlags.IsDead); } + internal bool IsDeluxoHovering { get => Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering); } + #endregion + + #region FIXED-DATA + + internal bool IsFlipped + { + get => IsMotorcycle || ((Quaternion * Vector3.RelativeTop).Z - (Quaternion * Vector3.RelativeBottom).Z) < 0.5; + } + internal bool IsMotorcycle; + internal bool IsAircraft; + internal bool HasRocketBoost; + internal bool HasParachute; + internal bool HasRoof; + internal bool IsSubmarineCar; + internal bool IsDeluxo; + + #endregion + + #region PRIVATE + private byte[] _lastVehicleColors = new byte[] { 0, 0 }; + private Dictionary _lastVehicleMods = new Dictionary(); + private bool _lastHornActive = false; + private bool _lastTransformed = false; + internal int _lastLivery = -1; + List _predictedTrace = new List(); + List _orgTrace = new List(); + + float _elapsed; + #endregion + + #region OUTGOING + internal float LastNozzleAngle { get; set; } + + internal float LastEngineHealth { get; set; } + internal Vector3 LastVelocity { get; set; } + #endregion + } +} \ No newline at end of file diff --git a/RageCoop.Client/Sync/Entities/SyncedVehicle.cs b/RageCoop.Client/Sync/Entities/SyncedVehicle.cs index fefc0b97..e3fd2305 100644 --- a/RageCoop.Client/Sync/Entities/SyncedVehicle.cs +++ b/RageCoop.Client/Sync/Entities/SyncedVehicle.cs @@ -12,7 +12,7 @@ namespace RageCoop.Client /// /// A synchronized vehicle instance /// - public class SyncedVehicle : SyncedEntity + public partial class SyncedVehicle : SyncedEntity { #region -- CONSTRUCTORS -- @@ -28,7 +28,18 @@ internal SyncedVehicle(Vehicle v) MainVehicle=v; MainVehicle.CanPretendOccupants=false; OwnerID=Main.LocalPlayerID; - + 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; } /// @@ -47,59 +58,7 @@ internal SyncedVehicle(int id) /// /// VehicleSeat,ID /// - public Vehicle MainVehicle { get; internal set; } - public Stopwatch LastSyncedStopWatch = new Stopwatch(); - - - #region LAST STATE - private byte[] _lastVehicleColors = new byte[] { 0, 0 }; - private Dictionary _lastVehicleMods = new Dictionary(); - #endregion - - #region -- CRITICAL STUFF -- - internal Vector3 RotationVelocity { get; set; } - internal float SteeringAngle { get; set; } - internal float ThrottlePower { get; set; } - internal float BrakePower { get; set; } - internal float DeluxoWingRatio { get; set; } = -1; - internal bool IsFlipped - { - get => _isMotorcycle || ((Quaternion * Vector3.RelativeTop).Z - (Quaternion * Vector3.RelativeBottom).Z) < 0.5; - } - private bool _isMotorcycle; - #endregion - #region FLAGS - internal bool EngineRunning { get => Flags.HasVehFlag(VehicleDataFlags.IsEngineRunning); } - private bool _lastTransformed = false; - internal bool Transformed { get => Flags.HasVehFlag(VehicleDataFlags.IsTransformed); } - private bool _lastHornActive = false; - internal bool HornActive { get => Flags.HasVehFlag(VehicleDataFlags.IsHornActive); } - internal bool LightsOn { get => Flags.HasVehFlag(VehicleDataFlags.AreLightsOn); } - internal bool BrakeLightsOn { get => Flags.HasVehFlag(VehicleDataFlags.AreBrakeLightsOn); } - internal bool HighBeamsOn { get => Flags.HasVehFlag(VehicleDataFlags.AreHighBeamsOn); } - internal bool SireneActive { get => Flags.HasVehFlag(VehicleDataFlags.IsSirenActive); } - internal bool IsDead { get => Flags.HasVehFlag(VehicleDataFlags.IsDead); } - internal bool IsDeluxoHovering { get => Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering); } - #endregion - - #region -- VEHICLE STATE -- - internal VehicleDataFlags Flags { get; set; } - - internal byte LandingGear { get; set; } - internal VehicleRoofState RoofState { get; set; } - internal VehicleDamageModel DamageModel { get; set; } - internal byte[] Colors { get; set; } - internal Dictionary Mods { get; set; } - internal float EngineHealth { get; set; } - internal VehicleLockStatus LockStatus { get; set; } - internal byte RadioStation = 255; - internal string LicensePlate { get; set; } - internal int _lastLivery = -1; - internal int Livery { get; set; } = -1; - List _predictedTrace = new List(); - List _orgTrace = new List(); - - #endregion + internal override void Update() { #if DEBUG_VEH @@ -113,12 +72,11 @@ internal override void Update() } #endif -#region -- INITIAL CHECK -- // Check if all data avalible - if (!IsReady || Owner==null ) { return; } -#endregion -#region -- CHECK EXISTENCE -- + if (!IsReady || Owner == null) { return; } + + // Check existence if ((MainVehicle == null) || (!MainVehicle.Exists()) || (MainVehicle.Model != Model)) { if (!CreateVehicle()) @@ -126,12 +84,12 @@ internal override void Update() return; } } + // Skip update if no new sync message has arrived. - if (!NeedUpdate) { - return; + if (!NeedUpdate) + { + return; } -#endregion -#region -- SYNC CRITICAL -- if (SteeringAngle != MainVehicle.SteeringAngle) { @@ -141,7 +99,7 @@ internal override void Update() MainVehicle.BrakePower=BrakePower; var v = Main.P.CurrentVehicle; DisplayVehicle(v != null && MainVehicle.IsTouching(v)); -#region FLAGS + if (IsDead) { if (MainVehicle.IsDead) @@ -191,24 +149,9 @@ internal override void Update() MainVehicle.AreHighBeamsOn = HighBeamsOn; } - if (MainVehicle.IsSubmarineCar) - { - if (Transformed) - { - if (!_lastTransformed) - { - _lastTransformed = true; - Function.Call(Hash._TRANSFORM_VEHICLE_TO_SUBMARINE, MainVehicle.Handle, false); - } - } - else if (_lastTransformed) - { - _lastTransformed = false; - Function.Call(Hash._TRANSFORM_SUBMARINE_TO_VEHICLE, MainVehicle.Handle, false); - } - } + - if (MainVehicle.IsAircraft) + if (IsAircraft) { if (LandingGear != (byte)MainVehicle.LandingGearState) { @@ -236,30 +179,37 @@ internal override void Update() MainVehicle.SoundHorn(1); } - if (MainVehicle.HasRoof && MainVehicle.RoofState!=RoofState) + if (HasRoof && MainVehicle.RoofState!=RoofState) { MainVehicle.RoofState=RoofState; } - if(MainVehicle.HasRocketBoost){ - if(Flags.HasFlag(VehicleDataFlags.IsRocketBoostActive) ) + 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) { - if(!MainVehicle.IsRocketBoostActive()){ - MainVehicle.SetRocketBoostActive(true); + if (!_lastTransformed) + { + _lastTransformed = true; + Function.Call(Hash._TRANSFORM_VEHICLE_TO_SUBMARINE, MainVehicle.Handle, false); } } - else if(MainVehicle.IsRocketBoostActive()){ - MainVehicle.SetRocketBoostActive(false); + else if (_lastTransformed) + { + _lastTransformed = false; + Function.Call(Hash._TRANSFORM_SUBMARINE_TO_VEHICLE, MainVehicle.Handle, false); } } - if(MainVehicle.HasParachute){ - if(Flags.HasFlag(VehicleDataFlags.IsParachuteActive)){ - if(!MainVehicle.IsParachuteActive()){ - MainVehicle.SetParachuteActive(true); - } - } - else if(MainVehicle.IsParachuteActive()){ - MainVehicle.SetParachuteActive(false); + else if(IsDeluxo && IsDeluxoHovering!=MainVehicle.IsDeluxoHovering()){ + MainVehicle.SetDeluxoHoverState(IsDeluxoHovering); + if(IsDeluxoHovering){ + MainVehicle.SetDeluxoWingRatio(DeluxoWingRatio); } } @@ -268,24 +218,7 @@ internal override void Update() } MainVehicle.LockStatus=LockStatus; - if (IsDeluxoHovering) - { - if (!MainVehicle.IsDeluxoHovering()) - { - MainVehicle.SetDeluxoHoverState(true); - } - MainVehicle.SetDeluxoWingRatio(DeluxoWingRatio); - } - else if (Model==1483171323) - { - if (MainVehicle.IsDeluxoHovering()) - { - MainVehicle.SetDeluxoHoverState(false); - } - } -#endregion -#endregion if (LastFullSynced>=LastUpdated) { #region -- SYNC STATE -- @@ -326,25 +259,24 @@ internal override void Update() } LastUpdated=Main.Ticked; } - float _elapsed; - Vector3 _predictedPos; void DisplayVehicle(bool touching) { _elapsed = Owner.PacketTravelTime+0.001f*LastSyncedStopWatch.ElapsedMilliseconds; - _predictedPos = Position+_elapsed*Velocity; + Position += _elapsed*Velocity; var current = MainVehicle.ReadPosition(); - var dist = current.DistanceTo(_predictedPos); - var cali = dist*(_predictedPos - current); + var dist = current.DistanceTo(Position); + var cali = dist*(Position - current); if (Velocity.Length()<0.1) { cali*=10; } if (dist>10) { - MainVehicle.Position = _predictedPos; + MainVehicle.Position = Position; MainVehicle.Velocity = Velocity; MainVehicle.Quaternion = Quaternion; return; } MainVehicle.Velocity = Velocity+cali; + if (IsFlipped) { MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.ReadQuaternion(), Quaternion, 0.5f); @@ -420,7 +352,7 @@ private bool CreateVehicle() w.Fix(); } if (IsInvincible) { MainVehicle.IsInvincible=true; } - _isMotorcycle=Model.IsMotorcycle; + SetUpFixedData(); Model.MarkAsNoLongerNeeded(); return true; } @@ -461,11 +393,6 @@ private void StopPedalingAnim(bool fast) } #endregion -#region OUTGOING - internal float LastNozzleAngle { get; set; } - internal float LastEngineHealth { get; set; } - internal Vector3 LastVelocity { get; set; } -#endregion } } diff --git a/RageCoop.Client/Util/VehicleExtensions.cs b/RageCoop.Client/Util/VehicleExtensions.cs index d48a2b40..cf2aa578 100644 --- a/RageCoop.Client/Util/VehicleExtensions.cs +++ b/RageCoop.Client/Util/VehicleExtensions.cs @@ -10,8 +10,9 @@ internal static class VehicleExtensions { #region VEHICLE - public static VehicleDataFlags GetVehicleFlags(this Vehicle veh) + public static VehicleDataFlags GetVehicleFlags(this SyncedVehicle v) { + var veh=v.MainVehicle; VehicleDataFlags flags = 0; if (veh.IsEngineRunning) @@ -49,27 +50,27 @@ public static VehicleDataFlags GetVehicleFlags(this Vehicle veh) flags |= VehicleDataFlags.IsHornActive; } - if (veh.IsSubmarineCar && Function.Call(Hash._GET_IS_SUBMARINE_VEHICLE_TRANSFORMED, veh.Handle)) + if (v.IsSubmarineCar && Function.Call(Hash._GET_IS_SUBMARINE_VEHICLE_TRANSFORMED, veh.Handle)) { flags |= VehicleDataFlags.IsTransformed; } - if (veh.IsAircraft) + if (v.IsAircraft) { flags |= VehicleDataFlags.IsAircraft; } - if (veh.Model.Hash==1483171323 && veh.IsDeluxoHovering()) + if (v.IsDeluxo && veh.IsDeluxoHovering()) { flags|= VehicleDataFlags.IsDeluxoHovering; } - if (veh.HasRoof) + if (v.HasRoof) { flags|=VehicleDataFlags.HasRoof; } - if (veh.IsRocketBoostActive()) + if (v.HasRocketBoost && veh.IsRocketBoostActive()) { flags|=VehicleDataFlags.IsRocketBoostActive; } - if(veh.IsParachuteActive()){ + if(v.HasParachute && veh.IsParachuteActive()){ flags|=VehicleDataFlags.IsParachuteActive; } if (veh.IsOnFire)