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

318
engine/bots/eval.gd Normal file
View file

@ -0,0 +1,318 @@
class_name BotEval
## Scoring helpers the greedy bot shares (bots/eval.ts): board values, combat odds and what a combat
## is worth. Everything reads a player's view and changes nothing.
# A location is worth about 10 points, plus 8 more when it's taken from the opponent. Bandwidth counts
# half a point per Bandwidth, a card in hand about 2.
const LOCATION_POINTS := 10.0
const ENEMY_LOCATION_POINTS := 8.0
const BANDWIDTH_POINTS := 0.5
const LOSS_POINTS := 4.0
const CARD_POINTS := 2.0
## Hardware's rough combat Cores, by name.
const HARDWARE_CORES := {"Additional Core": 1, "Overclocked CPU": 2, "Packet Accelerator": 1}
static func location_value(v: Dictionary, p: int, loc: String) -> float:
if GameMap.is_start(loc):
return 0.0 # starting Servers can't be captured
var taken := 0.0 if v.locations[loc].owner == null else ENEMY_LOCATION_POINTS
return LOCATION_POINTS + taken + v.rules.levelBandwidth[GameMap.level_for(p, loc)] * BANDWIDTH_POINTS
## How many Anomalies capturing `loc` would unlock for `p` (level unlocks only).
static func unlocks_at(v: Dictionary, p: int, loc: String) -> int:
if v.rules.recruit.on or v.rules.buyAnomalies.on or GameMap.is_start(loc):
return 0
var level := GameMap.level_for(p, loc)
if level <= v.players[p].unlockedLevel:
return 0
var n := 0
for av in v.players[p].anomalies.slice(0, mini(level, 4)):
if not av.faceUp:
n += 1
return n
## What losing an Anomaly costs: the tempo, plus its Hardware.
static func anomaly_stake(v: Dictionary, r: Dictionary) -> float:
var bw := 0
for h in State.hardware_of(v, r):
bw += 0 if h < 0 else State.def_of(v, h).bandwidth
return LOSS_POINTS + bw * BANDWIDTH_POINTS
# ---------------------------------------------------------------------------------------------
# Dice
## Distributions of the sum of k d6, by k: the probability of each total, as an Array by total.
## A static cache, filled on the main thread's first use; see diceSum in eval.ts.
static var _sum_dist: Array = [[1.0]]
static func dice_sum(k: int) -> Array:
while _sum_dist.size() <= k:
var prev: Array = _sum_dist[_sum_dist.size() - 1]
var next := []
next.resize(prev.size() + 6)
next.fill(0.0)
for s in prev.size():
var p: float = prev[s]
for f in range(1, 7):
next[s + f] += p / 6
_sum_dist.append(next)
return _sum_dist[k]
## P(a > b) where a = fa + ka d6 and b = fb + kb d6, ties rerolled at `tie` odds.
static func _beats(fa: int, ka: int, fb: int, kb: int, tie: float) -> float:
var da := dice_sum(ka)
var db := dice_sum(kb)
var win := 0.0
var draw := 0.0
for a in da.size():
var pa: float = da[a]
if pa == 0:
continue
for b in db.size():
var pb: float = db[b]
if pb == 0:
continue
var x := fa + a
var y := fb + b
if x > y:
win += pa * pb
elif x == y:
draw += pa * pb
return win + draw * tie
## Probability of each margin (a − b) where a = fa + ka d6 and b = fb + kb d6, in TS Map order.
static func margin_dist(fa: int, ka: int, fb: int, kb: int) -> Dictionary:
var da := dice_sum(ka)
var db := dice_sum(kb)
var out := {}
for a in da.size():
var pa: float = da[a]
if pa == 0:
continue
for b in db.size():
var pb: float = db[b]
if pb == 0:
continue
var m := fa + a - fb - b
out[m] = out.get(m, 0.0) + pa * pb
return out
## Averages margin distributions, for a combat whose bonus is decided by a coin flip (Race Condition).
static func _mix_dist(dists: Array) -> Dictionary:
var out := {}
for d in dists:
for m in d:
out[m] = out.get(m, 0.0) + d[m] / dists.size()
return out
## The dice each side of the current combat still has to roll (or has rolled), for the odds.
## `reroll` is null or {ref, k}.
static func _combat_dice(v: Dictionary, reroll: Variant) -> Dictionary:
var c: Dictionary = v.combat
var fa := 0
var ka := 0
var fd := 0
var kd := 0
for r in State.fighters_of(c):
var want := Query.cores_of(v, Query.fighter_of(v, r))
var dice := []
if c.rolled:
dice = c.dice.get(State.ref_key(r), []).duplicate()
dice.sort()
if dice.size() > want:
dice = dice.slice(0, want)
var extra := want - dice.size()
if reroll != null and State.ref_key(reroll.ref) == State.ref_key(r):
extra += dice.filter(func(d): return d <= reroll.k).size()
dice = dice.filter(func(d): return d > reroll.k)
var fixed := Util.sum(dice)
if r == c.attacker:
fa += fixed
ka += extra
else:
fd += fixed
kd += extra
return {"fa": fa, "ka": ka, "fd": fd, "kd": kd}
## Chance the attacker wins the current combat. Before the roll that's the Core odds. After it, the
## dice on the table count: Core changes add dice or drop the highest ones, and `reroll` lets a
## fighter reroll its dice showing k or less (Bit Flip).
static func combat_odds(v: Dictionary, reroll: Variant = null) -> float:
var c: Dictionary = v.combat
var atk := 0
var def := 0
for r in State.fighters_of(c):
var n := Query.cores_of(v, Query.fighter_of(v, r))
if r == c.attacker:
atk += n
else:
def += n
var tie := Odds.win_chance(atk, def)
if not c.rolled:
return tie
var d := _combat_dice(v, reroll)
return _beats(d.fa, d.ka, d.fd, d.kd, tie)
## Health rule: the margin distribution of the current combat, given the dice on the table.
static func combat_dist(v: Dictionary, reroll: Variant = null) -> Dictionary:
var d := _combat_dice(v, reroll)
return margin_dist(d.fa, d.ka, d.fd, d.kd)
## Cores each side would have if `r` attacked `target` from `origin` now, counting what the target's
## Programs do when a combat starts there (Bloatware, Visual Artifact, Memory Parasite), and whether
## Race Condition may add +2 to the defence. Returns {atk, def, race}.
static func _attack_cores(v: Dictionary, r: Dictionary, origin: String, target: String, bonus: int) -> Dictionary:
var atk := Query.cores_of(v, {"ref": r, "role": "attack", "at": origin, "loc": target, "combat": null}) + bonus
var def := 0
for d in State.defenders_at(v, target):
def += Query.cores_of(v, {"ref": d, "role": "defend", "at": target, "loc": target, "combat": null})
var race := false
for x in Query.sources(v):
if x.at != target or x.controller == r.player:
continue
var hw := State.hardware_of(v, r)
if x.name == "Bloatware" and not Query.bug_immune(v, r, x.controller):
atk -= hw.size()
if x.name == "Visual Artifact" and not v.cards[x.id].get("spent", false):
atk -= 1
if x.name == "Race Condition":
race = true
if x.name == "Memory Parasite":
var bonuses := []
for h in hw:
if Query.hardware_active(v, h):
bonuses.append(hardware_cores(v, h))
if not bonuses.is_empty():
atk -= bonuses.min()
return {"atk": maxi(1, atk), "def": maxi(1, def), "race": race}
## Chance an attack would win the roll if declared now.
static func attack_odds(v: Dictionary, r: Dictionary, origin: String, target: String, bonus := 0) -> float:
var c := _attack_cores(v, r, origin, target, bonus)
if c.race:
return (Odds.win_chance(c.atk, c.def) + Odds.win_chance(c.atk, c.def + 2)) / 2
return Odds.win_chance(c.atk, c.def)
## Health rule: margin distribution of an attack declared now.
static func attack_dist(v: Dictionary, r: Dictionary, origin: String, target: String, bonus := 0) -> Dictionary:
var c := _attack_cores(v, r, origin, target, bonus)
var plain := margin_dist(0, c.atk, 0, c.def)
return _mix_dist([plain, margin_dist(0, c.atk, 0, c.def + 2)]) if c.race else plain
## Health rule: expected points for `me` from a combat with the given margin distribution.
## Destroying every defender wins the location and their stakes. Damage short of that is worth a
## share of those stakes plus half the location, since the defender can still recall and heal.
## Damage to the attacker is worth the same share of its own stake.
static func damage_value(v: Dictionary, me: int, attacker: Dictionary, defenders: Array, dist: Dictionary, loc_value: float) -> float:
var left_d := 0
var stake_d := 0.0
for d in defenders:
left_d += Query.health_left(v, d)
stake_d += anomaly_stake(v, d)
var sum_left := maxi(1, left_d)
var stake_a := anomaly_stake(v, attacker)
var left_a := maxi(1, Query.health_left(v, attacker))
var total := 0.0
for m in dist:
var p: float = dist[m]
if m > 0:
total += p * (loc_value + stake_d if m >= sum_left else (m * (stake_d + loc_value / 2)) / sum_left)
elif m < 0:
total -= p * (stake_a if -m >= left_a else (-m * stake_a) / left_a)
return total if attacker.player == me else -total
## Points for the attacking player from declaring an attack now: under the health rule the expected
## damage value, otherwise the odds of taking the location against losing the Anomaly.
static func attack_value(v: Dictionary, r: Dictionary, origin: String, target: String, loc_value: float, bonus := 0) -> float:
if not v.rules.health.on:
var chance := attack_odds(v, r, origin, target, bonus)
return chance * loc_value - (1 - chance) * anomaly_stake(v, r)
return damage_value(v, r.player, r, State.defenders_at(v, target), attack_dist(v, r, origin, target, bonus), loc_value)
## Chance `target` falls to this attack: the win odds, or under the health rule the odds of
## destroying every defender.
static func fall_chance(v: Dictionary, r: Dictionary, origin: String, target: String, bonus := 0) -> float:
if not v.rules.health.on:
return attack_odds(v, r, origin, target, bonus)
var need := 0
for d in State.defenders_at(v, target):
need += Query.health_left(v, d)
var p := 0.0
var dist := attack_dist(v, r, origin, target, bonus)
for m in dist:
if m >= need:
p += dist[m]
return p
## Rough Cores a Hardware card adds in combat.
static func hardware_cores(v: Dictionary, hw: int) -> int:
return HARDWARE_CORES.get(v.cards[hw].name, 0)
## Health a Hardware card adds to its wearer (RAM Expansion), when the health rule is on.
static func hardware_health(v: Dictionary, hw: int) -> int:
return CardScripts.of(v.cards[hw].name).get("health", 0) if v.rules.health.on else 0
# ---------------------------------------------------------------------------------------------
# What the current combat is worth
## Points for `me` if the combat goes each way: {win, lose}.
static func stakes(v: Dictionary, me: int) -> Dictionary:
var c: Dictionary = v.combat
var loc := location_value(v, c.attacker.player, c.loc)
var atk := anomaly_stake(v, c.attacker)
var def := 0.0
for d in c.defenders:
def += anomaly_stake(v, d)
if c.attacker.player == me:
return {"win": loc + def / 2, "lose": -atk}
return {"win": atk / 2, "lose": -(loc + def)}
## Expected points for `me` from the current combat, or from how it ended. `reroll` ({ref, k})
## values a Bit Flip of one fighter's dice showing k or less.
static func combat_value(v: Dictionary, me: int, reroll: Variant = null) -> float:
var c = v.combat
if c == null:
return 0.0
var i_attack: bool = c.attacker.player == me
if c.ended:
# Ended early: the attacker takes the location only if every defender left.
var taken: bool = c.defenders.is_empty() and State.anomaly(v, c.attacker).at != null
if not taken:
return 0.0
return location_value(v, me, c.loc) if i_attack else -location_value(v, c.attacker.player, c.loc)
if v.rules.health.on:
return damage_value(v, me, c.attacker, c.defenders, combat_dist(v, reroll), location_value(v, c.attacker.player, c.loc))
var st := stakes(v, me)
var p_atk := combat_odds(v, reroll)
var p := p_atk if i_attack else 1 - p_atk
return p * st.win + (1 - p) * st.lose
static func card_name(v: Dictionary, id: int) -> String:
return "" if id < 0 else v.cards[id].name
static func printed_bandwidth(v: Dictionary, id: int) -> int:
return 0 if id < 0 else State.def_of(v, id).bandwidth

1
engine/bots/eval.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://cldo5g32fpwlm

1245
engine/bots/greedy.gd Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
uid://c8eurfxxusevb

32
engine/bots/random.gd Normal file
View file

@ -0,0 +1,32 @@
class_name RandomBot
extends RefCounted
## Baseline bot (bots/random.ts): ends the step or passes 30% of the time, otherwise picks any legal
## action. Choices are picked at random too.
##
## A bot has `bot_name` and `choose(view, actions, me) -> action`, picking one of `actions` for
## player `me` while seeing only `view` (see RulesEngine.view_for). Bots are made per game, from a
## seed, so they can keep per-game memory.
var bot_name := "random"
var _rng: Dictionary
func _init(seed: int) -> void:
_rng = Rng.make(seed)
func choose(_view: Dictionary, actions: Array, _me: int) -> Dictionary:
var stop = null
var others := []
for a in actions:
if a.type == "end" or a.type == "pass":
if stop == null:
stop = a
else:
others.append(a)
if others.is_empty():
return stop if stop != null else actions[0]
if stop != null and Rng.next_float(_rng) < 0.3:
return stop
var choices := others.filter(func(a): return a.type == "choose")
return Rng.pick(_rng, choices if not choices.is_empty() and Rng.next_float(_rng) < 0.8 else others)

View file

@ -0,0 +1 @@
uid://dnsq54w8tqkx7