179 lines
6.4 KiB
C#
Raw Normal View History

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
{
internal static partial class Networking
2022-05-22 15:55:26 +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>
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
public static void SendPed(SyncedPed c)
2022-05-22 15:55:26 +08:00
{
Ped p = c.MainPed;
var packet=new Packets.PedSync()
{
ID =c.ID,
Health = p.Health,
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,
Flag = p.GetPedFlags(),
Heading=p.Heading,
};
2022-06-21 18:13:30 +08:00
if (packet.Flag.HasPedFlag(PedDataFlags.IsAiming))
2022-05-22 15:55:26 +08:00
{
packet.AimCoords = p.GetAimCoord();
2022-05-22 15:55:26 +08:00
}
2022-06-21 18:13:30 +08:00
if (packet.Flag.HasPedFlag(PedDataFlags.IsRagdoll))
2022-05-22 15:55:26 +08:00
{
packet.RotationVelocity=p.RotationVelocity;
2022-05-22 15:55:26 +08:00
}
Send(packet, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
public static void SendPedState(SyncedPed c)
2022-05-22 15:55:26 +08:00
{
Ped p = c.MainPed;
var packet = new Packets.PedStateSync()
2022-05-22 15:55:26 +08:00
{
ID = c.ID,
OwnerID=c.OwnerID,
Clothes=p.GetPedClothes(),
ModelHash=p.Model.Hash,
WeaponComponents=p.Weapons.Current.GetWeaponComponents(),
WeaponTint=(byte)Function.Call<int>(Hash.GET_PED_WEAPON_TINT_INDEX, p, p.Weapons.Current.Hash),
2022-05-23 15:48:02 +08:00
};
Blip b;
if (c.IsPlayer)
{
packet.BlipColor=Scripting.API.Config.BlipColor;
2022-07-04 22:50:47 +08:00
packet.BlipSprite=Scripting.API.Config.BlipSprite;
packet.BlipScale=Scripting.API.Config.BlipScale;
}
else if ((b = p.AttachedBlip) !=null)
{
packet.BlipColor=b.Color;
2022-07-04 22:50:47 +08:00
packet.BlipSprite=b.Sprite;
if (packet.BlipSprite==BlipSprite.PoliceOfficer || packet.BlipSprite==BlipSprite.PoliceOfficer2)
{
packet.BlipScale=0.5f;
}
}
Send(packet, ConnectionChannel.PedSync);
2022-05-22 15:55:26 +08:00
}
public static void SendVehicle(SyncedVehicle v)
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,
SteeringAngle = veh.SteeringAngle,
Position = veh.PredictPosition(),
2022-06-03 14:40:41 +08:00
Quaternion=veh.Quaternion,
// Rotation = veh.Rotation,
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
};
if (v.MainVehicle.Model.Hash==1483171323) { packet.DeluxoWingRatio=v.MainVehicle.GetDeluxoWingRatio(); }
2022-05-23 15:48:02 +08:00
Send(packet,ConnectionChannel.VehicleSync);
2022-05-22 15:55:26 +08:00
}
public static void SendVehicleState(SyncedVehicle v)
2022-05-22 15:55:26 +08:00
{
Vehicle veh = v.MainVehicle;
byte primaryColor = 0;
byte secondaryColor = 0;
unsafe
{
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
}
2022-07-09 22:18:00 +08:00
var packet = new Packets.VehicleStateSync()
2022-05-22 15:55:26 +08:00
{
ID =v.ID,
OwnerID = v.OwnerID,
Flag = veh.GetVehicleFlags(),
Colors=new byte[] { primaryColor, secondaryColor },
DamageModel=veh.GetVehicleDamageModel(),
LandingGear = veh.IsAircraft ? (byte)veh.LandingGearState : (byte)0,
2022-06-19 13:36:23 +08:00
RoofState=(byte)veh.RoofState,
2022-05-22 15:55:26 +08:00
Mods = veh.Mods.GetVehicleMods(),
ModelHash=veh.Model.Hash,
EngineHealth=veh.EngineHealth,
Passengers=veh.GetPassengers(),
LockStatus=veh.LockStatus,
2022-07-09 22:18:00 +08:00
LicensePlate=Function.Call<string>(Hash.GET_VEHICLE_NUMBER_PLATE_TEXT, veh),
Livery=Function.Call<int>(Hash.GET_VEHICLE_LIVERY, veh)
2022-05-23 15:48:02 +08:00
};
2022-06-03 13:11:17 +08:00
if (v.MainVehicle==Game.Player.LastVehicle)
{
packet.RadioStation=Util.GetPlayerRadioIndex();
}
2022-05-23 15:48:02 +08:00
Send(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,
Position=p.Position,
Rotation=p.Rotation,
Velocity=p.Velocity,
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
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
{
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
{
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
}
}