Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Outposts: Add NRE safeguards in SatisfyNeeds
Browse files Browse the repository at this point in the history
  • Loading branch information
legodude17 committed Feb 18, 2022
1 parent 9628a86 commit b706c48
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Binary file modified 1.3/Assemblies/Outposts.dll
Binary file not shown.
16 changes: 9 additions & 7 deletions Source/Outposts/Outpost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,24 @@ public virtual void SatisfyNeeds()

public virtual void SatisfyNeeds(Pawn pawn)
{
var food = pawn.needs.food;
if (food.CurLevelPercentage <= pawn.RaceProps.FoodLevelPercentageWantEat && ProvidedFood is {IsNutritionGivingIngestible: true} && ProvidedFood.ingestible.HumanEdible)
if (pawn is null) return;
var food = pawn.needs?.food;
if (food is not null && food.CurLevelPercentage <= pawn.RaceProps.FoodLevelPercentageWantEat && ProvidedFood is {IsNutritionGivingIngestible: true} &&
ProvidedFood.ingestible.HumanEdible)
{
var thing = ThingMaker.MakeThing(ProvidedFood);
if (thing.IngestibleNow && pawn.RaceProps.CanEverEat(thing)) food.CurLevel += thing.Ingested(pawn, food.NutritionWanted);
}

if (GenLocalDate.HourInteger(Tile) >= 23 || GenLocalDate.HourInteger(Tile) <= 5) pawn.needs.rest.TickResting(0.75f);
if (GenLocalDate.HourInteger(Tile) >= 23 || GenLocalDate.HourInteger(Tile) <= 5) pawn.needs?.rest?.TickResting(0.75f);

if (pawn.health.HasHediffsNeedingTend())
if (pawn.health is not null && pawn.health.HasHediffsNeedingTend())
{
var doctor = AllPawns.Where(p => p.RaceProps.Humanlike && !p.Downed).MaxBy(p => p.skills?.GetSkill(SkillDefOf.Medicine)?.Level ?? -1f);
Medicine medicine = null;
var potency = 0f;
foreach (var thing in containedItems)
if (thing.def.IsMedicine && pawn.playerSettings.medCare.AllowsMedicine(thing.def))
if (thing.def.IsMedicine && (pawn.playerSettings is null || pawn.playerSettings.medCare.AllowsMedicine(thing.def)))
{
var statValue = thing.GetStatValue(StatDefOf.MedicalPotency);
if (statValue > potency || medicine == null)
Expand All @@ -335,11 +337,11 @@ public virtual void SatisfyNeeds(Pawn pawn)
TendUtility.DoTend(doctor, pawn, medicine);
}

if (pawn.health.hediffSet.HasNaturallyHealingInjury())
if (pawn.health?.hediffSet is not null && pawn.health.hediffSet.HasNaturallyHealingInjury())
pawn.health.hediffSet.GetHediffs<Hediff_Injury>().Where(x => x.CanHealNaturally()).RandomElement()
.Heal(pawn.HealthScale * 0.01f * pawn.GetStatValue(StatDefOf.InjuryHealingFactor));

if (pawn.health.hediffSet.HasTendedAndHealingInjury())
if (pawn.health?.hediffSet is not null && pawn.health.hediffSet.HasTendedAndHealingInjury())
{
var injury = pawn.health.hediffSet.GetHediffs<Hediff_Injury>().Where(x => x.CanHealFromTending()).RandomElement();
injury.Heal(GenMath.LerpDouble(0f, 1f, 0.5f, 1.5f, Mathf.Clamp01(injury.TryGetComp<HediffComp_TendDuration>().tendQuality)) * pawn.HealthScale * 0.01f *
Expand Down

0 comments on commit b706c48

Please sign in to comment.