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

193 lines
7.7 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System.Collections.Generic;
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.Client.Scripting;
2022-10-15 17:06:19 +08:00
using RageCoop.Core;
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-10-23 19:02:39 +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-09-06 21:46:35 +08:00
public static void SendPed(SyncedPed sp, bool full)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if (sp.LastSentStopWatch.ElapsedMilliseconds < SyncInterval) return;
var ped = sp.MainPed;
var p = SendPackets.PedPacket;
2022-09-06 21:46:35 +08:00
p.ID = sp.ID;
p.OwnerID = sp.OwnerID;
p.Health = ped.Health;
p.Rotation = ped.ReadRotation();
p.Velocity = ped.ReadVelocity();
p.Speed = ped.GetPedSpeed();
p.Flags = ped.GetPedFlags();
2022-09-06 21:46:35 +08:00
p.Heading = ped.Heading;
2022-10-23 19:02:39 +08:00
if (p.Flags.HasPedFlag(PedDataFlags.IsAiming)) p.AimCoords = ped.GetAimCoord();
if (p.Flags.HasPedFlag(PedDataFlags.IsRagdoll))
2022-07-17 18:44:16 +08:00
{
2022-09-06 21:46:35 +08:00
p.HeadPosition = ped.Bones[Bone.SkelHead].Position;
p.RightFootPosition = ped.Bones[Bone.SkelRightFoot].Position;
p.LeftFootPosition = ped.Bones[Bone.SkelLeftFoot].Position;
2022-07-17 18:44:16 +08:00
}
else
{
// Seat sync
2022-09-06 21:46:35 +08:00
if (p.Speed >= 4)
{
2022-10-23 19:02:39 +08:00
var veh = ped.CurrentVehicle?.GetSyncEntity() ??
ped.VehicleTryingToEnter?.GetSyncEntity() ?? ped.LastVehicle?.GetSyncEntity();
p.VehicleID = veh?.ID ?? 0;
2023-02-13 17:51:18 +08:00
if (p.VehicleID == 0) Log.Error("Invalid vehicle");
2022-09-06 21:46:35 +08:00
if (p.Speed == 5)
p.Seat = ped.GetSeatTryingToEnter();
else
2022-09-06 21:46:35 +08:00
p.Seat = ped.SeatIndex;
if (!veh.IsLocal && p.Speed == 4 && p.Seat == VehicleSeat.Driver)
{
2022-09-06 21:46:35 +08:00
veh.OwnerID = Main.LocalPlayerID;
SyncEvents.TriggerChangeOwner(veh.ID, Main.LocalPlayerID);
}
}
2022-10-23 19:02:39 +08:00
p.Position = ped.ReadPosition();
2022-07-17 18:44:16 +08:00
}
2022-10-23 19:02:39 +08:00
sp.LastSentStopWatch.Restart();
if (full)
{
if (p.Speed == 4)
p.VehicleWeapon = ped.VehicleWeapon;
p.CurrentWeapon = ped.Weapons.Current.Hash;
p.Flags |= PedDataFlags.IsFullSync;
2022-09-06 21:46:35 +08:00
p.Clothes = ped.GetPedClothes();
p.ModelHash = ped.Model.Hash;
p.WeaponComponents = ped.Weapons.Current.GetWeaponComponents();
p.WeaponTint = (byte)Call<int>(GET_PED_WEAPON_TINT_INDEX, ped, ped.Weapons.Current.Hash);
2022-07-04 22:50:47 +08:00
Blip b;
if (sp.IsPlayer)
{
2022-10-08 23:49:48 +08:00
p.BlipColor = API.Config.BlipColor;
p.BlipSprite = API.Config.BlipSprite;
p.BlipScale = API.Config.BlipScale;
}
2022-09-06 21:46:35 +08:00
else if ((b = ped.AttachedBlip) != null)
2022-07-04 22:50:47 +08:00
{
2022-09-06 21:46:35 +08:00
p.BlipColor = b.Color;
p.BlipSprite = b.Sprite;
2022-09-06 21:46:35 +08:00
if (p.BlipSprite == BlipSprite.PoliceOfficer || p.BlipSprite == BlipSprite.PoliceOfficer2)
p.BlipScale = 0.5f;
2022-07-04 22:50:47 +08:00
}
2022-08-13 08:22:14 +08:00
else
{
2022-09-06 21:46:35 +08:00
p.BlipColor = (BlipColor)255;
2022-08-13 08:22:14 +08:00
}
}
2022-10-23 19:02:39 +08:00
SendSync(p, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +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-10-23 19:02:39 +08:00
if (v.LastSentStopWatch.ElapsedMilliseconds < SyncInterval) return;
var veh = v.MainVehicle;
2022-08-13 08:22:14 +08:00
var packet = SendPackets.VehicelPacket;
2022-09-06 21:46:35 +08:00
packet.ID = v.ID;
packet.OwnerID = v.OwnerID;
2022-08-18 20:44:39 +08:00
packet.Flags = v.GetVehicleFlags();
2022-08-13 08:22:14 +08:00
packet.SteeringAngle = veh.SteeringAngle;
2022-08-16 19:51:56 +08:00
packet.Position = veh.ReadPosition();
2022-09-06 21:46:35 +08:00
packet.Velocity = veh.Velocity;
packet.Quaternion = veh.ReadQuaternion();
packet.RotationVelocity = veh.RotationVelocity;
2022-08-13 08:22:14 +08:00
packet.ThrottlePower = veh.ThrottlePower;
packet.BrakePower = veh.BrakePower;
v.LastSentStopWatch.Restart();
2022-10-23 19:02:39 +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
{
Call<byte>(GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
}
2022-10-23 19:02:39 +08:00
2022-07-17 12:22:11 +08:00
packet.Flags |= VehicleDataFlags.IsFullSync;
2022-10-23 19:02:39 +08:00
packet.Colors = new[] { primaryColor, secondaryColor };
2022-09-06 21:46:35 +08:00
packet.DamageModel = veh.GetVehicleDamageModel();
packet.LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0;
2022-09-06 21:46:35 +08:00
packet.RoofState = (byte)veh.RoofState;
packet.Mods = veh.Mods.GetVehicleMods();
2022-09-06 21:46:35 +08:00
packet.ModelHash = veh.Model.Hash;
packet.EngineHealth = veh.EngineHealth;
packet.LockStatus = veh.LockStatus;
packet.LicensePlate = Call<string>(GET_VEHICLE_NUMBER_PLATE_TEXT, veh);
packet.Livery = Call<int>(GET_VEHICLE_LIVERY, veh);
2022-10-23 19:02:39 +08:00
if (v.MainVehicle == Game.Player.LastVehicle) packet.RadioStation = Util.GetPlayerRadioIndex();
if (packet.EngineHealth > v.LastEngineHealth) packet.Flags |= VehicleDataFlags.Repaired;
2022-09-06 21:46:35 +08:00
v.LastEngineHealth = packet.EngineHealth;
2022-06-03 13:11:17 +08:00
}
2022-10-23 19:02:39 +08:00
2022-08-11 16:25:38 +08:00
SendSync(packet, ConnectionChannel.VehicleSync);
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
public static void SendProjectile(SyncedProjectile sp)
{
2022-08-14 17:08:43 +08:00
sp.ExtractData(ref SendPackets.ProjectilePacket);
2022-10-23 19:02:39 +08:00
if (sp.MainProjectile.IsDead) EntityPool.RemoveProjectile(sp.ID, "Dead");
2022-08-14 17:08:43 +08:00
SendSync(SendPackets.ProjectilePacket, ConnectionChannel.ProjectileSync);
}
2022-10-23 19:02:39 +08:00
public static void SendChatMessage(string message)
{
Peer.SendTo(new Packets.ChatMessage(s => Security.Encrypt(s.GetBytes()))
{ Username = Main.Settings.Username, Message = message }, ServerConnection, ConnectionChannel.Chat,
2022-10-23 19:02:39 +08:00
NetDeliveryMethod.ReliableOrdered);
Peer.FlushSendQueue();
}
public static void SendVoiceMessage(byte[] buffer, int recorded)
{
SendSync(new Packets.Voice { ID = Main.LocalPlayerID, Buffer = buffer, Recorded = recorded },
ConnectionChannel.Voice, NetDeliveryMethod.ReliableOrdered);
}
/// <summary>
/// Reduce GC pressure by reusing frequently used packets
/// </summary>
private static class SendPackets
{
public static readonly Packets.PedSync PedPacket = new Packets.PedSync();
public static readonly Packets.VehicleSync VehicelPacket = new Packets.VehicleSync();
public static Packets.ProjectileSync ProjectilePacket = new Packets.ProjectileSync();
}
2022-05-23 15:48:02 +08:00
2022-05-22 15:55:26 +08:00
#region SYNC EVENTS
2022-10-23 19:02:39 +08:00
public static void SendBullet(int ownerID, uint weapon, Vector3 end)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
SendSync(new Packets.BulletShot
2022-05-22 15:55:26 +08:00
{
EndPosition = end,
2022-05-22 15:55:26 +08:00
OwnerID = ownerID,
2022-10-23 19:02:39 +08:00
WeaponHash = weapon
2022-05-23 15:48:02 +08:00
}, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
2022-05-22 15:55:26 +08:00
#endregion
}
2022-10-23 19:02:39 +08:00
}