160 lines
8.7 KiB
C#
160 lines
8.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Linq;
|
|
using KernelPanic.Core;
|
|
|
|
namespace KernelPanic.Combat
|
|
{
|
|
public sealed partial class CombatManager
|
|
{
|
|
public NixGovernorState Nix { get; } = new();
|
|
public GoroutineScheduler Go { get; } = new();
|
|
public Goroutine SelectedGoroutine { get; private set; }
|
|
public CardInstance PendingGoroutineCard => _pendingGoroutineCard;
|
|
private CardInstance _pendingGoroutineCard;
|
|
private bool _switchingGovernor;
|
|
|
|
private void ApplyGovernorModifiers()
|
|
{
|
|
if (_playerState == null) return;
|
|
_playerState.GovernorDamagePercent = Nix.DamagePercent;
|
|
_playerState.GovernorShieldPercent = Nix.ShieldPercent;
|
|
}
|
|
// V5 uses the selected living enemy, otherwise the first living enemy in process order.
|
|
private CombatantState GovernorTarget() => _selectedEnemyIndex >= 0 && _selectedEnemyIndex < _enemies.Count
|
|
&& IsLiveGoTarget(_enemies[_selectedEnemyIndex].State) ? _enemies[_selectedEnemyIndex].State : FirstLivingGoTarget();
|
|
public NixGovernorSwitchBlockReason GetGovernorSwitchBlockReason(NixGovernor governor)
|
|
{
|
|
if (_switchingGovernor) return NixGovernorSwitchBlockReason.SwitchingInProgress;
|
|
if (TutorialGuide?.Active == true) return NixGovernorSwitchBlockReason.TutorialActive;
|
|
if (currentPhase != TurnPhase.Execute) return NixGovernorSwitchBlockReason.OutsideExecutePhase;
|
|
if (!CanSurrenderRun) return NixGovernorSwitchBlockReason.CombatUnavailable;
|
|
var reason = Nix.GetSwitchBlockReason(governor, RoundNumber, _playerState.Cycles);
|
|
if (reason != NixGovernorSwitchBlockReason.None) return reason;
|
|
return Nix.Version >= 5 && governor == NixGovernor.Performance && GovernorTarget() == null
|
|
? NixGovernorSwitchBlockReason.NoLivingTarget : NixGovernorSwitchBlockReason.None;
|
|
}
|
|
public bool CanSwitchGovernor(NixGovernor governor) =>
|
|
GetGovernorSwitchBlockReason(governor) == NixGovernorSwitchBlockReason.None;
|
|
public bool TrySwitchGovernor(NixGovernor governor)
|
|
{
|
|
var reason = GetGovernorSwitchBlockReason(governor);
|
|
if (reason != NixGovernorSwitchBlockReason.None)
|
|
{
|
|
if (reason != NixGovernorSwitchBlockReason.SwitchingInProgress)
|
|
Log($"[nixos] {reason.GetMessage()}");
|
|
return false;
|
|
}
|
|
var target = GovernorTarget();
|
|
var previous = Nix.Governor;
|
|
_switchingGovernor = true;
|
|
try
|
|
{
|
|
if (!Nix.TrySwitch(governor, RoundNumber, _playerState.Cycles, out int cost)) return false;
|
|
_playerState.Cycles -= cost;
|
|
ApplyGovernorModifiers();
|
|
Log($"[nixos] governor switched: {previous.ToString().ToLowerInvariant()} -> {governor.ToString().ToLowerInvariant()}");
|
|
if (Nix.Version >= 5)
|
|
{
|
|
switch (governor)
|
|
{
|
|
case NixGovernor.Performance:
|
|
var hit = _damagePipeline.DealDamage(new DamageRequest(_playerState, target, 5, Language.Go, false, false, false)
|
|
.WithCause("NixOS specialisation"));
|
|
if (hit.TargetDefeated) QueueDefeatedCombatantForRemoval(target);
|
|
break;
|
|
case NixGovernor.Powersave: _playerState.Shield = CardScaling.Add(_playerState.Shield, 6); break;
|
|
case NixGovernor.Schedutil: DrawCards(1, "nixos specialisation"); break;
|
|
}
|
|
}
|
|
CheckWinOrLoss(); StateChanged?.Invoke(); return true;
|
|
}
|
|
finally { _switchingGovernor = false; StateChanged?.Invoke(); }
|
|
}
|
|
|
|
public string GoPlayBlockReason(CardInstance card)
|
|
{
|
|
var operation = GoEffect.OperationFor(card);
|
|
if (operation == GoOperation.Deploy && (!Go.HasCapacity || Go.Active.Any(g => g.SourceCard == card))) return "goroutine limit: 3 active";
|
|
if (operation == GoOperation.Cancel && Go.Count == 0) return "no active goroutine to cancel";
|
|
if (operation is GoOperation.Extend or GoOperation.ExtendAll && !Go.Active.Any(Go.CanExtend)) return "no goroutine can be extended";
|
|
return null;
|
|
}
|
|
private bool NeedsGoSelection(CardInstance card) => GoEffect.OperationFor(card) switch
|
|
{
|
|
GoOperation.Cancel => Go.Count > 0,
|
|
GoOperation.Extend or GoOperation.Rollback => Go.Active.Any(Go.CanExtend),
|
|
_ => false
|
|
};
|
|
public bool IsEligibleGoSelection(Goroutine work, CardInstance card) => work?.Active == true && Go.Active.Contains(work)
|
|
&& (GoEffect.OperationFor(card) == GoOperation.Cancel || Go.CanExtend(work));
|
|
private bool ValidateGoPlay(CardInstance card)
|
|
{
|
|
string reason = GoPlayBlockReason(card);
|
|
if (reason != null) { Log(reason); return false; }
|
|
if (NeedsGoSelection(card) && !IsEligibleGoSelection(SelectedGoroutine, card))
|
|
{
|
|
_pendingGoroutineCard = card; _pendingTargetCard = null;
|
|
Log("select a goroutine in the scheduler"); StateChanged?.Invoke(); return false;
|
|
}
|
|
return true;
|
|
}
|
|
public void SelectGoroutine(long id)
|
|
{
|
|
if (!CanSurrenderRun) return;
|
|
var work = Go.Active.FirstOrDefault(g => g.Id == id);
|
|
if (work == null || _pendingGoroutineCard != null && !IsEligibleGoSelection(work, _pendingGoroutineCard)) return;
|
|
SelectedGoroutine = work;
|
|
var pending = _pendingGoroutineCard; _pendingGoroutineCard = null;
|
|
if (pending != null && _handController.Cards.Contains(pending)) PlayCard(pending);
|
|
StateChanged?.Invoke();
|
|
}
|
|
public void CancelGoroutineSelection() { _pendingGoroutineCard = null; SelectedGoroutine = null; StateChanged?.Invoke(); }
|
|
internal void DeployGoroutine(CombatContext context, int damage, int ticks)
|
|
{
|
|
var work = Go.Deploy(context.Card, damage, ticks, RoundNumber, context.Targets.FirstOrDefault());
|
|
if (work != null) Log($"[go] PID {work.Id:000} deployed / {work.RemainingTicks} ticks / starts next round");
|
|
StateChanged?.Invoke();
|
|
}
|
|
private void OnGoroutineTerminated(Goroutine work)
|
|
{
|
|
work.SourceCard.ClearTargetSnapshot(); _deckController.Discard(work.SourceCard);
|
|
if (SelectedGoroutine == work) SelectedGoroutine = null;
|
|
Log($"[go] PID {work.Id:000} terminated");
|
|
}
|
|
private bool IsLiveGoTarget(CombatantState target) => target != null && target.CurrentUptime > 0 && !target.IsDefeated
|
|
&& _enemies.Any(enemy => enemy.State == target && !enemy.PendingRevive);
|
|
private CombatantState FirstLivingGoTarget() => _enemies.Select(enemy => enemy.State).FirstOrDefault(IsLiveGoTarget);
|
|
public int GoroutineDamageAmount(Goroutine work) => CardScaling.Add(work.BaseDamage,
|
|
CardScaling.Add(_playerState?.FlatEffectBonus ?? 0, _playerState?.FlatCardDamageBonus ?? 0));
|
|
internal void ResolveGoroutineTick(Goroutine work)
|
|
{
|
|
if (!IsLiveGoTarget(work.Target)) work.Target = FirstLivingGoTarget();
|
|
StateChanged?.Invoke();
|
|
if (work.Target == null) { Log($"[go] PID {work.Id:000} skipped / no living target"); return; }
|
|
var context = BuildContext(work.SourceCard, new[] { work.Target });
|
|
CardDamageExecutor.ApplyHit(context, DamageRequest.FromCard(context, work.Target,
|
|
GoroutineDamageAmount(work), Language.Go, false, true));
|
|
Log($"[go] PID {work.Id:000} tick / {Math.Max(0, work.RemainingTicks - 1)} remaining");
|
|
RemoveDefeatedEnemies();
|
|
}
|
|
private IEnumerator TickGoroutinesSequenced()
|
|
{
|
|
foreach (var work in Go.Active.ToArray())
|
|
{
|
|
while (VisualResolutionPending && !RunFinished && !_awaitingWaveContinue) yield return null;
|
|
if (CheckWinOrLoss()) yield break;
|
|
if (work.DeploymentRound < RoundNumber && work.LastTickRound < RoundNumber && work.Active)
|
|
{
|
|
work.Resolving = true; StateChanged?.Invoke();
|
|
yield return null;
|
|
}
|
|
if (!Go.Tick(work, RoundNumber, ResolveGoroutineTick)) continue;
|
|
StateChanged?.Invoke();
|
|
if (CheckWinOrLoss()) yield break;
|
|
yield return Wait(CombatTuning.QueueCardResolveDelaySeconds);
|
|
}
|
|
}
|
|
}
|
|
}
|