using System; using System.Collections.Generic; using System.Linq; using KernelPanic.Core; namespace KernelPanic.Combat { /// Immutable execution snapshot, owned by an enqueue, never by a clonable card. public sealed class LazyStackEntry { public CardInstance Card { get; } public int MultiplierPercent { get; } public IReadOnlyList Targets { get; } public string MultiplierLabel => $"x{MultiplierPercent / 100}.{MultiplierPercent % 100 / 10}"; internal LazyStackEntry(CardInstance card, int multiplierPercent) { Card = card; MultiplierPercent = multiplierPercent; Targets = Array.AsReadOnly(card.TargetSnapshot.ToArray()); } } /// Top-first immutable snapshots. CombatManager alone executes/disposes cards. public sealed class LazyStack : IResolutionTrack { private readonly List _entries = new(); private readonly List _cards = new(); public int Count => _entries.Count; public IReadOnlyList Cards => _cards.AsReadOnly(); public IReadOnlyList Entries => _entries.AsReadOnly(); public void Enqueue(CardInstance card) { if (card == null || _cards.Contains(card)) return; int multiplier = 100; if (card.Definition != null && card.Definition.Language == Language.Haskell) multiplier = 100 + 50 * Math.Min(3, _entries.Count(e => e.Card.Definition != null && e.Card.Definition.Language == Language.Haskell)); _entries.Insert(0, new LazyStackEntry(card, multiplier)); _cards.Insert(0, card); } public void Resolve(CombatContext context) => ForceEvaluate(context); public void ForceEvaluate(CombatContext context) => context?.CombatManager?.EvaluateLazyStack(); public bool TryPopEntry(out LazyStackEntry entry) { entry = null; if (_entries.Count == 0) return false; entry = _entries[0]; _entries.RemoveAt(0); _cards.RemoveAt(0); return true; } public bool TryPop(out CardInstance card) { bool popped = TryPopEntry(out var entry); card = entry?.Card; if (popped) GameEvents.RaiseCardResolving(new CardResolvedEvent(card, ResolutionTrack.LazyStack)); return popped; } public void Clear() { foreach (var card in _cards) card.SetTargetSnapshot(Array.Empty()); _entries.Clear(); _cards.Clear(); } } }