Files
RAGECOOP-V/RageCoop.Client/Networking/Send.cs

187 lines
7.7 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;
using Lidgren.Network;
using RageCoop.Core;
2022-07-29 18:17:45 +08:00
using System;
2022-08-08 17:03:41 +08:00
using System.Collections.Generic;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
internal static partial class Networking
2022-05-22 15:55:26 +08:00
{
2022-08-13 08:22:14 +08:00
/// <summary>
/// Reduce GC pressure by reusing frequently used packets
/// </summary>
static class SendPackets
{
public static Packets.PedSync PedPacket = new Packets.PedSync();
public static Packets.VehicleSync VehicelPacket = new Packets.VehicleSync();
public static Packets.ProjectileSync ProjectilePacket = new Packets.ProjectileSync();
}
2022-08-04 17:38:28 +08:00
public static int SyncInterval = 30;
2022-08-08 17:03:41 +08:00
public static List<NetConnection> Targets = new List<NetConnection>();
2022-08-11 16:25:38 +08:00
public static void SendSync(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
2022-05-23 15:48:02 +08:00
{
2022-08-11 16:25:38 +08:00
Peer.SendTo(p, Targets, channel, method);
2022-08-08 17:03:41 +08:00
}
2022-08-11 16:25:38 +08:00
2022-07-20 17:50:01 +08:00
public static void SendPed(SyncedPed c, bool full)
2022-05-22 15:55:26 +08:00
{
2022-08-04 17:38:28 +08:00
if (c.LastSentStopWatch.ElapsedMilliseconds<SyncInterval)
{
return;
}
2022-05-22 15:55:26 +08:00
Ped p = c.MainPed;
2022-08-13 08:22:14 +08:00
var packet = SendPackets.PedPacket;
packet.ID =c.ID;
packet.OwnerID=c.OwnerID;
packet.Health = p.Health;
packet.Rotation = p.ReadRotation();
packet.Velocity = p.ReadVelocity();
packet.Speed = p.GetPedSpeed();
packet.Flags = p.GetPedFlags();
packet.Heading=p.Heading;
2022-07-17 12:22:11 +08:00
if (packet.Flags.HasPedFlag(PedDataFlags.IsAiming))
2022-05-22 15:55:26 +08:00
{
packet.AimCoords = p.GetAimCoord();
2022-05-22 15:55:26 +08:00
}
2022-07-17 18:44:16 +08:00
if (packet.Flags.HasPedFlag(PedDataFlags.IsRagdoll))
{
packet.HeadPosition=p.Bones[Bone.SkelHead].Position;
packet.RightFootPosition=p.Bones[Bone.SkelRightFoot].Position;
packet.LeftFootPosition=p.Bones[Bone.SkelLeftFoot].Position;
}
else
{
packet.Position = p.ReadPosition();
2022-07-17 18:44:16 +08:00
}
2022-08-04 17:38:28 +08:00
c.LastSentStopWatch.Restart();
if (full)
{
2022-08-11 14:59:09 +02:00
packet.CurrentWeaponHash = packet.Flags.HasPedFlag(PedDataFlags.IsInVehicle) ? (uint)p.VehicleWeapon : (uint)p.Weapons.Current.Hash;
2022-07-17 12:22:11 +08:00
packet.Flags |= PedDataFlags.IsFullSync;
packet.Clothes=p.GetPedClothes();
packet.ModelHash=p.Model.Hash;
packet.WeaponComponents=p.Weapons.Current.GetWeaponComponents();
packet.WeaponTint=(byte)Function.Call<int>(Hash.GET_PED_WEAPON_TINT_INDEX, p, p.Weapons.Current.Hash);
2022-07-04 22:50:47 +08:00
Blip b;
if (c.IsPlayer)
{
packet.BlipColor=Scripting.API.Config.BlipColor;
packet.BlipSprite=Scripting.API.Config.BlipSprite;
packet.BlipScale=Scripting.API.Config.BlipScale;
}
else if ((b = p.AttachedBlip) !=null)
2022-07-04 22:50:47 +08:00
{
packet.BlipColor=b.Color;
packet.BlipSprite=b.Sprite;
if (packet.BlipSprite==BlipSprite.PoliceOfficer || packet.BlipSprite==BlipSprite.PoliceOfficer2)
{
packet.BlipScale=0.5f;
}
2022-07-04 22:50:47 +08:00
}
2022-08-13 08:22:14 +08:00
else
{
packet.BlipColor=(BlipColor)255;
}
}
2022-08-11 16:25:38 +08:00
SendSync(packet, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void SendVehicle(SyncedVehicle v, bool full)
2022-05-22 15:55:26 +08:00
{
2022-08-04 17:38:28 +08:00
if (v.LastSentStopWatch.ElapsedMilliseconds<SyncInterval)
{
return;
}
2022-05-22 15:55:26 +08:00
Vehicle veh = v.MainVehicle;
2022-08-13 08:22:14 +08:00
var packet = SendPackets.VehicelPacket;
packet.ID =v.ID;
packet.OwnerID=v.OwnerID;
packet.Flags = veh.GetVehicleFlags();
packet.SteeringAngle = veh.SteeringAngle;
packet.Position = veh.Position;
packet.Velocity=veh.Velocity;
packet.Quaternion=veh.ReadQuaternion();
packet.RotationVelocity=veh.RotationVelocity;
packet.ThrottlePower = veh.ThrottlePower;
packet.BrakePower = veh.BrakePower;
2022-08-08 17:03:41 +08:00
if (v.LastVelocity==default) {v.LastVelocity=packet.Velocity; }
v.LastSentStopWatch.Restart();
2022-08-08 17:03:41 +08:00
v.LastVelocity= packet.Velocity;
2022-07-17 12:22:11 +08:00
if (packet.Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering)) { packet.DeluxoWingRatio=v.MainVehicle.GetDeluxoWingRatio(); }
if (full)
2022-06-03 13:11:17 +08:00
{
byte primaryColor = 0;
byte secondaryColor = 0;
unsafe
{
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
}
2022-07-17 12:22:11 +08:00
packet.Flags |= VehicleDataFlags.IsFullSync;
packet.Colors = new byte[] { primaryColor, secondaryColor };
packet.DamageModel=veh.GetVehicleDamageModel();
packet.LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0;
packet.RoofState=(byte)veh.RoofState;
packet.Mods = veh.Mods.GetVehicleMods();
packet.ModelHash=veh.Model.Hash;
packet.EngineHealth=veh.EngineHealth;
packet.Passengers=veh.GetPassengers();
packet.LockStatus=veh.LockStatus;
packet.LicensePlate=Function.Call<string>(Hash.GET_VEHICLE_NUMBER_PLATE_TEXT, veh);
packet.Livery=Function.Call<int>(Hash.GET_VEHICLE_LIVERY, veh);
if (v.MainVehicle==Game.Player.LastVehicle)
{
packet.RadioStation=Util.GetPlayerRadioIndex();
}
if (packet.EngineHealth>v.LastEngineHealth)
{
packet.Flags |= VehicleDataFlags.Repaired;
}
v.LastEngineHealth=packet.EngineHealth;
2022-06-03 13:11:17 +08:00
}
2022-08-11 16:25:38 +08:00
SendSync(packet, ConnectionChannel.VehicleSync);
2022-05-22 15:55:26 +08:00
}
public static void SendProjectile(SyncedProjectile sp)
{
var p = sp.MainProjectile;
2022-08-13 08:22:14 +08:00
var packet = SendPackets.ProjectilePacket;
packet.ID =sp.ID;
packet.ShooterID=sp.ShooterID;
packet.Rotation=p.Rotation;
packet.Position=p.Position;
packet.Velocity=p.Velocity;
packet.WeaponHash=(uint)p.WeaponHash;
packet.Exploded=p.IsDead;
2022-07-20 17:50:01 +08:00
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
2022-08-11 16:25:38 +08:00
SendSync(packet, ConnectionChannel.ProjectileSync);
}
2022-05-23 15:48:02 +08:00
2022-05-22 15:55:26 +08:00
#region SYNC EVENTS
2022-07-20 17:50:01 +08:00
public static void SendBulletShot(Vector3 start, Vector3 end, uint weapon, int ownerID)
2022-05-22 15:55:26 +08:00
{
2022-08-11 16:25:38 +08:00
SendSync(new Packets.BulletShot()
2022-05-22 15:55:26 +08:00
{
StartPosition = start,
EndPosition = end,
2022-05-22 15:55:26 +08:00
OwnerID = ownerID,
WeaponHash=weapon,
2022-05-23 15:48:02 +08:00
}, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
#endregion
public static void SendChatMessage(string message)
2022-05-22 15:55:26 +08:00
{
2022-08-11 14:59:09 +02:00
Peer.SendTo(new Packets.ChatMessage(new Func<string, byte[]>((s) => Security.Encrypt(s.GetBytes())))
2022-08-11 16:25:38 +08:00
{ Username = Main.Settings.Username, Message = message },ServerConnection, ConnectionChannel.Chat, NetDeliveryMethod.ReliableOrdered);
2022-08-06 12:32:04 +08:00
Peer.FlushSendQueue();
2022-05-22 15:55:26 +08:00
}
2022-08-13 03:39:11 +02:00
public static void SendVoiceMessage(byte[] buffer, int recorded)
2022-08-13 02:19:40 +02:00
{
2022-08-13 03:39:11 +02:00
SendSync(new Packets.Voice() { Buffer = buffer, Recorded = recorded }, ConnectionChannel.Voice, NetDeliveryMethod.ReliableOrdered);
2022-08-13 02:19:40 +02:00
}
2022-05-22 15:55:26 +08:00
}
}