class_name CardUtil ## Helpers shared by card scripts (cards/util.ts). ## Locations where `p` has Ghost Process working, as a Dictionary used as a set. static func ghosts_of(s: Dictionary, p: int) -> Dictionary: var out := {} for x in Query.sources(s): if x.name == "Ghost Process" and x.controller == p: out[x.at] = true return out ## Locations an Execution played on `target` can reach: Ghost Process adds one adjacent location. ## Pass `ghosts` (from ghosts_of) when calling it for many targets. static func scope_of(s: Dictionary, p: int, target: String, ghosts: Variant = null) -> Array: if ghosts == null: ghosts = ghosts_of(s, p) return [target] + State.neighbors(s, target) if ghosts.has(target) else [target] static func mine(s: Dictionary, p: int, loc: String) -> Array: return State.at_server(s, loc).filter(func(r): return r.player == p) ## Active, installed Programs on a location. static func programs_on(s: Dictionary, loc: String) -> Array: return s.locations[loc].programs.filter(func(id): return Query.program_active(s, id)) static func active_hardware(s: Dictionary, ids: Array) -> Array: return ids.filter(func(id): return Query.hardware_active(s, id)) ## Places Bug counters: like Infection counters, for Bug cards. No card places them yet. ECC Memory ## stops opponents from placing them. static func add_bugs(s: Dictionary, r: Dictionary, n: int, source: String, by: int) -> bool: if Query.bug_immune(s, r, by): State.say(s, "ECC Memory blocks %s's Bug counters on %s." % [source, State.anomaly_name(s, r)]) State.fired(s, "ECC Memory", r.player) return false State.anomaly(s, r).bugs += n State.fired(s, source, by) State.say(s, "%s: %s gets %d Bug counter%s (%d total)." % [source, State.anomaly_name(s, r), n, "" if n == 1 else "s", State.anomaly(s, r).bugs]) return true static func infect(s: Dictionary, r: Dictionary, n: int, source: String, by: int, permanent := false) -> void: var av := State.anomaly(s, r) if permanent: av.permanentInfection += n else: av.infection += n State.fired(s, source, by) State.say(s, "%s: %s gets %s %sInfection counter%s (%d total)." % [source, State.anomaly_name(s, r), "an" if n == 1 else str(n), "permanent " if permanent else "", "" if n == 1 else "s", av.infection + av.permanentInfection]) ## One option per distinct card name. static func card_options(s: Dictionary, ids: Array) -> Array: var seen := {} var out := [] for id in ids: var nm := State.name_of(s, id) if seen.has(nm): continue seen[nm] = true out.append({"label": nm, "value": id}) return out static func is_virus(s: Dictionary, id: int) -> bool: var d := State.def_of(s, id) return d.kind == "program" and d.type == "V" static func is_patch(s: Dictionary, id: int) -> bool: var d := State.def_of(s, id) return d.kind == "program" and d.type == "P" static func whose(p: int) -> String: return State.player_name(p) ## Distinct non-empty subsets of up to 3 items. static func subsets(items: Array) -> Array: var out := [] var n := mini(items.size(), 3) for mask in range(1, 1 << n): var sub := [] for i in n: if mask & (1 << i): sub.append(items[i]) out.append(sub) return out static func src_label(s: Dictionary, src: Dictionary) -> String: if src.kind == "anomaly": var d := State.def_of(s, src.id) return "%s (%s)" % [Util.js(d.ability.name if d.has("ability") else null), src.name] return src.name static func every_anomaly_in_play(s: Dictionary) -> Array: return State.refs_in_play(s, 0) + State.refs_in_play(s, 1)