128 lines
5.3 KiB
C#
128 lines
5.3 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, becomes 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>
|
||
|
|
/// 24 stored names from a value: a list of names or of TimeAssignmentDefs. 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 => Resolve(n).defName).ToList();
|
||
|
|
return result.Count == Hours ? result : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <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();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|