Files
ArachnaeSwarm/Source/ArachnaeSwarm/HediffGiver_RandomWithSeverity.cs

54 lines
2.1 KiB
C#

using Verse;
namespace ArachnaeSwarm
{
/// <summary>
/// A custom HediffGiver that is similar to HediffGiver_Random,
/// but allows specifying a random severity range for the given hediff.
/// </summary>
public class HediffGiver_RandomWithSeverity : HediffGiver
{
// XML configurable fields
public float mtbDays;
public FloatRange severityRange = new FloatRange(0.01f, 1f);
public override void OnIntervalPassed(Pawn pawn, Hediff cause)
{
// Calculate the chance based on Mean Time Between (MTB) days and pawn-specific factors
float mtb = this.mtbDays;
float chanceFactor = ChanceFactor(pawn);
if (chanceFactor != 0f && Rand.MTBEventOccurs(mtb / chanceFactor, 60000f, 60f))
{
// Try to apply the hediff with our custom logic
if (TryApplyWithCustomSeverity(pawn))
{
// If successful, send a letter to the player
SendLetter(pawn, cause);
}
}
}
/// <summary>
/// Applies the hediff and then sets its severity to a random value within the specified range.
/// </summary>
/// <param name="pawn">The pawn to apply the hediff to.</param>
/// <returns>True if the hediff was successfully applied, false otherwise.</returns>
private bool TryApplyWithCustomSeverity(Pawn pawn)
{
// First, apply the hediff using the base class logic.
// This will add the hediff with its default initial severity.
if (base.TryApply(pawn))
{
// If the hediff was successfully added, find it.
Hediff hediff = pawn.health.hediffSet.GetFirstHediffOfDef(this.hediff);
if (hediff != null)
{
// Set its severity to a random value from our configured range.
hediff.Severity = this.severityRange.RandomInRange;
return true;
}
}
return false;
}
}
}