Skip to content

Commit

Permalink
Clean up even more of the partial armor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AluminumAlman committed Jan 18, 2025
1 parent 320be74 commit 34956c9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 109 deletions.
9 changes: 5 additions & 4 deletions Source/CombatExtended/CombatExtended/ArmorUtilityCE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ public static void ApplyParryDamage(DamageInfo dinfo, Thing parryThing)
// Pawns run their own armor calculations
dinfo.SetAmount(dinfo.Amount * Mathf.Clamp01(Rand.Range(0.5f - pawn.GetStatValue(CE_StatDefOf.MeleeParryChance), 1f - pawn.GetStatValue(CE_StatDefOf.MeleeParryChance) * 1.25f)));
pawn.TakeDamage(dinfo);
return;
}
else if (dinfo.IsAmbientDamage())
{
Expand Down Expand Up @@ -681,7 +682,7 @@ public static float PartialStat(this Apparel apparel, StatDef stat, BodyPartReco
{
continue;
}
result = partial.staticValue > 0f ? partial.staticValue : result * partial.mult;
result = partial.GetStatValue(result);
}
return result;
}
Expand Down Expand Up @@ -719,7 +720,7 @@ public static float PartialStat(this Pawn pawn, StatDef stat, BodyPartRecord par
{
continue;
}
result = partial.staticValue > 0f ? partial.staticValue : result * partial.mult;
result = partial.GetStatValue(result);
}
return result;
}
Expand All @@ -742,7 +743,7 @@ public static float PartialStat(this Apparel apparel, StatDef stat, BodyPartDef
{
continue;
}
result = partial.staticValue > 0f ? partial.staticValue : result * partial.mult;
result = partial.GetStatValue(result);
}
return result;
}
Expand All @@ -765,7 +766,7 @@ public static float PartialStat(this Pawn pawn, StatDef stat, BodyPartDef part)
{
continue;
}
result = partial.staticValue > 0f ? partial.staticValue : result * partial.mult;
result = partial.GetStatValue(result);
}
return result;
}
Expand Down
38 changes: 14 additions & 24 deletions Source/CombatExtended/CombatExtended/Defs/ApparelPartialStat.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using RimWorld;
using Verse;
using System.Xml;
Expand All @@ -13,34 +9,28 @@ public class ApparelPartialStat
{
public StatDef stat;

public float mult = 0f;

public List<BodyPartDef> parts;

public float staticValue = 0f;
public float statValue = 1f;

public bool isStatValueStatic = false;

public float GetStatValue(float baseValue)
{
return isStatValueStatic ? statValue : statValue * baseValue;
}

public void LoadDataFromXmlCustom(XmlNode xmlRoot)
{
bool useStatic = false;
int index = 1;
int index = 0;
if (xmlRoot.FirstChild.Name.Contains("use"))
{
useStatic = ParseHelper.FromString<bool>(xmlRoot.FirstChild.InnerText);
}
else
{
index = 0;
}
DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(this, "stat", xmlRoot.ChildNodes[index].Name, null, null);
if (useStatic)
{
this.staticValue = ParseHelper.FromString<float>(xmlRoot.ChildNodes[index].InnerText);
}
else
{
this.mult = ParseHelper.FromString<float>(xmlRoot.ChildNodes[index].InnerText);
isStatValueStatic = ParseHelper.FromString<bool>(xmlRoot.FirstChild.InnerText);
index = 1;
}

DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(this, "stat", xmlRoot.ChildNodes[index].Name, null, null);
this.statValue = ParseHelper.FromString<float>(xmlRoot.ChildNodes[index].InnerText);

if (parts == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override string GetExplanationFinalizePart(StatRequest req, ToStringNumbe
result += part.ExplanationPart(req) + "\n";
}
}
result += "\n" + "CE_StatWorker_ArmorGeneral".Translate() + finalVal.ToString() + " \n \n" + "CE_StatWorker_ArmorSpecific".Translate();
result += "\n" + "CE_StatWorker_ArmorGeneral".Translate() + finalVal.ToString("0.00") + " \n \n" + "CE_StatWorker_ArmorSpecific".Translate();

var ext = req.Thing.def.GetModExtension<PartialArmorExt>();
;
Expand Down Expand Up @@ -87,14 +87,8 @@ public override string GetExplanationFinalizePart(StatRequest req, ToStringNumbe

if (partstat.stat == this.stat)
{
if (partstat.staticValue <= 0f)
{
result += "\n" + "CE_Multiplier".Translate() + " " + partstat.mult.ToStringPercent();
}
else
{
result += "\n" + "CE_SetValPartial".Translate() + " " + partstat.staticValue.ToStringPercent();
}
var statTranslationKey = partstat.isStatValueStatic ? "CE_SetValPartial" : "CE_Multiplier";
result += "\n" + statTranslationKey.Translate() + " " + partstat.GetStatValue(1f).ToStringPercent();

foreach (var bp in partstat.parts)
{
Expand Down Expand Up @@ -148,84 +142,46 @@ public override string GetExplanationFinalizePart(StatRequest req, ToStringNumbe

public override string GetStatDrawEntryLabel(StatDef stat, float value, ToStringNumberSense numberSense, StatRequest optionalReq, bool finalized = true)
{
if (Controller.settings.PartialStat && (this.stat == global::RimWorld.StatDefOf.ArmorRating_Blunt ||
this.stat == global::RimWorld.StatDefOf.ArmorRating_Sharp))
if (optionalReq == null)
{
return base.GetStatDrawEntryLabel(stat, value, numberSense, optionalReq, finalized);
}

if (optionalReq != null)
{
if (optionalReq.Thing is Apparel apparel)
{
float minArmor = value;
float maxArmor = value;
if (apparel.def.HasModExtension<PartialArmorExt>())
{
foreach (ApparelPartialStat p in apparel.def.GetModExtension<PartialArmorExt>().stats)
{
if (!Controller.settings.PartialStat
|| !(this.stat == global::RimWorld.StatDefOf.ArmorRating_Blunt
|| this.stat == global::RimWorld.StatDefOf.ArmorRating_Sharp))
{
return base.GetStatDrawEntryLabel(stat, value, numberSense, optionalReq, finalized);
}

float thisArmor = value;
if (p.staticValue > 0f)
{
thisArmor = p.staticValue;
}
else
{
thisArmor *= p.mult;
}
if (thisArmor < minArmor)
{
minArmor = thisArmor;
}
else if (thisArmor > maxArmor)
{
maxArmor = thisArmor;
}
}
string minArmorString = minArmor.ToString("0.00");
string maxArmorString = maxArmor.ToString("0.00");
return string.Format(stat.formatString, $"{minArmorString} ~ {maxArmorString}");
}
}
else if (optionalReq.Def?.HasModExtension<PartialArmorExt>() ?? false)
{
Def defToCheck = (optionalReq.Thing as Apparel)?.def ?? optionalReq.Def;
var partialExt = defToCheck?.GetModExtension<PartialArmorExt>();
if (partialExt == null)
{
return base.GetStatDrawEntryLabel(stat, value, numberSense, optionalReq, finalized);
}

float minArmor = value;
float maxArmor = value;
var ext = optionalReq.Def.GetModExtension<PartialArmorExt>();
foreach (ApparelPartialStat partstat in ext.stats)
{
float thisArmor = value;
if (partstat.stat == stat)
{
if (partstat.staticValue > 0f)
{
thisArmor = partstat.staticValue;
}
else
{
thisArmor *= partstat.mult;
}
}
if (thisArmor < minArmor)
{
minArmor = thisArmor;
}
else if (thisArmor > maxArmor)
{
maxArmor = thisArmor;
}
if (minArmor != value || maxArmor != value)
{
string minArmorString = minArmor.ToString("0.00");
string maxArmorString = maxArmor.ToString("0.00");
return string.Format(stat.formatString, $"{minArmorString} ~ {maxArmorString}");
}
}
}
float minArmor = value;
float maxArmor = value;
foreach (ApparelPartialStat partialStat in partialExt.stats)
{
if (partialStat.stat != stat)
{
continue;
}
float thisArmor = partialStat.GetStatValue(value);
if (thisArmor < minArmor)
{
minArmor = thisArmor;
}
else if (thisArmor > maxArmor)
{
maxArmor = thisArmor;
}
}
return base.GetStatDrawEntryLabel(stat, value, numberSense, optionalReq, finalized);
string minArmorString = minArmor.ToString("0.00");
string maxArmorString = maxArmor.ToString("0.00");
return string.Format(stat.formatString, $"{minArmorString} ~ {maxArmorString}");
}

}
}

0 comments on commit 34956c9

Please sign in to comment.