From cf119959388f032b2ddc8178a29ece113383c8d9 Mon Sep 17 00:00:00 2001 From: Alicecomma Date: Fri, 21 Feb 2020 20:19:49 +0100 Subject: [PATCH] Fix #937 - CE_StatsReport_LoadedAmmo added to language keys (needs translation) - Stats.xml patched to add CombatExtended.StatPart_LoadedAmmo to Mass stat - Added file StatPart_LoadedAmmo - CompAmmoUser now immediately calls UpdateInventory WHENEVER CurMagCount is changed, as it should be - CompInventory stopped looking at loaded ammo for weight/bulk - StatPart_LoadedAmmo was added, which does everything previously done in CompInventory, but which is common in current RimWorld versions - Bulk StatDef now uses StatPart_LoadedAmmo - AmmoUser CompProperties include a loadedAmmoBulkFactor with default value 0f. This allows RPG-7 and similar weapons to have a higher bulkiness (volume) when loaded. - StatPart_LoadedAmmo multiplies by loadedAmmoBulkFactor when parentStat is Bulk --- Defs/Stats/Stats_Basics_Inventory.xml | 3 ++ Languages/English/Keyed/BulkAndWeight.xml | 2 +- Patches/Core/Stats/Stats.xml | 7 ++++ Source/CombatExtended/CombatExtended.csproj | 1 + .../CombatExtended/Comps/CompAmmoUser.cs | 25 +++++++----- .../CombatExtended/Comps/CompInventory.cs | 3 ++ .../Comps/CompProperties_AmmoUser.cs | 1 + .../StatParts/StatPart_LoadedAmmo.cs | 40 +++++++++++++++++++ 8 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 Source/CombatExtended/CombatExtended/StatParts/StatPart_LoadedAmmo.cs diff --git a/Defs/Stats/Stats_Basics_Inventory.xml b/Defs/Stats/Stats_Basics_Inventory.xml index f12c71945d..46eb8c5d5f 100644 --- a/Defs/Stats/Stats_Basics_Inventory.xml +++ b/Defs/Stats/Stats_Basics_Inventory.xml @@ -10,6 +10,9 @@ 0.001 FloatTwo 10 + +
  • + diff --git a/Languages/English/Keyed/BulkAndWeight.xml b/Languages/English/Keyed/BulkAndWeight.xml index 82e4b1cbff..96c348301f 100644 --- a/Languages/English/Keyed/BulkAndWeight.xml +++ b/Languages/English/Keyed/BulkAndWeight.xml @@ -38,6 +38,6 @@ Add ammo for {0} Drop excess Pickup missing and drop excess - + Loaded ammunition diff --git a/Patches/Core/Stats/Stats.xml b/Patches/Core/Stats/Stats.xml index e5602863c5..87767678e8 100644 --- a/Patches/Core/Stats/Stats.xml +++ b/Patches/Core/Stats/Stats.xml @@ -17,6 +17,13 @@ + + Defs/StatDef[defName = "Mass"]/parts + +
  • + + + diff --git a/Source/CombatExtended/CombatExtended.csproj b/Source/CombatExtended/CombatExtended.csproj index 16de99b1b7..f605bfc2e1 100644 --- a/Source/CombatExtended/CombatExtended.csproj +++ b/Source/CombatExtended/CombatExtended.csproj @@ -179,6 +179,7 @@ + diff --git a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs index 0d71d0f097..f6c448a934 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs @@ -41,6 +41,14 @@ public int CurMagCount { return curMagCountInt; } + set + { + if (curMagCountInt != value && value >= 0) + { + curMagCountInt = value; + if (CompInventory != null) CompInventory.UpdateInventory(); //Must be positioned after curMagCountInt is updated, because it relies on that value + } + } } public CompEquippable CompEquippable { @@ -262,11 +270,11 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1) // If magazine is empty, return false if (curMagCountInt <= 0) { - curMagCountInt = 0; + CurMagCount = 0; return false; } // Reduce ammo count and update inventory - curMagCountInt = (curMagCountInt - ammoConsumedPerShot < 0) ? 0 : curMagCountInt - ammoConsumedPerShot; + CurMagCount = (curMagCountInt - ammoConsumedPerShot < 0) ? 0 : curMagCountInt - ammoConsumedPerShot; /*if (curMagCountInt - ammoConsumedPerShot < 0) @@ -279,11 +287,7 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1) // Original: curMagCountInt--; - - if (CompInventory != null) - { - CompInventory.UpdateInventory(); - } + if (curMagCountInt < 0) TryStartReload(); return true; } @@ -398,7 +402,7 @@ public bool TryUnload(out Thing droppedAmmo, bool forceUnload = false) } // don't forget to set the clip to empty... - curMagCountInt = 0; + CurMagCount = 0; return true; } @@ -466,7 +470,6 @@ public void LoadAmmo(Thing ammo = null) newMagCount = Props.magazineSize; ammoThing.stackCount -= Props.magazineSize; } - if (CompInventory != null) CompInventory.UpdateInventory(); } // If there's less ammo in inventory than the weapon can hold, or if there's only one bullet left if reloading one at a time @@ -487,7 +490,7 @@ public void LoadAmmo(Thing ammo = null) { newMagCount = (Props.reloadOneAtATime) ? (curMagCountInt + 1) : Props.magazineSize; } - curMagCountInt = newMagCount; + CurMagCount = newMagCount; if (turret != null) turret.isReloading = false; if (parent.def.soundInteract != null) parent.def.soundInteract.PlayOneShot(new TargetInfo(Position, Find.CurrentMap, false)); } @@ -503,7 +506,7 @@ public void ResetAmmoCount(AmmoDef newAmmo = null) { currentAmmoInt = newAmmo; } - curMagCountInt = Props.magazineSize; + CurMagCount = Props.magazineSize; } public bool TryFindAmmoInInventory(out Thing ammoThing) diff --git a/Source/CombatExtended/CombatExtended/Comps/CompInventory.cs b/Source/CombatExtended/CombatExtended/Comps/CompInventory.cs index 7b6ad1d193..65678df335 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompInventory.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompInventory.cs @@ -281,6 +281,8 @@ public static void GetEquipmentStats(ThingWithComps eq, out float weight, out fl weight = eq.GetStatValue(StatDefOf.Mass); //old weight = eq.GetStatValue(CE_StatDefOf.Weight); bulk = eq.GetStatValue(CE_StatDefOf.Bulk); + + /* CompAmmoUser comp = eq.TryGetComp(); if (comp != null && comp.CurrentAmmo != null) { @@ -288,6 +290,7 @@ public static void GetEquipmentStats(ThingWithComps eq, out float weight, out fl //old weight += comp.currentAmmo.GetStatValueAbstract(CE_StatDefOf.Weight) * comp.curMagCount; bulk += comp.CurrentAmmo.GetStatValueAbstract(CE_StatDefOf.Bulk) * comp.CurMagCount; } + */ } /// diff --git a/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoUser.cs b/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoUser.cs index 44f76c0c0d..d4ab55a63a 100644 --- a/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoUser.cs +++ b/Source/CombatExtended/CombatExtended/Comps/CompProperties_AmmoUser.cs @@ -16,6 +16,7 @@ public class CompProperties_AmmoUser : CompProperties public bool throwMote = true; public AmmoSetDef ammoSet = null; public bool spawnUnloaded = false; + public float loadedAmmoBulkFactor = 0f; public CompProperties_AmmoUser() { diff --git a/Source/CombatExtended/CombatExtended/StatParts/StatPart_LoadedAmmo.cs b/Source/CombatExtended/CombatExtended/StatParts/StatPart_LoadedAmmo.cs new file mode 100644 index 0000000000..47902b3110 --- /dev/null +++ b/Source/CombatExtended/CombatExtended/StatParts/StatPart_LoadedAmmo.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using Verse; +using RimWorld; + +namespace CombatExtended +{ + public class StatPart_LoadedAmmo : StatPart + { + public override void TransformValue(StatRequest req, ref float val) + { + if (TryGetValue(req, out float num)) + val += num; + } + + public override string ExplanationPart(StatRequest req) + { + return TryGetValue(req, out float num) + ? "CE_StatsReport_LoadedAmmo".Translate() + ": " + parentStat.ValueToString(num) + : null; + } + + public bool TryGetValue(StatRequest req, out float num) + { + num = 0f; + if (req.HasThing) + { + var ammoUser = req.Thing.TryGetComp(); + if (ammoUser != null && ammoUser.CurrentAmmo != null) + { + num = ammoUser.CurrentAmmo.GetStatValueAbstract(parentStat) * ammoUser.CurMagCount; + + if (parentStat == CE_StatDefOf.Bulk) + num *= ammoUser.Props.loadedAmmoBulkFactor; + } + } + return num != 0f; + } + } +} \ No newline at end of file