410 lines
16 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;
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-07-20 17:50:01 +08:00
Networking.Send(new Packets.PedKilled() { VictimID=victim.ID }, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void TriggerEnteringVehicle(SyncedPed c, SyncedVehicle veh, VehicleSeat seat)
2022-05-22 15:55:26 +08:00
{
Networking.
2022-05-23 15:48:02 +08:00
Send(new Packets.EnteringVehicle()
{
PedID=c.ID,
VehicleID= veh.ID,
VehicleSeat=(short)seat,
}, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
public static void TriggerEnteredVehicle(SyncedPed c, SyncedVehicle veh, VehicleSeat seat)
{
if (seat==VehicleSeat.Driver)
{
veh.OwnerID=Main.LocalPlayerID;
2022-05-22 15:55:26 +08:00
veh.LastSynced=Main.Ticked;
2022-05-29 18:27:07 +08:00
TriggerChangeOwner(veh, c.ID);
2022-05-22 15:55:26 +08:00
}
Networking.Send(new Packets.EnteredVehicle()
{
VehicleSeat=(short)seat,
PedID=c.ID,
VehicleID=veh.ID
2022-07-20 17:50:01 +08:00
}, ConnectionChannel.SyncEvents);
2022-05-22 15:55:26 +08:00
}
public static void TriggerChangeOwner(SyncedVehicle c, int newOwnerID)
{
2022-05-23 15:48:02 +08:00
Networking.Send(new Packets.OwnerChanged()
2022-05-23 15:48:02 +08:00
{
ID= c.ID,
NewOwnerID= newOwnerID,
}, ConnectionChannel.SyncEvents);
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.SendBulletShot(start, impactPosition, hash, owner.ID);
2022-05-22 15:55:26 +08:00
}
public static void TriggerLeaveVehicle(int id)
{
Networking.
2022-05-23 15:48:02 +08:00
Send(new Packets.LeaveVehicle()
{
ID=id
}, ConnectionChannel.SyncEvents);
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
{
// ANNIHL
if (veh.Model.Hash==837858166)
{
2022-07-20 17:50:01 +08:00
Networking.SendBulletShot(veh.Bones[35].Position, veh.Bones[35].Position+veh.Bones[35].ForwardVector, hash, owner.ID);
Networking.SendBulletShot(veh.Bones[36].Position, veh.Bones[36].Position+veh.Bones[36].ForwardVector, hash, owner.ID);
Networking.SendBulletShot(veh.Bones[37].Position, veh.Bones[37].Position+veh.Bones[37].ForwardVector, hash, owner.ID);
Networking.SendBulletShot(veh.Bones[38].Position, veh.Bones[38].Position+veh.Bones[38].ForwardVector, hash, owner.ID);
return;
}
2022-05-22 15:55:26 +08:00
var info = veh.GetMuzzleInfo();
2022-07-20 17:50:01 +08:00
if (info==null) { Main.Logger.Warning($"Failed to get muzzle info for vehicle:{veh.DisplayName}"); return; }
Networking.SendBulletShot(info.Position, info.Position+info.ForawardVector, hash, owner.ID);
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
public static void TriggerNozzleTransform(int vehID, bool hover)
{
Networking.Send(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 HandleLeaveVehicle(Packets.LeaveVehicle p)
2022-05-22 15:55:26 +08:00
{
2022-05-31 09:55:54 +08:00
var ped = EntityPool.GetPedByID(p.ID);
2022-07-09 19:59:17 +08:00
var veh = ped.MainPed.CurrentVehicle.GetSyncEntity();
veh._checkSeat=false;
2022-05-22 15:55:26 +08:00
var flag = LeaveVehicleFlags.None;
2022-05-31 09:55:54 +08:00
if (ped.MainPed?.CurrentVehicle==null) { return; }
2022-05-22 15:55:26 +08:00
// Bail out
2022-07-20 17:50:01 +08:00
if (ped.MainPed.CurrentVehicle.Speed>5) { flag|=LeaveVehicleFlags.BailOut; }
2022-07-09 19:59:17 +08:00
// ped.PauseUpdate((ulong)Game.FPS*2);
2022-07-20 17:50:01 +08:00
ped.MainPed.Task.LeaveVehicle(flag);
2022-07-09 19:59:17 +08:00
Task.Run(() =>
{
Thread.Sleep(1000);
veh._checkSeat=true;
});
2022-05-22 15:55:26 +08:00
}
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 HandleEnteringVehicle(SyncedPed c, SyncedVehicle veh, VehicleSeat seat)
2022-05-22 15:55:26 +08:00
{
2022-07-20 17:50:01 +08:00
c.MainPed?.Task.EnterVehicle(veh.MainVehicle, seat, -1, 2, EnterVehicleFlags.WarpToDoor|EnterVehicleFlags.AllowJacking);
2022-05-22 15:55:26 +08:00
}
2022-05-26 17:11:37 +08:00
private static void HandleEnteredVehicle(int pedId, int vehId, VehicleSeat seat)
2022-05-22 15:55:26 +08:00
{
var v = EntityPool.GetVehicleByID(vehId);
var p = EntityPool.GetPedByID(pedId)?.MainPed;
if (v==null||p==null) { return; }
if (!v.MainVehicle.IsSeatFree(seat))
{
if (v.MainVehicle.GetPedOnSeat(seat)!=p)
{
v.MainVehicle.GetPedOnSeat(seat).Task.WarpOutOfVehicle(v.MainVehicle);
}
}
p.SetIntoVehicle(v.MainVehicle, seat);
2022-05-22 15:55:26 +08:00
}
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;
v.Model=v.MainVehicle.Model;
2022-06-03 13:11:17 +08:00
v.LastSynced=Main.Ticked;
// So this vehicle doesn's get re-spawned
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, "muz_assault_rifle", p.GetMuzzlePosition(), w.Rotation, 1);
}
}
else if (p.VehicleWeapon!=VehicleWeaponHash.Invalid)
{
if (p.VehicleWeapon==VehicleWeaponHash.Tank)
{
World.CreateParticleEffectNonLooped(CorePFXAsset, "muz_tank", p.CurrentVehicle.GetMuzzleInfo().Position, p.CurrentVehicle.Bones[35].ForwardVector.ToEulerRotation(p.CurrentVehicle.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();
p.Unpack(data);
HandleBulletShot(p.StartPosition, p.EndPosition, p.WeaponHash, p.OwnerID);
2022-05-22 15:55:26 +08:00
break;
}
2022-07-01 12:22:31 +08:00
case PacketType.EnteringVehicle:
2022-05-22 15:55:26 +08:00
{
Packets.EnteringVehicle p = new Packets.EnteringVehicle();
p.Unpack(data);
HandleEnteringVehicle(EntityPool.GetPedByID(p.PedID), EntityPool.GetVehicleByID(p.VehicleID), (VehicleSeat)p.VehicleSeat);
}
break;
2022-07-01 12:22:31 +08:00
case PacketType.LeaveVehicle:
2022-05-22 15:55:26 +08:00
{
Packets.LeaveVehicle packet = new Packets.LeaveVehicle();
packet.Unpack(data);
HandleLeaveVehicle(packet);
}
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();
packet.Unpack(data);
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();
packet.Unpack(data);
HandlePedKilled(packet);
}
break;
2022-07-01 12:22:31 +08:00
case PacketType.EnteredVehicle:
2022-05-22 15:55:26 +08:00
{
var packet = new Packets.EnteredVehicle();
packet.Unpack(data);
2022-07-20 17:50:01 +08:00
HandleEnteredVehicle(packet.PedID, packet.VehicleID, (VehicleSeat)packet.VehicleSeat);
2022-05-22 15:55:26 +08:00
break;
}
2022-07-01 12:22:31 +08:00
case PacketType.NozzleTransform:
{
var packet = new Packets.NozzleTransform();
packet.Unpack(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)
{
if (i>5)
{
endPos=subject.GetAimCoord();
if (subject.IsInVehicle() && subject.VehicleWeapon!=VehicleWeaponHash.Invalid)
{
if (subject.IsOnTurretSeat())
{
2022-07-20 17:50:01 +08:00
TriggerBulletShot((uint)subject.VehicleWeapon, c, endPos);
}
else
{
2022-07-20 17:50:01 +08:00
TriggerVehBulletShot((uint)subject.VehicleWeapon, subject.CurrentVehicle, c);
}
}
else
{
2022-07-20 17:50:01 +08:00
TriggerBulletShot((uint)subject.Weapons.Current.Hash, c, endPos);
}
return true;
}
i++;
return false;
}
else
2022-05-22 15:55:26 +08:00
{
if (subject.IsInVehicle() && subject.VehicleWeapon!=VehicleWeaponHash.Invalid)
{
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;
}
});
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
2022-05-22 15:55:26 +08:00
}
2022-07-20 17:50:01 +08:00
2022-05-22 15:55:26 +08:00
// Vehicles
var g = subject.IsGettingIntoVehicle;
2022-07-20 17:50:01 +08:00
if (g && (!c._lastEnteringVehicle))
{
2022-05-22 15:55:26 +08:00
var v = subject.VehicleTryingToEnter.GetSyncEntity();
TriggerEnteringVehicle(c, v, subject.GetSeatTryingToEnter());
}
2022-07-20 17:50:01 +08:00
var currentSitting = subject.IsSittingInVehicle();
2022-05-22 15:55:26 +08:00
if (c._lastSittingInVehicle)
{
if (!currentSitting)
{
var veh = subject.CurrentVehicle;
if (veh!=null)
{
var v = veh.GetSyncEntity();
TriggerLeaveVehicle(c.ID);
}
}
}
else if (currentSitting)
{
TriggerEnteredVehicle(c, subject.CurrentVehicle.GetSyncEntity(), subject.SeatIndex);
}
c._lastSittingInVehicle=currentSitting;
c._lastEnteringVehicle=g;
}
2022-07-20 17:50:01 +08:00
public static void Check(SyncedVehicle v)
{
if (v.MainVehicle!=null&&v.MainVehicle.HasNozzle())
{
2022-07-20 17:50:01 +08:00
if ((v.LastNozzleAngle==1) && (v.MainVehicle.GetNozzleAngel()!=1))
{
2022-07-20 17:50:01 +08:00
TriggerNozzleTransform(v.ID, false);
}
2022-07-20 17:50:01 +08:00
else if ((v.LastNozzleAngle==0) && (v.MainVehicle.GetNozzleAngel()!=0))
{
2022-07-20 17:50:01 +08:00
TriggerNozzleTransform(v.ID, true);
}
v.LastNozzleAngle=v.MainVehicle.GetNozzleAngel();
}
}
2022-05-22 15:55:26 +08:00
#endregion
}
}