Skip to content

Commit

Permalink
Store settings to static variable to use instead of looking them up e…
Browse files Browse the repository at this point in the history
…verywhere
  • Loading branch information
alextd committed Feb 8, 2022
1 parent 79c5ee5 commit b77aee9
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 35 deletions.
6 changes: 3 additions & 3 deletions Source/FieldTending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static class GoodLayingStatusForTend_Patch
public static void Postfix(Pawn patient, ref bool __result)
{
if (!__result) Log.Message($"StatusFor {patient} is {__result}");
if (!__result && Settings.Get().FieldTendingActive(patient))
if (!__result && Mod.settings.FieldTendingActive(patient))
__result = (patient.GetPosture() != PawnPosture.Standing)
|| (patient.Drafted && patient.jobs.curDriver is JobDriver_Wait //Tend while idle + drafted
&& !patient.stances.FullBodyBusy && !patient.stances.Staggered);
Expand Down Expand Up @@ -104,7 +104,7 @@ public static class UseTempSleepSpot

public static ThinkResult LayDownInPlace(Pawn pawn, JobGiver_PatientGoToBed giver)
{
if (Settings.Get().FieldTendingActive(pawn))
if (Mod.settings.FieldTendingActive(pawn))
{
Building_Bed tempTendSpot = pawn.Position.GetThingList(pawn.Map).FirstOrDefault(t => t.def == TempSleepSpot) as Building_Bed;
if (tempTendSpot == null &&
Expand Down Expand Up @@ -180,7 +180,7 @@ static class ShouldBeFed_Patch
{
public static bool InBed_Patch(Pawn pawn)
{
return pawn.InBed() || Settings.Get().FieldTendingActive(pawn);
return pawn.InBed() || Mod.settings.FieldTendingActive(pawn);
}
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/InventorySurgery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void Postfix(Pawn pawn, Thing billGiver, List<Thing> relevantThing
}

//Tiny addition to use minimal medicine for Anesthetize bill. TODO: Make this a def extension so any recipe could use it, though no one will so why really
int statAdjust = (Settings.Get().minimalMedicineForNonUrgent && HackityGetBill.bill.recipe == Anesthetize ? 1 : -1);
int statAdjust = (Mod.settings.minimalMedicineForNonUrgent && HackityGetBill.bill.recipe == Anesthetize ? 1 : -1);
relevantThings.SortBy(
(Thing x) => statAdjust * x.GetStatValue(StatDefOf.MedicalPotency),
//Check if item is in inventory or spawned in map: inventory "distance" is 0
Expand Down
22 changes: 11 additions & 11 deletions Source/MedicineGrabbing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public static void FilterInjuriesForMedCount(List<Hediff> hediffs)
});
}

if (Settings.Get().noMedicineForNonUrgent)
if (Mod.settings.noMedicineForNonUrgent)
{
hediffs.RemoveAll(h => !h.IsUrgent());
}
else if (Settings.Get().minimalMedicineForNonUrgent && __beep_beep_MinimalMedicineAvailable)
else if (Mod.settings.minimalMedicineForNonUrgent && __beep_beep_MinimalMedicineAvailable)
{
if (hediffs.Any(h => h.IsUrgent()))
hediffs.RemoveAll(h => !h.IsUrgent());
Expand Down Expand Up @@ -342,7 +342,7 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun
Log.Message($"{healer} is tending to {patient}");

float sufficientQuality = maxMedicineQuality + 1; // nothing is sufficient!
if (Settings.Get().minimalMedicineForNonUrgent)
if (Mod.settings.minimalMedicineForNonUrgent)
{
if (patient.health.hediffSet.hediffs.All(h => !h.TendableNow() || !h.IsUrgent()))
{
Expand Down Expand Up @@ -414,18 +414,18 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun

List<Pawn> pawns = healer.Map.mapPawns.SpawnedPawnsInFaction(Faction.OfPlayer).ListFullCopy();

if (!Settings.Get().useDoctorMedicine)
if (!Mod.settings.useDoctorMedicine)
pawns.Remove(healer);
if (!Settings.Get().usePatientMedicine)
if (!Mod.settings.usePatientMedicine)
pawns.Remove(patient);
if (!Settings.Get().useColonistMedicine)
if (!Mod.settings.useColonistMedicine)
pawns.RemoveAll(p => p.IsFreeColonist && p != healer && p != patient);
if (!Settings.Get().useAnimalMedicine)
if (!Mod.settings.useAnimalMedicine)
pawns.RemoveAll(p => !p.IsColonist);

int minDistance = DistanceTo(healer, patient);
if (!Settings.Get().useOtherEvenIfFar)
pawns.RemoveAll(p => DistanceTo(p, healer, patient) > minDistance + Settings.Get().distanceToUseFromOther * 2); //*2, there and back
if (!Mod.settings.useOtherEvenIfFar)
pawns.RemoveAll(p => DistanceTo(p, healer, patient) > minDistance + Mod.settings.distanceToUseFromOther * 2); //*2, there and back

pawns.RemoveAll(p => !validatorHolder(p));

Expand Down Expand Up @@ -458,7 +458,7 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun
if (bestMed.thing != null)
{
allMeds.RemoveLast();
if (Settings.Get().useCloseMedicine && bestMed.pawn != null)
if (Mod.settings.useCloseMedicine && bestMed.pawn != null)
{
bestMed.DebugLog("Best: ");
Log.Message($"checking nearby:");
Expand All @@ -467,7 +467,7 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun
{
MedicineEvaluator closeMed = equalMedicines.MinBy(eval => eval.distance);
closeMed.DebugLog("Nearby med on the way there: ");
if (closeMed.distance <= minDistance + Settings.Get().distanceToUseEqualOnGround * 2) //*2, there and back
if (closeMed.distance <= minDistance + Mod.settings.distanceToUseEqualOnGround * 2) //*2, there and back
bestMed = closeMed;
}
}
Expand Down
7 changes: 1 addition & 6 deletions Source/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SmartMedicine
{
class Settings : ModSettings
public class Settings : ModSettings
{
//TODO: save per map
public bool useDoctorMedicine = true;
Expand Down Expand Up @@ -39,11 +39,6 @@ public bool FieldTendingActive(Pawn patient)
(fieldTendingForLackOfBed && RestUtility.FindPatientBedFor(patient) == null));
}

public static Settings Get()
{
return LoadedModManager.GetMod<SmartMedicine.Mod>().GetSettings<Settings>();
}

public void DoWindowContents(Rect wrect)
{
var options = new Listing_Standard();
Expand Down
5 changes: 3 additions & 2 deletions Source/SmartMedicine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ namespace SmartMedicine
{
public class Mod : Verse.Mod
{
public static Settings settings;
public Mod(ModContentPack content) : base(content)
{
// initialize settings
GetSettings<Settings>();
settings = GetSettings<Settings>();
#if DEBUG
Harmony.DEBUG = true;
#endif
Expand All @@ -26,7 +27,7 @@ public Mod(ModContentPack content) : base(content)
public override void DoSettingsWindowContents(Rect inRect)
{
base.DoSettingsWindowContents(inRect);
GetSettings<Settings>().DoWindowContents(inRect);
settings.DoWindowContents(inRect);
}

public override string SettingsCategory()
Expand Down
6 changes: 3 additions & 3 deletions Source/StockUp/StockUpGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static void DrawMissingThings(Pawn pawn, ref float y, Rect viewRect)
{
if (!pawn.IsFreeColonist || pawn.Dead) return;

if (!Settings.Get().stockUp) return;
if (!Mod.settings.stockUp) return;

foreach (ThingDef def in pawn.StockingUpList().ToList())
if (pawn.StockUpMissing(def))
Expand All @@ -222,7 +222,7 @@ public static void DrawMissingThings(Pawn pawn, ref float y, Rect viewRect)

public static void DrawStockUpButton(Pawn pawn, ref float y, float width)
{
if (!Settings.Get().stockUp) return;
if (!Mod.settings.stockUp) return;

GUI.color = ThingLabelColor;

Expand Down Expand Up @@ -308,7 +308,7 @@ public static class InterfaceDrop_Patch
public static PropertyInfo SelPawnForGearInfo = AccessTools.Property(typeof(ITab_Pawn_Gear), "SelPawnForGear");
public static void Postfix(Thing t, ITab_Pawn_Gear __instance)
{
if (!Settings.Get().stockUp) return;
if (!Mod.settings.stockUp) return;

Pawn pawn = (Pawn)SelPawnForGearInfo.GetValue(__instance, new object[] { });

Expand Down
14 changes: 7 additions & 7 deletions Source/StockUp/StockUpUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public static void StockUpClearSettings(this Pawn pawn)
}
public static bool StockingUpOnAnything(this Pawn pawn)
{
if (!Settings.Get().stockUp || pawn.inventory == null) return false;
if (!Mod.settings.stockUp || pawn.inventory == null) return false;

return pawn.StockUpSettings().Count > 0;
}
public static IEnumerable<ThingDef> StockingUpList(this Pawn pawn)
{
if (!Settings.Get().stockUp || pawn.inventory == null) return new List<ThingDef>();
if (!Mod.settings.stockUp || pawn.inventory == null) return new List<ThingDef>();

return pawn.StockUpSettings().Keys;
}
Expand All @@ -91,7 +91,7 @@ public static IEnumerable<ThingDef> StockingUpList(this Pawn pawn)

public static bool StockingUpOn(this Pawn pawn, ThingDef thingDef)
{
if (!Settings.Get().stockUp || pawn.inventory == null || pawn.inventory.UnloadEverything) return false;
if (!Mod.settings.stockUp || pawn.inventory == null || pawn.inventory.UnloadEverything) return false;

return pawn.StockUpSettings().ContainsKey(thingDef);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public static int StockUpNeeds(this Pawn pawn, ThingDef thingDef)
if (invCount > capacity) return capacity - invCount;

if (!EnoughAvailable(thingDef, pawn.Map))
return Settings.Get().stockUpReturn ? -invCount : 0;
return Mod.settings.stockUpReturn ? -invCount : 0;

return capacity - invCount;
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public static void StockUpStop(this Pawn pawn, ThingDef thingDef)

public static Thing StockUpThingToReturn(this Pawn pawn)
{
if (!Settings.Get().stockUp || pawn.inventory == null) return null;
if (!Mod.settings.stockUp || pawn.inventory == null) return null;

ThingDef td = pawn.StockUpSettings().FirstOrDefault(kvp => pawn.StockUpNeeds(kvp.Key) < 0).Key;
if (td == null) return null;
Expand All @@ -170,15 +170,15 @@ public static Thing StockUpThingToReturn(this Pawn pawn)

public static bool StockUpIsFull(this Pawn pawn)
{
if (!Settings.Get().stockUp) return true;
if (!Mod.settings.stockUp) return true;

return !pawn.StockUpSettings().Any(kvp => pawn.StockUpNeeds(kvp.Key) != 0);
}

public static bool EnoughAvailable(Thing thing) => EnoughAvailable(thing.def, thing.Map);
public static bool EnoughAvailable(ThingDef thingDef, Map map)
{
float enough = Settings.Get().stockUpEnough;
float enough = Mod.settings.stockUpEnough;
if (enough == 0.0f) return true;

float stockUpCount = 0;
Expand Down
4 changes: 2 additions & 2 deletions Source/SurgeryUnlimited/SurgeryUnlimited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SurgeryUnlimitedGameComponent : GameComponent

public SurgeryUnlimitedGameComponent(Game game)
{
surgeryUnlimitedDefault = Settings.Get().defaultUnlimitedSurgery;
surgeryUnlimitedDefault = Mod.settings.defaultUnlimitedSurgery;
surgeryUnlimited = new HashSet<Pawn>();
}

Expand All @@ -37,7 +37,7 @@ public void Set(Pawn pawn, bool val)

public override void ExposeData()
{
Scribe_Values.Look(ref surgeryUnlimitedDefault, "surgeryUnlimitedDefault", Settings.Get().defaultUnlimitedSurgery);
Scribe_Values.Look(ref surgeryUnlimitedDefault, "surgeryUnlimitedDefault", Mod.settings.defaultUnlimitedSurgery);
Scribe_Collections.Look(ref surgeryUnlimited, "surgeryUnlimitedList", LookMode.Reference);
}
}
Expand Down

0 comments on commit b77aee9

Please sign in to comment.