From b0c90bd6983a22f9dab9553bf201ec440670dbc5 Mon Sep 17 00:00:00 2001 From: Keshash <54061981+Keshash@users.noreply.github.com> Date: Sun, 10 Sep 2023 15:14:48 +0300 Subject: [PATCH 1/5] Add direction and force of recoil to casing flecks --- .../CombatExtended/CE_Utility.cs | 18 ++++++++++++------ .../CombatExtended/Verbs/Verb_ShootCE.cs | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Source/CombatExtended/CombatExtended/CE_Utility.cs b/Source/CombatExtended/CombatExtended/CE_Utility.cs index fe706eec9f..a9a85cf9eb 100644 --- a/Source/CombatExtended/CombatExtended/CE_Utility.cs +++ b/Source/CombatExtended/CombatExtended/CE_Utility.cs @@ -697,22 +697,28 @@ public static Thing GetWeaponFromLauncher(Thing launcher) #endregion Misc #region MoteThrower - public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDef, float size = 1f) + public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDef, float recoilAmount, float shotRotation, float size = 1f) { if (!Controller.settings.ShowCasings || !loc.ShouldSpawnMotesAt(map) || map.moteCounter.SaturatedLowPriority) { return; } - + if (recoilAmount <= 0) + { + recoilAmount = 1; //avoid division errors in case of guns without recoil + } Rand.PushState(); FleckCreationData creationData = FleckMaker.GetDataStatic(loc, map, casingFleckDef); creationData.airTimeLeft = 1.5f; creationData.scale = Rand.Range(0.5f, 0.3f) * size; - creationData.rotation = Rand.Range(-3f, 4f); creationData.spawnPosition = loc; - creationData.velocitySpeed = Rand.Range(0.7f, 0.5f); - creationData.velocityAngle = Rand.Range(160, 200); - creationData.rotationRate = (float)Rand.Range(-300, 300); + creationData.velocitySpeed = Rand.Range(0.6f, 0.4f) * recoilAmount; + int randomAngle = Rand.Range(-20, 20); + //shotRotation goes from -270 to +90, while fleck angle uses 0 to 360 degrees (0 deg being North for both cases), so a conversion is used + //+90 makes casings fly to gun's right side + creationData.velocityAngle = shotRotation>0? 360 - shotRotation +90 + randomAngle : 0 - shotRotation + 90 + randomAngle; + creationData.rotation = creationData.velocityAngle + Rand.Range(-3f, 4f); + creationData.rotationRate = (float)Rand.Range(-150, 150) / recoilAmount; map.flecks.CreateFleck(creationData); Rand.PopState(); } diff --git a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs index 6026433989..b905c96bec 100644 --- a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs +++ b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs @@ -358,7 +358,7 @@ public override bool TryCastShot() //Drop casings if (VerbPropsCE.ejectsCasings && projectilePropsCE.dropsCasings) { - CE_Utility.ThrowEmptyCasing(caster.DrawPos, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname)); + CE_Utility.ThrowEmptyCasing(caster.DrawPos, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname), VerbPropsCE.recoilAmount, shotRotation); CE_Utility.MakeCasingFilth(caster.Position, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingFilthDefname)); } // This needs to here for weapons without magazine to ensure their last shot plays sounds From 03848ee0588196ab1636195732f4d69a9adb100e Mon Sep 17 00:00:00 2001 From: Keshash <54061981+Keshash@users.noreply.github.com> Date: Sun, 10 Sep 2023 16:19:14 +0300 Subject: [PATCH 2/5] code style --- Source/CombatExtended/CombatExtended/CE_Utility.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/CombatExtended/CombatExtended/CE_Utility.cs b/Source/CombatExtended/CombatExtended/CE_Utility.cs index 995fcc6de8..5078dd0b15 100644 --- a/Source/CombatExtended/CombatExtended/CE_Utility.cs +++ b/Source/CombatExtended/CombatExtended/CE_Utility.cs @@ -755,7 +755,7 @@ public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDe int randomAngle = Rand.Range(-20, 20); //shotRotation goes from -270 to +90, while fleck angle uses 0 to 360 degrees (0 deg being North for both cases), so a conversion is used //+90 makes casings fly to gun's right side - creationData.velocityAngle = shotRotation>0? 360 - shotRotation +90 + randomAngle : 0 - shotRotation + 90 + randomAngle; + creationData.velocityAngle = shotRotation > 0 ? 360 - shotRotation + 90 + randomAngle : 0 - shotRotation + 90 + randomAngle; creationData.rotation = creationData.velocityAngle + Rand.Range(-3f, 4f); creationData.rotationRate = (float)Rand.Range(-150, 150) / recoilAmount; map.flecks.CreateFleck(creationData); From b33e5fb09d0ac91f98abfd1ba949c2749f50d3d0 Mon Sep 17 00:00:00 2001 From: Keshash <54061981+Keshash@users.noreply.github.com> Date: Sun, 10 Sep 2023 16:21:53 +0300 Subject: [PATCH 3/5] Update Verb_ShootCE.cs --- Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs index 8b6331a2cd..7b52ad67f6 100644 --- a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs +++ b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs @@ -364,7 +364,7 @@ protected virtual bool OnCastSuccessful() //Drop casings if (VerbPropsCE.ejectsCasings && projectilePropsCE.dropsCasings) { - CE_Utility.ThrowEmptyCasing(caster.DrawPos, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname)); + CE_Utility.ThrowEmptyCasing(caster.DrawPos, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname), VerbPropsCE.recoilAmount, shotRotation); CE_Utility.MakeCasingFilth(caster.Position, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingFilthDefname)); } // This needs to here for weapons without magazine to ensure their last shot plays sounds From 37d7348b7b6a26e9eb13a2de1745df5de936a7b8 Mon Sep 17 00:00:00 2001 From: Keshash <54061981+Keshash@users.noreply.github.com> Date: Fri, 22 Sep 2023 12:07:34 +0300 Subject: [PATCH 4/5] Add casing throwing to vehicle turrets --- Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs b/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs index 97f4808a4b..bcf685ab61 100644 --- a/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs +++ b/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs @@ -98,6 +98,14 @@ public static object LaunchProjectileCE(ThingDef projectileDef, } var p = ThingMaker.MakeThing(projectileDef, null); ProjectileCE projectile = (ProjectileCE)p; + + var projectilePropsCE = projectileDef.projectile as ProjectilePropertiesCE; + if (projectilePropsCE.dropsCasings) + { + CE_Utility.ThrowEmptyCasing(vehicle.DrawPos, vehicle.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname), 3f, shotRotation); + CE_Utility.MakeCasingFilth(vehicle.Position, vehicle.Map, DefDatabase.GetNamed(projectilePropsCE.casingFilthDefname)); + } + GenSpawn.Spawn(projectile, vehicle.Position, vehicle.Map); projectile.ExactPosition = origin; projectile.canTargetSelf = false; From a600933edc74a58f3ed551e68ece37bed574b843 Mon Sep 17 00:00:00 2001 From: Keshash <54061981+Keshash@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:00:15 +0300 Subject: [PATCH 5/5] Prepare for VF-side integration --- .../CombatExtended/CE_Utility.cs | 22 ++++++++++++++----- .../CombatExtended/Verbs/Verb_ShootCE.cs | 5 ++--- .../VehiclesCompat/VehiclesCompat.cs | 8 ------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Source/CombatExtended/CombatExtended/CE_Utility.cs b/Source/CombatExtended/CombatExtended/CE_Utility.cs index 5078dd0b15..25429f39d5 100644 --- a/Source/CombatExtended/CombatExtended/CE_Utility.cs +++ b/Source/CombatExtended/CombatExtended/CE_Utility.cs @@ -736,9 +736,25 @@ public static Thing GetWeaponFromLauncher(Thing launcher) #endregion Misc #region MoteThrower + public static void GenerateAmmoCasings(ProjectilePropertiesCE projProps, Vector3 drawPosition, Map map, float shotRotation = -180f, float recoilAmount = 2f) + { + if (projProps.dropsCasings) + { + if (Controller.settings.ShowCasings) + { + ThrowEmptyCasing(drawPosition, map, DefDatabase.GetNamed(projProps.casingMoteDefname), recoilAmount, shotRotation); + } + if (Controller.settings.CreateCasingsFilth) + { + MakeCasingFilth(drawPosition.ToIntVec3(), map, DefDatabase.GetNamed(projProps.casingFilthDefname)); + } + } + } + + public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDef, float recoilAmount, float shotRotation, float size = 1f) { - if (!Controller.settings.ShowCasings || !loc.ShouldSpawnMotesAt(map) || map.moteCounter.SaturatedLowPriority) + if (!loc.ShouldSpawnMotesAt(map) || map.moteCounter.SaturatedLowPriority) { return; } @@ -764,10 +780,6 @@ public static void ThrowEmptyCasing(Vector3 loc, Map map, FleckDef casingFleckDe public static void MakeCasingFilth(IntVec3 position, Map map, ThingDef casingFilthDef) { - if (!Controller.settings.CreateCasingsFilth) - { - return; - } Rand.PushState(); float makeFilthChance = Rand.Range(0f, 1f); if (makeFilthChance > 0.9f && position.Walkable(map)) diff --git a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs index 7b52ad67f6..c494e6dd0f 100644 --- a/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs +++ b/Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs @@ -362,10 +362,9 @@ protected virtual bool OnCastSuccessful() ShooterPawn.records.Increment(RecordDefOf.ShotsFired); } //Drop casings - if (VerbPropsCE.ejectsCasings && projectilePropsCE.dropsCasings) + if (VerbPropsCE.ejectsCasings) { - CE_Utility.ThrowEmptyCasing(caster.DrawPos, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname), VerbPropsCE.recoilAmount, shotRotation); - CE_Utility.MakeCasingFilth(caster.Position, caster.Map, DefDatabase.GetNamed(projectilePropsCE.casingFilthDefname)); + CE_Utility.GenerateAmmoCasings(projectilePropsCE, caster.DrawPos, caster.Map, shotRotation, VerbPropsCE.recoilAmount); } // This needs to here for weapons without magazine to ensure their last shot plays sounds if (CompAmmo != null && !CompAmmo.HasMagazine && CompAmmo.UseAmmo) diff --git a/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs b/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs index bcf685ab61..97f4808a4b 100644 --- a/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs +++ b/Source/VehiclesCompat/VehiclesCompat/VehiclesCompat.cs @@ -98,14 +98,6 @@ public static object LaunchProjectileCE(ThingDef projectileDef, } var p = ThingMaker.MakeThing(projectileDef, null); ProjectileCE projectile = (ProjectileCE)p; - - var projectilePropsCE = projectileDef.projectile as ProjectilePropertiesCE; - if (projectilePropsCE.dropsCasings) - { - CE_Utility.ThrowEmptyCasing(vehicle.DrawPos, vehicle.Map, DefDatabase.GetNamed(projectilePropsCE.casingMoteDefname), 3f, shotRotation); - CE_Utility.MakeCasingFilth(vehicle.Position, vehicle.Map, DefDatabase.GetNamed(projectilePropsCE.casingFilthDefname)); - } - GenSpawn.Spawn(projectile, vehicle.Position, vehicle.Map); projectile.ExactPosition = origin; projectile.canTargetSelf = false;