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;
private static List<Func<bool>> QueuedActions = new List<Func<bool>>();
public static Worker Worker;
/// <summary>
/// Don't use it!
/// </summary>
@ -101,7 +102,14 @@ namespace RageCoop.Client
Util.NativeMemory();
Counter.Restart();
}
string hash(string t)
{
unchecked
{
return "0x"+((uint)Game.GenerateHash(t)).ToString("X");
}
}
#if DEBUG
#endif
public static Ped P;

View File

@ -179,7 +179,7 @@ namespace RageCoop.Client
#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()
{
@ -189,6 +189,17 @@ namespace RageCoop.Client
WeaponHash=weapon,
}, 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
public static void SendChatMessage(string message)
{

View File

@ -84,7 +84,7 @@ namespace RageCoop.Client
var v = Shooter?.MainPed?.CurrentVehicle;
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);

View File

@ -4,6 +4,7 @@ using RageCoop.Core;
using System;
using System.Threading;
using System.Threading.Tasks;
using Lidgren.Network;
namespace RageCoop.Client
{
@ -22,7 +23,7 @@ namespace RageCoop.Client
{
ID= vehicleID,
NewOwnerID= newOwnerID,
}, ConnectionChannel.SyncEvents);
}, ConnectionChannel.SyncEvents,NetDeliveryMethod.ReliableOrdered);
}
@ -38,24 +39,29 @@ namespace RageCoop.Client
// Reduce latency
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)
{
int i;
// ANNIHL
if (veh.Model.Hash==837858166)
{
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;
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}");
}
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)
{
@ -81,10 +87,8 @@ namespace RageCoop.Client
if (v==null) { return; }
v.OwnerID=p.NewOwnerID;
v.LastSynced=Main.Ticked;
v.LastFullSynced=Main.Ticked;
v.Position=v.MainVehicle.Position;
v.Quaternion=v.MainVehicle.Quaternion;
// So this vehicle doesn's get re-spawned
}
private static void HandleNozzleTransform(Packets.NozzleTransform p)
{
@ -144,13 +148,19 @@ namespace RageCoop.Client
}
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)
{
World.CreateParticleEffectNonLooped(CorePFXAsset, "muz_tank", p.CurrentVehicle.GetMuzzleInfo().Position, p.CurrentVehicle.Bones[35].ForwardVector.ToEulerRotation(p.CurrentVehicle.Bones[35].UpVector), 1);
}
}
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);
}
public static void HandleEvent(PacketType type, byte[] data)
{
@ -163,6 +173,11 @@ namespace RageCoop.Client
HandleBulletShot(p.StartPosition, p.EndPosition, p.WeaponHash, p.OwnerID);
break;
}
case PacketType.VehicleBulletShot:
{
HandleVehicleBulletShot(data.GetPacket<Packets.VehicleBulletShot>());
break;
}
case PacketType.OwnerChanged:
{
Packets.OwnerChanged packet = new Packets.OwnerChanged();

View File

@ -2,6 +2,7 @@
using GTA.Math;
using GTA.Native;
using System.Collections.Generic;
using System.Xml;
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++;
int i;
switch (v.Model.Hash)
{
// JB7002
case 394110044:
i=BulletsShot%2==0 ? 54 : 53;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 54 : 53;
// DOMINATOR5
case -1375060657:
i=BulletsShot%2==0 ? 35 : 36;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 35 : 36;
// IMPALER3
case -1924800695:
i=BulletsShot%2==0 ? 75 : 76;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 75 : 76;
// IMPERATOR2
case 1637620610:
i=BulletsShot%2==0 ? 97 : 99;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 97 : 99;
// SLAMVAN5
case 373261600:
i=BulletsShot%2==0 ? 51 : 53;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 51 : 53;
// RUINER2
case 941494461:
i=BulletsShot%2==0 ? 65 : 66;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 65 : 66;
// TAMPA3
case -1210451983:
return new MuzzleInfo(v.Bones[87].Position, v.Bones[87].ForwardVector);
return 87;
// SCRAMJET
case -638562243:
i=BulletsShot%2==0 ? 44 : 45;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 44 : 45;
// VIGILANTE
case -1242608589:
i=BulletsShot%2==0 ? 42 : 43;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 42 : 43;
// ZR380
case 540101442:
i=BulletsShot%2==0 ? 57 : 63;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 57 : 63;
// ZR3802
case -1106120762:
i=BulletsShot%2==0 ? 57 : 63;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 57 : 63;
// ZR3803
case -1478704292:
i=BulletsShot%2==0 ? 53 : 59;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 53 : 59;
// STROMBERG
case 886810209:
i=BulletsShot%2==0 ? 85 : 84;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 85 : 84;
// SLAMVAN4
case -2061049099:
i=BulletsShot%2==0 ? 76 : 78;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 76 : 78;
// IMPERATOR
case 444994115:
i=BulletsShot%2==0 ? 88 : 86;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 88 : 86;
// IMPALER2
case 1009171724:
i=BulletsShot%2==0 ? 63 : 64;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 63 : 64;
// DOMINATOR4
case -688189648:
i=BulletsShot%2==0 ? 59 : 60;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 59 : 60;
// SAVAGE
case -82626025:
return new MuzzleInfo(v.Bones[30].Position, v.Bones[30].ForwardVector);
return 30;
// BUZZARD
case 788747387:
i=BulletsShot%2==0 ? 28 : 23;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 28 : 23;
// ANNIHL
case 837858166:
i=(int)BulletsShot%4+35;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return (int)BulletsShot%4+35;
// HYDRA
case 970385471:
i=BulletsShot%2==0 ? 29 : 28;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 29 : 28;
// STARLING
case -1700874274:
i=BulletsShot%2==0 ? 24 : 12;
return new MuzzleInfo(v.Bones[i].Position, v.Bones[i].ForwardVector);
return BulletsShot%2==0 ? 24 : 12;
// RHINO
case 782665360:
return new MuzzleInfo(v.Bones[35].Position, v.Bones[35].ForwardVector);
return 30;
default:
return null;
return -1;
}
}
@ -315,5 +307,71 @@ namespace RageCoop.Client
(VehicleWeaponHash)3565779982, // STROMBERG 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,
BulletShot=31,
OwnerChanged=35,
VehicleBulletShot = 36,
VehicleBulletShot = 32,
OwnerChanged =35,
NozzleTransform=37,
#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
}
}
}
}