Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

Commit

Permalink
WIP part 2
Browse files Browse the repository at this point in the history
- Remove FT implementation
- SpentAmmo having mass costs implemented
- Magazine bulk implemented
- Set Revolver to ejectsCasings = FALSE
- Ported over previous "Fix-for-#1044" to new Development with PRs
- ConservedMassFactorWhenFired added
- ConfigError checking for Amount without backup
- Loaded ammo statPart now includes several discussed things
- VerbShootCE ejecting casings checks whether conservedMassFactorWhenFiring is positive
  • Loading branch information
Alicecomma committed Feb 22, 2020
1 parent 2ff8c43 commit d210c94
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 88 deletions.
10 changes: 0 additions & 10 deletions Defs/Stats/Stats_Weapons_Ranged.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,5 @@
<showIfUndefined>true</showIfUndefined>
<displayPriorityInCategory>897</displayPriorityInCategory>
</StatDef>

<StatDef>
<defName>ammoConsumedPerShotCount</defName>
<workerClass>CombatExtended.StatWorker_AmmoConsumedPerShotCount</workerClass>
<label>ammo consumed per shot</label>
<description>How many units of ammunition this gun consumes for each individual shot.\n\nThis applies to guns with multiple barrels that fire simultaneously with each pull of the trigger, or exotic energy weapons that use up different amounts of energy from a power cell.</description>
<category>Weapon</category>
<showIfUndefined>true</showIfUndefined>
<displayPriorityInCategory>877</displayPriorityInCategory>
</StatDef>

</Defs>
1 change: 1 addition & 0 deletions Languages/English/Keyed/BulkAndWeight.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
<CE_DropExcess>Drop excess</CE_DropExcess>
<CE_PickupMissingAndDropExcess>Pickup missing and drop excess</CE_PickupMissingAndDropExcess>
<CE_StatsReport_LoadedAmmo>Loaded ammunition</CE_StatsReport_LoadedAmmo>
<CE_StatsReport_SpentAmmo>Spent cartridges</CE_StatsReport_SpentAmmo>

</LanguageData>
1 change: 1 addition & 0 deletions Languages/English/Keyed/Keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

<!-- Info Box -->
<CE_MagazineSize>Magazine size</CE_MagazineSize>
<CE_MagazineBulk>Magazine bulk</CE_MagazineBulk>
<CE_ReloadTime>Reload time</CE_ReloadTime>
<CE_AmmoSet>Caliber</CE_AmmoSet>
<CE_FireModes>Fire modes</CE_FireModes>
Expand Down
1 change: 1 addition & 0 deletions Patches/Core/ThingDefs_Misc/Weapons_Guns.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<soundCast>Shot_Revolver</soundCast>
<soundCastTail>GunTail_Light</soundCastTail>
<muzzleFlashScale>9</muzzleFlashScale>
<ejectsCasings>FALSE</ejectsCasings>
</Properties>
<AmmoUser>
<magazineSize>6</magazineSize>
Expand Down
1 change: 0 additions & 1 deletion Source/CombatExtended/CombatExtended.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@
<Compile Include="CombatExtended\Settings.cs" />
<Compile Include="CombatExtended\Projectiles\Projectile_FireTrail.cs" />
<Compile Include="CombatExtended\SightUtility.cs" />
<Compile Include="CombatExtended\StatWorkers\StatWorker_AmmoConsumedPerShotCount.cs" />
<Compile Include="CombatExtended\StatParts\StatPart_LoadedAmmo.cs" />
<Compile Include="CombatExtended\StatWorkers\StatWorker_ArmorCoverage.cs" />
<Compile Include="CombatExtended\StatWorkers\StatWorker_BodyPartDensity.cs" />
Expand Down
52 changes: 32 additions & 20 deletions Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CompAmmoUser : CompRangedGizmoGiver
{
#region Fields

private int lastLoadedMagCountInt = 0;
private int curMagCountInt = 0;
private AmmoDef currentAmmoInt = null;
private AmmoDef selectedAmmo;
Expand All @@ -35,6 +36,11 @@ public CompProperties_AmmoUser Props
}
}

bool ejectsCasings = false;
//Used by StatPart_LoadedAmmo to calculate remaining weight of e.g casings or spent batteries
public int SpentRounds => (ejectsCasings && ((CurAmmoProjectile.projectile as ProjectilePropertiesCE)?.dropsCasings ?? false))
? 0 : lastLoadedMagCountInt - curMagCountInt;

public int CurMagCount
{
get
Expand All @@ -46,6 +52,8 @@ public int CurMagCount
if (curMagCountInt != value && value >= 0)
{
curMagCountInt = value;
lastLoadedMagCountInt = Mathf.Max(lastLoadedMagCountInt, value);

if (CompInventory != null) CompInventory.UpdateInventory(); //Must be positioned after curMagCountInt is updated, because it relies on that value
}
}
Expand Down Expand Up @@ -118,7 +126,11 @@ public AmmoDef CurrentAmmo
return UseAmmo ? currentAmmoInt : null;
}
}
public ThingDef CurAmmoProjectile => Props.ammoSet?.ammoTypes?.FirstOrDefault(x => x.ammo == CurrentAmmo)?.projectile ?? parent.def.Verbs.FirstOrDefault().defaultProjectile;
public AmmoLink CurrentLink => Props.ammoSet?.ammoTypes?
.Where(x => x.ammo == CurrentAmmo && x.amount <= CurMagCount)
.MaxByWithFallback(x => x.amount);
public ThingDef CurAmmoProjectile => CurrentLink?.projectile
?? parent.def.Verbs.FirstOrDefault().defaultProjectile;
public CompInventory CompInventory
{
get
Expand Down Expand Up @@ -172,6 +184,7 @@ public override void Initialize(CompProperties vprops)
base.Initialize(vprops);

//curMagCountInt = Props.spawnUnloaded && UseAmmo ? 0 : Props.magazineSize;
ejectsCasings = parent.def.Verbs.Select(x => x as VerbPropertiesCE).First()?.ejectsCasings ?? true;

// Initialize ammo with default if none is set
if (UseAmmo)
Expand All @@ -196,6 +209,12 @@ public override void PostExposeData()
Scribe_Values.Look(ref curMagCountInt, "count", 0);
Scribe_Defs.Look(ref currentAmmoInt, "currentAmmo");
Scribe_Defs.Look(ref selectedAmmo, "selectedAmmo");

var val = SpentRounds;
Scribe_Values.Look(ref val, "conservedRounds", 0);
lastLoadedMagCountInt = curMagCountInt + val;

ejectsCasings = parent.def.Verbs.Select(x => x as VerbPropertiesCE).First()?.ejectsCasings ?? true;
}

private void AssignJobToWielder(Job job)
Expand Down Expand Up @@ -236,12 +255,11 @@ public bool Notify_PostShotFired()
}

/// <summary>
/// <para>Reduces ammo count and updates inventory if necessary, call this whenever ammo is consumed by the gun (e.g. firing a shot, clearing a jam). </para>
/// <para>Has an optional argument for the amount of ammo to consume per shot, which defaults to 1; this caters for special cases such as different sci-fi weapons using up different amounts of the same energy cell ammo type per shot, or a double-barrelled shotgun that fires both cartridges at the same time (projectile treated as a single, more powerful bullet)</para>
/// Reduces ammo count and updates inventory if necessary, call this whenever ammo is consumed by the gun (e.g. firing a shot, clearing a jam).
/// </summary>
public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1)
public bool TryReduceAmmoCount()
{
ammoConsumedPerShot = (ammoConsumedPerShot > 0) ? ammoConsumedPerShot : 1;
var ammoConsumedPerShot = (CurrentLink?.amount ?? 1);

if (Wielder == null && turret == null)
{
Expand All @@ -262,31 +280,24 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1)
currentAmmoInt = ammoToBeDeleted.def as AmmoDef;
}

if (ammoToBeDeleted.stackCount > 1)
ammoToBeDeleted = ammoToBeDeleted.SplitOff(1);
//Set CurMagCount since it changes CurrentLink return value
CurMagCount = ammoToBeDeleted.stackCount;
ammoConsumedPerShot = (CurrentLink?.amount ?? 1);

if (ammoToBeDeleted.stackCount > ammoConsumedPerShot)
ammoToBeDeleted = ammoToBeDeleted.SplitOff(ammoConsumedPerShot);
}
return true;
}
// If magazine is empty, return false
if (curMagCountInt <= 0)
{
CurMagCount = 0;
lastLoadedMagCountInt = 0;
return false;
}
// Reduce ammo count and update inventory
CurMagCount = (curMagCountInt - ammoConsumedPerShot < 0) ? 0 : curMagCountInt - ammoConsumedPerShot;


/*if (curMagCountInt - ammoConsumedPerShot < 0)
{
curMagCountInt = 0;
} else
{
curMagCountInt = curMagCountInt - ammoConsumedPerShot;
}*/


// Original: curMagCountInt--;
CurMagCount -= ammoConsumedPerShot;

if (curMagCountInt < 0) TryStartReload();
return true;
Expand Down Expand Up @@ -403,6 +414,7 @@ public bool TryUnload(out Thing droppedAmmo, bool forceUnload = false)

// don't forget to set the clip to empty...
CurMagCount = 0;
lastLoadedMagCountInt = 0;

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace CombatExtended
public class CompProperties_AmmoUser : CompProperties
{
public int magazineSize = 0;
public float magazineBulk = 0f;
public float reloadTime = 1;
public bool reloadOneAtATime = false;
public bool throwMote = true;
Expand Down
1 change: 1 addition & 0 deletions Source/CombatExtended/CombatExtended/Defs/AmmoDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AmmoDef : ThingDef
public int defaultAmmoCount = 1;
public float cookOffSpeed = 1f;
public float cookOffFlashScale = 1;
public float conservedMassFactorWhenFired = 0.1f;
public ThingDef cookOffProjectile = null;
public SoundDef cookOffSound = null;
public SoundDef cookOffTailSound = null;
Expand Down
10 changes: 8 additions & 2 deletions Source/CombatExtended/CombatExtended/Defs/AmmoLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class AmmoLink
{
public AmmoDef ammo;
public ThingDef projectile;
public int amount = 1;

public AmmoLink() { }

Expand All @@ -31,16 +32,21 @@ public void LoadDataFromXmlCustom(XmlNode xmlRoot)
}
DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(this, "ammo", xmlRoot.Name);
DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(this, "projectile", (string)ParseHelper.FromString(xmlRoot.FirstChild.Value, typeof(string)));
if (xmlRoot.Attributes["Amount"] != null)
amount = (int)ParseHelper.FromString(xmlRoot.Attributes["Amount"].Value, typeof(int));
}

public override string ToString()
{
return string.Concat("(", ammo == null ? "null" : ammo.defName, " -> ", projectile == null ? "null" : projectile.defName, ")");
return "("
+ (ammo == null ? "null" : ammo.defName)
+ (amount > 1 ? "x" + amount + " -> " : " -> ")
+ (projectile == null ? "null" : projectile.defName + ")");
}

public override int GetHashCode()
{
return ammo.shortHash + projectile.shortHash << 16;
return ammo.shortHash + projectile.shortHash + amount << 16;
}
}
}
16 changes: 16 additions & 0 deletions Source/CombatExtended/CombatExtended/Defs/AmmoSetDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ namespace CombatExtended
public class AmmoSetDef : Def
{
public List<AmmoLink> ammoTypes;

public override IEnumerable<string> ConfigErrors()
{
foreach (string str in base.ConfigErrors())
{
yield return str;
}

var multiplesWithoutSingles =
ammoTypes.Where(x => x.amount > 1).Select(x => x.ammo)
.Except(ammoTypes.Where(x => x.amount == 1).Select(x => x.ammo));

if (multiplesWithoutSingles.Any())
yield return "has multiple ammoDefs with Amount > 1, without a fallback of Amount = 1; namely "
+ String.Join(",", multiplesWithoutSingles.Select(x => x.defName).ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using Verse;
using System.Text;
using RimWorld;
using Verse;
using UnityEngine;

namespace CombatExtended
{
public class StatPart_LoadedAmmo : StatPart
{
float cartridges = 0f;
float spentCartridges = 0f;
float magazine = 0f;

public override void TransformValue(StatRequest req, ref float val)
{
if (TryGetValue(req, out float num))
Expand All @@ -15,25 +21,52 @@ public override void TransformValue(StatRequest req, ref float val)

public override string ExplanationPart(StatRequest req)
{
return TryGetValue(req, out float num)
? "CE_StatsReport_LoadedAmmo".Translate() + ": " + parentStat.ValueToString(num)
: null;
StringBuilder stringBuilder = new StringBuilder();

if (TryGetValue(req, out float _))
{
if (cartridges != 0f)
stringBuilder.AppendLine("CE_StatsReport_LoadedAmmo".Translate() + ": " + parentStat.ValueToString(cartridges));

if (spentCartridges != 0f)
stringBuilder.AppendLine("CE_StatsReport_SpentAmmo".Translate() + ": " + parentStat.ValueToString(spentCartridges));

if (magazine != 0f)
stringBuilder.AppendLine("CE_MagazineBulk".Translate() + ": " + parentStat.ValueToString(magazine));

return stringBuilder.ToString().TrimEndNewlines();
}

return null;
}

public bool TryGetValue(StatRequest req, out float num)
{
num = 0f;
cartridges = 0f;
spentCartridges = 0f;
magazine = 0f;

if (req.HasThing)
{
var ammoUser = req.Thing.TryGetComp<CompAmmoUser>();
if (ammoUser != null && ammoUser.CurrentAmmo != null)
{
num = ammoUser.CurrentAmmo.GetStatValueAbstract(parentStat) * ammoUser.CurMagCount;
var numSingle = ammoUser.CurrentAmmo.GetStatValueAbstract(parentStat);

cartridges = numSingle * ammoUser.CurMagCount;

if (Controller.settings.EnableAmmoSystem && parentStat == StatDefOf.Mass)
spentCartridges = ammoUser.SpentRounds * numSingle * ammoUser.CurrentAmmo.conservedMassFactorWhenFired;
else if (parentStat == CE_StatDefOf.Bulk)
{
cartridges *= ammoUser.Props.loadedAmmoBulkFactor;

if (parentStat == CE_StatDefOf.Bulk)
num *= ammoUser.Props.loadedAmmoBulkFactor;
if (ammoUser.HasMagazine && ammoUser.CurMagCount > 0)
magazine = ammoUser.Props.magazineBulk;
}
}
}
num = cartridges + spentCartridges + magazine;
return num != 0f;
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace CombatExtended
public class VerbPropertiesCE : VerbProperties
{
public RecoilPattern recoilPattern = RecoilPattern.None;
public int ammoConsumedPerShotCount = 1;
public float recoilAmount = 0;
public float indirectFirePenalty = 0;
public float circularError = 0;
Expand Down
5 changes: 3 additions & 2 deletions Source/CombatExtended/CombatExtended/Verbs/Verb_ShootCE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected override bool TryCastShot()
//Reduce ammunition
if (CompAmmo != null)
{
if (!CompAmmo.TryReduceAmmoCount(VerbPropsCE.ammoConsumedPerShotCount))
if (!CompAmmo.TryReduceAmmoCount())
{
return false;
}
Expand All @@ -192,7 +192,8 @@ protected override bool TryCastShot()
ShooterPawn.records.Increment(RecordDefOf.ShotsFired);
}
//Drop casings
if (VerbPropsCE.ejectsCasings && projectilePropsCE.dropsCasings)
if (VerbPropsCE.ejectsCasings && projectilePropsCE.dropsCasings
&& (CompAmmo?.CurrentAmmo?.conservedMassFactorWhenFired > 0f))
{
CE_Utility.ThrowEmptyCasing(caster.DrawPos, caster.Map, ThingDef.Named(projectilePropsCE.casingMoteDefname));
}
Expand Down

0 comments on commit d210c94

Please sign in to comment.