126 lines
6.4 KiB
C#
126 lines
6.4 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
using Verse.AI;
|
||
|
|
|
||
|
|
namespace BlaxLaws
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// The group entries of each category, in picker order (design: "Act catalogue and detection", the Groups
|
||
|
|
/// column). Labels are Keyed "BlaxLaws_Group_<id without @>"; the "any" group of a category is
|
||
|
|
/// "BlaxLaws_Group_any<Category>", since one id serves several categories.
|
||
|
|
///
|
||
|
|
/// Def-level groups are expanded into Watch's set of watched defs. The meat groups that look into a meal's
|
||
|
|
/// ingredients read the thing (ActEvent.facts from hook C); their def check says which defs could match
|
||
|
|
/// (a meal could hold meat), which sets their size for "the smaller group wins".
|
||
|
|
/// </summary>
|
||
|
|
public static class CatalogueGroups
|
||
|
|
{
|
||
|
|
public static string LabelKey(string groupId) => "BlaxLaws_Group_" + (groupId.StartsWith(ActKey.GroupPrefix) ? groupId.Substring(1) : groupId);
|
||
|
|
|
||
|
|
public static string AnyLabelKey(string categoryId) => "BlaxLaws_Group_any" + categoryId;
|
||
|
|
|
||
|
|
/// <summary>Any break, any drug binge, any tantrum, any insulting spree, any aggressive break.</summary>
|
||
|
|
public static IEnumerable<ActGroup> Break(string cat)
|
||
|
|
{
|
||
|
|
yield return new CatalogueAnyGroup(cat, AnyLabelKey(cat));
|
||
|
|
yield return DefGroup(cat, ActGroups.AnyDrugBinge, d => StateIs(d, typeof(MentalState_BingingDrug)));
|
||
|
|
yield return DefGroup(cat, ActGroups.AnyTantrum, d => StateIs(d, typeof(MentalState_Tantrum)));
|
||
|
|
yield return DefGroup(cat, ActGroups.AnyInsultingSpree, d => StateIs(d, typeof(MentalState_InsultingSpree)));
|
||
|
|
yield return DefGroup(cat, ActGroups.AnyAggressive, d => d is MentalStateDef s && s.IsAggro);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Anything eaten (the food binge's link), the drug groups by ingestible.drugCategory, the meat groups,
|
||
|
|
/// raw food by preferability and any meal by the Meal food type.
|
||
|
|
/// </summary>
|
||
|
|
public static IEnumerable<ActGroup> Ate(string cat)
|
||
|
|
{
|
||
|
|
yield return new CatalogueAnyGroup(cat, AnyLabelKey(cat));
|
||
|
|
yield return DefGroup(cat, ActGroups.AnyDrug, d => d is ThingDef t && t.IsDrug);
|
||
|
|
yield return DefGroup(cat, ActGroups.HardDrugs, d => DrugInfo.CategoryOf(d as ThingDef) == DrugCategory.Hard);
|
||
|
|
yield return DefGroup(cat, ActGroups.SocialDrugs, d => DrugInfo.CategoryOf(d as ThingDef) == DrugCategory.Social);
|
||
|
|
yield return DefGroup(cat, ActGroups.MedicalDrugs, d => DrugInfo.CategoryOf(d as ThingDef) == DrugCategory.Medical);
|
||
|
|
yield return ThingGroup(cat, ActGroups.AnyMeat, ActFacts.Meat,
|
||
|
|
d => d is ThingDef t && (FoodUtility.GetFoodKind(t) == FoodKind.Meat || FoodFacts.HasIngredients(t)));
|
||
|
|
yield return DefGroup(cat, ActGroups.RawMeatOrCorpse, d => FoodFacts.IsRawMeatOrCorpse(d as ThingDef));
|
||
|
|
yield return ThingGroup(cat, ActGroups.HumanMeat, ActFacts.HumanMeat,
|
||
|
|
d => d is ThingDef t && (FoodFacts.SourceIsHumanlike(t) || FoodFacts.HasIngredients(t)));
|
||
|
|
yield return ThingGroup(cat, ActGroups.InsectMeat, ActFacts.InsectMeat,
|
||
|
|
d => d is ThingDef t && (FoodFacts.SourceIsInsect(t) || FoodFacts.HasIngredients(t)));
|
||
|
|
yield return DefGroup(cat, ActGroups.RawFood, d => FoodFacts.IsRawFood(d as ThingDef));
|
||
|
|
yield return DefGroup(cat, ActGroups.AnyMeal, d => FoodFacts.IsMeal(d as ThingDef));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Insult or slight: the two vanilla defs and any interaction whose worker is theirs or derives from it.</summary>
|
||
|
|
public static IEnumerable<ActGroup> Interaction(string cat)
|
||
|
|
{
|
||
|
|
yield return DefGroup(cat, ActGroups.InsultOrSlight, IsInsultOrSlight);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Any surgery (RecipeDef.IsSurgery, Verse/RecipeDef.cs:362).</summary>
|
||
|
|
public static IEnumerable<ActGroup> Bill(string cat)
|
||
|
|
{
|
||
|
|
yield return DefGroup(cat, ActGroups.AnySurgery, d => d is RecipeDef r && r.IsSurgery);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static bool IsInsultOrSlight(Def d)
|
||
|
|
{
|
||
|
|
if (!(d is InteractionDef i))
|
||
|
|
return false;
|
||
|
|
if (i == InteractionDefOf.Insult || i.defName == "Slight")
|
||
|
|
return true;
|
||
|
|
// workerClass is private; the worker is made once per def anyway (RimWorld/InteractionDef.cs:45-55).
|
||
|
|
InteractionWorker w = i.Worker;
|
||
|
|
return w is InteractionWorker_Insult || w is InteractionWorker_Slight;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool StateIs(Def d, Type stateClass) =>
|
||
|
|
d is MentalStateDef s && s.stateClass != null && stateClass.IsAssignableFrom(s.stateClass);
|
||
|
|
|
||
|
|
private static ActGroup DefGroup(string cat, string id, Func<Def, bool> pred) => new PredicateGroup(cat, id, LabelKey(id), pred);
|
||
|
|
|
||
|
|
private static ActGroup ThingGroup(string cat, string id, ActFacts fact, Func<Def, bool> could) =>
|
||
|
|
new PredicateGroup(cat, id, LabelKey(id), could, act => (FoodFacts.Of(act) & fact) != 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// A category's "any": every def entry of the category, and nothing the catalogue left out (an "any
|
||
|
|
/// break" row never catches a social fight or a manhunter state). Rank 1, the least specific row.
|
||
|
|
/// Membership is a set of the category's defs, rebuilt when the catalogue is.
|
||
|
|
/// </summary>
|
||
|
|
public sealed class CatalogueAnyGroup : ActGroup
|
||
|
|
{
|
||
|
|
private HashSet<Def> members;
|
||
|
|
private int membersVersion = -1;
|
||
|
|
|
||
|
|
public CatalogueAnyGroup(string categoryId, string labelKey)
|
||
|
|
{
|
||
|
|
this.categoryId = categoryId;
|
||
|
|
id = ActGroups.Any;
|
||
|
|
this.labelKey = labelKey;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override bool MatchesDef(Def def)
|
||
|
|
{
|
||
|
|
if (def == null)
|
||
|
|
return false;
|
||
|
|
if (members == null || membersVersion != ActCatalogue.Version)
|
||
|
|
{
|
||
|
|
var set = new HashSet<Def>();
|
||
|
|
foreach (ActEntry e in ActCatalogue.EntriesOf(categoryId))
|
||
|
|
{
|
||
|
|
if (e.def != null)
|
||
|
|
set.Add(e.def);
|
||
|
|
}
|
||
|
|
members = set;
|
||
|
|
membersVersion = ActCatalogue.Version;
|
||
|
|
}
|
||
|
|
return members.Contains(def);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override bool Matches(ActEvent act) => act?.def != null && (act.categoryId == null || act.categoryId == categoryId) && MatchesDef(act.def);
|
||
|
|
}
|
||
|
|
}
|