402 lines
16 KiB
GDScript
402 lines
16 KiB
GDScript
class_name Query
|
|
## Questions about the current state that depend on card effects (query.ts): what's in play, Cores,
|
|
## costs, capacity and protection.
|
|
##
|
|
## A Source (something with an effect in play) is {kind, id, name, controller, at} plus `ref` for
|
|
## Anomalies and Hardware and `virtual` for copied effects; see types.ts. A Fighter (an Anomaly as
|
|
## it fights, or would fight) is {ref, role, at, loc, combat}.
|
|
|
|
## Health when no value is printed and no override is set.
|
|
const FALLBACK_HEALTH := 10
|
|
|
|
|
|
static func program_active(s: Dictionary, id: int) -> bool:
|
|
var c: Dictionary = s.cards[id]
|
|
return not State.running(s, c.disabledUntil) and not State.running(s, c.ignoredUntil)
|
|
|
|
|
|
static func hardware_active(s: Dictionary, id: int) -> bool:
|
|
return not State.running(s, s.cards[id].disabledUntil)
|
|
|
|
|
|
## Recruit rule: `p` has fewer than 4 Anomalies, counting copies. Always true without the rule.
|
|
static func anomaly_room(s: Dictionary, p: int) -> bool:
|
|
if not s.rules.recruit.on:
|
|
return true
|
|
var avs: Array = s.players[p].anomalies
|
|
var n := 0
|
|
for i in avs.size():
|
|
if (i < 4 and avs[i].card >= 0) or (i >= 4 and avs[i].at != null):
|
|
n += 1
|
|
return n < 4
|
|
|
|
|
|
## Whether `p` may make a copy now: the copy-rule switches, and room under the recruit rule.
|
|
static func can_copy(s: Dictionary, p: int) -> bool:
|
|
var c: Dictionary = s.rules.copies
|
|
return c.allowed and (not c.needAnomaly2 or s.players[p].anomalies[1].faceUp) and anomaly_room(s, p)
|
|
|
|
|
|
## Everything with an effect in play right now.
|
|
static func sources(s: Dictionary) -> Array:
|
|
var out := []
|
|
for loc in GameMap.LOCATIONS:
|
|
var L: Dictionary = s.locations[loc]
|
|
if L.owner == null:
|
|
continue
|
|
if L.server != null:
|
|
var c: Dictionary = s.cards[L.server]
|
|
out.append({"kind": "server", "id": L.server, "name": c.name, "controller": L.owner, "at": loc})
|
|
if c.get("copies"):
|
|
out.append({"kind": "server", "id": L.server, "name": c.copies, "controller": L.owner, "at": loc, "virtual": -2})
|
|
var live: Array = L.programs.filter(func(id): return program_active(s, id))
|
|
for id in live:
|
|
out.append({"kind": "program", "id": id, "name": s.cards[id].name, "controller": L.owner, "at": loc})
|
|
for w in live:
|
|
var to = s.cards[w].get("wormTo") if s.cards[w].name == "Worm Protocol" else null
|
|
if not to:
|
|
continue
|
|
for id in live:
|
|
if id != w:
|
|
out.append({"kind": "program", "id": id, "name": s.cards[id].name, "controller": L.owner, "at": to, "virtual": -1})
|
|
for v in s.virtuals:
|
|
if State.running(s, v.until):
|
|
out.append({"kind": "program", "id": v.from, "name": v.name, "controller": v.controller, "at": v.at, "virtual": v.id})
|
|
for p in [0, 1]:
|
|
for r in State.refs_in_play(s, p):
|
|
if State.is_copy(r):
|
|
continue
|
|
var av := State.anomaly(s, r)
|
|
out.append({"kind": "anomaly", "id": av.card, "name": s.cards[av.card].name, "controller": p, "at": av.at, "ref": r})
|
|
for hw in av.hardware:
|
|
if hardware_active(s, hw):
|
|
out.append({"kind": "hardware", "id": hw, "name": s.cards[hw].name, "controller": p, "at": av.at, "ref": r})
|
|
return out
|
|
|
|
|
|
static func same_source(a: Dictionary, b: Dictionary) -> bool:
|
|
if a.id != b.id or a.name != b.name or a.at != b.at or a.get("virtual") != b.get("virtual"):
|
|
return false
|
|
var ra = a.get("ref")
|
|
var rb = b.get("ref")
|
|
return (ra == null and rb == null) or State.same_ref(ra, rb)
|
|
|
|
|
|
static func still_active(s: Dictionary, src: Dictionary) -> bool:
|
|
for x in sources(s):
|
|
if same_source(x, src):
|
|
return true
|
|
return false
|
|
|
|
|
|
## Active sources with this card name (including copies) applying at `loc`, or anywhere for null.
|
|
static func sources_named(s: Dictionary, card_name: String, loc: Variant = null) -> Array:
|
|
return sources(s).filter(func(x): return x.name == card_name and (loc == null or x.at == loc))
|
|
|
|
|
|
## Queues `hook` for every source whose script has it.
|
|
static func emit(s: Dictionary, hook: String, data: Variant) -> void:
|
|
var tasks := []
|
|
for src in sources(s):
|
|
if CardScripts.of(src.name).has(hook):
|
|
tasks.append({"t": "hook", "hook": hook, "src": src, "data": data})
|
|
State.push_tasks(s, tasks)
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Capacity
|
|
|
|
## How many of its owner's Anomalies a location holds (Cluster Server, Pingable Server: 2).
|
|
static func capacity(s: Dictionary, loc: String) -> int:
|
|
var sv = s.locations[loc].server
|
|
if sv == null or s.locations[loc].owner == null:
|
|
return 1
|
|
var c: Dictionary = s.cards[sv]
|
|
var copied: int = CardScripts.of(c.copies).get("capacity", 1) if c.get("copies") else 1
|
|
return maxi(1, maxi(CardScripts.of(c.name).get("capacity", 1), copied))
|
|
|
|
|
|
## Whether `p` can put another Anomaly on `loc`.
|
|
static func has_room(s: Dictionary, p: int, loc: String) -> bool:
|
|
if not State.controls(s, p, loc):
|
|
return false
|
|
# Nothing enters a location mid-combat (a copy, or an Anomaly moved by Disconnect).
|
|
if s.combat != null and not s.combat.ended and s.combat.loc == loc:
|
|
return false
|
|
var n := 0
|
|
for r in State.refs_in_play(s, p):
|
|
if State.anomaly(s, r).at == loc:
|
|
n += 1
|
|
return n < capacity(s, loc)
|
|
|
|
|
|
## Program slots on a location: 2, plus active Programs that add slots (Compatibility Layer).
|
|
static func program_slots(s: Dictionary, loc: String) -> int:
|
|
var n: int = s.rules.maxProgramsPerLocation
|
|
for id in s.locations[loc].programs:
|
|
if program_active(s, id):
|
|
n += CardScripts.of(s.cards[id].name).get("programSlots", 0)
|
|
return n
|
|
|
|
|
|
## Programs installed on a location, counting copies that count as installed (Process Clone).
|
|
static func installed_count(s: Dictionary, loc: String) -> int:
|
|
var n: int = s.locations[loc].programs.size()
|
|
for v in s.virtuals:
|
|
if v.installed and v.at == loc and State.running(s, v.until):
|
|
n += 1
|
|
return n
|
|
|
|
|
|
static func slots_used(s: Dictionary, r: Dictionary) -> int:
|
|
var n := 0
|
|
for id in State.anomaly(s, r).hardware:
|
|
if not CardScripts.of(s.cards[id].name).get("noSlot", false):
|
|
n += 1
|
|
return n
|
|
|
|
|
|
## Expansion Slots an Anomaly has: printed, plus Hardware that adds some (Network Interface Upgrade).
|
|
static func slots_total(s: Dictionary, r: Dictionary) -> int:
|
|
var av := State.anomaly(s, r)
|
|
var n: int = State.def_of(s, av.card).slots
|
|
for id in av.hardware:
|
|
n += CardScripts.of(s.cards[id].name).get("extraSlots", 0)
|
|
return n
|
|
|
|
|
|
static func slots_free(s: Dictionary, r: Dictionary) -> int:
|
|
return slots_total(s, r) - slots_used(s, r)
|
|
|
|
|
|
## Whether a Hardware takes an Expansion Slot: Network Interface Upgrade and Hardware pushed on over
|
|
## the limit don't.
|
|
static func takes_slot(s: Dictionary, id: int) -> bool:
|
|
return not CardScripts.of(s.cards[id].name).get("noSlot", false) and not s.cards[id].has("pushedUntil")
|
|
|
|
|
|
## Whether an Anomaly's Hardware fits its Expansion Slots, with `out` taken off and `into` put on.
|
|
static func slots_fit(s: Dictionary, r: Dictionary, out: Variant = null, into: Variant = null) -> bool:
|
|
var av := State.anomaly(s, r)
|
|
var hw: Array = av.hardware.filter(func(id): return id != out)
|
|
if into != null:
|
|
hw.append(into)
|
|
var total: int = State.def_of(s, av.card).slots
|
|
var taken := 0
|
|
for id in hw:
|
|
total += CardScripts.of(s.cards[id].name).get("extraSlots", 0)
|
|
if takes_slot(s, id):
|
|
taken += 1
|
|
return taken <= total
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Cores
|
|
|
|
## Where an Anomaly would fight from, as a fighter, with the current combat if it's in it.
|
|
static func fighter_of(s: Dictionary, r: Dictionary) -> Dictionary:
|
|
var c = s.combat
|
|
var av := State.anomaly(s, r)
|
|
if c != null and not c.ended:
|
|
if State.same_ref(c.attacker, r):
|
|
return {"ref": r, "role": "attack", "at": c.origin, "loc": c.loc, "combat": c}
|
|
for d in c.defenders:
|
|
if State.same_ref(d, r):
|
|
return {"ref": r, "role": "defend", "at": c.loc, "loc": c.loc, "combat": c}
|
|
return {"ref": r, "role": null, "at": av.at, "loc": null, "combat": null}
|
|
|
|
|
|
## Hardware on an Anomaly (a copy uses its original's) whose script passes `flag`, if active.
|
|
static func wears(s: Dictionary, r: Dictionary, flag: Callable) -> Array:
|
|
return State.hardware_of(s, r).filter(func(hw):
|
|
return hardware_active(s, hw) and flag.call(CardScripts.of(s.cards[hw].name)))
|
|
|
|
|
|
## Opponents' Bug effects can't touch an Anomaly wearing ECC Memory.
|
|
static func bug_immune(s: Dictionary, r: Dictionary, by: int) -> bool:
|
|
return by != r.player and not wears(s, r, func(sc): return sc.get("bugImmunity", false)).is_empty()
|
|
|
|
|
|
## Active sources with a script flag at a location, from a player other than `p`.
|
|
static func enemy_flag_at(s: Dictionary, loc: String, p: int, flag: Callable) -> Array:
|
|
return sources(s).filter(func(x): return x.at == loc and x.controller != p and flag.call(CardScripts.of(x.name)))
|
|
|
|
|
|
## Stock Rice: no Hardware bonuses in a combat at its location.
|
|
static func _hardware_blocked(s: Dictionary, f: Dictionary, all: Array) -> bool:
|
|
if not f.loc or f.role == null:
|
|
return false
|
|
for x in all:
|
|
if x.at == f.loc and CardScripts.of(x.name).get("blocksHardwareInCombat", false) and not bug_immune(s, f.ref, x.controller):
|
|
return true
|
|
return false
|
|
|
|
|
|
## Desynchronization: an attacker can't use once-per-turn Hardware abilities in a combat there.
|
|
static func desynced(s: Dictionary, r: Dictionary) -> bool:
|
|
var c = s.combat
|
|
if c == null or c.ended or not State.same_ref(c.attacker, r):
|
|
return false
|
|
return not enemy_flag_at(s, c.loc, r.player, func(sc): return sc.get("blocksHardwareAbilities", false)).is_empty()
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Health (health rule)
|
|
|
|
## Health rule: the damage that destroys an Anomaly. Printed Health (or the rules' override), plus
|
|
## active Hardware that adds Health. A copy has its original's Health.
|
|
static func health_of(s: Dictionary, r: Dictionary) -> int:
|
|
var av := State.anomaly(s, r)
|
|
var n: int = s.rules.health.override
|
|
if n == 0:
|
|
n = 0 if av.card < 0 else State.def_of(s, av.card).health
|
|
if n == 0:
|
|
n = FALLBACK_HEALTH
|
|
for hw in State.hardware_of(s, r):
|
|
if hardware_active(s, hw):
|
|
n += CardScripts.of(s.cards[hw].name).get("health", 0)
|
|
return maxi(1, n)
|
|
|
|
|
|
## Health rule: Health an Anomaly has left.
|
|
static func health_left(s: Dictionary, r: Dictionary) -> int:
|
|
return health_of(s, r) - State.anomaly(s, r).damage
|
|
|
|
|
|
## Combat Cores for a fighter. `credit` is called with (name, player) for every card that changed the
|
|
## total, so the card report can count static effects (Stack Overflow, Server bonuses) as firing.
|
|
## `detail`, when given, collects every part of the total as {source, kind, controller, cores}, the
|
|
## printed Cores first. The total is at least 1.
|
|
static func cores_of(s: Dictionary, f: Dictionary, credit := Callable(), detail: Variant = null) -> int:
|
|
var av := State.anomaly(s, f.ref)
|
|
var d: Dictionary = State.def_of(s, av.card)
|
|
var n: int = d.cores
|
|
if detail != null:
|
|
detail.append({"source": d.name, "kind": "anomaly", "controller": f.ref.player, "cores": n})
|
|
var all := sources(s)
|
|
if not _hardware_blocked(s, f, all):
|
|
for hw in State.hardware_of(s, f.ref):
|
|
if not hardware_active(s, hw) or (f.combat != null and f.combat.suppressed.has(hw)):
|
|
continue
|
|
var hw_name: String = s.cards[hw].name
|
|
var sc := CardScripts.of(hw_name)
|
|
if sc.has("cores"):
|
|
var src := {"kind": "hardware", "id": hw, "name": hw_name, "controller": f.ref.player, "at": f.at, "ref": f.ref}
|
|
n += _part(sc.cores.call(s, src, f), hw_name, f.ref.player, "hardware", credit, detail)
|
|
var counted := {}
|
|
for src in all:
|
|
if src.kind == "hardware":
|
|
continue
|
|
var sc := CardScripts.of(src.name)
|
|
if sc.has("cores"):
|
|
n += _part(sc.cores.call(s, src, f), src.name, src.controller, src.kind, credit, detail)
|
|
if sc.has("globalCores") and not counted.has(src.name):
|
|
counted[src.name] = true
|
|
n += _part(sc.globalCores.call(s, f), src.name, src.controller, src.kind, credit, detail)
|
|
for m in State.active_mods(s, av):
|
|
n += m.cores
|
|
if m.cores != 0 and detail != null:
|
|
detail.append({"source": m.source, "kind": "modifier", "controller": m.by, "cores": m.cores})
|
|
return maxi(1, n)
|
|
|
|
|
|
## One part of a Core total: reports it to `credit` and `detail`, and returns it.
|
|
static func _part(delta: int, src_name: String, p: int, kind: String, credit: Callable, detail: Variant) -> int:
|
|
if delta != 0:
|
|
if credit.is_valid():
|
|
credit.call(src_name, p)
|
|
if detail != null:
|
|
detail.append({"source": src_name, "kind": kind, "controller": p, "cores": delta})
|
|
return delta
|
|
|
|
|
|
## Cores for each side of the current combat: {atk, def}.
|
|
static func side_cores(s: Dictionary) -> Dictionary:
|
|
var c: Dictionary = s.combat
|
|
var def_total := 0
|
|
for d in c.defenders:
|
|
def_total += cores_of(s, fighter_of(s, d))
|
|
return {"atk": cores_of(s, fighter_of(s, c.attacker)), "def": def_total}
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Costs
|
|
#
|
|
# A Payment is {kind, player} plus what it's for: card, at (a Program's location or an Execution's
|
|
# target), ref (a Hardware's wearer), line and level (a Firewall), hostile (enemy Anomalies an
|
|
# Execution targets). kind is "program", "hardware", "execution", "firewall" or "ability".
|
|
|
|
static func _base_cost(s: Dictionary, payment: Dictionary) -> int:
|
|
if payment.kind == "firewall":
|
|
return State.firewall_cost(s, payment.get("level", 0))
|
|
return 0 if payment.get("card") == null else State.def_of(s, payment.card).bandwidth
|
|
|
|
|
|
## Bandwidth a payment costs after every discount and surcharge in play. Pass `all` (the current
|
|
## sources) when pricing many payments against the same state.
|
|
static func cost_of(s: Dictionary, payment: Dictionary, all: Variant = null) -> int:
|
|
var n := _base_cost(s, payment)
|
|
for src in (all if all != null else sources(s)):
|
|
var sc := CardScripts.of(src.name)
|
|
if sc.has("costMod"):
|
|
n += sc.costMod.call(s, src, payment)
|
|
return maxi(0, n)
|
|
|
|
|
|
## Pays for something, using up once-per-turn discounts. Returns the amount paid.
|
|
static func pay(s: Dictionary, payment: Dictionary, stat_key: String) -> int:
|
|
var n := _base_cost(s, payment)
|
|
var touched := []
|
|
for src in sources(s):
|
|
var sc := CardScripts.of(src.name)
|
|
var d: int = sc.costMod.call(s, src, payment) if sc.has("costMod") else 0
|
|
if d != 0:
|
|
n += d
|
|
touched.append(src)
|
|
var cost := maxi(0, n)
|
|
if cost > s.players[payment.player].bandwidth:
|
|
Util.fail("Can't afford %d Gb" % cost)
|
|
State.add_bandwidth(s, payment.player, -cost)
|
|
State.stat(s, stat_key, payment.player, cost)
|
|
for src in touched:
|
|
var sc := CardScripts.of(src.name)
|
|
if sc.has("onPaid"):
|
|
sc.onPaid.call(s, src, payment)
|
|
return cost
|
|
|
|
|
|
## Bandwidth a player could have for a payment: current Bandwidth plus the Salvage value of the rest
|
|
## of the hand.
|
|
static func reachable_bandwidth(s: Dictionary, p: int, except: Variant = null) -> int:
|
|
var pl: Dictionary = s.players[p]
|
|
var n: int = pl.bandwidth
|
|
for id in pl.hand:
|
|
if id != except and id >= 0:
|
|
n += State.def_of(s, id).bandwidth
|
|
return n
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Protection
|
|
|
|
## Hardware Sandbox: opponents can't target the Program it protects (Execute: Root Access lifts it).
|
|
static func is_protected(s: Dictionary, program: int, by: int) -> bool:
|
|
var owner: int = s.cards[program].owner
|
|
if by == owner:
|
|
return false
|
|
var loc = null
|
|
for l in GameMap.LOCATIONS:
|
|
if s.locations[l].programs.has(program):
|
|
loc = l
|
|
break
|
|
if loc != null and State.running(s, s.locations[loc].rootAccess[by]):
|
|
return false
|
|
for x in sources(s):
|
|
if x.kind == "hardware" and x.name == "Hardware Sandbox" and s.cards[x.id].get("protects") == program \
|
|
and loc != null and x.at == loc and x.controller == owner:
|
|
return true
|
|
return false
|
|
|
|
|
|
static func can_target_program(s: Dictionary, program: int, by: int) -> bool:
|
|
return not is_protected(s, program, by)
|