class_name Flow ## The parts of a turn that run on their own once started (flow.ts): resolving declarations, ## combat, defeat, capture and drawing. Card scripts call into these too (Trace Route starts a ## combat, Disconnect pulls an Anomaly out of one). ## Lets an app see each combat as it happens, since a whole combat can run inside one ## apply_action. Called with (s, stage) after a combat starts ("start"), after the roll ("roll"), ## before the result is applied, with the final dice ("result"), and when it ends ("end"). Unset in ## the sim and tests. static var combat_watch := Callable() # --------------------------------------------------------------------------------------------- # Drawing static func draw_cards(s: Dictionary, p: int, n: int) -> void: var tasks := [] for i in n: tasks.append({"t": "draw", "player": p}) State.push_tasks(s, tasks) ## Draws one card without "when drawing" effects. Returns it, or null for an empty deck. static func draw_now(s: Dictionary, p: int) -> Variant: var pl: Dictionary = s.players[p] if pl.deck.is_empty(): return null var card: int = pl.deck.pop_front() pl.hand.append(card) State.stat(s, "cardsDrawn", p) return card static func draw_task(s: Dictionary, p: int) -> void: var card = draw_now(s, p) if card != null: Query.emit(s, "onDraw", {"player": p, "card": card}) # --------------------------------------------------------------------------------------------- # Anomalies leaving and entering play static func _clear_status(av: Dictionary) -> void: av.infection = 0 av.bugs = 0 av.mods = [] av.disabledUntil = 0 av.rootedUntil = 0 av.damage = 0 ## A copy leaves play. It has no Hardware of its own. static func remove_copy(s: Dictionary, r: Dictionary) -> void: var cp := State.anomaly(s, r) if cp.at == null: return State.say(s, "%s is removed." % State.anomaly_name(s, r), {"tag": "move", "player": r.player, "ref": r}) _clear_status(cp) cp.permanentInfection = 0 cp.at = null cp.faceUp = false drop_from_combat(s, r) ## Recruit rule: an Anomaly recruited over leaves for the discard pile with its Hardware, and its ## copy leaves play. static func retire_anomaly(s: Dictionary, r: Dictionary) -> void: var av := State.anomaly(s, r) remove_copy(s, State.copy_of(r)) if not av.hardware.is_empty(): State.stat(s, "hardwareLost", r.player, av.hardware.size()) for hw in av.hardware: State.to_discard(s, hw) var from := " from %s" % av.at if av.at else "" State.say(s, "%s retires %s%s." % [State.player_name(r.player), State.anomaly_name(s, r), from], {"tag": "card", "player": r.player, "ref": r}) drop_from_combat(s, r) State.to_discard(s, av.card) State.stat(s, "anomaliesRetired", r.player) ## The loser of a combat goes back to its slot. Its Hardware is discarded unless `keep_hardware`, ## and its counters and effects are removed (Persistent Infection's counters stay). static func knock_out(s: Dictionary, r: Dictionary, loc: String, role: String, keep_hardware: bool) -> void: var av := State.anomaly(s, r) if av.at == null: return if State.is_copy(r): remove_copy(s, r) else: if not keep_hardware and not av.hardware.is_empty(): var names := ", ".join(av.hardware.map(func(h): return State.name_of(s, h))) State.say(s, "%s loses %s." % [State.player_name(r.player), names], {"tag": "combat", "player": r.player, "ref": r, "loc": loc}) State.stat(s, "hardwareLost", r.player, av.hardware.size()) for hw in av.hardware: State.to_discard(s, hw) av.hardware = [] _clear_status(av) av.at = null State.say(s, "%s returns to its slot." % State.anomaly_name(s, r), {"tag": "combat", "player": r.player, "ref": r, "loc": loc}) remove_copy(s, State.copy_of(r)) State.stat(s, "anomaliesDestroyed", r.player) Query.emit(s, "onDefeat", {"ref": r, "loc": loc, "role": role}) ## Health rule: an Anomaly returns to its slot by its owner's choice, as its placement for the ## round. It heals fully and keeps its Hardware; like a defeat, its counters and effects come off ## (Persistent Infection's stay) and its copy leaves play. static func recall_anomaly(s: Dictionary, r: Dictionary) -> void: var av := State.anomaly(s, r) if av.at == null: return var healed: int = av.damage var healing := ", healing %d damage" % healed if healed else "" State.say(s, "%s recalls %s from %s to its slot%s." % [State.player_name(r.player), State.anomaly_name(s, r), av.at, healing], {"tag": "move", "player": r.player, "ref": r, "loc": av.at}) _clear_status(av) av.at = null av.placed = true remove_copy(s, State.copy_of(r)) State.stat(s, "recalls", r.player) ## Places a copy of `original` on `to`. static func create_copy(s: Dictionary, original: Dictionary, to: String) -> void: var r := State.copy_of(original) var cp := State.anomaly(s, r) cp.merge({ "card": State.anomaly(s, original).card, "faceUp": true, "at": to, "hardware": [], "damage": 0, "placed": false, "advanced": false, "attacks": 0, "infection": 0, "bugs": 0, "permanentInfection": 0, "mods": [], "disabledUntil": 0, "rootedUntil": 0, "breachedTurn": 0, "createdTurn": s.turn, }, true) State.say(s, "%s creates %s on %s." % [State.player_name(r.player), State.anomaly_name(s, r), to], {"tag": "card", "player": r.player, "ref": r, "loc": to}) ## Moves an Anomaly by a card effect. Callers check the move is legal. static func move_anomaly(s: Dictionary, r: Dictionary, to: String) -> void: var av := State.anomaly(s, r) State.say(s, "%s moves %s → %s." % [State.anomaly_name(s, r), av.at, to], {"tag": "move", "player": r.player, "ref": r, "loc": to}) av.at = to # --------------------------------------------------------------------------------------------- # Step 5: declarations static func resolve_declaration(s: Dictionary, index: int) -> void: var d: Dictionary = s.declarations[index] var p: int = d.ref.player var av := State.anomaly(s, d.ref) var who := State.anomaly_name(s, d.ref) if av.at == null: return if State.is_rooted(s, d.ref) or State.is_disabled_anomaly(s, d.ref): State.say(s, "%s can't move, so its %s on %s fails." % [who, d.kind, d.to], {"tag": "move", "player": p, "ref": d.ref, "loc": d.to}) return if State.controls(s, p, d.to): return var defended := not State.defenders_at(s, d.to).is_empty() var how := "capturesUnowned" if s.locations[d.to].owner == null else "capturesUndefended" if d.kind == "advance": var reachable := false for n in State.neighbors(s, d.to): if State.controls(s, p, n): reachable = true if not reachable: State.say(s, "%s can no longer reach %s." % [who, d.to], {"tag": "move", "player": p, "ref": d.ref, "loc": d.to}) return if defended: State.start_combat(s, d.ref, av.at, d.to, "advance") else: State.push_capture(s, d.ref, d.to, how) return if (not d.second and av.at != d.from) or not State.controls(s, p, d.from) or not State.adjacent(s, d.from, d.to): State.say(s, "%s's attack on %s fails: it's no longer in position." % [who, d.to], {"tag": "move", "player": p, "ref": d.ref, "loc": d.to}) return if defended: State.start_combat(s, d.ref, d.from, d.to, "attack") else: State.push_capture(s, d.ref, d.to, how) # --------------------------------------------------------------------------------------------- # Combat static func _roll(s: Dictionary, n: int) -> Array: var out := [] for i in n: out.append(State.roll_die(s)) return out static func _side_totals(c: Dictionary) -> Dictionary: var def_total := 0 for d in c.defenders: def_total += Util.sum(c.dice.get(State.ref_key(d), [])) return {"atk": Util.sum(c.dice.get(State.ref_key(c.attacker), [])), "def": def_total} static func _dice_text(s: Dictionary, c: Dictionary) -> String: var one := func(r): var dice: Array = c.dice.get(State.ref_key(r), []) return "%s [%s]" % [State.name_of(s, State.anomaly(s, r).card), ",".join(dice.map(func(v): return str(v)))] var t := _side_totals(c) return "%s=%d vs %s=%d" % [one.call(c.attacker), t.atk, " + ".join(c.defenders.map(one)), t.def] ## Rolls every fighter's dice, rerolling all of them on a tie (under the health rule a tie stands). static func _roll_all(s: Dictionary, c: Dictionary) -> void: while true: for r in State.fighters_of(c): c.dice[State.ref_key(r)] = _roll(s, Query.cores_of(s, Query.fighter_of(s, r))) var t := _side_totals(c) if t.atk != t.def or s.rules.health.on: break c.ties += 1 c.rolled = true # --------------------------------------------------------------------------------------------- # Damage (health rule) ## Health text for the combat log, e.g. "7/10 Health". static func health_text(s: Dictionary, r: Dictionary) -> String: return "%d/%d Health" % [Query.health_left(s, r), Query.health_of(s, r)] ## Deals `n` damage to an Anomaly. Returns true if that destroys it (its damage reaches its Health). static func damage_anomaly(s: Dictionary, r: Dictionary, n: int) -> bool: var av := State.anomaly(s, r) if av.at == null or n <= 0: return false av.damage += n var dead: bool = av.damage >= Query.health_of(s, r) var how := " and is destroyed" if dead else " (%s)" % health_text(s, r) State.say(s, "%s takes %d damage%s." % [State.anomaly_name(s, r), n, how], {"tag": "combat", "player": r.player, "ref": r, "amount": n, "loc": av.at}) return dead ## The attacker splits `n` damage among the defenders. The engine picks the split that destroys the ## most defenders, weakest first, and puts what's left on the weakest survivor. Returns the hits as ## {ref, amount, destroyed}. static func _allocate_damage(s: Dictionary, defenders: Array, n: int) -> Array: var order := Util.sorted_by(defenders, func(r): return Query.health_left(s, r)) var hits := [] for i in order.size(): if n <= 0: break var d: Dictionary = order[i] var left := Query.health_left(s, d) var hit := left if n >= left else (n if i == order.size() - 1 else 0) if hit == 0: # Can't destroy this one: dump the rest on the weakest. var first = null for h in hits: if State.ref_key(h.ref) == State.ref_key(order[0]): first = h break damage_anomaly(s, order[0], n) if first != null: first.amount += n else: hits.append({"ref": order[0], "amount": n, "destroyed": false}) break n -= hit hits.append({"ref": d, "amount": hit, "destroyed": damage_anomaly(s, d, hit)}) return hits ## Tells the cards that an Anomaly lost a roll (Resource Leech, Overflow Error, Persistent Infection). static func _emit_damage(s: Dictionary, r: Dictionary, loc: String, role: String, amount: int, destroyed: bool) -> void: Query.emit(s, "onDamage", {"ref": r, "loc": loc, "role": role, "amount": amount, "destroyed": destroyed}) ## Health rule: the loser of the roll takes the difference as damage; destroyed Anomalies are defeated. static func _resolve_damage(s: Dictionary, c: Dictionary, t: Dictionary) -> void: if t.atk == t.def: c.ties += 1 State.stat(s, "combatsTied", c.attacker.player) State.say(s, "Tie, %d to %d: a stalemate. Nobody takes damage." % [t.atk, t.def], {"tag": "combat", "loc": c.loc}) return var margin: int = absi(t.atk - t.def) var attacker_wins: bool = t.atk > t.def var defender: int = s.locations[c.loc].owner var winner: int = c.attacker.player if attacker_wins else defender State.say(s, "%s wins the roll, %d to %d: %d damage." % [State.player_name(winner), t.atk, t.def, margin], {"tag": "combat", "player": winner, "loc": c.loc}) State.stat(s, "damageDealt", winner, margin) if attacker_wins: State.stat(s, "attacksWon", c.attacker.player) var hits := _allocate_damage(s, c.defenders, margin) var tasks := [] var destroyed := 0 for h in hits: if h.destroyed: destroyed += 1 tasks.append({"t": "defeat", "ref": h.ref, "loc": c.loc, "role": "defend"}) if destroyed == c.defenders.size(): tasks.append({"t": "capture", "ref": c.attacker, "loc": c.loc, "stage": "hooks", "how": "capturesInCombat"}) else: State.say(s, "%s holds." % c.loc, {"tag": "combat", "player": defender, "loc": c.loc}) State.push_tasks(s, tasks) # Pushed last, so the damage hooks run before the defeats. for h in hits: _emit_damage(s, h.ref, c.loc, "defend", h.amount, h.destroyed) else: var dead := damage_anomaly(s, c.attacker, margin) if dead: State.push_tasks(s, [{"t": "defeat", "ref": c.attacker, "loc": c.loc, "role": "attack"}]) _emit_damage(s, c.attacker, c.loc, "attack", margin, dead) ## Cores that change after the roll change the dice: each Core gained rolls one more die, and each ## Core lost removes that Anomaly's highest die. static func sync_dice(s: Dictionary, c: Dictionary) -> void: if not c.rolled: return for r in State.fighters_of(c): var key := State.ref_key(r) var dice: Array = c.dice.get(key, []) var want := Query.cores_of(s, Query.fighter_of(s, r)) while dice.size() < want: dice.append(State.roll_die(s)) if dice.size() > want: dice.sort() dice.resize(want) c.dice[key] = dice ## Current dice totals {atk, def}, after syncing to Core changes. static func combat_totals(s: Dictionary) -> Dictionary: var c: Dictionary = s.combat sync_dice(s, c) return _side_totals(c) ## Stops the current combat with no winner. static func end_combat(s: Dictionary, why: String) -> void: var c = s.combat if c == null or c.ended: return c.ended = true State.stat(s, "combatsEnded", c.attacker.player) State.say(s, "The combat at %s ends: %s." % [c.loc, why], {"tag": "combat", "loc": c.loc}) ## Takes an Anomaly out of the current combat. If the attacker leaves, the combat ends. If the last ## defender leaves, the attacker takes the location as undefended. static func drop_from_combat(s: Dictionary, r: Dictionary) -> void: var c = s.combat if c == null or c.ended: return if State.same_ref(c.attacker, r): end_combat(s, "%s left it" % State.anomaly_name(s, r)) return var i := -1 for k in c.defenders.size(): if State.same_ref(c.defenders[k], r): i = k break if i < 0: return c.defenders.remove_at(i) c.dice.erase(State.ref_key(r)) if c.defenders.is_empty(): end_combat(s, "no defender is left") if State.anomaly(s, c.attacker).at != null: State.push_capture(s, c.attacker, c.loc, "capturesUndefended") static func combat_stage(s: Dictionary, stage: String) -> void: var c = s.combat if c == null: return _run_stage(s, c, stage) # "result" is watched inside _run_stage, before the damage. if (stage == "start" or stage == "roll" or stage == "end") and combat_watch.is_valid(): combat_watch.call(s, stage) static func _run_stage(s: Dictionary, c: Dictionary, stage: String) -> void: if stage == "cleanup": for r in State.fighters_of(c): var av := State.anomaly(s, r) av.mods = av.mods.filter(func(m): return m.get("combat") != c.id) s.combat = null return if stage == "end": State.push_tasks(s, [{"t": "combat", "stage": "cleanup"}]) if c.rolled or c.ended: Query.emit(s, "onCombatEnd", null) return if c.ended: return if stage == "start": State.stat(s, "attacks", c.attacker.player) var sides := Query.side_cores(s) var hp := func(r): return ", %s" % health_text(s, r) if s.rules.health.on else "" var defs := " and ".join(c.defenders.map(func(d): return "%s%s" % [State.anomaly_name(s, d), hp.call(d)])) State.say(s, "Combat at %s: %s (%d Cores, from %s%s) vs %s (%d Cores)." % [c.loc, State.anomaly_name(s, c.attacker), sides.atk, c.origin, hp.call(c.attacker), defs, sides.def], {"tag": "combat", "player": c.attacker.player, "loc": c.loc, "ref": c.attacker}) Query.emit(s, "onCombatStart", null) return if stage == "roll": # Static Core effects count as firing once per combat. var credited := {} var credit := func(card_name, p): var k := "%s:%d" % [card_name, p] if not credited.has(k): credited[k] = true State.fired(s, card_name, p) for r in State.fighters_of(c): Query.cores_of(s, Query.fighter_of(s, r), credit) _roll_all(s, c) var ties := " after %d tie reroll(s)" % c.ties if c.ties else "" State.say(s, "Roll: %s%s." % [_dice_text(s, c), ties], {"tag": "combat", "loc": c.loc}) return # result var t := combat_totals(s) if combat_watch.is_valid(): combat_watch.call(s, "result") if s.rules.health.on: _resolve_damage(s, c, t) return if t.atk == t.def: c.ties += 1 _roll_all(s, c) State.say(s, "Tie, so both sides reroll: %s." % _dice_text(s, c), {"tag": "combat", "loc": c.loc}) State.push_tasks(s, [ State._window("postRoll", c.attacker.player, s.stack.size()), {"t": "combat", "stage": "result"}, ]) return var attacker_wins: bool = t.atk > t.def var winner: int = c.attacker.player if attacker_wins else s.locations[c.loc].owner State.say(s, "%s wins the combat, %d to %d." % [State.player_name(winner), t.atk, t.def], {"tag": "combat", "player": winner, "loc": c.loc}) if attacker_wins: State.stat(s, "attacksWon", c.attacker.player) var tasks := [] for r in c.defenders: tasks.append({"t": "defeat", "ref": r, "loc": c.loc, "role": "defend"}) tasks.append({"t": "capture", "ref": c.attacker, "loc": c.loc, "stage": "hooks", "how": "capturesInCombat"}) State.push_tasks(s, tasks) # Without the health rule a lost roll destroys, so the damage hooks fire with the full Health. for r in c.defenders: _emit_damage(s, r, c.loc, "defend", Query.health_left(s, r), true) else: State.push_tasks(s, [{"t": "defeat", "ref": c.attacker, "loc": c.loc, "role": "attack"}]) _emit_damage(s, c.attacker, c.loc, "attack", Query.health_left(s, c.attacker), true) # --------------------------------------------------------------------------------------------- # Defeat static func defeat(s: Dictionary, r: Dictionary, loc: String, role: String) -> void: var av := State.anomaly(s, r) if av.at == null: return if not State.is_copy(r): # Redundant Power Supply, then RAID Array, may save the Hardware. var savers: Array = av.hardware.filter(func(hw): return Query.hardware_active(s, hw) and CardScripts.of(s.cards[hw].name).has("onWearerDefeated")) savers = Util.sorted_by(savers, func(hw): return CardScripts.of(s.cards[hw].name).get("savePriority", 0)) for hw in savers: if CardScripts.of(s.cards[hw].name).onWearerDefeated.call(s, hw, {"ref": r, "loc": loc, "role": role}): return knock_out(s, r, loc, role, false) # --------------------------------------------------------------------------------------------- # Capture static func capture_hooks(s: Dictionary, r: Dictionary, loc: String, how: String) -> void: var p: int = r.player if State.anomaly(s, r).at == null or State.controls(s, p, loc) or GameMap.is_start(loc): return State.push_tasks(s, [{"t": "capture", "ref": r, "loc": loc, "stage": "main", "how": how}]) var prev = s.locations[loc].owner if prev != null and prev != p: State.anomaly(s, r).breachedTurn = s.turn Query.emit(s, "onCaptured", {"ref": r, "loc": loc}) Query.emit(s, "onBreach", {"ref": r, "loc": loc}) static func capture_main(s: Dictionary, r: Dictionary, loc: String, how: String) -> void: var p: int = r.player var me: Dictionary = s.players[p] var L: Dictionary = s.locations[loc] if State.anomaly(s, r).at == null or State.controls(s, p, loc): return var previous = L.owner if L.server != null: State.reset_card(s, L.server) s.players[s.cards[L.server].owner].serverPile.append(L.server) if previous != null and not L.programs.is_empty(): State.stat(s, "programsLost", previous, L.programs.size()) for prog in L.programs: State.to_discard(s, prog) L.programs = [] L.owner = p L.server = me.serverPile.pop_front() if not me.serverPile.is_empty() else null State.anomaly(s, r).at = loc var level := GameMap.level_for(p, loc) var turn1 = s.rules.firstTurn if s.turn == 1 else null var bonus: int = 0 if L.server == null else CardScripts.of(s.cards[L.server].name).get("bandwidthOnPlace", 0) var no_capture_bandwidth: bool = turn1 != null and turn1.captureBandwidth == false var bandwidth: int = (0 if no_capture_bandwidth else s.rules.levelBandwidth[level]) + bonus State.gain_bandwidth(s, p, bandwidth, "bandwidthFromCaptures") State.stat(s, how, p) var from := "" if previous == null else " from %s" % State.player_name(previous) var server := "no Server" if L.server == null else State.name_of(s, L.server) State.say(s, "%s captures %s%s (Lv %d) with %s: +%d Gb (now %d)." % [State.player_name(p), loc, from, level, server, bandwidth, me.bandwidth], {"tag": "capture", "player": p, "loc": loc}) var unlocks: bool = not s.rules.recruit.on and not s.rules.buyAnomalies.on if unlocks and level > me.unlockedLevel and not (turn1 != null and turn1.unlock == false): me.unlockedLevel = level for i in 4: var av: Dictionary = me.anomalies[i] if i < level and not av.faceUp: av.faceUp = true av.unlockedTurn = s.turn State.say(s, "%s unlocks Anomaly %d: %s." % [State.player_name(p), i + 1, State.name_of(s, av.card)], {"tag": "capture", "player": p, "loc": loc}) if State.grid_count(s, p) >= s.rules.winLocations: s.over = true s.winner = p s.endReason = "win" State.say(s, "%s wins with %d locations." % [State.player_name(p), State.grid_count(s, p)], {"tag": "game", "player": p}) return if L.server != null: State.played(s, State.name_of(s, L.server), p) Query.emit(s, "onPlaced", {"card": L.server})