rimworld-blax-castes/Source/BlaxCastes/ScheduleSlot.cs

154 lines
6.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>
/// The schedule slot: 24 TimeAssignmentDef defNames, hour 0 first. Names resolve with
/// GetNamedSilentFail; an unknown name, and Meditate without Royalty, applies as Anything. A caste
/// keeps a name of an official DLC that is off (Meditate without Royalty, see DlcSupport), so those
/// hours come back with the DLC; a name nothing knows (a removed mod) is stored as Anything. Written
/// with Pawn_TimetableTracker.SetAssignment, only on pawns that have a timetable (prisoners from
/// other factions have none). The Follow rule looks at the whole day.
/// </summary>
public static class ScheduleSlot
{
public const int Hours = 24;
/// <summary>The time assignment for a stored name: unknown names, and Meditate without Royalty, become Anything.</summary>
public static TimeAssignmentDef Resolve(string defName)
{
TimeAssignmentDef def = defName.NullOrEmpty() ? null : DefDatabase<TimeAssignmentDef>.GetNamedSilentFail(defName);
if (def != null && def == TimeAssignmentDefOf.Meditate && !ModsConfig.RoyaltyActive)
def = null;
return def ?? TimeAssignmentDefOf.Anything;
}
/// <summary>True when the name resolves to itself (not to the Anything fallback).</summary>
public static bool IsKnown(string defName) => !defName.NullOrEmpty() && Resolve(defName).defName == defName;
/// <summary>
/// The name belongs to an official DLC that is off (Meditate without Royalty): the caste keeps it, and
/// it applies as Anything until the DLC is on again.
/// </summary>
public static bool IsDlcOff(string defName) =>
!defName.NullOrEmpty() && !IsKnown(defName) && DlcSupport.InactiveDlcFor(typeof(TimeAssignmentDef), defName) != null;
/// <summary>A name a caste keeps as it is: known, or of a DLC that is off.</summary>
public static bool IsKept(string defName) => IsKnown(defName) || IsDlcOff(defName);
/// <summary>
/// 24 stored names from a value: a list of names or of TimeAssignmentDefs. Names of a DLC that is off
/// are kept; other unknown names become Anything. Null for null or anything that is not 24 entries long.
/// </summary>
public static List<string> Normalize(object value)
{
IEnumerable<string> names;
switch (value)
{
case IEnumerable<string> s: names = s; break;
case IEnumerable<TimeAssignmentDef> d: names = d.Select(x => x?.defName); break;
default: return null;
}
List<string> result = names.Select(n => IsKept(n) ? n : TimeAssignmentDefOf.Anything.defName).ToList();
return result.Count == Hours ? result : null;
}
/// <summary>
/// The stored names of a DLC that is off, each once, with the DLC: "Meditate (needs Royalty)". Empty when
/// there are none. The manager shows them under the grid, which draws those hours as Anything.
/// </summary>
public static List<string> DlcOffLabels(IReadOnlyList<string> names)
{
var result = new List<string>();
if (names == null)
return result;
foreach (string name in names.Where(IsDlcOff).Distinct())
result.Add("BlaxCastes_NeedsDlc".Translate(name.Named("WHAT"), DlcSupport.InactiveDlcFor(typeof(TimeAssignmentDef), name).Named("DLC")));
return result;
}
/// <summary>True for a value Normalize accepts or null.</summary>
public static bool Fits(object value)
{
switch (value)
{
case null: return true;
case IEnumerable<string> s: return s.Count() == Hours;
case IEnumerable<TimeAssignmentDef> d: return d.Count() == Hours;
default: return false;
}
}
/// <summary>The game's starting schedule for a new pawn: sleep from 22 to 5, anything else the rest of the day.</summary>
public static List<string> Default()
{
var result = new List<string>(Hours);
for (int i = 0; i < Hours; i++)
result.Add((i > 5 && i <= 21 ? TimeAssignmentDefOf.Anything : TimeAssignmentDefOf.Sleep).defName);
return result;
}
public static bool HasTimetable(Pawn pawn) => pawn?.timetable?.times != null && pawn.timetable.times.Count == Hours;
/// <summary>The pawn's 24 time assignment defNames, or null without a timetable.</summary>
public static string[] Of(Pawn pawn)
{
if (!HasTimetable(pawn))
return null;
return pawn.timetable.times.Select(t => (t ?? TimeAssignmentDefOf.Anything).defName).ToArray();
}
/// <summary>True when every hour of the pawn's timetable is the caste's resolved assignment.</summary>
public static bool Matches(Pawn pawn, IReadOnlyList<string> names)
{
if (names == null || names.Count != Hours || !HasTimetable(pawn))
return false;
for (int hour = 0; hour < Hours; hour++)
{
if (pawn.timetable.times[hour] != Resolve(names[hour]))
return false;
}
return true;
}
/// <summary>Writes all 24 hours through SetAssignment. False when the pawn has no timetable.</summary>
public static bool Write(Pawn pawn, IReadOnlyList<string> names)
{
if (names == null || names.Count != Hours || !HasTimetable(pawn))
return false;
for (int hour = 0; hour < Hours; hour++)
{
TimeAssignmentDef def = Resolve(names[hour]);
if (pawn.timetable.GetAssignment(hour) != def)
pawn.timetable.SetAssignment(hour, def);
}
return true;
}
/// <summary>The day as runs of the same assignment, for tooltips and logs: "sleep 0-5, anything 6-21, sleep 22-23".</summary>
public static string Summary(IReadOnlyList<string> names)
{
if (names == null || names.Count != Hours)
return "BlaxCastes_Unmanaged".Translate();
var sb = new StringBuilder();
int start = 0;
for (int hour = 1; hour <= Hours; hour++)
{
if (hour < Hours && Resolve(names[hour]) == Resolve(names[start]))
continue;
if (sb.Length > 0)
sb.Append(", ");
sb.Append(Resolve(names[start]).label).Append(' ').Append(start);
if (hour - 1 != start)
sb.Append('-').Append(hour - 1);
start = hour;
}
return sb.ToString();
}
}
}