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
{
[HarmonyPatch(typeof(NewMedicalRecipesUtility), "TraverseBody")]
public static class NewMedicalRecipesUtilityPatch
{
2021-12-14 17:15:09 +00:00
[HarmonyPrefix]
public static bool AddRjwParts ( RecipeInfo recipeInfo , Corpse corpse , float skillChance , ref IEnumerable < Thing > __result )
2021-12-11 19:18:21 +00:00
{
2021-12-14 17:15:09 +00:00
//Collect vanilla parts
BodyPartRecord core = corpse . InnerPawn . RaceProps . body . corePart ;
List < BodyPartRecord > queue = new List < BodyPartRecord > { core } ;
HediffSet hediffSet = corpse . InnerPawn . health . hediffSet ;
List < Thing > results = new List < Thing > ( ) ;
List < BodyPartRecord > damagedParts = new List < BodyPartRecord > ( ) ;
while ( queue . Count > 0 )
{
BodyPartRecord 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 ( BodyPartRecord part in damagedParts ) NewMedicalRecipesUtility . DamageHarvested ( corpse . InnerPawn , part ) ;
2021-12-11 19:18:21 +00:00
//Collect rjw rediffs
2021-12-14 17:15:09 +00:00
List < Hediff > rjwNaturalDiffs = ( from x in corpse . InnerPawn . health . hediffSet . hediffs
where x is Hediff_PartBaseNatural
select x ) . ToList ( ) ;
List < Hediff > rjwArtificialDiffs = ( from x in corpse . InnerPawn . health . hediffSet . hediffs
where x is Hediff_PartBaseArtifical
select x ) . ToList ( ) ;
//Log.Message(String.Format("Collected {0} natural and {1} artifical hediffs", rjwNaturalDiffs.Count(), rjwArtificialDiffs.Count()));
2021-12-11 19:18:21 +00:00
//Collect parts from hediffs rjw's surgery methods
2021-12-14 17:15:09 +00:00
List < Thing > rjwNaturalThings = rjwNaturalDiffs . Select ( d = > SexPartAdder . recipePartRemover ( d ) ) . ToList ( ) ;
List < Thing > rjwArtificialThings = rjwArtificialDiffs . Select ( d = > SexPartAdder . recipePartRemover ( d ) ) . ToList ( ) ;
//Log.Message(String.Format("Collected {0} things from {1} natural and {2} things from {3} artifical hediffs", rjwArtificialThings.Count(), rjwNaturalDiffs.Count(), rjwArtificialThings.Count(), rjwArtificialDiffs.Count()));
2021-12-11 19:18:21 +00:00
//Simulate success chance scaled with skill etc.
2021-12-14 17:15:09 +00:00
rjwNaturalThings . ToList ( ) . ForEach ( t = > { if ( Rand . Chance ( Math . Min ( skillChance , recipeInfo . NaturalChance ) ) ) results . Add ( t ) ; } ) ;
rjwArtificialThings . ToList ( ) . ForEach ( t = > { if ( Rand . Chance ( Math . Min ( skillChance , recipeInfo . BionicChance ) ) ) results . Add ( t ) ; } ) ;
2021-12-11 19:18:21 +00:00
//Remove all parts that were tried to harves from the corpse
2021-12-14 17:15:09 +00:00
rjwNaturalDiffs . ToList ( ) . ForEach ( d = > corpse . InnerPawn . health . RemoveHediff ( d ) ) ;
rjwArtificialDiffs . ToList ( ) . ForEach ( d = > corpse . InnerPawn . health . RemoveHediff ( d ) ) ;
if ( results . Count ( ) > recipeInfo . PartNumber )
{
Random random = new Random ( ) ;
__result = results . OrderBy ( i = > random . Next ( ) ) . Take ( recipeInfo . PartNumber ) ;
}
else
{
__result = results ;
}
2021-12-11 19:18:21 +00:00
2021-12-14 17:15:09 +00:00
return false ;
2021-12-11 19:18:21 +00:00
}
}
}