2026-11-27 18:53:36 +00:00
|
|
|
using System;
|
2026-11-28 18:33:08 +00:00
|
|
|
using System.Collections.Generic;
|
2026-11-27 18:53:36 +00:00
|
|
|
using HarmonyLib;
|
|
|
|
|
using RimWorld;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Verse;
|
|
|
|
|
|
|
|
|
|
namespace BlaxSlaves
|
|
|
|
|
{
|
|
|
|
|
public class BlaxSlavesSettings : ModSettings
|
|
|
|
|
{
|
2026-11-28 18:33:08 +00:00
|
|
|
// How "buy as slave or colonist" picks the default for recruitable pawns in trader stock.
|
|
|
|
|
public const int PurchaseVanilla = 0, PurchaseAlwaysSlave = 1, PurchaseAlwaysColonist = 2;
|
|
|
|
|
// What traders whose ideoligion rejects slavery do with colony slaves and prisoners.
|
|
|
|
|
public const int AntiSlaveryBuyAnyway = 0, AntiSlaveryRefuse = 1;
|
|
|
|
|
|
|
|
|
|
// General (1.0/1.1)
|
2026-11-27 18:53:36 +00:00
|
|
|
public bool returnRunaways = true;
|
|
|
|
|
public float returnChance = 0.8f;
|
|
|
|
|
public int returnDelayMinDays = 3;
|
|
|
|
|
public int returnDelayMaxDays = 10;
|
|
|
|
|
public bool suppressSlaveHarmMood = true;
|
|
|
|
|
public bool keepRelationGrief = true;
|
2026-11-28 18:33:08 +00:00
|
|
|
public bool showTimeInSlaveTab = true;
|
|
|
|
|
|
|
|
|
|
// Trade (1.2)
|
|
|
|
|
// A 1.1 setting, drawn and reset on the Trade tab; its Scribe key is unchanged, so saved values load.
|
2026-11-27 18:53:36 +00:00
|
|
|
public float traderBrandChance = 0.35f;
|
2026-11-28 18:33:08 +00:00
|
|
|
public bool buyModeEnabled = true;
|
|
|
|
|
public int defaultPurchaseMode = PurchaseVanilla;
|
|
|
|
|
public bool allTradersBuySlaves = true;
|
|
|
|
|
public bool visitorsBuySlaves = true;
|
|
|
|
|
public int antiSlaveryTraders = AntiSlaveryBuyAnyway;
|
|
|
|
|
public bool slaveStockIgnoresColonySize = true;
|
|
|
|
|
public float slaveSellFactor = 0.6f;
|
|
|
|
|
public float slaveBuyFactor = 0.6f;
|
2026-11-27 18:53:36 +00:00
|
|
|
|
|
|
|
|
public override void ExposeData()
|
|
|
|
|
{
|
|
|
|
|
base.ExposeData();
|
|
|
|
|
Scribe_Values.Look(ref returnRunaways, "returnRunaways", true);
|
|
|
|
|
Scribe_Values.Look(ref returnChance, "returnChance", 0.8f);
|
|
|
|
|
Scribe_Values.Look(ref returnDelayMinDays, "returnDelayMinDays", 3);
|
|
|
|
|
Scribe_Values.Look(ref returnDelayMaxDays, "returnDelayMaxDays", 10);
|
|
|
|
|
Scribe_Values.Look(ref suppressSlaveHarmMood, "suppressSlaveHarmMood", true);
|
|
|
|
|
Scribe_Values.Look(ref keepRelationGrief, "keepRelationGrief", true);
|
|
|
|
|
Scribe_Values.Look(ref traderBrandChance, "traderBrandChance", 0.35f);
|
2026-11-28 18:33:08 +00:00
|
|
|
Scribe_Values.Look(ref showTimeInSlaveTab, "showTimeInSlaveTab", true);
|
|
|
|
|
|
|
|
|
|
Scribe_Values.Look(ref buyModeEnabled, "buyModeEnabled", true);
|
|
|
|
|
Scribe_Values.Look(ref defaultPurchaseMode, "defaultPurchaseMode", PurchaseVanilla);
|
|
|
|
|
Scribe_Values.Look(ref allTradersBuySlaves, "allTradersBuySlaves", true);
|
|
|
|
|
Scribe_Values.Look(ref visitorsBuySlaves, "visitorsBuySlaves", true);
|
|
|
|
|
Scribe_Values.Look(ref antiSlaveryTraders, "antiSlaveryTraders", AntiSlaveryBuyAnyway);
|
|
|
|
|
Scribe_Values.Look(ref slaveStockIgnoresColonySize, "slaveStockIgnoresColonySize", true);
|
|
|
|
|
Scribe_Values.Look(ref slaveSellFactor, "slaveSellFactor", 0.6f);
|
|
|
|
|
Scribe_Values.Look(ref slaveBuyFactor, "slaveBuyFactor", 0.6f);
|
|
|
|
|
|
|
|
|
|
if (Scribe.mode == LoadSaveMode.PostLoadInit)
|
|
|
|
|
Clamp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clamp()
|
|
|
|
|
{
|
|
|
|
|
returnChance = Mathf.Clamp01(returnChance);
|
|
|
|
|
returnDelayMinDays = Mathf.Clamp(returnDelayMinDays, 1, 30);
|
|
|
|
|
returnDelayMaxDays = Mathf.Clamp(returnDelayMaxDays, returnDelayMinDays, 60);
|
|
|
|
|
traderBrandChance = Mathf.Clamp01(traderBrandChance);
|
|
|
|
|
defaultPurchaseMode = Mathf.Clamp(defaultPurchaseMode, PurchaseVanilla, PurchaseAlwaysColonist);
|
|
|
|
|
antiSlaveryTraders = Mathf.Clamp(antiSlaveryTraders, AntiSlaveryBuyAnyway, AntiSlaveryRefuse);
|
|
|
|
|
slaveSellFactor = Mathf.Clamp(slaveSellFactor, 0.3f, 1f);
|
|
|
|
|
slaveBuyFactor = Mathf.Clamp(slaveBuyFactor, 0.3f, 1f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetGeneral()
|
|
|
|
|
{
|
|
|
|
|
var d = new BlaxSlavesSettings();
|
|
|
|
|
returnRunaways = d.returnRunaways;
|
|
|
|
|
returnChance = d.returnChance;
|
|
|
|
|
returnDelayMinDays = d.returnDelayMinDays;
|
|
|
|
|
returnDelayMaxDays = d.returnDelayMaxDays;
|
|
|
|
|
suppressSlaveHarmMood = d.suppressSlaveHarmMood;
|
|
|
|
|
keepRelationGrief = d.keepRelationGrief;
|
|
|
|
|
showTimeInSlaveTab = d.showTimeInSlaveTab;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetTrade()
|
|
|
|
|
{
|
|
|
|
|
var d = new BlaxSlavesSettings();
|
|
|
|
|
traderBrandChance = d.traderBrandChance;
|
|
|
|
|
buyModeEnabled = d.buyModeEnabled;
|
|
|
|
|
defaultPurchaseMode = d.defaultPurchaseMode;
|
|
|
|
|
allTradersBuySlaves = d.allTradersBuySlaves;
|
|
|
|
|
visitorsBuySlaves = d.visitorsBuySlaves;
|
|
|
|
|
antiSlaveryTraders = d.antiSlaveryTraders;
|
|
|
|
|
slaveStockIgnoresColonySize = d.slaveStockIgnoresColonySize;
|
|
|
|
|
slaveSellFactor = d.slaveSellFactor;
|
|
|
|
|
slaveBuyFactor = d.slaveBuyFactor;
|
2026-11-27 18:53:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class BlaxSlavesMod : Mod
|
|
|
|
|
{
|
|
|
|
|
public static BlaxSlavesSettings Settings;
|
|
|
|
|
|
2026-11-28 18:33:08 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Raised after the settings window closes and the settings are written. Features that
|
|
|
|
|
/// change defs at startup (trader generators, stock flags) re-apply themselves here.
|
|
|
|
|
/// Never raised during a trade session: the settings window can't open while trading.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static event Action SettingsChanged;
|
|
|
|
|
|
|
|
|
|
private enum Tab { Trade, General, Compat }
|
|
|
|
|
private static Tab tab = Tab.Trade;
|
|
|
|
|
private static Vector2 scroll;
|
|
|
|
|
private static float viewHeight = 900f;
|
|
|
|
|
|
2026-11-27 18:53:36 +00:00
|
|
|
public BlaxSlavesMod(ModContentPack content) : base(content)
|
|
|
|
|
{
|
|
|
|
|
Settings = GetSettings<BlaxSlavesSettings>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string SettingsCategory() => "Blax Slaves";
|
|
|
|
|
|
2026-11-28 18:33:08 +00:00
|
|
|
public override void WriteSettings()
|
|
|
|
|
{
|
|
|
|
|
Settings.Clamp();
|
|
|
|
|
base.WriteSettings();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SettingsChanged?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("[Blax Slaves] Re-applying settings failed: " + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-27 18:53:36 +00:00
|
|
|
public override void DoSettingsWindowContents(Rect inRect)
|
|
|
|
|
{
|
2026-11-28 18:33:08 +00:00
|
|
|
var tabs = new List<TabRecord>
|
|
|
|
|
{
|
|
|
|
|
new TabRecord("BlaxSlaves_TabTrade".Translate(), () => tab = Tab.Trade, tab == Tab.Trade),
|
|
|
|
|
new TabRecord("BlaxSlaves_TabGeneral".Translate(), () => tab = Tab.General, tab == Tab.General),
|
|
|
|
|
new TabRecord("BlaxSlaves_TabCompat".Translate(), () => tab = Tab.Compat, tab == Tab.Compat),
|
|
|
|
|
};
|
|
|
|
|
Rect body = inRect;
|
|
|
|
|
body.yMin += 32f;
|
|
|
|
|
TabDrawer.DrawTabs(body, tabs);
|
|
|
|
|
|
|
|
|
|
Rect outRect = body.ContractedBy(4f);
|
|
|
|
|
Rect viewRect = new Rect(0f, 0f, outRect.width - 16f, viewHeight);
|
|
|
|
|
Widgets.BeginScrollView(outRect, ref scroll, viewRect);
|
|
|
|
|
// One column: otherwise rows past last frame's viewHeight wrap into a second column outside the
|
|
|
|
|
// scroll group, CurHeight counts only that column, and viewHeight never grows back after a tab switch.
|
2026-11-27 18:53:36 +00:00
|
|
|
var l = new Listing_Standard();
|
2026-11-28 18:33:08 +00:00
|
|
|
l.maxOneColumn = true;
|
|
|
|
|
l.Begin(viewRect);
|
|
|
|
|
switch (tab)
|
|
|
|
|
{
|
|
|
|
|
case Tab.Trade:
|
|
|
|
|
DoTrade(l);
|
|
|
|
|
break;
|
|
|
|
|
case Tab.General:
|
|
|
|
|
DoGeneral(l);
|
|
|
|
|
break;
|
|
|
|
|
case Tab.Compat:
|
|
|
|
|
DoCompat(l);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
viewHeight = Mathf.Max(outRect.height, l.CurHeight + 12f);
|
|
|
|
|
l.End();
|
|
|
|
|
Widgets.EndScrollView();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DoTrade(Listing_Standard l)
|
|
|
|
|
{
|
|
|
|
|
BlaxSlavesSettings s = Settings;
|
|
|
|
|
if (!ModsConfig.IdeologyActive)
|
|
|
|
|
{
|
|
|
|
|
l.Label("BlaxSlaves_SettingNeedsIdeology".Translate());
|
|
|
|
|
l.GapLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingBuyMode".Translate(), ref s.buyModeEnabled, "BlaxSlaves_SettingBuyModeTip".Translate());
|
|
|
|
|
if (Compat.IsActive(Compat.TradeAsSlaveOrColonist))
|
|
|
|
|
l.Label("BlaxSlaves_SettingBuyModeStandsDown".Translate());
|
|
|
|
|
l.Label("BlaxSlaves_SettingPurchaseMode".Translate());
|
|
|
|
|
if (l.RadioButton("BlaxSlaves_SettingPurchaseVanilla".Translate(), s.defaultPurchaseMode == BlaxSlavesSettings.PurchaseVanilla, 12f))
|
|
|
|
|
s.defaultPurchaseMode = BlaxSlavesSettings.PurchaseVanilla;
|
|
|
|
|
if (l.RadioButton("BlaxSlaves_SettingPurchaseSlave".Translate(), s.defaultPurchaseMode == BlaxSlavesSettings.PurchaseAlwaysSlave, 12f))
|
|
|
|
|
s.defaultPurchaseMode = BlaxSlavesSettings.PurchaseAlwaysSlave;
|
|
|
|
|
if (l.RadioButton("BlaxSlaves_SettingPurchaseColonist".Translate(), s.defaultPurchaseMode == BlaxSlavesSettings.PurchaseAlwaysColonist, 12f))
|
|
|
|
|
s.defaultPurchaseMode = BlaxSlavesSettings.PurchaseAlwaysColonist;
|
2026-11-27 18:53:36 +00:00
|
|
|
|
|
|
|
|
l.GapLine();
|
2026-11-28 18:33:08 +00:00
|
|
|
TraderBuyers.SettingNotes buyerNotes = TraderBuyers.Notes; // cached, DoTrade runs every frame
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingAllTradersBuy".Translate(), ref s.allTradersBuySlaves, "BlaxSlaves_SettingAllTradersBuyTip".Translate());
|
|
|
|
|
if (buyerNotes.everybodyBuysTraders)
|
|
|
|
|
l.Label("BlaxSlaves_SettingAllTradersBuyEverybodyBuys".Translate());
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingVisitorsBuy".Translate(), ref s.visitorsBuySlaves, "BlaxSlaves_SettingVisitorsBuyTip".Translate());
|
|
|
|
|
if (buyerNotes.everybodyBuysVisitors)
|
|
|
|
|
l.Label("BlaxSlaves_SettingVisitorsBuyEverybodyBuys".Translate());
|
|
|
|
|
if (buyerNotes.hospitality)
|
|
|
|
|
l.Label("BlaxSlaves_SettingVisitorsBuyHospitality".Translate());
|
|
|
|
|
l.Label("BlaxSlaves_SettingAntiSlavery".Translate());
|
|
|
|
|
if (l.RadioButton("BlaxSlaves_SettingAntiSlaveryBuy".Translate(), s.antiSlaveryTraders == BlaxSlavesSettings.AntiSlaveryBuyAnyway, 12f))
|
|
|
|
|
s.antiSlaveryTraders = BlaxSlavesSettings.AntiSlaveryBuyAnyway;
|
|
|
|
|
if (l.RadioButton("BlaxSlaves_SettingAntiSlaveryRefuse".Translate(), s.antiSlaveryTraders == BlaxSlavesSettings.AntiSlaveryRefuse, 12f))
|
|
|
|
|
s.antiSlaveryTraders = BlaxSlavesSettings.AntiSlaveryRefuse;
|
2026-11-27 18:53:36 +00:00
|
|
|
|
|
|
|
|
l.GapLine();
|
2026-11-28 18:33:08 +00:00
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingStockAnySize".Translate(), ref s.slaveStockIgnoresColonySize, "BlaxSlaves_SettingStockAnySizeTip".Translate());
|
|
|
|
|
l.Label("BlaxSlaves_SettingTraderBrand".Translate(s.traderBrandChance.ToStringPercent()));
|
|
|
|
|
s.traderBrandChance = l.Slider(s.traderBrandChance, 0f, 1f);
|
2026-11-27 18:53:36 +00:00
|
|
|
|
2026-11-28 18:33:08 +00:00
|
|
|
l.GapLine();
|
|
|
|
|
l.Label("BlaxSlaves_SettingSellFactor".Translate(s.slaveSellFactor.ToStringPercent()), tooltip: "BlaxSlaves_SettingSellFactorTip".Translate());
|
|
|
|
|
s.slaveSellFactor = Mathf.Round(l.Slider(s.slaveSellFactor, 0.3f, 1f) * 20f) / 20f;
|
|
|
|
|
l.Label("BlaxSlaves_SettingBuyFactor".Translate(s.slaveBuyFactor.ToStringPercent()), tooltip: "BlaxSlaves_SettingBuyFactorTip".Translate());
|
|
|
|
|
s.slaveBuyFactor = Mathf.Round(l.Slider(s.slaveBuyFactor, 0.3f, 1f) * 20f) / 20f;
|
|
|
|
|
|
|
|
|
|
l.Gap();
|
|
|
|
|
if (l.ButtonText("BlaxSlaves_ResetTab".Translate()))
|
|
|
|
|
s.ResetTrade();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DoGeneral(Listing_Standard l)
|
|
|
|
|
{
|
|
|
|
|
BlaxSlavesSettings s = Settings;
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingReturn".Translate(), ref s.returnRunaways, "BlaxSlaves_SettingReturnTip".Translate());
|
|
|
|
|
l.Label("BlaxSlaves_SettingReturnChance".Translate(s.returnChance.ToStringPercent()));
|
|
|
|
|
s.returnChance = l.Slider(s.returnChance, 0f, 1f);
|
|
|
|
|
l.Label("BlaxSlaves_SettingReturnDelayMin".Translate(s.returnDelayMinDays));
|
|
|
|
|
s.returnDelayMinDays = Mathf.RoundToInt(l.Slider(s.returnDelayMinDays, 1f, 30f));
|
|
|
|
|
l.Label("BlaxSlaves_SettingReturnDelayMax".Translate(s.returnDelayMaxDays));
|
|
|
|
|
s.returnDelayMaxDays = Mathf.RoundToInt(l.Slider(s.returnDelayMaxDays, 1f, 60f));
|
|
|
|
|
if (s.returnDelayMaxDays < s.returnDelayMinDays)
|
|
|
|
|
s.returnDelayMaxDays = s.returnDelayMinDays;
|
|
|
|
|
|
|
|
|
|
l.GapLine();
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingMood".Translate(), ref s.suppressSlaveHarmMood, "BlaxSlaves_SettingMoodTip".Translate());
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingGrief".Translate(), ref s.keepRelationGrief, "BlaxSlaves_SettingGriefTip".Translate());
|
|
|
|
|
|
|
|
|
|
l.GapLine();
|
|
|
|
|
l.CheckboxLabeled("BlaxSlaves_SettingTimeAsSlave".Translate(), ref s.showTimeInSlaveTab, "BlaxSlaves_SettingTimeAsSlaveTip".Translate());
|
|
|
|
|
|
|
|
|
|
l.Gap();
|
|
|
|
|
if (l.ButtonText("BlaxSlaves_ResetTab".Translate()))
|
|
|
|
|
s.ResetGeneral();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void DoCompat(Listing_Standard l)
|
|
|
|
|
{
|
|
|
|
|
l.Label("BlaxSlaves_CompatSiblings".Translate());
|
|
|
|
|
l.Label(" " + Siblings.Describe());
|
|
|
|
|
l.GapLine();
|
|
|
|
|
|
|
|
|
|
l.Label("BlaxSlaves_CompatReplaced".Translate());
|
|
|
|
|
int shown = 0;
|
|
|
|
|
foreach (Compat.ReplacedMod m in Compat.Replaced)
|
|
|
|
|
{
|
|
|
|
|
if (!Compat.IsActive(m.packageId))
|
|
|
|
|
continue;
|
|
|
|
|
shown++;
|
|
|
|
|
l.Label(" " + m.name + " (" + m.packageId + "): " + m.note.Translate());
|
|
|
|
|
}
|
|
|
|
|
if (shown == 0)
|
|
|
|
|
l.Label(" " + "BlaxSlaves_CompatNoneActive".Translate());
|
2026-11-27 18:53:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Applies the Harmony patches after every Mod class exists (so the Harmony mod has set up
|
|
|
|
|
/// its 1.7 backend), one class at a time so a single broken target can't take the rest down.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[StaticConstructorOnStartup]
|
|
|
|
|
public static class BlaxSlavesStartup
|
|
|
|
|
{
|
|
|
|
|
public static readonly Harmony Harmony = new Harmony("blax.slaves");
|
|
|
|
|
|
|
|
|
|
static BlaxSlavesStartup()
|
|
|
|
|
{
|
|
|
|
|
int ok = 0, failed = 0;
|
|
|
|
|
foreach (Type type in typeof(BlaxSlavesStartup).Assembly.GetTypes())
|
|
|
|
|
{
|
|
|
|
|
if (!type.IsDefined(typeof(HarmonyPatch), false))
|
|
|
|
|
continue;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Harmony.CreateClassProcessor(type).Patch();
|
|
|
|
|
ok++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
failed++;
|
|
|
|
|
Log.Error("[Blax Slaves] Patch " + type.Name + " failed: " + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ok += Compat.Apply(Harmony);
|
|
|
|
|
Log.Message("[Blax Slaves] " + ok + " patches applied" + (failed > 0 ? ", " + failed + " failed" : ""));
|
2026-11-28 18:33:08 +00:00
|
|
|
Siblings.Init();
|
2026-11-27 18:53:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefOf]
|
|
|
|
|
public static class BlaxDefOf
|
|
|
|
|
{
|
|
|
|
|
public static HediffDef BlaxSlaves_SlaveBrand;
|
|
|
|
|
|
|
|
|
|
static BlaxDefOf()
|
|
|
|
|
{
|
|
|
|
|
DefOfHelper.EnsureInitializedInCtor(typeof(BlaxDefOf));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|