using rjw; using System; using System.Collections.Generic; using System.Linq; using Verse; namespace RJW_BGS { public class RaceGeneDef_Helper { //code based on racegroupdefinternal which has a similar function public static RaceGeneDef GetRaceGeneDefInternal(Pawn pawn) { List Valids = ValidRaceGeneDefs(pawn); if (Valids.Count > 0) { RaceGeneDef result = Valids.MaxBy(r => r.priority); return result; } return null; } public static List ValidRaceGeneDefs(Pawn pawn) { PawnKindDef kindDef = pawn.kindDef; if (kindDef == null) { ModLog.Warning($"Error looking up PawnKindDef for {pawn.Name} - Could not lookup Animal Inheritance Genes"); return null; } string raceName = kindDef.race.defName; string pawnKindName = kindDef.defName; //Wild animals have no name, so we will use pawnkindname instead string pawnName = pawn.Name != null ? pawn.Name.ToStringFull : pawnKindName; PawnData pawnData = SaveStorage.DataStore.GetPawnData(pawn); RaceGroupDef raceGroupDef = pawnData.RaceSupportDef; if (RJW_BGSSettings.rjw_bgs_detailed_debug) ModLog.Message($"Looking up Animal-Inheritable Genes for {pawnName} with KindDef {kindDef.defName},RaceName {raceName}, PawnKind {pawnKindName} and RaceGroup {raceGroupDef.defName}"); IEnumerable allDefs = DefDatabase.AllDefs; List pawnKindDefs = allDefs.Where(delegate (RaceGeneDef group) { List pawnKindNames = group.pawnKindNames; return pawnKindNames != null && pawnKindNames.Contains(pawnKindName); }).ToList(); if (pawnKindDefs.Count() > 0) { DebugPrintRaceGeneDefs("PawnKindDefs", pawnName,pawnKindDefs); return pawnKindDefs; } else if (RJW_BGSSettings.rjw_bgs_detailed_debug) ModLog.Message($"Did not find PawnKindDefs for {pawnName}"); List raceKindDefs = allDefs.Where(delegate (RaceGeneDef group) { List raceNames = group.raceNames; return raceNames != null && raceNames.Contains(raceName); }).ToList(); if (raceKindDefs.Count() > 0) { DebugPrintRaceGeneDefs("PawnKindDefs", pawnName, raceKindDefs); return raceKindDefs; } else if (RJW_BGSSettings.rjw_bgs_detailed_debug) ModLog.Message($"Did not find RaceKindDefs for {pawnName}"); List raceGroupDefs = new List(); if (raceGroupDef != null) { raceGroupDefs = allDefs.Where(delegate (RaceGeneDef group) { string raceGroupDefName = group.raceGroup; List list_raceGroupDefName = group.raceGroups; return (raceGroupDefName != null && raceGroupDefName == raceGroupDef.defName) || (list_raceGroupDefName != null && list_raceGroupDefName.Contains(raceGroupDef.defName)); }).ToList(); } if (raceGroupDefs.Count() > 0) { DebugPrintRaceGeneDefs("RaceKindDefs", pawnName, raceGroupDefs); return raceGroupDefs; } else if (RJW_BGSSettings.rjw_bgs_detailed_debug) ModLog.Message($"Did not find RaceGroupDefs for {pawnName}"); ModLog.Message($"Did not find any Genes inheritable for {pawnName}"); return new List(); } private static void DebugPrintRaceGeneDefs(String header,String identifier,List 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}"); } } } }