using System.Collections.Generic; using System.Linq; using System.Text; using RimWorld; using Verse; namespace BlaxCastes { /// /// 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. /// public static class ScheduleSlot { public const int Hours = 24; /// The time assignment for a stored name: unknown names, and Meditate without Royalty, become Anything. public static TimeAssignmentDef Resolve(string defName) { TimeAssignmentDef def = defName.NullOrEmpty() ? null : DefDatabase.GetNamedSilentFail(defName); if (def != null && def == TimeAssignmentDefOf.Meditate && !ModsConfig.RoyaltyActive) def = null; return def ?? TimeAssignmentDefOf.Anything; } /// True when the name resolves to itself (not to the Anything fallback). public static bool IsKnown(string defName) => !defName.NullOrEmpty() && Resolve(defName).defName == defName; /// /// 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. /// public static bool IsDlcOff(string defName) => !defName.NullOrEmpty() && !IsKnown(defName) && DlcSupport.InactiveDlcFor(typeof(TimeAssignmentDef), defName) != null; /// A name a caste keeps as it is: known, or of a DLC that is off. public static bool IsKept(string defName) => IsKnown(defName) || IsDlcOff(defName); /// /// 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. /// public static List Normalize(object value) { IEnumerable names; switch (value) { case IEnumerable s: names = s; break; case IEnumerable d: names = d.Select(x => x?.defName); break; default: return null; } List result = names.Select(n => IsKept(n) ? n : TimeAssignmentDefOf.Anything.defName).ToList(); return result.Count == Hours ? result : null; } /// /// 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. /// public static List DlcOffLabels(IReadOnlyList names) { var result = new List(); 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; } /// True for a value Normalize accepts or null. public static bool Fits(object value) { switch (value) { case null: return true; case IEnumerable s: return s.Count() == Hours; case IEnumerable d: return d.Count() == Hours; default: return false; } } /// The game's starting schedule for a new pawn: sleep from 22 to 5, anything else the rest of the day. public static List Default() { var result = new List(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; /// The pawn's 24 time assignment defNames, or null without a timetable. public static string[] Of(Pawn pawn) { if (!HasTimetable(pawn)) return null; return pawn.timetable.times.Select(t => (t ?? TimeAssignmentDefOf.Anything).defName).ToArray(); } /// True when every hour of the pawn's timetable is the caste's resolved assignment. public static bool Matches(Pawn pawn, IReadOnlyList 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; } /// Writes all 24 hours through SetAssignment. False when the pawn has no timetable. public static bool Write(Pawn pawn, IReadOnlyList 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; } /// The day as runs of the same assignment, for tooltips and logs: "sleep 0-5, anything 6-21, sleep 22-23". public static string Summary(IReadOnlyList 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(); } } }