From 95bbca6499265ff0b37ba8c6d65578ecbf1ad595 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 4 Dec 2026 19:11:27 +0100 Subject: [PATCH] Load 15-vehicle catalog from .cc and add VTOL flight, pitch arcs, PD, and quick-bolt --- src/RustToDust.Core/Broadcast.cs | 5 + src/RustToDust.Core/Catalog.cs | 351 ++++++---------- src/RustToDust.Core/MatchRules.cs | 49 ++- src/RustToDust.Core/MatchState.cs | 75 ++-- src/RustToDust.Core/SpectatorRing.cs | 29 +- src/RustToDust.Ymir/ArenaSession.cs | 573 ++++++++++++++++++++++----- 6 files changed, 713 insertions(+), 369 deletions(-) diff --git a/src/RustToDust.Core/Broadcast.cs b/src/RustToDust.Core/Broadcast.cs index 89df8c3..25e3226 100644 --- a/src/RustToDust.Core/Broadcast.cs +++ b/src/RustToDust.Core/Broadcast.cs @@ -9,6 +9,7 @@ namespace RustToDust.Core ExemplarWakeup, PatronClearance, ChassisSwap, + QuickBolt, Overheat, Elimination, Victory @@ -55,6 +56,10 @@ namespace RustToDust.Core Emit(timestamp, BroadcastKind.ChassisSwap, $"Contender {contenderId + 1:00} sheds a ruin and mounts a {chassisName}. Vulgar improvisation, yet watchable."); + public void EmitQuickBolt(float timestamp, int contenderId, string moduleName, string hardpointId) => + Emit(timestamp, BroadcastKind.QuickBolt, + $"Contender {contenderId + 1:00} quick-bolts {moduleName} onto {hardpointId} mid-run. Crude fieldcraft, live fire ready."); + public void EmitOverheat(float timestamp, int contenderId) => Emit(timestamp, BroadcastKind.Overheat, $"Contender {contenderId + 1:00} glows past the thermal plateau — the crowd admires the smoke."); diff --git a/src/RustToDust.Core/Catalog.cs b/src/RustToDust.Core/Catalog.cs index f54fa43..77a7160 100644 --- a/src/RustToDust.Core/Catalog.cs +++ b/src/RustToDust.Core/Catalog.cs @@ -1,232 +1,68 @@ using System; using System.Collections.Generic; +using System.Linq; namespace RustToDust.Core { public static class Catalog { - private static bool[] ParseMask(params string[] rows) + private static readonly Dictionary _packagesById; + private static readonly Dictionary _chassisById; + private static readonly IReadOnlyList _allChassis; + + static Catalog() { - int h = rows.Length; - int w = rows[0].Length; - var mask = new bool[w * h]; - for (int y = 0; y < h; y++) + var loaded = VehicleCatalogLoader.LoadAll(); + _packagesById = new Dictionary(StringComparer.OrdinalIgnoreCase); + _chassisById = new Dictionary(StringComparer.OrdinalIgnoreCase); + var list = new List(loaded.Count); + + foreach (var (pkg, spec) in loaded) { - for (int x = 0; x < w; x++) - { - mask[y * w + x] = rows[y][x] == '#'; - } + _packagesById[pkg.Id] = pkg; + _chassisById[spec.Id] = spec; + list.Add(spec); } - return mask; + + _allChassis = list.AsReadOnly(); } - // ---------------- CHASSIS (6 PLAYABLE) ---------------- + private static ChassisSpec RequireChassis(string id) => + _chassisById.TryGetValue(id, out var spec) + ? spec + : throw new KeyNotFoundException($"Missing chassis '{id}' in loaded vehicle.cc catalog."); - public static readonly ChassisSpec Buggy = new( - Id: "buggy", - Name: "Dune Buggy", - Plane: GridPlane.Dorsal, - Width: 3, - Height: 4, - Mask: ParseMask( - "###", - "###", - "###", - "###"), - Hardpoints: new[] - { - new HardpointSpec("wpn_roof", HardpointKind.Weapon, 1, 0, 1, 1, 5f), - new HardpointSpec("eng_mid", HardpointKind.Engine, 1, 2, 1, 1, 5f), - new HardpointSpec("rad_rear", HardpointKind.Radiator, 1, 3, 1, 1, 0f) - }, - HullConductivity: 8f, - BaseCellThermalMass: 10f, - BaseCellArmor: 24f, - MassKg: 950f, - BaseThrust: 14500f, - BaseTurnRate: 4.2f, - MaxSpeed: 34f, - HalfWidth: 1.05f, - HalfHeight: 0.75f, - HalfLength: 1.65f, - CargoCapacity: 4); + // ---------------- CHASSIS (15 BROKKR PACKAGES LOADED FROM .CC) ---------------- - public static readonly ChassisSpec Technical = new( - Id: "technical", - Name: "Gun Truck", - Plane: GridPlane.Dorsal, - Width: 3, - Height: 5, - Mask: ParseMask( - "###", - "###", - "###", - "###", - "###"), - Hardpoints: new[] - { - new HardpointSpec("wpn_cab", HardpointKind.Weapon, 1, 0, 1, 1, 6f), - new HardpointSpec("eng_hood", HardpointKind.Engine, 1, 1, 1, 1, 6f), - new HardpointSpec("wpn_bed", HardpointKind.Weapon, 1, 2, 1, 2, 8f), - new HardpointSpec("rad_tail", HardpointKind.Radiator, 2, 4, 1, 1, 0f), - new HardpointSpec("arm_bumper", HardpointKind.Armor, 0, 0, 1, 1, 10f) - }, - HullConductivity: 8.5f, - BaseCellThermalMass: 13f, - BaseCellArmor: 34f, - MassKg: 1850f, - BaseThrust: 23500f, - BaseTurnRate: 3.3f, - MaxSpeed: 29f, - HalfWidth: 1.25f, - HalfHeight: 0.95f, - HalfLength: 2.3f, - CargoCapacity: 6); + public static ChassisSpec Jackal => RequireChassis("jackal"); + public static ChassisSpec Mule => RequireChassis("mule"); + public static ChassisSpec Ferret => RequireChassis("ferret"); + public static ChassisSpec Hornet => RequireChassis("hornet"); + public static ChassisSpec Mammoth => RequireChassis("mammoth"); + public static ChassisSpec Ironclad => RequireChassis("ironclad"); + public static ChassisSpec Anvil => RequireChassis("anvil"); + public static ChassisSpec Condor => RequireChassis("condor"); + public static ChassisSpec Breaker => RequireChassis("breaker"); + public static ChassisSpec Paladin => RequireChassis("paladin"); + public static ChassisSpec Raptor => RequireChassis("raptor"); + public static ChassisSpec Manticore => RequireChassis("manticore"); + public static ChassisSpec Bastion => RequireChassis("bastion"); + public static ChassisSpec Spectre => RequireChassis("spectre"); + public static ChassisSpec Kestrel => RequireChassis("kestrel"); - public static readonly ChassisSpec Tank = new( - Id: "tank", - Name: "Ironclad Tank", - Plane: GridPlane.Dorsal, - Width: 4, - Height: 5, - Mask: ParseMask( - "####", - "####", - "####", - "####", - "####"), - Hardpoints: new[] - { - new HardpointSpec("arm_glacis", HardpointKind.Armor, 1, 0, 2, 1, 24f), - new HardpointSpec("wpn_coax", HardpointKind.Weapon, 0, 1, 1, 1, 10f), - new HardpointSpec("wpn_turret", HardpointKind.Weapon, 1, 1, 2, 2, 18f), - new HardpointSpec("arm_skirt", HardpointKind.Armor, 0, 2, 1, 1, 16f), - new HardpointSpec("eng_rear", HardpointKind.Engine, 1, 3, 2, 2, 10f), - new HardpointSpec("rad_deck", HardpointKind.Radiator, 3, 3, 1, 1, 0f) - }, - HullConductivity: 9.5f, - BaseCellThermalMass: 18f, - BaseCellArmor: 62f, - MassKg: 4200f, - BaseThrust: 38000f, - BaseTurnRate: 2.1f, - MaxSpeed: 20f, - HalfWidth: 1.65f, - HalfHeight: 1.1f, - HalfLength: 2.65f, - CargoCapacity: 8); + // Backward-compatible aliases for legacy Cut 1/2 call sites + public static ChassisSpec Buggy => Jackal; + public static ChassisSpec Technical => Mule; + public static ChassisSpec Tank => Anvil; - public static readonly ChassisSpec Manticore = new( - Id: "manticore", - Name: "Manticore Quadruped", - Plane: GridPlane.Dorsal, - Width: 5, - Height: 5, - Mask: ParseMask( - "#####", - ".###.", - "#####", - ".###.", - "#.#.#"), - Hardpoints: new[] - { - new HardpointSpec("wpn_chin", HardpointKind.Weapon, 2, 0, 1, 1, 10f), - new HardpointSpec("wpn_left", HardpointKind.Weapon, 0, 2, 1, 1, 8f), - new HardpointSpec("wpn_right", HardpointKind.Weapon, 4, 2, 1, 1, 8f), - new HardpointSpec("eng_core", HardpointKind.Engine, 2, 1, 1, 2, 12f), - new HardpointSpec("rad_left", HardpointKind.Radiator, 1, 1, 1, 1, 4f), - new HardpointSpec("rad_right", HardpointKind.Radiator, 3, 1, 1, 1, 4f), - new HardpointSpec("arm_back", HardpointKind.Armor, 2, 3, 1, 1, 18f) - }, - HullConductivity: 11f, - BaseCellThermalMass: 15f, - BaseCellArmor: 46f, - MassKg: 2800f, - BaseThrust: 34500f, - BaseTurnRate: 3.6f, - MaxSpeed: 26f, - HalfWidth: 1.6f, - HalfHeight: 1.35f, - HalfLength: 2.1f, - CargoCapacity: 7); + public static IReadOnlyList AllChassis => _allChassis; + public static IReadOnlyDictionary ChassisById => _chassisById; + public static IReadOnlyDictionary PackagesById => _packagesById; - public static readonly ChassisSpec Raptor = new( - Id: "raptor", - Name: "Raptor Biped", - Plane: GridPlane.Coronal, - Width: 5, - Height: 6, - Mask: ParseMask( - "..#..", - ".###.", - "#####", - "#.#.#", - ".#.#.", - ".#.#."), - Hardpoints: new[] - { - new HardpointSpec("rad_lsh", HardpointKind.Radiator, 1, 1, 1, 1, 4f), - new HardpointSpec("rad_rsh", HardpointKind.Radiator, 3, 1, 1, 1, 4f), - new HardpointSpec("wpn_larm", HardpointKind.Weapon, 0, 2, 1, 2, 8f), - new HardpointSpec("eng_chest", HardpointKind.Engine, 2, 2, 1, 2, 12f), - new HardpointSpec("wpn_rarm", HardpointKind.Weapon, 4, 2, 1, 2, 8f) - }, - HullConductivity: 10.5f, - BaseCellThermalMass: 14f, - BaseCellArmor: 42f, - MassKg: 2300f, - BaseThrust: 33000f, - BaseTurnRate: 4.1f, - MaxSpeed: 30f, - HalfWidth: 1.35f, - HalfHeight: 1.95f, - HalfLength: 1.15f, - CargoCapacity: 6); + public static VehiclePackage? TryGetPackage(string id) => + _packagesById.TryGetValue(id, out var pkg) ? pkg : null; - public static readonly ChassisSpec Paladin = new( - Id: "paladin", - Name: "Paladin Heavy Biped", - Plane: GridPlane.Coronal, - Width: 6, - Height: 7, - Mask: ParseMask( - "..##..", - ".####.", - "######", - "######", - ".####.", - ".#..#.", - ".#..#."), - Hardpoints: new[] - { - new HardpointSpec("wpn_lsh", HardpointKind.Weapon, 1, 1, 1, 1, 12f), - new HardpointSpec("arm_collar", HardpointKind.Armor, 2, 1, 2, 1, 22f), - new HardpointSpec("wpn_rsh", HardpointKind.Weapon, 4, 1, 1, 1, 12f), - new HardpointSpec("wpn_larm", HardpointKind.Weapon, 0, 2, 1, 2, 12f), - new HardpointSpec("eng_core", HardpointKind.Engine, 2, 2, 2, 2, 16f), - new HardpointSpec("wpn_rarm", HardpointKind.Weapon, 5, 2, 1, 2, 12f), - new HardpointSpec("rad_l", HardpointKind.Radiator, 1, 4, 1, 1, 6f), - new HardpointSpec("arm_waist", HardpointKind.Armor, 2, 4, 2, 1, 20f), - new HardpointSpec("rad_r", HardpointKind.Radiator, 4, 4, 1, 1, 6f) - }, - HullConductivity: 11.5f, - BaseCellThermalMass: 18f, - BaseCellArmor: 58f, - MassKg: 4100f, - BaseThrust: 43000f, - BaseTurnRate: 2.7f, - MaxSpeed: 22f, - HalfWidth: 1.75f, - HalfHeight: 2.35f, - HalfLength: 1.4f, - CargoCapacity: 9); - - public static readonly IReadOnlyList AllChassis = new[] - { - Buggy, Technical, Tank, Manticore, Raptor, Paladin - }; - - // ---------------- HUMAN SALVAGE MODULES (8) ---------------- + // ---------------- HUMAN SALVAGE MODULES (9) ---------------- public static readonly ModuleSpec Mg762 = new( Id: "mg_762", @@ -303,6 +139,34 @@ namespace RustToDust.Core SpreadRad: 0.14f, BurstCount: 5); + public static readonly ModuleSpec PointDefenseFlak = new( + Id: "pd_flak", + Name: "Flak Point-Defense", + Kind: HardpointKind.Weapon, + Width: 1, + Height: 1, + IsImperial: false, + ThermalMass: 6f, + Conductivity: 10f, + OptimalTemp: 65f, + PlateauWidth: 80f, + MinTemp: 0f, + MaxTemp: 165f, + HeatOutput: 24f, + CoolingPower: 0f, + ThrustBonus: 0f, + TurnBonus: 0f, + ArmorBonus: 0f, + WeaponDamage: 11f, + WeaponRange: 175f, + FireCooldown: 0.24f, + ProjectileSpeed: 340f, + SpreadRad: 0.045f, + BurstCount: 3, + IsWideArc: true, + MinPitchDeg: -10f, + MaxPitchDeg: 85f); + public static readonly ModuleSpec Recoilless = new( Id: "recoilless", Name: "105mm Recoilless Rifle", @@ -428,7 +292,7 @@ namespace RustToDust.Core SpreadRad: 0f, BurstCount: 0); - // ---------------- IMPERIAL TOURIST HIGH-END MODULES (5) ---------------- + // ---------------- IMPERIAL TOURIST HIGH-END MODULES (6) ---------------- public static readonly ModuleSpec GildedLance = new( Id: "gilded_lance", @@ -480,6 +344,34 @@ namespace RustToDust.Core SpreadRad: 0.02f, BurstCount: 1); + public static readonly ModuleSpec ImperialAegisPd = new( + Id: "aegis_pd", + Name: "Aegis Imperial PD", + Kind: HardpointKind.Weapon, + Width: 1, + Height: 1, + IsImperial: true, + ThermalMass: 8f, + Conductivity: 16f, + OptimalTemp: 90f, + PlateauWidth: 90f, + MinTemp: 0f, + MaxTemp: 215f, + HeatOutput: 26f, + CoolingPower: 0f, + ThrustBonus: 0f, + TurnBonus: 0f, + ArmorBonus: 0f, + WeaponDamage: 16f, + WeaponRange: 215f, + FireCooldown: 0.16f, + ProjectileSpeed: 420f, + SpreadRad: 0.015f, + BurstCount: 2, + IsWideArc: true, + MinPitchDeg: -10f, + MaxPitchDeg: 85f); + public static readonly ModuleSpec GoldRadiator = new( Id: "gold_radiator", Name: "Gold-Leaf Phase Radiator", @@ -557,26 +449,45 @@ namespace RustToDust.Core public static readonly IReadOnlyList HumanModules = new[] { - Mg762, Autocannon, Flak, Recoilless, DieselCore, TurboCore, CopperRad, WeldedPlate + Mg762, Autocannon, Flak, PointDefenseFlak, Recoilless, DieselCore, TurboCore, CopperRad, WeldedPlate }; public static readonly IReadOnlyList ImperialModules = new[] { - GildedLance, TouringPlasma, GoldRadiator, NobleCore, GildedPlate + GildedLance, TouringPlasma, ImperialAegisPd, GoldRadiator, NobleCore, GildedPlate }; public static readonly IReadOnlyList AllModules = new[] { - Mg762, Autocannon, Flak, Recoilless, DieselCore, TurboCore, CopperRad, WeldedPlate, - GildedLance, TouringPlasma, GoldRadiator, NobleCore, GildedPlate + Mg762, Autocannon, Flak, PointDefenseFlak, Recoilless, DieselCore, TurboCore, CopperRad, WeldedPlate, + GildedLance, TouringPlasma, ImperialAegisPd, GoldRadiator, NobleCore, GildedPlate }; + public static readonly IReadOnlyDictionary ModulesById = + AllModules.ToDictionary(m => m.Id, StringComparer.OrdinalIgnoreCase); + public static ChassisGrid CreateStarterBuggy() { - var grid = new ChassisGrid(Buggy); - grid.TryMount(Mg762, "wpn_roof"); - grid.TryMount(DieselCore, "eng_mid"); - grid.TryMount(CopperRad, "rad_rear"); + var grid = new ChassisGrid(Jackal); + grid.TryMount(Mg762, "hp.weapon.roof"); + grid.TryMount(DieselCore, "hp.engine.rear"); + grid.TryMount(CopperRad, "hp.radiator.rear"); + return grid; + } + + public static ChassisGrid CreateWithDefaultLoadout(ChassisSpec spec) + { + var grid = new ChassisGrid(spec); + if (_packagesById.TryGetValue(spec.Id, out var pkg)) + { + foreach (var kvp in pkg.DefaultLoadout) + { + if (ModulesById.TryGetValue(kvp.Value, out var mod)) + { + grid.TryMount(mod, kvp.Key); + } + } + } return grid; } } diff --git a/src/RustToDust.Core/MatchRules.cs b/src/RustToDust.Core/MatchRules.cs index 6d097f0..0475104 100644 --- a/src/RustToDust.Core/MatchRules.cs +++ b/src/RustToDust.Core/MatchRules.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; namespace RustToDust.Core { @@ -6,13 +7,40 @@ namespace RustToDust.Core public static class MatchRules { + public const float VtolMinClearanceAgl = 2.5f; + public static float GetObserverEyeY(float y, float halfHeight) => y + halfHeight * 1.4f; - public static float GetTargetAimY(float y, float halfHeight) => y + halfHeight * 1.1f; + public static float GetTargetAimY(float y, float halfHeight) => y + halfHeight * 0.35f; + + public static (float MinPitchDeg, float MaxPitchDeg) GetEffectivePitchArc(HardpointSpec hp, ModuleSpec? mod) + { + if (mod is not null && mod.IsWideArc) + { + return (MathF.Min(hp.MinPitchDeg, mod.MinPitchDeg), MathF.Max(hp.MaxPitchDeg, mod.MaxPitchDeg)); + } + if (hp.IsWideArc) + { + return (MathF.Min(hp.MinPitchDeg, -10f), MathF.Max(hp.MaxPitchDeg, 85f)); + } + return (hp.MinPitchDeg, hp.MaxPitchDeg); + } + + public static float ComputeLocalPitchDeg(Vector3 aimDelta, Quaternion chassisQuat) + { + Vector3 local = Vector3.Transform(aimDelta, Quaternion.Conjugate(chassisQuat)); + float horiz = MathF.Sqrt(local.X * local.X + local.Z * local.Z); + return MathF.Atan2(local.Y, horiz) * (180f / MathF.PI); + } public static float NormalizeAngle(float rad) { - while (rad > MathF.PI) rad -= MathF.Tau; - while (rad < -MathF.PI) rad += MathF.Tau; + if (!float.IsFinite(rad)) + { + return 0f; + } + rad = MathF.IEEERemainder(rad, MathF.Tau); + if (rad > MathF.PI) rad -= MathF.Tau; + if (rad < -MathF.PI) rad += MathF.Tau; return rad; } @@ -23,6 +51,7 @@ namespace RustToDust.Core { player.ThrottleCmd = 0f; player.SteerCmd = 0f; + player.VerticalThrottleCmd = 0f; player.FireCmd = false; player.BrakeCmd = true; return; @@ -30,6 +59,7 @@ namespace RustToDust.Core player.ThrottleCmd = Math.Clamp(input.Throttle, -1f, 1f); player.SteerCmd = Math.Clamp(input.Steer, -1f, 1f); + player.VerticalThrottleCmd = Math.Clamp(input.VerticalThrottle, -1f, 1f); player.AimX = input.AimWorldX; player.AimY = input.AimWorldY; player.AimZ = input.AimWorldZ; @@ -209,6 +239,19 @@ namespace RustToDust.Core ai.SteerCmd = Math.Clamp(yawError * 1.8f, -1f, 1f); ai.ThrottleCmd = MathF.Abs(yawError) > 1.25f ? 0.35f : 0.85f; ai.BrakeCmd = false; + if (ai.Grid.Spec.IsVtol) + { + float groundH = state.Terrain.SampleHeight(ai.X, ai.Z); + float currentAgl = ai.Y - ai.Grid.Spec.HalfHeight - groundH; + float desiredAgl = target is null + ? MathF.Min(ai.Grid.Spec.HoverMaxAltitudeM * 0.6f, 22f) + : MathF.Min(ai.Grid.Spec.HoverMaxAltitudeM * 0.45f, 14f + 4f * MathF.Sin(state.Time * 0.9f + ai.Id)); + ai.VerticalThrottleCmd = Math.Clamp((desiredAgl - currentAgl) * 0.25f, -1f, 1f); + } + else + { + ai.VerticalThrottleCmd = 0f; + } ai.AimX = aimX; ai.AimY = target is not null ? GetTargetAimY(target.Y, target.Grid.Spec.HalfHeight) : float.NaN; ai.AimZ = aimZ; diff --git a/src/RustToDust.Core/MatchState.cs b/src/RustToDust.Core/MatchState.cs index 691f183..5721df3 100644 --- a/src/RustToDust.Core/MatchState.cs +++ b/src/RustToDust.Core/MatchState.cs @@ -18,7 +18,8 @@ namespace RustToDust.Core bool Fire, bool InteractPickup, bool Brake, - float AimWorldY = float.NaN); + float AimWorldY = float.NaN, + float VerticalThrottle = 0f); public readonly record struct TracerEvent( float StartX, @@ -102,9 +103,14 @@ namespace RustToDust.Core public float ThrottleCmd { get; set; } public float SteerCmd { get; set; } + public float VerticalThrottleCmd { get; set; } + public float TargetHoverAltAgl { get; set; } = 3.0f; public bool FireCmd { get; set; } public bool BrakeCmd { get; set; } public float AiScavengeCooldown { get; set; } + public bool AllWeaponsOutOfArc { get; set; } + public int OutOfArcWeaponCount { get; set; } + public int InArcWeaponCount { get; set; } public bool Alive => !Grid.IsDestroyed; } @@ -273,12 +279,16 @@ namespace RustToDust.Core return false; } - if (!contender.Grid.TryAddCargo(nearest.Module)) + if (!contender.Grid.TryQuickBoltOrCargo(nearest.Module, out string? boltedHardpointId)) { return false; } nearest.PickedUp = true; + if (boltedHardpointId is not null) + { + Broadcast.EmitQuickBolt(Time, contenderId, nearest.Module.Name, boltedHardpointId); + } return true; } @@ -331,7 +341,9 @@ namespace RustToDust.Core SpawnModuleDrop(contender.X + 1.5f + i * 0.7f, contender.Z - 1.5f, overflow[i]); } - contender.Y = Terrain.SampleHeight(contender.X, contender.Z) + contender.Grid.Spec.HalfHeight + 0.15f; + contender.Y = Terrain.SampleHeight(contender.X, contender.Z) + contender.Grid.Spec.HalfHeight + + (contender.Grid.Spec.IsVtol ? contender.TargetHoverAltAgl : 0.15f); + contender.AiScavengeCooldown = 4.0f; Broadcast.EmitChassisSwap(Time, contender.Id, contender.Grid.Spec.Name); ChassisSwapped?.Invoke(contender); return true; @@ -339,6 +351,13 @@ namespace RustToDust.Core private void SpawnContenders() { + var aiRoster = new List(Catalog.AllChassis); + for (int k = aiRoster.Count - 1; k > 0; k--) + { + int j = Rng.Next(k + 1); + (aiRoster[k], aiRoster[j]) = (aiRoster[j], aiRoster[k]); + } + // Spawn 12 contenders (player + 11 AI) spread across r ~ 240..460m (well inside SafeRadius = 560m) for (int i = 0; i < ContenderCount; i++) { @@ -362,8 +381,16 @@ namespace RustToDust.Core float yaw = MathF.Atan2(-x, -z); string name = i == 0 ? "Contender 01 (You)" : $"Contender {i + 1:00}"; - var contender = new ContenderState(i, name, i == 0, Catalog.CreateStarterBuggy(), x, z, yaw); - contender.Y = Terrain.SampleHeight(x, z) + contender.Grid.Spec.HalfHeight + 0.15f; + var grid = i == 0 + ? Catalog.CreateStarterBuggy() + : Catalog.CreateWithDefaultLoadout(aiRoster[(i - 1) % aiRoster.Count]); + var contender = new ContenderState(i, name, i == 0, grid, x, z, yaw); + if (grid.Spec.IsVtol) + { + contender.TargetHoverAltAgl = 10f; + } + contender.Y = Terrain.SampleHeight(x, z) + contender.Grid.Spec.HalfHeight + + (grid.Spec.IsVtol ? contender.TargetHoverAltAgl : 0.15f); contender.AimY = contender.Y; Contenders.Add(contender); if (i > 0) @@ -380,32 +407,34 @@ namespace RustToDust.Core private void ScatterInitialLoot() { - // 10 deterministic salvageable chassis wrecks scattered across canyons, passes, and salvage pads - ChassisSpec[] chassisPool = + // 14 deterministic salvageable chassis wrecks covering all non-starter vehicles in the 15-vehicle roster + var chassisPool = new List(Catalog.AllChassis.Count); + for (int i = 0; i < Catalog.AllChassis.Count; i++) { - Catalog.Technical, Catalog.Technical, - Catalog.Tank, Catalog.Tank, - Catalog.Manticore, Catalog.Manticore, - Catalog.Raptor, Catalog.Raptor, - Catalog.Paladin, Catalog.Paladin - }; + var c = Catalog.AllChassis[i]; + if (!string.Equals(c.Id, Catalog.Jackal.Id, StringComparison.OrdinalIgnoreCase)) + { + chassisPool.Add(c); + } + } - for (int i = 0; i < chassisPool.Length; i++) + for (int i = 0; i < chassisPool.Count; i++) { - float angle = (i / (float)chassisPool.Length) * MathF.Tau + 0.35f; - float r = i >= 8 ? (55f + i * 10f) : (145f + (i % 4) * 90f); + float angle = (i / (float)chassisPool.Count) * MathF.Tau + 0.35f; + float r = i >= chassisPool.Count - 3 ? (55f + (i % 3) * 18f) : (135f + (i % 4) * 85f); SpawnChassisDrop(MathF.Cos(angle) * r, MathF.Sin(angle) * r, chassisPool[i]); } - // 24 ground salvage module drops spread across canyons, passes, and ruins + // 26 ground salvage module drops spread across canyons, passes, and ruins (including 1x1 Wide-Arc PD!) ModuleSpec[] salvagePool = { - Catalog.Autocannon, Catalog.Flak, Catalog.Recoilless, Catalog.TurboCore, - Catalog.CopperRad, Catalog.WeldedPlate, Catalog.Mg762, Catalog.DieselCore, - Catalog.Autocannon, Catalog.Flak, Catalog.Recoilless, Catalog.TurboCore, - Catalog.CopperRad, Catalog.WeldedPlate, Catalog.Autocannon, Catalog.Recoilless, - Catalog.Flak, Catalog.TurboCore, Catalog.CopperRad, Catalog.WeldedPlate, - Catalog.Mg762, Catalog.Autocannon, Catalog.DieselCore, Catalog.CopperRad + Catalog.Autocannon, Catalog.Flak, Catalog.PointDefenseFlak, Catalog.Recoilless, + Catalog.TurboCore, Catalog.CopperRad, Catalog.WeldedPlate, Catalog.Mg762, + Catalog.DieselCore, Catalog.PointDefenseFlak, Catalog.Autocannon, Catalog.Flak, + Catalog.Recoilless, Catalog.TurboCore, Catalog.CopperRad, Catalog.WeldedPlate, + Catalog.Autocannon, Catalog.PointDefenseFlak, Catalog.Recoilless, Catalog.Flak, + Catalog.TurboCore, Catalog.CopperRad, Catalog.WeldedPlate, Catalog.Mg762, + Catalog.Autocannon, Catalog.ImperialAegisPd }; for (int i = 0; i < salvagePool.Length; i++) diff --git a/src/RustToDust.Core/SpectatorRing.cs b/src/RustToDust.Core/SpectatorRing.cs index b14263a..fa748ed 100644 --- a/src/RustToDust.Core/SpectatorRing.cs +++ b/src/RustToDust.Core/SpectatorRing.cs @@ -759,11 +759,12 @@ namespace RustToDust.Core case SpectatorTier.Tourist: { var grid = new ChassisGrid(Catalog.Paladin); - MountRequired(grid, Catalog.GildedLance, "wpn_larm"); - MountRequired(grid, Catalog.TouringPlasma, "wpn_lsh"); - MountRequired(grid, Catalog.NobleCore, "eng_core"); - MountRequired(grid, Catalog.GoldRadiator, "rad_l"); - MountRequired(grid, Catalog.GildedPlate, "arm_collar"); + MountRequired(grid, Catalog.GildedLance, "hp.weapon.arm_l"); + MountRequired(grid, Catalog.ImperialAegisPd, "hp.weapon.sh_l"); + MountRequired(grid, Catalog.TouringPlasma, "hp.weapon.sh_r"); + MountRequired(grid, Catalog.NobleCore, "hp.engine.chest"); + MountRequired(grid, Catalog.GoldRadiator, "hp.radiator.back_l"); + MountRequired(grid, Catalog.GildedPlate, "hp.armor.torso"); return new SpectatorState( id, tier, grid, angle, ringOffset, radarSignature: 1.0f, @@ -774,10 +775,10 @@ namespace RustToDust.Core case SpectatorTier.Patron: { var grid = new ChassisGrid(Catalog.Raptor); - MountRequired(grid, Catalog.Autocannon, "wpn_larm"); - MountRequired(grid, Catalog.Flak, "wpn_rarm"); - MountRequired(grid, Catalog.TurboCore, "eng_chest"); - MountRequired(grid, Catalog.CopperRad, "rad_lsh"); + MountRequired(grid, Catalog.Autocannon, "hp.weapon.arm_l"); + MountRequired(grid, Catalog.Flak, "hp.weapon.arm_r"); + MountRequired(grid, Catalog.TurboCore, "hp.engine.chest"); + MountRequired(grid, Catalog.CopperRad, "hp.radiator.sh_l"); return new SpectatorState( id, tier, grid, angle, ringOffset, radarSignature: 0.6f, @@ -801,11 +802,11 @@ namespace RustToDust.Core HalfLength = 0.8f }; var grid = new ChassisGrid(exemplarSpec); - MountRequired(grid, Catalog.GildedLance, "wpn_rarm"); - MountRequired(grid, Catalog.TouringPlasma, "wpn_larm"); - MountRequired(grid, Catalog.NobleCore, "eng_chest"); - MountRequired(grid, Catalog.GoldRadiator, "rad_lsh"); - MountRequired(grid, Catalog.GoldRadiator, "rad_rsh"); + MountRequired(grid, Catalog.GildedLance, "hp.weapon.arm_r"); + MountRequired(grid, Catalog.TouringPlasma, "hp.weapon.arm_l"); + MountRequired(grid, Catalog.NobleCore, "hp.engine.chest"); + MountRequired(grid, Catalog.GoldRadiator, "hp.radiator.sh_l"); + MountRequired(grid, Catalog.GoldRadiator, "hp.radiator.sh_r"); return new SpectatorState( id, tier, grid, angle, ringOffset, radarSignature: 0.18f, diff --git a/src/RustToDust.Ymir/ArenaSession.cs b/src/RustToDust.Ymir/ArenaSession.cs index 879694c..24b214a 100644 --- a/src/RustToDust.Ymir/ArenaSession.cs +++ b/src/RustToDust.Ymir/ArenaSession.cs @@ -263,7 +263,10 @@ namespace RustToDust.Ymir float density = spec.MassKg / MathF.Max(0.1f, volume); var q = Quaternion.CreateFromAxisAngle(Vector3.UnitY, c.Yaw); ulong stableId = ContenderStableIdBase + (ulong)c.Id; - float spawnY = State.Terrain.SampleHeight(c.X, c.Z) + spec.HalfHeight + 0.15f; + float clearance = spec.IsVtol + ? Math.Clamp(c.TargetHoverAltAgl, MatchRules.VtolMinClearanceAgl, MathF.Max(MatchRules.VtolMinClearanceAgl, spec.HoverMaxAltitudeM)) + : 0.15f; + float spawnY = State.Terrain.SampleHeight(c.X, c.Z) + spec.HalfHeight + clearance; c.Y = spawnY; var handle = _world.AddBox( @@ -394,6 +397,12 @@ namespace RustToDust.Ymir var vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]); var angVel = new Vector3(view.AngularVelocityX[lane], view.AngularVelocityY[lane], view.AngularVelocityZ[lane]); + if (c.Grid.Spec.IsVtol) + { + ApplyVtolFlightDrive(c, lane, mass, pos, q, vel, angVel, view); + continue; + } + ComputeTerrainSupport( pos, q, vel, angVel, mass, c.Grid.Spec, out Vector3 supportForce, @@ -544,6 +553,123 @@ namespace RustToDust.Ymir } } + private void ApplyVtolFlightDrive( + ContenderState c, + int lane, + float mass, + Vector3 pos, + Quaternion q, + Vector3 vel, + Vector3 angVel, + Box3DSoaView view) + { + var spec = c.Grid.Spec; + float minAlt = MatchRules.VtolMinClearanceAgl; + float maxAlt = MathF.Max(minAlt, spec.HoverMaxAltitudeM); + const float verticalSpeed = 14f; + c.TargetHoverAltAgl = Math.Clamp(c.TargetHoverAltAgl + c.VerticalThrottleCmd * verticalSpeed * FixedDt, minAlt, maxAlt); + + var forward = Vector3.Transform(Vector3.UnitZ, q); + forward.Y = 0f; + if (forward.LengthSquared() < 1e-4f) forward = Vector3.UnitZ; + else forward = Vector3.Normalize(forward); + var right = Vector3.Cross(forward, Vector3.UnitY); + var up = Vector3.Transform(Vector3.UnitY, q); + + // Sample terrain below craft and immediately ahead so VTOL tracks contours cleanly without bouncing + float ghCenter = State.Terrain.SampleHeight(pos.X, pos.Z); + float ghNear = State.Terrain.SampleHeight(pos.X + forward.X * 2.5f, pos.Z + forward.Z * 2.5f); + var (tnx, tny, tnz) = State.Terrain.SampleNormal(pos.X, pos.Z); + var terrainNormal = new Vector3(tnx, tny, tnz); + + float bottomAgl = (pos.Y - spec.HalfHeight) - ghCenter; + float localGround = ghNear > ghCenter && MathF.Abs(c.ThrottleCmd) > 0.1f + ? ghCenter * 0.70f + ghNear * 0.30f + : (bottomAgl > c.TargetHoverAltAgl && MathF.Abs(c.ThrottleCmd) > 0.1f ? ghCenter * 0.75f + ghNear * 0.25f : ghCenter); + float targetCenterY = localGround + spec.HalfHeight + c.TargetHoverAltAgl; + + float altErr = targetCenterY - pos.Y; + float hoverForceY = mass * (20f + altErr * 32f - vel.Y * 14f); + if (bottomAgl < 0.8f) + { + hoverForceY += mass * (0.8f - bottomAgl) * 65f; + } + if (bottomAgl < 0.2f) + { + hoverForceY += mass * (0.2f - bottomAgl) * 160f; + } + hoverForceY = Math.Clamp(hoverForceY, -mass * 38f, mass * 48f); + + float thrust = c.Grid.ComputeDriveThrust(); + float maxSpeed = MathF.Max(6f, spec.MaxSpeed); + Vector3 horizVel = new Vector3(vel.X, 0f, vel.Z); + float forwardSpeed = Vector3.Dot(horizVel, forward); + float lateralSpeed = Vector3.Dot(horizVel, right); + + Vector3 force = Vector3.UnitY * hoverForceY; + + // Steep-cliff deflection & flare airbrake: never fly nose-first through solid terrain walls + float vDotN = Vector3.Dot(vel, terrainNormal); + if (bottomAgl < 0.4f && vDotN < 0f) + { + float deflectMag = MathF.Min(-vDotN * mass * 24f, -vDotN * mass / FixedDt); + force += terrainNormal * deflectMag; + } + + Vector3 effectiveFwd = forward; + float slopeAhead = ghNear - ghCenter; + if (slopeAhead > 1.2f && bottomAgl < c.TargetHoverAltAgl + 1.2f) + { + float climbBrake = Math.Clamp(1f - (slopeAhead - 1.2f) / 3.5f, 0.15f, 1f); + effectiveFwd *= climbBrake; + force -= horizVel * (mass * 9f * (1f - climbBrake)); + } + + force += effectiveFwd * (c.ThrottleCmd * thrust) - forward * ((thrust / maxSpeed) * forwardSpeed); + force -= right * (lateralSpeed * mass * 3.8f); + if (c.BrakeCmd) + { + force -= horizVel * (mass * 3.5f); + } + + // Engine heat from hover lift + horizontal thrust + float liftWork = 0.25f + 0.55f * MathF.Abs(c.VerticalThrottleCmd); + float heatFraction = MathF.Max(MathF.Abs(c.ThrottleCmd), liftWork); + for (int m = 0; m < c.Grid.Mounted.Count; m++) + { + var mod = c.Grid.Mounted[m]; + if (mod.Spec.Kind == HardpointKind.Engine && mod.Spec.HeatOutput > 0f) + { + c.Grid.InjectHardpointHeat(mod.HardpointId, mod.Spec.HeatOutput * heatFraction * FixedDt); + } + } + + // Gentle bank-into-turn and subtle nose-down pitch into forward thrust + float speedScale = Math.Clamp(MathF.Abs(forwardSpeed) / MathF.Max(8f, maxSpeed * 0.5f), 0f, 1f); + float bankRad = -c.SteerCmd * speedScale * 0.26f; + float nosePitchRad = c.ThrottleCmd * 0.11f; + Vector3 desiredUp = Vector3.Normalize(Vector3.UnitY + right * MathF.Sin(bankRad) + forward * MathF.Sin(nosePitchRad)); + + Vector3 tiltAxis = Vector3.Cross(up, desiredUp); + float maxFoot = MathF.Max(spec.HalfWidth, spec.HalfLength); + float iTilt = MathF.Max(1f, (spec.HalfHeight * spec.HalfHeight + maxFoot * maxFoot) / 3f); + Vector3 torque = tiltAxis * (mass * 30f * iTilt) - new Vector3(angVel.X, 0f, angVel.Z) * (mass * 9.5f * iTilt); + + float turnRate = c.Grid.ComputeTurnRate(); + float targetYawVel = -c.SteerCmd * turnRate; + float hx = view.Size0[lane]; + float hz = view.Size2[lane]; + float iy = mass * (hx * hx + hz * hz) / 3f; + torque.Y = iy * (targetYawVel - angVel.Y) * 8f; + + view.ForceX[lane] = force.X; + view.ForceY[lane] = force.Y; + view.ForceZ[lane] = force.Z; + view.TorqueX[lane] = torque.X; + view.TorqueY[lane] = torque.Y; + view.TorqueZ[lane] = torque.Z; + } + private static Vector3 ComputeRightingTorque( ChassisSpec spec, Quaternion q, @@ -645,8 +771,8 @@ namespace RustToDust.Ymir Vector3 supportDir = Vector3.Normalize(Vector3.Lerp(Vector3.UnitY, terrainNormal, slopeBlend)); float orthoScale = MathF.Max(0.35f, terrainNormal.Y); - float hx = spec.HalfWidth * 0.9f; - float hz = spec.HalfLength * 0.9f; + float hx = MathF.Min(spec.HalfWidth * 0.9f, 2.4f); + float hz = MathF.Min(spec.HalfLength * 0.9f, 2.8f); float bottomY = -spec.HalfHeight; const float restTravel = 0.20f; float kSpring = mass * 85f; @@ -683,7 +809,9 @@ namespace RustToDust.Ymir } // Center skid-plate bump-stop prevents belly clipping when straddling sharp ridge crests at speed - float centerPen = (State.Terrain.SampleHeight(pos.X, pos.Z) + 0.08f) - (pos.Y - spec.HalfHeight); + float ghCenter = State.Terrain.SampleHeight(pos.X, pos.Z); + float centerGap = (pos.Y - spec.HalfHeight) - ghCenter; + float centerPen = (ghCenter + 0.08f) - (pos.Y - spec.HalfHeight); if (centerPen > 0f) { groundedCorners = Math.Max(groundedCorners, 1); @@ -694,6 +822,14 @@ namespace RustToDust.Ymir supportForce += supportDir * fCenter; } + // Prevent large overhangs from catapulting the chassis when the belly center is already well above ground + if (centerGap > 1.2f && supportForce.Y > 0f) + { + float overhangAtten = Math.Clamp(1f - (centerGap - 1.2f) / 1.8f, 0f, 1f); + supportForce *= overhangAtten; + supportTorque *= overhangAtten; + } + // Base lift attenuation on normal separation velocity vDotN rather than world vel.Y so steep uphill // climbs (vDotN <= 0) retain full support while separating crests (vDotN > 2.0) don't sky-launch. float vDotN = Vector3.Dot(vel, terrainNormal); @@ -954,31 +1090,49 @@ namespace RustToDust.Ymir for (int i = 0; i < State.Contenders.Count; i++) { var c = State.Contenders[i]; - if (!c.Alive || !c.FireCmd || !_contenderBodies.TryGetValue(c.Id, out var handle)) + if (!c.Alive || !_contenderBodies.TryGetValue(c.Id, out var handle)) { continue; } + int lane = handle.Lane; + var shooterQuat = new Quaternion( + view.RotationX[lane], + view.RotationY[lane], + view.RotationZ[lane], + view.RotationW[lane]); ulong shooterStableId = ContenderStableIdBase + (ulong)c.Id; - FireMountedWeapons( - shooterStableId, - shooterContender: c, - shooterLabel: c.Name, - isSpectatorShooter: false, - shooterGrid: c.Grid, - shooterPos: new Vector3(c.X, c.Y, c.Z), - shooterYaw: c.Yaw, - aimX: c.AimX, - aimY: c.AimY, - aimZ: c.AimZ, - extraSpread: 0f, - view: view); + + UpdateWeaponArcTelemetry(c, shooterQuat, shooterStableId); + + if (c.FireCmd) + { + FireMountedWeapons( + shooterStableId, + shooterContender: c, + shooterLabel: c.Name, + isSpectatorShooter: false, + shooterGrid: c.Grid, + shooterPos: new Vector3(c.X, c.Y, c.Z), + shooterYaw: c.Yaw, + shooterQuat: shooterQuat, + aimX: c.AimX, + aimY: c.AimY, + aimZ: c.AimZ, + extraSpread: 0f, + view: view); + } + + if (!c.Grid.Spec.IsVtol) + { + TryAutoPointDefenseFire(c, shooterStableId, shooterQuat, view); + } } for (int i = 0; i < State.Spectators.Spectators.Count; i++) { var s = State.Spectators.Spectators[i]; - if (!s.Alive || s.Stance == SpectatorStance.Neutral || !_spectatorBodies.TryGetValue(s.Id, out _)) + if (!s.Alive || s.Stance == SpectatorStance.Neutral || !_spectatorBodies.TryGetValue(s.Id, out var specHandle)) { continue; } @@ -989,6 +1143,12 @@ namespace RustToDust.Ymir continue; } + int lane = specHandle.Lane; + var specQuat = new Quaternion( + view.RotationX[lane], + view.RotationY[lane], + view.RotationZ[lane], + view.RotationW[lane]); ulong shooterStableId = SpectatorStableIdBase + (ulong)s.Id; FireMountedWeapons( shooterStableId, @@ -998,6 +1158,7 @@ namespace RustToDust.Ymir shooterGrid: s.Grid, shooterPos: new Vector3(s.X, s.Y, s.Z), shooterYaw: s.Yaw, + shooterQuat: specQuat, aimX: aimX, aimY: float.NaN, aimZ: aimZ, @@ -1006,6 +1167,148 @@ namespace RustToDust.Ymir } } + private void UpdateWeaponArcTelemetry(ContenderState c, Quaternion shooterQuat, ulong shooterStableId) + { + float targetY = !float.IsNaN(c.AimY) ? c.AimY : ResolveAimTargetY(c.AimX, c.AimZ, shooterStableId); + var shooterPos = new Vector3(c.X, c.Y, c.Z); + int inArc = 0; + int outArc = 0; + + for (int m = 0; m < c.Grid.Mounted.Count; m++) + { + var mod = c.Grid.Mounted[m]; + if (mod.Spec.Kind != HardpointKind.Weapon) + { + continue; + } + + var hp = c.Grid.FindHardpoint(mod.HardpointId); + if (hp is null) + { + continue; + } + + Vector3 muzzle = ComputeWeaponMuzzle(shooterPos, c.Yaw, c.Grid.Spec, c.AimX, c.AimZ); + Vector3 aimDelta = new Vector3(c.AimX - muzzle.X, targetY - muzzle.Y, c.AimZ - muzzle.Z); + float pitchDeg = MatchRules.ComputeLocalPitchDeg(aimDelta, shooterQuat); + var (minPitch, maxPitch) = MatchRules.GetEffectivePitchArc(hp, mod.Spec); + if (pitchDeg >= minPitch && pitchDeg <= maxPitch) + { + inArc++; + } + else + { + outArc++; + } + } + + c.InArcWeaponCount = inArc; + c.OutOfArcWeaponCount = outArc; + c.AllWeaponsOutOfArc = outArc > 0 && inArc == 0; + } + + private void TryAutoPointDefenseFire( + ContenderState shooter, + ulong shooterStableId, + Quaternion shooterQuat, + Box3DSoaView view) + { + const float maxPdTrackRange = 140f; + var shooterPos = new Vector3(shooter.X, shooter.Y, shooter.Z); + + for (int m = 0; m < shooter.Grid.Mounted.Count; m++) + { + var mod = shooter.Grid.Mounted[m]; + if (mod.Spec.Kind != HardpointKind.Weapon || + !mod.Spec.IsWideArc || + mod.Cooldown > 0f || + mod.CurrentPerformance <= 0.05f) + { + continue; + } + + // Avoid self-cooking on auto-PD if close to thermal ceiling + if (mod.CurrentTemp > mod.Spec.OptimalTemp + mod.Spec.PlateauWidth * 0.45f) + { + continue; + } + + var hp = shooter.Grid.FindHardpoint(mod.HardpointId); + if (hp is null) + { + continue; + } + + var (minPitch, maxPitch) = MatchRules.GetEffectivePitchArc(hp, mod.Spec); + float range = MathF.Min(maxPdTrackRange, MathF.Max(5f, mod.Spec.WeaponRange)); + float rangeSq = range * range; + + ContenderState? bestVtol = null; + float bestDistSq = rangeSq; + + for (int j = 0; j < State.Contenders.Count; j++) + { + var other = State.Contenders[j]; + if (other.Id == shooter.Id || !other.Alive || !other.Grid.Spec.IsVtol) + { + continue; + } + + float groundH = State.Terrain.SampleHeight(other.X, other.Z); + float agl = (other.Y - other.Grid.Spec.HalfHeight) - groundH; + if (agl <= 2.0f) + { + continue; + } + + float targetY = MatchRules.GetTargetAimY(other.Y, other.Grid.Spec.HalfHeight); + Vector3 muzzle = ComputeWeaponMuzzle(shooterPos, shooter.Yaw, shooter.Grid.Spec, other.X, other.Z); + Vector3 delta = new Vector3(other.X - muzzle.X, targetY - muzzle.Y, other.Z - muzzle.Z); + float d2 = delta.LengthSquared(); + if (d2 > bestDistSq) + { + continue; + } + + float pitchDeg = MatchRules.ComputeLocalPitchDeg(delta, shooterQuat); + if (pitchDeg < minPitch || pitchDeg > maxPitch) + { + continue; + } + + if (!HasLineOfSight(muzzle.X, muzzle.Y, muzzle.Z, other.X, targetY, other.Z)) + { + continue; + } + + bestDistSq = d2; + bestVtol = other; + } + + if (bestVtol is null) + { + continue; + } + + float aimY = MatchRules.GetTargetAimY(bestVtol.Y, bestVtol.Grid.Spec.HalfHeight); + FireSingleWeapon( + mod, + shooterStableId, + shooter, + shooter.Name, + isSpectatorShooter: false, + shooter.Grid, + shooterPos, + shooter.Yaw, + shooterQuat, + bestVtol.X, + aimY, + bestVtol.Z, + extraSpread: 0f, + view: view); + } + } + private void FireMountedWeapons( ulong shooterStableId, ContenderState? shooterContender, @@ -1014,6 +1317,7 @@ namespace RustToDust.Ymir ChassisGrid shooterGrid, Vector3 shooterPos, float shooterYaw, + Quaternion shooterQuat, float aimX, float aimY, float aimZ, @@ -1030,106 +1334,157 @@ namespace RustToDust.Ymir continue; } - Vector3 horizDir = new Vector3(aimX - shooterPos.X, 0f, aimZ - shooterPos.Z); - if (horizDir.LengthSquared() < 1e-3f) + FireSingleWeapon( + mod, + shooterStableId, + shooterContender, + shooterLabel, + isSpectatorShooter, + shooterGrid, + shooterPos, + shooterYaw, + shooterQuat, + aimX, + targetY, + aimZ, + extraSpread, + view); + } + } + + private static Vector3 ComputeWeaponMuzzle( + Vector3 shooterPos, + float shooterYaw, + ChassisSpec spec, + float aimX, + float aimZ) + { + Vector3 horizDir = new Vector3(aimX - shooterPos.X, 0f, aimZ - shooterPos.Z); + if (horizDir.LengthSquared() < 1e-3f) + { + horizDir = Vector3.UnitZ; + } + else + { + horizDir = Vector3.Normalize(horizDir); + } + + float cosYaw = MathF.Cos(shooterYaw); + float sinYaw = MathF.Sin(shooterYaw); + float localDirX = horizDir.X * cosYaw - horizDir.Z * sinYaw; + float localDirZ = horizDir.X * sinYaw + horizDir.Z * cosYaw; + float clearance = + MathF.Abs(localDirX) * spec.HalfWidth + + MathF.Abs(localDirZ) * spec.HalfLength + + 0.35f; + + return new Vector3( + shooterPos.X + horizDir.X * clearance, + shooterPos.Y + spec.HalfHeight * 0.25f, + shooterPos.Z + horizDir.Z * clearance); + } + + private void FireSingleWeapon( + MountedModule mod, + ulong shooterStableId, + ContenderState? shooterContender, + string shooterLabel, + bool isSpectatorShooter, + ChassisGrid shooterGrid, + Vector3 shooterPos, + float shooterYaw, + Quaternion shooterQuat, + float aimX, + float targetY, + float aimZ, + float extraSpread, + Box3DSoaView view) + { + var hp = shooterGrid.FindHardpoint(mod.HardpointId); + Vector3 muzzle = ComputeWeaponMuzzle(shooterPos, shooterYaw, shooterGrid.Spec, aimX, aimZ); + Vector3 aimDelta = new Vector3(aimX - muzzle.X, targetY - muzzle.Y, aimZ - muzzle.Z); + if (hp is not null) + { + float pitchDeg = MatchRules.ComputeLocalPitchDeg(aimDelta, shooterQuat); + var (minPitch, maxPitch) = MatchRules.GetEffectivePitchArc(hp, mod.Spec); + if (pitchDeg < minPitch || pitchDeg > maxPitch) { - horizDir = Vector3.UnitZ; + return; + } + } + + Vector3 baseAim = aimDelta.LengthSquared() < 1e-4f + ? Vector3.UnitZ + : Vector3.Normalize(aimDelta); + + int burst = Math.Max(1, mod.Spec.BurstCount); + float totalSpread = mod.Spec.SpreadRad + extraSpread; + float range = MathF.Max(5f, mod.Spec.WeaponRange); + uint tracerColor = mod.Spec.IsImperial + ? 0xFFD966FFu + : (mod.Spec.IsWideArc ? 0x66E6FFFFu : 0xFF8833FFu); + + for (int b = 0; b < burst; b++) + { + float yawJitter = ((float)State.Rng.NextDouble() * 2f - 1f) * totalSpread; + float pitchJitter = ((float)State.Rng.NextDouble() * 2f - 1f) * (totalSpread * 0.35f); + var dir = ApplyAimSpread(baseAim, yawJitter, pitchJitter); + + var hit = _world.CastRayClosest( + (muzzle.X, muzzle.Y, muzzle.Z), + (dir.X * range, dir.Y * range, dir.Z * range)); + + float boxDist = hit.Hit + ? Vector3.Distance(muzzle, new Vector3(hit.Point.X, hit.Point.Y, hit.Point.Z)) + : range; + + bool hitTerrain = State.Terrain.Raycast( + muzzle.X, muzzle.Y, muzzle.Z, + dir.X, dir.Y, dir.Z, + boxDist, + out float tx, out float ty, out float tz, out float terrainDist); + + Vector3 endPt; + if (hitTerrain && terrainDist < boxDist) + { + endPt = new Vector3(tx, ty, tz); + StampTerrainWeaponImpact(mod.Spec.Id, tx, tz, burstIndex: b); } else { - horizDir = Vector3.Normalize(horizDir); - } + endPt = hit.Hit + ? new Vector3(hit.Point.X, hit.Point.Y, hit.Point.Z) + : muzzle + dir * range; - float cosYaw = MathF.Cos(shooterYaw); - float sinYaw = MathF.Sin(shooterYaw); - float localDirX = horizDir.X * cosYaw - horizDir.Z * sinYaw; - float localDirZ = horizDir.X * sinYaw + horizDir.Z * cosYaw; - float clearance = - MathF.Abs(localDirX) * shooterGrid.Spec.HalfWidth + - MathF.Abs(localDirZ) * shooterGrid.Spec.HalfLength + - 0.35f; - Vector3 muzzle = new Vector3( - shooterPos.X + horizDir.X * clearance, - shooterPos.Y + shooterGrid.Spec.HalfHeight * 0.25f, - shooterPos.Z + horizDir.Z * clearance); - - Vector3 baseAim = new Vector3(aimX - muzzle.X, targetY - muzzle.Y, aimZ - muzzle.Z); - if (baseAim.LengthSquared() < 1e-4f) - { - baseAim = horizDir; - } - else - { - baseAim = Vector3.Normalize(baseAim); - } - - int burst = Math.Max(1, mod.Spec.BurstCount); - float totalSpread = mod.Spec.SpreadRad + extraSpread; - float range = MathF.Max(5f, mod.Spec.WeaponRange); - uint tracerColor = mod.Spec.IsImperial ? 0xFFD966FFu : 0xFF8833FFu; - - for (int b = 0; b < burst; b++) - { - float yawJitter = ((float)State.Rng.NextDouble() * 2f - 1f) * totalSpread; - float pitchJitter = ((float)State.Rng.NextDouble() * 2f - 1f) * (totalSpread * 0.35f); - var dir = ApplyAimSpread(baseAim, yawJitter, pitchJitter); - - var hit = _world.CastRayClosest( - (muzzle.X, muzzle.Y, muzzle.Z), - (dir.X * range, dir.Y * range, dir.Z * range)); - - float boxDist = hit.Hit - ? Vector3.Distance(muzzle, new Vector3(hit.Point.X, hit.Point.Y, hit.Point.Z)) - : range; - - bool hitTerrain = State.Terrain.Raycast( - muzzle.X, muzzle.Y, muzzle.Z, - dir.X, dir.Y, dir.Z, - boxDist, - out float tx, out float ty, out float tz, out float terrainDist); - - Vector3 endPt; - if (hitTerrain && terrainDist < boxDist) + if (hit.Hit && hit.StableId != shooterStableId && _entityByStableId.TryGetValue(hit.StableId, out var targetRef)) { - endPt = new Vector3(tx, ty, tz); - StampTerrainWeaponImpact(mod.Spec.Id, tx, tz, burstIndex: b); + ApplyHitToTarget( + targetRef, + hit.Point, + mod.Spec.WeaponDamage * mod.CurrentPerformance, + mod.Spec.HeatOutput * 0.25f, + shooterContender, + shooterLabel, + isSpectatorShooter, + view); } - else - { - endPt = hit.Hit - ? new Vector3(hit.Point.X, hit.Point.Y, hit.Point.Z) - : muzzle + dir * range; - - if (hit.Hit && hit.StableId != shooterStableId && _entityByStableId.TryGetValue(hit.StableId, out var targetRef)) - { - ApplyHitToTarget( - targetRef, - hit.Point, - mod.Spec.WeaponDamage * mod.CurrentPerformance, - mod.Spec.HeatOutput * 0.25f, - shooterContender, - shooterLabel, - isSpectatorShooter, - view); - } - } - - State.Tracers.Add(new TracerEvent( - muzzle.X, muzzle.Y, muzzle.Z, - endPt.X, endPt.Y, endPt.Z, - tracerColor, - 0.09f)); } - shooterGrid.InjectHardpointHeat(mod.HardpointId, mod.Spec.HeatOutput); - mod.Cooldown = mod.Spec.FireCooldown; + State.Tracers.Add(new TracerEvent( + muzzle.X, muzzle.Y, muzzle.Z, + endPt.X, endPt.Y, endPt.Z, + tracerColor, + 0.09f)); + } - if (shooterContender is not null) - { - State.LastGunfireX = shooterPos.X; - State.LastGunfireZ = shooterPos.Z; - State.LastGunfireTime = State.Time; - } + shooterGrid.InjectHardpointHeat(mod.HardpointId, mod.Spec.HeatOutput); + mod.Cooldown = mod.Spec.FireCooldown; + + if (shooterContender is not null) + { + State.LastGunfireX = shooterPos.X; + State.LastGunfireZ = shooterPos.Z; + State.LastGunfireTime = State.Time; } }