182 lines
7.3 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-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-07-20 17:50:01 +08:00
var packet = new Packets.PedSync()
2022-05-22 15:55:26 +08:00
{
ID =c.ID,
OwnerID=c.OwnerID,
2022-05-22 15:55:26 +08:00
Health = p.Health,
Rotation = p.ReadRotation(),
2022-07-30 15:45:27 +08:00
Velocity = p.ReadVelocity(),
2022-05-22 15:55:26 +08:00
Speed = p.GetPedSpeed(),
2022-07-17 12:22:11 +08:00
Flags = p.GetPedFlags(),
2022-05-22 15:55:26 +08:00
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-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-05-23 15:48:02 +08:00
var packet = new Packets.VehicleSync()
2022-05-22 15:55:26 +08:00
{
ID =v.ID,
OwnerID=v.OwnerID,
2022-07-17 12:22:11 +08:00
Flags = veh.GetVehicleFlags(),
2022-05-22 15:55:26 +08:00
SteeringAngle = veh.SteeringAngle,
2022-08-10 20:42:47 +08:00
Position = veh.Position,
2022-08-08 17:03:41 +08:00
Velocity=veh.Velocity,
Quaternion=veh.ReadQuaternion(),
RotationVelocity=veh.RotationVelocity,
2022-05-22 15:55:26 +08:00
ThrottlePower = veh.ThrottlePower,
BrakePower = veh.BrakePower,
2022-05-23 15:48:02 +08:00
};
2022-08-08 17:03:41 +08:00
if (v.LastVelocity==default) {v.LastVelocity=packet.Velocity; }
packet.Acceleration = (packet.Velocity-v.LastVelocity)*1000/v.LastSentStopWatch.ElapsedMilliseconds;
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;
var packet = new Packets.ProjectileSync()
{
ID =sp.ID,
ShooterID=sp.ShooterID,
Rotation=p.Rotation,
2022-08-10 20:42:47 +08:00
Position=p.Position,
Velocity=p.Velocity,
WeaponHash=(uint)p.WeaponHash,
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 02:19:40 +02:00
public static void SendVoiceMessage(byte[] buffer)
{
SendSync(new Packets.Voice() { Buffer = buffer }, ConnectionChannel.Voice, NetDeliveryMethod.ReliableOrdered);
}
2022-05-22 15:55:26 +08:00
}
}