Skip to content

Commit

Permalink
1.3 updates.
Browse files Browse the repository at this point in the history
Used bool onlyUseInventory which is true when drafted-ordered-to-tend
apparently playerSettings can be null, so... hopefully other things aren't broken but don't check for null.
Deleting old transpiler inside MakeNewToils didn't apply and seemingly didn't matter
  • Loading branch information
alextd committed Sep 16, 2021
1 parent 0259265 commit 440af75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 93 deletions.
1 change: 0 additions & 1 deletion Source/GetPawnMedicalCareCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static MedicalCareCategory GetCare(this Pawn pawn)
else
{
care = pawn.playerSettings?.medCare ?? MedicalCareCategory.NoCare;
Log.Message($"Care for {pawn} is {care}");
}
return care;
}
Expand Down
123 changes: 31 additions & 92 deletions Source/MedicineGrabbing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,70 +251,6 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
}
}


//[HarmonyPatch(typeof(JobDriver_TendPatient), "MakeNewToils")]
static class MakeNewToils_Patch
{
static MakeNewToils_Patch()
{
HarmonyMethod transpiler = new HarmonyMethod(typeof(PickupMedicine_Patch), nameof(Transpiler));
Harmony harmony = new Harmony("uuugggg.rimworld.SmartMedicine.main");

Predicate<MethodInfo> check = m => m.Name.Contains("MakeNewToils");

harmony.PatchGeneratedMethod(typeof(JobDriver_TendPatient), check, transpiler: transpiler);
}
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo FindBestMedicineInfo = AccessTools.Method(typeof(HealthAIUtility), nameof(HealthAIUtility.FindBestMedicine));
MethodInfo FindInfo = AccessTools.Method(typeof(FindBestMedicine), nameof(FindBestMedicine.Find));


//MethodInfo FirstOrDefaultInfo = AccessTools.Method(typeof(Enumerable), "FirstOrDefault", new Type[] { typeof(IEnumerable<ThingCount>) }, new Type[] { typeof(IEnumerable<ThingCount>) });
//MethodInfo FirstOrDefaultInfo = AccessTools.FirstMethod(typeof(Enumerable), mi => mi.Name == "FirstOrDefault" && mi.GetParameters().Count() == 1);
//FirstOrDefaultInfo = FirstOrDefaultInfo.MakeGenericMethod(new Type[] { typeof(IEnumerable<ThingCount>) });
//MethodInfo ThingCountThingInfo = AccessTools.Property(typeof(ThingCount), "Thing").GetGetMethod();

//
MethodInfo GetTheBloodyThingInfo = AccessTools.Method(typeof(MakeNewToils_Patch), "GetTheBloodyThing");

FieldInfo jobFieldInfo = AccessTools.Field(
typeof(JobDriver), nameof(JobDriver.job));
FieldInfo jobCountInfo = AccessTools.Field(
typeof(Job), nameof(Job.count));
List<CodeInstruction> iList = instructions.ToList();
List<CodeInstruction> jobInstructions = new List<CodeInstruction>();
for (int i = 0; i < iList.Count(); i++)
{
if (iList[i].LoadsField(jobFieldInfo))
{
jobInstructions.AddRange(iList.GetRange(i - 3, 4));
break;
}
}

foreach (CodeInstruction i in instructions)
{
if (i.Calls(FindBestMedicineInfo))
{
foreach (CodeInstruction jobI in jobInstructions)
yield return jobI;
yield return new CodeInstruction(OpCodes.Ldflda, jobCountInfo);
yield return new CodeInstruction(OpCodes.Call, FindInfo);
yield return new CodeInstruction(OpCodes.Call, GetTheBloodyThingInfo);
//yield return new CodeInstruction(OpCodes.Call, FirstOrDefaultInfo);
//yield return new CodeInstruction(OpCodes.Call, ThingCountThingInfo);
}
else yield return i;
}
}
public static Thing GetTheBloodyThing(List<ThingCount> x)
{
return x.FirstOrDefault().Thing;
}
}


[HarmonyPatch(typeof(HealthAIUtility))]
[HarmonyPatch("FindBestMedicine")]
[HarmonyBefore(new string[] { "fluffy.rimworld.pharmacist" })]
Expand Down Expand Up @@ -378,17 +314,17 @@ static FindBestMedicine()
}

//FindBestMedicine Replacement
private static bool Prefix(Pawn healer, Pawn patient, ref Thing __result)
private static bool Prefix(Pawn healer, Pawn patient, ref Thing __result, bool onlyUseInventory = false)
{
if (patient.GetCare() <= MedicalCareCategory.NoMeds || Medicine.GetMedicineCountToFullyHeal(patient) <= 0)
return true;

__result = Find(healer, patient, out int dummy).FirstOrDefault().Thing;
__result = Find(healer, patient, out int dummy, onlyUseInventory).FirstOrDefault().Thing;
return false;
}

//public static Thing FindBestMedicine(Pawn healer, Pawn patient)
public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCount)
public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCount, bool onlyUseInventory = false)
{
totalCount = 0;
Log.Message($"{healer} is tending to {patient}");
Expand Down Expand Up @@ -418,7 +354,7 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun

finalCare = toUse > finalCare ? toUse : finalCare;
}
Log.Message($"defaultCare = {defaultCare}, care = {finalCare}");
Log.Message($"Care for {patient} is {defaultCare}, Custom care = {finalCare}");

//Android Droid support;
Predicate<Thing> validatorDroid = t => true;
Expand All @@ -429,17 +365,35 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun
validatorDroid = t => t.def.modExtensions?.Any(e => extRepair.IsAssignableFrom(e.GetType())) ?? false;
}

//Med
Predicate<Thing> validatorMed = t => finalCare.AllowsMedicine(t.def) && validatorDroid(t);
//Find valid Meds:
List<MedicineEvaluator> allMeds = new List<MedicineEvaluator>();

//Ground
Predicate<Thing> validatorMed = t => finalCare.AllowsMedicine(t.def) && validatorDroid(t);
Map map = patient.Map;
TraverseParms traverseParams = TraverseParms.For(healer, Danger.Deadly, TraverseMode.ByPawn, false);
Predicate<Thing> validator = (Thing t) => validatorMed(t)
&& map.reachability.CanReach(patient.Position, t, PathEndMode.ClosestTouch, traverseParams)
&& !t.IsForbidden(healer) && healer.CanReserve(t, FindBestMedicine.maxPawns, 1);//can reserve at least 1
Func<Thing, float> priorityGetter = (Thing t) => MedicineRating(t, sufficientQuality);
List<Thing> groundMedicines = patient.Map.listerThings.ThingsInGroup(isDroid?ThingRequestGroup.HaulableEver:ThingRequestGroup.Medicine).FindAll(t => validator(t));

//Ground
if (!onlyUseInventory)
{
Predicate<Thing> validator = (Thing t) => validatorMed(t)
&& map.reachability.CanReach(patient.Position, t, PathEndMode.ClosestTouch, traverseParams)
&& !t.IsForbidden(healer) && healer.CanReserve(t, FindBestMedicine.maxPawns, 1);//can reserve at least 1
Func<Thing, float> priorityGetter = (Thing t) => MedicineRating(t, sufficientQuality);
List<Thing> groundMedicines = map.listerThings.ThingsInGroup(isDroid ? ThingRequestGroup.HaulableEver : ThingRequestGroup.Medicine).FindAll(t => validator(t));

//Add each ground
foreach (Thing t in groundMedicines)
allMeds.Add(new MedicineEvaluator()
{
thing = t,
pawn = null,
rating = MedicineRating(t, sufficientQuality),
distance = DistanceTo(t, healer, patient)
});
}

//Ground-only medicines:
List<MedicineEvaluator> groundEvaluators = allMeds.ListFullCopy();

//Pawns
Predicate<Pawn> validatorHolder = (Pawn p) =>
Expand All @@ -463,21 +417,6 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun

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

//Evaluate them all
List<MedicineEvaluator> allMeds = new List<MedicineEvaluator>();

//Add each ground
foreach (Thing t in groundMedicines)
allMeds.Add(new MedicineEvaluator()
{
thing = t,
pawn = null,
rating = MedicineRating(t, sufficientQuality),
distance = DistanceTo(t, healer, patient)
});

List<MedicineEvaluator> groundEvaluators = allMeds.ListFullCopy();

//Add best from each pawn
foreach (Pawn p in pawns)
{
Expand Down Expand Up @@ -574,7 +513,7 @@ public static List<Hediff> HediffsToTend(Pawn patient)

private static Thing FindBestMedicineInInventory(Pawn pawn, Pawn patient, Predicate<Thing> validatorMed, float sufficientQuality, bool isHealer)
{
if (pawn == null || pawn.inventory == null || patient == null || patient.playerSettings == null)
if (pawn == null || pawn.inventory == null || patient == null)
return null;

List<Thing> items = new List<Thing>(pawn.inventory.innerContainer.InnerListForReading);
Expand Down

0 comments on commit 440af75

Please sign in to comment.