299 lines
14 KiB
C#
299 lines
14 KiB
C#
using KernelPanic.Core;
|
|
using UnityEngine;
|
|
|
|
namespace KernelPanic.Combat
|
|
{
|
|
public readonly struct DamageRequest
|
|
{
|
|
public DamageRequest(
|
|
CombatantState source,
|
|
CombatantState target,
|
|
int amount,
|
|
Language language,
|
|
bool trueDamage,
|
|
bool canCrit,
|
|
bool applySourceModifiers = true)
|
|
{
|
|
Source = source;
|
|
Target = target;
|
|
Amount = amount;
|
|
Language = language;
|
|
TrueDamage = trueDamage;
|
|
CanCrit = canCrit;
|
|
ApplySourceModifiers = applySourceModifiers;
|
|
LazyMultiplierPercent = 100;
|
|
CompilationPercent = 100;
|
|
IsCardDamage = false;
|
|
Cause = null;
|
|
}
|
|
|
|
// Explicit provenance: DOT, package damage, enemy damage, recoil and spill use the
|
|
// original constructor, so none can inherit a card's execution modifiers accidentally.
|
|
public static DamageRequest FromCard(CombatContext context, CombatantState target, int amount,
|
|
Language language, bool trueDamage, bool canCrit) => new(
|
|
new DamageRequest(context.Source, target, amount, language, trueDamage, canCrit), context);
|
|
|
|
private DamageRequest(DamageRequest request, CombatContext context)
|
|
: this(request.Source, request.Target, request.Amount, request.Language, request.TrueDamage, request.CanCrit)
|
|
{
|
|
IsCardDamage = true;
|
|
LazyMultiplierPercent = context.LazyMultiplierPercent;
|
|
CompilationPercent = context.CombatManager?.Compilation.DamagePercent ?? 100;
|
|
}
|
|
|
|
// Preview requests retain card provenance without executing a card or constructing a
|
|
// mutable combat context. Multipliers describe the prospective execution snapshot.
|
|
public static DamageRequest PreviewCard(CombatantState source, CombatantState target, int amount,
|
|
Language language, bool trueDamage, bool canCrit, int lazyPercent, int compilationPercent) =>
|
|
new(source, target, amount, language, trueDamage, canCrit, lazyPercent, compilationPercent);
|
|
|
|
private DamageRequest(CombatantState source, CombatantState target, int amount,
|
|
Language language, bool trueDamage, bool canCrit, int lazyPercent, int compilationPercent)
|
|
: this(source, target, amount, language, trueDamage, canCrit)
|
|
{
|
|
IsCardDamage = true;
|
|
LazyMultiplierPercent = lazyPercent;
|
|
CompilationPercent = compilationPercent;
|
|
}
|
|
|
|
public CombatantState Source { get; }
|
|
public CombatantState Target { get; }
|
|
public int Amount { get; }
|
|
public Language Language { get; }
|
|
public bool TrueDamage { get; }
|
|
public bool CanCrit { get; }
|
|
public bool ApplySourceModifiers { get; }
|
|
public bool IsCardDamage { get; }
|
|
public int LazyMultiplierPercent { get; }
|
|
public int CompilationPercent { get; }
|
|
public string Cause { get; }
|
|
|
|
public DamageRequest WithCause(string cause) => new(this, cause);
|
|
|
|
private DamageRequest(DamageRequest request, string cause)
|
|
: this(request.Source, request.Target, request.Amount, request.Language, request.TrueDamage,
|
|
request.CanCrit, request.ApplySourceModifiers)
|
|
{
|
|
IsCardDamage = request.IsCardDamage;
|
|
LazyMultiplierPercent = request.LazyMultiplierPercent;
|
|
CompilationPercent = request.CompilationPercent;
|
|
Cause = cause;
|
|
}
|
|
}
|
|
|
|
public readonly struct DamageResult
|
|
{
|
|
public DamageResult(int finalAmount, bool targetDefeated, bool wasCritical = false, int absorbedAmount = 0, int shieldDamage = 0, int uptimeDamage = 0, int incomingAmount = 0)
|
|
{
|
|
FinalAmount = finalAmount;
|
|
TargetDefeated = targetDefeated;
|
|
WasCritical = wasCritical;
|
|
AbsorbedAmount = absorbedAmount;
|
|
ShieldDamage = shieldDamage;
|
|
UptimeDamage = uptimeDamage;
|
|
IncomingAmount = incomingAmount;
|
|
}
|
|
|
|
public int FinalAmount { get; }
|
|
public bool TargetDefeated { get; }
|
|
public bool WasCritical { get; }
|
|
public int AbsorbedAmount { get; }
|
|
public int ShieldDamage { get; }
|
|
public int UptimeDamage { get; }
|
|
public int IncomingAmount { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Single damage chokepoint for shield, uptime, damage events, and defeat decisions.
|
|
/// </summary>
|
|
public sealed class DamagePipeline
|
|
{
|
|
private System.Func<DamageRequest, int, int> _resistanceResolver;
|
|
public System.Func<DamageRequest, int, int> ResistanceResolver
|
|
{
|
|
get => _resistanceResolver;
|
|
set
|
|
{
|
|
if (_resistanceResolver != value) PreviewResistanceResolver = null;
|
|
_resistanceResolver = value;
|
|
}
|
|
}
|
|
// Only explicitly pure resistance resolvers are usable by a read-only preview. Never
|
|
// invoke the runtime delegate here: custom runtime hooks may consume resources.
|
|
public System.Func<DamageRequest, int, int> PreviewResistanceResolver { get; set; }
|
|
|
|
public bool CanPreviewResistance => ResistanceResolver == null || PreviewResistanceResolver != null;
|
|
|
|
public int PreviewIncomingDamage(DamageRequest request, bool critical = false,
|
|
int? archStacks = null, int? sourceMultiplierPercent = null, int? cardMultiplierPercent = null)
|
|
{
|
|
int amount = Mathf.Max(0, request.Amount);
|
|
if (request.ApplySourceModifiers)
|
|
{
|
|
amount = ApplyFlatAdditions(amount, request, out _, archStacks);
|
|
amount = ApplyMultipliers(amount, request, sourceMultiplierPercent, cardMultiplierPercent);
|
|
if (critical && CanRollCritical(request, amount)) amount = ScaleCritical(amount);
|
|
}
|
|
return PreviewResistanceResolver == null ? amount : PreviewResistanceResolver(request, amount);
|
|
}
|
|
|
|
public DamageResult DefeatCombatant(CombatantState target, CombatantState source, Language language)
|
|
{
|
|
if (target == null || target.IsDefeated)
|
|
{
|
|
return new DamageResult(0, false);
|
|
}
|
|
|
|
int uptimeDamage = Mathf.Max(0, target.CurrentUptime);
|
|
target.CurrentUptime = 0;
|
|
GameEvents.RaiseDamageDealt(new DamageDealtEvent(source, target, uptimeDamage, language, uptimeDamage, 0, false, 0, uptimeDamage, true));
|
|
target.IsDefeated = true;
|
|
GameEvents.RaiseCombatantDefeated(new CombatantDefeatedEvent(target));
|
|
return new DamageResult(uptimeDamage, true, false, 0, 0, uptimeDamage, uptimeDamage);
|
|
}
|
|
|
|
public DamageResult DealDamage(DamageRequest request)
|
|
{
|
|
if (request.Target == null || request.Target.IsDefeated)
|
|
{
|
|
return new DamageResult(0, false);
|
|
}
|
|
|
|
int amount = Mathf.Max(0, request.Amount);
|
|
bool wasCritical = false;
|
|
int archBtwBonus = 0;
|
|
if (request.ApplySourceModifiers)
|
|
{
|
|
amount = ApplyFlatAdditions(amount, request, out archBtwBonus);
|
|
amount = ApplyMultipliers(amount, request);
|
|
amount = ApplyCrit(amount, request, out wasCritical);
|
|
}
|
|
|
|
amount = ApplyResistancesAndWeaknesses(amount, request);
|
|
|
|
int finalAmount = ApplyShieldAndUptime(amount, request, out int absorbedAmount, out int shieldDamage, out int uptimeDamage);
|
|
ApplyArchRollingReleaseSave(request);
|
|
bool rollingReleaseSaveTriggered = request.Target != null && request.Target.ArchRollingReleaseRecoveredThisHit;
|
|
GameEvents.RaiseDamageDealt(new DamageDealtEvent(request.Source, request.Target, finalAmount, request.Language, amount, absorbedAmount, wasCritical, shieldDamage, uptimeDamage, request.TrueDamage, archBtwBonus, rollingReleaseSaveTriggered).WithCause(request.Cause));
|
|
|
|
bool defeated = false;
|
|
if (request.Target.CurrentUptime <= 0 && !request.Target.IsDefeated)
|
|
{
|
|
request.Target.IsDefeated = true;
|
|
defeated = true;
|
|
GameEvents.RaiseCombatantDefeated(new CombatantDefeatedEvent(request.Target));
|
|
}
|
|
|
|
return new DamageResult(finalAmount, defeated, wasCritical, absorbedAmount, shieldDamage, uptimeDamage, amount);
|
|
}
|
|
|
|
private static int ApplyMultipliers(int amount, DamageRequest request,
|
|
int? sourceMultiplierPercent = null, int? cardMultiplierPercent = null)
|
|
{
|
|
if (amount == int.MaxValue)
|
|
{
|
|
return amount;
|
|
}
|
|
|
|
if (request.Source != null && request.Source.IgnoreDamageMultipliers)
|
|
{
|
|
// Mint V1+ ignores multiplicative damage modifiers. Mint V4 still allows flat
|
|
// bonuses before this step; no flat buff status exists yet, so the flag is a hook.
|
|
return amount;
|
|
}
|
|
|
|
int multiplier = Mathf.Max(0, sourceMultiplierPercent ?? request.Source?.DamageMultiplierPercent ?? 100);
|
|
int currentCardMultiplier = Mathf.Max(0, cardMultiplierPercent ?? request.Source?.CurrentCardDamageMultiplierPercent ?? 100);
|
|
if (request.IsCardDamage && (request.LazyMultiplierPercent != 100 || request.CompilationPercent != 100 || request.Source?.GovernorDamagePercent != 100))
|
|
return CardScaling.Scale(amount, request.LazyMultiplierPercent, request.CompilationPercent, multiplier, currentCardMultiplier, request.Source?.GovernorDamagePercent ?? 100);
|
|
return Mathf.RoundToInt(amount * (multiplier / 100f) * (currentCardMultiplier / 100f));
|
|
}
|
|
|
|
private static int ApplyFlatAdditions(int amount, DamageRequest request, out int archBtwBonus, int? archStacks = null)
|
|
{
|
|
archBtwBonus = 0;
|
|
if (amount == int.MaxValue)
|
|
{
|
|
return amount;
|
|
}
|
|
|
|
if (request.Source != null && request.Language == Language.JavaScript)
|
|
{
|
|
amount += Mathf.Max(0, request.Source.JavaScriptFlatDamageBonus);
|
|
}
|
|
|
|
if (request.Source != null && (request.Language == Language.C || request.Language == Language.Rust))
|
|
{
|
|
archBtwBonus = Mathf.Max(0, archStacks ?? request.Source.ArchBtwStacks) * Mathf.Max(1, request.Source.ArchBtwDamagePerStack);
|
|
amount += archBtwBonus;
|
|
}
|
|
|
|
return amount;
|
|
}
|
|
|
|
private static int ApplyCrit(int amount, DamageRequest request, out bool wasCritical)
|
|
{
|
|
wasCritical = false;
|
|
if (!CanRollCritical(request, amount))
|
|
{
|
|
return amount;
|
|
}
|
|
|
|
int critChance = request.Language is Language.C or Language.CPlusPlus ? CombatTuning.CCriticalChancePercent : 25;
|
|
wasCritical = RandomRoll.RollRange(1, 100, new RollContext(request.Source)) <= critChance;
|
|
return wasCritical ? ScaleCritical(amount) : amount;
|
|
}
|
|
|
|
private static bool CanRollCritical(DamageRequest request, int amount) =>
|
|
request.CanCrit && request.Language != Language.TypeScript && amount > 0 && amount != int.MaxValue && !(request.Source?.ForceMaxRolls ?? false);
|
|
|
|
private static int ScaleCritical(int amount) =>
|
|
(int)System.Math.Min(int.MaxValue, ((long)amount * 3 + 1) / 2);
|
|
|
|
private static void ApplyArchRollingReleaseSave(DamageRequest request)
|
|
{
|
|
CombatantState target = request.Target;
|
|
if (target == null || target.CurrentUptime > 0 || target.ArchRollingReleaseSavesRemaining <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
target.ArchRollingReleaseSavesRemaining--;
|
|
target.ArchRollingReleaseAvailableThisWave = target.ArchRollingReleaseSavesRemaining > 0;
|
|
target.ArchBtwStacks = 0;
|
|
target.CurrentUptime = 1;
|
|
target.Shield += Mathf.Max(0, target.ArchRollingReleaseShieldOnSave);
|
|
target.Cycles += Mathf.Max(0, target.ArchRollingReleaseCyclesOnSave);
|
|
target.IsDefeated = false;
|
|
target.ArchRollingReleaseRecoveredThisHit = true;
|
|
}
|
|
|
|
private int ApplyResistancesAndWeaknesses(int amount, DamageRequest request)
|
|
{
|
|
// TODO: Enemy resistances/reductions are not authored yet. Mint V5 should re-resolve
|
|
// a once-per-combat reduced effect here once those reduction sources exist.
|
|
// TODO: Apply enemy language resistances and weaknesses here.
|
|
return ResistanceResolver == null ? amount : ResistanceResolver(request, amount);
|
|
}
|
|
|
|
private static int ApplyShieldAndUptime(int amount, DamageRequest request, out int absorbedAmount, out int shieldDamage, out int uptimeDamage)
|
|
{
|
|
CombatantState target = request.Target;
|
|
int remaining = amount;
|
|
shieldDamage = 0;
|
|
|
|
if (!request.TrueDamage && target.Shield > 0)
|
|
{
|
|
shieldDamage = Mathf.Min(target.Shield, remaining);
|
|
target.Shield -= shieldDamage;
|
|
remaining -= shieldDamage;
|
|
}
|
|
|
|
uptimeDamage = Mathf.Min(target.CurrentUptime, remaining);
|
|
target.CurrentUptime = Mathf.Max(0, target.CurrentUptime - uptimeDamage);
|
|
absorbedAmount = Mathf.Max(0, amount - shieldDamage - uptimeDamage);
|
|
return shieldDamage + uptimeDamage;
|
|
}
|
|
}
|
|
}
|