using System; using HarmonyLib; using RimWorld; using UnityEngine; using Verse; namespace BlaxSlaves { /// /// Slave price sliders (1.2). With Ideology, vanilla charges 60% of the colonist price for a pawn /// that joins as a slave and pays 60% for any humanlike pawn the player sells. The settings /// slaveBuyFactor and slaveSellFactor replace those two factors, and a hard cap keeps a captive's /// sale price below what the best possible negotiator would pay for that pawn as a slave, so the /// sliders never make buying low and selling high pay, between traders or by switching negotiators. /// Raising only the sell slider never pays less than vanilla. Later pricing (appraisal, abolitionist buyers, /// indenture) extends Patch_SlavePrices rather than adding another patch on the same method. /// public static class SlavePrices { /// Vanilla's slave factor (Tradeable.PriceFactor_JoinAsSlaveOrColonyPawn, private). public const float VanillaFactor = 0.6f; /// /// A captive sells for at most this share of what the best possible negotiator would pay for it as a /// slave (see SellCap). /// public const float CapShareOfSlaveBuyPrice = 0.9f; /// Above this, vanilla rounds trade prices to whole silver (TradeUtility.GetPricePlayerBuy/Sell). private const float RoundAbove = 99.5f; /// The slider steps by 5%, so a small tolerance is enough to call a factor vanilla. public static bool IsVanilla(float factor) => Mathf.Abs(factor - VanillaFactor) < 0.001f; /// /// True when either slider is off vanilla. At 60%/60% nothing is touched, cap included: vanilla's own /// prices can exceed the cap (a negotiator's trade gain above about 21% at a settlement or 27% at a ship, /// or a trader that sells humans cheap), and the defaults must stay exactly vanilla. /// public static bool AnyFactorChanged(BlaxSlavesSettings s) => s != null && !(IsVanilla(s.slaveSellFactor) && IsVanilla(s.slaveBuyFactor)); /// /// True when the buy slider is at vanilla or above. There a sale at vanilla's own price faces purchase prices /// at or above vanilla's, so it opens no loop that vanilla doesn't already have. /// public static bool BuyAtOrAboveVanilla(float slaveBuyFactor) => IsVanilla(slaveBuyFactor) || slaveBuyFactor > VanillaFactor; /// /// The best trade gain any negotiator can reach: TradePriceImprovement's maxValue (39.5% in Core, where /// StatWorker.FinalizeValue clamps the stat), or the current negotiator's gain if a mod pushes it past that. /// public static float BestNegotiatorGain(float priceGain_PlayerNegotiator) => Mathf.Max(StatDefOf.TradePriceImprovement.maxValue, priceGain_PlayerNegotiator); /// /// The most the player gets for selling this humanlike pawn: CapShareOfSlaveBuyPrice x what the best /// possible negotiator (BestNegotiatorGain) would pay this trader for it as a slave at slaveBuyFactor, /// with this trader's settlement gain. The trader's buy price type counts at most Normal, so a trader /// that sells humans expensive doesn't lift the cap above a Normal trader's slave price. Independent of /// who negotiates, so buying with a skilled negotiator and selling with a poor one gains nothing, and a /// better negotiator never gets less. Whole silver above 99.5 like vanilla's prices, rounded down so it /// never exceeds the share. /// public static float SellCap(Thing pawn, float priceFactorBuy_TraderPriceType, float priceGain_PlayerNegotiator, float priceGain_Settlement, float slaveBuyFactor) { float priceType = Mathf.Min(priceFactorBuy_TraderPriceType, PriceType.Normal.PriceMultiplier()); float slaveBuy = TradeUtility.GetPricePlayerBuy(pawn, priceType, slaveBuyFactor, BestNegotiatorGain(priceGain_PlayerNegotiator), priceGain_Settlement); float cap = slaveBuy * CapShareOfSlaveBuyPrice; if (cap > RoundAbove) cap = Mathf.Floor(cap); return Mathf.Max(cap, TradeUtility.MinimumSellPrice); } } /// /// Applies the slave price sliders where vanilla computes trade prices. Vanilla stores both prices already /// clamped and rounded, so they are recomputed from the stored price types and gains with the new factors /// instead of being scaled. The factor fields are what vanilla's GetPriceTooltip prints ("Slave: x50%" on /// the buy side, "x 0.80 (Slave)" on the sell side), so the tooltip follows with no extra patch. /// Fields are bound by Harmony's ___name injection: a renamed field fails this class at patch time /// instead of throwing on every price lookup. /// [HarmonyPatch(typeof(Tradeable), "InitPriceDataIfNeeded")] public static class Patch_SlavePrices { /// /// Vanilla returns at once while pricePlayerBuy is above 0 (cached). Only a real compute resets the /// factor fields, so the postfix acts only then. That keeps it idempotent: buy mode invalidates the /// prices (sets them to -1) and calls in again, which runs vanilla and this postfix once more from scratch. /// public static void Prefix(float ___pricePlayerBuy, out bool __state) { __state = ___pricePlayerBuy <= 0f; } public static void Postfix(Tradeable __instance, bool __state, ref float ___pricePlayerBuy, ref float ___pricePlayerSell, ref float ___priceFactorBuy_JoinAs, ref float ___priceFactorSell_HumanPawn, float ___priceFactorBuy_TraderPriceType, float ___priceFactorSell_TraderPriceType, float ___priceGain_PlayerNegotiator, float ___priceGain_Settlement, float ___priceGain_DrugBonus, float ___priceGain_AnimalProduceBonus) { if (!__state || !ModsConfig.IdeologyActive || !TradeSession.Active) return; BlaxSlavesSettings s = BlaxSlavesMod.Settings; if (!SlavePrices.AnyFactorChanged(s)) return; try { // AnyThing without its "lacks AnyThing" error for thing-less tradeables (royal favor). if (!((__instance.FirstThingColony ?? __instance.FirstThingTrader) is Pawn pawn) || pawn.RaceProps == null || !pawn.RaceProps.Humanlike) return; // Only the factors vanilla just set to its slave value: a pawn joining as a colonist keeps 1 on the // buy side, a humanlike without a guest tracker keeps 1 on the sell side. bool slaveBuy = SlavePrices.IsVanilla(___priceFactorBuy_JoinAs); bool humanSell = SlavePrices.IsVanilla(___priceFactorSell_HumanPawn); if (!slaveBuy && !humanSell) return; float joinAs = slaveBuy ? s.slaveBuyFactor : ___priceFactorBuy_JoinAs; float human = humanSell ? s.slaveSellFactor : ___priceFactorSell_HumanPawn; TradeCurrency currency = TradeSession.TradeCurrency; float buy = TradeUtility.GetPricePlayerBuy(pawn, ___priceFactorBuy_TraderPriceType, joinAs, ___priceGain_PlayerNegotiator, ___priceGain_Settlement); float sell = TradeUtility.GetPricePlayerSell(pawn, ___priceFactorSell_TraderPriceType, human, ___priceGain_PlayerNegotiator, ___priceGain_Settlement, ___priceGain_DrugBonus, ___priceGain_AnimalProduceBonus, currency); if (sell >= buy) sell = buy; // Favor traders pay in favor (RoyalFavorValue), which a silver cap can't bound. if (humanSell && currency == TradeCurrency.Silver) { float cap = SlavePrices.SellCap(pawn, ___priceFactorBuy_TraderPriceType, ___priceGain_PlayerNegotiator, ___priceGain_Settlement, s.slaveBuyFactor); // A skilled seller's vanilla price can sit above the cap, so raising only the sell slider // would pay less than vanilla. Vanilla's own 60% sale is the floor while slaves cost vanilla // or more (it adds no loop vanilla lacks); below that the floor would reopen the loop. if (SlavePrices.BuyAtOrAboveVanilla(s.slaveBuyFactor)) cap = Mathf.Max(cap, TradeUtility.GetPricePlayerSell(pawn, ___priceFactorSell_TraderPriceType, SlavePrices.VanillaFactor, ___priceGain_PlayerNegotiator, ___priceGain_Settlement, ___priceGain_DrugBonus, ___priceGain_AnimalProduceBonus, currency)); sell = Mathf.Min(sell, cap); } ___priceFactorBuy_JoinAs = joinAs; ___priceFactorSell_HumanPawn = human; ___pricePlayerBuy = buy; ___pricePlayerSell = sell; } catch (Exception e) { // The trade window asks for prices every frame; log once and leave vanilla's prices. Log.ErrorOnce("[Blax Slaves] Slave price sliders failed, vanilla prices kept: " + e, 0x51A7E0); } } } }