137 lines
5.8 KiB
C#
137 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxCastes
|
|
{
|
|
/// <summary>
|
|
/// The Caste column in three tables. The XML patches put it in the Assign table right before
|
|
/// HostilityResponse (Patches/BlaxCastes_Assign.xml), in the Schedule table right after the timetable
|
|
/// (the tab draws its time assignment selector over the top of the header left of the timetable, where
|
|
/// the column's buttons would sit), and in the Work table right before the copy and paste column, so
|
|
/// just left of the work columns (Patches/BlaxCastes_ScheduleWork.xml). At startup this checks the
|
|
/// placement, and removes the column again when the showAssignColumn setting is off, so turning the
|
|
/// setting on or off takes effect after a restart.
|
|
/// </summary>
|
|
[StaticConstructorOnStartup]
|
|
public static class CasteColumns
|
|
{
|
|
/// <summary>The Assign table's anchor: the Caste column goes right before it.</summary>
|
|
public const string AnchorColumn = "HostilityResponse";
|
|
|
|
/// <summary>The Schedule table's column the Caste column goes right after.</summary>
|
|
public const string ScheduleAfter = "Timetable";
|
|
|
|
/// <summary>The Schedule table's anchors without the timetable: the column goes right before the first one found.</summary>
|
|
public static readonly string[] ScheduleAnchors = { "AllowedArea" };
|
|
|
|
/// <summary>The Work table's anchor. Without it the column goes before the first work column.</summary>
|
|
public static readonly string[] WorkAnchors = { "CopyPasteWorkPriorities" };
|
|
|
|
static CasteColumns()
|
|
{
|
|
try
|
|
{
|
|
bool show = BlaxCastesMod.Settings == null || BlaxCastesMod.Settings.showAssignColumn;
|
|
var notPlaced = new List<string>();
|
|
foreach (PawnTableDef table in Tables)
|
|
{
|
|
if (show && table?.columns != null && Column != null && !table.columns.Contains(Column))
|
|
notPlaced.Add(table.defName);
|
|
}
|
|
ApplyAll(show);
|
|
if (notPlaced.Count > 0)
|
|
Log.Warning("[Blax Castes] The XML patches did not place the Caste column in " + string.Join(", ", notPlaced) + "; it was added in code instead.");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error("[Blax Castes] Setting up the Caste column failed: " + e);
|
|
}
|
|
}
|
|
|
|
/// <summary>The Assign tab's table.</summary>
|
|
public static PawnTableDef Table => PawnTableDefOf.Assign;
|
|
|
|
/// <summary>The Schedule tab's table (defName Restrict).</summary>
|
|
public static PawnTableDef ScheduleTable => PawnTableDefOf.Restrict;
|
|
|
|
public static PawnTableDef WorkTable => PawnTableDefOf.Work;
|
|
|
|
public static IEnumerable<PawnTableDef> Tables
|
|
{
|
|
get
|
|
{
|
|
yield return Table;
|
|
yield return ScheduleTable;
|
|
yield return WorkTable;
|
|
}
|
|
}
|
|
|
|
public static PawnColumnDef Column => DefDatabase<PawnColumnDef>.GetNamedSilentFail(PawnColumnWorker_BlaxCaste.DefName);
|
|
|
|
/// <summary>
|
|
/// The Assign table only. Off: removes the column. On: adds it back right before HostilityResponse
|
|
/// (or after the first column if that is gone) unless it is already there. Returns true if the
|
|
/// table changed. Open tabs pick the change up the next time they open.
|
|
/// </summary>
|
|
public static bool Apply(bool show) => ApplyTo(Table, show, AnchorColumn);
|
|
|
|
/// <summary>All three tables. Returns true if any changed.</summary>
|
|
public static bool ApplyAll(bool show)
|
|
{
|
|
bool changed = Apply(show);
|
|
changed |= ApplyToSchedule(show);
|
|
changed |= ApplyTo(WorkTable, show, WorkAnchors);
|
|
return changed;
|
|
}
|
|
|
|
/// <summary>The Schedule table: right after the timetable, else as ApplyTo with ScheduleAnchors.</summary>
|
|
public static bool ApplyToSchedule(bool show)
|
|
{
|
|
PawnTableDef table = ScheduleTable;
|
|
PawnColumnDef column = Column;
|
|
if (table?.columns == null || column == null)
|
|
return false;
|
|
if (show && !table.columns.Contains(column))
|
|
{
|
|
int timetable = table.columns.FindIndex(c => c != null && c.defName == ScheduleAfter);
|
|
if (timetable >= 0)
|
|
{
|
|
table.columns.Insert(timetable + 1, column);
|
|
return true;
|
|
}
|
|
}
|
|
return ApplyTo(table, show, ScheduleAnchors);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Off: removes the column from the table. On: adds it right before the first anchor found, else
|
|
/// before the first work column, else after the first column, unless it is already there.
|
|
/// </summary>
|
|
public static bool ApplyTo(PawnTableDef table, bool show, params string[] anchors)
|
|
{
|
|
PawnColumnDef column = Column;
|
|
if (table?.columns == null || column == null)
|
|
return false;
|
|
if (!show)
|
|
return table.columns.RemoveAll(c => c == column) > 0;
|
|
if (table.columns.Contains(column))
|
|
return false;
|
|
int at = -1;
|
|
foreach (string anchor in anchors)
|
|
{
|
|
at = table.columns.FindIndex(c => c != null && c.defName == anchor);
|
|
if (at >= 0)
|
|
break;
|
|
}
|
|
if (at < 0)
|
|
at = table.columns.FindIndex(c => c?.workType != null);
|
|
if (at < 0)
|
|
at = Math.Min(1, table.columns.Count);
|
|
table.columns.Insert(at, column);
|
|
return true;
|
|
}
|
|
}
|
|
}
|