using System; using System.Collections.Generic; using System.Linq; using KernelPanic.Core; namespace KernelPanic.Combat { /// /// Shared card-hit lifecycle. Amount producers apply card magnitude and source flat bonuses /// once; this executor passes that amount through the damage pipeline without scaling it again. /// internal static class CardDamageExecutor { internal static int ScaleAmount(int baseAmount, CombatContext context) => UpgradeMath.ScaleAmount(CardScaling.Add(UpgradeMath.ApplySourceFlatBonus(baseAmount, context), context?.Source?.FlatCardDamageBonus ?? 0), context.Card); internal static void Execute(CombatContext context, Func rollAmount, Language language, bool trueDamage = false, bool canCrit = true, bool allEnemies = false, bool overkillToShield = false, Action reportHit = null) { IReadOnlyList targets = ResolveTargets(context, allEnemies); bool dealtDamage = false; for (int i = 0; i < targets.Count; i++) { CombatantState target = targets[i]; if (target == null || target.IsDefeated) continue; int amount = rollAmount(); bool rustOverflow = CardDamageLanguageRules.TryRollRustOverflow(context, language); if (rustOverflow) amount = int.MaxValue; int capacity = CardDamageLanguageRules.GetDamageCapacity(target, trueDamage); DamageResult result = ApplyHit(context, DamageRequest.FromCard(context, target, amount, language, trueDamage, canCrit && !rustOverflow), overkillToShield); dealtDamage |= result.IncomingAmount > 0; dealtDamage |= CardDamageLanguageRules.TrySpillOverkill(context, target, result, capacity, language, trueDamage); reportHit?.Invoke(amount); } SegfaultRecoil.TryApply(context, language, dealtDamage); } internal static DamageResult ApplyHit(CombatContext context, DamageRequest request, bool overkillToShield = false) { int capacity = request.Target == null ? 0 : CardDamageLanguageRules.GetDamageCapacity(request.Target, request.TrueDamage); DamageResult result = context.DamagePipeline.DealDamage(request); // Preserve ordering: shield conversion precedes enemy death payloads, then spill follows removal queuing. if (request.IsCardDamage) CardDamageLanguageRules.ApplyOverkillShield(context, result, capacity, request.Language, overkillToShield); if (result.TargetDefeated) context.CombatManager.QueueDefeatedCombatantForRemoval(request.Target); return result; } private static IReadOnlyList ResolveTargets(CombatContext context, bool allEnemies) { IReadOnlyList targets = allEnemies ? context.Enemies.Where(enemy => enemy?.State != null && !enemy.State.IsDefeated) .Select(enemy => enemy.State).ToList() : context.Targets; if (!StatusEffectController.Has(context.Source, StatusType.RaceCondition) || targets.Count < 2) return targets; List shuffled = targets.ToList(); for (int i = shuffled.Count - 1; i > 0; i--) { int swapIndex = RandomRoll.RollRange(0, i, new RollContext(context.Source)); (shuffled[i], shuffled[swapIndex]) = (shuffled[swapIndex], shuffled[i]); } return shuffled; } } }