231 lines
8.8 KiB
C#
231 lines
8.8 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using Verse;
|
||
|
|
using Verse.AI;
|
||
|
|
|
||
|
|
namespace BlaxLaws
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// A category of the act catalogue (Defs/BlaxLaws_Categories.xml). The defName is the category id, the
|
||
|
|
/// first part of every act key, so it never changes. The worker reads the loaded defs once and returns
|
||
|
|
/// the category's entries.
|
||
|
|
/// </summary>
|
||
|
|
public class ActCategoryDef : Def
|
||
|
|
{
|
||
|
|
public Type workerClass = typeof(ActCategoryWorker);
|
||
|
|
|
||
|
|
/// <summary>Picker order; lower first.</summary>
|
||
|
|
public int order;
|
||
|
|
|
||
|
|
/// <summary>Def names the worker adds beyond its rule (Berserk for the breaks).</summary>
|
||
|
|
public List<string> include = new List<string>();
|
||
|
|
|
||
|
|
/// <summary>Def names the worker leaves out (animal and baby states, faction-level events).</summary>
|
||
|
|
public List<string> exclude = new List<string>();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Break category only: mental state classes hostile to the colony, subclasses included (berserk). Their
|
||
|
|
/// entries never offer Subdue. Enforcement re-checks ForceHostileTo(Faction.OfPlayer) on the live
|
||
|
|
/// state, which wins over this list.
|
||
|
|
/// </summary>
|
||
|
|
public List<Type> hostileStateClasses = new List<Type>();
|
||
|
|
|
||
|
|
[Unsaved]
|
||
|
|
private ActCategoryWorker workerInt;
|
||
|
|
|
||
|
|
public ActCategoryWorker Worker
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (workerInt == null)
|
||
|
|
{
|
||
|
|
workerInt = (ActCategoryWorker)Activator.CreateInstance(workerClass ?? typeof(ActCategoryWorker));
|
||
|
|
workerInt.def = this;
|
||
|
|
}
|
||
|
|
return workerInt;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool Includes(string defName) => include != null && include.Contains(defName);
|
||
|
|
|
||
|
|
public bool Excludes(string defName) => exclude != null && exclude.Contains(defName);
|
||
|
|
|
||
|
|
public override IEnumerable<string> ConfigErrors()
|
||
|
|
{
|
||
|
|
foreach (string e in base.ConfigErrors())
|
||
|
|
yield return e;
|
||
|
|
if (workerClass == null || !typeof(ActCategoryWorker).IsAssignableFrom(workerClass))
|
||
|
|
yield return "workerClass must be an ActCategoryWorker";
|
||
|
|
if (defName.IndexOf(ActKey.Separator) >= 0)
|
||
|
|
yield return "defName must not contain '" + ActKey.Separator + "': it is the first part of an act key";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// A special act with its own hook (Defs/BlaxLaws_Acts.xml): SetFire, DisplayedCorpse, StartedSocialFight in
|
||
|
|
/// the first release. The defName is the act's id in "Special/<defName>", so it never changes. Its
|
||
|
|
/// hook files it with an ActEvent whose def is this ActDef.
|
||
|
|
/// </summary>
|
||
|
|
public class ActDef : Def
|
||
|
|
{
|
||
|
|
public Type workerClass = typeof(ActWorker);
|
||
|
|
|
||
|
|
/// <summary>Picker order inside "Special acts"; lower first, then A to Z.</summary>
|
||
|
|
public int order;
|
||
|
|
|
||
|
|
[Unsaved]
|
||
|
|
private ActWorker workerInt;
|
||
|
|
|
||
|
|
public ActWorker Worker
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (workerInt == null)
|
||
|
|
{
|
||
|
|
workerInt = (ActWorker)Activator.CreateInstance(workerClass ?? typeof(ActWorker));
|
||
|
|
workerInt.def = this;
|
||
|
|
}
|
||
|
|
return workerInt;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public string Key => ActKey.Of(ActCategories.Special, defName);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Behaviour of one special act beyond its hook. The first release needs none; the hook files the act.
|
||
|
|
/// Later acts (the violent ones, the uprisings) override what they need.
|
||
|
|
/// </summary>
|
||
|
|
public class ActWorker
|
||
|
|
{
|
||
|
|
public ActDef def;
|
||
|
|
|
||
|
|
/// <summary>The tooltip the lawbook row shows, saying who counts as the offender. Default: the def's description.</summary>
|
||
|
|
public virtual string RowTooltip => def?.description;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// A break's "Did it" (Defs/BlaxLaws_BreakLinks.xml): the act that counts, for a mental state class and its
|
||
|
|
/// subclasses. Another mod links its own break class with one of these and no code.
|
||
|
|
/// </summary>
|
||
|
|
public class BreakLinkDef : Def
|
||
|
|
{
|
||
|
|
/// <summary>The mental state class, subclasses included.</summary>
|
||
|
|
public Type stateClass;
|
||
|
|
|
||
|
|
/// <summary>The linked act's key in another category: a single def ("Special/SetFire") or a group ("Ate/@anyDrug").</summary>
|
||
|
|
public string act;
|
||
|
|
|
||
|
|
/// <summary>The row's drug option filters the act (drug binges).</summary>
|
||
|
|
public bool usesDrugOption;
|
||
|
|
|
||
|
|
public override IEnumerable<string> ConfigErrors()
|
||
|
|
{
|
||
|
|
foreach (string e in base.ConfigErrors())
|
||
|
|
yield return e;
|
||
|
|
if (stateClass == null || !typeof(MentalState).IsAssignableFrom(stateClass))
|
||
|
|
yield return "stateClass must be a MentalState class";
|
||
|
|
if (ActKey.CategoryOf(act) == null || ActKey.NameOf(act) == null)
|
||
|
|
yield return "act must be an act key such as Special/SetFire";
|
||
|
|
if (ActKey.CategoryOf(act) == ActCategories.Break)
|
||
|
|
yield return "act must name an act outside the breaks";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Resolves the link of a mental state: the most derived linked class wins.</summary>
|
||
|
|
public static class BreakLinks
|
||
|
|
{
|
||
|
|
private static readonly Dictionary<Type, BreakLinkDef> byClass = new Dictionary<Type, BreakLinkDef>();
|
||
|
|
private static readonly Dictionary<Type, bool> hostileByClass = new Dictionary<Type, bool>();
|
||
|
|
|
||
|
|
/// <summary>The link for a break, or null when its class has none (Planning only).</summary>
|
||
|
|
public static BreakLinkDef For(MentalStateDef state) => For(state?.stateClass);
|
||
|
|
|
||
|
|
public static BreakLinkDef For(Type stateClass)
|
||
|
|
{
|
||
|
|
if (stateClass == null)
|
||
|
|
return null;
|
||
|
|
lock (byClass)
|
||
|
|
{
|
||
|
|
if (byClass.TryGetValue(stateClass, out BreakLinkDef cachedLink))
|
||
|
|
return cachedLink;
|
||
|
|
}
|
||
|
|
BreakLinkDef best = null;
|
||
|
|
int bestDepth = -1;
|
||
|
|
foreach (BreakLinkDef link in DefDatabase<BreakLinkDef>.AllDefsListForReading)
|
||
|
|
{
|
||
|
|
if (link.stateClass == null || !link.stateClass.IsAssignableFrom(stateClass))
|
||
|
|
continue;
|
||
|
|
int depth = Depth(link.stateClass);
|
||
|
|
if (depth > bestDepth)
|
||
|
|
{
|
||
|
|
best = link;
|
||
|
|
bestDepth = depth;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lock (byClass) byClass[stateClass] = best;
|
||
|
|
return best;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Whether the act is the link's act: same category and the link's def, or a member of the link's
|
||
|
|
/// group. The drug option is not checked here (Lawbook.BestRow checks it on the break row).
|
||
|
|
/// </summary>
|
||
|
|
public static bool Matches(BreakLinkDef link, ActEvent act)
|
||
|
|
{
|
||
|
|
if (link == null || act == null || act.def == null)
|
||
|
|
return false;
|
||
|
|
if (ActKey.CategoryOf(link.act) != act.categoryId)
|
||
|
|
return false;
|
||
|
|
string name = ActKey.NameOf(link.act);
|
||
|
|
if (!ActKey.IsGroup(link.act))
|
||
|
|
return name == act.def.defName;
|
||
|
|
ActGroup group = ActCatalogue.GroupOf(link.act);
|
||
|
|
return group != null && group.Matches(act);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>A state hostile to the colony by its class (berserk), from the Break category's hostileStateClasses.</summary>
|
||
|
|
public static bool IsHostile(MentalStateDef state)
|
||
|
|
{
|
||
|
|
Type cls = state?.stateClass;
|
||
|
|
if (cls == null)
|
||
|
|
return false;
|
||
|
|
lock (hostileByClass)
|
||
|
|
{
|
||
|
|
if (hostileByClass.TryGetValue(cls, out bool h))
|
||
|
|
return h;
|
||
|
|
}
|
||
|
|
bool hostile = false;
|
||
|
|
ActCategoryDef breaks = DefDatabase<ActCategoryDef>.GetNamedSilentFail(ActCategories.Break);
|
||
|
|
if (breaks?.hostileStateClasses != null)
|
||
|
|
{
|
||
|
|
foreach (Type t in breaks.hostileStateClasses)
|
||
|
|
{
|
||
|
|
if (t != null && t.IsAssignableFrom(cls))
|
||
|
|
{
|
||
|
|
hostile = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lock (hostileByClass) hostileByClass[cls] = hostile;
|
||
|
|
return hostile;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Drops the caches (tests that add a link at runtime).</summary>
|
||
|
|
public static void ClearCache()
|
||
|
|
{
|
||
|
|
lock (byClass) byClass.Clear();
|
||
|
|
lock (hostileByClass) hostileByClass.Clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
private static int Depth(Type t)
|
||
|
|
{
|
||
|
|
int d = 0;
|
||
|
|
for (Type x = t; x != null; x = x.BaseType)
|
||
|
|
d++;
|
||
|
|
return d;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|