Skip to content

Instantly share code, notes, and snippets.

@feldoh
Created October 28, 2023 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feldoh/e715c99ef144bc5ee31460c765489024 to your computer and use it in GitHub Desktop.
Save feldoh/e715c99ef144bc5ee31460c765489024 to your computer and use it in GitHub Desktop.
Liver Protection Hediff Gene
public class LiverProtectionHediffGene : Gene
{
public static Lazy<HediffDef> LiverProtectionHediff = new(() => DefDatabase<HediffDef>.GetNamed("Your_Cool_Hediff"));
public override void PostRemove()
{
if (GetHediff() is { } firstHediffOfDef)
pawn.health.RemoveHediff(firstHediffOfDef);
base.PostRemove();
}
public override void PostAdd()
{
base.PostAdd();
ApplyHediff();
}
public override void Tick()
{
base.Tick();
if (!pawn.Spawned || !pawn.IsHashIntervalTick(GenTicks.TickLongInterval) || Rand.Chance(0.75f) || GetHediff() != null) return;
if (Rand.Chance(0.5f)) ApplyHediff();
}
public Hediff GetHediff() => pawn.health.hediffSet.GetFirstHediffOfDef(LiverProtectionHediff.Value);
public void ApplyHediff()
{
if (GetHediff() != null || GetLiver() is not {} liver) return;
Hediff liverHediff = HediffMaker.MakeHediff(LiverProtectionHediff.Value, pawn, liver);
pawn.health.AddHediff(liverHediff);
}
public BodyPartRecord GetLiver()
{
foreach (BodyPartRecord notMissingPart in pawn.health.hediffSet?.GetNotMissingParts() ?? new List<BodyPartRecord>())
{
if (notMissingPart.def.tags.Contains(BodyPartTagDefOf.BloodFiltrationLiver))
return notMissingPart;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment