Skip to content

Commit

Permalink
casing eject delay
Browse files Browse the repository at this point in the history
  • Loading branch information
CMDR-Bill-Doors committed Jul 30, 2024
1 parent 848d3c2 commit b36ba93
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
8 changes: 8 additions & 0 deletions Patches/Core/ThingDefs_Misc/Weapons_Guns.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@
<li Class="CombatExtended.GunDrawExtension">
<DrawSize>1.00,1.00</DrawSize>
<DrawOffset>0.0,0.0</DrawOffset>
<DropCasingWhenReload>true</DropCasingWhenReload>
<AdvancedCasingVariables>true</AdvancedCasingVariables>
<CasingRotationRandomRange>20</CasingRotationRandomRange>
<!--You'll want a lower casing speed and longer casing life time for this case-->
<CasingOffset>-0.1,0.05</CasingOffset>
<CasingSpeedOverrideRange>1.5~2</CasingSpeedOverrideRange>
<CasingAngleOffset>-135</CasingAngleOffset>
<CasingLifeTimeMultiplier>2</CasingLifeTimeMultiplier>
</li>
</value>
</Operation>
Expand Down
15 changes: 11 additions & 4 deletions Source/CombatExtended/CombatExtended/CE_Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FleckDef>.GetNamed(projProps.casingMoteDefname), recoilAmount, shotRotation, 1f, fromPawn, extension);
ThrowEmptyCasing(drawPosition, map, DefDatabase<FleckDef>.GetNamed(projProps.casingMoteDefname), recoilAmount, shotRotation, 1f, fromPawn, extension, randSeedOverride);
}
if (Controller.settings.CreateCasingsFilth)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
29 changes: 14 additions & 15 deletions Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,6 +126,8 @@ public Pawn Wielder
}
}
public bool IsEquippedGun => Wielder != null;

GunDrawExtension gunDrawExt => parent.def.GetModExtension<GunDrawExtension>();
public Pawn Holder
{
get
Expand Down Expand Up @@ -269,8 +269,6 @@ private Map Map
}
public bool ShouldThrowMote => Props.throwMote && MagSize > 1;

GunDrawExtension gunDrawExt => parent.def.GetModExtension<GunDrawExtension>();

public AmmoDef SelectedAmmo
{
get
Expand Down Expand Up @@ -418,7 +416,6 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1)


// Original: curMagCountInt--;
shotsFiredBeforeReload += ammoConsumedPerShot;
if (curMagCountInt < 0)
{
TryStartReload();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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).
/// <summary>
/// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class GunDrawExtension : DefModExtension

public Vector2 CasingOffset = Vector2.zero;
public float CasingAngleOffset = 0;
public bool DropCasingWhenReload;

public bool AdvancedCasingVariables = false;

Expand Down
7 changes: 7 additions & 0 deletions Source/CombatExtended/CombatExtended/Jobs/JobDriver_Reload.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Mono.Unix.Native;
using RimWorld;
using UnityEngine;
using Verse;
Expand Down Expand Up @@ -186,6 +187,12 @@ public override IEnumerable<Toil> 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
Expand Down
3 changes: 2 additions & 1 deletion Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GunDrawExtension>()?.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()
Expand Down

0 comments on commit b36ba93

Please sign in to comment.