2022-07-01 14:39:43 +08:00
|
|
|
|
using Lidgren.Network;
|
2022-05-22 15:55:26 +08:00
|
|
|
|
using RageCoop.Core;
|
|
|
|
|
using GTA;
|
|
|
|
|
using GTA.Native;
|
|
|
|
|
using GTA.Math;
|
|
|
|
|
|
|
|
|
|
namespace RageCoop.Client
|
|
|
|
|
{
|
2022-05-25 10:09:59 +08:00
|
|
|
|
internal static partial class Networking
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
2022-05-25 10:09:59 +08:00
|
|
|
|
|
|
|
|
|
#region -- SEND --
|
2022-05-23 15:48:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pack the packet then send to server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <param name="channel"></param>
|
|
|
|
|
/// <param name="method"></param>
|
2022-05-25 10:09:59 +08:00
|
|
|
|
public static void Send(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
|
2022-05-23 15:48:02 +08:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
|
|
|
|
p.Pack(outgoingMessage);
|
|
|
|
|
Client.SendMessage(outgoingMessage, method, (int)channel);
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}
|
2022-05-23 15:48:02 +08:00
|
|
|
|
|
2022-07-17 11:15:02 +08:00
|
|
|
|
public static void SendPed(SyncedPed c,bool full)
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
|
|
|
|
Ped p = c.MainPed;
|
|
|
|
|
var packet=new Packets.PedSync()
|
|
|
|
|
{
|
|
|
|
|
ID =c.ID,
|
2022-07-17 11:15:02 +08:00
|
|
|
|
OwnerID=c.OwnerID,
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Health = p.Health,
|
2022-05-30 14:32:38 +08:00
|
|
|
|
Position = p.Position,
|
|
|
|
|
Rotation = p.Rotation,
|
|
|
|
|
Velocity = p.Velocity,
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Speed = p.GetPedSpeed(),
|
|
|
|
|
CurrentWeaponHash = (uint)p.Weapons.Current.Hash,
|
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
|
|
|
|
{
|
2022-05-30 14:32:38 +08:00
|
|
|
|
packet.AimCoords = p.GetAimCoord();
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}
|
2022-07-17 11:15:02 +08:00
|
|
|
|
if (full)
|
2022-07-03 10:46:24 +08:00
|
|
|
|
{
|
2022-07-17 12:22:11 +08:00
|
|
|
|
packet.Flags |= PedDataFlags.IsFullSync;
|
2022-07-17 11:15:02 +08:00
|
|
|
|
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
|
|
|
|
|
2022-07-17 11:15:02 +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
|
|
|
|
{
|
2022-07-17 11:15:02 +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-07-03 10:46:24 +08:00
|
|
|
|
}
|
2022-05-25 10:09:59 +08:00
|
|
|
|
Send(packet, ConnectionChannel.PedSync);
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}
|
2022-07-17 11:59:37 +08:00
|
|
|
|
public static void SendVehicle(SyncedVehicle v,bool full)
|
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,
|
2022-07-17 11:59:37 +08:00
|
|
|
|
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-05-30 14:32:38 +08:00
|
|
|
|
Position = veh.PredictPosition(),
|
2022-06-03 14:40:41 +08:00
|
|
|
|
Quaternion=veh.Quaternion,
|
2022-05-30 14:32:38 +08:00
|
|
|
|
Velocity = veh.Velocity,
|
|
|
|
|
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-07-17 12:22:11 +08:00
|
|
|
|
if (packet.Flags.HasVehFlag(VehicleDataFlags.IsDeluxoHovering)) { packet.DeluxoWingRatio=v.MainVehicle.GetDeluxoWingRatio(); }
|
2022-07-17 11:59:37 +08:00
|
|
|
|
if (full)
|
2022-06-03 13:11:17 +08:00
|
|
|
|
{
|
2022-07-17 11:59:37 +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;
|
2022-07-17 11:59:37 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
2022-06-03 13:11:17 +08:00
|
|
|
|
}
|
2022-07-17 11:59:37 +08:00
|
|
|
|
Send(packet,ConnectionChannel.VehicleSync);
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}
|
2022-05-25 10:09:59 +08:00
|
|
|
|
public static void SendProjectile(SyncedProjectile sp)
|
|
|
|
|
{
|
|
|
|
|
var p = sp.MainProjectile;
|
|
|
|
|
var packet = new Packets.ProjectileSync()
|
|
|
|
|
{
|
|
|
|
|
ID =sp.ID,
|
|
|
|
|
ShooterID=sp.ShooterID,
|
2022-05-30 14:32:38 +08:00
|
|
|
|
Position=p.Position,
|
|
|
|
|
Rotation=p.Rotation,
|
|
|
|
|
Velocity=p.Velocity,
|
2022-05-25 10:09:59 +08:00
|
|
|
|
WeaponHash=(uint)p.WeaponHash,
|
|
|
|
|
Exploded=p.IsDead
|
|
|
|
|
};
|
|
|
|
|
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID,"Dead"); }
|
|
|
|
|
Send(packet, ConnectionChannel.ProjectileSync);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:02 +08:00
|
|
|
|
|
2022-05-22 15:55:26 +08:00
|
|
|
|
#region SYNC EVENTS
|
2022-05-23 16:59:49 +08:00
|
|
|
|
public static void SendBulletShot(Vector3 start,Vector3 end,uint weapon,int ownerID)
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
2022-05-23 15:48:02 +08:00
|
|
|
|
Send(new Packets.BulletShot()
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
2022-05-30 14:32:38 +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
|
2022-05-23 16:59:49 +08:00
|
|
|
|
public static void SendChatMessage(string message)
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
|
|
|
|
|
|
|
|
|
new Packets.ChatMessage() { Username = Main.Settings.Username, Message = message }.Pack(outgoingMessage);
|
|
|
|
|
|
|
|
|
|
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
|
|
|
|
|
Client.FlushSendQueue();
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|