Skip to content

Commit

Permalink
try to use medicine in inventory first
Browse files Browse the repository at this point in the history
  • Loading branch information
FluffierThanThou committed Aug 14, 2021
1 parent 7ca2c61 commit 4967faf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
41 changes: 36 additions & 5 deletions Source/HarmonyPatches/HealthAIUtility_FindBestMedicine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// HealthAIUtility_FindBestMedicine.cs
// 2017-02-11

using System.Linq;
using HarmonyLib;
using RimWorld;
using Verse;
Expand All @@ -10,25 +11,55 @@
namespace Pharmacist {
[HarmonyPatch(typeof(HealthAIUtility), nameof(HealthAIUtility.FindBestMedicine))]
public class HealthAIUtility_FindBestMedicine {
public static bool Prefix(Pawn healer, Pawn patient, ref Thing __result) {
public static bool Prefix(Pawn healer, Pawn patient, bool onlyUseInventory, ref Thing __result) {

// get lowest of pawn care settings & pharmacy settings
MedicalCareCategory pharmacistAdvice = PharmacistUtility.TendAdvice( patient );

if (pharmacistAdvice <= MedicalCareCategory.NoMeds) {
__result = null;
return false;
}

// check count required
int countRequired = Medicine.GetMedicineCountToFullyHeal(patient);
if (countRequired <= 0) {
__result = null;
return false;
}

float potencyGetter(Thing t) {
return t.def.GetStatValueAbstract(StatDefOf.MedicalPotency);
}
bool allowedPredicate(Thing m) {
return !m.IsForbidden(healer)
&& m.def.IsMedicine
&& pharmacistAdvice.AllowsMedicine(m.def)
&& healer.CanReserve(m, 10, 1);
}

// check pockets first
// note: vanilla actually selects the medicine with the _lowest_ potency,
// I have to assume that that is not intentional.
//
// thanks to KennethSammael for adding this check in the unofficial 1.3 update,
// this code is adapted from his changes.
__result = healer.inventory.innerContainer
.Where(allowedPredicate)
.MaxBy(potencyGetter);
if (__result is not null || onlyUseInventory) {
return false;
}

// search for best meds
__result = GenClosest.ClosestThing_Global_Reachable(
patient.Position,
patient.Map,
patient.Map.listerThings.ThingsInGroup(ThingRequestGroup.Medicine),
PathEndMode.ClosestTouch,
TraverseParms.For(healer),
9999f,
(m) => !m.IsForbidden(healer) && pharmacistAdvice.AllowsMedicine(m.def) && healer.CanReserve(m, 1),
(m) => m.def.GetStatValueAbstract(StatDefOf.MedicalPotency));

allowedPredicate,
potencyGetter);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Pharmacist.Properties {
[HarmonyPatch(typeof(WorkGiver_DoBill), "GetMedicalCareCategory")]
public static class WorkGiver_DoBill_GetMedicalCareCategory {
public static bool Prefix(Thing billGiver, ref MedicalCareCategory __result) {
if (!(billGiver is Pawn pawn)) {
if (billGiver is not Pawn pawn) {
// because this is the fallback vanilla uses...
__result = MedicalCareCategory.Best;
} else {
Expand Down
1 change: 1 addition & 0 deletions Source/Pharmacist.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<OutputPath>..\Assemblies\</OutputPath>
<DebugType>portable</DebugType>
<PostBuildEvent>mod update</PostBuildEvent>
<LangVersion>9</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.3.3087" />
Expand Down

0 comments on commit 4967faf

Please sign in to comment.