forked from troopersmith1/Death-Rattle-Continued
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Hediff_Comatose.cs, reformat all source files for 1.4.
- Loading branch information
Showing
13 changed files
with
309 additions
and
190 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 114 additions & 68 deletions
182
1.4/Source/DeathRattle/Harmony/ShouldBeDeadFromRequiredCapacity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ namespace DeathRattle; | |
public static class HediffDefOfComatose | ||
{ | ||
public static HediffDef ArtificialComa; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.