Files
linuxdistro-roguelite/Assets/Scripts/Combat/CardCatalog.cs
T

294 lines
23 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using KernelPanic.Core;
namespace KernelPanic.Combat
{
public enum CardTargeting { None, SingleEnemy, AllEnemies }
public enum CardEffectKind
{
Damage, Shield, Draw, ChanceDamage, PreviousLanguageDamage, QueuedGrowthDamage,
BtwScaledDamage, CompilationDraw, MakeJ4, HaskellFilter, HaskellMaybe,
NextTurnCycle, QueueRepeat, CopyLowestCostHandCard, UnattendedUpgrades,
Cleanse, ShuffleToken, UpgradeHandCards, GenerateCard, NoOp, UpdateManager,
ShieldRepeatDamage, Timeshift, DnfAutoremove, FirstToPackage, Rawhide,
IncomingAttackHalve, ConsultWiki, RandomCommonLanguageCard, MinimalInstall, SizeofDamage,
RepairCorruptedCards, AskUbuntu, FixedDraw, Python
}
/// <summary>
/// Immutable, unscaled effect data shared by execution, rules and read-only previews.
/// SecondaryAmount describes the conditional amount, threshold or rider for its Kind.
/// Preview readers must interpret the Kind; a custom effect is never implicitly plain damage.
/// </summary>
public sealed class CardEffectSpec
{
public CardEffectKind Kind { get; }
public int Amount { get; }
public int MaxAmount { get; }
public int SecondaryAmount { get; }
public Language Language { get; }
public bool AllEnemies { get; }
public bool TrueDamage { get; }
public bool CanCrit { get; }
public string ReferenceId { get; }
internal CardEffectSpec(CardEffectKind kind, int amount = 0, int maxAmount = -1,
int secondaryAmount = 0, Language language = default, bool allEnemies = false,
bool trueDamage = false, bool canCrit = true, string referenceId = null)
{
Kind = kind;
Amount = amount;
MaxAmount = maxAmount < 0 ? amount : maxAmount;
SecondaryAmount = secondaryAmount;
Language = language;
AllEnemies = allEnemies;
TrueDamage = trueDamage;
CanCrit = canCrit;
ReferenceId = referenceId;
}
public ICardEffect CreateRuntimeEffect()
{
return Kind switch
{
CardEffectKind.Damage => new DealDamageEffect(Amount, MaxAmount, Language, TrueDamage, CanCrit, AllEnemies),
CardEffectKind.Shield => new ShieldEffect(Amount),
CardEffectKind.Draw => new DrawEffect(Amount),
CardEffectKind.FixedDraw => new FixedDrawEffect(Amount),
CardEffectKind.Python => new PythonEffect((PythonOperation)SecondaryAmount, Amount),
CardEffectKind.AskUbuntu => new AskUbuntuEffect(),
CardEffectKind.ChanceDamage => new ChanceDamageEffect(Amount, SecondaryAmount, Language),
CardEffectKind.PreviousLanguageDamage => new PreviousLanguageDamageEffect(Amount, SecondaryAmount, Language),
CardEffectKind.QueuedGrowthDamage => new QueuedGrowthDamageEffect(Amount, Language),
CardEffectKind.BtwScaledDamage => new BtwScaledDamageEffect(Amount, SecondaryAmount, Language),
CardEffectKind.CompilationDraw => new CompilationDrawEffect(),
CardEffectKind.MakeJ4 => new MakeJ4Effect(),
CardEffectKind.HaskellFilter => new HaskellFilterEffect(),
CardEffectKind.HaskellMaybe => new HaskellMaybeEffect(),
CardEffectKind.NextTurnCycle => new NextTurnCycleEffect(Amount),
CardEffectKind.QueueRepeat => new QueueRepeatEffect(Amount),
CardEffectKind.CopyLowestCostHandCard => new CopyLowestCostHandCardEffect(),
CardEffectKind.UnattendedUpgrades => new ApplyStatusEffect(StatusType.UnattendedUpgrades, Amount, SecondaryAmount, targetSelf: true, skipNextTick: true),
CardEffectKind.Cleanse => new CleanseEffect(harmfulOnly: true),
CardEffectKind.ShuffleToken => new ShuffleTokenIntoDrawPileEffect(ReferenceId),
CardEffectKind.UpgradeHandCards => new UpgradeHandCardsEffect(Amount, MaxAmount),
CardEffectKind.GenerateCard => new GenerateCardEffect(Language, Rarity.Common),
CardEffectKind.NoOp => new NoOpCardEffect(ReferenceId),
CardEffectKind.UpdateManager => new UpdateManagerEffect(Amount),
CardEffectKind.ShieldRepeatDamage => new ConditionalShieldRepeatDamageEffect(Amount, Language),
CardEffectKind.Timeshift => new TimeshiftSnapshotEffect(),
CardEffectKind.DnfAutoremove => new DnfAutoremoveEffect(Amount, SecondaryAmount),
CardEffectKind.FirstToPackage => new FirstToPackageEffect(Amount),
CardEffectKind.Rawhide => new RawhideEffect(),
CardEffectKind.IncomingAttackHalve => new IncomingAttackHalveEffect(Amount),
CardEffectKind.ConsultWiki => new ConsultWikiEffect(),
CardEffectKind.RandomCommonLanguageCard => new RandomCommonLanguageCardEffect(),
CardEffectKind.MinimalInstall => new MinimalInstallEffect(),
CardEffectKind.SizeofDamage => new SizeofDamageEffect(),
CardEffectKind.RepairCorruptedCards => new RepairCorruptedCardsEffect(Amount),
_ => throw new InvalidOperationException($"Unsupported authored effect {Kind}.")
};
}
}
public sealed class CardCatalogEntry
{
private readonly string _rules;
public string Id { get; }
public CardTargeting Targeting { get; }
public bool RequiresSingleTarget => Targeting == CardTargeting.SingleEnemy;
public bool TargetsAllEnemies => Targeting == CardTargeting.AllEnemies;
public bool BleedingEdgeEligible { get; }
public IReadOnlyList<CardEffectSpec> Effects { get; }
internal CardCatalogEntry(string id, CardTargeting targeting, bool bleedingEdgeEligible,
string rules, params CardEffectSpec[] effects)
{
Id = id;
Targeting = targeting;
BleedingEdgeEligible = bleedingEdgeEligible;
_rules = rules;
Effects = Array.AsReadOnly((CardEffectSpec[])effects.Clone());
}
public IReadOnlyList<ICardEffect> CreateEffects()
{
ICardEffect[] result = new ICardEffect[Effects.Count];
for (int i = 0; i < result.Length; i++) result[i] = Effects[i].CreateRuntimeEffect();
return result;
}
/// <summary>
/// Rules reference descriptor values by index: amount/shield/max/secondary scale with
/// card magnitude, while raw/rawSecondary/rawMax deliberately remain unscaled.
/// Queue growth is resolved from the card without changing any combat state.
/// </summary>
public string GetRulesText(CardInstance card)
=> GetRulesText(card, null, 1);
public string GetRulesText(CardInstance card, string distroId, int distroVersion)
{
if (_rules == null)
{
return string.IsNullOrWhiteSpace(card?.Definition?.Description)
? "effect not authored."
: card.Definition.Description + (card.UpgradeLevel > 0 ? " [upgraded]" : string.Empty);
}
int inspectCount = PythonPipeline.InspectCount(distroId, distroVersion);
int btwMultiplier = string.Equals(distroId, "arch", StringComparison.OrdinalIgnoreCase) && distroVersion >= 3 ? 4 : 3;
string text = _rules.Replace("{inspectCount}", inspectCount.ToString(CultureInfo.InvariantCulture))
.Replace("{btwMultiplier}", btwMultiplier.ToString(CultureInfo.InvariantCulture));
for (int i = 0; i < Effects.Count; i++)
{
CardEffectSpec effect = Effects[i];
text = text.Replace("{plural:" + i + "}", UpgradeMath.ScaleAmount(effect.Amount, card) == 1 ? string.Empty : "s");
text = Replace(text, "amount", i, UpgradeMath.ScaleAmount(effect.Amount, card));
text = Replace(text, "shield", i, UpgradeMath.ScaleShield(effect.Amount, card));
text = Replace(text, "max", i, UpgradeMath.ScaleAmount(effect.MaxAmount, card));
text = Replace(text, "secondary", i, UpgradeMath.ScaleAmount(effect.SecondaryAmount, card));
text = Replace(text, "secondaryShield", i, UpgradeMath.ScaleShield(effect.SecondaryAmount, card));
text = Replace(text, "raw", i, effect.Amount);
text = Replace(text, "rawMax", i, effect.MaxAmount);
text = Replace(text, "rawSecondary", i, effect.SecondaryAmount);
text = Replace(text, "failure", i, 100 - effect.SecondaryAmount);
text = Replace(text, "growth", i, UpgradeMath.ScaleAmount(effect.Amount + UnityEngine.Mathf.Max(0, (card?.QueuePlayCount ?? 0) - 1), card));
text = Replace(text, "listGrowth", i, CardScaling.Add(UpgradeMath.ScaleAmount(effect.Amount, card),
(int)Math.Min(int.MaxValue, 2L * Math.Max(0, (card?.QueuePlayCount ?? 0) - 1))));
text = Replace(text, "pythonBonus", i, UpgradeMath.ScaleAmount(5, card));
}
return text + (card?.UpgradeLevel > 0 ? " [upgraded]" : string.Empty);
}
private static string Replace(string text, string name, int index, int value)
{
return text.Replace("{" + name + ":" + index + "}", value.ToString(CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Each built-in card ID is authored once here. Asset IDs and serialized effect overrides
/// stay unchanged; adding a card no longer requires parallel effect, targeting and rules switches.
/// </summary>
public static class CardCatalog
{
private const CardTargeting None = CardTargeting.None;
private const CardTargeting Single = CardTargeting.SingleEnemy;
private const CardTargeting All = CardTargeting.AllEnemies;
private const string CRecoil = " 50% of overkill spills per jump (rounded down). 25%: segfault, uptime -3 and corrupt a hand card.";
private const string Jit = " overkill spills. JIT: -1 Cycle per 2 Java plays, capped at -3; discount cools by 1 after each wave.";
private static readonly IReadOnlyDictionary<string, CardCatalogEntry> ById;
public static IReadOnlyList<CardCatalogEntry> Entries { get; }
static CardCatalog()
{
var entries = new List<CardCatalogEntry>();
void Add(string id, CardTargeting target, bool bleeding, string rules, params CardEffectSpec[] effects)
=> entries.Add(new CardCatalogEntry(id, target, bleeding, rules, effects));
Add("gentoo_emerge_sync", None, false, "stack: gain {shield:0} Shield. draw {amount:1}.", Shield(2), Draw(1));
Add("gentoo_ebuild", Single, false, "stack: deal {amount:0}.", Damage(4, Language.Haskell));
Add("gentoo_use_flags", None, false, "gain {shield:0} Shield. if Compilation >= {rawSecondary:1}, draw {amount:1}.",
Shield(4), Fx(CardEffectKind.CompilationDraw, CompilationDrawEffect.DrawCount, secondary: CompilationDrawEffect.RequiredCompilation));
Add("gentoo_make_j4", Single, true, "deal {amount:0}; {secondary:0} if Compilation >= {rawMax:0}. one C hit; 50% of overkill spills per jump (rounded down); 25%: segfault, uptime -3 and corrupt a hand card.",
Fx(CardEffectKind.MakeJ4, MakeJ4Effect.BaseDamage, MakeJ4Effect.RequiredCompilation, MakeJ4Effect.CompiledDamage, Language.C));
Add("gentoo_emerge_world", All, false, "stack: deal {amount:0} to all living enemies. gain {shield:1} Shield.", Damage(3, Language.Haskell, all: true), Shield(2));
Add("lang_hs_map", Single, false, "stack: deal {amount:0}.", Damage(2, Language.Haskell));
Add("lang_hs_foldr", None, false, "stack: gain {shield:0} Shield. draw {amount:1}.", Shield(3), Draw(1));
Add("lang_hs_thunk", Single, false, "stack: deal {amount:0}.", Damage(4, Language.Haskell));
Add("shop_hs_filter", Single, false, "stack: deal {amount:0}; {secondary:0} if target has a harmful status. one hit.",
Fx(CardEffectKind.HaskellFilter, HaskellFilterEffect.BaseDamage, secondary: HaskellFilterEffect.ConditionalDamage, language: Language.Haskell));
Add("shop_hs_fmap", None, false, "stack: gain {shield:0} Shield.", Shield(2));
Add("shop_hs_zipwith", All, false, "stack: deal {amount:0} to all living enemies.", Damage(2, Language.Haskell, all: true));
Add("shop_hs_maybe", None, false, "stack: if below maximum Uptime, restore {amount:0} Uptime. otherwise gain {secondaryShield:0} Shield.",
Fx(CardEffectKind.HaskellMaybe, HaskellMaybeEffect.HealAmount, secondary: HaskellMaybeEffect.ShieldAmount));
Add("shop_hs_sequence", None, false, "stack: draw {amount:0}.", Draw(2));
Add("shop_hs_const", Single, false, "stack: deal {amount:0}.", Damage(2, Language.Haskell));
Add("shop_hs_scanl", None, false, "stack: gain {shield:0} Shield. draw {amount:1}.", Shield(4), Draw(1));
Add("shop_hs_foldmap", All, false, "stack: deal {amount:0} to all living enemies.", Damage(4, Language.Haskell, all: true));
Add("shop_hs_deepseq", Single, false, "stack: deal {amount:0}.", Damage(9, Language.Haskell));
Add("shop_hs_dollar_bang", Single, false, "stack: deal {amount:0}. gain {shield:1} Shield.", Damage(5, Language.Haskell), Shield(2));
Add("lang_py_print", Single, false, "payload: deal {amount:0} to a chosen enemy.", Py(PythonOperation.Print, 4));
Add("lang_py_import_antigravity", None, false, "preparation: immediately draw 2 (fixed).", Py(PythonOperation.Draw, 2));
Add("lang_py_for_loop", None, false, "modifier: gain {shield:0} Shield. repeat the next payload once. unused repeat expires after next round.", Py(PythonOperation.Loop, 2));
Add("lang_js_console_log", Single, true, "deal {amount:0}-{max:0}.", Damage(3, Language.JavaScript, 9));
Add("lang_js_fetch", Single, true, "deal {amount:0}-{max:0}.", Damage(5, Language.JavaScript, 13));
Add("lang_rs_fn_main", Single, true, "deal {amount:0}. overkill becomes shield.", Damage(6, Language.Rust));
Add("lang_rs_unwrap", Single, true, "deal {amount:0}. overkill becomes shield. draw {raw:1} (fixed).", Damage(5, Language.Rust), Fx(CardEffectKind.FixedDraw, 1));
Add("lang_rs_borrow", None, false, "gain {shield:0} shield.", Shield(5));
Add("lang_c_malloc", Single, true, "deal {amount:0}." + CRecoil, Damage(12, Language.C));
Add("lang_c_printf", Single, true, "deal {amount:0}." + CRecoil + " then repair up to {amount:1} corrupted card{plural:1} in your active deck.",
Damage(4, Language.C), Fx(CardEffectKind.RepairCorruptedCards, 1));
Add("lang_c_pointer", Single, true, "deal {amount:0}; {secondary:0} if the previous card was C." + CRecoil,
Fx(CardEffectKind.PreviousLanguageDamage, 9, secondary: 15, language: Language.C));
Add("lang_java_public_static_main", Single, true, "uses 2 RAM. deal {amount:0}." + Jit, Damage(7, Language.Java));
Add("lang_java_system_out_println", Single, true, "uses 2 RAM. deal {amount:0}." + Jit, Damage(5, Language.Java));
Add("lang_java_new_object", Single, true, "uses 2 RAM. deal {amount:0}." + Jit, Damage(3, Language.Java));
Add("ubuntu_ask_ubuntu", None, false, "preparation: inspect the top {inspectCount} cards. take one; others keep their order.", Py(PythonOperation.Inspect));
Add("ubuntu_unattended_upgrades", None, false, "automation: gain {shield:0} Shield now and before the queue on each of the next 2 rounds. uses 1 of 2 script slots.", Py(PythonOperation.Unattended, 3));
Add("mint_fix_broken", Single, true, "deal {amount:0}. cleanse harmful statuses.", Damage(8, Language.JavaScript), Fx(CardEffectKind.Cleanse));
Add("lang_js_typeof", Single, true, "{rawSecondary:0}%: deal {amount:0}. {failure:0}%: deal 0.", Fx(CardEffectKind.ChanceDamage, 14, secondary: 75, language: Language.JavaScript));
Add("shop_py_list_append", Single, false, "payload: deal {listGrowth:0}. +2 per subsequent hand play this wave; replays do not grow it.", Py(PythonOperation.List, 6));
Add("shop_py_async_def", None, false, "automation: draw 1 immediately on play (fixed). capture the next payload without modifiers; replay it before the queue on each of the next 2 rounds. uses 1 of 2 script slots.", Py(PythonOperation.Async));
Add("shop_py_zip", Single, false, "modifier: deal {amount:0}. the next single-target damage payload hits all enemies. unused conversion expires after next round.", Py(PythonOperation.Zip, 2));
Add("shop_js_math_random", Single, true, "deal {amount:0}-{max:0}.", Damage(2, Language.JavaScript, 20));
CardEffectSpec promiseHit = Damage(4, Language.JavaScript, 10);
Add("shop_js_promise_all", Single, true, "deal {amount:0}-{max:0} three times.", promiseHit, promiseHit, promiseHit);
Add("shop_js_spread", None, false, "copy the lowest-cost card in hand.", Fx(CardEffectKind.CopyLowestCostHandCard));
Add("ubuntu_snap_install", Single, true, "deal {amount:0}-{max:0}. shuffle an ubuntu pro trial into your draw pile.", Damage(6, Language.JavaScript, 14),
new CardEffectSpec(CardEffectKind.ShuffleToken, referenceId: "ubuntu_pro_trial"));
Add("ubuntu_do_release_upgrade", None, false, "upgrade {amount:0}-{max:0} random hand cards.", Fx(CardEffectKind.UpgradeHandCards, 1, 3));
Add("ubuntu_apt_install", None, false, "preparation: choose one of 3 Python cards. temporary copy costs 0 this turn and exhausts after play.", Py(PythonOperation.Install));
Add("ubuntu_pro_trial", None, false, null, new CardEffectSpec(CardEffectKind.NoOp, referenceId: "ubuntu pro trial is unplayable"));
// UpdateManager expands its own targets at execution, and historically asks for no target.
Add("mint_update_manager", All, false, "payload: deal {amount:0} to all enemies; {pythonBonus:0} if an automation ran earlier this phase.", Py(PythonOperation.Update, 3, true));
Add("mint_cinnamon", None, false, "payload: gain {shield:0} Shield; {pythonBonus:0} if an earlier Python payload dealt damage this phase.", Py(PythonOperation.Cinnamon, 3));
Add("mint_nemo", Single, true, "deal {amount:0}. if you have shield, deal it again.", Fx(CardEffectKind.ShieldRepeatDamage, 6, language: Language.JavaScript));
Add("mint_timeshift", None, false, "automation: save your Uptime when this resolves. after enemies act in each of the next 2 rounds, heal up to 6 Uptime, without exceeding the saved value. no healing this round or if defeated. healing does not scale with upgrades. uses 1 of 2 script slots until both rounds finish; ends when the wave ends.", Py(PythonOperation.Timeshift));
Add("fedora_dnf_autoremove", Single, true, "deal {amount:0}. exhaust a junk/NOP/broken card from hand; if you do, gain {secondary:0} cycle. if not, do nothing.", Fx(CardEffectKind.DnfAutoremove, 7, secondary: 1, language: Language.Rust));
Add("fedora_first_to_package", Single, true, "deal {amount:0}. Java cards cost 1 less this turn.", Fx(CardEffectKind.FirstToPackage, 7, language: Language.Java));
Add("fedora_dnf_update", Single, true, "deal {amount:0}." + Jit, Damage(9, Language.Java));
Add("fedora_rawhide", None, false, "draw {amount:0}. next card this turn gains bleeding edge.", Fx(CardEffectKind.Rawhide, RawhideEffect.DrawCount));
Add("fedora_selinux", None, false, "next {amount:0} enemy attacks deal half damage.", Fx(CardEffectKind.IncomingAttackHalve, 2));
Add("arch_pacman_syu", Single, true, "deal {amount:0}." + CRecoil, Damage(14, Language.C));
Add("arch_consult_wiki", Single, true, "fix a broken card and draw {secondary:0}; if none, deal {amount:0}.",
Fx(CardEffectKind.ConsultWiki, ConsultWikiEffect.FallbackDamage, secondary: ConsultWikiEffect.DrawCount, language: Language.Rust));
Add("arch_makepkg", Single, true, "deal {amount:0} + btw x{btwMultiplier}." + CRecoil, Fx(CardEffectKind.BtwScaledDamage, 7, secondary: 3, language: Language.C));
Add("arch_aur_helper", None, false, "add a random C/Rust common card to hand; it costs 0 this turn.", Fx(CardEffectKind.RandomCommonLanguageCard));
Add("arch_minimal_install", None, false, "exhaust up to {raw:0} hand cards. per card: gain {rawSecondary:0} Shield and {rawMax:0} Cycle.",
Fx(CardEffectKind.MinimalInstall, MinimalInstallEffect.MaxExhaust, MinimalInstallEffect.CyclesPerCard, MinimalInstallEffect.ShieldPerCard));
Add("shop_c_free", Single, true, "deal {amount:0}. 50% of overkill spills per jump (rounded down). 25%: segfault, uptime -3, corrupt a hand card, and draw 2. then repair up to {amount:1} corrupted card{plural:1} in your active deck.",
Damage(18, Language.C), Fx(CardEffectKind.RepairCorruptedCards, 1));
Add("shop_c_sizeof", Single, true, "deal {raw:0} x C cards in hand." + CRecoil, Fx(CardEffectKind.SizeofDamage, SizeofDamageEffect.DamagePerCard, language: Language.C));
Add("shop_c_kernel_exploit", Single, true, "deal {amount:0} ignoring Shield. 50% of overkill spills per jump (rounded down). 50%: segfault, uptime -8 and corrupt a hand card.", Damage(36, Language.C, trueDamage: true));
Add("shop_c_gdb", None, false, "repair up to {amount:0} corrupted cards in your active deck. draw {amount:1}.",
Fx(CardEffectKind.RepairCorruptedCards, 2), Draw(1));
Add("shop_c_valgrind", None, false, "repair up to {amount:0} corrupted cards in your active deck. gain {shield:1} Shield.",
Fx(CardEffectKind.RepairCorruptedCards, 3), Shield(6));
var byId = new Dictionary<string, CardCatalogEntry>(StringComparer.Ordinal);
foreach (CardCatalogEntry entry in entries) byId.Add(entry.Id, entry);
Entries = entries.AsReadOnly();
ById = new ReadOnlyDictionary<string, CardCatalogEntry>(byId);
}
public static bool TryGet(string id, out CardCatalogEntry entry)
{
entry = null;
return id != null && ById.TryGetValue(id, out entry);
}
private static CardEffectSpec Damage(int amount, Language language, int max = -1, bool all = false, bool trueDamage = false)
=> new(CardEffectKind.Damage, amount, max, language: language, allEnemies: all, trueDamage: trueDamage);
private static CardEffectSpec Shield(int amount) => new(CardEffectKind.Shield, amount);
private static CardEffectSpec Draw(int amount) => new(CardEffectKind.Draw, amount);
private static CardEffectSpec Py(PythonOperation operation, int amount = 0, bool all = false) =>
new(CardEffectKind.Python, amount, secondaryAmount: (int)operation, language: Language.Python, allEnemies: all);
private static CardEffectSpec Fx(CardEffectKind kind, int amount = 0, int max = -1, int secondary = 0, Language language = default, bool all = false)
=> new(kind, amount, max, secondary, language, all);
}
}