rjw-patch-autopsy/Source/RJW_patch_Autopsy/Patches/NewMedicalRecipesUtilityPatch.cs

105 lines
4.1 KiB
C#
Raw Normal View History

2021-12-11 19:18:21 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
using rjw;
using Autopsy;
using HarmonyLib;
namespace RJW_patch_Autopsy
{
2022-01-23 21:06:32 +00:00
[HarmonyPatch(typeof(NewMedicalRecipesUtility), nameof(NewMedicalRecipesUtility.TraverseBody))]
2021-12-11 19:18:21 +00:00
public static class NewMedicalRecipesUtilityPatch
{
2022-09-12 16:26:15 +00:00
private const bool DEBUG = false;
2022-01-23 21:06:32 +00:00
private static void log(String message)
{
2022-09-12 16:26:15 +00:00
if (DEBUG)
2022-01-23 21:06:32 +00:00
{
Log.Message(message);
}
}
2022-01-23 20:28:34 +00:00
[HarmonyPostfix]
2022-01-23 21:06:32 +00:00
public static IEnumerable<Thing> AddRjwParts(IEnumerable<Thing> __result, RecipeInfo recipeInfo, Corpse corpse,
float skillChance)
2021-12-11 19:18:21 +00:00
{
2022-01-23 20:28:34 +00:00
/*//Collect vanilla parts
2021-12-18 01:32:26 +00:00
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)
{
2021-12-18 01:32:26 +00:00
var part = queue.First();
queue.Remove(part);
//Drop parts and bionics that are higher on the body tree.
2021-12-18 01:32:26 +00:00
if (NewMedicalRecipesUtility.TryGetParts(corpse, recipeInfo, part, skillChance, ref results,
ref damagedParts) && core != part)
continue;
queue.AddRange(part.parts.Where(x => !hediffSet.PartIsMissing(x)));
}
2021-12-18 01:32:26 +00:00
foreach (var part in damagedParts)
NewMedicalRecipesUtility.DamageHarvested(corpse.InnerPawn, part);
2022-01-23 20:28:34 +00:00
*/
var results = __result.ToList();
2022-01-23 21:06:32 +00:00
log($"Collected {results.Count} vanilla parts");
2021-12-11 19:18:21 +00:00
//Collect rjw rediffs
2021-12-18 01:32:26 +00:00
var rjwNaturalDiffs = (from x in corpse.InnerPawn.health.hediffSet.hediffs
where x is Hediff_PartBaseNatural
select x).ToList();
var rjwArtificialDiffs = (from x in corpse.InnerPawn.health.hediffSet.hediffs
where x is Hediff_PartBaseArtifical
select x).ToList();
2022-01-23 21:06:32 +00:00
log($"Collected {rjwNaturalDiffs.Count} natural and {rjwArtificialDiffs.Count} artificial hediffs");
2021-12-11 19:18:21 +00:00
//Collect parts from hediffs rjw's surgery methods
2022-01-23 20:28:34 +00:00
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();
2022-01-23 21:06:32 +00:00
log(
2022-01-23 20:28:34 +00:00
$"Collected {rjwNaturalThings.Count} things from {rjwNaturalDiffs.Count} natural and {rjwArtificialThings.Count} things from {rjwArtificialDiffs.Count} artificial hediffs");
2021-12-11 19:18:21 +00:00
//Simulate success chance scaled with skill etc.
2022-01-22 14:02:58 +00:00
rjwNaturalThings.ForEach(t =>
2021-12-18 01:32:26 +00:00
{
2022-09-12 16:26:15 +00:00
if (DEBUG || Rand.Chance(Math.Min(skillChance, recipeInfo.NaturalChance))) results.Add(t);
2021-12-18 01:32:26 +00:00
});
2022-01-22 14:02:58 +00:00
rjwArtificialThings.ForEach(t =>
2021-12-18 01:32:26 +00:00
{
2022-09-12 16:26:15 +00:00
if (DEBUG || Rand.Chance(Math.Min(skillChance, recipeInfo.BionicChance))) results.Add(t);
2021-12-18 01:32:26 +00:00
});
2021-12-11 19:18:21 +00:00
2022-01-22 14:02:58 +00:00
//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));
2022-01-22 14:02:58 +00:00
if (results.Count > recipeInfo.PartNumber)
{
2021-12-18 01:32:26 +00:00
var random = new Random();
2022-01-23 21:06:32 +00:00
results = results.OrderBy(i => random.Next()).Take(recipeInfo.PartNumber).ToList();
}
2022-01-23 21:06:32 +00:00
foreach (var result in results)
{
2022-01-23 21:06:32 +00:00
yield return result;
}
2022-01-23 20:28:34 +00:00
// return false;
2021-12-11 19:18:21 +00:00
}
}
2021-12-18 01:32:26 +00:00
}