Implement more QoL features and rebalances
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using KernelPanic.Combat;
|
||||
using KernelPanic.Core;
|
||||
using KernelPanic.Data;
|
||||
using KernelPanic.Run;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
public class RunReproducibilityTests
|
||||
{
|
||||
private GameObject _owner;
|
||||
private RunManager _run;
|
||||
private CardDatabase _cards;
|
||||
private LanguageDeckDatabase _languages;
|
||||
private DistroDefinition _distro;
|
||||
private object _combatRandom;
|
||||
private UnityEngine.Random.State _unityRandom;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_combatRandom = typeof(RandomRoll).GetField("_random", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
|
||||
_unityRandom = UnityEngine.Random.state;
|
||||
_owner = new GameObject("seeded repository regression");
|
||||
_run = _owner.AddComponent<RunManager>();
|
||||
_cards = AssetDatabase.LoadAssetAtPath<CardDatabase>("Assets/Data/Cards/CardDatabase.asset");
|
||||
_languages = AssetDatabase.LoadAssetAtPath<LanguageDeckDatabase>("Assets/Data/Cards/Languages/LanguageDeckDatabase.asset");
|
||||
_distro = AssetDatabase.LoadAssetAtPath<DistroDefinition>("Assets/Data/Distros/Gentoo.asset");
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
UnityEngine.Object.DestroyImmediate(_owner);
|
||||
typeof(RandomRoll).GetField("_random", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, _combatRandom);
|
||||
UnityEngine.Random.state = _unityRandom;
|
||||
}
|
||||
|
||||
[TestCase(0)]
|
||||
[TestCase(72531)]
|
||||
[TestCase(int.MinValue)]
|
||||
[TestCase(int.MaxValue)]
|
||||
public void SameSeedReplaysPurchasesRerollsAndLaterRepositoryVisits(int seed)
|
||||
{
|
||||
string expected = CaptureShopSequence(seed, false);
|
||||
string actual = CaptureShopSequence(seed, true);
|
||||
Assert.That(actual, Is.EqualTo(expected), "Combat rolls and unrelated Unity random calls must not change repository offers.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DifferentSeedsProduceDifferentRepositorySequences()
|
||||
{
|
||||
Assert.That(CaptureShopSequence(1001, false), Is.Not.EqualTo(CaptureShopSequence(1002, false)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShopBrowsingDoesNotAdvanceCombatOrPresentationRandomness()
|
||||
{
|
||||
RandomRoll.Seed(47);
|
||||
int[] expected = Enumerable.Range(0, 12).Select(_ => RandomRoll.RollRange(1, 100, RollContext.None)).ToArray();
|
||||
RandomRoll.Seed(47);
|
||||
var unityState = UnityEngine.Random.state;
|
||||
CaptureShopSequence(47, false);
|
||||
Assert.That(UnityEngine.Random.state, Is.EqualTo(unityState));
|
||||
Assert.That(Enumerable.Range(0, 12).Select(_ => RandomRoll.RollRange(1, 100, RollContext.None)), Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeckAndEnemyIntentsReplayDespiteExtraRepositoryBrowsing()
|
||||
{
|
||||
string Capture(bool browse)
|
||||
{
|
||||
_run.StartRun(Config(912));
|
||||
RandomRoll.Seed(912);
|
||||
var output = new List<string>();
|
||||
var enemy = new EnemyInstance(EnemyArchetypeCatalog.Get("zombie_process"), 20);
|
||||
for (int wave = 0; wave < 4; wave++)
|
||||
{
|
||||
if (browse) _run.GenerateRepositoryOffers(_cards, _languages);
|
||||
var deck = new DeckController();
|
||||
deck.Initialize(_run.RunDeck);
|
||||
output.Add(string.Join(",", deck.Draw(20).Select(card => card.Definition.Id)));
|
||||
for (int turn = 0; turn < 8; turn++)
|
||||
{
|
||||
enemy.PickNextIntent();
|
||||
output.Add(enemy.CurrentIntent.DisplayText);
|
||||
}
|
||||
}
|
||||
return string.Join("|", output);
|
||||
}
|
||||
Assert.That(Capture(true), Is.EqualTo(Capture(false)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExplicitSeedSurvivesSceneHandoffIncludingZeroAndNegativeValues()
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic;
|
||||
FieldInfo pending = typeof(RunContext).GetField("_pendingRun", flags);
|
||||
FieldInfo last = typeof(RunContext).GetField("<LastRunSeed>k__BackingField", flags);
|
||||
object previousPending = pending.GetValue(null);
|
||||
object previousLast = last.GetValue(null);
|
||||
try
|
||||
{
|
||||
foreach (int seed in new[] { 0, int.MinValue, int.MaxValue, -17 })
|
||||
{
|
||||
RunContext.Set(_distro, Config(seed).StartingDeck, Array.Empty<PackageInstance>(), Language.C, Language.Haskell, 3, seed);
|
||||
Assert.That(RunContext.TryCreateRunConfig(_languages, out RunConfig config), Is.True);
|
||||
Assert.That(config.RunSeed, Is.EqualTo(seed));
|
||||
Assert.That(config.DistroVersion, Is.EqualTo(3));
|
||||
Assert.That(RunContext.LastRunSeed, Is.EqualTo(seed));
|
||||
Assert.That(RunContext.HasPendingRun, Is.False);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
pending.SetValue(null, previousPending);
|
||||
last.SetValue(null, previousLast);
|
||||
}
|
||||
}
|
||||
|
||||
private RunConfig Config(int seed) => new(_distro, Language.C, Language.Haskell,
|
||||
new[] { _cards.FindById("lang_c_malloc"), _cards.FindById("lang_c_printf"), _cards.FindById("lang_c_pointer") }, seed);
|
||||
|
||||
private string CaptureShopSequence(int seed, bool consumeUnrelatedRandom)
|
||||
{
|
||||
_run.StartRun(Config(seed));
|
||||
Assert.That(_run.RepositoryVisitActive, Is.False);
|
||||
Assert.That(_run.RepositoryOffers, Is.Empty, "Restarting a run clears the previous repository visit.");
|
||||
_run.AddBits(1000);
|
||||
var player = new CombatantState(50, 8, 4);
|
||||
var output = new List<string>();
|
||||
for (int visit = 0; visit < 4; visit++)
|
||||
{
|
||||
if (consumeUnrelatedRandom)
|
||||
{
|
||||
for (int i = 0; i < 31 + visit; i++)
|
||||
{
|
||||
RandomRoll.RollRange(0, 999, RollContext.None);
|
||||
UnityEngine.Random.Range(0, 999);
|
||||
}
|
||||
}
|
||||
_run.GenerateRepositoryOffers(_cards, _languages);
|
||||
for (int roll = 0; roll < 3; roll++)
|
||||
{
|
||||
Assert.That(_run.RepositoryOffers, Is.Not.Empty);
|
||||
output.Add(string.Join(";", _run.RepositoryOffers.Select(OfferSignature)));
|
||||
Assert.That(_run.BuyOffer(_run.RepositoryOffers[0], player), Is.True);
|
||||
Assert.That(_run.RerollRepositoryOffers(_cards, _languages), Is.True);
|
||||
}
|
||||
}
|
||||
return string.Join("|", output);
|
||||
}
|
||||
|
||||
private string OfferSignature(RepositoryOffer offer)
|
||||
{
|
||||
int targetIndex = offer.TargetCard == null ? -1 : _run.RunDeck.ToList().IndexOf(offer.TargetCard);
|
||||
return $"{offer.Kind}:{offer.CardDefinition?.Id}:{targetIndex}:{offer.CardUpgradeKind}:{offer.StatUpgradeKind}:{offer.Price}:{offer.Description}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user