169 lines
6.8 KiB
C#
169 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using RimWorld;
|
|
using Verse;
|
|
using Verse.AI;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>
|
|
/// The subdue job's driver (JobDef BlaxLaws_Subdue, design: "Subdue"). One toil,
|
|
/// Toils_Combat.FollowAndMeleeAttack (Toils_Combat.cs:115-178): it follows the target, calls Hit while
|
|
/// adjacent and ends when the target is downed (:165-167) or unreachable (:132-134). The toil runs as a
|
|
/// tickIntervalAction, which Thing.DoTick calls every 1 to 15 ticks depending on the camera
|
|
/// (Thing.cs:726-735; GenTicks.cs:130-142), so hits are timed by game ticks, never by calls: one every 90
|
|
/// ticks, also off screen.
|
|
///
|
|
/// The job ends with a message once three hits in a row add no pain (a hit counts as painless when
|
|
/// PainTotal rises by less than a quarter of the new injury's own pain: go-juice's pain factor of 0.1,
|
|
/// painkillers, every allowed part at its cap, the pawn's injuries at the total cap of SubdueHits) or after
|
|
/// 40 hits. The beaten pawn loses the break's
|
|
/// recovery thought (Catharsis) that the downing gives through RecoverFromState (Pawn_HealthTracker.cs:
|
|
/// 850-853; MentalState.cs:92-95) while "Subdued pawns lose their catharsis" is on.
|
|
///
|
|
/// Never saved: the JobDef isn't suspendable (an interrupt ends it instead of queueing it), the job
|
|
/// reserves nothing, and Patch_Pawn_JobTracker_ExposeData hides it from the save. So none of its fields
|
|
/// needs saving; after a load the sweep issues a new subdue.
|
|
/// </summary>
|
|
public class JobDriver_Subdue : JobDriver
|
|
{
|
|
public const int HitIntervalTicks = 90;
|
|
public const int MaxHits = 40;
|
|
public const int PainlessHitsToStop = 3;
|
|
|
|
/// <summary>A hit is painful when PainTotal rises by at least this share of the injury's own pain.</summary>
|
|
public const float PainShare = 0.25f;
|
|
|
|
private int lastHitTick = -1000000;
|
|
private int hits;
|
|
private int painlessHits;
|
|
private LawCase lawCase;
|
|
|
|
private static readonly List<Thought_Memory> memoriesBefore = new List<Thought_Memory>();
|
|
private static readonly List<int> agesBefore = new List<int>();
|
|
|
|
public Pawn Target => job?.GetTarget(TargetIndex.A).Thing as Pawn;
|
|
|
|
/// <summary>Hits dealt so far.</summary>
|
|
public int Hits => hits;
|
|
|
|
/// <summary>The job reserves nothing, so no reservation list ever refers to it.</summary>
|
|
public override bool TryMakePreToilReservations(bool errorOnFailed) => true;
|
|
|
|
public override void Notify_Starting()
|
|
{
|
|
base.Notify_Starting();
|
|
lawCase = SubdueFlow.CaseFor(pawn, Target);
|
|
}
|
|
|
|
protected override IEnumerable<Toil> MakeNewToils()
|
|
{
|
|
this.FailOnDespawnedOrNull(TargetIndex.A);
|
|
this.FailOn(() => Target == null || Target.Dead || lawCase == null || lawCase.IsClosed || lawCase.enforcer != pawn);
|
|
// No longer in the break the case recorded (it ended, or was cured some other way).
|
|
this.FailOn(() => Target != null && !Target.Downed && !SubdueFlow.InBreak(lawCase));
|
|
yield return Toils_Combat.FollowAndMeleeAttack(TargetIndex.A, Hit);
|
|
}
|
|
|
|
private void Hit()
|
|
{
|
|
try
|
|
{
|
|
Pawn target = Target;
|
|
if (target == null || lawCase == null || target.Dead)
|
|
return;
|
|
int now = Find.TickManager.TicksGame;
|
|
if (now - lastHitTick < HitIntervalTicks)
|
|
return;
|
|
lastHitTick = now;
|
|
hits++;
|
|
pawn.rotationTracker?.FaceTarget(target);
|
|
|
|
ThoughtDef recovery = target.MentalStateDef?.moodRecoveryThought;
|
|
bool dropRecovery = recovery != null && BlaxLawsMod.S.subdueRemovesCatharsis;
|
|
if (dropRecovery)
|
|
RememberMemories(target, recovery);
|
|
float painBefore = target.health.hediffSet.PainTotal;
|
|
|
|
float injuryPain = SubdueHits.Hit(pawn, target);
|
|
|
|
if (target.Dead)
|
|
{
|
|
EndJobWith(JobCondition.Incompletable);
|
|
return;
|
|
}
|
|
if (target.Downed)
|
|
{
|
|
if (dropRecovery)
|
|
DropNewMemories(target, recovery);
|
|
SubdueFlow.OnDowned(lawCase, pawn);
|
|
ReadyForNextToil();
|
|
return;
|
|
}
|
|
float gain = target.health.hediffSet.PainTotal - painBefore;
|
|
if (injuryPain <= 0f || gain < injuryPain * PainShare)
|
|
painlessHits++;
|
|
else
|
|
painlessHits = 0;
|
|
if (painlessHits >= PainlessHitsToStop || hits >= MaxHits)
|
|
{
|
|
SubdueFlow.OnGaveUp(lawCase, pawn);
|
|
EndJobWith(JobCondition.Incompletable);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
HookGuard.Fail("Subdue hit", e);
|
|
try
|
|
{
|
|
EndJobWith(JobCondition.Incompletable);
|
|
}
|
|
catch (Exception e2)
|
|
{
|
|
HookGuard.Fail("Subdue end after a failed hit", e2);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>The memories of the recovery thought the target has before the hit, with their ages.</summary>
|
|
private static void RememberMemories(Pawn target, ThoughtDef def)
|
|
{
|
|
memoriesBefore.Clear();
|
|
agesBefore.Clear();
|
|
List<Thought_Memory> list = target.needs?.mood?.thoughts?.memories?.Memories;
|
|
if (list == null)
|
|
return;
|
|
foreach (Thought_Memory m in list)
|
|
{
|
|
if (m.def == def)
|
|
{
|
|
memoriesBefore.Add(m);
|
|
agesBefore.Add(m.age);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Removes the recovery memory the downing gave, and puts back the age of one it only renewed.</summary>
|
|
private static void DropNewMemories(Pawn target, ThoughtDef def)
|
|
{
|
|
MemoryThoughtHandler handler = target.needs?.mood?.thoughts?.memories;
|
|
if (handler == null)
|
|
return;
|
|
List<Thought_Memory> now = handler.Memories;
|
|
for (int i = now.Count - 1; i >= 0; i--)
|
|
{
|
|
Thought_Memory m = now[i];
|
|
if (m.def != def)
|
|
continue;
|
|
int idx = memoriesBefore.IndexOf(m);
|
|
if (idx < 0)
|
|
handler.RemoveMemory(m);
|
|
else if (m.age < agesBefore[idx])
|
|
m.age = agesBefore[idx];
|
|
}
|
|
memoriesBefore.Clear();
|
|
agesBefore.Clear();
|
|
}
|
|
}
|
|
}
|