292 lines
9.8 KiB
C#
Raw Normal View History

2022-05-22 15:55:26 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using GTA;
using GTA.Math;
2022-05-22 15:55:26 +08:00
using Lidgren.Network;
2022-06-20 10:27:45 +08:00
using System.Linq;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
{
2022-07-01 14:39:43 +08:00
internal partial class Packets
2022-05-22 15:55:26 +08:00
{
public class VehicleSync : Packet
2022-05-22 15:55:26 +08:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.VehicleSync;
2022-05-22 15:55:26 +08:00
public int ID { get; set; }
public int OwnerID { get; set; }
2022-07-17 12:22:11 +08:00
public VehicleDataFlags Flags { get; set; }
public Vector3 Position { get; set; }
public Quaternion Quaternion { get; set; }
// public Vector3 Rotation { get; set; }
public Vector3 Velocity { get; set; }
public Vector3 Acceleration { get; set; }
public Vector3 RotationVelocity { get; set; }
public float ThrottlePower { get; set; }
public float BrakePower { get; set; }
public float SteeringAngle { get; set; }
public float DeluxoWingRatio { get; set; } = -1;
#region FULL-SYNC
2022-05-22 15:55:26 +08:00
public int ModelHash { get; set; }
public float EngineHealth { get; set; }
public byte[] Colors { get; set; }
public Dictionary<int, int> Mods { get; set; }
public VehicleDamageModel DamageModel { get; set; }
public byte LandingGear { get; set; }
2022-06-19 13:36:23 +08:00
public byte RoofState { get; set; }
2022-05-22 15:55:26 +08:00
public VehicleLockStatus LockStatus { get; set; }
2022-07-09 22:18:00 +08:00
public int Livery { get; set; } = -1;
2022-05-22 15:55:26 +08:00
/// <summary>
/// VehicleSeat,PedID
/// </summary>
public Dictionary<int, int> Passengers { get; set; }
2022-06-03 13:11:17 +08:00
public byte RadioStation { get; set; } = 255;
2022-06-20 10:27:45 +08:00
public string LicensePlate { get; set; }
#endregion
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-05-22 15:55:26 +08:00
{
List<byte> byteArray = new List<byte>(100);
2022-05-22 15:55:26 +08:00
byteArray.AddInt(ID);
byteArray.AddInt(OwnerID);
2022-07-17 12:22:11 +08:00
byteArray.AddUshort((ushort)Flags);
byteArray.AddVector3(Position);
byteArray.AddQuaternion(Quaternion);
byteArray.AddVector3(Velocity);
byteArray.AddVector3(Acceleration);
byteArray.AddVector3(RotationVelocity);
byteArray.AddFloat(ThrottlePower);
byteArray.AddFloat(BrakePower);
byteArray.AddFloat(SteeringAngle);
2022-06-03 13:11:17 +08:00
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering))
2022-06-20 10:27:45 +08:00
{
byteArray.AddFloat(DeluxoWingRatio);
2022-06-20 10:27:45 +08:00
}
2022-05-22 15:55:26 +08:00
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.IsFullSync))
2022-05-22 15:55:26 +08:00
{
byteArray.AddInt(ModelHash);
byteArray.AddFloat(EngineHealth);
2022-05-22 15:55:26 +08:00
// Check
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.IsAircraft))
2022-05-22 15:55:26 +08:00
{
// Write the vehicle landing gear
byteArray.Add(LandingGear);
}
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.HasRoof))
{
byteArray.Add(RoofState);
}
// Write vehicle colors
byteArray.Add(Colors[0]);
byteArray.Add(Colors[1]);
// Write vehicle mods
// Write the count of mods
byteArray.AddRange(BitConverter.GetBytes((short)Mods.Count));
// Loop the dictionary and add the values
foreach (KeyValuePair<int, int> mod in Mods)
{
// Write the mod value
byteArray.AddRange(BitConverter.GetBytes(mod.Key));
byteArray.AddRange(BitConverter.GetBytes(mod.Value));
}
2022-05-22 15:55:26 +08:00
if (!DamageModel.Equals(default(VehicleDamageModel)))
{
// Write boolean = true
byteArray.Add(0x01);
// Write vehicle damage model
byteArray.Add(DamageModel.BrokenDoors);
byteArray.Add(DamageModel.OpenedDoors);
byteArray.Add(DamageModel.BrokenWindows);
byteArray.AddRange(BitConverter.GetBytes(DamageModel.BurstedTires));
byteArray.Add(DamageModel.LeftHeadLightBroken);
byteArray.Add(DamageModel.RightHeadLightBroken);
}
else
{
// Write boolean = false
byteArray.Add(0x00);
}
2022-05-22 15:55:26 +08:00
// Write passengers
byteArray.AddRange(BitConverter.GetBytes(Passengers.Count));
2022-05-22 15:55:26 +08:00
foreach (KeyValuePair<int, int> p in Passengers)
{
byteArray.AddRange(BitConverter.GetBytes(p.Key));
byteArray.AddRange(BitConverter.GetBytes(p.Value));
}
2022-05-22 15:55:26 +08:00
// Write LockStatus
byteArray.Add((byte)LockStatus);
2022-05-22 15:55:26 +08:00
// Write RadioStation
byteArray.Add(RadioStation);
2022-05-22 15:55:26 +08:00
// Write LicensePlate
while (LicensePlate.Length<8)
{
LicensePlate+=" ";
}
if (LicensePlate.Length>8)
{
LicensePlate=new string(LicensePlate.Take(8).ToArray());
}
byteArray.AddRange(Encoding.ASCII.GetBytes(LicensePlate));
2022-05-22 15:55:26 +08:00
byteArray.Add((byte)(Livery+1));
}
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-05-22 15:55:26 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-05-22 15:55:26 +08:00
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
// Read vehicle id
ID = reader.ReadInt();
OwnerID = reader.ReadInt();
2022-07-17 12:22:11 +08:00
Flags=(VehicleDataFlags)reader.ReadUShort();
2022-05-22 15:55:26 +08:00
// Read position
Position = reader.ReadVector3();
2022-05-22 15:55:26 +08:00
2022-06-03 14:40:41 +08:00
// Read quaternion
Quaternion=reader.ReadQuaternion();
2022-05-22 15:55:26 +08:00
// Read velocity
Velocity =reader.ReadVector3();
2022-05-22 15:55:26 +08:00
Acceleration=reader.ReadVector3();
2022-05-22 15:55:26 +08:00
// Read rotation velocity
RotationVelocity=reader.ReadVector3();
2022-05-22 15:55:26 +08:00
// Read throttle power
ThrottlePower=reader.ReadFloat();
// Read brake power
BrakePower=reader.ReadFloat();
// Read steering angle
SteeringAngle = reader.ReadFloat();
2022-06-03 13:11:17 +08:00
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering))
{
DeluxoWingRatio = reader.ReadFloat();
}
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.IsFullSync))
{
// Read vehicle model hash
ModelHash = reader.ReadInt();
// Read vehicle engine health
EngineHealth = reader.ReadFloat();
// Check
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.IsAircraft))
{
// Read vehicle landing gear
LandingGear = reader.ReadByte();
}
2022-07-17 12:22:11 +08:00
if (Flags.HasVehFlag(VehicleDataFlags.HasRoof))
{
RoofState=reader.ReadByte();
}
// Read vehicle colors
byte vehColor1 = reader.ReadByte();
byte vehColor2 = reader.ReadByte();
Colors = new byte[] { vehColor1, vehColor2 };
// Read vehicle mods
// Create new Dictionary
Mods = new Dictionary<int, int>();
// Read count of mods
short vehModCount = reader.ReadShort();
// Loop
for (int i = 0; i < vehModCount; i++)
{
// Read the mod value
Mods.Add(reader.ReadInt(), reader.ReadInt());
}
if (reader.ReadBool())
{
// Read vehicle damage model
DamageModel = new VehicleDamageModel()
{
BrokenDoors = reader.ReadByte(),
OpenedDoors=reader.ReadByte(),
BrokenWindows = reader.ReadByte(),
BurstedTires = reader.ReadShort(),
LeftHeadLightBroken = reader.ReadByte(),
RightHeadLightBroken = reader.ReadByte()
};
}
// Read Passengers
Passengers=new Dictionary<int, int>();
int count = reader.ReadInt();
for (int i = 0; i<count; i++)
{
int seat, id;
seat = reader.ReadInt();
id = reader.ReadInt();
Passengers.Add(seat, id);
}
// Read LockStatus
LockStatus=(VehicleLockStatus)reader.ReadByte();
// Read RadioStation
RadioStation=reader.ReadByte();
LicensePlate=Encoding.ASCII.GetString(reader.ReadByteArray(8));
Livery=(int)(reader.ReadByte()-1);
}
2022-05-22 15:55:26 +08:00
#endregion
}
}
}
}