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

97 lines
4.2 KiB
GDScript

class_name Invariants
## Rule invariants (invariants.ts): checks a state is consistent. Used by tests and the
## conformance runner's --check.
## Rule violations in a state; empty when it's consistent.
static func check(s: Dictionary) -> Array:
var errors := []
var zone := {}
var put := func(id: int, where: String) -> void:
if zone.has(id):
var nm = s.cards[id].name if id >= 0 and id < s.cards.size() else null
errors.append("card %d (%s) is in both %s and %s" % [id, Util.js(nm), zone[id], where])
zone[id] = where
for p in s.players.size():
var pl: Dictionary = s.players[p]
var own := func(ids: Array, where: String) -> void:
for id in ids:
put.call(id, "p%d.%s" % [p, where])
if s.cards[id].owner != p:
errors.append("p%d.%s holds the opponent's card %d" % [p, where, id])
own.call(pl.hand, "hand")
own.call(pl.deck, "deck")
own.call(pl.discard, "discard")
own.call(pl.serverPile, "serverPile")
if pl.bandwidth < 0:
errors.append("p%d has %d Gb" % [p, pl.bandwidth])
var count := {}
for i in pl.anomalies.size():
var av: Dictionary = pl.anomalies[i]
var slot: int = i + 1
var r := State.ref(p, slot)
if slot <= 4:
if av.card >= 0 or not s.rules.recruit.on:
own.call([av.card], "anomaly%d" % slot)
elif av.at != null or not av.hardware.is_empty():
errors.append("p%d empty slot %d is in use" % [p, slot])
own.call(av.hardware, "anomaly%d.hardware" % slot)
var ported: Array = av.hardware.filter(func(h):
return not CardScripts.of(s.cards[h].name).get("noSlot", false) and not s.cards[h].has("pushedUntil"))
if av.card >= 0 and ported.size() > Query.slots_total(s, r):
errors.append("p%d Anomaly %d is over its slots" % [p, slot])
else:
if not av.hardware.is_empty():
errors.append("p%d copy %d has Hardware of its own" % [p, slot - 4])
if av.at != null and pl.anomalies[i - 4].at == null:
errors.append("p%d copy %d is in play without its original" % [p, slot - 4])
if av.card != pl.anomalies[i - 4].card:
errors.append("p%d copy %d doesn't match its original" % [p, slot - 4])
if av.damage < 0:
errors.append("p%d Anomaly %d has negative damage" % [p, slot])
if av.at == null:
if av.damage != 0:
errors.append("p%d Anomaly %d is in its slot with %d damage" % [p, slot, av.damage])
continue
if not av.faceUp:
errors.append("p%d Anomaly %d is in play face down" % [p, slot])
# A destroyed Anomaly may still stand there while its defeat is being resolved (RAID Array's choice).
var dying: bool = s.choice != null
for t in s.tasks:
if t.t == "defeat" and t.ref.player == p and t.ref.slot == slot:
dying = true
if s.rules.health.on and not dying and av.damage >= Query.health_of(s, r):
errors.append("p%d Anomaly %d is in play with no Health left" % [p, slot])
if not State.controls(s, p, av.at):
errors.append("p%d Anomaly %d is on %s, which it doesn't control" % [p, slot, av.at])
count[av.at] = count.get(av.at, 0) + 1
for loc in count:
if count[loc] > Query.capacity(s, loc):
errors.append("p%d has %d Anomalies on %s" % [p, count[loc], loc])
var total := 0
for i in pl.anomalies.size():
if (i < 4 and pl.anomalies[i].card >= 0) or (i >= 4 and pl.anomalies[i].at != null):
total += 1
if s.rules.recruit.on and total > 4:
errors.append("p%d has %d Anomalies, counting copies" % [p, total])
for loc in GameMap.LOCATIONS:
var L: Dictionary = s.locations[loc]
if L.server != null:
put.call(L.server, "%s.server" % loc)
for id in L.programs:
put.call(id, "%s.programs" % loc)
if L.owner == null and (L.server != null or not L.programs.is_empty()):
errors.append("%s is unowned but has cards" % loc)
var layers: int = L.programs.filter(func(id): return s.cards[id].name == "Compatibility Layer").size()
if L.programs.size() > s.rules.maxProgramsPerLocation + layers:
errors.append("%s has too many Programs" % loc)
for item in s.stack:
if item.kind == "execution":
put.call(item.card, "stack")
if zone.size() != s.cards.size():
errors.append("%d cards are missing from every zone" % (s.cards.size() - zone.size()))
if s.choice != null and s.choice.options.is_empty():
errors.append("choice %s has no options" % s.choice.kind)
return errors