This commit is contained in:
Forrest 2026-10-01 17:22:18 -04:00
commit 0cba7f5ee0
385 changed files with 17912 additions and 0 deletions

109
engine/rules.gd Normal file
View file

@ -0,0 +1,109 @@
class_name Rules
## Every number from rules.md that a rule variant might change (rules.ts). A rules Dictionary is
## shaped like the TS RulesConfig; the field docs are there. Pass a modified copy to
## RulesEngine.create_game to A/B test a rule.
## The current rules, as a fresh Dictionary.
static func defaults() -> Dictionary:
return {
"board": "classic",
"startingBandwidth": 4,
"openingHand": 7,
"drawPerTurn": 1,
"winLocations": 10,
"maxTurns": 200,
"levelBandwidth": {1: 4, 2: 4, 3: 4, 4: 4, 5: 4},
"firewallCost": [2, 4, 8],
"maxProgramsPerLocation": 2,
"deck": {"anomalies": 4, "servers": 20, "main": 45, "maxCopies": 2},
"startingAnomalies": 1,
"firstTurn": {"draw": true, "advance": true, "captureBandwidth": true, "unlock": true, "cards": true},
"secondPlayer": {"bandwidth": 0, "anomalies": 0, "cards": 0, "firstDraw": 0, "locations": 0},
"recruit": {"on": false, "anomaliesInDeck": 3, "price": 6, "pricePerCore": 0},
"buyAnomalies": {"on": false, "prices": [8, 12, 16]},
"copies": {"allowed": true, "waitATurn": false, "needAnomaly2": false},
"commands": {"on": true, "perPlayer": 2, "leads": "F", "snake": true},
"health": {"on": true, "override": 0, "recall": true},
}
## Rules read from JSON (a trace, a game record): numbers back to ints, and levelBandwidth keyed by
## int level.
static func from_json(d: Dictionary) -> Dictionary:
var r: Dictionary = Util.ints(d)
var lb := {}
for k in r.levelBandwidth:
lb[int(k)] = r.levelBandwidth[k]
r.levelBandwidth = lb
return r
## Anomalies kept outside the main deck: all of them, or just the starting one when recruiting.
static func anomalies_outside(rules: Dictionary) -> int:
return 1 if rules.recruit.on else rules.deck.anomalies
## A copy of `rules` with one setting changed, from text like "winLocations=9",
## "secondPlayer.bandwidth=4", "firstTurn.advance=false" or "levelBandwidth=4,4,4,4,4" (Lv 1 to 5).
## "board=grid4" also sets winLocations to 12, so give a winLocations after it to change that.
## Returns null (and records an error) for a bad spec.
static func with_rule(rules: Dictionary, spec: String) -> Variant:
var m := RegEx.create_from_string("^([\\w.]+)=(.+)$").search(spec)
if m == null:
return Util.fail("Rule \"%s\" should look like name=value" % spec)
var key := m.get_string(1)
var raw := m.get_string(2)
var next := rules.duplicate(true)
var path := key.split(".")
var obj: Dictionary = next
for part in path.slice(0, -1):
if not (obj.get(part) is Dictionary):
return Util.fail("Unknown rule \"%s\"" % key)
obj = obj[part]
var last := path[path.size() - 1]
var old = obj.get(last)
if key == "board":
if not GameMap.BOARDS.has(raw):
return Util.fail("Unknown board \"%s\". Boards: %s" % [raw, ", ".join(GameMap.BOARDS.keys())])
obj.board = raw
obj.winLocations = GameMap.BOARDS[raw].win
elif old is int or old is float:
var n = _num(key, raw)
if n == null:
return null
obj[last] = n
elif old is String:
obj[last] = raw
elif old is bool:
if raw != "true" and raw != "false":
return Util.fail("Rule \"%s\" needs true or false" % key)
obj[last] = raw == "true"
elif key == "levelBandwidth":
var lb := {}
var parts := raw.split(",")
for i in parts.size():
var n = _num(key, parts[i])
if n == null:
return null
lb[i + 1] = n
obj[last] = lb
elif old is Array:
var out := []
for x in raw.split(","):
var n = _num(key, x)
if n == null:
return null
out.append(n)
obj[last] = out
else:
return Util.fail("Unknown rule \"%s\"" % key)
return next
static func _num(key: String, x: String) -> Variant:
if x.is_valid_int():
return int(x)
if x.is_valid_float():
return float(x)
return Util.fail("Rule \"%s\" needs numbers, got \"%s\"" % [key, x])