Skip to content

Commit

Permalink
Handle mods making tend subclases.
Browse files Browse the repository at this point in the history
If they don't use Smart Medicine Find, it won't set job count, so only use jobcount if it is set, and re-calculate Medicine count when needed, aka just like vanilla does
  • Loading branch information
alextd committed Apr 15, 2023
1 parent 6437cdd commit 856e00e
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions Source/MedicineGrabbing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,29 @@ public static void Prefix(JobDriver_TendPatient __instance)
Pawn healer = __instance.pawn;
Pawn patient = job.targetA.Thing as Pawn;
Thing medicineToDrop = job.targetB.Thing;

// Nothing to drop
if (medicineToDrop == null) return;

// Job not created with Smart Medicine ; let's hope you don't need to drop it.
if (job.count == 0)
return;

if (job.draftedTend)
{
//WorkGiver_Tend patch above sets job.count
//but 1.3 added right-click tend option - that dropdown menu delegate is a pain to transpile in the job count...
//so just set it here. A bit redundant but what can you do.
FindBestMedicine.Find(healer, patient, out job.count, job.draftedTend);
// WorkGiver_Tend patch above sets job.count
// but 1.3 added right-click tend option - that dropdown menu delegate is a pain to transpile in the job count...
// and sets job.count = 1 ? BUT DOES NOT USE IT?!?!
// so just set it here. A bit redundant but what can you do.
job.count = Medicine.GetMedicineCountToFullyHeal(patient);

//I don't fuckin understand but maybe a mod conflict makes this 0 and 0 here is bad.
//Probably it is sovled with above job.draftedTend though.
//if (job.count < 1) job.count = 1;
}
int needCount = Mathf.Min(medicineToDrop.stackCount, job.count);

Log.Message($"{healer} Starting Tend with {medicineToDrop}");
Log.Message($"{healer} Starting Tend with {medicineToDrop}:{needCount}");

job.targetB = DropIt(medicineToDrop, needCount, healer, job);

Expand Down Expand Up @@ -229,8 +236,6 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
typeof(Medicine), nameof(Medicine.GetMedicineCountToFullyHeal));
MethodInfo GetCarriedThingInfo = AccessTools.Property(
typeof(Pawn_CarryTracker), nameof(Pawn_CarryTracker.CarriedThing)).GetGetMethod();
FieldInfo countInfo = AccessTools.Field(
typeof(Job), nameof(Job.count));

bool branchNext = false;
bool branched = false;
Expand All @@ -246,10 +251,8 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

if (i.Calls(GetMedicineCountToFullyHealInfo))
{
yield return new CodeInstruction(OpCodes.Pop);//pawn

yield return new CodeInstruction(OpCodes.Ldloc_1);//job
yield return new CodeInstruction(OpCodes.Ldfld, countInfo);//job.count
yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(PickupMedicine_Patch), nameof(JobCountOrCalculate))); //JobCountOrCalculate(injured, job)
}
else
yield return i;
Expand All @@ -260,6 +263,19 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
}
}
}

public static int JobCountOrCalculate(Pawn injured, Job job)
{
if (job.count > 0)
{
Log.Message($"PickupMedicine using job.count:{job.count}");
return job.count; //Probably because Smart Medicine Set it
}

//Otherwise, use what was already there.
Log.Message($"PickupMedicine using calculated: {Medicine.GetMedicineCountToFullyHeal(injured)}");
return Medicine.GetMedicineCountToFullyHeal(injured);
}
}

[HarmonyPatch(typeof(HealthAIUtility))]
Expand Down Expand Up @@ -509,7 +525,7 @@ public static List<ThingCount> Find(Pawn healer, Pawn patient, out int totalCoun
break;

usedCount = Mathf.Min(closeMed.thing.stackCount, count);
closeMed.DebugLog("Using: ({usedCount})");
closeMed.DebugLog($"Using: ({usedCount})");

result.Add(new ThingCount(closeMed.thing, usedCount));
count -= usedCount;
Expand Down

0 comments on commit 856e00e

Please sign in to comment.