Created
October 28, 2023 20:07
-
-
Save feldoh/e715c99ef144bc5ee31460c765489024 to your computer and use it in GitHub Desktop.
Liver Protection Hediff Gene
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
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