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

177 lines
9.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>
/// The ready-made Colonist, Slave and Prisoner castes, wired to the join rows.
/// - Colonist: the database's current default apparel, food, drug and reading policy (which already
/// carry the ideo drug rule and any earlier "Make default"). Medical care unmanaged.
/// - Slave (Ideology only): the Slave outfit, Simple food, No drugs. Reading stays unmanaged, because
/// slave children need books to learn; medical care stays unmanaged, so the game's default care for
/// slaves stays in charge.
/// - Prisoner: Simple food.
/// Policies are found by their translated starting labels, No drugs by its source def. A miss leaves
/// the slot unmanaged; Castes never creates vanilla policies itself.
/// Rows: bought as colonist and new colonist use Default caste (Colonist); bought as slave and new
/// slave use Slave; new prisoner uses Prisoner.
/// </summary>
public static class StarterCastes
{
public const string NoDrugsSourceDef = "NoDrugs";
private static bool SettingOn => BlaxCastesMod.Settings == null || BlaxCastesMod.Settings.seedStarterCastes;
public static string ColonistLabel => "BlaxCastes_StarterColonist".Translate();
public static string SlaveLabel => "BlaxCastes_StarterSlave".Translate();
public static string PrisonerLabel => "BlaxCastes_StarterPrisoner".Translate();
// ---- policy lookups ------------------------------------------------------------------
public static ApparelPolicy SlaveOutfit() => Current.Game?.outfitDatabase?.AllOutfits?.FirstOrDefault(o => o != null && o.label == (string)"OutfitSlave".Translate());
public static FoodPolicy SimpleFood() => Current.Game?.foodRestrictionDatabase?.AllFoodRestrictions?.FirstOrDefault(f => f != null && f.label == (string)"FoodRestrictionSimple".Translate());
public static DrugPolicy NoDrugs() => Current.Game?.drugPolicyDatabase?.AllPolicies?.FirstOrDefault(d => d?.sourceDef != null && d.sourceDef.defName == NoDrugsSourceDef);
/// <summary>True for the game's Slave outfit (by its starting label), which has no cold protection.</summary>
public static bool IsSlaveOutfit(Policy policy) => policy is ApparelPolicy a && a.label == (string)"OutfitSlave".Translate();
/// <summary>Ideology is active and the game has no Slave outfit (renamed or deleted): the manager offers to create one.</summary>
public static bool CanCreateSlaveOutfit => ModsConfig.IdeologyActive && Current.Game?.outfitDatabase != null && SlaveOutfit() == null;
/// <summary>
/// The manager's "Create Slave outfit" button: a new apparel policy named like the game's starting
/// Slave outfit that allows the apparel the game tags for slaves, the way a new game sets it up.
/// Only on the player's click; seeding never creates policies. Returns the existing outfit if there
/// is one, or null without Ideology.
/// </summary>
public static ApparelPolicy CreateSlaveOutfit()
{
if (!ModsConfig.IdeologyActive)
return null;
ApparelPolicy existing = SlaveOutfit();
OutfitDatabase db = Current.Game?.outfitDatabase;
if (existing != null || db == null)
return existing;
ApparelPolicy policy = db.MakeNewOutfit();
policy.label = "OutfitSlave".Translate();
policy.filter.SetDisallowAll();
if (SpecialThingFilterDefOf.AllowDeadmansApparel != null)
policy.filter.SetAllow(SpecialThingFilterDefOf.AllowDeadmansApparel, allow: false);
foreach (ThingDef def in DefDatabase<ThingDef>.AllDefs)
{
if (def.apparel?.defaultOutfitTags != null && def.apparel.defaultOutfitTags.Contains("Slave"))
policy.filter.SetAllow(def, allow: true);
}
return policy;
}
// ---- seeding -------------------------------------------------------------------------
/// <summary>
/// A new game: adds the starter castes and wires the rows, unless the setting is off or the
/// component already has castes. Returns true if it seeded.
/// </summary>
public static bool SeedNewGame(GameComponent_BlaxCastes comp)
{
if (comp == null || !SettingOn || comp.StarterCastesSeeded)
return false;
if (comp.Castes.Count > 0)
{
comp.MarkStarterCastesSeeded();
return false;
}
Seed(comp);
return true;
}
/// <summary>
/// A loaded save: adds the starter castes once per save, only if the save has no castes. A save
/// that already has castes is marked, so deleting them later does not bring the starters back.
/// Nobody is moved. Returns true if it seeded.
/// </summary>
public static bool SeedOnLoad(GameComponent_BlaxCastes comp) => SeedNewGame(comp);
/// <summary>Creates the castes in the component and wires the rows. Writes the component directly, so it works on any component.</summary>
internal static void Seed(GameComponent_BlaxCastes comp)
{
Game game = Current.Game;
Caste colonist = NewCaste(comp, ColonistLabel);
SetIfFound(colonist, CasteSlot.Apparel, game?.outfitDatabase?.DefaultOutfit());
SetIfFound(colonist, CasteSlot.Food, game?.foodRestrictionDatabase?.DefaultFoodRestriction());
SetIfFound(colonist, CasteSlot.Drug, game?.drugPolicyDatabase?.DefaultDrugPolicy());
SetIfFound(colonist, CasteSlot.Reading, game?.readingPolicyDatabase?.DefaultReadingPolicy());
Caste slave = null;
if (ModsConfig.IdeologyActive)
{
slave = NewCaste(comp, SlaveLabel);
SetIfFound(slave, CasteSlot.Apparel, SlaveOutfit());
SetIfFound(slave, CasteSlot.Food, SimpleFood());
SetIfFound(slave, CasteSlot.Drug, NoDrugs());
}
Caste prisoner = NewCaste(comp, PrisonerLabel);
SetIfFound(prisoner, CasteSlot.Food, SimpleFood());
comp.SetDefaultRaw(colonist);
comp.SetRowRaw(JoinReason.BoughtColonist, JoinRowMode.DefaultCaste, null);
comp.SetRowRaw(JoinReason.NewColonist, JoinRowMode.DefaultCaste, null);
if (slave != null)
{
comp.SetRowRaw(JoinReason.BoughtSlave, JoinRowMode.SpecificCaste, slave);
comp.SetRowRaw(JoinReason.NewSlave, JoinRowMode.SpecificCaste, slave);
}
comp.SetRowRaw(JoinReason.NewPrisoner, JoinRowMode.SpecificCaste, prisoner);
comp.MarkStarterCastesSeeded();
List<string> missing = new List<string>();
if (slave != null)
{
if (!slave.IsManaged(CasteSlot.Apparel)) missing.Add(SlaveLabel + ": " + CasteUtility.SlotLabel(CasteSlot.Apparel));
if (!slave.IsManaged(CasteSlot.Food)) missing.Add(SlaveLabel + ": " + CasteUtility.SlotLabel(CasteSlot.Food));
if (!slave.IsManaged(CasteSlot.Drug)) missing.Add(SlaveLabel + ": " + CasteUtility.SlotLabel(CasteSlot.Drug));
}
if (!prisoner.IsManaged(CasteSlot.Food)) missing.Add(PrisonerLabel + ": " + CasteUtility.SlotLabel(CasteSlot.Food));
if (missing.Count > 0)
Log.Message("[Blax Castes] Starter castes added; policies not found (renamed or removed), left unmanaged: " + string.Join(", ", missing));
}
private static Caste NewCaste(GameComponent_BlaxCastes comp, string label)
{
var caste = new Caste(comp.TakeNextId(), label);
comp.AddCaste(caste);
return caste;
}
/// <summary>No members exist yet, so the raw write needs no member sync.</summary>
private static void SetIfFound(Caste caste, CasteSlot slot, Policy policy)
{
if (policy != null && CasteUtility.PolicyExists(policy))
caste.SetSlotRaw(slot, policy);
}
/// <summary>The one message for a save that just got the starter castes.</summary>
public static void ShowSeededMessage(GameComponent_BlaxCastes comp)
{
string text = SeededMessageText(comp);
LongEventHandler.ExecuteWhenFinished(() => Messages.Message(text, MessageTypeDefOf.NeutralEvent));
}
/// <summary>
/// The seeding message. It points to Assign > Manage castes, or, while the Caste column is not in the
/// Assign tab (so that button is not there), to the mod settings, which open the same window. The
/// column is there as the game started (CasteColumns.Shown), whatever the setting says until a restart.
/// </summary>
public static string SeededMessageText(GameComponent_BlaxCastes comp)
{
string names = comp.Castes.Select(c => c.label).ToCommaList(useAnd: true);
string key = CasteColumns.Shown ? "BlaxCastes_StarterSeededMessage" : "BlaxCastes_StarterSeededMessageNoColumn";
return key.Translate(names.Named("CASTES"));
}
}
}