rjw-patch-autopsy/Source/RJW_patch_Autopsy/1.5/Patches/NewMedicalRecipesUtilityPatch.cs
2025-07-10 14:44:48 +02:00

115 lines
No EOL
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
using rjw;
using Autopsy;
using HarmonyLib;
namespace RJW_patch_Autopsy
{
[HarmonyPatch(typeof(NewMedicalRecipesUtility), nameof(NewMedicalRecipesUtility.TraverseBody))]
public static class NewMedicalRecipesUtilityPatch
{
private const bool DEBUG = true;
private static void log(String message)
{
if (DEBUG)
{
Log.Message($"[RJW_Autopsy] {message}");
}
}
[HarmonyPostfix]
public static IEnumerable<Thing> Postfix(IEnumerable<Thing> vanillaParts, RecipeInfo recipeInfo, Corpse corpse,
float skillChance)
{
log("Starting AddRjwParts");
/*//Collect vanilla parts
var core = corpse.InnerPawn.RaceProps.body.corePart;
var queue = new List<BodyPartRecord> {core};
var hediffSet = corpse.InnerPawn.health.hediffSet;
var results = new List<Thing>();
var damagedParts = new List<BodyPartRecord>();
while (queue.Count > 0)
{
var part = queue.First();
queue.Remove(part);
//Drop parts and bionics that are higher on the body tree.
if (NewMedicalRecipesUtility.TryGetParts(corpse, recipeInfo, part, skillChance, ref results,
ref damagedParts) && core != part)
continue;
queue.AddRange(part.parts.Where(x => !hediffSet.PartIsMissing(x)));
}
foreach (var part in damagedParts)
NewMedicalRecipesUtility.DamageHarvested(corpse.InnerPawn, part);
*/
// 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($"Obtained natural part: {tmp} from hediff: {hediff}");
return tmp;
}).ToList();
var rjwArtificialThings = rjwArtificialDiffs.Select(hediff =>
{
var tmp = SexPartAdder.recipePartRemover(hediff);
log($"Obtained artificial part: {tmp} from hediff: {hediff}");
return tmp;
}).ToList();
log(
$"Collected {rjwNaturalThings.Count} things from {rjwNaturalDiffs.Count} natural and {rjwArtificialThings.Count} things from {rjwArtificialDiffs.Count} artificial hediffs");
var results = new HashSet<Thing>();
//Simulate success chance scaled with skill etc.
rjwNaturalThings.ForEach(t =>
{
if (DEBUG || Rand.Chance(Math.Min(skillChance, recipeInfo.NaturalChance))) 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(i => random.Next()).Take(recipeInfo.PartNumber).ToHashSet();
}
foreach (var result in results)
{
log(result.ToString());
yield return result;
}
foreach (var result in vanillaParts)
{
log(result.ToString());
yield return result;
}
// return false;
}
}
}