rimworld-blax-laws/Source/BlaxLaws/Catalogue/ActEntry.cs

120 lines
4.6 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// One entry of the act catalogue: a single def or a group. Built once per game start by ActCatalogue
/// from the category workers; never saved (rows save the key). The worker fills key, category, def or
/// group, label and source; ActCatalogue.Build fills the stage and break fields from the link table.
/// </summary>
public class ActEntry
{
/// <summary>"Ate/WakeUp" or "Ate/@hardDrugs".</summary>
public string key;
public string categoryId;
/// <summary>The def's defName, or the group id with its "@".</summary>
public string name;
/// <summary>The def of a single entry; null for a group.</summary>
public Def def;
/// <summary>The group of a group entry; null for a single def.</summary>
public ActGroup group;
/// <summary>What the picker and the row show.</summary>
public string label;
/// <summary>"Core", the DLC's name or the mod's name.</summary>
public string sourceMod;
/// <summary>The source's package id, lower case.</summary>
public string sourcePackageId;
/// <summary>Break entries: a planning stage (always, for a break).</summary>
public bool hasPlanning;
/// <summary>A did-it stage: every entry outside the breaks, and a break with a link.</summary>
public bool hasDidIt = true;
/// <summary>A break hostile to the colony (berserk): no Subdue. For a group: every member is hostile.</summary>
public bool hostileBreak;
/// <summary>A drug binge entry: rows keep a drug option.</summary>
public bool hasDrugOption;
/// <summary>Break entries: the links of the entry's breaks (one for a def, the distinct links of its members for a group).</summary>
public List<BreakLinkDef> links = new List<BreakLinkDef>();
public bool IsGroup => group != null;
public bool IsBreak => categoryId == ActCategories.Break;
/// <summary>Rank for "the most specific row wins": a single def 3, a group 2, the category's "any" 1.</summary>
public int Rank => def != null ? 3 : group != null && !group.IsAny ? 2 : 1;
/// <summary>The stages the entry offers, planning first.</summary>
public IEnumerable<LawStage> Stages
{
get
{
if (hasPlanning)
yield return LawStage.Planning;
if (hasDidIt)
yield return LawStage.DidIt;
}
}
public bool HasStage(LawStage stage) => stage == LawStage.Planning ? hasPlanning : hasDidIt;
/// <summary>The stage a new row gets.</summary>
public LawStage FirstStage => hasPlanning ? LawStage.Planning : LawStage.DidIt;
/// <summary>Whether the act is this entry's: the same def, or a member of the group.</summary>
public bool Matches(ActEvent act)
{
if (act == null || act.categoryId != categoryId)
return false;
if (group != null)
return group.Matches(act);
return act.def != null && (act.def == def || act.def.defName == name);
}
// ---- builders for the category workers -------------------------------------------------
/// <summary>A single-def entry with the def's label and source mod.</summary>
public static ActEntry ForDef(string categoryId, Def def, string label = null)
{
return new ActEntry
{
key = ActKey.Of(categoryId, def.defName),
categoryId = categoryId,
name = def.defName,
def = def,
label = !label.NullOrEmpty() ? label : !def.label.NullOrEmpty() ? def.LabelCap.ToString() : def.defName,
sourceMod = DlcSupport.SourceName(def.modContentPack),
sourcePackageId = DlcSupport.SourcePackageId(def.modContentPack),
};
}
/// <summary>A group entry. Groups are Laws' own, so their source is Core.</summary>
public static ActEntry ForGroup(ActGroup group, string label = null)
{
return new ActEntry
{
key = group.Key,
categoryId = group.categoryId,
name = group.id,
group = group,
label = label.NullOrEmpty() ? group.Label : label,
sourceMod = DlcSupport.CoreName,
sourcePackageId = DlcSupport.CorePackageId,
};
}
public override string ToString() => key + " (" + label + ", " + sourceMod + ")";
}
}