using System;
namespace KernelPanic.Combat
{
/// Wave-scoped Gentoo progression. Round IDs make completion idempotent.
public sealed class CompilationState
{
// Explicit checkpoint values: no gameplay hooks or random draws during restoration.
internal CompilationStateValues CaptureValues() => new()
{
_version = _version,
_lastCompletedRound = _lastCompletedRound,
LastAdvancedRound = LastAdvancedRound,
Level = Level,
};
internal void RestoreValues(CompilationStateValues value)
{
_version = value._version;
_lastCompletedRound = value._lastCompletedRound;
LastAdvancedRound = value.LastAdvancedRound;
Level = value.Level;
}
private int _version;
private int _lastCompletedRound;
public int LastAdvancedRound { get; private set; }
public bool Active => _version > 0;
public int Level { get; private set; }
public int Maximum => !Active ? 0 : _version >= 5 ? 6 : 4;
public int DamagePercent => !Active ? 100 : Level == 0 ? (_version >= 2 ? 95 : 85) : 100 + (Level - 1) * 10;
public int ShieldPercent => _version >= 4 ? 100 + Level * 5 : 100;
public void BeginWave(int version)
{
_version = Math.Max(0, Math.Min(5, version));
Level = _version >= 3 ? 1 : 0;
_lastCompletedRound = 0;
LastAdvancedRound = 0;
}
public bool CompleteRound(int round)
{
if (!Active || round <= _lastCompletedRound) return false;
_lastCompletedRound = round;
if (Level >= Maximum) return false;
Level++;
LastAdvancedRound = round;
return true;
}
// Silent equipment advancement: no completion or compilation-increase hooks.
public void AdvanceFromEquipment(int amount, int round)
{
if (!Active || amount <= 0 || Level >= Maximum) return;
Level = (int)Math.Min(Maximum, (long)Level + amount);
LastAdvancedRound = round;
}
}
///
/// New card multipliers use exact decimal arithmetic and one final nearest-even rounding,
/// matching Mathf.RoundToInt's tie policy. Saturate before conversion; never wrap negative.
/// Crit/resistance retain their existing, later rounding stages.
///
public static class CardScaling
{
public static int Scale(int amount, params int[] percentages)
{
decimal value = Math.Max(0, amount);
try
{
foreach (int percent in percentages) value = value * Math.Max(0, percent) / 100m;
}
catch (OverflowException) { return int.MaxValue; }
return (int)Math.Min(int.MaxValue, decimal.Round(value, 0, MidpointRounding.ToEven));
}
public static int Add(int amount, int bonus) => (int)Math.Min(int.MaxValue, (long)Math.Max(0, amount) + Math.Max(0, bonus));
}
}