Added isBloodRelated filter attribute

This commit is contained in:
amevarashi 2022-11-21 22:40:44 +05:00
parent 765e6c0778
commit 7d6f0d043c
8 changed files with 92 additions and 142 deletions

View file

@ -0,0 +1,84 @@
using RimWorld;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Verse;
namespace RJWSexperience.Ideology.Filters
{
/// <summary>
/// Filter to describe how one pawn sees another
/// </summary>
[SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")]
public class RelationFilter
{
public bool? isVeneratedAnimal;
public bool? isAlien;
public bool? isBloodRelated;
public List<PawnRelationDef> hasOneOfRelations;
public List<PawnRelationDef> hasNoneOfRelations;
private bool initialized = false;
private HashSet<PawnRelationDef> hasOneOfRelationsHashed;
private HashSet<PawnRelationDef> hasNoneOfRelationsHashed;
/// <summary>
/// Check if the pair of pawns fits filter conditions
/// </summary>
public bool Applies(Pawn pawn, Pawn partner)
{
// Fail if any single condition fails
if (isVeneratedAnimal != null && isVeneratedAnimal != pawn.Ideo.IsVeneratedAnimal(partner))
return false;
//if (isAlien != null && isAlien != 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 && isBloodRelated == null)
return true;
IEnumerable<PawnRelationDef> relations = pawn.GetRelations(partner);
if (isBloodRelated != null && isBloodRelated != relations.Any(def => def.familyByBloodRelation))
return false;
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<PawnRelationDef>(hasNoneOfRelations);
if (!hasOneOfRelations.NullOrEmpty())
hasOneOfRelationsHashed = new HashSet<PawnRelationDef>(hasOneOfRelations);
initialized = true;
}
}
}

View file

@ -0,0 +1,35 @@
using rjw;
using System.Diagnostics.CodeAnalysis;
using Verse;
namespace RJWSexperience.Ideology.Filters
{
/// <summary>
/// Filter to describe one pawn
/// </summary>
[SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")]
public class SinglePawnFilter
{
public bool? isAnimal;
public bool? isSlave;
public bool? isPrisoner;
/// <summary>
/// Check if pawn fits filter conditions
/// </summary>
public bool Applies(Pawn pawn)
{
// Fail if any single condition fails
if (isAnimal != null && isAnimal != pawn.IsAnimal())
return false;
if (isSlave != null && isSlave != pawn.IsSlave)
return false;
if (isPrisoner != null && isPrisoner != pawn.IsPrisoner)
return false;
return true;
}
}
}

View file

@ -0,0 +1,34 @@
using System.Diagnostics.CodeAnalysis;
using Verse;
namespace RJWSexperience.Ideology.Filters
{
/// <summary>
/// Filter to describe two pawns and relations between them
/// </summary>
[SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")]
public class TwoPawnFilter
{
public SinglePawnFilter doer;
public SinglePawnFilter partner;
public RelationFilter relations;
/// <summary>
/// Check if the pair of pawns fits filter conditions
/// </summary>
public bool Applies(Pawn pawn, Pawn partner)
{
// Fail if any single condition fails
if (doer?.Applies(pawn) == false)
return false;
if (this.partner?.Applies(partner) == false)
return false;
if (relations?.Applies(pawn, partner) == false)
return false;
return true;
}
}
}