using RimWorld; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Verse; namespace RJWSexperience.Ideology.Filters { /// /// Filter to describe how one pawn sees another /// [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")] public class RelationFilter { public bool? isVeneratedAnimal; public List hasOneOfRelations; public List hasNoneOfRelations; public List hasOneOfRelationDegrees; private bool initialized = false; private HashSet hasOneOfRelationsHashed; private HashSet hasNoneOfRelationsHashed; private HashSet hasOneOfRelationDegreesHashed; /// /// Check if the pair of pawns fits filter conditions /// public bool Applies(Pawn pawn, Pawn partner) { // Fail if any single condition fails if (isVeneratedAnimal != null && isVeneratedAnimal != pawn.Ideo.IsVeneratedAnimal(partner)) return false; if (!CheckRelations(pawn, partner)) return false; return true; } private bool CheckRelations(Pawn pawn, Pawn partner) { if (!initialized) Initialize(); if (hasNoneOfRelationsHashed == null && hasOneOfRelationsHashed == null && hasOneOfRelationDegreesHashed == null) return true; if (hasOneOfRelationDegreesHashed != null && !hasOneOfRelationDegreesHashed.Contains(RelationHelpers.GetBloodRelationDegree(pawn, partner))) { return false; } IEnumerable relations = pawn.GetRelations(partner); if (hasOneOfRelationsHashed != null) { if (relations.EnumerableNullOrEmpty()) return false; if (!hasOneOfRelationsHashed.Overlaps(relations)) return false; } if (hasNoneOfRelationsHashed != null && !relations.EnumerableNullOrEmpty() && hasNoneOfRelationsHashed.Overlaps(relations)) { return false; } return true; } private void Initialize() { if (!hasNoneOfRelations.NullOrEmpty()) hasNoneOfRelationsHashed = new HashSet(hasNoneOfRelations); if (!hasOneOfRelations.NullOrEmpty()) hasOneOfRelationsHashed = new HashSet(hasOneOfRelations); if (!hasOneOfRelationDegrees.NullOrEmpty()) hasOneOfRelationDegreesHashed = new HashSet(hasOneOfRelationDegrees); initialized = true; } } }