Skip to content

Commit

Permalink
Updates for 1.0
Browse files Browse the repository at this point in the history
More to come;
  • Loading branch information
alextd committed Jun 19, 2018
1 parent 8af15e8 commit efc625d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
23 changes: 9 additions & 14 deletions Source/MedicineGrabbing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static bool IsUrgent(this Hediff h)
return !(h is Hediff_Injury)
|| (h as Hediff_Injury).Bleeding
|| (h as Hediff_Injury).TryGetComp<HediffComp_Infecter>() != null
|| (h as Hediff_Injury).TryGetComp<HediffComp_GetsOld>() != null;
|| (h as Hediff_Injury).TryGetComp<HediffComp_GetsPermanent>() != null;
}
}

Expand Down Expand Up @@ -194,11 +194,9 @@ static class MakeNewToils_Patch
//Insert FilterForUrgentHediffs when counting needed medicine
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
LocalBuilder localCountInfo = generator.DeclareLocal(typeof(int));
LocalBuilder localJobInfo = generator.DeclareLocal(typeof(Job));

MethodInfo GetMedicineCountToFullyHealInfo = AccessTools.Method(
typeof(Medicine), nameof(Medicine.GetMedicineCountToFullyHeal));
FieldInfo medCountInfo = AccessTools.Field(typeof(JobOnThing_Patch), "medCount");

FieldInfo jobFieldInfo = AccessTools.Field(
typeof(JobDriver), nameof(JobDriver.job));
FieldInfo jobCountInfo = AccessTools.Field(
Expand All @@ -210,11 +208,6 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
foreach (CodeInstruction i in instructions)
{
yield return i;
if (i.opcode == OpCodes.Call && i.operand == GetMedicineCountToFullyHealInfo)
{
yield return new CodeInstruction(OpCodes.Stloc, localCountInfo);
yield return new CodeInstruction(OpCodes.Ldloc, localCountInfo);
}
if (i.opcode == OpCodes.Ldfld && i.operand == jobFieldInfo)
{
yield return new CodeInstruction(OpCodes.Stloc, localJobInfo);
Expand All @@ -223,7 +216,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
if (i.opcode == OpCodes.Call && i.operand == JumpToToilInfo)
{
yield return new CodeInstruction(OpCodes.Ldloc, localJobInfo);
yield return new CodeInstruction(OpCodes.Ldloc, localCountInfo);
yield return new CodeInstruction(OpCodes.Ldsfld, medCountInfo);
yield return new CodeInstruction(OpCodes.Stfld, jobCountInfo);
}
}
Expand All @@ -237,7 +230,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
[StaticConstructorOnStartup]
public static class FindBestMedicine
{
public const int maxPawns = 100;
public const int maxPawns = 10;
struct MedicineEvaluator : IComparable
{
public Thing thing;
Expand Down Expand Up @@ -282,9 +275,11 @@ static FindBestMedicine()
maxMedicineQuality = medQualities.Max();
minMedicineQuality = medQualities.Min();
}

//FindBestMedicine Replacement
private static bool Prefix(Pawn healer, Pawn patient, ref Thing __result)
{
if (patient.playerSettings == null || patient.playerSettings.medCare <= MedicalCareCategory.NoMeds ||
if (patient.playerSettings == null || patient.playerSettings.medCare <= MedicalCareCategory.NoMeds || Medicine.GetMedicineCountToFullyHeal(patient) <= 0 ||
!healer.Faction.IsPlayer)
return true;

Expand All @@ -298,7 +293,7 @@ private static bool Prefix(Pawn healer, Pawn patient, ref Thing __result)
}
if (Settings.Get().minimalMedicineForNonUrgent)
{
if (patient.health.hediffSet.hediffs.All(h => !h.TendableNow || !h.IsUrgent()))
if (patient.health.hediffSet.hediffs.All(h => !h.TendableNow() || !h.IsUrgent()))
{
sufficientQuality = minMedicineQuality;
Log.Message("Sufficient medicine for non-urgent care is " + sufficientQuality);
Expand Down
2 changes: 1 addition & 1 deletion Source/SmartMedicine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Mod(ModContentPack content) : base(content)
harmony.PatchAll(Assembly.GetExecutingAssembly());

{
Type nestedType = AccessTools.Inner(typeof(Toils_Tend), "<PickupMedicine>c__AnonStorey0");
Type nestedType = AccessTools.Inner(typeof(Toils_Tend), "<PickupMedicine>c__AnonStorey1");
harmony.Patch(AccessTools.Method(nestedType, "<>m__0"),
null, null, new HarmonyMethod(typeof(PickupMedicine_Patch), "Transpiler"));
}
Expand Down
28 changes: 11 additions & 17 deletions Source/StockUp/DontDropStockedDrugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,28 @@ namespace SmartMedicine.StockUp
[HarmonyPatch(typeof(JobGiver_DropUnusedInventory), "TryGiveJob")]
public static class DontDropStockedDrugs
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator il, MethodBase mb)
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo IsDrugInfo = AccessTools.Property(typeof(ThingDef), "IsDrug").GetGetMethod();

bool foundDrugInfo = false;
bool nextLineIsBranch = false;

MethodInfo StockingUpInfo = AccessTools.Method(typeof(StockUpUtility), nameof(StockUpUtility.StockingUpOn),
new Type[] { typeof(Pawn), typeof(Thing)});
new Type[] { typeof(Pawn), typeof(Thing) });

foreach (CodeInstruction i in instructions)
List<CodeInstruction> instList = instructions.ToList();
for (int i = 0; i < instList.Count(); i++)
{
yield return i;
CodeInstruction inst = instList[i];
yield return inst;

if (i.opcode == OpCodes.Callvirt && i.operand == IsDrugInfo)
{
if (!foundDrugInfo) foundDrugInfo = true;
else nextLineIsBranch = true;
}
else if (i.opcode == OpCodes.Brfalse && nextLineIsBranch)
if (inst.opcode == OpCodes.Brfalse
&& instList[i - 1].opcode == OpCodes.Callvirt && instList[i - 1].operand == IsDrugInfo)
{
yield return new CodeInstruction(OpCodes.Ldarg_1);//pawn
yield return new CodeInstruction(OpCodes.Ldloc_3);//thing
yield return new CodeInstruction(OpCodes.Ldloc_S, instList[i-3].operand);//thing
yield return new CodeInstruction(OpCodes.Call, StockingUpInfo);//pawn.StockingUpOn(thing)
yield return new CodeInstruction(OpCodes.Brtrue, i.operand);
nextLineIsBranch = false;
yield return new CodeInstruction(OpCodes.Brtrue, inst.operand);
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion Source/StockUp/JobGiver_StockUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override Job TryGiveJob(Pawn pawn)
if (pawn.StockUpIsFull()) return null;

Log.Message("Skip need tend?");
if (pawn.Map.mapPawns.AllPawnsSpawned.Any(p => HealthAIUtility.ShouldBeTendedNow(p) && pawn.CanReserveAndReach(p, PathEndMode.ClosestTouch, Danger.Deadly)))
if (pawn.Map.mapPawns.AllPawnsSpawned.Any(p => HealthAIUtility.ShouldBeTendedNowByPlayer(p) && pawn.CanReserveAndReach(p, PathEndMode.ClosestTouch, Danger.Deadly)))
return null;

Log.Message("any things?");
Expand Down
7 changes: 3 additions & 4 deletions Source/StockUp/StockUpGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ public static void DrawStockUpButton(Pawn pawn, ref float y, float width)
iconRect.x = rect.x - 28f;
if (StockUpUtility.CopiedPawn() == pawn)
{
TooltipHandler.TipRegion(iconRect, String.Format("TD.CancelCopyStockUp".Translate(), pawn.NameStringShort));
TooltipHandler.TipRegion(iconRect, String.Format("TD.CancelCopyStockUp".Translate(), pawn.Name.ToStringShort));
if (Widgets.ButtonImage(iconRect, TexButton.Ignore))
StockUpUtility.StockUpCopySettings(null);
}
else
{
if (StockUpUtility.CopiedPawn() != null)
{
TooltipHandler.TipRegion(iconRect, String.Format("TD.CopyStockUpFrom".Translate(), StockUpUtility.CopiedPawn().NameStringShort));
TooltipHandler.TipRegion(iconRect, String.Format("TD.CopyStockUpFrom".Translate(), StockUpUtility.CopiedPawn().Name.ToStringShort));
if (Widgets.ButtonImage(iconRect, TexButton.Paste))
pawn.StockUpPasteSettings();
}
Expand Down Expand Up @@ -332,9 +332,8 @@ public override Vector2 InitialSize
public Dialog_StockUp(Pawn p)
{
pawn = p;
title = String.Format("TD.StockUpSettingsForPawn".Translate(), p.NameStringShort);
title = String.Format("TD.StockUpSettingsForPawn".Translate(), p.Name.ToStringShort);
//absorbInputAroundWindow = true;
closeOnEscapeKey = true;
doCloseX = true;
draggable = true;
preventCameraMotion = false;
Expand Down

0 comments on commit efc625d

Please sign in to comment.