From 114e59a3bda67753613fab6118152f1358f8d6eb Mon Sep 17 00:00:00 2001 From: Sardelka9515 Date: Thu, 23 Mar 2023 22:30:39 +0800 Subject: [PATCH] Add toggle mods sync --- Client/Scripts/Networking/Receive.cs | 1 + Client/Scripts/Networking/Send.cs | 2 +- .../Vehicle/SyncedVehicle.Variables.cs | 3 ++- .../Sync/Entities/Vehicle/SyncedVehicle.cs | 12 ++++++++-- Client/Scripts/Util/VehicleExtensions.cs | 23 ++++++++----------- Core/Packets/VehicleSync.cs | 6 ++++- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Client/Scripts/Networking/Receive.cs b/Client/Scripts/Networking/Receive.cs index a94f0ec..e297340 100644 --- a/Client/Scripts/Networking/Receive.cs +++ b/Client/Scripts/Networking/Receive.cs @@ -341,6 +341,7 @@ namespace RageCoop.Client v.DamageModel = packet.DamageModel; v.EngineHealth = packet.EngineHealth; v.Mods = packet.Mods; + v.ToggleModsMask = packet.ToggleModsMask; v.Model = packet.ModelHash; v.Colors = packet.Colors; v.LandingGear = packet.LandingGear; diff --git a/Client/Scripts/Networking/Send.cs b/Client/Scripts/Networking/Send.cs index a631d4c..2576ed3 100644 --- a/Client/Scripts/Networking/Send.cs +++ b/Client/Scripts/Networking/Send.cs @@ -130,7 +130,7 @@ namespace RageCoop.Client packet.DamageModel = veh.GetVehicleDamageModel(); packet.LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0; packet.RoofState = (byte)veh.RoofState; - packet.Mods = veh.Mods.GetVehicleMods(); + packet.Mods = v.GetVehicleMods(out packet.ToggleModsMask); packet.ModelHash = veh.Model.Hash; packet.EngineHealth = veh.EngineHealth; packet.LockStatus = veh.LockStatus; diff --git a/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.Variables.cs b/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.Variables.cs index aa38549..6a140c8 100644 --- a/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.Variables.cs +++ b/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.Variables.cs @@ -32,6 +32,7 @@ namespace RageCoop.Client internal int Livery { get; set; } = -1; internal VehicleDataFlags Flags { get; set; } internal ushort ExtrasMask; + internal byte ToggleModsMask; #endregion @@ -67,7 +68,7 @@ namespace RageCoop.Client #endregion #region PRIVATE - + private byte _lastToggleMods; private (byte, byte) _lastVehicleColors; private ushort _lastExtras; private (int, int)[] _lastVehicleMods = Array.Empty<(int, int)>(); diff --git a/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.cs b/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.cs index ce290bd..1d9f5be 100644 --- a/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.cs +++ b/Client/Scripts/Sync/Entities/Vehicle/SyncedVehicle.cs @@ -131,16 +131,24 @@ namespace RageCoop.Client _lastVehicleColors = Colors; } - + MainVehicle.EngineHealth = EngineHealth; if (Mods != null && !Mods.SequenceEqual(_lastVehicleMods)) { Call(SET_VEHICLE_MOD_KIT, MainVehicle, 0); foreach (var mod in Mods) MainVehicle.Mods[(VehicleModType)mod.Item1].Index = mod.Item2; - + _lastVehicleMods = Mods; } + if (ToggleModsMask != _lastToggleMods) + { + for (int i = 0; i < 7; i++) + { + Call(TOGGLE_VEHICLE_MOD, MainVehicle.Handle, i + 17, (ToggleModsMask & (1 << i)) != 0); + } + _lastToggleMods = ToggleModsMask; + } if (Call(GET_VEHICLE_NUMBER_PLATE_TEXT, MainVehicle) != LicensePlate) Call(SET_VEHICLE_NUMBER_PLATE_TEXT, MainVehicle, LicensePlate); diff --git a/Client/Scripts/Util/VehicleExtensions.cs b/Client/Scripts/Util/VehicleExtensions.cs index 698edda..23c4dc2 100644 --- a/Client/Scripts/Util/VehicleExtensions.cs +++ b/Client/Scripts/Util/VehicleExtensions.cs @@ -47,14 +47,22 @@ namespace RageCoop.Client return flags; } - public static (int, int)[] GetVehicleMods(this VehicleModCollection mods) + public static (int, int)[] GetVehicleMods(this SyncedVehicle sv, out byte togglesMask) { - var modsArr = mods.ToArray(); + var modsArr = sv.MainVehicle.Mods.ToArray(); var result = new (int, int)[modsArr.Length]; for (int i = 0; i < modsArr.Length; i++) { result[i] = ((int)modsArr[i].Type, modsArr[i].Index); } + togglesMask = 0; + for (int i = 0; i < 6; i++) + { + if (Call(IS_TOGGLE_MOD_ON, sv.MainVehicle.Handle, i + 17)) + { + togglesMask |= (byte)(1 << i); + } + } return result; } @@ -154,17 +162,6 @@ namespace RageCoop.Client veh.IsRightHeadLightBroken = model.RightHeadLightBroken > 0; } - public static Dictionary GetPassengers(this Vehicle veh) - { - var ps = new Dictionary(); - var d = veh.Driver; - if (d != null && d.IsSittingInVehicle()) ps.Add(-1, d.GetSyncEntity().ID); - foreach (var p in veh.Passengers) - if (p.IsSittingInVehicle()) - ps.Add((int)p.SeatIndex, p.GetSyncEntity().ID); - return ps; - } - public static void SetDeluxoHoverState(this Vehicle deluxo, bool hover) { Call(SET_SPECIAL_FLIGHT_MODE_TARGET_RATIO, deluxo, hover ? 1f : 0f); diff --git a/Core/Packets/VehicleSync.cs b/Core/Packets/VehicleSync.cs index 588e7f7..d684ab6 100644 --- a/Core/Packets/VehicleSync.cs +++ b/Core/Packets/VehicleSync.cs @@ -63,7 +63,6 @@ namespace RageCoop.Core // Write vehicle mods // Write the count of mods m.Write((short)Mods.Length); - // Loop the dictionary and add the values foreach (var mod in Mods) { // Write the mod value @@ -71,6 +70,8 @@ namespace RageCoop.Core m.Write(mod.Item2); } + m.Write(ToggleModsMask); + if (!DamageModel.Equals(default(VehicleDamageModel))) { // Write boolean = true @@ -150,6 +151,8 @@ namespace RageCoop.Core // Read the mod value Mods[i] = (m.ReadInt32(), m.ReadInt32()); + ToggleModsMask = m.ReadByte(); + if (m.ReadBoolean()) // Read vehicle damage model DamageModel = new VehicleDamageModel @@ -189,6 +192,7 @@ namespace RageCoop.Core public (int, int)[] Mods { get; set; } + public byte ToggleModsMask; public VehicleDamageModel DamageModel { get; set; } public byte LandingGear { get; set; }