Skip to content

Commit

Permalink
Refactor Hediff_Comatose.cs, reformat all source files for 1.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
igoforth committed Sep 25, 2023
1 parent 52a372f commit a8c610e
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 190 deletions.
Binary file modified 1.4/Assemblies/net472/DeathRattle.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion 1.4/Source/DeathRattle/DeathRattleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ namespace DeathRattle;
public class DeathRattleBase : ModBase
{
public override string ModIdentifier => "DeathRattle";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace DeathRattle.Harmony;


/* [HarmonyPatch(typeof(CompUseEffect_FixWorstHealthCondition), nameof(CompUseEffect_FixWorstHealthCondition.DoEffect))]
public static class CompUseEffect_FixWorstHealthCondition_DestroyedOrganPatch
{
Expand Down Expand Up @@ -32,4 +33,4 @@ private static BodyPartRecord FindMissingOrgan(Pawn pawn)
}
return bodyPartRecord;
}
} */
} */
182 changes: 114 additions & 68 deletions 1.4/Source/DeathRattle/Harmony/ShouldBeDeadFromRequiredCapacity.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,130 @@
using System.Reflection.Emit;
using HarmonyLib;
using HarmonyLib;
using RimWorld;
using System.Reflection.Emit;
using Verse;

namespace DeathRattle.Harmony;

[HarmonyPatch(typeof(Pawn_HealthTracker), "ShouldBeDeadFromRequiredCapacity")]
public static class ShouldBeDeadFromRequiredCapacityPatch
{
public static Dictionary<string, HediffDef> ailmentDictionary = new()
{
{ "Metabolism", HediffDefOfDeathRattle.IntestinalFailure },
{ "BloodFiltration", null },
{ "BloodPumping", HediffDefOfDeathRattle.ClinicalDeathNoHeartbeat },
{ "Breathing", HediffDefOfDeathRattle.ClinicalDeathAsphyxiation },
{ "Consciousness", HediffDefOfDeathRattle.Coma }
};
public static Dictionary<string, HediffDef> ailmentDictionary =
new()
{
{ "Metabolism", HediffDefOfDeathRattle.IntestinalFailure },
{ "BloodFiltration", null },
{ "BloodPumping", HediffDefOfDeathRattle.ClinicalDeathNoHeartbeat },
{ "Breathing", HediffDefOfDeathRattle.ClinicalDeathAsphyxiation },
{ "Consciousness", HediffDefOfDeathRattle.Coma }
};

[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> DeathRattleException(IEnumerable<CodeInstruction> instrs)
{
var trigger = false;
foreach (var itr in instrs)
{
yield return itr;
if (trigger)
{
trigger = false;
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(Pawn_HealthTracker), "pawn"));
yield return new CodeInstruction(OpCodes.Ldloc_2);
yield return new CodeInstruction(OpCodes.Callvirt,
AccessTools.Method(typeof(ShouldBeDeadFromRequiredCapacityPatch), "AddCustomHediffs",
new[] { typeof(Pawn_HealthTracker), typeof(Pawn), typeof(PawnCapacityDef) }));
yield return itr;
}
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> DeathRattleException(
IEnumerable<CodeInstruction> instrs
)
{
var trigger = false;
foreach (var itr in instrs)
{
yield return itr;
if (trigger)
{
trigger = false;
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(
OpCodes.Ldfld,
AccessTools.Field(typeof(Pawn_HealthTracker), "pawn")
);
yield return new CodeInstruction(OpCodes.Ldloc_2);
yield return new CodeInstruction(
OpCodes.Callvirt,
AccessTools.Method(
typeof(ShouldBeDeadFromRequiredCapacityPatch),
"AddCustomHediffs",
new[] { typeof(Pawn_HealthTracker), typeof(Pawn), typeof(PawnCapacityDef) }
)
);
yield return itr;
}

if (itr.opcode == OpCodes.Callvirt && itr.operand.Equals(AccessTools.Method(typeof(PawnCapacitiesHandler),
"CapableOf", new[] { typeof(PawnCapacityDef) }))) trigger = true;
}
}
if (
itr.opcode == OpCodes.Callvirt
&& itr.operand.Equals(
AccessTools.Method(
typeof(PawnCapacitiesHandler),
"CapableOf",
new[] { typeof(PawnCapacityDef) }
)
)
)
trigger = true;
}
}

public static bool AddCustomHediffs(Pawn_HealthTracker tracker, Pawn pawn, PawnCapacityDef pawnCapacityDef)
{
if (pawn.health.hediffSet.GetBrain() == null)
return false;
public static bool AddCustomHediffs(
Pawn_HealthTracker tracker,
Pawn pawn,
PawnCapacityDef pawnCapacityDef
)
{
// Exit if pawn does not have brain
if (pawn.health.hediffSet.GetBrain() == null)
return false;

if (ModsConfig.BiotechActive
&& pawn.genes != null
&& pawn.genes.HasGene(GeneDefOf.Deathless))
return false;
// Exit if Biotech is active and pawn has Deathless gene
if (
ModsConfig.BiotechActive
&& pawn.genes != null
&& pawn.genes.HasGene(GeneDefOf.Deathless)
)
return false;

if (pawn.RaceProps.IsFlesh
&& pawnCapacityDef.lethalFlesh
&& !tracker.capacities.CapableOf(pawnCapacityDef)
&& ailmentDictionary.ContainsKey(pawnCapacityDef.defName))
{
var def = ailmentDictionary[pawnCapacityDef.defName];
if (def == null)
{
var notMissingParts = pawn.health.hediffSet.GetNotMissingParts(depth: BodyPartDepth.Inside);
if (notMissingParts.FirstOrDefault(p => p.def.defName == "Liver") == null
&& !pawn.health.hediffSet.HasHediff(HediffDefOfDeathRattle.LiverFailure))
def = HediffDefOfDeathRattle.LiverFailure;
else if (notMissingParts.FirstOrDefault(p => p.def.defName.Contains("Kidney")) == null
&& !pawn.health.hediffSet.HasHediff(HediffDefOfDeathRattle.KidneyFailure))
def = HediffDefOfDeathRattle.KidneyFailure;
}
// Check if pawn's race is flesh and if the capacity is lethal
if (
pawn.RaceProps.IsFlesh
&& pawnCapacityDef.lethalFlesh
&& !tracker.capacities.CapableOf(pawnCapacityDef)
)
{
// Check if the ailment exists in dictionary
if (ailmentDictionary.TryGetValue(pawnCapacityDef.defName, out var def))
{
var notMissingParts = pawn.health.hediffSet.GetNotMissingParts(
depth: BodyPartDepth.Inside
);

if (def != null && !pawn.health.hediffSet.HasHediff(def))
{
var ailment = (Hediff_DeathRattle)HediffMaker.MakeHediff(def, pawn);
ailment.cause = pawnCapacityDef;
pawn.health.AddHediff(ailment);
}
// Check for specific ailments like liver or kidney failure
if (def == null)
{
if (
!pawn.health.hediffSet.HasHediff(HediffDefOfDeathRattle.LiverFailure)
&& !notMissingParts.Any(p => p.def.defName == "Liver")
)
{
def = HediffDefOfDeathRattle.LiverFailure;
}
else if (
!pawn.health.hediffSet.HasHediff(HediffDefOfDeathRattle.KidneyFailure)
&& !notMissingParts.Any(p => p.def.defName.Contains("Kidney"))
)
{
def = HediffDefOfDeathRattle.KidneyFailure;
}
}

return true;
}
// If pawn does not have the hediff, add it
if (def != null && !pawn.health.hediffSet.HasHediff(def))
{
var ailment = (Hediff_DeathRattle)HediffMaker.MakeHediff(def, pawn);
ailment.cause = pawnCapacityDef;
pawn.health.AddHediff(ailment);
}

return false;
}
}
return true;
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion 1.4/Source/DeathRattle/HediffDefOfComatose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ namespace DeathRattle;
public static class HediffDefOfComatose
{
public static HediffDef ArtificialComa;
}
}
2 changes: 1 addition & 1 deletion 1.4/Source/DeathRattle/HediffDefOfDeathRattle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public static class HediffDefOfDeathRattle
public static HediffDef ClinicalDeathNoHeartbeat;
public static HediffDef ClinicalDeathAsphyxiation;
public static HediffDef Coma;
}
}
2 changes: 1 addition & 1 deletion 1.4/Source/DeathRattle/HediffDefOfDeathRattleStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public static class HediffDefOfDeathRattleStrings
public static string ClinicalDeathNoHeartbeat = "ClinicalDeathNoHeartbeat";
public static string ClinicalDeathAsphyxiation = "ClinicalDeathAsphyxiation";
public static string Coma = "Coma";
}
}
2 changes: 1 addition & 1 deletion 1.4/Source/DeathRattle/HediffGiver_BrainDamage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public override void OnIntervalPassed(Pawn pawn, Hediff cause)
SendLetter(pawn, cause);
pawn.health.hediffSet.GetFirstHediffOfDef(hediff).Severity += severityAmount;
}
}
}
Loading

0 comments on commit a8c610e

Please sign in to comment.