From 7d6f0d043ce9da407c2e51e72512f14fb93c7017 Mon Sep 17 00:00:00 2001 From: amevarashi Date: Mon, 21 Nov 2022 22:40:44 +0500 Subject: [PATCH] Added isBloodRelated filter attribute --- 1.4/Defs/PreceptDefs/Precepts_Incest.xml | 95 +------------------ .../{ => Filters}/RelationFilter.cs | 21 ++-- .../{ => Filters}/SinglePawnFilter.cs | 13 ++- Source/IdeologyAddon/Filters/TwoPawnFilter.cs | 34 +++++++ .../HistoryEvents/TwoPawnEventRule.cs | 10 +- Source/IdeologyAddon/IdeologyAddon.csproj | 6 +- .../Precepts/DefExtension_ModifyPreference.cs | 25 +++-- Source/IdeologyAddon/TwoPawnFilter.cs | 30 ------ 8 files changed, 92 insertions(+), 142 deletions(-) rename Source/IdeologyAddon/{ => Filters}/RelationFilter.cs (73%) rename Source/IdeologyAddon/{ => Filters}/SinglePawnFilter.cs (50%) create mode 100644 Source/IdeologyAddon/Filters/TwoPawnFilter.cs delete mode 100644 Source/IdeologyAddon/TwoPawnFilter.cs diff --git a/1.4/Defs/PreceptDefs/Precepts_Incest.xml b/1.4/Defs/PreceptDefs/Precepts_Incest.xml index ddcc32a..97d5d0d 100644 --- a/1.4/Defs/PreceptDefs/Precepts_Incest.xml +++ b/1.4/Defs/PreceptDefs/Precepts_Incest.xml @@ -44,24 +44,7 @@
  • - -
  • Parent
  • -
  • Child
  • -
  • Sibling
  • -
  • HalfSibling
  • -
  • Grandparent
  • -
  • Grandchild
  • -
  • NephewOrNiece
  • -
  • UncleOrAunt
  • -
  • GreatGrandparent
  • -
  • GreatGrandchild
  • -
  • GranduncleOrGrandaunt
  • -
  • GrandnephewOrGrandniece
  • -
  • CousinOnceRemoved
  • -
  • SecondCousin
  • -
  • Cousin
  • -
  • Kin
  • - + true RSI_IncestuosMarriage @@ -107,24 +90,7 @@
  • - -
  • Parent
  • -
  • Child
  • -
  • Sibling
  • -
  • HalfSibling
  • -
  • Grandparent
  • -
  • Grandchild
  • -
  • NephewOrNiece
  • -
  • UncleOrAunt
  • -
  • GreatGrandparent
  • -
  • GreatGrandchild
  • -
  • GranduncleOrGrandaunt
  • -
  • GrandnephewOrGrandniece
  • -
  • CousinOnceRemoved
  • -
  • SecondCousin
  • -
  • Cousin
  • -
  • Kin
  • - + true RSI_IncestuosSex @@ -223,24 +189,7 @@
  • - -
  • Parent
  • -
  • Child
  • -
  • Sibling
  • -
  • HalfSibling
  • -
  • Grandparent
  • -
  • Grandchild
  • -
  • NephewOrNiece
  • -
  • UncleOrAunt
  • -
  • GreatGrandparent
  • -
  • GreatGrandchild
  • -
  • GranduncleOrGrandaunt
  • -
  • GrandnephewOrGrandniece
  • -
  • CousinOnceRemoved
  • -
  • SecondCousin
  • -
  • Cousin
  • -
  • Kin
  • - + true
  • Spouse
  • @@ -282,24 +231,7 @@
  • - -
  • Parent
  • -
  • Child
  • -
  • Sibling
  • -
  • HalfSibling
  • -
  • Grandparent
  • -
  • Grandchild
  • -
  • NephewOrNiece
  • -
  • UncleOrAunt
  • -
  • GreatGrandparent
  • -
  • GreatGrandchild
  • -
  • GranduncleOrGrandaunt
  • -
  • GrandnephewOrGrandniece
  • -
  • CousinOnceRemoved
  • -
  • SecondCousin
  • -
  • Cousin
  • -
  • Kin
  • - + true
  • Spouse
  • @@ -336,24 +268,7 @@
  • - -
  • Parent
  • -
  • Child
  • -
  • Sibling
  • -
  • HalfSibling
  • -
  • Grandparent
  • -
  • Grandchild
  • -
  • NephewOrNiece
  • -
  • UncleOrAunt
  • -
  • GreatGrandparent
  • -
  • GreatGrandchild
  • -
  • GranduncleOrGrandaunt
  • -
  • GrandnephewOrGrandniece
  • -
  • CousinOnceRemoved
  • -
  • SecondCousin
  • -
  • Cousin
  • -
  • Kin
  • - + true 2.0 diff --git a/Source/IdeologyAddon/RelationFilter.cs b/Source/IdeologyAddon/Filters/RelationFilter.cs similarity index 73% rename from Source/IdeologyAddon/RelationFilter.cs rename to Source/IdeologyAddon/Filters/RelationFilter.cs index cff5738..32fa1ed 100644 --- a/Source/IdeologyAddon/RelationFilter.cs +++ b/Source/IdeologyAddon/Filters/RelationFilter.cs @@ -1,27 +1,33 @@ using RimWorld; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; using Verse; -namespace RJWSexperience.Ideology +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 { - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public bool? isVeneratedAnimal; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public bool? isAlien; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] + public bool? isBloodRelated; public List hasOneOfRelations; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public List hasNoneOfRelations; private bool initialized = false; private HashSet hasOneOfRelationsHashed; private HashSet hasNoneOfRelationsHashed; + /// + /// 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; @@ -39,11 +45,14 @@ namespace RJWSexperience.Ideology if (!initialized) Initialize(); - if (hasNoneOfRelationsHashed == null && hasOneOfRelationsHashed == null) + if (hasNoneOfRelationsHashed == null && hasOneOfRelationsHashed == null && isBloodRelated == null) return true; IEnumerable relations = pawn.GetRelations(partner); + if (isBloodRelated != null && isBloodRelated != relations.Any(def => def.familyByBloodRelation)) + return false; + if (hasOneOfRelationsHashed != null) { if (relations.EnumerableNullOrEmpty()) diff --git a/Source/IdeologyAddon/SinglePawnFilter.cs b/Source/IdeologyAddon/Filters/SinglePawnFilter.cs similarity index 50% rename from Source/IdeologyAddon/SinglePawnFilter.cs rename to Source/IdeologyAddon/Filters/SinglePawnFilter.cs index daa240f..40e7a0a 100644 --- a/Source/IdeologyAddon/SinglePawnFilter.cs +++ b/Source/IdeologyAddon/Filters/SinglePawnFilter.cs @@ -2,19 +2,24 @@ using System.Diagnostics.CodeAnalysis; using Verse; -namespace RJWSexperience.Ideology +namespace RJWSexperience.Ideology.Filters { + /// + /// Filter to describe one pawn + /// + [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")] public class SinglePawnFilter { - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public bool? isAnimal; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public bool? isSlave; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public bool? isPrisoner; + /// + /// Check if pawn fits filter conditions + /// public bool Applies(Pawn pawn) { + // Fail if any single condition fails if (isAnimal != null && isAnimal != pawn.IsAnimal()) return false; diff --git a/Source/IdeologyAddon/Filters/TwoPawnFilter.cs b/Source/IdeologyAddon/Filters/TwoPawnFilter.cs new file mode 100644 index 0000000..c0580b0 --- /dev/null +++ b/Source/IdeologyAddon/Filters/TwoPawnFilter.cs @@ -0,0 +1,34 @@ +using System.Diagnostics.CodeAnalysis; +using Verse; + +namespace RJWSexperience.Ideology.Filters +{ + /// + /// Filter to describe two pawns and relations between them + /// + [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; + + /// + /// Check if the pair of pawns fits filter conditions + /// + 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; + } + } +} diff --git a/Source/IdeologyAddon/HistoryEvents/TwoPawnEventRule.cs b/Source/IdeologyAddon/HistoryEvents/TwoPawnEventRule.cs index dc61b89..9c9e3f1 100644 --- a/Source/IdeologyAddon/HistoryEvents/TwoPawnEventRule.cs +++ b/Source/IdeologyAddon/HistoryEvents/TwoPawnEventRule.cs @@ -1,16 +1,22 @@ using RimWorld; +using RJWSexperience.Ideology.Filters; using System.Diagnostics.CodeAnalysis; using Verse; namespace RJWSexperience.Ideology.HistoryEvents { + /// + /// Type to associate single HistoryEventDef with a TwoPawnFilter + /// + [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")] public class TwoPawnEventRule { - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public HistoryEventDef historyEventDef; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public TwoPawnFilter filter; + /// + /// Check if the pair of pawns fits filter conditions + /// public bool Applies(Pawn pawn, Pawn partner) => filter?.Applies(pawn, partner) == true; } } diff --git a/Source/IdeologyAddon/IdeologyAddon.csproj b/Source/IdeologyAddon/IdeologyAddon.csproj index 0636e40..d23b8ae 100644 --- a/Source/IdeologyAddon/IdeologyAddon.csproj +++ b/Source/IdeologyAddon/IdeologyAddon.csproj @@ -59,13 +59,13 @@ - + - - + + diff --git a/Source/IdeologyAddon/Precepts/DefExtension_ModifyPreference.cs b/Source/IdeologyAddon/Precepts/DefExtension_ModifyPreference.cs index 698bda1..19689bb 100644 --- a/Source/IdeologyAddon/Precepts/DefExtension_ModifyPreference.cs +++ b/Source/IdeologyAddon/Precepts/DefExtension_ModifyPreference.cs @@ -1,30 +1,41 @@ -using System.Collections.Generic; +using RJWSexperience.Ideology.Filters; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; using Verse; namespace RJWSexperience.Ideology.Precepts { + /// + /// Def extension to enable changing SexAppraiser results based on filters + /// + [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Def loader")] public class DefExtension_ModifyPreference : DefModExtension { - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public List rules; + /// + /// Apply SexAppraiser modifiers from rules with a satisfied filter + /// public void Apply(Pawn pawn, Pawn partner, ref float preference) { - foreach (Rule rule in rules) + foreach (Rule rule in rules.Where(rule => rule.Applies(pawn, partner))) { - if (rule.Applies(pawn, partner)) - preference *= rule.multiplier; + preference *= rule.multiplier; } } + /// + /// Type to associate SexAppraiser result modifier with a TwoPawnFilter + /// public class Rule { - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public float multiplier = 1f; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public TwoPawnFilter filter; + /// + /// Check if the pair of pawns fits filter conditions + /// public bool Applies(Pawn pawn, Pawn partner) { if (filter == null) diff --git a/Source/IdeologyAddon/TwoPawnFilter.cs b/Source/IdeologyAddon/TwoPawnFilter.cs deleted file mode 100644 index 7c49f92..0000000 --- a/Source/IdeologyAddon/TwoPawnFilter.cs +++ /dev/null @@ -1,30 +0,0 @@ -using rjw; -using System.Diagnostics.CodeAnalysis; -using Verse; - -namespace RJWSexperience.Ideology -{ - public class TwoPawnFilter - { - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] - public SinglePawnFilter doer; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] - public SinglePawnFilter partner; - [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] - public RelationFilter relations; - - public bool Applies(Pawn pawn, Pawn partner) - { - 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; - } - } -}