80 lines
3.4 KiB
C#
80 lines
3.4 KiB
C#
using System;
|
|
using Verse;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>
|
|
/// A group entry of a category ("Any drug binge", "Hard drugs", "Human meat", "Any surgery"). Groups are
|
|
/// predicates over defs, written once in code by the catalogue package; Watch expands a def-level group
|
|
/// into the set of defs it covers when the lawbook changes. A group that depends on the thing rather
|
|
/// than its def (a meal with human meat in it) sets DependsOnThing and overrides Matches(ActEvent); its
|
|
/// check then runs on every act of the category while the category is watched.
|
|
/// </summary>
|
|
public abstract class ActGroup
|
|
{
|
|
/// <summary>The category id.</summary>
|
|
public string categoryId;
|
|
|
|
/// <summary>The group id with its "@" (ActGroups), unique within the category.</summary>
|
|
public string id;
|
|
|
|
/// <summary>The Keyed key of the label ("BlaxLaws_Group_hardDrugs").</summary>
|
|
public string labelKey;
|
|
|
|
/// <summary>
|
|
/// Def entries of the category this group covers, set by ActCatalogue.Build. "The smaller group wins"
|
|
/// when two groups match one act.
|
|
/// </summary>
|
|
public int size;
|
|
|
|
public string Key => ActKey.OfGroup(categoryId, id);
|
|
|
|
/// <summary>The category's "any": every entry. The least specific row.</summary>
|
|
public bool IsAny => id == ActGroups.Any;
|
|
|
|
/// <summary>The check needs the thing itself (ActEvent.facts or thing), not only the def.</summary>
|
|
public virtual bool DependsOnThing => false;
|
|
|
|
public virtual string Label => labelKey.NullOrEmpty() ? id : labelKey.Translate().ToString();
|
|
|
|
/// <summary>
|
|
/// Whether a def of the category belongs to the group. For a DependsOnThing group: whether a thing of
|
|
/// this def could (a meal could hold meat); the act check decides.
|
|
/// </summary>
|
|
public abstract bool MatchesDef(Def def);
|
|
|
|
/// <summary>Whether the act belongs to the group. Default: its def does.</summary>
|
|
public virtual bool Matches(ActEvent act) => IsAny || (act?.def != null && MatchesDef(act.def));
|
|
}
|
|
|
|
/// <summary>A group from two predicates, for the catalogue package's group lists.</summary>
|
|
public sealed class PredicateGroup : ActGroup
|
|
{
|
|
private readonly Func<Def, bool> defPredicate;
|
|
private readonly Func<ActEvent, bool> actPredicate;
|
|
|
|
/// <param name="actPredicate">Null for a def-level group; set for a group that reads the thing.</param>
|
|
public PredicateGroup(string categoryId, string id, string labelKey, Func<Def, bool> defPredicate, Func<ActEvent, bool> actPredicate = null)
|
|
{
|
|
this.categoryId = categoryId;
|
|
this.id = id.StartsWith(ActKey.GroupPrefix) ? id : ActKey.GroupPrefix + id;
|
|
this.labelKey = labelKey;
|
|
this.defPredicate = defPredicate ?? (_ => true);
|
|
this.actPredicate = actPredicate;
|
|
}
|
|
|
|
public override bool DependsOnThing => actPredicate != null;
|
|
|
|
public override bool MatchesDef(Def def) => def != null && defPredicate(def);
|
|
|
|
public override bool Matches(ActEvent act)
|
|
{
|
|
if (IsAny)
|
|
return true;
|
|
if (act == null)
|
|
return false;
|
|
return actPredicate != null ? actPredicate(act) : act.def != null && defPredicate(act.def);
|
|
}
|
|
}
|
|
}
|