2023-01-15 08:50:29 +00:00
using rjw ;
using System ;
2022-12-19 19:48:56 +00:00
using System.Collections.Generic ;
using System.Linq ;
using Verse ;
namespace RJW_BGS
{
2022-12-22 18:04:56 +00:00
public class RaceGeneDef_Helper
2022-12-19 19:48:56 +00:00
{
//code based on racegroupdefinternal which has a similar function
public static RaceGeneDef GetRaceGeneDefInternal ( Pawn pawn )
{
2022-12-22 18:04:56 +00:00
List < RaceGeneDef > Valids = ValidRaceGeneDefs ( pawn ) ;
if ( Valids . Count > 0 )
{
RaceGeneDef result = Valids . MaxBy ( r = > r . priority ) ;
return result ;
}
return null ;
}
2023-01-15 16:48:11 +00:00
2022-12-22 18:04:56 +00:00
public static List < RaceGeneDef > ValidRaceGeneDefs ( Pawn pawn )
{
2022-12-19 19:48:56 +00:00
PawnKindDef kindDef = pawn . kindDef ;
if ( kindDef = = null )
2022-12-22 18:04:56 +00:00
{
2023-01-15 16:48:11 +00:00
ModLog . Warning ( $"Error looking up PawnKindDef for {pawn.Name} - Could not lookup Animal Inheritance Genes" ) ;
2022-12-19 19:48:56 +00:00
return null ;
2022-12-22 18:04:56 +00:00
}
2023-01-27 20:30:30 +00:00
2022-12-19 19:48:56 +00:00
string raceName = kindDef . race . defName ;
string pawnKindName = kindDef . defName ;
2023-01-27 20:30:30 +00:00
//Wild animals have no name, so we will use pawnkindname instead
string pawnName = pawn . Name ! = null ? pawn . Name . ToStringFull : pawnKindName ;
2022-12-19 19:48:56 +00:00
PawnData pawnData = SaveStorage . DataStore . GetPawnData ( pawn ) ;
RaceGroupDef raceGroupDef = pawnData . RaceSupportDef ;
2022-12-22 18:04:56 +00:00
2023-01-15 16:48:11 +00:00
if ( RJW_BGSSettings . rjw_bgs_detailed_debug )
2023-01-27 20:30:30 +00:00
ModLog . Message ( $"Looking up Animal-Inheritable Genes for {pawnName} with KindDef {kindDef.defName},RaceName {raceName}, PawnKind {pawnKindName} and RaceGroup {raceGroupDef.defName}" ) ;
2023-01-15 16:48:11 +00:00
2022-12-22 18:04:56 +00:00
IEnumerable < RaceGeneDef > allDefs = DefDatabase < RaceGeneDef > . AllDefs ;
2022-12-19 19:48:56 +00:00
List < RaceGeneDef > pawnKindDefs = allDefs . Where ( delegate ( RaceGeneDef group )
{
List < string > pawnKindNames = group . pawnKindNames ;
return pawnKindNames ! = null & & pawnKindNames . Contains ( pawnKindName ) ;
} ) . ToList < RaceGeneDef > ( ) ;
2022-12-22 18:04:56 +00:00
if ( pawnKindDefs . Count ( ) > 0 )
2023-01-15 16:48:11 +00:00
{
2023-01-27 20:30:30 +00:00
DebugPrintRaceGeneDefs ( "PawnKindDefs" , pawnName , pawnKindDefs ) ;
2022-12-22 18:04:56 +00:00
return pawnKindDefs ;
2023-01-15 16:48:11 +00:00
}
else if ( RJW_BGSSettings . rjw_bgs_detailed_debug )
2023-01-27 20:30:30 +00:00
ModLog . Message ( $"Did not find PawnKindDefs for {pawnName}" ) ;
2023-01-15 08:50:29 +00:00
2022-12-19 19:48:56 +00:00
List < RaceGeneDef > raceKindDefs = allDefs . Where ( delegate ( RaceGeneDef group )
{
List < string > raceNames = group . raceNames ;
return raceNames ! = null & & raceNames . Contains ( raceName ) ;
} ) . ToList < RaceGeneDef > ( ) ;
2022-12-22 18:04:56 +00:00
if ( raceKindDefs . Count ( ) > 0 )
2023-01-15 16:48:11 +00:00
{
2023-01-27 20:30:30 +00:00
DebugPrintRaceGeneDefs ( "PawnKindDefs" , pawnName , raceKindDefs ) ;
2022-12-22 18:04:56 +00:00
return raceKindDefs ;
2023-01-15 16:48:11 +00:00
}
else if ( RJW_BGSSettings . rjw_bgs_detailed_debug )
2023-01-27 20:30:30 +00:00
ModLog . Message ( $"Did not find RaceKindDefs for {pawnName}" ) ;
2023-01-15 08:50:29 +00:00
2022-12-19 19:48:56 +00:00
List < RaceGeneDef > raceGroupDefs = new List < RaceGeneDef > ( ) ;
if ( raceGroupDef ! = null )
{
raceGroupDefs = allDefs . Where ( delegate ( RaceGeneDef group )
{
2023-01-15 12:30:33 +00:00
string raceGroupDefName = group . raceGroup ;
List < string > list_raceGroupDefName = group . raceGroups ;
return ( raceGroupDefName ! = null & & raceGroupDefName = = raceGroupDef . defName )
| | ( list_raceGroupDefName ! = null & & list_raceGroupDefName . Contains ( raceGroupDef . defName ) ) ;
2022-12-19 19:48:56 +00:00
} ) . ToList < RaceGeneDef > ( ) ;
}
2023-01-15 16:48:11 +00:00
2022-12-22 18:04:56 +00:00
if ( raceGroupDefs . Count ( ) > 0 )
2023-01-15 16:48:11 +00:00
{
2023-01-27 20:30:30 +00:00
DebugPrintRaceGeneDefs ( "RaceKindDefs" , pawnName , raceGroupDefs ) ;
2022-12-22 18:04:56 +00:00
return raceGroupDefs ;
2023-01-15 16:48:11 +00:00
}
else if ( RJW_BGSSettings . rjw_bgs_detailed_debug )
2023-01-27 20:30:30 +00:00
ModLog . Message ( $"Did not find RaceGroupDefs for {pawnName}" ) ;
2023-01-15 08:50:29 +00:00
2023-01-27 20:30:30 +00:00
ModLog . Message ( $"Did not find any Genes inheritable for {pawnName}" ) ;
2022-12-22 18:04:56 +00:00
return new List < RaceGeneDef > ( ) ;
2022-12-19 19:48:56 +00:00
}
2023-01-15 16:48:11 +00:00
private static void DebugPrintRaceGeneDefs ( String header , String identifier , List < RaceGeneDef > defs )
{
if ( RJW_BGSSettings . rjw_bgs_detailed_debug )
{
var defString = "[" ;
foreach ( RaceGeneDef raceGeneDef in defs )
defString + = $"({raceGeneDef.priority}:{raceGeneDef.defName} - {raceGeneDef.genes.Count} Genes)" ;
defString + = "]" ;
ModLog . Message ( $"Found the following {header}-Genes for {identifier}: {defString}" ) ;
}
}
2022-12-19 19:48:56 +00:00
}
}