rjw-patch-autopsy/Source/RJW_patch_Autopsy/1.5/Patches/NewMedicalRecipesUtilityPatch.cs
2025-09-05 21:23:19 +02:00

89 lines
No EOL
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
using rjw;
using Autopsy;
using HarmonyLib;
using RimWorld;
namespace RJW_patch_Autopsy
{
[HarmonyPatch(typeof(NewMedicalRecipesUtility), nameof(NewMedicalRecipesUtility.TraverseBody))]
public static class NewMedicalRecipesUtilityPatch
{
private const bool DEBUG = false;
private static void log(String message)
{
if (DEBUG)
{
Log.Message(message);
}
}
[HarmonyPostfix]
public static IEnumerable<Thing> AddRjwParts(IEnumerable<Thing> __result, RecipeInfo recipeInfo, Corpse corpse,
float skillChance)
{
var results = __result.ToList();
log($"Collected {results.Count} vanilla parts");
//Collect rjw rediffs
var rjwNaturalDiffs = (from x in corpse.InnerPawn.health.hediffSet.hediffs
where x is Hediff_NaturalSexPart
select x).ToList();
var rjwArtificialDiffs = (from x in corpse.InnerPawn.health.hediffSet.hediffs
where x is Hediff_ArtificialSexPart
select x).ToList();
log($"Collected {rjwNaturalDiffs.Count} natural and {rjwArtificialDiffs.Count} artificial hediffs");
//Collect parts from hediffs rjw's surgery methods
var rjwNaturalThings = rjwNaturalDiffs.Select(hediff =>
{
var tmp = SexPartAdder.recipePartRemover(hediff);
Log.Message($"obtained ${tmp} from ${hediff} via rjw");
return tmp;
}).ToList();
var rjwArtificialThings = rjwArtificialDiffs.Select(hediff =>
{
var tmp = SexPartAdder.recipePartRemover(hediff);
Log.Message($"obtained ${tmp} from ${hediff} via rjw");
return tmp;
}).ToList();
log(
$"Collected {rjwNaturalThings.Count} things from {rjwNaturalDiffs.Count} natural and {rjwArtificialThings.Count} things from {rjwArtificialDiffs.Count} artificial hediffs");
//Simulate success chance scaled with skill etc.
rjwNaturalThings.ForEach(t =>
{
CompRottable rot = corpse.TryGetComp<CompRottable>();
if (DEBUG || rot == null
? corpse.Age <= recipeInfo.CorpseValidAge
: rot.RotProgress + (corpse.Age - rot.RotProgress) * recipeInfo.FrozenDecay <=
recipeInfo.CorpseValidAge) results.Add(t);
});
rjwArtificialThings.ForEach(t =>
{
if (DEBUG || Rand.Chance(Math.Min(skillChance, recipeInfo.BionicChance))) results.Add(t);
});
//Remove all parts that were tried to harvest from the corpse
rjwNaturalDiffs.ForEach(d => corpse.InnerPawn.health.RemoveHediff(d));
rjwArtificialDiffs.ForEach(d => corpse.InnerPawn.health.RemoveHediff(d));
if (results.Count > recipeInfo.PartNumber)
{
var random = new Random();
results = results.OrderBy(_ => random.Next()).Take(recipeInfo.PartNumber).ToList();
}
foreach (var result in results)
{
yield return result;
}
}
}
}