From b36ba93fc7badec1869a3e34c9791cf5ef321bdf Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Tue, 30 Jul 2024 16:34:18 +0100 Subject: [PATCH] casing eject delay --- Patches/Core/ThingDefs_Misc/Weapons_Guns.xml | 8 +++++ .../CombatExtended/CE_Utility.cs | 15 +++++++--- .../CombatExtended/Comps/CompAmmoUser.cs | 29 +++++++++---------- .../CombatExtended/Defs/GunDrawExtension.cs | 1 + .../CombatExtended/Jobs/JobDriver_Reload.cs | 7 +++++ .../CombatExtended/Verbs/Verb_ShootCE.cs | 3 +- 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Patches/Core/ThingDefs_Misc/Weapons_Guns.xml b/Patches/Core/ThingDefs_Misc/Weapons_Guns.xml index ab421429e9..069bc5b995 100644 --- a/Patches/Core/ThingDefs_Misc/Weapons_Guns.xml +++ b/Patches/Core/ThingDefs_Misc/Weapons_Guns.xml @@ -168,6 +168,14 @@
  • 1.00,1.00 0.0,0.0 + true + true + 20 + + -0.1,0.05 + 1.5~2 + -135 + 2
  • diff --git a/Source/CombatExtended/CombatExtended/CE_Utility.cs b/Source/CombatExtended/CombatExtended/CE_Utility.cs index 2c319e90b7..3efef9e1b8 100644 --- a/Source/CombatExtended/CombatExtended/CE_Utility.cs +++ b/Source/CombatExtended/CombatExtended/CE_Utility.cs @@ -845,13 +845,13 @@ public static void Recoil(ThingDef weaponDef, Verb shootVerb, out Vector3 drawOf #endregion Misc #region MoteThrower - public static void GenerateAmmoCasings(ProjectilePropertiesCE projProps, Vector3 drawPosition, Map map, float shotRotation = -180f, float recoilAmount = 2f, bool fromPawn = false, GunDrawExtension extension = null) + public static void GenerateAmmoCasings(ProjectilePropertiesCE projProps, Vector3 drawPosition, Map map, float shotRotation = -180f, float recoilAmount = 2f, bool fromPawn = false, GunDrawExtension extension = null, int randSeedOverride = -1) { if (projProps.dropsCasings) { if (Controller.settings.ShowCasings) { - ThrowEmptyCasing(drawPosition, map, DefDatabase.GetNamed(projProps.casingMoteDefname), recoilAmount, shotRotation, 1f, fromPawn, extension); + ThrowEmptyCasing(drawPosition, map, DefDatabase.GetNamed(projProps.casingMoteDefname), recoilAmount, shotRotation, 1f, fromPawn, extension, randSeedOverride); } if (Controller.settings.CreateCasingsFilth) { @@ -882,7 +882,7 @@ static Vector3 RandomOriginOffset(Vector2 randRange) } - public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDef, float recoilAmount, float shotRotation, float size = 1f, bool fromPawn = false, GunDrawExtension extension = null) + public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDef, float recoilAmount, float shotRotation, float size = 1f, bool fromPawn = false, GunDrawExtension extension = null, int randSeedOverride = -1) { if (!loc.ShouldSpawnMotesAt(map) || map.moteCounter.SaturatedLowPriority) { @@ -892,7 +892,14 @@ public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDe { recoilAmount = 1; //avoid division errors in case of guns without recoil } - Rand.PushState(); + if (randSeedOverride > 0) + { + Rand.PushState(randSeedOverride); + } + else + { + Rand.PushState(); + } FleckCreationData creationData = FleckMaker.GetDataStatic(loc, map, casingFleckDef); creationData.velocitySpeed = Rand.Range(1.5f, 2f) * recoilAmount; creationData.airTimeLeft = Rand.Range(1f, 1.5f) / creationData.velocitySpeed; diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs index 36f4ca65cf..154a0445ba 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs @@ -21,8 +21,6 @@ public class CompAmmoUser : CompRangedGizmoGiver private AmmoDef currentAmmoInt = null; private AmmoDef selectedAmmo; - public int shotsFiredBeforeReload = 0; - private Thing ammoToBeDeleted; public Building_Turret turret; // Cross-linked from CE turret @@ -128,6 +126,8 @@ public Pawn Wielder } } public bool IsEquippedGun => Wielder != null; + + GunDrawExtension gunDrawExt => parent.def.GetModExtension(); public Pawn Holder { get @@ -269,8 +269,6 @@ private Map Map } public bool ShouldThrowMote => Props.throwMote && MagSize > 1; - GunDrawExtension gunDrawExt => parent.def.GetModExtension(); - public AmmoDef SelectedAmmo { get @@ -418,7 +416,6 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1) // Original: curMagCountInt--; - shotsFiredBeforeReload += ammoConsumedPerShot; if (curMagCountInt < 0) { TryStartReload(); @@ -489,16 +486,6 @@ public void TryStartReload() if (UseAmmo) { TryUnload(); - //For revolvers and - if (gunDrawExt != null && gunDrawExt.DropCasingWhenReload && CompEquippable?.PrimaryVerb is Verb_ShootCE verbCE && verbCE.VerbPropsCE.ejectsCasings) - { - Log.Message(shotsFiredBeforeReload.ToString()); - for (int i = 0; i < Props.magazineSize; i++) - { - verbCE.ExternalCallDropCasing(i); - } - } - shotsFiredBeforeReload = 0; // Check for ammo if (IsEquippedGun && !HasAmmo) @@ -528,6 +515,18 @@ public void TryStartReload() } } + public void DropCasing(int count) + { + //For revolvers and break actions + if (gunDrawExt != null && gunDrawExt.DropCasingWhenReload && CompEquippable?.PrimaryVerb is Verb_ShootCE verbCE && verbCE.VerbPropsCE.ejectsCasings) + { + for (int i = 0; i < count; i++) + { + verbCE.ExternalCallDropCasing(i); + } + } + } + // used by both turrets (JobDriver_ReloadTurret) and pawns (JobDriver_Reload). /// /// Used to unload the weapon. Ammo will be dumped to the unloading Pawn's inventory or the ground if insufficient space. Any ammo that can't be dropped diff --git a/Source/CombatExtended/CombatExtended/Defs/GunDrawExtension.cs b/Source/CombatExtended/CombatExtended/Defs/GunDrawExtension.cs index 7733ccc97f..822fc76ecd 100644 --- a/Source/CombatExtended/CombatExtended/Defs/GunDrawExtension.cs +++ b/Source/CombatExtended/CombatExtended/Defs/GunDrawExtension.cs @@ -18,6 +18,7 @@ public class GunDrawExtension : DefModExtension public Vector2 CasingOffset = Vector2.zero; public float CasingAngleOffset = 0; + public bool DropCasingWhenReload; public bool AdvancedCasingVariables = false; diff --git a/Source/CombatExtended/CombatExtended/Jobs/JobDriver_Reload.cs b/Source/CombatExtended/CombatExtended/Jobs/JobDriver_Reload.cs index 3eba09c422..62120541ea 100644 --- a/Source/CombatExtended/CombatExtended/Jobs/JobDriver_Reload.cs +++ b/Source/CombatExtended/CombatExtended/Jobs/JobDriver_Reload.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Mono.Unix.Native; using RimWorld; using UnityEngine; using Verse; @@ -186,6 +187,12 @@ public override IEnumerable MakeNewToils() waitToil.initAction = () => waitToil.actor.pather.StopDead(); waitToil.defaultCompleteMode = ToilCompleteMode.Delay; waitToil.defaultDuration = Mathf.CeilToInt(weapon.GetStatValue(CE_StatDefOf.ReloadTime).SecondsToTicks() / pawn.GetStatValue(CE_StatDefOf.ReloadSpeed)); + //If we're some way through the reload timer, drop casings if dropcasingwhenreload. + waitToil.AddPreTickAction(() => + { + if (waitToil.actor.jobs.curDriver.ticksLeftThisToil == (int)(waitToil.defaultDuration * 0.8f)) + { compReloader.DropCasing(compReloader.Props.magazineSize); } + }); yield return waitToil.WithProgressBarToilDelay(indReloader); //Actual reloader diff --git a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs index a425103ca4..b4f8a1a443 100644 --- a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs +++ b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs @@ -373,7 +373,8 @@ public void ExternalCallDropCasing(int randomSeedOffset = -1) { fromPawn = drawPos != Vector3.zero; } - CE_Utility.GenerateAmmoCasings(projectilePropsCE, fromPawn ? drawPos : caster.DrawPos + CasingOffsetRotated(ext), caster.Map, AimAngle, VerbPropsCE.recoilAmount, fromPawn: fromPawn, casingAngleOffset: EquipmentSource?.def.GetModExtension()?.CasingAngleOffset ?? 0, randomSeedOffset); + //No aim angle because casing eject happens when pawn lowers its gun to reload + CE_Utility.GenerateAmmoCasings(projectilePropsCE, fromPawn ? drawPos : caster.DrawPos, caster.Map, 0, VerbPropsCE.recoilAmount, fromPawn: fromPawn, extension: ext, randomSeedOffset); } public override bool TryCastShot()