297 lines
11 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;
using RageCoop.Core;
2022-07-20 17:50:01 +08:00
using System;
2022-05-22 15:55:26 +08:00
using System.Threading;
2022-07-20 17:50:01 +08:00
using System.Threading.Tasks;
using Lidgren.Network;
2022-05-22 15:55:26 +08:00
2022-07-20 17:50:01 +08:00
namespace RageCoop.Client
{
2022-05-22 15:55:26 +08:00
internal static class SyncEvents
{
#region TRIGGER
public static void TriggerPedKilled(SyncedPed victim)
{
2022-08-11 16:25:38 +08:00
Networking.SendSync(new Packets.PedKilled() { VictimID=victim.ID }, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
public static void TriggerChangeOwner(int vehicleID, int newOwnerID)
2022-05-22 15:55:26 +08:00
{
2022-05-23 15:48:02 +08:00
2022-08-11 16:25:38 +08:00
Networking.SendSync(new Packets.OwnerChanged()
2022-05-23 15:48:02 +08:00
{
ID= vehicleID,
2022-05-23 15:48:02 +08:00
NewOwnerID= newOwnerID,
}, ConnectionChannel.SyncEvents,NetDeliveryMethod.ReliableOrdered);
2022-05-23 15:48:02 +08:00
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void TriggerBulletShot(uint hash, SyncedPed owner, Vector3 impactPosition)
2022-05-22 15:55:26 +08:00
{
2022-07-01 12:22:31 +08:00
// Main.Logger.Trace($"bullet shot:{(WeaponHash)hash}");
2022-05-22 15:55:26 +08:00
var start = owner.MainPed.GetMuzzlePosition();
if (owner.MainPed.IsOnTurretSeat()) { start=owner.MainPed.Bones[Bone.SkelHead].Position; }
if (start.DistanceTo(impactPosition)>10)
{
// Reduce latency
start=impactPosition-(impactPosition-start).Normalized*10;
}
Networking.SendBullet(start, impactPosition, hash, owner.ID);
2022-05-22 15:55:26 +08:00
}
public static void TriggerVehBulletShot(uint hash, Vehicle veh, SyncedPed owner)
2022-05-22 15:55:26 +08:00
{
int i;
// ANNIHL
if (veh.Model.Hash==837858166)
{
Networking.SendVehicleBullet(hash, owner, veh.Bones[35]);
Networking.SendVehicleBullet(hash, owner, veh.Bones[36]);
Networking.SendVehicleBullet(hash, owner, veh.Bones[37]);
Networking.SendVehicleBullet(hash, owner, veh.Bones[38]);
}
else if((i = veh.GetMuzzleIndex())!=-1)
{
Networking.SendVehicleBullet(hash, owner, veh.Bones[i]);
}
else
{
Main.Logger.Warning($"Failed to get muzzle info for vehicle:{veh.DisplayName}");
}
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void TriggerNozzleTransform(int vehID, bool hover)
{
2022-08-11 16:25:38 +08:00
Networking.SendSync(new Packets.NozzleTransform() { VehicleID=vehID, Hover=hover }, ConnectionChannel.SyncEvents);
}
2022-05-22 15:55:26 +08:00
#endregion
#region HANDLE
2022-06-02 15:51:58 +08:00
public static ParticleEffectAsset CorePFXAsset = new ParticleEffectAsset("core");
static WeaponAsset _weaponAsset = default;
static uint _lastWeaponHash;
2022-05-26 17:11:37 +08:00
private static void HandlePedKilled(Packets.PedKilled p)
2022-05-22 15:55:26 +08:00
{
EntityPool.GetPedByID(p.VictimID)?.MainPed?.Kill();
}
2022-05-26 17:11:37 +08:00
private static void HandleOwnerChanged(Packets.OwnerChanged p)
2022-05-22 15:55:26 +08:00
{
var v = EntityPool.GetVehicleByID(p.ID);
if (v==null) { return; }
v.OwnerID=p.NewOwnerID;
2022-06-03 13:11:17 +08:00
v.LastSynced=Main.Ticked;
v.Position=v.MainVehicle.Position;
v.Quaternion=v.MainVehicle.Quaternion;
2022-05-22 15:55:26 +08:00
}
private static void HandleNozzleTransform(Packets.NozzleTransform p)
{
EntityPool.GetVehicleByID(p.VehicleID)?.MainVehicle?.SetNozzleAngel(p.Hover ? 1 : 0);
}
2022-05-26 17:11:37 +08:00
private static void HandleBulletShot(Vector3 start, Vector3 end, uint weaponHash, int ownerID)
2022-05-22 15:55:26 +08:00
{
2022-06-02 10:34:33 +08:00
switch (weaponHash)
{
// Minigun, not working for some reason
case (uint)WeaponHash.Minigun:
2022-06-02 12:44:05 +08:00
weaponHash=1176362416;
2022-06-02 10:34:33 +08:00
break;
// Valkyire, not working for some reason
case 2756787765:
2022-06-02 12:44:05 +08:00
weaponHash=1176362416;
2022-07-02 13:12:59 +08:00
break;
// Tampa3, not working for some reason
case 3670375085:
weaponHash=1176362416;
2022-07-02 13:21:55 +08:00
break;
// Ruiner2, not working for some reason
case 50118905:
weaponHash=1176362416;
2022-06-02 10:34:33 +08:00
break;
2022-06-02 13:09:13 +08:00
// SAVAGE
case 1638077257:
weaponHash=(uint)VehicleWeaponHash.PlayerLazer;
break;
2022-07-20 17:50:01 +08:00
2022-06-02 10:34:33 +08:00
case (uint)VehicleWeaponHash.PlayerBuzzard:
2022-06-02 12:44:05 +08:00
weaponHash=1176362416;
2022-07-20 17:50:01 +08:00
break;
2022-05-22 15:55:26 +08:00
}
2022-06-02 10:34:33 +08:00
2022-05-22 15:55:26 +08:00
var p = EntityPool.GetPedByID(ownerID)?.MainPed;
if (p == null) { p=Game.Player.Character; Main.Logger.Warning("Failed to find owner for bullet"); }
2022-06-02 10:34:33 +08:00
if (!CorePFXAsset.IsLoaded) { CorePFXAsset.Request(); }
2022-05-22 15:55:26 +08:00
if (_lastWeaponHash!=weaponHash)
{
2022-05-31 10:11:06 +08:00
_weaponAsset.MarkAsNoLongerNeeded();
2022-05-22 15:55:26 +08:00
_weaponAsset=new WeaponAsset(weaponHash);
_lastWeaponHash=weaponHash;
}
if (!_weaponAsset.IsLoaded) { _weaponAsset.Request(); }
2022-06-17 11:08:49 +08:00
World.ShootBullet(start, end, p, _weaponAsset, (int)p.GetWeaponDamage(weaponHash));
2022-06-02 10:34:33 +08:00
Prop w;
2022-07-20 17:50:01 +08:00
if (((w = p.Weapons.CurrentWeaponObject) != null)&&(p.VehicleWeapon==VehicleWeaponHash.Invalid))
2022-05-22 15:55:26 +08:00
{
if (p.Weapons.Current.Components.GetSuppressorComponent().Active)
{
World.CreateParticleEffectNonLooped(CorePFXAsset, "muz_pistol_silencer", p.GetMuzzlePosition(), w.Rotation, 1);
}
else
{
World.CreateParticleEffectNonLooped(CorePFXAsset, WeaponUtil.GetFlashFX((WeaponHash)weaponHash), p.GetMuzzlePosition(), w.Rotation, 1);
2022-05-22 15:55:26 +08:00
}
}
}
public static void HandleVehicleBulletShot(Packets.VehicleBulletShot p)
{
HandleBulletShot(p.StartPosition,p.EndPosition,p.WeaponHash,p.OwnerID);
var v = EntityPool.GetPedByID(p.OwnerID)?.MainPed.CurrentVehicle;
if(v == null) { return; }
var b = v.Bones[p.Bone];
World.CreateParticleEffectNonLooped(CorePFXAsset,
WeaponUtil.GetFlashFX((WeaponHash)p.WeaponHash),
b.Position,b.ForwardVector.ToEulerRotation(v.Bones[35].UpVector),1);
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void HandleEvent(PacketType type, byte[] data)
2022-05-22 15:55:26 +08:00
{
switch (type)
{
2022-07-01 12:22:31 +08:00
case PacketType.BulletShot:
2022-05-22 15:55:26 +08:00
{
Packets.BulletShot p = new Packets.BulletShot();
2022-08-06 10:43:24 +08:00
p.Deserialize(data);
HandleBulletShot(p.StartPosition, p.EndPosition, p.WeaponHash, p.OwnerID);
2022-05-22 15:55:26 +08:00
break;
}
case PacketType.VehicleBulletShot:
{
HandleVehicleBulletShot(data.GetPacket<Packets.VehicleBulletShot>());
break;
}
2022-07-01 12:22:31 +08:00
case PacketType.OwnerChanged:
2022-05-22 15:55:26 +08:00
{
Packets.OwnerChanged packet = new Packets.OwnerChanged();
2022-08-06 10:43:24 +08:00
packet.Deserialize(data);
2022-05-22 15:55:26 +08:00
HandleOwnerChanged(packet);
}
break;
2022-07-01 12:22:31 +08:00
case PacketType.PedKilled:
2022-05-22 15:55:26 +08:00
{
var packet = new Packets.PedKilled();
2022-08-06 10:43:24 +08:00
packet.Deserialize(data);
2022-05-22 15:55:26 +08:00
HandlePedKilled(packet);
}
break;
2022-07-01 12:22:31 +08:00
case PacketType.NozzleTransform:
{
var packet = new Packets.NozzleTransform();
2022-08-06 10:43:24 +08:00
packet.Deserialize(data);
HandleNozzleTransform(packet);
break;
}
2022-05-22 15:55:26 +08:00
}
}
#endregion
#region CHECK EVENTS
public static void Check(SyncedPed c)
{
Ped subject = c.MainPed;
2022-05-31 09:55:54 +08:00
// Check bullets
if (subject.IsShooting)
2022-05-22 15:55:26 +08:00
{
if (!subject.IsUsingProjectileWeapon())
2022-05-22 15:55:26 +08:00
{
int i = 0;
Func<bool> getBulletImpact = (() =>
2022-05-22 15:55:26 +08:00
{
Vector3 endPos = subject.LastWeaponImpactPosition;
if (endPos==default)
{
2022-08-11 14:29:18 +02:00
if (++i<=5) { return false; }
endPos = subject.GetAimCoord();
if (subject.IsInVehicle() && subject.VehicleWeapon != VehicleWeaponHash.Invalid)
2022-05-22 15:55:26 +08:00
{
if (subject.IsOnTurretSeat())
{
2022-07-20 17:50:01 +08:00
TriggerBulletShot((uint)subject.VehicleWeapon, c, endPos);
2022-05-22 15:55:26 +08:00
}
else
{
2022-07-20 17:50:01 +08:00
TriggerVehBulletShot((uint)subject.VehicleWeapon, subject.CurrentVehicle, c);
2022-05-22 15:55:26 +08:00
}
}
else
{
2022-07-20 17:50:01 +08:00
TriggerBulletShot((uint)subject.Weapons.Current.Hash, c, endPos);
2022-05-22 15:55:26 +08:00
}
return true;
}
2022-08-11 14:29:18 +02:00
if (subject.IsInVehicle() && subject.VehicleWeapon != VehicleWeaponHash.Invalid)
{
if (subject.IsOnTurretSeat())
{
TriggerBulletShot((uint)subject.VehicleWeapon, c, endPos);
}
else
{
TriggerVehBulletShot((uint)subject.VehicleWeapon, subject.CurrentVehicle, c);
}
}
else
{
TriggerBulletShot((uint)subject.Weapons.Current.Hash, c, endPos);
}
2022-08-11 14:29:18 +02:00
return true;
});
2022-08-11 14:29:18 +02:00
if (!getBulletImpact())
2022-05-22 15:55:26 +08:00
{
Main.QueueAction(getBulletImpact);
2022-05-22 15:55:26 +08:00
}
}
else if (subject.VehicleWeapon==VehicleWeaponHash.Tank && subject.LastWeaponImpactPosition!=default)
2022-05-22 15:55:26 +08:00
{
2022-07-20 17:50:01 +08:00
TriggerBulletShot((uint)VehicleWeaponHash.Tank, c, subject.LastWeaponImpactPosition);
2022-05-22 15:55:26 +08:00
}
}
}
2022-07-20 17:50:01 +08:00
public static void Check(SyncedVehicle v)
{
2022-08-11 14:29:18 +02:00
if (v.MainVehicle==null||!v.MainVehicle.HasNozzle())
{
2022-08-11 14:29:18 +02:00
return;
}
if ((v.LastNozzleAngle == 1) && (v.MainVehicle.GetNozzleAngel() != 1))
{
TriggerNozzleTransform(v.ID, false);
}
else if ((v.LastNozzleAngle == 0) && (v.MainVehicle.GetNozzleAngel() != 0))
{
TriggerNozzleTransform(v.ID, true);
}
2022-08-11 14:29:18 +02:00
v.LastNozzleAngle = v.MainVehicle.GetNozzleAngel();
}
2022-05-22 15:55:26 +08:00
#endregion
}
}