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

Commit

Permalink
Merge pull request #1039 from CombatExtendedRWMod/Fix-for-#937
Browse files Browse the repository at this point in the history
Fix #937 (loaded ammo has weight/bulk update properly)
  • Loading branch information
zozilin authored Feb 22, 2020
2 parents 77a44f9 + cf11995 commit 9521fc9
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Defs/Stats/Stats_Basics_Inventory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<minValue>0.001</minValue>
<toStringStyle>FloatTwo</toStringStyle>
<displayPriorityInCategory>10</displayPriorityInCategory>
<parts>
<li Class="CombatExtended.StatPart_LoadedAmmo"/>
</parts>
</StatDef>

<StatDef>
Expand Down
2 changes: 1 addition & 1 deletion Languages/English/Keyed/BulkAndWeight.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
<CE_AddAmmoFor>Add ammo for {0}</CE_AddAmmoFor>
<CE_DropExcess>Drop excess</CE_DropExcess>
<CE_PickupMissingAndDropExcess>Pickup missing and drop excess</CE_PickupMissingAndDropExcess>

<CE_StatsReport_LoadedAmmo>Loaded ammunition</CE_StatsReport_LoadedAmmo>

</LanguageData>
7 changes: 7 additions & 0 deletions Patches/Core/Stats/Stats.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
</value>
</Operation>

<Operation Class="PatchOperationAdd">
<xpath>Defs/StatDef[defName = "Mass"]/parts</xpath>
<value>
<li Class="CombatExtended.StatPart_LoadedAmmo"/>
</value>
</Operation>

<!-- ========== Apparel ========== -->

<!-- Armor Cap -->
Expand Down
1 change: 1 addition & 0 deletions Source/CombatExtended/CombatExtended.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
<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" />
<Compile Include="CombatExtended\StatWorkers\StatWorker_BodyPartKPA.cs" />
Expand Down
25 changes: 14 additions & 11 deletions Source/CombatExtended/CombatExtended/Comps/CompAmmoUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -279,11 +287,7 @@ public bool TryReduceAmmoCount(int ammoConsumedPerShot = 1)


// Original: curMagCountInt--;

if (CompInventory != null)
{
CompInventory.UpdateInventory();
}

if (curMagCountInt < 0) TryStartReload();
return true;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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));
}
Expand All @@ -504,7 +507,7 @@ public void ResetAmmoCount(AmmoDef newAmmo = null)
currentAmmoInt = newAmmo;
selectedAmmo = newAmmo;
}
curMagCountInt = Props.magazineSize;
CurMagCount = Props.magazineSize;
}

public bool TryFindAmmoInInventory(out Thing ammoThing)
Expand Down
3 changes: 3 additions & 0 deletions Source/CombatExtended/CombatExtended/Comps/CompInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,16 @@ 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<CompAmmoUser>();
if (comp != null && comp.CurrentAmmo != null)
{
weight += comp.CurrentAmmo.GetStatValueAbstract(StatDefOf.Mass) * comp.CurMagCount;
//old weight += comp.currentAmmo.GetStatValueAbstract(CE_StatDefOf.Weight) * comp.curMagCount;
bulk += comp.CurrentAmmo.GetStatValueAbstract(CE_StatDefOf.Bulk) * comp.CurMagCount;
}
*/
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CompAmmoUser>();
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;
}
}
}

0 comments on commit 9521fc9

Please sign in to comment.