using System;
using System.Collections.Generic;
using System.Linq;
using KernelPanic.Combat;
using KernelPanic.Core;
using KernelPanic.Data;
namespace KernelPanic.Run
{
/// Optional, single-fight lessons using real cards and real resolution rules.
public static class LanguageTraining
{
public static readonly IReadOnlyList Supported = Array.AsReadOnly(new[]
{ Language.C, Language.CPlusPlus, Language.Rust, Language.Python, Language.JavaScript, Language.TypeScript, Language.Haskell, Language.Java, Language.Go });
public static bool IsSupported(Language language) => Supported.Contains(language);
public static RunConfig CreateConfig(Language language, DistroDefinition host, CardDatabase cards)
{
if (!IsSupported(language)) throw new ArgumentException("This language has no playable training deck yet.");
if (host == null || cards == null) throw new InvalidOperationException("Training content is unavailable.");
var deck = DeckIds(language).Select(id => cards.FindById(id)
?? throw new InvalidOperationException("Missing training card: " + id)).ToArray();
return new RunConfig(host, language, language, deck, 95200 + (int)language, 1) { TrainingLanguage = language };
}
public static string[] DeckIds(Language language) => language switch
{
Language.Python => new[] { "lang_py_import_antigravity", "shop_py_async_def", "lang_py_for_loop", "mint_cinnamon", "lang_py_print", "lang_py_print" },
Language.Haskell => new[] { "lang_hs_map", "lang_hs_thunk", "lang_hs_foldr" },
Language.C => new[] { "lang_c_malloc", "lang_c_pointer", "lang_c_printf" },
Language.CPlusPlus => new[] { "lang_cpp_malloc", "lang_cpp_pointer", "lang_cpp_printf" },
Language.TypeScript => new[] { "lang_ts_console_log", "lang_ts_fetch", "lang_ts_typeof" },
Language.Rust => new[] { "lang_rs_fn_main", "lang_rs_unwrap", "lang_rs_borrow" },
Language.JavaScript => new[] { "lang_js_console_log", "lang_js_fetch", "lang_js_typeof" },
Language.Java => new[] { "lang_java_new_object", "lang_java_system_out_println", "lang_java_public_static_main" },
Language.Go => new[] { "lang_go_worker", "lang_go_worker", "lang_go_func", "lang_go_context_timeout" },
_ => throw new ArgumentOutOfRangeException(nameof(language))
};
public static int OpeningDraw(Language language) => language == Language.Python ? 4 : DeckIds(language).Length;
public static int TargetUptime(Language language, int slot) => slot == 1 ? 24
: language is Language.Rust or Language.C or Language.Java ? 2 : 30;
public static IReadOnlyList BuildSteps(Language language)
{
var steps = new List();
void Read(string id, string title, string copy, string anchor = "TutorialQueue") =>
steps.Add(new TutorialStep(id, title, copy, anchor));
void Play(string id, string cardId, string title, string copy, bool targeted = true, int target = 0, string targetCopy = null)
{
steps.Add(new TutorialStep(id, title, copy, "card", TutorialAction.SelectCard) { CardId = cardId, TargetSlot = target });
if (targeted) steps.Add(new TutorialStep(id + "-target", "Choose your target",
targetCopy ?? "Click the highlighted enemy to play the selected card.", "enemy", TutorialAction.TargetEnemy)
{ CardId = cardId, TargetSlot = target });
}
void End(string id, string copy) => steps.Add(new TutorialStep(id, "End your turn", copy, "TutorialEndTurn", TutorialAction.EndTurn));
string name = LanguageCatalog.Get(language).DisplayName;
string objective = language switch
{
Language.CPlusPlus => "Learn C's evolved cards: stronger hits, full overkill spill and combos without segfault recoil.",
Language.TypeScript => "Learn to plan reliable attacks using JavaScript's evolved cards, with fixed damage and no failed typeof rolls.",
Language.Python => "You'll learn to build a combo that attacks this turn and repeats on later turns. First, we'll draw the cards we need. Then we'll put them in order.",
Language.Haskell => "You'll learn to make a stronger attack by playing your cards in the right order. We'll build a stack together, then work through why it resolves backward.",
Language.Rust => "You'll learn to turn an attack into protection. We'll defeat the weaker enemy first, use the leftover damage as Shield, then attack the stronger one.",
Language.C => "You'll learn to use one big hit against two enemies, then choose your next card around C's risks. Start by finding the enemy with only 2 Uptime.",
Language.JavaScript => "You'll learn to plan attacks when damage isn't guaranteed. We'll play one card, check what actually happened, then decide how to follow up.",
Language.Java => "You'll learn to make expensive attacks cheaper. We'll play two cards to build a discount, then use it on the heavier attack still in your hand.",
Language.Go => "You'll learn to keep attacks running across turns. We'll deploy a worker, wait for its first tick, then practise managing its remaining lifetime.",
_ => throw new ArgumentOutOfRangeException(nameof(language))
};
Read("welcome", "Learn to use " + name, objective, "enemies");
switch (language)
{
case Language.CPlusPlus:
Read("evolution", "C without the segfault", "C++ keeps C's immediate attacks and elevated critical chance. Damage is stronger, all overkill spills, and playing C++ never causes segfault recoil.", "enemies");
Play("burst", "lang_cpp_malloc", "Start with upgraded malloc", "Play malloc to begin your C++ chain. Your hand stays safe from segfault corruption.");
Read("combo", "Follow with pointer", "Pointer deals 20 base damage after a C++ card instead of 12. Use printf to repair corruption from other sources. Full excess damage spills to the next living enemy.", "enemies");
break;
case Language.TypeScript:
Read("evolution", "Plan around fixed values", "TypeScript upgrades JavaScript's cards. Damage has a fixed value and no critical roll. Buffs and enemy defenses still apply.", "enemies");
Play("fixed", "lang_ts_console_log", "Deal a reliable 9", "Play console.log(). Its base damage is always 9 instead of a random 3 to 9.");
Read("guarantee", "No failed typeof", "Fetch deals 13 base damage. Typeof always deals 16 instead of risking a failed roll. Check enemy defenses, then choose your follow-up.", "enemies");
break;
case Language.Go:
Read("go-slots", "Start a worker for future turns", "Go has 3 active slots, separate from hand RAM. A deployed worker leaves your hand and starts attacking next round. Its card returns to the discard pile when it stops.", "GoroutineScheduler");
Play("go-worker", "lang_go_worker", "Deploy your first worker", "Select go worker(). It will deal 3 base damage on each of its next 3 eligible rounds. Its first tick is next round, so you still need to plan for this turn's enemies.",
targetCopy: "Click the highlighted enemy. The worker keeps this target while it is alive, then finds the first living enemy if it needs a new one.");
End("go-wait", "End this turn. The worker was just deployed, so it will wait. Check that its remaining ticks stay at 3.");
Read("go-ready", "Your worker is ready now", "The worker can tick during this round's Interpret phase, before Python and Haskell. You can spend this turn's Cycles on another card while the worker keeps its slot.", "GoroutineScheduler");
End("go-tick", "End your turn again. Watch the worker hit its target and its remaining ticks fall from 3 to 2.");
Read("go-manage", "Choose how long to keep it running", "Select a PID in the scheduler, then use context.WithTimeout() to add 2 ticks, up to 5 remaining. Extensions do not attack immediately. Cancelling a worker frees its slot.", "GoroutineScheduler");
break;
case Language.Python:
Read("python-order", "Start with the setup", "A useful order is preparation -> automation -> payload. Preparation finds cards. Automation saves an effect for later. The payload supplies the damage or Shield. Let's build one together.");
Play("prepare", "lang_py_import_antigravity", "First, draw your attack", "We need a payload before this combo can work. Play import antigravity to draw 2 cards immediately. You have enough free RAM to hold them.", false);
Read("prepared", "Find print() in your hand", "You now have print(), our damage payload. Drawing first lets you see what you can build around before spending Cycles on the rest of the combo.", "TutorialRam");
Play("automate", "shop_py_async_def", "Save the next payload for later", "Play async def before print(). It draws a card now and joins the queue. When the queue runs, it will capture the next payload's damage for future turns.", false);
Play("modify", "lang_py_for_loop", "Make this turn's attack hit twice", "Play for loop before print(). It gives Shield and repeats the next payload. Placing it after the attack would be too late to repeat that attack.", false);
Play("payload", "lang_py_print", "Now add the attack", "Select print(). The setup is ready: for loop will repeat this attack, and async def will save its base damage for later turns.",
targetCopy: "Click the highlighted enemy to aim print() at it. Its Uptime will stay the same for now: Python's queued attacks wait until you end your turn.");
Play("synergy", "mint_cinnamon", "Use the attack to improve your defense", "Play cinnamon after print(). It gives more Shield when an earlier Python payload dealt damage that phase, so putting the attack first improves your defense.", false);
Read("queued", "Check the order before ending", "Read from left to right: async saves the payload, for loop repeats it, print() attacks, then cinnamon gains extra Shield. The oldest queued card runs first; this is FIFO.");
End("execute", "Click End turn to run the queue. Look for print() hitting twice, then check your Shield. Both results come from the order you chose.");
Read("automation", "Your setup will keep paying off", "Look at the automation slot. Async saved print's base damage for the next 2 rounds. For loop repeated this turn's attack; that extra hit was not saved.", "PythonAutomationReadout");
End("replay", "This time, end your turn without playing a card. Watch the saved attack run on its own, before the queue. You don't need another print() to trigger it.");
Read("limits", "Use the turns your script gives you", "The script just attacked for you. On your next turn, you can add another payload or prepare another combo. Check the slot's remaining duration; scripts expire and clear between waves.", "PythonAutomationReadout");
break;
case Language.Haskell:
Read("stack", "Save the bigger attack for second", "Haskell cards wait in a stack until End turn. Each card you add gets a stronger multiplier. Start with a smaller attack so the bigger one benefits from the extra depth.", "TutorialStack");
Play("setup", "lang_hs_map", "Start the stack with map", "Select map, our smaller attack. Playing it first gives the next card a stronger multiplier. It will wait in the stack rather than deal damage immediately.",
targetCopy: "Click the highlighted enemy to aim map at it. Then we'll add a stronger attack on top of map.");
Play("payoff", "lang_hs_thunk", "Add the stronger attack", "Select thunk. With map already in the stack, thunk gets x1.75 damage instead of the first card's x1.5. This is why we saved the larger hit for second.",
targetCopy: "Click the same enemy to aim thunk at it. Thunk goes on top of map, so it will hit that enemy first.");
Play("shield", "lang_hs_foldr", "Put your Shield on top", "Play foldr last. It gets the third card's x2 multiplier for its Shield and will run before both attacks. Its card draw stays the same.", false);
Read("snapshot", "Read the stack from the top", "Foldr is on top, then thunk, then map. Their multipliers were set when you played them: x2, x1.75, and x1.5. Later plays do not increase cards already in the stack.", "TutorialStack");
End("execute", "Click End turn. You'll gain Shield from foldr first, then thunk and map will attack. Last played, first resolved: this is LIFO.");
Read("result", "Use order to choose the payoff", "You used map to give thunk a stronger hit, then put foldr on top for more Shield. Next time, choose which effect needs the bigger multiplier before you start playing.", "TutorialStack");
break;
case Language.Rust:
Read("overkill", "Find a target you can overkill", "Rust turns leftover attack damage into your Shield. The enemy with 2 Uptime needs only a small hit to defeat it, so a stronger hit can protect you as well.", "TutorialShield");
Play("overkill-hit", "lang_rs_fn_main", "Attack to build Shield", "Select fn main(). We'll use it on the weaker enemy so the damage left after the kill becomes Shield. Rust attacks take effect immediately.",
targetCopy: "Click the enemy with 2 Uptime. After it falls, check your Shield: the damage it didn't need has become protection against the other enemy.");
Read("shield-result", "Notice what the kill gave you", "Your Shield increased when the enemy fell. Choosing that target first removed one attacker and helped protect you from the one still alive.", "TutorialShield");
Play("followup", "lang_rs_unwrap", "Turn to the remaining enemy", "Select unwrap() to damage the surviving enemy and draw a card. You already have some protection, so you can use this play to keep attacking.", true, 1,
"Click the remaining enemy. Unwrap deals its damage now, then draws a card if there is room in your hand.");
Play("defend", "lang_rs_borrow", "Add Shield without needing a kill", "Play borrow() to gain more Shield. Use this kind of card when an enemy has too much Uptime for you to overkill it this turn.", false);
End("execute", "Click End turn and watch the enemy's attack hit your Shield. The protection you built lets you survive while working down its Uptime.");
Read("result", "Plan your defense before ending", "Check the next enemy intent, then compare it with your Shield. Look for a kill that gives more Shield, or use borrow() when an attack alone won't protect you.", "enemies");
break;
case Language.C:
Read("burst", "Use the weaker enemy to reach both", "C attacks deal damage immediately. When a hit exceeds the target's remaining defenses, half of the leftover damage spills to another enemy. Let's use that to hurt both.", "enemies");
Play("burst-hit", "lang_c_malloc", "Choose an attack larger than the target", "Select malloc. It can deal more damage than the weaker enemy needs. Targeting that enemy lets you defeat it and spill some damage into the other one.",
targetCopy: "Click the enemy with 2 Uptime. Watch it fall, then check the other enemy's Uptime to see the spill damage.");
Read("risk", "Check your hand after attacking", "C cards can segfault: you lose 3 Uptime and a hand card becomes corrupted. Check whether that happened before choosing your next play. A corrupted card cannot be played yet.", "TutorialUptime");
// A corrupted follow-up must never lock the learner into an impossible guided action.
Read("combo", "Choose damage or repair", "Your last play was C, so pointer can follow it with a stronger hit. If a card is corrupted, printf can repair it after attacking. Check which cards you can still use.", "TutorialPiles");
Read("practice", "Make the next choice yourself", "Use pointer after another C card for more damage, or printf to repair a card. If neither is playable, end your turn; corruption wears off after 3 rounds.", "enemies");
break;
case Language.JavaScript:
Read("variance", "Plan for a low roll", "JavaScript attacks happen immediately, but their damage varies. When you need a kill to prevent an enemy attack, plan around the low end of the range and keep a follow-up ready.", "enemies");
Play("roll", "lang_js_console_log", "Play one attack before planning the next", "Select console.log(). Its base damage ranges from 3 to 9. We'll check the enemy's remaining Uptime after the hit before committing another card.",
targetCopy: "Click the highlighted enemy. Compare its Uptime before and after the hit to see how much damage you actually dealt.");
Read("result", "Work with the result you got", "The enemy is still alive. Check its remaining Uptime now. Use that number to plan your next attack, rather than assuming the first card dealt its maximum damage.", "enemies");
Play("followup", "lang_js_fetch", "Follow up while you have Cycles", "Select fetch() to keep damaging the same enemy. Read its damage range first: a wide range gives you a chance at a big hit, but you still need a plan for a small one.",
targetCopy: "Click the same enemy to play fetch(). Check its remaining Uptime again after this hit; each result changes what you need next.");
Read("chance", "Keep a backup for a failed roll", "Look at typeof in your hand. It can deal no damage, so don't count on it to stop an incoming attack. Keep enough resources for another play when a failed roll would leave you exposed.", "enemies");
End("execute", "Click End turn to let the surviving enemies act and refill your Cycles. These attacks already resolved, so there is no queued damage still coming to finish an enemy.");
break;
case Language.Java:
Read("ram", "Play cheaper cards before the heavy hit", "Every 2 Java plays reduce later Java costs by 1 Cycle. This is JIT warmup. We'll build that discount first. Each Java card also needs 2 RAM, so leave room when drawing more.", "TutorialRam");
Play("warmup-one", "lang_java_new_object", "Make your first warmup play", "Select new Object(). We'll defeat the weaker enemy with it. Java spills all leftover damage into another enemy, so this play also helps against the stronger target.",
targetCopy: "Click the enemy with 2 Uptime. The leftover damage will spill into the other enemy, and this play will count toward your first JIT discount.");
Play("warmup-two", "lang_java_system_out_println", "Reach the first discount", "Select System.out.println. This is your second Java play. After it hits, look at the Cycle cost of public static void main in your hand.", true, 1,
"Click the remaining enemy. This second Java play lowers the cost of the Java card you saved for next.");
Read("discount", "Check the card you saved", "Public static void main now costs 1 Cycle less. Playing the cheaper cards first lets you spend fewer Cycles on this heavier attack.", "TutorialCycles");
Play("payoff", "lang_java_public_static_main", "Spend the discount on your heavy attack", "Select public static void main at its reduced cost. Keep this order in mind: use cheaper Java plays to warm up before spending on heavier ones.", true, 1,
"Click the remaining enemy to use the discounted attack. Check your Cycles afterward to see what the play cost.");
End("execute", "Click End turn to refill your Cycles. Your JIT discount stays active for the rest of this wave, so the next hand can benefit from the setup too.");
Read("cooling", "Keep building on your setup", "Two more Java plays will improve the discount again, up to 3 Cycles off. It drops by 1 between waves, so check your card costs when a new fight begins.", "TutorialCycles");
break;
default: throw new ArgumentOutOfRangeException(nameof(language));
}
string practice = language switch
{
Language.CPlusPlus => "Finish the fight. Chain C++ cards for pointer's bonus, use full overkill spill, and repair corruption with printf. Your C++ attacks never cause segfaults.",
Language.TypeScript => "Finish the fight. Use fixed damage to plan each kill, accounting for Shield and other modifiers. Typeof always succeeds.",
Language.Python => "Finish the fight yourself. Draw before committing to a combo, put setup before payloads, and check your active script. End your turn when the queue is ready.",
Language.Haskell => "Finish the fight yourself. Start with a smaller card, save a bigger effect for a higher multiplier, then check the stack from top to bottom before ending your turn.",
Language.Rust => "Finish the fight yourself. Compare the enemy's intent with your Shield. Use overkill for protection when you can, and borrow() when you need Shield without a kill.",
Language.C => "Finish the fight yourself. Check for corrupted cards after each play, use pointer after another C card for extra damage, and use printf when you need a repair.",
Language.JavaScript => "Finish the fight yourself. Check remaining Uptime after every hit, plan around the low end of damage ranges, and keep a follow-up ready for a failed roll.",
Language.Java => "Finish the fight yourself. Play cheaper cards to build JIT, then use the discount on heavier attacks. Check their current costs before deciding what you can afford.",
Language.Go => "Finish the fight yourself. Deploy more workers while slots are free, select a PID to extend it, and end turns to let them tick. All workers stop when the wave ends.",
_ => throw new ArgumentOutOfRangeException(nameof(language))
};
Read("finish", "Now put it into practice", practice, "enemies");
return steps.AsReadOnly();
}
}
}