Files
RAGECOOP-V/Client/Scripts/Util/WeaponUtil.cs

196 lines
6.7 KiB
C#
Raw Normal View History

2022-11-16 17:40:07 +08:00
using System.Collections.Generic;
2022-10-23 19:02:39 +08:00
using System.IO;
using GTA;
using GTA.Math;
2022-07-20 17:50:01 +08:00
using GTA.Native;
2022-11-16 17:37:31 +08:00
using RageCoop.Client.Scripting;
2022-10-19 19:07:46 +08:00
using RageCoop.Core;
namespace RageCoop.Client
{
2022-10-23 19:02:39 +08:00
internal class WeaponFix
2022-10-19 19:07:46 +08:00
{
2022-10-23 19:02:39 +08:00
public Dictionary<uint, string> Bullet = new Dictionary<uint, string>();
2022-10-19 19:07:46 +08:00
public Dictionary<uint, string> Lazer = new Dictionary<uint, string>();
2022-11-16 17:37:31 +08:00
public Dictionary<uint, string> Others = new Dictionary<uint, string>();
}
2022-10-23 19:02:39 +08:00
internal static class WeaponUtil
{
2022-10-19 19:07:46 +08:00
public static Dictionary<uint, VehicleWeaponInfo> VehicleWeapons = new Dictionary<uint, VehicleWeaponInfo>();
public static WeaponFix WeaponFix;
2022-11-16 17:37:31 +08:00
public static Dictionary<uint, WeaponInfo> Weapons;
2022-10-23 19:02:39 +08:00
2022-11-16 17:37:31 +08:00
static WeaponUtil()
2022-10-23 19:02:39 +08:00
{
2022-11-16 17:37:31 +08:00
// Parse and load to memory
2023-02-12 22:06:57 +08:00
foreach (var w in JsonDeserialize<VehicleWeaponInfo[]>(
2022-11-16 17:37:31 +08:00
File.ReadAllText(VehicleWeaponDataPath))) VehicleWeapons.Add(w.Hash, w);
2022-10-23 19:02:39 +08:00
2023-02-12 22:06:57 +08:00
Weapons = JsonDeserialize<Dictionary<uint, WeaponInfo>>(
2022-11-16 17:37:31 +08:00
File.ReadAllText(WeaponInfoDataPath));
2022-10-23 19:02:39 +08:00
2022-11-16 17:37:31 +08:00
if (File.Exists(WeaponFixDataPath))
2023-02-12 22:06:57 +08:00
WeaponFix = JsonDeserialize<WeaponFix>(File.ReadAllText(WeaponFixDataPath));
2022-11-16 17:37:31 +08:00
else
2023-02-13 17:51:18 +08:00
Log.Warning("Weapon fix data not found");
2022-10-19 19:07:46 +08:00
}
2022-11-16 17:40:07 +08:00
2022-11-16 17:37:31 +08:00
public static void DumpWeaponFix(string path)
2022-10-19 19:07:46 +08:00
{
var P = Game.Player.Character;
var pos = P.Position + Vector3.WorldUp * 3;
2022-10-23 19:02:39 +08:00
var types = new HashSet<int> { 3 };
2022-10-19 19:07:46 +08:00
P.IsInvincible = true;
var fix = new WeaponFix();
2022-11-16 17:37:31 +08:00
foreach (var w in Weapons)
2022-10-19 19:07:46 +08:00
{
Console.PrintInfo("Testing " + w.Value.Name);
2022-11-16 17:37:31 +08:00
if (w.Value.FireType != "PROJECTILE")
2022-10-19 19:07:46 +08:00
{
2022-11-16 17:37:31 +08:00
var asset = new WeaponAsset(w.Key);
2022-10-19 19:07:46 +08:00
asset.Request(1000);
World.ShootBullet(pos, pos + Vector3.WorldUp, P, asset, 0, 1000);
if (!Call<bool>(IS_BULLET_IN_AREA, pos.X, pos.Y, pos.Z, 10f, true) &&
!Call<bool>(IS_PROJECTILE_IN_AREA, pos.X - 10, pos.Y - 10, pos.Z - 10, pos.X + 10,
2022-10-23 19:02:39 +08:00
pos.Y + 10, pos.Z + 10, true))
2022-11-16 17:37:31 +08:00
switch (w.Value.DamageType)
{
case "BULLET":
fix.Bullet.Add(w.Key, w.Value.Name);
break;
case "EXPLOSIVE":
fix.Lazer.Add(w.Key, w.Value.Name);
break;
default:
fix.Others.Add(w.Key, w.Value.Name);
break;
}
2022-11-16 17:40:07 +08:00
2022-10-23 19:02:39 +08:00
foreach (var p in World.GetAllProjectiles()) p.Delete();
2022-10-19 19:07:46 +08:00
Script.Wait(50);
}
}
2022-10-23 19:02:39 +08:00
2023-02-12 22:06:57 +08:00
File.WriteAllText(path, JsonSerialize(fix));
2022-10-19 19:07:46 +08:00
P.IsInvincible = false;
}
2022-11-16 17:40:07 +08:00
2022-11-16 17:37:31 +08:00
public static uint GetWeaponFix(uint hash)
2022-10-19 19:07:46 +08:00
{
2022-11-16 17:37:31 +08:00
if (WeaponFix.Bullet.TryGetValue(hash, out _)) return 0x461DDDB0;
if (WeaponFix.Lazer.TryGetValue(hash, out _)) return 0xE2822A29;
2022-10-23 19:02:39 +08:00
2022-10-19 19:07:46 +08:00
return hash;
}
2022-10-23 19:02:39 +08:00
2022-11-16 17:37:31 +08:00
2022-07-12 17:14:06 +08:00
public static Dictionary<uint, bool> GetWeaponComponents(this Weapon weapon)
{
Dictionary<uint, bool> result = null;
if (weapon.Components.Count > 0)
{
result = new Dictionary<uint, bool>();
2022-10-23 19:02:39 +08:00
foreach (var comp in weapon.Components) result.Add((uint)comp.ComponentHash, comp.Active);
2022-07-12 17:14:06 +08:00
}
return result;
}
2023-03-19 13:43:46 +08:00
public static EntityBone GetMuzzleBone(this Ped p)
{
2023-03-19 13:43:46 +08:00
return p.Weapons.CurrentWeaponObject?.Bones["gun_muzzle"];
}
2022-09-06 21:46:35 +08:00
2022-06-17 11:08:49 +08:00
public static float GetWeaponDamage(this Ped P, uint hash)
{
var comp = P.Weapons.Current.Components.GetSuppressorComponent();
return Call<float>(GET_WEAPON_DAMAGE, hash,
2022-10-23 19:02:39 +08:00
comp.Active ? comp.ComponentHash : WeaponComponentHash.Invalid);
2022-06-17 11:08:49 +08:00
}
2022-10-19 19:07:46 +08:00
public static int GetMuzzleIndex(this Vehicle v, VehicleWeaponHash hash)
{
2022-10-23 19:02:39 +08:00
if (VehicleWeapons.TryGetValue((uint)v.Model.Hash, out var veh) &&
veh.Weapons.TryGetValue((uint)hash, out var wp))
2022-10-19 19:07:46 +08:00
return (int)wp.Bones[CoreUtils.RandInt(0, wp.Bones.Length)].BoneIndex;
return -1;
}
2022-10-23 19:02:39 +08:00
2022-10-19 19:07:46 +08:00
public static EntityBone GetMuzzleBone(this Vehicle v, VehicleWeaponHash hash)
{
2022-10-23 19:02:39 +08:00
if ((uint)hash == 1422046295) hash = VehicleWeaponHash.WaterCannon;
2022-10-19 19:07:46 +08:00
var i = v.GetMuzzleIndex(hash);
2022-10-23 19:02:39 +08:00
if (i == -1) return null;
2022-10-19 19:07:46 +08:00
return v.Bones[i];
}
2022-10-23 19:02:39 +08:00
public static bool IsUsingProjectileWeapon(this Ped p)
{
var vp = p.VehicleWeapon;
2022-11-16 17:40:07 +08:00
return Weapons.TryGetValue(vp != VehicleWeaponHash.Invalid ? (uint)vp : (uint)p.Weapons.Current.Hash,
out var info)
2022-11-16 17:37:31 +08:00
&& info.FireType == "PROJECTILE";
}
2022-11-16 17:40:07 +08:00
2022-10-23 19:02:39 +08:00
public static string GetFlashFX(this WeaponHash w, bool veh)
{
2022-10-19 19:07:46 +08:00
if (veh)
switch ((VehicleWeaponHash)w)
{
case VehicleWeaponHash.Tank:
return "muz_tank";
default: return "muz_buzzard";
}
2022-10-23 19:02:39 +08:00
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";
2022-10-19 19:07:46 +08:00
default:
return "muz_assault_rifle";
}
}
2022-10-23 19:02:39 +08:00
public static WeaponGroup GetWeaponGroup(this WeaponHash hash)
{
return Call<WeaponGroup>(GET_WEAPONTYPE_GROUP, hash);
}
}
2022-10-23 19:02:39 +08:00
}