files-game/engine/scripts.gd
2026-10-01 17:22:18 -04:00

71 lines
3.1 KiB
GDScript3

class_name CardScripts
## Card scripts: what each card does, in one place per card (scripts.ts). The engine calls into them
## at fixed points (hooks). Every script records the card text it was written for, so an edited card
## in cards.csv is reported instead of silently simulated with the old wording.
##
## A script is a Dictionary keyed like the TS Script interface; every key but `text` is optional.
## Values are numbers and flags (bandwidthOnPlace, capacity, programSlots, noSlot, extraSlots, health,
## extraAttacks, blocksHardwareInCombat, blocksHardwareAbilities, blocksAttackerExecutions,
## bugImmunity, executionPower, copiesExecutions, savePriority) or Callables, always called with the
## arguments listed here:
##
## cores(s, src, fighter) -> int globalCores(s, fighter) -> int
## costMod(s, src, payment) -> int onPaid(s, src, payment)
## <hook>(s, src, data) for each hook in HOOKS; data is null where TS passes none
## onWearerDefeated(s, hw, ev) -> bool negates(s, src, item) -> bool
## ability: {cost?, hardwareOncePerTurn?, uses(s, src) -> Array of args,
## resolve(s, item, src), extraCost?(s, src, args) -> int}
## execution: {plays(s, p) -> Array of {target, args}, resolve(s, item),
## targetsAnomalies?(s, item) -> Array of refs}
## resume: {fn name: Callable(s, data, value)}, continuations after a choice or a delayed task
const HOOKS: Array[String] = [
"onTurnStart", "onDraw", "onAttackDeclared", "onBreach", "onCaptured", "onPlaced", "onInstall",
"onCombatStart", "onDefeat", "onDamage", "onCombatEnd",
]
## Returned for cards with no script, so lookups don't need a null check.
const NONE := {}
static var _registry: Dictionary = {}
## All scripts by card name. Built on first use.
static func all() -> Dictionary:
if _registry.is_empty():
for group in [ServerCards.scripts(), ProgramCards.scripts(), HardwareCards.scripts(),
ExecutionCards.scripts(), AnomalyCards.scripts()]:
_registry.merge(group, true)
return _registry
## The script for a card name, or an empty Dictionary.
static func of(card_name: Variant) -> Dictionary:
return all().get(card_name, NONE)
static func is_implemented(card: Dictionary) -> bool:
return all().has(card.name)
## The text a card's script checks: the Ability for Anomalies, the description for everything else.
static func scripted_text(card: Dictionary) -> String:
if card.kind == "anomaly":
return card.ability.text.strip_edges() if card.has("ability") else ""
return card.text.strip_edges()
## Scripts whose card is missing from, or worded differently in, the CSV; and cards with no script.
static func stale_scripts(pool: Dictionary) -> Array:
var warnings := []
var scripts := all()
for card_name in scripts:
var card = pool.by_name.get(card_name)
if card == null:
warnings.append("%s: has a script but isn't in cards.csv" % card_name)
elif scripted_text(card) != scripts[card_name].text:
warnings.append("%s: card text changed; the script was written for \"%s\"" % [card_name, scripts[card_name].text])
for card in pool.all:
if not is_implemented(card):
warnings.append("%s: no script yet" % card.name)
return warnings