rjw-sexperience-ideology/Source/IdeologyAddon/HistoryEvents/HistoryEventDefExtensionMethods.cs

57 lines
2.2 KiB
C#
Raw Permalink Normal View History

2022-07-26 03:55:56 +00:00
using RimWorld;
using Verse;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
2022-07-26 03:55:56 +00:00
namespace RJWSexperience.Ideology.HistoryEvents
{
public static class HistoryEventDefExtensionMethods
{
public static void RecordEventWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner)
{
//Log.Message($"[RSI] Recording event {def.ToStringWithPartner(pawn, partner)}");
List<TwoPawnEventRule> secondaryEventRules = def.GetModExtension<DefExtension_SecondaryEvents>()?.generationRules;
2022-07-26 03:55:56 +00:00
if (!secondaryEventRules.NullOrEmpty())
2022-07-26 03:55:56 +00:00
{
//Log.Message($"[RSI] Event has {secondaryEventRules?.Count} secondary events");
foreach (var rule in secondaryEventRules.Where(rule => rule.Applies(pawn, partner)))
2022-07-26 03:55:56 +00:00
{
//Log.Message($"[RSI] Recording secondary event {def.defName}");
rule.historyEventDef.RecordEventWithPartner(pawn, partner);
2022-07-26 03:55:56 +00:00
}
}
HistoryEvent historyEvent = def.CreateEventWithPartner(pawn, partner);
Find.HistoryEventsManager.RecordEvent(historyEvent);
//Log.Message($"[RSI] Recorded event {historyEvent.def.ToStringWithPartner(pawn, partner)}");
2022-07-26 03:55:56 +00:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2022-07-26 03:55:56 +00:00
public static HistoryEvent CreateEvent(this HistoryEventDef def, Pawn pawn)
{
return new HistoryEvent(def, pawn.Named(HistoryEventArgsNames.Doer));
}
public static HistoryEvent CreateEventWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner)
{
//Log.Message($"[RSI] Creating event {def.ToStringWithPartner(pawn, partner)}");
HistoryEventDef overrideEvent = def.GetModExtension<DefExtension_EventOverrides>()?.overrideRules.FirstOrFallback(rule => rule.Applies(pawn, partner))?.historyEventDef;
2022-07-26 03:55:56 +00:00
if (overrideEvent != null)
2022-07-26 03:55:56 +00:00
{
//Log.Message($"[RSI] Event overridden by {overrideEvent.ToStringWithPartner(pawn, partner)}");
return overrideEvent.CreateEventWithPartner(pawn, partner);
2022-07-26 03:55:56 +00:00
}
return new HistoryEvent(def, pawn.Named(HistoryEventArgsNames.Doer), partner.Named(ArgsNamesCustom.Partner));
}
private static string ToStringWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner)
{
return $"{def.defName}, doer {pawn.NameShortColored}, partner {partner.NameShortColored}";
}
2022-07-26 03:55:56 +00:00
}
}