add C++, Typescript and more stuff
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using KernelPanic.Core;
|
||||
|
||||
namespace KernelPanic.Combat
|
||||
{
|
||||
public sealed class ExecutionAction
|
||||
{
|
||||
public string Stage { get; internal set; }
|
||||
public string Label { get; internal set; }
|
||||
public bool Complete { get; internal set; }
|
||||
public long UptimeDamage { get; internal set; }
|
||||
public long ShieldDamage { get; internal set; }
|
||||
public long ShieldGranted { get; internal set; }
|
||||
public long PlayerUptimeLost { get; internal set; }
|
||||
public long PlayerShieldBlocked { get; internal set; }
|
||||
public List<string> Details { get; } = new();
|
||||
}
|
||||
|
||||
/// <summary>A bounded journal of observed actions. Never predicts or executes combat.</summary>
|
||||
public sealed class CombatExecutionTrace : IDisposable
|
||||
{
|
||||
private readonly List<ExecutionAction> _actions = new();
|
||||
private readonly Stack<ExecutionAction> _active = new();
|
||||
private readonly CombatManager _combat;
|
||||
public IReadOnlyList<ExecutionAction> Actions => _actions;
|
||||
public string Stage { get; private set; } = "Native";
|
||||
public event Action Changed;
|
||||
|
||||
public CombatExecutionTrace(CombatManager combat)
|
||||
{
|
||||
_combat = combat;
|
||||
GameEvents.CardResolving += BeginCard;
|
||||
GameEvents.CardResolved += EndCard;
|
||||
GameEvents.BackgroundAction += Background;
|
||||
GameEvents.EnemyWouldAct += BeginEnemy;
|
||||
GameEvents.EnemyActed += EndEnemy;
|
||||
GameEvents.DamageDealt += Damage;
|
||||
GameEvents.CardContribution += Contribution;
|
||||
GameEvents.PhaseChanged += Phase;
|
||||
if (_combat != null) _combat.CombatLog += Detail;
|
||||
}
|
||||
|
||||
private void Begin(string stage, string label)
|
||||
{
|
||||
Stage = stage;
|
||||
var action = new ExecutionAction { Stage = stage, Label = label };
|
||||
_actions.Add(action); _active.Push(action);
|
||||
if (_actions.Count > 12) _actions.RemoveAt(0);
|
||||
Changed?.Invoke();
|
||||
}
|
||||
private void End()
|
||||
{
|
||||
if (_active.Count > 0) _active.Pop().Complete = true;
|
||||
Changed?.Invoke();
|
||||
}
|
||||
private void BeginCard(CardResolvedEvent value) => Begin(value.Track switch
|
||||
{ ResolutionTrack.InterpreterQueue => "FIFO", ResolutionTrack.LazyStack => "LIFO", _ => "Native" }, value.Card?.Definition?.DisplayName ?? "Card");
|
||||
private void EndCard(CardResolvedEvent value) => End();
|
||||
private void Background(BackgroundActionEvent value)
|
||||
{ if (value.Started) Begin(value.Stage, value.Label); else End(); }
|
||||
private void BeginEnemy(EnemyWouldActEvent value) => Begin("Enemies", value.Enemy.Name + " / " + value.Enemy.DisplayIntent.DisplayText);
|
||||
private void EndEnemy(EnemyActedEvent value) => End();
|
||||
private void Damage(DamageDealtEvent value)
|
||||
{
|
||||
bool standalone = _active.Count == 0;
|
||||
if (standalone) Begin(Stage, value.Cause ?? "Status or passive damage");
|
||||
var action = _active.Peek();
|
||||
if (_combat != null && ReferenceEquals(value.Target, _combat.PlayerState))
|
||||
{
|
||||
action.PlayerUptimeLost += Math.Max(0, value.UptimeDamage);
|
||||
action.PlayerShieldBlocked += Math.Max(0, value.ShieldDamage);
|
||||
}
|
||||
else
|
||||
{
|
||||
action.UptimeDamage += Math.Max(0, value.UptimeDamage);
|
||||
action.ShieldDamage += Math.Max(0, value.ShieldDamage);
|
||||
}
|
||||
if (standalone) End();
|
||||
Changed?.Invoke();
|
||||
}
|
||||
private void Contribution(CardContributionEvent value)
|
||||
{
|
||||
if (_active.Count == 0) return;
|
||||
_active.Peek().ShieldGranted += Math.Max(0, value.ShieldGranted);
|
||||
Changed?.Invoke();
|
||||
}
|
||||
private void Detail(string text)
|
||||
{
|
||||
if (_active.Count == 0 || string.IsNullOrWhiteSpace(text)) return;
|
||||
var details = _active.Peek().Details;
|
||||
if (details.Count == 3) details.RemoveAt(0);
|
||||
details.Add(text);
|
||||
Changed?.Invoke();
|
||||
}
|
||||
private void Phase(PhaseChangedEvent value)
|
||||
{
|
||||
if (value.NextPhase == TurnPhase.Boot) { _actions.Clear(); _active.Clear(); }
|
||||
Stage = value.NextPhase switch
|
||||
{
|
||||
TurnPhase.Interpret => "Go", TurnPhase.EnemyProcess => "Enemies",
|
||||
TurnPhase.GarbageCollection => "Cleanup", _ => "Native"
|
||||
};
|
||||
// Completed results stay readable during the next planning phase.
|
||||
Changed?.Invoke();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
GameEvents.CardResolving -= BeginCard; GameEvents.CardResolved -= EndCard;
|
||||
GameEvents.BackgroundAction -= Background;
|
||||
GameEvents.EnemyWouldAct -= BeginEnemy; GameEvents.EnemyActed -= EndEnemy;
|
||||
GameEvents.DamageDealt -= Damage; GameEvents.CardContribution -= Contribution;
|
||||
GameEvents.PhaseChanged -= Phase;
|
||||
if (_combat != null) _combat.CombatLog -= Detail;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user