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-01-23 21:06:32 +00:00
private const bool DO_LOG = false ;
private static void log ( String message )
{
if ( DO_LOG )
{
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 > ( ) ;
2021-12-14 17:15:09 +00:00
while ( queue . Count > 0 )
{
2021-12-18 01:32:26 +00:00
var part = queue . First ( ) ;
2021-12-14 17:15:09 +00:00
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 )
2021-12-14 17:15:09 +00:00
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 ( ) ;
2021-12-14 17:15:09 +00:00
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 ( ) ;
2021-12-14 17:15:09 +00:00
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
{
if ( Rand . Chance ( Math . Min ( skillChance , recipeInfo . NaturalChance ) ) ) results . Add ( t ) ;
} ) ;
2022-01-22 14:02:58 +00:00
rjwArtificialThings . ForEach ( t = >
2021-12-18 01:32:26 +00:00
{
if ( Rand . Chance ( Math . Min ( skillChance , recipeInfo . BionicChance ) ) ) results . Add ( t ) ;
} ) ;
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 ) ) ;
2021-12-14 17:15:09 +00:00
2022-01-22 14:02:58 +00:00
if ( results . Count > recipeInfo . PartNumber )
2021-12-14 17:15:09 +00:00
{
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 ( ) ;
2021-12-14 17:15:09 +00:00
}
2022-01-23 21:06:32 +00:00
foreach ( var result in results )
2021-12-14 17:15:09 +00:00
{
2022-01-23 21:06:32 +00:00
yield return result ;
2021-12-14 17:15:09 +00:00
}
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
}