using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxSlaves
{
public class HediffCompProperties_BrandOwner : HediffCompProperties
{
public HediffCompProperties_BrandOwner()
{
compClass = typeof(HediffComp_BrandOwner);
}
}
/// Remembers whose mark the brand is. The name is cached in case the faction is removed.
public class HediffComp_BrandOwner : HediffComp
{
public Faction owner;
public string ownerName;
public string OwnerName => owner?.Name ?? ownerName;
public void SetOwner(Faction faction)
{
owner = faction;
ownerName = faction?.Name;
}
public override string CompLabelInBracketsExtra => OwnerName;
public override string CompTipStringExtra =>
OwnerName.NullOrEmpty() ? null : (string)"BlaxSlaves_BrandOwnerTip".Translate(OwnerName);
public override void CompExposeData()
{
base.CompExposeData();
if (Scribe.mode == LoadSaveMode.Saving && owner != null && !Find.FactionManager.AllFactionsListForReading.Contains(owner))
owner = null;
Scribe_References.Look(ref owner, "owner");
Scribe_Values.Look(ref ownerName, "ownerName");
}
public override void CopyFrom(HediffComp other)
{
base.CopyFrom(other);
if (other is HediffComp_BrandOwner o)
{
owner = o.owner;
ownerName = o.ownerName;
}
}
}
public static class BrandUtility
{
public const float FreshBurnSeverity = 10f;
public static bool IsBranded(Pawn pawn) =>
pawn?.health?.hediffSet != null && pawn.health.hediffSet.HasHediff(BlaxDefOf.BlaxSlaves_SlaveBrand);
public static HediffComp_BrandOwner BrandOf(Pawn pawn) =>
pawn?.health?.hediffSet?.GetFirstHediffOfDef(BlaxDefOf.BlaxSlaves_SlaveBrand)?.TryGetComp();
public static bool IsBrandedByPlayer(Pawn pawn)
{
HediffComp_BrandOwner brand = BrandOf(pawn);
return brand != null && brand.owner != null && brand.owner == Faction.OfPlayer;
}
public static void Brand(Pawn pawn, Faction owner)
{
if (IsBranded(pawn))
{
BrandOf(pawn)?.SetOwner(owner);
return;
}
Hediff brand = HediffMaker.MakeHediff(BlaxDefOf.BlaxSlaves_SlaveBrand, pawn);
brand.TryGetComp()?.SetOwner(owner);
pawn.health.AddHediff(brand);
}
/// A fresh burn on the core part (torso for humans): about a day of pain.
public static void AddFreshBurn(Pawn pawn, float severity = FreshBurnSeverity)
{
HediffDef burnDef = DamageDefOf.Burn?.hediff;
BodyPartRecord part = pawn.RaceProps?.body?.corePart;
if (burnDef == null || part == null || pawn.health.hediffSet.PartIsMissing(part))
return;
if (pawn.health.WouldDieAfterAddingHediff(burnDef, part, severity))
return;
Hediff burn = HediffMaker.MakeHediff(burnDef, pawn, part);
burn.Severity = severity;
// A deliberate mark, like scarification: no infection roll. After a reload the recreated
// comp starts inert (ticksUntilInfect defaults to "will not infect").
(burn as HediffWithComps)?.comps?.RemoveAll(c => c is HediffComp_Infecter);
pawn.health.AddHediff(burn, part);
}
}
/// "Branded" mood only while they're someone's slave; a freed pawn keeps the scar, not the shame.
public class ThoughtWorker_Branded : ThoughtWorker_Hediff
{
protected override ThoughtState CurrentStateInternal(Pawn p) =>
p.IsSlave ? base.CurrentStateInternal(p) : ThoughtState.Inactive;
}
/// Branding surgery: colony slaves only. Never fails, marks them as the player's.
public class Recipe_BrandSlave : Recipe_AddHediff
{
public override bool AvailableOnNow(Thing thing, BodyPartRecord part = null) =>
thing is Pawn pawn && pawn.IsSlaveOfColony && base.AvailableOnNow(thing, part);
public override bool CompletableEver(Pawn surgeryTarget) =>
base.CompletableEver(surgeryTarget) && surgeryTarget.IsSlaveOfColony;
protected override void OnSurgerySuccess(Pawn pawn, BodyPartRecord part, Pawn billDoer, List ingredients, Bill bill)
{
base.OnSurgerySuccess(pawn, part, billDoer, ingredients, bill);
BrandUtility.BrandOf(pawn)?.SetOwner(Faction.OfPlayer);
BrandUtility.AddFreshBurn(pawn);
if (billDoer != null)
Messages.Message("BlaxSlaves_BrandedMessage".Translate(billDoer.Named("DOCTOR"), pawn.Named("PAWN")), pawn, MessageTypeDefOf.NeutralEvent);
}
}
/// Burns the colony's mark over another owner's brand.
public class Recipe_RebrandSlave : Recipe_Surgery
{
public override bool AvailableOnNow(Thing thing, BodyPartRecord part = null)
{
if (!(thing is Pawn pawn) || !pawn.IsSlaveOfColony)
return false;
HediffComp_BrandOwner brand = BrandUtility.BrandOf(pawn);
return brand != null && brand.owner != Faction.OfPlayer
&& !pawn.BillStack.Bills.Any(b => b.recipe == recipe)
&& base.AvailableOnNow(thing, part);
}
public override bool CompletableEver(Pawn surgeryTarget) =>
base.CompletableEver(surgeryTarget) && surgeryTarget.IsSlaveOfColony
&& BrandUtility.BrandOf(surgeryTarget) is HediffComp_BrandOwner brand && brand.owner != Faction.OfPlayer;
public override void ApplyOnPawn(Pawn pawn, BodyPartRecord part, Pawn billDoer, List ingredients, Bill bill)
{
HediffComp_BrandOwner brand = BrandUtility.BrandOf(pawn);
if (brand == null || brand.owner == Faction.OfPlayer)
return;
brand.SetOwner(Faction.OfPlayer);
BrandUtility.AddFreshBurn(pawn);
if (billDoer != null)
Messages.Message("BlaxSlaves_RebrandedMessage".Translate(billDoer.Named("DOCTOR"), pawn.Named("PAWN")), pawn, MessageTypeDefOf.NeutralEvent);
}
}
/// Some slaves in trader stock already carry an owner's brand: the trader's faction, or another one.
[HarmonyPatch(typeof(ThingWithComps), nameof(ThingWithComps.PostGeneratedForTrader))]
public static class Patch_TraderSlaveBrand
{
public static void Postfix(ThingWithComps __instance, TraderKindDef trader, Faction forFaction)
{
if (!(__instance is Pawn pawn) || pawn.Dead || pawn.RaceProps == null || !pawn.RaceProps.Humanlike)
return;
if (pawn.ageTracker == null || !pawn.ageTracker.Adult || BrandUtility.IsBranded(pawn))
return;
if (!IsSlaveStock(pawn, trader))
return;
if (pawn.guest != null && pawn.guest.joinStatus == JoinStatus.JoinAsColonist)
return;
if (!Rand.Chance(BlaxSlavesMod.Settings.traderBrandChance))
return;
Faction owner = forFaction;
if (owner == null || owner.IsPlayer)
{
owner = Find.FactionManager.AllFactionsVisible
.Where(f => !f.IsPlayer && !f.temporary && f.def.humanlikeFaction && f != pawn.Faction)
.RandomElementWithFallback();
}
if (owner != null)
BrandUtility.Brand(pawn, owner);
}
///
/// Only pawns a slave generator made: the Slave kind (vanilla, and More Slaves' own generator),
/// or a trader's custom slave kind (Royalty's Slave_Empire). Not hired pawns from mods like Rimdeed.
///
private static bool IsSlaveStock(Pawn pawn, TraderKindDef trader)
{
if (trader != null && trader.defName == "RD_PawnTrader")
return false;
if (pawn.kindDef == PawnKindDefOf.Slave)
return true;
return trader?.stockGenerators != null
&& trader.stockGenerators.Any(g => g is StockGenerator_Slaves s && s.slaveKindDef != null && s.slaveKindDef == pawn.kindDef);
}
}
}