Rebalance most languages and revamp main game loop
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using KernelPanic.Combat;
|
||||
using KernelPanic.Core;
|
||||
|
||||
namespace KernelPanic.Meta
|
||||
{
|
||||
/// <summary>Records real gameplay immediately, independently of run rewards and checkpoints.</summary>
|
||||
public sealed class DailyQuestTracker : IDisposable
|
||||
{
|
||||
private readonly SaveService _save;
|
||||
private readonly Func<bool> _eligible;
|
||||
private readonly Func<CombatantState> _player;
|
||||
private readonly Func<DateTime> _utcNow;
|
||||
private readonly List<(DailyQuestKind kind, int amount, DateTime time)> _pending = new();
|
||||
public string SaveError { get; private set; }
|
||||
|
||||
public DailyQuestTracker(SaveService save, Func<bool> eligible, Func<CombatantState> player,
|
||||
Func<DateTime> utcNow = null)
|
||||
{
|
||||
_save = save;
|
||||
_eligible = eligible;
|
||||
_player = player;
|
||||
_utcNow = utcNow ?? (() => DateTime.UtcNow);
|
||||
GameEvents.WaveCleared += OnWaveCleared;
|
||||
GameEvents.CardPlayed += OnCardPlayed;
|
||||
GameEvents.DamageDealt += OnDamageDealt;
|
||||
GameEvents.PhaseChanged += OnPhaseChanged;
|
||||
}
|
||||
|
||||
private void OnWaveCleared(WaveClearedEvent evt)
|
||||
{
|
||||
Record(DailyQuestKind.Waves, 1);
|
||||
if (evt.WaveNumber == 5) Record(DailyQuestKind.WaveFive, 1);
|
||||
}
|
||||
|
||||
private void OnCardPlayed(CardPlayedEvent evt)
|
||||
{
|
||||
Record(DailyQuestKind.Cards, 1);
|
||||
if (evt.Track == ResolutionTrack.Native) Record(DailyQuestKind.NativeCards, 1);
|
||||
}
|
||||
|
||||
private void OnPhaseChanged(PhaseChangedEvent evt)
|
||||
{
|
||||
if (evt.PreviousPhase == TurnPhase.EnemyProcess && evt.NextPhase == TurnPhase.GarbageCollection
|
||||
&& _player()?.CurrentUptime > 0) Record(DailyQuestKind.SurviveRounds, 1);
|
||||
}
|
||||
private void OnDamageDealt(DamageDealtEvent evt)
|
||||
{
|
||||
var player = _player();
|
||||
if (player != null && ReferenceEquals(evt.Source, player) && !ReferenceEquals(evt.Target, player))
|
||||
{
|
||||
Record(DailyQuestKind.Damage, evt.UptimeDamage);
|
||||
Record(DailyQuestKind.ShieldDamage, evt.ShieldDamage);
|
||||
}
|
||||
else if (player != null && ReferenceEquals(evt.Target, player) && !ReferenceEquals(evt.Source, player))
|
||||
Record(DailyQuestKind.BlockDamage, evt.ShieldDamage);
|
||||
}
|
||||
|
||||
private void Record(DailyQuestKind kind, int amount)
|
||||
{
|
||||
if (!_eligible() || amount <= 0) return;
|
||||
_pending.Add((kind, amount, _utcNow()));
|
||||
Flush();
|
||||
}
|
||||
|
||||
public bool Flush()
|
||||
{
|
||||
if (_pending.Count == 0) return true;
|
||||
try
|
||||
{
|
||||
// Reload before every transaction so checkpoint and run settlement writes are preserved.
|
||||
var data = _save.Load();
|
||||
bool changed = false;
|
||||
foreach (var entry in _pending)
|
||||
changed |= DailyQuests.Record(data.dailyQuests, entry.kind, entry.amount, entry.time);
|
||||
changed |= DailyQuests.Refresh(data.dailyQuests, _utcNow());
|
||||
if (changed) _save.Save(data);
|
||||
_pending.Clear();
|
||||
SaveError = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
|
||||
{
|
||||
SaveError = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GameEvents.WaveCleared -= OnWaveCleared;
|
||||
GameEvents.CardPlayed -= OnCardPlayed;
|
||||
GameEvents.DamageDealt -= OnDamageDealt;
|
||||
GameEvents.PhaseChanged -= OnPhaseChanged;
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace KernelPanic.Meta
|
||||
{
|
||||
// Keep values stable: selections and claim bits are persisted in player saves.
|
||||
public enum DailyQuestKind { Waves, Cards, Damage, BlockDamage, NativeCards, ShieldDamage, SurviveRounds, WaveFive }
|
||||
|
||||
[Serializable]
|
||||
public sealed class DailyQuestState
|
||||
{
|
||||
public long refreshAtUtcTicks;
|
||||
public int waves;
|
||||
public int cards;
|
||||
public int damage;
|
||||
public int claimedMask;
|
||||
public DailyQuestKind[] selectedQuests;
|
||||
public int selectionSeed;
|
||||
public int blockedDamage;
|
||||
public int nativeCards;
|
||||
public int shieldDamage;
|
||||
public int survivedRounds;
|
||||
public int waveFive;
|
||||
}
|
||||
|
||||
/// <summary>One persistent, rolling 24-hour window. All time comes from the caller for deterministic tests.</summary>
|
||||
public static class DailyQuests
|
||||
{
|
||||
public const int Count = 3;
|
||||
public const long PeriodTicks = TimeSpan.TicksPerDay;
|
||||
public static readonly IReadOnlyList<DailyQuestKind> Pool = Array.AsReadOnly((DailyQuestKind[])Enum.GetValues(typeof(DailyQuestKind)));
|
||||
private static readonly DailyQuestKind[] LegacySelection = { DailyQuestKind.Waves, DailyQuestKind.Cards, DailyQuestKind.Damage };
|
||||
|
||||
public static IReadOnlyList<DailyQuestKind> Active(DailyQuestState state) =>
|
||||
ValidSelection(state.selectedQuests) ? state.selectedQuests : LegacySelection;
|
||||
|
||||
public static bool IsActive(DailyQuestState state, DailyQuestKind kind)
|
||||
{
|
||||
foreach (var selected in Active(state)) if (selected == kind) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool ValidSelection(DailyQuestKind[] selection)
|
||||
{
|
||||
if (selection == null || selection.Length != Count) return false;
|
||||
for (int i = 0; i < selection.Length; i++)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(DailyQuestKind), selection[i])) return false;
|
||||
for (int j = 0; j < i; j++) if (selection[i] == selection[j]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void PickQuests(DailyQuestState state)
|
||||
{
|
||||
if (state.selectionSeed == 0)
|
||||
{
|
||||
state.selectionSeed = Guid.NewGuid().GetHashCode();
|
||||
if (state.selectionSeed == 0) state.selectionSeed = 1;
|
||||
}
|
||||
// Local randomness never consumes the combat, shop, or Unity RNG streams.
|
||||
var random = new Random(unchecked(state.selectionSeed ^ (int)state.refreshAtUtcTicks ^ (int)(state.refreshAtUtcTicks >> 32)));
|
||||
var candidates = (DailyQuestKind[])Enum.GetValues(typeof(DailyQuestKind));
|
||||
for (int i = candidates.Length - 1; i > 0; i--)
|
||||
{
|
||||
int swap = random.Next(i + 1);
|
||||
(candidates[i], candidates[swap]) = (candidates[swap], candidates[i]);
|
||||
}
|
||||
bool sameSet = ValidSelection(state.selectedQuests);
|
||||
for (int i = 0; i < Count; i++) sameSet &= IsActive(state, candidates[i]);
|
||||
if (sameSet) (candidates[Count - 1], candidates[Count]) = (candidates[Count], candidates[Count - 1]);
|
||||
state.selectedQuests = new DailyQuestKind[Count];
|
||||
Array.Copy(candidates, state.selectedQuests, Count);
|
||||
}
|
||||
|
||||
public static int Target(DailyQuestKind kind) => kind switch
|
||||
{
|
||||
DailyQuestKind.Waves => 3,
|
||||
DailyQuestKind.Cards => 25,
|
||||
DailyQuestKind.Damage => 200,
|
||||
DailyQuestKind.BlockDamage => 40,
|
||||
DailyQuestKind.NativeCards => 15,
|
||||
DailyQuestKind.ShieldDamage => 60,
|
||||
DailyQuestKind.SurviveRounds => 8,
|
||||
DailyQuestKind.WaveFive => 1,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
||||
};
|
||||
|
||||
// Routine actions < focused combat objectives < sustained/encounter-dependent play < wave-5 milestone.
|
||||
// Display and settlement share this table; rewards are not persisted separately from claim flags.
|
||||
public static (int Bandwidth, int Entropy) Reward(DailyQuestKind kind) => kind switch
|
||||
{
|
||||
DailyQuestKind.Cards => (75, 25),
|
||||
DailyQuestKind.NativeCards => (75, 25),
|
||||
DailyQuestKind.Damage => (100, 50),
|
||||
DailyQuestKind.BlockDamage => (100, 50),
|
||||
DailyQuestKind.Waves => (150, 75),
|
||||
DailyQuestKind.ShieldDamage => (150, 75),
|
||||
DailyQuestKind.SurviveRounds => (150, 75),
|
||||
DailyQuestKind.WaveFive => (250, 125),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
||||
};
|
||||
|
||||
public static string Description(DailyQuestKind kind) => kind switch
|
||||
{
|
||||
DailyQuestKind.Waves => "Clear 3 waves",
|
||||
DailyQuestKind.Cards => "Play 25 cards",
|
||||
DailyQuestKind.Damage => "Deal 200 Uptime damage",
|
||||
DailyQuestKind.BlockDamage => "Block 40 damage with Shield",
|
||||
DailyQuestKind.NativeCards => "Play 15 Native cards",
|
||||
DailyQuestKind.ShieldDamage => "Deal 60 damage to enemy Shields",
|
||||
DailyQuestKind.SurviveRounds => "Survive 8 enemy rounds",
|
||||
DailyQuestKind.WaveFive => "Clear wave 5",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
||||
};
|
||||
|
||||
public static int Progress(DailyQuestState state, DailyQuestKind kind) => Math.Clamp(kind switch
|
||||
{
|
||||
DailyQuestKind.Waves => state.waves,
|
||||
DailyQuestKind.Cards => state.cards,
|
||||
DailyQuestKind.Damage => state.damage,
|
||||
DailyQuestKind.BlockDamage => state.blockedDamage,
|
||||
DailyQuestKind.NativeCards => state.nativeCards,
|
||||
DailyQuestKind.ShieldDamage => state.shieldDamage,
|
||||
DailyQuestKind.SurviveRounds => state.survivedRounds,
|
||||
DailyQuestKind.WaveFive => state.waveFive,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
||||
}, 0, Target(kind));
|
||||
|
||||
public static bool Claimed(DailyQuestState state, DailyQuestKind kind) =>
|
||||
(state.claimedMask & (1 << (int)kind)) != 0;
|
||||
|
||||
public static bool Refresh(DailyQuestState state, DateTime utcNow)
|
||||
{
|
||||
long now = utcNow.ToUniversalTime().Ticks;
|
||||
if (state.refreshAtUtcTicks > now && state.refreshAtUtcTicks <= DateTime.MaxValue.Ticks)
|
||||
{
|
||||
if (ValidSelection(state.selectedQuests)) return false;
|
||||
// Schema 7 had three fixed quests. Preserve that day's progress and rewards.
|
||||
state.selectedQuests = (DailyQuestKind[])LegacySelection.Clone();
|
||||
return true;
|
||||
}
|
||||
// Advance by whole periods, retaining the original reset time even after days away.
|
||||
if (state.refreshAtUtcTicks > 0 && !ValidSelection(state.selectedQuests))
|
||||
state.selectedQuests = (DailyQuestKind[])LegacySelection.Clone();
|
||||
state.refreshAtUtcTicks = state.refreshAtUtcTicks > 0 && state.refreshAtUtcTicks <= now
|
||||
? state.refreshAtUtcTicks + ((now - state.refreshAtUtcTicks) / PeriodTicks + 1) * PeriodTicks
|
||||
: now + PeriodTicks;
|
||||
state.waves = state.cards = state.damage = state.claimedMask = 0;
|
||||
state.blockedDamage = state.nativeCards = state.shieldDamage = state.survivedRounds = state.waveFive = 0;
|
||||
PickQuests(state);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Record(DailyQuestState state, DailyQuestKind kind, int amount, DateTime utcNow)
|
||||
{
|
||||
if (amount <= 0 || !Enum.IsDefined(typeof(DailyQuestKind), kind)) return false;
|
||||
bool changed = Refresh(state, utcNow);
|
||||
// Delayed retries from an expired period must not count toward today's quests.
|
||||
if (utcNow.ToUniversalTime().Ticks < state.refreshAtUtcTicks - PeriodTicks || !IsActive(state, kind)) return changed;
|
||||
int previous = Progress(state, kind);
|
||||
int next = (int)Math.Min(Target(kind), (long)previous + amount);
|
||||
switch (kind)
|
||||
{
|
||||
case DailyQuestKind.Waves: state.waves = next; break;
|
||||
case DailyQuestKind.Cards: state.cards = next; break;
|
||||
case DailyQuestKind.Damage: state.damage = next; break;
|
||||
case DailyQuestKind.BlockDamage: state.blockedDamage = next; break;
|
||||
case DailyQuestKind.NativeCards: state.nativeCards = next; break;
|
||||
case DailyQuestKind.ShieldDamage: state.shieldDamage = next; break;
|
||||
case DailyQuestKind.SurviveRounds: state.survivedRounds = next; break;
|
||||
case DailyQuestKind.WaveFive: state.waveFive = next; break;
|
||||
}
|
||||
return changed || next != previous;
|
||||
}
|
||||
|
||||
public static bool Claim(SaveData data, DailyQuestKind kind, long expectedRefresh, DateTime utcNow)
|
||||
{
|
||||
data.dailyQuests ??= new DailyQuestState();
|
||||
Refresh(data.dailyQuests, utcNow);
|
||||
if (!Enum.IsDefined(typeof(DailyQuestKind), kind) || !IsActive(data.dailyQuests, kind) || data.dailyQuests.refreshAtUtcTicks != expectedRefresh
|
||||
|| Claimed(data.dailyQuests, kind) || Progress(data.dailyQuests, kind) < Target(kind)) return false;
|
||||
var reward = Reward(kind);
|
||||
if (data.bandwidthBalance > int.MaxValue - reward.Bandwidth
|
||||
|| data.entropyBalance > int.MaxValue - reward.Entropy) return false;
|
||||
data.dailyQuests.claimedMask |= 1 << (int)kind;
|
||||
data.bandwidthBalance += reward.Bandwidth;
|
||||
data.entropyBalance += reward.Entropy;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string Countdown(DailyQuestState state, DateTime utcNow)
|
||||
{
|
||||
var remaining = TimeSpan.FromTicks(Math.Max(0, state.refreshAtUtcTicks - utcNow.ToUniversalTime().Ticks));
|
||||
return $"refresh in {(int)remaining.TotalHours:00}:{remaining.Minutes:00}:{remaining.Seconds:00}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ namespace KernelPanic.Meta
|
||||
{
|
||||
1 => "start of turn: apt update chooses the cheaper of top 2 draw cards",
|
||||
2 => "apt update looks at top 3 instead of top 2",
|
||||
3 => "ask ubuntu draws 3 instead of 2",
|
||||
3 => "ask ubuntu inspects 5 cards instead of 3; take one",
|
||||
4 => "apt update card costs 1 less this turn",
|
||||
5 => "once per combat, empty hand refills up to RAM",
|
||||
_ => "--"
|
||||
|
||||
Reference in New Issue
Block a user