Better vehicle weapon handling and muzzle flash

This commit is contained in:
Sardelka
2022-08-14 13:04:39 +08:00
parent d68941b3ac
commit 8305d9997a
7 changed files with 219 additions and 73 deletions

View File

@ -41,6 +41,7 @@ namespace RageCoop.Client
internal static Scripting.Resources Resources = null; internal static Scripting.Resources Resources = null;
private static List<Func<bool>> QueuedActions = new List<Func<bool>>(); private static List<Func<bool>> QueuedActions = new List<Func<bool>>();
public static Worker Worker; public static Worker Worker;
/// <summary> /// <summary>
/// Don't use it! /// Don't use it!
/// </summary> /// </summary>
@ -102,6 +103,13 @@ namespace RageCoop.Client
Counter.Restart(); Counter.Restart();
} }
string hash(string t)
{
unchecked
{
return "0x"+((uint)Game.GenerateHash(t)).ToString("X");
}
}
#if DEBUG #if DEBUG
#endif #endif
public static Ped P; public static Ped P;

View File

@ -179,7 +179,7 @@ namespace RageCoop.Client
#region SYNC EVENTS #region SYNC EVENTS
public static void SendBulletShot(Vector3 start, Vector3 end, uint weapon, int ownerID) public static void SendBullet(Vector3 start, Vector3 end, uint weapon, int ownerID)
{ {
SendSync(new Packets.BulletShot() SendSync(new Packets.BulletShot()
{ {
@ -189,6 +189,17 @@ namespace RageCoop.Client
WeaponHash=weapon, WeaponHash=weapon,
}, ConnectionChannel.SyncEvents); }, ConnectionChannel.SyncEvents);
} }
public static void SendVehicleBullet(uint hash,SyncedPed owner,EntityBone b)
{
SendSync(new Packets.VehicleBulletShot
{
StartPosition = b.Position,
EndPosition = b.Position+b.ForwardVector,
OwnerID=owner.ID,
Bone=(ushort)b.Index,
WeaponHash=hash
});
}
#endregion #endregion
public static void SendChatMessage(string message) public static void SendChatMessage(string message)
{ {

View File

@ -84,7 +84,7 @@ namespace RageCoop.Client
var v = Shooter?.MainPed?.CurrentVehicle; var v = Shooter?.MainPed?.CurrentVehicle;
if (v!=null) if (v!=null)
{ {
World.CreateParticleEffectNonLooped(SyncEvents.CorePFXAsset, "muz_tank", v.GetMuzzleInfo().Position, v.Bones[35].ForwardVector.ToEulerRotation(v.Bones[35].UpVector), 1); World.CreateParticleEffectNonLooped(SyncEvents.CorePFXAsset, "muz_tank", v.Bones[v.GetMuzzleIndex()].Position, v.Bones[35].ForwardVector.ToEulerRotation(v.Bones[35].UpVector), 1);
} }
} }
EntityPool.Add(this); EntityPool.Add(this);

View File

@ -4,6 +4,7 @@ using RageCoop.Core;
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Lidgren.Network;
namespace RageCoop.Client namespace RageCoop.Client
{ {
@ -22,7 +23,7 @@ namespace RageCoop.Client
{ {
ID= vehicleID, ID= vehicleID,
NewOwnerID= newOwnerID, NewOwnerID= newOwnerID,
}, ConnectionChannel.SyncEvents); }, ConnectionChannel.SyncEvents,NetDeliveryMethod.ReliableOrdered);
} }
@ -38,24 +39,29 @@ namespace RageCoop.Client
// Reduce latency // Reduce latency
start=impactPosition-(impactPosition-start).Normalized*10; start=impactPosition-(impactPosition-start).Normalized*10;
} }
Networking.SendBulletShot(start, impactPosition, hash, owner.ID); Networking.SendBullet(start, impactPosition, hash, owner.ID);
} }
public static void TriggerVehBulletShot(uint hash, Vehicle veh, SyncedPed owner) public static void TriggerVehBulletShot(uint hash, Vehicle veh, SyncedPed owner)
{ {
int i;
// ANNIHL // ANNIHL
if (veh.Model.Hash==837858166) if (veh.Model.Hash==837858166)
{ {
Networking.SendBulletShot(veh.Bones[35].Position, veh.Bones[35].Position+veh.Bones[35].ForwardVector, hash, owner.ID); Networking.SendVehicleBullet(hash, owner, veh.Bones[35]);
Networking.SendBulletShot(veh.Bones[36].Position, veh.Bones[36].Position+veh.Bones[36].ForwardVector, hash, owner.ID); Networking.SendVehicleBullet(hash, owner, veh.Bones[36]);
Networking.SendBulletShot(veh.Bones[37].Position, veh.Bones[37].Position+veh.Bones[37].ForwardVector, hash, owner.ID); Networking.SendVehicleBullet(hash, owner, veh.Bones[37]);
Networking.SendBulletShot(veh.Bones[38].Position, veh.Bones[38].Position+veh.Bones[38].ForwardVector, hash, owner.ID); Networking.SendVehicleBullet(hash, owner, veh.Bones[38]);
return; }
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}");
} }
var info = veh.GetMuzzleInfo();
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);
} }
public static void TriggerNozzleTransform(int vehID, bool hover) public static void TriggerNozzleTransform(int vehID, bool hover)
{ {
@ -81,10 +87,8 @@ namespace RageCoop.Client
if (v==null) { return; } if (v==null) { return; }
v.OwnerID=p.NewOwnerID; v.OwnerID=p.NewOwnerID;
v.LastSynced=Main.Ticked; v.LastSynced=Main.Ticked;
v.LastFullSynced=Main.Ticked;
v.Position=v.MainVehicle.Position; v.Position=v.MainVehicle.Position;
v.Quaternion=v.MainVehicle.Quaternion; v.Quaternion=v.MainVehicle.Quaternion;
// So this vehicle doesn's get re-spawned
} }
private static void HandleNozzleTransform(Packets.NozzleTransform p) private static void HandleNozzleTransform(Packets.NozzleTransform p)
{ {
@ -144,13 +148,19 @@ namespace RageCoop.Client
} }
else else
{ {
World.CreateParticleEffectNonLooped(CorePFXAsset, "muz_assault_rifle", p.GetMuzzlePosition(), w.Rotation, 1); World.CreateParticleEffectNonLooped(CorePFXAsset, WeaponUtil.GetFlashFX((WeaponHash)weaponHash), p.GetMuzzlePosition(), w.Rotation, 1);
} }
} }
else if (p.VehicleWeapon!=VehicleWeaponHash.Invalid && p.VehicleWeapon == VehicleWeaponHash.Tank) }
public static void HandleVehicleBulletShot(Packets.VehicleBulletShot p)
{ {
World.CreateParticleEffectNonLooped(CorePFXAsset, "muz_tank", p.CurrentVehicle.GetMuzzleInfo().Position, p.CurrentVehicle.Bones[35].ForwardVector.ToEulerRotation(p.CurrentVehicle.Bones[35].UpVector), 1); 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);
} }
public static void HandleEvent(PacketType type, byte[] data) public static void HandleEvent(PacketType type, byte[] data)
{ {
@ -163,6 +173,11 @@ namespace RageCoop.Client
HandleBulletShot(p.StartPosition, p.EndPosition, p.WeaponHash, p.OwnerID); HandleBulletShot(p.StartPosition, p.EndPosition, p.WeaponHash, p.OwnerID);
break; break;
} }
case PacketType.VehicleBulletShot:
{
HandleVehicleBulletShot(data.GetPacket<Packets.VehicleBulletShot>());
break;
}
case PacketType.OwnerChanged: case PacketType.OwnerChanged:
{ {
Packets.OwnerChanged packet = new Packets.OwnerChanged(); Packets.OwnerChanged packet = new Packets.OwnerChanged();

View File

@ -2,6 +2,7 @@
using GTA.Math; using GTA.Math;
using GTA.Native; using GTA.Native;
using System.Collections.Generic; using System.Collections.Generic;
using System.Xml;
namespace RageCoop.Client namespace RageCoop.Client
{ {
@ -73,132 +74,123 @@ namespace RageCoop.Client
*/ */
} }
public static MuzzleInfo GetMuzzleInfo(this Vehicle v) public static int GetMuzzleIndex(this Vehicle v)
{ {
BulletsShot++; BulletsShot++;
int i;
switch (v.Model.Hash) switch (v.Model.Hash)
{ {
// JB7002 // JB7002
case 394110044: case 394110044:
i=BulletsShot%2==0 ? 54 : 53; return BulletsShot%2==0 ? 54 : 53;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// DOMINATOR5 // DOMINATOR5
case -1375060657: case -1375060657:
i=BulletsShot%2==0 ? 35 : 36; return BulletsShot%2==0 ? 35 : 36;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// IMPALER3 // IMPALER3
case -1924800695: case -1924800695:
i=BulletsShot%2==0 ? 75 : 76; return BulletsShot%2==0 ? 75 : 76;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// IMPERATOR2 // IMPERATOR2
case 1637620610: case 1637620610:
i=BulletsShot%2==0 ? 97 : 99; return BulletsShot%2==0 ? 97 : 99;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// SLAMVAN5 // SLAMVAN5
case 373261600: case 373261600:
i=BulletsShot%2==0 ? 51 : 53; return BulletsShot%2==0 ? 51 : 53;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// RUINER2 // RUINER2
case 941494461: case 941494461:
i=BulletsShot%2==0 ? 65 : 66; return BulletsShot%2==0 ? 65 : 66;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// TAMPA3 // TAMPA3
case -1210451983: case -1210451983:
return new MuzzleInfo(v.Bones[87].Position, v.Bones[87].ForwardVector); return 87;
// SCRAMJET // SCRAMJET
case -638562243: case -638562243:
i=BulletsShot%2==0 ? 44 : 45; return BulletsShot%2==0 ? 44 : 45;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// VIGILANTE // VIGILANTE
case -1242608589: case -1242608589:
i=BulletsShot%2==0 ? 42 : 43; return BulletsShot%2==0 ? 42 : 43;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// ZR380 // ZR380
case 540101442: case 540101442:
i=BulletsShot%2==0 ? 57 : 63; return BulletsShot%2==0 ? 57 : 63;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// ZR3802 // ZR3802
case -1106120762: case -1106120762:
i=BulletsShot%2==0 ? 57 : 63; return BulletsShot%2==0 ? 57 : 63;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// ZR3803 // ZR3803
case -1478704292: case -1478704292:
i=BulletsShot%2==0 ? 53 : 59; return BulletsShot%2==0 ? 53 : 59;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// STROMBERG // STROMBERG
case 886810209: case 886810209:
i=BulletsShot%2==0 ? 85 : 84; return BulletsShot%2==0 ? 85 : 84;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// SLAMVAN4 // SLAMVAN4
case -2061049099: case -2061049099:
i=BulletsShot%2==0 ? 76 : 78; return BulletsShot%2==0 ? 76 : 78;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// IMPERATOR // IMPERATOR
case 444994115: case 444994115:
i=BulletsShot%2==0 ? 88 : 86; return BulletsShot%2==0 ? 88 : 86;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// IMPALER2 // IMPALER2
case 1009171724: case 1009171724:
i=BulletsShot%2==0 ? 63 : 64; return BulletsShot%2==0 ? 63 : 64;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// DOMINATOR4 // DOMINATOR4
case -688189648: case -688189648:
i=BulletsShot%2==0 ? 59 : 60; return BulletsShot%2==0 ? 59 : 60;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// SAVAGE // SAVAGE
case -82626025: case -82626025:
return new MuzzleInfo(v.Bones[30].Position, v.Bones[30].ForwardVector); return 30;
// BUZZARD // BUZZARD
case 788747387: case 788747387:
i=BulletsShot%2==0 ? 28 : 23; return BulletsShot%2==0 ? 28 : 23;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// ANNIHL // ANNIHL
case 837858166: case 837858166:
i=(int)BulletsShot%4+35; return (int)BulletsShot%4+35;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// HYDRA // HYDRA
case 970385471: case 970385471:
i=BulletsShot%2==0 ? 29 : 28; return BulletsShot%2==0 ? 29 : 28;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// STARLING // STARLING
case -1700874274: case -1700874274:
i=BulletsShot%2==0 ? 24 : 12; return BulletsShot%2==0 ? 24 : 12;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
// RHINO // RHINO
case 782665360: case 782665360:
return new MuzzleInfo(v.Bones[35].Position, v.Bones[35].ForwardVector); return 30;
default: default:
return null; return -1;
} }
} }
@ -315,5 +307,71 @@ namespace RageCoop.Client
(VehicleWeaponHash)3565779982, // STROMBERG missiles (VehicleWeaponHash)3565779982, // STROMBERG missiles
(VehicleWeaponHash)3169388763, // SCRAMJET missiles (VehicleWeaponHash)3169388763, // SCRAMJET missiles
}; };
public static string GetFlashFX(this WeaponHash w)
{
switch (w.GetWeaponGroup())
{
case WeaponGroup.SMG:
return "muz_smg";
case WeaponGroup.Shotgun:
return "muz_smg";
case WeaponGroup.AssaultRifle:
return "muz_assault_rifle";
case WeaponGroup.Pistol:
return "muz_pistol";
case WeaponGroup.Stungun:
return "muz_stungun";
case WeaponGroup.Heavy:
switch (w)
{
case WeaponHash.Minigun:
return "muz_minigun";
case WeaponHash.RPG:
return "muz_rpg";
default:
return "muz_minigun";
}
case WeaponGroup.Sniper:
return "muz_alternate_star";
case WeaponGroup.PetrolCan:
return "weap_petrol_can";
case WeaponGroup.FireExtinguisher:
return "weap_extinguisher";
}
switch ((VehicleWeaponHash)w)
{
case VehicleWeaponHash.Tank:
return "muz_tank";
case VehicleWeaponHash.PlayerBuzzard:
return "muz_buzzard";
}
return "muz_assault_rifle";
}
public static WeaponGroup GetWeaponGroup(this WeaponHash hash)
{
return Function.Call<WeaponGroup>(Hash.GET_WEAPONTYPE_GROUP, hash);
}
}
class WeaponInfo
{
public string Name;
public string MuzzleFx;
}
public class AimingInfo
{
public string Name;
public float HeadingLimit;
public float SweepPitchMin;
public float SweepPitchMax;
} }
} }

View File

@ -47,8 +47,8 @@ namespace RageCoop.Core
PedKilled=30, PedKilled=30,
BulletShot=31, BulletShot=31,
VehicleBulletShot = 32,
OwnerChanged =35, OwnerChanged =35,
VehicleBulletShot = 36,
NozzleTransform=37, NozzleTransform=37,
#endregion #endregion

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
using GTA.Math;
namespace RageCoop.Core
{
internal partial class Packets
{
internal class VehicleBulletShot : Packet
{
public override PacketType Type => PacketType.VehicleBulletShot;
public int OwnerID { get; set; }
public ushort Bone { get; set; }
public uint WeaponHash { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
public override byte[] Serialize()
{
List<byte> byteArray = new List<byte>();
byteArray.AddInt(OwnerID);
byteArray.AddUshort(Bone);
byteArray.AddUint(WeaponHash);
byteArray.AddVector3(StartPosition);
byteArray.AddVector3(EndPosition);
return byteArray.ToArray();
}
public override void Deserialize(byte[] array)
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
OwnerID=reader.ReadInt32();
Bone=reader.ReadUInt16();
WeaponHash=reader.ReadUInt32();
StartPosition=reader.ReadVector3();
EndPosition=reader.ReadVector3();
#endregion
}
}
}
}