237 lines
7.1 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.Math;
using GTA;
2022-05-22 15:55:26 +08:00
using Lidgren.Network;
namespace RageCoop.Core
{
2022-07-01 14:39:43 +08:00
internal partial class Packets
2022-05-22 15:55:26 +08:00
{
2022-07-01 14:39:43 +08:00
internal class PedSync : Packet
2022-05-22 15:55:26 +08:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.PedSync;
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 PedDataFlags Flags { get; set; }
2022-05-22 15:55:26 +08:00
public int Health { get; set; }
public Vector3 Position { get; set; }
2022-05-22 15:55:26 +08:00
public Vector3 Rotation { get; set; }
2022-05-22 15:55:26 +08:00
public Vector3 Velocity { get; set; }
2022-05-22 15:55:26 +08:00
2022-07-17 18:44:16 +08:00
#region RAGDOLL
public Vector3 HeadPosition { get; set; }
public Vector3 RightFootPosition { get; set; }
public Vector3 LeftFootPosition { get; set; }
#endregion
2022-05-22 15:55:26 +08:00
public byte Speed { get; set; }
public Vector3 AimCoords { get; set; }
2022-05-22 15:55:26 +08:00
public uint CurrentWeaponHash { get; set; }
public float Heading { get; set; }
#region FULL
public int ModelHash { get; set; }
public byte[] Clothes { get; set; }
public Dictionary<uint, bool> WeaponComponents { get; set; }
public byte WeaponTint { get; set; }
public BlipColor BlipColor { get; set; } = (BlipColor)255;
public BlipSprite BlipSprite { get; set; } = 0;
public float BlipScale { get; set; } = 1;
#endregion
2022-05-22 15:55:26 +08:00
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>();
// Write ped ID
byteArray.AddInt(ID);
2022-05-22 15:55:26 +08:00
// Write OwnerID
byteArray.AddInt(OwnerID);
2022-05-22 15:55:26 +08:00
// Write ped flags
2022-07-17 12:22:11 +08:00
byteArray.AddRange(BitConverter.GetBytes((ushort)Flags));
2022-05-22 15:55:26 +08:00
// Write ped health
byteArray.AddRange(BitConverter.GetBytes(Health));
2022-07-17 18:44:16 +08:00
// ragdoll sync
if (Flags.HasPedFlag(PedDataFlags.IsRagdoll))
{
byteArray.AddVector3(HeadPosition);
byteArray.AddVector3(RightFootPosition);
byteArray.AddVector3(LeftFootPosition);
}
else
{
// Write ped position
byteArray.AddVector3(Position);
}
2022-05-22 15:55:26 +08:00
// Write ped rotation
byteArray.AddVector3(Rotation);
2022-05-22 15:55:26 +08:00
// Write ped velocity
byteArray.AddVector3(Velocity);
2022-05-22 15:55:26 +08:00
// Write ped speed
byteArray.Add(Speed);
// Write ped weapon hash
byteArray.AddRange(BitConverter.GetBytes(CurrentWeaponHash));
2022-07-17 12:22:11 +08:00
if (Flags.HasPedFlag(PedDataFlags.IsAiming))
2022-05-22 15:55:26 +08:00
{
// Write ped aim coords
byteArray.AddVector3(AimCoords);
2022-05-22 15:55:26 +08:00
}
byteArray.AddFloat(Heading);
2022-07-17 12:22:11 +08:00
if (Flags.HasPedFlag(PedDataFlags.IsFullSync))
{
// Write model hash
byteArray.AddInt(ModelHash);
byteArray.AddRange(Clothes);
// Write player weapon components
if (WeaponComponents != null)
{
byteArray.Add(0x01);
byteArray.AddRange(BitConverter.GetBytes((ushort)WeaponComponents.Count));
foreach (KeyValuePair<uint, bool> component in WeaponComponents)
{
byteArray.AddRange(BitConverter.GetBytes(component.Key));
byteArray.AddRange(BitConverter.GetBytes(component.Value));
}
}
else
{
// Player weapon doesn't have any components
byteArray.Add(0x00);
}
byteArray.Add(WeaponTint);
byteArray.Add((byte)BlipColor);
if ((byte)BlipColor!=255)
{
byteArray.AddUshort((ushort)BlipSprite);
byteArray.AddFloat(BlipScale);
}
}
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 player netHandle
2022-08-06 11:40:38 +08:00
ID = reader.ReadInt32();
2022-05-22 15:55:26 +08:00
2022-08-06 11:40:38 +08:00
OwnerID=reader.ReadInt32();
2022-05-22 15:55:26 +08:00
// Read player flags
2022-08-06 11:40:38 +08:00
Flags = (PedDataFlags)reader.ReadUInt16();
2022-05-22 15:55:26 +08:00
// Read player health
2022-08-06 11:40:38 +08:00
Health = reader.ReadInt32();
2022-05-22 15:55:26 +08:00
2022-07-17 18:44:16 +08:00
if (Flags.HasPedFlag(PedDataFlags.IsRagdoll))
{
HeadPosition=reader.ReadVector3();
RightFootPosition=reader.ReadVector3();
LeftFootPosition=reader.ReadVector3();
Position=HeadPosition;
}
else
{
// Read player position
Position = reader.ReadVector3();
}
2022-05-22 15:55:26 +08:00
// Read player rotation
Rotation = reader.ReadVector3();
2022-05-22 15:55:26 +08:00
// Read player velocity
Velocity = reader.ReadVector3();
2022-05-22 15:55:26 +08:00
// Read player speed
Speed = reader.ReadByte();
// Read player weapon hash
2022-08-06 11:40:38 +08:00
CurrentWeaponHash = reader.ReadUInt32();
2022-05-22 15:55:26 +08:00
// Try to read aim coords
2022-07-17 12:22:11 +08:00
if (Flags.HasPedFlag(PedDataFlags.IsAiming))
2022-05-22 15:55:26 +08:00
{
// Read player aim coords
AimCoords = reader.ReadVector3();
2022-05-22 15:55:26 +08:00
}
2022-08-06 11:40:38 +08:00
Heading=reader.ReadSingle();
2022-07-17 12:22:11 +08:00
if (Flags.HasPedFlag(PedDataFlags.IsFullSync))
{
// Read player model hash
2022-08-06 11:40:38 +08:00
ModelHash = reader.ReadInt32();
// Read player clothes
2022-08-06 11:40:38 +08:00
Clothes =reader.ReadBytes(36);
// Read player weapon components
2022-08-06 11:40:38 +08:00
if (reader.ReadBoolean())
{
WeaponComponents = new Dictionary<uint, bool>();
2022-08-06 11:40:38 +08:00
ushort comCount = reader.ReadUInt16();
for (ushort i = 0; i < comCount; i++)
{
2022-08-06 11:40:38 +08:00
WeaponComponents.Add(reader.ReadUInt32(), reader.ReadBoolean());
}
}
WeaponTint=reader.ReadByte();
BlipColor=(BlipColor)reader.ReadByte();
if ((byte)BlipColor!=255)
{
2022-08-06 11:40:38 +08:00
BlipSprite=(BlipSprite)reader.ReadUInt16();
BlipScale=reader.ReadSingle();
}
}
2022-05-22 15:55:26 +08:00
#endregion
}
}
}
}