diff --git a/CLAUDE.md b/CLAUDE.md index 5feaba0..c4bb8c1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -32,6 +32,7 @@ continuity. | `tools/capture.sh ` | Renders the real client offscreen in a throwaway `tidewall-capture-*` Docker container (Xvfb, Mesa software rendering) and saves a PNG. Fails on a blank frame or a renderer fallback. | | `tools/godot.sh --script res://tools/island_map.gd -- --seed=N --out=res://captures/island.png` | Draws a top-down map of a seed's island, with a contour at each tide level. | | `make bot-matches` | Plays 20 bot-only matches (about ten minutes, under `nice`) and checks every one has a winner, under a tenth of deaths are drownings, and easy bots win less than an even share and take the fewest eliminations. `BOT_MATCH_SEEDS=5 BOT_MATCH_FIRST=40` changes the seeds; under 20 matches the difficulty checks are skipped. | +| `make bot-builds` | M6: plays 40 bot-only matches (about 12 minutes, under `nice`) in which every other bot gathers and builds, and compares them with the rest. Builders must build in over a third of the fights they begin with the materials for a wall, lose fewer of the fights they build in than of the rest, and place no worse on average; every match's p99 tick must be within budget. `BOT_BUILDS_SEEDS=5 BOT_BUILDS_FIRST=40` changes the seeds; under 20 matches only completion and timing are judged. Lines in `build/bot_builds/matches.jsonl`. See docs/BALANCE.md. | | `make balance` | H3: plays 200 bot-only matches in four processes under `nice` (about 15 min) and writes `build/balance/report.txt`: match length, players standing over time, causes of death, eliminations by weapon and by difficulty. Fails if the median match is outside 12 to 18 minutes or a weapon takes over 35% of eliminations (`BALANCE_*` in tuning). `BALANCE_MATCHES`, `BALANCE_FIRST`, `BALANCE_JOBS` scale it. See docs/BALANCE.md. | | `make match` | Plays one whole offline match, lobby to results: a scripted player and 23 bots on a local server (about 25 s). Saves the command log to `build/match/match.tclog`, replays it and checks the replay ends on the same state hash. `MATCH_SEED=N` changes the seed; the default, 29, is one where the scripted player also builds. | | `make match-captures` | Renders eight stages of that match from its command log (`captures/match_.png`), about 3 minutes. Needs Docker. Look at them. | diff --git a/Makefile b/Makefile index ad86668..29af8c4 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Tidewall build entry points. Everything runs headless with the repo-local engine. GODOT := tools/godot.sh -.PHONY: qa-captures effects-captures app-icon deb deb-check bench dist dist-check build-captures poi-captures art figure figure-captures sea-captures models item-captures audio audio-check check import parse test test-selfcheck smoke capture-test terrain-capture binaries verify-engine balance bot-matches match match-captures client-check client-captures hud-captures style-captures menu-captures chart-captures discovery-check fuzz stability soak soak-selfcheck server-package-check +.PHONY: qa-captures effects-captures app-icon deb deb-check bench dist dist-check build-captures poi-captures art figure figure-captures sea-captures models item-captures audio audio-check check import parse test test-selfcheck smoke capture-test terrain-capture binaries verify-engine balance bot-matches bot-builds match match-captures client-check client-captures hud-captures style-captures menu-captures chart-captures discovery-check fuzz stability soak soak-selfcheck server-package-check ## A8: regenerates every sound in assets/audio/ from tools/audio/ (about 6 s). audio: import @@ -116,6 +116,13 @@ balance: import bot-matches: import @tools/bot_matches.sh +## M6: plays 40 bot-only matches in which every other bot gathers and builds (about 12 +## minutes, under nice) and checks the builders build in over a third of the fights they +## can afford to, lose fewer of the fights they build in than of the rest, and place no +## worse than the others. BOT_BUILDS_SEEDS and BOT_BUILDS_FIRST change the seeds. +bot-builds: import + @tools/bot_builds.sh + ## Plays one whole offline match, lobby to results, with a scripted player and 23 bots, ## then replays its command log and checks the replay ends in the same state. ## MATCH_SEED picks the seed. diff --git a/README.md b/README.md index aea39a3..8db6275 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ All of this is checked headless or with **software rendering in a container**. N - **Untested on real hardware.** No real GPU, Wayland session or hybrid-graphics machine has run it yet ([docs/PLAY.md](docs/PLAY.md#tested-configurations)). - **Solo only.** Duos and reviving ([#30](https://gitea.eom.dev/claude/tidewall/issues/30)) aren't in this release. - Bots carry powder kegs but don't throw them ([#83](https://gitea.eom.dev/claude/tidewall/issues/83)). -- **Bots don't build** ([#29](https://gitea.eom.dev/claude/tidewall/issues/29)), and most of them fall early in a match ([#74](https://gitea.eom.dev/claude/tidewall/issues/74)). +- Most bots fall early in a match ([#74](https://gitea.eom.dev/claude/tidewall/issues/74)). Bots build on `main` since 1.0.0 was tagged ([#29](https://gitea.eom.dev/claude/tidewall/issues/29)), but only cover walls, ramps and a last-stand ring of breakwaters, not forts. The 1.0.0 packages have bots that don't build. - **No gamepad support or tutorial** ([#53](https://gitea.eom.dev/claude/tidewall/issues/53), [#52](https://gitea.eom.dev/claude/tidewall/issues/52)). No character customization ([#43](https://gitea.eom.dev/claude/tidewall/issues/43)) or editing of placed pieces ([#18](https://gitea.eom.dev/claude/tidewall/issues/18)). - **The Join menu only searches the LAN** ([#82](https://gitea.eom.dev/claude/tidewall/issues/82)). List servers work from the command line (`--browse --list=...`); in the menu, join other servers by address. - Under OpenGL (the fallback when Vulkan can't start) the sun casts no shadows, because that renderer drew them with washed-out colours ([#81](https://gitea.eom.dev/claude/tidewall/issues/81)). diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 208800e..5198fde 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -300,8 +300,8 @@ What isn't sent: props, chests' places and their contents, which the client roll itself from the seed, as it does the island; and a piece's health, which follows from how far its building had got, the ticks since, and the damage it has taken. So a piece being built costs nothing on the wire. A scripted match of 12,198 ticks with 24 -players made 121 messages, 2.3 KB in all. Bots don't build yet, so a match of people -building will make far more: a new piece is about 16 bytes to each client. +players made 121 messages, 2.3 KB in all. That was before bots built (M6); a match with +building makes more: a new piece is about 16 bytes to each client. `ClientWorld` applies them to the client's world. `NetWorld.world_hash()` covers everything replicated this way, and a client that has applied every message up to a diff --git a/docs/BALANCE.md b/docs/BALANCE.md index d9ced6b..5f79a54 100644 --- a/docs/BALANCE.md +++ b/docs/BALANCE.md @@ -112,6 +112,52 @@ Every match ended with one player standing before the tide's clock ran out. Matc when the rising water brings the last few together: half of them end between 11 and 14 minutes, from phase 4's rise to phase 6's hold. +## Bots that build (M6, 2026-11-15) + +`make bot-builds` (`tools/bot_builds.sh`) plays bot-only matches in which every other +bot gathers and builds and the rest don't, so the two groups meet the same islands, +opponents and difficulty mix. Each bot counts its fights (a stretch with an opponent in +which shots or swings were traded), whether it began them with the materials for a +wall, whether it placed a piece in them, and whether it fell. + +What bots build: a wall toward a shooter who catches them reloading, from ambush or +worn down; a wall to heal behind instead of running when badly hurt; ramps up to +someone on a cliff or a steep rise; and, late in the match, a ring of breakwaters round +their cell when the tide would catch them or at the last stand. + +The first batches had builders gathering up to 120 of a material whenever a prop was +in reach. Over 40 matches (seeds 501 to 540) they won 15 to the control group's 25 and, +on seeds 521 to 540, lost 70% of their fights to the control group's 63%. Fights they +built in went better, so the cost was in gathering: standing at a prop facing away +from trouble, and time not spent looting. Gathering now waits until 10 s after the +last sight of an opponent or hit taken, not when hurt, and stops at 60 (120 in the last +three tide phases, for a ring). + +With that, over seeds 501 to 540: + +| | builders | control | +|---|---|---| +| fights | 584 | 594 | +| fights lost | 65.8% | 65.7% | +| mean place | 12.1 | 12.8 | +| wins | 20 | 20 | + +- Builders began 23% of their fights with the materials for a wall, and built in 53% of + those, 12% of all their fights. Most fights are in the landing rush, before anyone has + gathered anything. +- In the fights they built in they lost 58% (41 of 71); in the rest, 67%. +- They place better on average, by 0.2 to 1.0 places in every 20-match batch measured. +- They don't yet win fights or matches more often overall. Building helps a bot in the + fights it builds in. The time spent gathering, and fewer than a quarter of fights + begun with materials, leave the totals level. +- Breakwater rings are rare: few matches last to the last stand, and few bots by then + have 120 of one material. Tests cover the ring itself. +- The worst p99 tick in any of these matches was 3.4 ms, inside the 12 ms budget. + +`make bot-builds` checks what held: builders build in over a third of the fights they +can afford to, lose fewer of the fights they build in than of the rest, and place no +worse than the control group. + ## Known and left open - **The landing is still bloody.** About three quarters of a bot lobby falls in the first diff --git a/docs/GDD.md b/docs/GDD.md index d9d4865..af46fd7 100644 --- a/docs/GDD.md +++ b/docs/GDD.md @@ -60,7 +60,11 @@ from it, so a whole session can be replayed from one seed. Bots fill empty slots. They pick a landing spot, loot, fight, bandage themselves and keep ahead of the tide, knowing only what a player would. They come in three difficulties, easy, normal and hard, which differ in reaction time, sight range and aim; a server -mixes them unless told otherwise. Bots don't build yet, don't break crates, and don't +mixes them unless told otherwise. Bots build too: once armed, they gather with the +boathook (crates included) when nothing more pressing is going on. In a fight they put +up a wall when shot from ambush, while reloading or when worn down, wall themselves off +to heal instead of running when badly hurt, and build ramps up to someone on a cliff +or a steep rise. At the last stand they ring their spot with breakwaters. They don't use powder kegs or the line thrower. Offline play is a local server filled with bots, the same game code as online play. A downed duo partner can be revived, within `REVIVE_SECONDS`. diff --git a/src/bots/bot_brain.gd b/src/bots/bot_brain.gd index 4dc56fd..29182c7 100644 --- a/src/bots/bot_brain.gd +++ b/src/bots/bot_brain.gd @@ -13,6 +13,14 @@ ## boathook if not; use a bandage or armor when nobody is in view; pick up ## useful items and open chests; walk to the next loot marker; otherwise wander the safe ## ground, which is where everyone ends up. +## +## Bots build too (M6). An armed bot with nothing more pressing, and nobody about, +## gathers materials with the boathook. In a fight it puts a wall between itself and a +## shooter who catches it reloading, from ambush or worn down; walls itself off to heal +## instead of running when badly hurt; and builds ramps up to an opponent on a cliff or +## a steep rise. Late in the match, when the tide would catch it or at the last stand, +## it rings its cell with breakwaters. Every piece goes through the same build command +## a player sends, after the same Building.check the server makes. class_name BotBrain extends RefCounted @@ -30,7 +38,8 @@ var player_id: int = 0 var difficulty: int = NORMAL var nav: NavBot ## What the bot is doing, for logs and tests: "drop", "flee", "fight", "chase", "heal", -## "loot", "hunt" or "idle". +## "loot", "hunt", "hold", "idle", "retreat", "brawl", "gather", "cover", "ramp" or +## "breakwater". var state: String = "idle" ## The opponent the bot is fighting or looking for, or 0. var target_id: int = 0 @@ -92,6 +101,56 @@ var _leave_tries: int = 0 var _direct_best: float = INF var _direct_stalled: int = 0 +## M6: whether this bot gathers and builds. A batch turns it off for a control group. +var builds: bool = true +## M6: what this bot built and how its fights went, for batch measurement. A fight is +## a stretch with an opponent in which shots or swings were traded; it ends after +## Tuning.BOT_FIGHT_GAP_SECONDS with nobody to fight, or with the bot's fall ("lost"). +var stats: Dictionary = { + "pieces": 0, "walls": 0, "ramps": 0, "breakwaters": 0, "harvested": 0, + "fights": 0, "fights_built": 0, "fights_lost": 0, "fights_built_lost": 0, + "fights_stocked": 0, "fights_stocked_built": 0, +} +var _in_fight: bool = false +var _fight_traded: bool = false +var _fight_built: bool = false +var _fight_stocked: bool = false +var _fight_seen: int = 0 +var _stock_seen: int = 0 +## Gathering: the prop, when to give up on it, when each search radius was last tried +## (near, far), props given up on, and this tick's swing. +var _gather_prop: int = -1 +var _gather_deadline: int = 0 +var _gather_checked: PackedInt32Array = PackedInt32Array([-100000, -100000]) +var _gather_skipped: Dictionary = {} +var _swing: bool = false +var _swing_aim: Vector2 = Vector2.ZERO +## Cover: until when the bot stays behind it, which way the wall goes, whether it's up, +## when the cover began and when cover may be taken again. +var _covering_until: int = -100000 +var _cover_yaw: float = 0.0 +var _cover_walls: int = 0 +var _cover_since: int = 0 +var _cover_ready: int = 0 +## Ramps placed this fight, until when the bot walks on up them, which way, and the +## slot of the last one, so the next continues from its top. +var _ramps: int = 0 +var _ramp_until: int = -100000 +var _ramp_direction: Vector2 = Vector2.ZERO +var _ramp_last: Vector4i = Vector4i.ZERO +## A ring of breakwaters: the cell it's round, its four slots, when it was begun (or +## -1), and the highest forecast level it's worth staying in for. +var _ring_cell: Vector2i = Vector2i.ZERO +var _ring_slots: Array[Vector4i] = [] +var _ring_started: int = -1 +var _ring_level: float = 0.0 +## This tick's piece, if any: kind (-1 for none), facing yaw and material. +var _piece_kind: int = -1 +var _piece_yaw: float = 0.0 +var _piece_material: int = 0 +## A stand-in for the bot, posed for Building.target() and Building.check(). +var _probe: SimEntity = SimEntity.new(0, SimEntity.ARCHETYPE_PLAYER) + func _init(id: int, level: int = NORMAL) -> void: player_id = id @@ -114,9 +173,12 @@ func difficulty_name() -> String: func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: var me: SimEntity = sim.entities.find(player_id) if me == null or me.eliminated: + if _in_fight: + _end_fight(true) return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0) if me.drop_state != Drop.NONE: return _drop(sim, grid, world) + _count_building(world, me) _sense(sim, world, me) _check_tide(sim, world, me) _update_aim_error(sim) @@ -128,17 +190,25 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: var buttons: int = 0 _check_retreat(sim, world, me, target, armed) var retreating: bool = sim.tick < _retreat_until - var fighting: bool = target != null and target_visible and armed and not me.swimming and not retreating + if _fleeing or me.swimming: + _covering_until = mini(_covering_until, sim.tick) + var covering: bool = sim.tick < _covering_until + if covering and target != null and target_visible and distance <= Tuning.BOT_BRAWL_METRES: + # They came round the wall: fight. + _covering_until = sim.tick + covering = false + var fighting: bool = target != null and target_visible and armed and not me.swimming and not retreating and not covering # Inside a hut or loft too: someone beating on you there gets a swing back. An # unarmed bot doesn't start a brawl; it keeps looking for a weapon (H3). var provoked: bool = target != null and target.id == _hit_by and sim.tick - _hit_tick <= roundi(Tuning.BOT_MEMORY_SECONDS * TICKS) var brawling: bool = (target != null and target_visible and not armed and not me.swimming - and not _fleeing and not retreating and distance <= Tuning.BOT_BRAWL_METRES and provoked) + and not _fleeing and not retreating and not covering and distance <= Tuning.BOT_BRAWL_METRES and provoked) var interact: bool = false var walk: bool = true # Where to go. _direct_on = false + _swing = false var urgent: bool = _fleeing or retreating or (target != null and armed) if _stage != OUTSIDE: # Inside a hut or up a loft: loot, or walk back out first if something's up. @@ -146,9 +216,15 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: _set_stage(sim, LEAVING, maxi(_route_index - 1, 0) if _stage == ENTERING else _route.size() - 2) state = "flee" if _fleeing else "loot" interact = _walk_route(sim, me) + elif _in_ring(sim, me): + state = "breakwater" + _stop() elif _fleeing: state = "flee" _go(_flee_goal, 3.0) + elif covering: + state = "cover" + _stop() elif retreating: state = "retreat" _go(_retreat_goal, 3.0) @@ -170,10 +246,11 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: selection = heal_slot + 1 buttons |= PlayerCommand.BUTTON_FIRE walk = _goal_set - else: + elif not (builds and armed and _gather(sim, world, me, false)): interact = _loot(sim, grid, world, me) - if state != "loot": + if state != "loot" and not (builds and armed and _gather(sim, world, me, true)): _hunt(sim, world) + _plan_build(sim, grid, world, me, target, distance, fighting) # Walking, from the path follower. var direction: Vector2 = Vector2.ZERO @@ -200,6 +277,12 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: buttons |= step.buttons & (PlayerCommand.BUTTON_SPRINT | PlayerCommand.BUTTON_JUMP) if state == "heal": buttons &= ~PlayerCommand.BUTTON_SPRINT + if _swing: + yaw = _swing_aim.x + pitch = _swing_aim.y + direction = Vector2.ZERO + selection = Simulation.BOATHOOK_SLOT + buttons |= PlayerCommand.BUTTON_FIRE # Fighting: face the target and fire, whatever the feet are doing. if fighting or (target != null and target_visible and armed and not me.swimming and _fleeing): @@ -207,7 +290,7 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: var aim: Vector2 = _aim_at(me, target, def) yaw = aim.x pitch = aim.y - if not _fleeing and _stage == OUTSIDE: + if not _fleeing and _stage == OUTSIDE and state != "breakwater": direction = _combat_move(sim, grid, me, target, def) buttons &= ~PlayerCommand.BUTTON_SPRINT var in_range: bool = distance <= _fire_range(def) @@ -228,21 +311,61 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: var reach: float = Harvest.eye(me).distance_to(target.position + Vector3(0.0, Movement.body_height(target) * 0.62, 0.0)) if reach <= Tuning.BOT_SWING_METRES and sim.tick - _acquired_tick >= _reaction_ticks: buttons |= PlayerCommand.BUTTON_FIRE + elif covering: + # Behind the wall: heal, or finish reloading. + var heal_slot: int = _heal_slot(me) + if heal_slot >= 0: + selection = heal_slot + 1 + buttons |= PlayerCommand.BUTTON_FIRE + elif me.reload_ticks > 0: + # Stay behind it until the magazine's full. + _covering_until = maxi(_covering_until, sim.tick + 2) + elif armed: + var def: ItemDef = ItemCatalog.get_def(me.slot_items[_weapon_slot]) + if me.slot_rounds[_weapon_slot] < def.magazine and me.ammo[Inventory.ammo_index(def.ammo_code)] > 0: + buttons |= PlayerCommand.BUTTON_RELOAD + elif sim.tick - _cover_since > roundi(Tuning.BOT_COVER_HOLD_SECONDS * TICKS): + _covering_until = sim.tick + elif sim.tick - _cover_since > roundi(Tuning.BOT_COVER_HOLD_SECONDS * TICKS): + _covering_until = sim.tick elif armed and state != "heal" and me.reload_ticks == 0: # Nobody in view: top up the magazine. var def: ItemDef = ItemCatalog.get_def(me.slot_items[_weapon_slot]) if me.slot_rounds[_weapon_slot] < def.magazine and me.ammo[Inventory.ammo_index(def.ammo_code)] > 0: buttons |= PlayerCommand.BUTTON_RELOAD + # Up a ramp toward someone above: keep walking, still shooting, until off the top of + # the last one. + var g: float = StructureSet.cell_size() + var on_last: bool = (_ramps > 0 and Vector2i(floori(me.position.x / g), floori(me.position.z / g)) == Vector2i(_ramp_last.x, _ramp_last.z) + and me.position.y > _ramp_last.y * Tuning.BUILD_LEVEL_METRES - 0.5) + if on_last: + _ramp_until = maxi(_ramp_until, sim.tick + TICKS / 2) + if sim.tick < _ramp_until and target != null and not _fleeing and not covering and _stage == OUTSIDE: + state = "ramp" + direction = _ramp_direction + if interact and not _interact_last: buttons |= PlayerCommand.BUTTON_INTERACT _interact_last = true else: _interact_last = false + var chosen_material: int = 0 + if _piece_kind >= 0: + # This tick places a piece: build mode, facing where it goes, level. + yaw = _piece_yaw + pitch = 0.0 + var cleared: int = PlayerCommand.BUTTON_AIM | PlayerCommand.BUTTON_RELOAD | PlayerCommand.BUTTON_INTERACT | PlayerCommand.BUTTON_SPRINT + buttons = (buttons & ~cleared) | PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE + selection = _piece_kind + chosen_material = _piece_material + if _piece_kind != StructureSet.RAMP: + direction = Vector2.ZERO + _track_fight(sim, world, me, buttons) var forward: Vector2 = Vector2(sin(yaw), cos(yaw)) var right: Vector2 = Vector2(-cos(yaw), sin(yaw)) var move: Vector2 = Vector2(direction.dot(right), direction.dot(forward)) - return PlayerCommand.from_input(sim.tick, move, yaw, pitch, buttons, selection) + return PlayerCommand.from_input(sim.tick, move, yaw, pitch, buttons, selection, chosen_material) # Movement goals @@ -382,7 +505,7 @@ func _can_see(sim: Simulation, world: BotWorld, me: SimEntity, other: SimEntity, ## BOT_RETREAT_SECONDS, then heals if it can. Any bot without a gun runs from one held ## in view; a bot badly hurt with a way to heal breaks off too, except easy ones. func _check_retreat(sim: Simulation, world: BotWorld, me: SimEntity, target: SimEntity, armed: bool) -> void: - if target == null or _fleeing or sim.tick < _retreat_until or me.swimming: + if target == null or _fleeing or sim.tick < _retreat_until or sim.tick < _covering_until or me.swimming: return var outgunned: bool = not armed and target_visible and _holds_gun(target) var hurt: bool = _retreat_health > 0 and me.health + me.armor <= _retreat_health and _heal_slot(me) >= 0 @@ -390,6 +513,10 @@ func _check_retreat(sim: Simulation, world: BotWorld, me: SimEntity, target: Sim return if sim.tick - _retreat_until < roundi(Tuning.BOT_RETREAT_SECONDS * TICKS): return + if hurt and not outgunned and builds and sim.tick >= _cover_ready and _build_material(me, StructureSet.WALL) >= 0: + # M6: wall off and heal where it stands instead of running across open ground. + _take_cover(sim, me, target, Tuning.BOT_COVER_HEAL_SECONDS) + return var away: Vector3 = me.position - target.position away.y = 0.0 away = away.normalized() if away.length() > 0.1 else Vector3(1.0, 0.0, 0.0) @@ -859,3 +986,339 @@ func _hunt(sim: Simulation, world: BotWorld) -> void: _stop() else: _go(_hunt_goal, 4.0) + + +# Building (M6) + +## Counts what the bot placed and gathered since it last thought. +func _count_building(world: BotWorld, me: SimEntity) -> void: + for kind: int in world.placed.get(player_id, []): + stats["pieces"] += 1 + match kind: + StructureSet.WALL: + stats["walls"] += 1 + StructureSet.RAMP: + stats["ramps"] += 1 + StructureSet.BREAKWATER: + stats["breakwaters"] += 1 + if _in_fight: + _fight_built = true + var stock: int = 0 + for amount: int in me.materials: + stock += amount + if stock > _stock_seen: + stats["harvested"] += stock - _stock_seen + _stock_seen = stock + + +## Starts or ends the fight the bot is in, for the measurement. +func _track_fight(sim: Simulation, world: BotWorld, me: SimEntity, buttons: int) -> void: + var engaged: bool = target_id != 0 or state == "cover" or state == "retreat" + if engaged: + if not _in_fight: + _in_fight = true + _fight_traded = false + _fight_built = false + _fight_stocked = builds and _build_material(me, StructureSet.WALL) >= 0 + _ramps = 0 + _fight_seen = sim.tick + var shooting: bool = (buttons & PlayerCommand.BUTTON_FIRE) != 0 and (state == "fight" or state == "brawl" or state == "ramp") + if shooting or world.attackers.has(player_id): + _fight_traded = true + elif _in_fight and sim.tick - _fight_seen > roundi(Tuning.BOT_FIGHT_GAP_SECONDS * TICKS): + _end_fight(false) + + +func _end_fight(lost: bool) -> void: + _in_fight = false + if not _fight_traded: + return + stats["fights"] += 1 + if _fight_built: + stats["fights_built"] += 1 + if _fight_stocked: + stats["fights_stocked"] += 1 + if _fight_built: + stats["fights_stocked_built"] += 1 + if lost: + stats["fights_lost"] += 1 + if _fight_built: + stats["fights_built_lost"] += 1 + + +## Goes behind a wall, toward `target`, for `seconds`. +func _take_cover(sim: Simulation, me: SimEntity, target: SimEntity, seconds: float) -> void: + _covering_until = sim.tick + roundi(seconds * TICKS) + _cover_since = sim.tick + _cover_yaw = atan2(target.position.x - me.position.x, target.position.z - me.position.z) + _cover_walls = 0 + _cover_ready = sim.tick + roundi(Tuning.BOT_COVER_SECONDS * TICKS) + + +## Chooses the piece, if any, this tick places. Sets `_piece_kind`. +func _plan_build(sim: Simulation, grid: NavGrid, world: BotWorld, me: SimEntity, target: SimEntity, distance: float, fighting: bool) -> void: + _piece_kind = -1 + if not builds or me.swimming or not me.on_ground or me.action_cooldown > 0 or _stage != OUTSIDE: + return + var stranded: bool = _stranded(sim, world, me) + if _in_ring(sim, me) or stranded or _ring_due(sim, world, me): + if not _in_ring(sim, me): + _begin_ring(sim, me, stranded) + for facing: int in 4: + if _try_place(sim, me, StructureSet.BREAKWATER, facing * PI * 0.5): + state = "breakwater" + return + if state == "cover": + if _cover_walls == 0: + if _try_place(sim, me, StructureSet.WALL, _cover_yaw): + _cover_walls = 1 + elif sim.tick - _cover_since > TICKS / 2: + # Nowhere to put a wall: give up on cover, and break off as before. + _covering_until = sim.tick + return + if target == null: + return + var toward: float = atan2(target.position.x - me.position.x, target.position.z - me.position.z) + var rise: float = target.position.y - me.position.y + var across: float = Vector2(target.position.x - me.position.x, target.position.z - me.position.z).length() + var climbing: bool = sim.tick < _ramp_until + var ramp_wanted: bool = ((fighting or state == "chase" or state == "ramp") and _ramps < Tuning.BOT_RAMP_MAX + and rise >= Tuning.BOT_RAMP_RISE_METRES and across <= Tuning.BOT_RAMP_REACH_METRES + and (climbing or rise / maxf(across, 0.1) >= Tuning.BOT_RAMP_SLOPE or _steep_between(grid, me, target)) + and _build_material(me, StructureSet.RAMP) >= 0) + # Shot at while reloading, from ambush or when worn down: a wall to shelter behind, + # unless it's from up a cliff, where a wall would only block the way up. + var shot_at: bool = _hit_by == target.id and sim.tick - _hit_tick <= TICKS / 2 + var exposed: bool = me.reload_ticks > 0 or sim.tick - _acquired_tick <= TICKS or me.health + me.armor <= Tuning.BOT_COVER_HEALTH + if not ramp_wanted and (fighting or state == "chase") and shot_at and exposed and distance >= Tuning.BOT_COVER_MIN_METRES and sim.tick >= _cover_ready: + if _try_place(sim, me, StructureSet.WALL, toward): + _take_cover(sim, me, target, Tuning.BOT_COVER_HOLD_SECONDS) + _cover_walls = 1 + state = "cover" + return + # Someone up a steep slope or a cliff: ramp up to them. + if ramp_wanted: + # On the way up, the next ramp goes on from the top of the last: one cell on, + # one cell up. Until the bot is there, it keeps walking. + var facing: int = _ramp_last.w if climbing else posmod(roundi(toward / (PI * 0.5)), 4) + var yaw: float = facing * PI * 0.5 + if not climbing: + # A ramp of its own already on the way (it broke off to take cover, say): + # walk on up it. + _probe.position = me.position + _probe.yaw = posmod(roundi(yaw / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS) + _probe.pitch = 0 + var first: Building.Target = Building.target(_probe, StructureSet.RAMP) + for dy: int in [-1, 0, 1]: + var slot: Vector4i = Vector4i(first.slot.x, first.slot.y + dy, first.slot.z, first.slot.w) + var piece: StructureSet.Piece = sim.structures.find(sim.structures.at_slot(slot)) + if piece != null and piece.kind == StructureSet.RAMP and piece.owner == player_id and piece.facing == facing: + _ramp_last = Vector4i(slot.x, slot.y, slot.z, facing) + var ahead: Vector2i = StructureSet.FACING_DIRECTIONS[facing] + _ramp_direction = Vector2(ahead.x, ahead.y) + _ramp_until = sim.tick + roundi(Tuning.BOT_RAMP_WALK_SECONDS * TICKS) + return + if climbing: + _probe.position = me.position + _probe.yaw = posmod(roundi(yaw / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS) + _probe.pitch = 0 + var next: Building.Target = Building.target(_probe, StructureSet.RAMP) + var step: Vector2i = StructureSet.FACING_DIRECTIONS[facing] + var level_step: int = roundi(StructureSet.cell_size() / Tuning.BUILD_LEVEL_METRES) + if next.slot.x != _ramp_last.x + step.x or next.slot.z != _ramp_last.z + step.y or next.slot.y != _ramp_last.y + level_step: + return + if _try_place(sim, me, StructureSet.RAMP, yaw): + var placed: Building.Target = Building.target(_probe, StructureSet.RAMP) + _ramp_last = Vector4i(placed.slot.x, placed.slot.y, placed.slot.z, facing) + var step: Vector2i = StructureSet.FACING_DIRECTIONS[facing] + _ramp_direction = Vector2(step.x, step.y) + _ramps += 1 + _ramp_until = sim.tick + roundi(Tuning.BOT_RAMP_WALK_SECONDS * TICKS) + + +## True if the ground on the line to `target` is a cliff, or a solid that `target` +## stands up on, somewhere a bot can't walk straight up. +static func _steep_between(grid: NavGrid, me: SimEntity, target: SimEntity) -> bool: + var from: Vector2 = Vector2(me.position.x, me.position.z) + var to: Vector2 = Vector2(target.position.x, target.position.z) + var samples: int = ceili(from.distance_to(to) / Tuning.BOT_NAV_CELL_METRES) + for index: int in range(1, samples + 1): + var at: Vector2 = from.lerp(to, float(index) / samples) + var point: Vector2i = grid.point_of(Vector3(at.x, 0.0, at.y)) + var cell: int = grid.index_of(point) + if grid.steep[cell] != 0: + return true + if grid.blocked[cell] > 0 and target.position.y > grid.heights[cell] + Tuning.BOT_RAMP_RISE_METRES * 0.5: + return true + return false + + +## Starts a ring of breakwaters round the bot's cell. Stranded, it's worth staying in +## while the coming level is within what a breakwater holds back; at the last stand, +## whatever comes. +func _begin_ring(sim: Simulation, me: SimEntity, stranded: bool) -> void: + var g: float = StructureSet.cell_size() + _ring_cell = Vector2i(floori(me.position.x / g), floori(me.position.z / g)) + _ring_started = sim.tick + _ring_level = me.position.y + Tuning.BREAKWATER_MAX_HEAD - 1.0 if stranded else INF + _ring_slots.clear() + _probe.position = me.position + _probe.pitch = 0 + for facing: int in 4: + _probe.yaw = posmod(roundi(facing * 0.25 * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS) + _ring_slots.append(Building.target(_probe, StructureSet.BREAKWATER).slot) + + +## True while the bot is in the cell it began ringing: for two seconds while the ring +## goes up, then for as long as all four stand and the coming level is one it holds. +func _in_ring(sim: Simulation, me: SimEntity) -> bool: + if _ring_started < 0: + return false + var g: float = StructureSet.cell_size() + if Vector2i(floori(me.position.x / g), floori(me.position.z / g)) != _ring_cell or me.swimming: + _ring_started = -1 + return false + if sim.tide != null and sim.tide.forecast_at(sim.tick) > _ring_level: + _ring_started = -1 + return false + if sim.tick - _ring_started < 2 * TICKS: + return true + for slot: Vector4i in _ring_slots: + if not sim.structures.occupied(slot): + _ring_started = -1 + return false + return true + + +## True when the bot is running from the tide late in the match (the last +## BOT_STRAND_PHASES phases) and can't make it: the sea will reach its feet before it +## could walk to dry ground, though no higher than a breakwater holds back, and it can +## afford a ring. +func _stranded(sim: Simulation, world: BotWorld, me: SimEntity) -> bool: + if not _fleeing or sim.tide == null or world.phase_count() == 0: + return false + var material: int = _build_material(me, StructureSet.BREAKWATER) + if material < 0 or me.materials[material] < 4 * StructureSet.cost_of(StructureSet.BREAKWATER): + return false + if sim.tide.phase_at(sim.tick) < world.phase_count() - Tuning.BOT_STRAND_PHASES: + return false + var feet: float = me.position.y + if sim.tide.forecast_at(sim.tick) > feet + Tuning.BREAKWATER_MAX_HEAD - 1.0: + return false + if sim.water_level_at(me.position) > feet - 0.3: + return false + var walk: float = Vector2(_flee_goal.x - me.position.x, _flee_goal.z - me.position.z).length() / Tuning.BOT_FLEE_SPEED + for second: int in range(0, ceili(walk) + 1): + if sim.tide.level_at(sim.tick + second * TICKS) >= feet - 0.3: + return true + return false + + +## True at the last stand: the tide's phase leaves no dry ground, the bot is on the +## last high point, and the rise has begun or is BOT_RING_SECONDS off. +func _ring_due(sim: Simulation, world: BotWorld, me: SimEntity) -> bool: + if sim.tide == null or world.phase_count() == 0: + return false + var phase: int = clampi(sim.tide.phase_at(sim.tick), 0, world.phase_count() - 1) + if world.phase_cells[phase] != 0 or not world.is_safe(me.position, phase): + return false + return sim.tide.stage_at(sim.tick) != Tide.HOLD or sim.tide.ticks_until_rise(sim.tick) <= roundi(Tuning.BOT_RING_SECONDS * TICKS) + + +## The material to build `kind` in: whichever the bot has most of, if that covers the +## cost, or -1. +static func _build_material(me: SimEntity, kind: int) -> int: + var best: int = 0 + for code: int in Materials.NAMES.size(): + if me.materials[code] > me.materials[best]: + best = code + return best if me.materials[best] >= StructureSet.cost_of(kind) else -1 + + +## Sets this tick's piece to `kind`, facing `yaw`, if the server would accept it from +## where the bot stands. Returns whether it did. +func _try_place(sim: Simulation, me: SimEntity, kind: int, yaw: float) -> bool: + var material: int = _build_material(me, kind) + if material < 0: + return false + _probe.position = me.position + _probe.yaw = posmod(roundi(yaw / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS) + _probe.pitch = 0 + _probe.health = me.health + _probe.crouching = me.crouching + _probe.swimming = me.swimming + _probe.drop_state = me.drop_state + _probe.materials = me.materials + var at: Building.Target = Building.target(_probe, kind) + if Building.check(sim, _probe, at, material) != Building.Result.OK: + return false + _piece_kind = kind + _piece_yaw = yaw + _piece_material = material + return true + + +## Gathers materials with the boathook: carries on with the prop it's at, or, at most +## once a second and only when it's been quiet a while and the bot isn't hurt, looks +## for one within BOT_HARVEST_NEAR_METRES (`far` false) or +## BOT_HARVEST_METRES. Sets `state` to "gather" and returns true while it has one. +func _gather(sim: Simulation, world: BotWorld, me: SimEntity, far: bool) -> bool: + var stock: int = 0 + for amount: int in me.materials: + stock = maxi(stock, amount) + var wanted: int = Tuning.BOT_BUILD_STOCK + if sim.tide != null and world.phase_count() > 0 and sim.tide.phase_at(sim.tick) >= world.phase_count() - Tuning.BOT_STRAND_PHASES: + # Late in the match: enough for a ring of breakwaters. + wanted = 4 * StructureSet.cost_of(StructureSet.BREAKWATER) + if stock >= wanted or not Harvest.can_swing(me): + _gather_prop = -1 + return false + if _gather_prop >= 0 and (not sim.props.is_standing(_gather_prop) or sim.tick > _gather_deadline): + if sim.props.is_standing(_gather_prop): + _gather_skipped[_gather_prop] = true + _gather_prop = -1 + if _gather_prop < 0: + var quiet: int = roundi(Tuning.BOT_GATHER_QUIET_SECONDS * TICKS) + if sim.tick - _last_seen_tick < quiet or sim.tick - _hit_tick < quiet or me.health + me.armor < Tuning.BOT_GATHER_HEALTH: + return false + var index: int = 1 if far else 0 + if sim.tick - _gather_checked[index] < TICKS: + return false + _gather_checked[index] = sim.tick + _gather_prop = _find_gather_prop(sim, world, me, Tuning.BOT_HARVEST_METRES if far else Tuning.BOT_HARVEST_NEAR_METRES) + if _gather_prop < 0: + return false + var gap: float = PropSet._distance_to_box(sim.props.boxes[_gather_prop], me.position) + _gather_deadline = sim.tick + roundi((Tuning.BOT_HARVEST_SECONDS + gap / 3.0) * TICKS) + state = "gather" + _stop() + var box: AABB = sim.props.boxes[_gather_prop] + var eye: Vector3 = Harvest.eye(me) + var centre: Vector3 = box.get_center() + var aim: Vector3 = Vector3(centre.x, clampf(eye.y, box.position.y + 0.2, box.end.y - 0.1), centre.z) + var to: Vector3 = aim - eye + if PropSet._distance_to_box(box, eye) <= Harvest.reach() - 0.6: + _swing = true + _swing_aim = Vector2(atan2(to.x, to.z), atan2(to.y, Vector2(to.x, to.z).length())) + return true + _walk_to(me, Vector3(centre.x, box.position.y, centre.z), 0.0) + if _direct_stalled > 3 * TICKS: + _gather_skipped[_gather_prop] = true + _gather_prop = -1 + return true + + +## The nearest standing prop of a building material within `radius`, on dry ground +## about level with the bot and outside huts and lofts, that it hasn't given up on. +func _find_gather_prop(sim: Simulation, world: BotWorld, me: SimEntity, radius: float) -> int: + for prop: int in sim.props.near(me.position, radius): + if _gather_skipped.has(prop) or not Materials.is_valid(Materials.code_of(sim.props.material_of(prop))): + continue + var box: AABB = sim.props.boxes[prop] + var foot: Vector3 = Vector3(box.get_center().x, box.position.y, box.get_center().z) + if absf(box.position.y - me.position.y) > 3.0 or not _spot_allowed(sim, world, foot): + continue + if world.in_routed_place(foot): + continue + return prop + return -1 diff --git a/src/bots/bot_world.gd b/src/bots/bot_world.gd index 93feb33..20ebb38 100644 --- a/src/bots/bot_world.gd +++ b/src/bots/bot_world.gd @@ -46,6 +46,8 @@ var walkable: PackedByteArray = PackedByteArray() var landings: Array[Vector3] = [] ## Victim entity ID -> attacker entity ID, for hits landed during the last tick. var attackers: Dictionary = {} +## Builder entity ID -> the kinds of the pieces it placed during the last tick (M6). +var placed: Dictionary = {} ## `grid` is the match's navigation grid. Loot markers and safe ground that nothing can @@ -120,12 +122,19 @@ func in_routed_place(position: Vector3) -> bool: return false -## Notes who hit whom during the last tick. Call once per tick, before bots think. +## Notes who hit whom, and who placed what, during the last tick. Call once per tick, +## before bots think. func observe(sim: Simulation) -> void: attackers.clear() + placed.clear() for event: Dictionary in sim.events: if event["type"] == "player_hit" and int(event["player"]) != 0: attackers[int(event["victim"])] = int(event["player"]) + elif event["type"] == "piece_placed": + var builder: int = int(event["player"]) + if not placed.has(builder): + placed[builder] = [] + placed[builder].append(int(event["kind"])) func phase_count() -> int: diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index 4f9be5b..c10e775 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -324,6 +324,47 @@ const BOT_LOOT_TIDE_SECONDS: float = 30.0 ## Bots give up on a loot spot or an item they can't get to after this long. const BOT_LOOT_TIMEOUT_SECONDS: float = 45.0 const BOT_PICKUP_TIMEOUT_SECONDS: float = 8.0 +## M6, bots that build. An armed bot with nothing more pressing gathers with the +## boathook until its best material reaches BOT_BUILD_STOCK (in the last +## BOT_STRAND_PHASES tide phases, enough for four breakwaters): from a prop within +## BOT_HARVEST_NEAR_METRES while it loots, or BOT_HARVEST_METRES once it has nothing +## left to loot. It gives up on a prop after BOT_HARVEST_SECONDS. It doesn't start +## within BOT_GATHER_QUIET_SECONDS of last seeing an opponent or being hit, or with +## health and armor under BOT_GATHER_HEALTH. +const BOT_BUILD_STOCK: int = 60 +const BOT_GATHER_QUIET_SECONDS: float = 10.0 +const BOT_GATHER_HEALTH: int = 60 +const BOT_HARVEST_NEAR_METRES: float = 12.0 +const BOT_HARVEST_METRES: float = 24.0 +const BOT_HARVEST_SECONDS: float = 12.0 +## Cover: a bot shot while it reloads, within a second of first seeing its shooter, or +## with health and armor down to BOT_COVER_HEALTH, puts a wall between them, and waits +## behind it at least BOT_COVER_HOLD_SECONDS and until any reload is done. Only for a +## shooter at least BOT_COVER_MIN_METRES off, and no more often than BOT_COVER_SECONDS. +const BOT_COVER_HOLD_SECONDS: float = 2.0 +const BOT_COVER_HEALTH: int = 60 +const BOT_COVER_MIN_METRES: float = 6.0 +const BOT_COVER_SECONDS: float = 5.0 +## A badly hurt bot that could break off (BOT_DIFFICULTIES "retreat") and can afford a +## wall walls itself off and heals there instead, for up to this long. +const BOT_COVER_HEAL_SECONDS: float = 7.0 +## Ramps: up toward an opponent at least BOT_RAMP_RISE_METRES higher, within +## BOT_RAMP_REACH_METRES across, and up ground at least this steep (rise over run). +## At most BOT_RAMP_MAX ramps a fight; the bot walks on BOT_RAMP_WALK_SECONDS after each. +const BOT_RAMP_RISE_METRES: float = 3.0 +const BOT_RAMP_REACH_METRES: float = 24.0 +const BOT_RAMP_SLOPE: float = 0.5 +const BOT_RAMP_MAX: int = 5 +const BOT_RAMP_WALK_SECONDS: float = 1.5 +## At the last stand (a tide phase that leaves no dry ground), a bot on the last high +## point rings its own cell with breakwaters once the rise is this close. +const BOT_RING_SECONDS: float = 30.0 +## In the last this many tide phases, a bot running from the tide that can't reach dry +## ground before the sea reaches its feet rings its cell with breakwaters instead, if +## the coming level is one they hold back (BREAKWATER_MAX_HEAD, less a metre). +const BOT_STRAND_PHASES: int = 3 +## A fight ends for the measurement (M6) once the bot has had nobody to fight this long. +const BOT_FIGHT_GAP_SECONDS: float = 4.0 # Building const GRID_CELL_METRES: float = 4.0 diff --git a/test/bots/bot_batch.gd b/test/bots/bot_batch.gd index 3f6b5b4..68dfc71 100644 --- a/test/bots/bot_batch.gd +++ b/test/bots/bot_batch.gd @@ -14,8 +14,14 @@ static func tick_limit() -> int: ## "causes": {cause: count}, "places": {difficulty name: [place, ...]}, ## "eliminations": {difficulty name: count}, "weapon_kills": {item id: count}, ## "death_seconds": [seconds into the match of each fall], "shots", "slow_ticks", -## "timing", "state_hash"}. Seconds count from the barge leaving. -static func run_match(seed_value: int, bots: int = Tuning.SOLO_PLAYERS, difficulty: int = -1) -> Dictionary: +## "timing", "state_hash", "building": {"build": group, "control": group}}. Seconds +## count from the barge leaving. Each group (M6) adds up its bots' BotBrain.stats and +## holds their "bots", "places" and "wins". +## +## With `split_builders`, only every other bot gathers and builds; the rest are the +## control group. Bot difficulty also goes by turns of three, so both groups have each +## level equally (within one bot). +static func run_match(seed_value: int, bots: int = Tuning.SOLO_PLAYERS, difficulty: int = -1, split_builders: bool = false) -> Dictionary: var loop: ServerLoop = ServerLoop.new(seed_value, 0, bots, null, difficulty) var flow: Match = loop.match_flow var level_of: Dictionary = {} @@ -27,7 +33,13 @@ static func run_match(seed_value: int, bots: int = Tuning.SOLO_PLAYERS, difficul "slow_ticks": [], "weapon_kills": {}, "death_seconds": [], } var limit: int = tick_limit() + var split: bool = false while loop.ticks < limit and flow.phase != Match.RESULTS: + if split_builders and not split and not loop.bots.is_empty(): + # The bots join when the match starts, before any of them thinks. + split = true + for index: int in loop.bots.size(): + loop.bots[index].builds = index % 2 == 0 var before: int = Time.get_ticks_usec() loop.run_tick() if Time.get_ticks_usec() - before > ServerLoop.TICK_BUDGET_USEC: @@ -62,6 +74,19 @@ static func run_match(seed_value: int, bots: int = Tuning.SOLO_PLAYERS, difficul # The boathook isn't in the catalog: its code is ItemCatalog.NONE. var weapon: String = def.id if def != null else (ItemCatalog.BOATHOOK if int(record["item"]) == ItemCatalog.NONE else "unknown") result["weapon_kills"][weapon] = int(result["weapon_kills"].get(weapon, 0)) + 1 + var place_of: Dictionary = {} + for row: Dictionary in flow.summary(): + place_of[row["handle"]] = row["place"] + var groups: Dictionary = {"build": _empty_group(), "control": _empty_group()} + for bot: BotBrain in loop.bots: + var group: Dictionary = groups["build" if bot.builds else "control"] + group["bots"] += 1 + for key: String in bot.stats: + group[key] += int(bot.stats[key]) + var place: int = int(place_of.get(flow.handle_of[bot.player_id], 0)) + group["places"].append(place) + group["wins"] += 1 if place == 1 else 0 + result["building"] = groups result["timing"] = loop.timing_stats() result["state_hash"] = loop.sim.state_hash() return result @@ -104,3 +129,42 @@ static func summarise(results: Array[Dictionary]) -> Dictionary: total["drown_share"] = float(total["causes"].get("drown", 0)) / maxi(total["deaths"], 1) total["mean_ticks"] = ticks / maxi(results.size(), 1) return total + + +static func _empty_group() -> Dictionary: + var group: Dictionary = {"bots": 0, "places": [], "wins": 0} + for key: String in BotBrain.new(0).stats: + group[key] = 0 + return group + + +## Adds up the "building" groups of run_match() results: per group the totals, plus +## "mean_place", "fight_share_built" (fights with a piece placed in them, of all +## fights), "stocked_share" (fights begun with the materials for a wall, of all fights), +## "stocked_share_built" (of those, the ones built in) and "fight_loss" (fights lost, +## of all fights), and for the build group +## "loss_when_built" and "loss_when_not". +static func summarise_building(results: Array[Dictionary]) -> Dictionary: + var total: Dictionary = {"build": _empty_group(), "control": _empty_group()} + for result: Dictionary in results: + for name: String in total: + var group: Dictionary = result["building"][name] + for key: String in group: + if key == "places": + total[name]["places"].append_array(group["places"]) + else: + total[name][key] += int(group[key]) + for name: String in total: + var group: Dictionary = total[name] + var sum: int = 0 + for place: int in group["places"]: + sum += place + group["mean_place"] = float(sum) / maxi(group["places"].size(), 1) + group["fight_share_built"] = float(group["fights_built"]) / maxi(group["fights"], 1) + group["stocked_share"] = float(group["fights_stocked"]) / maxi(group["fights"], 1) + group["stocked_share_built"] = float(group["fights_stocked_built"]) / maxi(group["fights_stocked"], 1) + group["fight_loss"] = float(group["fights_lost"]) / maxi(group["fights"], 1) + group["loss_when_built"] = float(group["fights_built_lost"]) / maxi(group["fights_built"], 1) + group["loss_when_not"] = float(group["fights_lost"] - group["fights_built_lost"]) / maxi(group["fights"] - group["fights_built"], 1) + group.erase("places") + return total diff --git a/test/bots/test_bot_building.gd b/test/bots/test_bot_building.gd new file mode 100644 index 0000000..45a4a29 --- /dev/null +++ b/test/bots/test_bot_building.gd @@ -0,0 +1,369 @@ +extends TestCase + +## Bots that build, from issue M6: they gather materials with the boathook, wall off a +## shooter while they reload, wall off and heal instead of running when badly hurt, +## ramp up to an opponent on a cliff, and ring their cell with breakwaters at the last +## stand. Every piece goes through the same build command and checks as a player's. + +const FLOOR: float = 20.0 +const HOME: Vector3 = Vector3(202.0, FLOOR, 202.0) +const TICKS: int = Tuning.TICK_RATE_HZ +## Yaw steps facing +x. +const FACING_X: int = PlayerCommand.YAW_STEPS / 4 + +var sim: Simulation +var grid: NavGrid +var world: BotWorld +var brains: Array[BotBrain] = [] +## Victim ID -> attacker ID, written into the bots' world every tick, as if each victim +## kept being hit by its attacker. +var hits: Dictionary = {} + + +func _setup(ground: SimGround = null) -> void: + sim = Simulation.new(7, ground if ground != null else SimGround.flat(FLOOR)) + brains.clear() + hits.clear() + + +func _player(at: Vector3, yaw: int = 0) -> SimEntity: + var player: SimEntity = sim.entities.find(sim.add_player()) + player.position = at + player.on_ground = true + player.yaw = yaw + return player + + +func _bot(at: Vector3, level: int = BotBrain.NORMAL, yaw: int = 0) -> BotBrain: + var brain: BotBrain = BotBrain.new(_player(at, yaw).id, level) + brains.append(brain) + return brain + + +func _pin(brain: BotBrain) -> void: + brain._hunt_goal = sim.entities.find(brain.player_id).position + brain._holding = true + brain._hunt_until = 1 << 30 + + +func _arm(player: SimEntity, rounds: int = 150) -> void: + assert_eq(Inventory.give(player, ItemCatalog.code_of("dock_carbine"), 1), 0) + Inventory.give(player, ItemCatalog.code_of("cartridges"), rounds) + + +func _ready_world() -> void: + grid = NavGrid.build(sim) + world = BotWorld.build(sim, grid) + for entity: SimEntity in sim.entities.entities(): + if entity.archetype == SimEntity.ARCHETYPE_PLAYER: + world.player_ids.append(entity.id) + + +func _run(ticks: int, until: Callable = Callable()) -> void: + for _index: int in ticks: + grid.sync(sim) + world.observe(sim) + for victim: int in hits: + world.attackers[victim] = hits[victim] + for brain: BotBrain in brains: + sim.submit_command(brain.player_id, brain.think(sim, grid, world)) + sim.step() + if until.is_valid() and until.call(): + return + + +func _pieces_of(owner: int, kind: int = -1) -> Array[StructureSet.Piece]: + var found: Array[StructureSet.Piece] = [] + for piece: StructureSet.Piece in sim.structures.pieces.values(): + if piece.owner == owner and (kind < 0 or piece.kind == kind): + found.append(piece) + return found + + +## Puts a single boulder (stone) on the flat test ground, `at` its footprint centre. +func _boulder(at: Vector3) -> void: + var props: PropSet = PropSet.new() + props.kinds.append(PropSet.KINDS.find("boulder")) + props.positions.append(at) + props.yaws.append(0.0) + props.boxes.append(AABB(at - Vector3(1.0, 0.0, 1.0), Vector3(2.0, 1.5, 2.0))) + props.health.append(int(Tuning.PROP_KINDS["boulder"]["health"])) + props.add_solids_to(sim.ground) + sim.props = props + sim.structures.props = props + + +func test_an_armed_bot_gathers_materials_from_a_prop_nearby() -> void: + _setup() + var brain: BotBrain = _bot(HOME) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + _boulder(HOME + Vector3(6.0, 0.0, 0.0)) + _ready_world() + var states: Dictionary = {} + _run(20 * TICKS, func() -> bool: + states[brain.state] = true + return me.materials[Materials.STONE] >= Tuning.HARVEST_PER_HIT * 3) + assert_true(states.has("gather"), "it went to gather: %s" % [states.keys()]) + assert_true(me.materials[Materials.STONE] >= Tuning.HARVEST_PER_HIT * 3, "it broke stone (%d)" % me.materials[Materials.STONE]) + _run(1) + assert_eq(brain.stats["harvested"], me.materials[Materials.STONE]) + + +func test_a_bot_with_enough_stock_or_no_weapon_leaves_props_alone() -> void: + for armed: bool in [true, false]: + _setup() + var brain: BotBrain = _bot(HOME) + var me: SimEntity = sim.entities.find(brain.player_id) + if armed: + _arm(me) + me.materials[Materials.DRIFTWOOD] = Tuning.BOT_BUILD_STOCK + _boulder(HOME + Vector3(6.0, 0.0, 0.0)) + _ready_world() + var states: Dictionary = {} + _run(8 * TICKS, func() -> bool: + states[brain.state] = true + return false) + assert_false(states.has("gather"), "armed %s: %s" % [armed, states.keys()]) + assert_eq(me.materials[Materials.STONE], 0) + + +func test_a_control_bot_never_gathers_or_builds() -> void: + _setup() + var brain: BotBrain = _bot(HOME) + brain.builds = false + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + _boulder(HOME + Vector3(6.0, 0.0, 0.0)) + _ready_world() + _run(8 * TICKS) + assert_eq(me.materials[Materials.STONE], 0) + assert_eq(brain.stats["pieces"], 0) + + +func test_a_bot_shot_at_while_reloading_walls_off_the_shooter() -> void: + _setup() + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + me.materials[Materials.DRIFTWOOD] = 50 + var shooter: SimEntity = _player(HOME + Vector3(20.0, 0.0, 0.0), FACING_X * 3) + _arm(shooter) + shooter.selected = 1 + _ready_world() + _pin(brain) + # Seen, then shot while its magazine runs dry. + var seen: Dictionary = {} + _run(2 * TICKS, func() -> bool: + seen[brain.state] = true + return brain.state == "fight") + assert_eq(brain.state, "fight", "%s target %d visible %s" % [seen.keys(), brain.target_id, brain.target_visible]) + me.slot_rounds[0] = 0 + hits[me.id] = shooter.id + var states: Dictionary = {} + _run(2 * TICKS, func() -> bool: + states[brain.state] = true + return not _pieces_of(me.id).is_empty()) + var walls: Array[StructureSet.Piece] = _pieces_of(me.id, StructureSet.WALL) + assert_eq(walls.size(), 1, "one wall: %s" % [states.keys()]) + var wall: AABB = walls[0].boxes[0] + assert_true(wall.get_center().x > me.position.x and wall.get_center().x < shooter.position.x, "between them (wall at x %.1f)" % wall.get_center().x) + assert_eq(me.materials[Materials.DRIFTWOOD], 50 - StructureSet.cost_of(StructureSet.WALL)) + hits.clear() + _run(Tuning.BOT_SENSE_TICKS + 2) + assert_eq(brain.state, "cover") + assert_false(brain.target_visible, "the wall hides the shooter") + _run(roundi(Tuning.BOT_COVER_HOLD_SECONDS * TICKS) + TICKS) + assert_true(me.slot_rounds[0] > 0, "it reloaded behind the wall") + assert_ne(brain.state, "cover", "and came out again") + + +func test_a_badly_hurt_bot_walls_off_and_heals_instead_of_running() -> void: + _setup() + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + Inventory.give(me, ItemCatalog.code_of("sailcloth_bandage"), 2) + me.health = 30 + me.materials[Materials.DRIFTWOOD] = 30 + var other: SimEntity = _player(HOME + Vector3(20.0, 0.0, 0.0), FACING_X * 3) + _arm(other) + other.selected = 1 + _ready_world() + _pin(brain) + var states: Dictionary = {} + var start: Vector3 = me.position + _run(8 * TICKS, func() -> bool: + states[brain.state] = true + return false) + assert_true(states.has("cover"), "it took cover: %s" % [states.keys()]) + assert_false(states.has("retreat"), "rather than running: %s" % [states.keys()]) + assert_eq(_pieces_of(me.id, StructureSet.WALL).size(), 1) + assert_true(me.health > 30, "and healed (%d)" % me.health) + assert_true(me.position.distance_to(start) < 3.0, "where it stood (moved %.1f m)" % me.position.distance_to(start)) + + +## Flat ground with a block 8 m high from x = 212 m: a cliff to its top. +func _cliff() -> SimGround: + var ground: SimGround = SimGround.flat(FLOOR) + ground.add_solid(AABB(Vector3(212.0, 0.0, 180.0), Vector3(30.0, FLOOR + 8.0, 40.0))) + return ground + + +func test_a_bot_ramps_up_a_cliff_to_an_opponent_on_top() -> void: + _setup(_cliff()) + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + me.materials[Materials.DRIFTWOOD] = 100 + var above: SimEntity = _player(Vector3(222.0, FLOOR + 8.0, 202.0), FACING_X * 3) + _arm(above) + above.selected = 1 + _ready_world() + _pin(brain) + # The one on top keeps shooting down at it. + hits[me.id] = above.id + var states: Dictionary = {} + _run(12 * TICKS, func() -> bool: + states[brain.state] = true + return me.position.y >= FLOOR + 7.5 and me.on_ground) + var ramps: Array[StructureSet.Piece] = _pieces_of(me.id, StructureSet.RAMP) + assert_true(ramps.size() >= 2, "a staircase of ramps (%d): %s" % [ramps.size(), states.keys()]) + assert_true(ramps.size() <= Tuning.BOT_RAMP_MAX) + assert_true(me.position.y >= FLOOR + 7.5, "up on top (at %.1f m)" % me.position.y) + + +func test_a_bot_on_a_gentle_rise_walks_rather_than_ramps() -> void: + _setup() + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + me.materials[Materials.DRIFTWOOD] = 100 + # 4 m higher but 20 m off, with open ground between: no cliff. + var other: SimEntity = _player(HOME + Vector3(20.0, 4.0, 0.0), FACING_X * 3) + other.on_ground = true + _ready_world() + _pin(brain) + hits[me.id] = other.id + _run(TICKS) + assert_eq(_pieces_of(me.id, StructureSet.RAMP).size(), 0) + + +func test_a_bot_rings_itself_with_breakwaters_at_the_last_stand() -> void: + _setup() + var brain: BotBrain = _bot(HOME) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + me.materials[Materials.STONE] = 4 * StructureSet.cost_of(StructureSet.BREAKWATER) + sim.start_tide() + _ready_world() + _pin(brain) + # Make the first phase a last stand on the bots' map, with its high point where the + # bot stands, then fast forward to just before the rise. + world.phase_cells[0] = 0 + world.phase_tops[0] = me.position + assert_true(brain._ring_due(sim, world, me) == (sim.tide.ticks_until_rise(sim.tick) <= roundi(Tuning.BOT_RING_SECONDS * TICKS))) + var rise_in: int = sim.tide.ticks_until_rise(sim.tick) - roundi(Tuning.BOT_RING_SECONDS * TICKS) + 1 + sim.tick += maxi(rise_in, 0) + _run(TICKS, func() -> bool: return _pieces_of(me.id, StructureSet.BREAKWATER).size() >= 4) + var ring: Array[StructureSet.Piece] = _pieces_of(me.id, StructureSet.BREAKWATER) + assert_eq(ring.size(), 4, "a breakwater on each side of its cell") + var g: float = StructureSet.cell_size() + var cell: Vector2i = Vector2i(floori(me.position.x / g), floori(me.position.z / g)) + for piece: StructureSet.Piece in ring: + var centre: Vector3 = piece.boxes[0].get_center() + assert_true(absf(centre.x - (cell.x + 0.5) * g) <= g * 0.5 + 0.5 and absf(centre.z - (cell.y + 0.5) * g) <= g * 0.5 + 0.5, "round its own cell") + assert_eq(me.materials[Materials.STONE], 0) + _run(1) + assert_eq(brain.stats["breakwaters"], 4) + + +func test_fights_are_counted_with_and_without_building() -> void: + _setup() + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + var other: SimEntity = _player(HOME + Vector3(15.0, 0.0, 0.0), FACING_X * 3) + _ready_world() + _pin(brain) + _run(15 * TICKS, func() -> bool: return other.eliminated) + assert_true(other.eliminated, "it won the fight") + _run(roundi((Tuning.BOT_FIGHT_GAP_SECONDS + 1.0) * TICKS)) + assert_eq(brain.stats["fights"], 1) + assert_eq(brain.stats["fights_built"], 0) + assert_eq(brain.stats["fights_lost"], 0) + + +func test_the_batch_summary_compares_builders_with_the_control_group() -> void: + var results: Array[Dictionary] = [] + for builds: Array in [[4, 1, 2, 1], [6, 1, 1, 0]]: + var build: Dictionary = BotBatch._empty_group() + build["bots"] = 12 + build["places"] = [1, 3] + build["wins"] = 1 + build["fights"] = builds[0] + build["fights_built"] = builds[1] + build["fights_lost"] = builds[2] + build["fights_built_lost"] = builds[3] + build["fights_stocked"] = 2 + build["fights_stocked_built"] = builds[1] + var control: Dictionary = BotBatch._empty_group() + control["bots"] = 12 + control["places"] = [2, 4] + control["fights"] = 5 + control["fights_lost"] = 3 + results.append({"building": {"build": build, "control": control}}) + var total: Dictionary = BotBatch.summarise_building(results) + assert_eq(total["build"]["fights"], 10) + assert_near(total["build"]["fight_share_built"], 0.2, 0.0001) + assert_near(total["build"]["stocked_share"], 0.4, 0.0001) + assert_near(total["build"]["stocked_share_built"], 0.5, 0.0001) + assert_near(total["build"]["fight_loss"], 0.3, 0.0001) + assert_near(total["build"]["loss_when_built"], 0.5, 0.0001) + assert_near(total["build"]["loss_when_not"], 0.25, 0.0001) + assert_near(total["build"]["mean_place"], 2.0, 0.0001) + assert_near(total["control"]["mean_place"], 3.0, 0.0001) + assert_near(total["control"]["fight_loss"], 0.6, 0.0001) + assert_eq(total["build"]["wins"], 2) + assert_false(total["build"].has("places")) + + +func test_a_bot_the_tide_would_catch_rings_itself_in_and_stays_dry() -> void: + _setup() + var brain: BotBrain = _bot(HOME) + var me: SimEntity = sim.entities.find(brain.player_id) + _arm(me) + me.materials[Materials.STONE] = 4 * StructureSet.cost_of(StructureSet.BREAKWATER) + # The tide's late phases: a rise to 3 m over the bot's ground, starting now, with + # dry ground 300 m off, far too far to walk before the water arrives. + var phases: int = Tide.phase_count() + var levels: PackedFloat32Array = PackedFloat32Array() + for index: int in phases: + levels.append(FLOOR + 3.0 if index >= phases - Tuning.BOT_STRAND_PHASES else FLOOR - 10.0) + sim.start_tide() + sim.tide = Tide.new(sim.tick - _rise_start_of(phases - Tuning.BOT_STRAND_PHASES), levels) + _ready_world() + _pin(brain) + brain._fleeing = true + brain._flee_goal = HOME + Vector3(300.0, 0.0, 0.0) + brain._tide_checked = sim.tick + assert_true(brain._stranded(sim, world, me), "it can't outrun the water") + var states: Dictionary = {} + _run(2 * TICKS, func() -> bool: + states[brain.state] = true + return false) + assert_eq(_pieces_of(me.id, StructureSet.BREAKWATER).size(), 4, "it rang its cell: %s" % [states.keys()]) + var at: Vector3 = me.position + _run(20 * TICKS) + assert_eq(brain.state, "breakwater", "and stays in it") + assert_true(me.position.distance_to(at) < 1.0, "without wandering off (%.1f m)" % me.position.distance_to(at)) + + +## Ticks from the tide's start to the start of phase `index`'s rise. +static func _rise_start_of(index: int) -> int: + var at: int = 0 + for phase: int in index + 1: + at += roundi(float(Tuning.TIDE_PHASES[phase]["hold"]) * TICKS) + if phase < index: + at += maxi(roundi(float(Tuning.TIDE_PHASES[phase]["rise"]) * TICKS), 1) + return at diff --git a/test/bots/test_bot_building.gd.uid b/test/bots/test_bot_building.gd.uid new file mode 100644 index 0000000..b107e0b --- /dev/null +++ b/test/bots/test_bot_building.gd.uid @@ -0,0 +1 @@ +uid://dy6flf2fftvmq diff --git a/tools/bot_builds.gd b/tools/bot_builds.gd new file mode 100644 index 0000000..001bcb2 --- /dev/null +++ b/tools/bot_builds.gd @@ -0,0 +1,86 @@ +## M6: measures whether bots that build do better. Plays bot-only matches in which +## every other bot gathers and builds and the rest don't (BotBatch's split_builders), +## and compares the two groups. +## +## tools/godot.sh --script res://tools/bot_builds.gd -- [--seeds=20] [--first=501] [--out=FILE] +## +## Prints one JSON line per match and a summary, and checks: of the fights builders +## begin with the materials for a wall, they place a piece in at least STOCKED_SHARE; +## they lose a smaller share of the fights they built in than of the rest; their mean +## place is no worse than the control group's; and every match's p99 tick is within +## the budget. Exits 1 if a check fails. +## +## How the two groups' fight losses and wins compare overall is printed but not judged: +## over 40 matches (2026-11-15) they came out level, so building helps a bot in the +## fights it builds in without, yet, making builders win more. See docs/BALANCE.md. +extends SceneTree + +## Fewer matches than this are reported but not judged. +const MIN_MATCHES: int = 20 +## Builders must place a piece in at least this share of the fights they begin with +## enough for a wall. (Most fights are in the landing, before anyone has gathered, so +## the share of all fights is reported but not judged.) +const STOCKED_SHARE: float = 0.35 + + +func _init() -> void: + var count: int = 40 + var first: int = 501 + var out_path: String = "" + for arg: String in OS.get_cmdline_user_args(): + if arg.begins_with("--seeds="): + count = arg.trim_prefix("--seeds=").to_int() + elif arg.begins_with("--first="): + first = arg.trim_prefix("--first=").to_int() + elif arg.begins_with("--out="): + out_path = arg.trim_prefix("--out=") + var out: FileAccess = FileAccess.open(out_path, FileAccess.WRITE) if out_path != "" else null + var results: Array[Dictionary] = [] + var worst_p99: int = 0 + var over_budget: int = 0 + var unfinished: int = 0 + for seed_value: int in range(first, first + count): + var result: Dictionary = BotBatch.run_match(seed_value, Tuning.SOLO_PLAYERS, -1, true) + results.append(result) + worst_p99 = maxi(worst_p99, int(result["timing"]["p99_us"])) + over_budget += int(result["timing"]["over_budget"]) + unfinished += 0 if result["finished"] and result["winner"] != 0 else 1 + var line: String = JSON.stringify({ + "seed": seed_value, "ticks": result["ticks"], "winner": result["winner"], + "p99_us": result["timing"]["p99_us"], "building": result["building"], + }) + print(line) + if out != null: + out.store_line(line) + out.flush() + var total: Dictionary = BotBatch.summarise_building(results) + print(JSON.stringify({"summary": total, "worst_p99_us": worst_p99, "over_budget": over_budget})) + var build: Dictionary = total["build"] + var control: Dictionary = total["control"] + print("bot-builds: builders placed %d pieces (%d walls, %d ramps, %d breakwaters) and gathered %d" % [build["pieces"], build["walls"], build["ramps"], build["breakwaters"], build["harvested"]]) + print("bot-builds: builders began %.1f%% of their fights with the materials for a wall, and built in %.1f%% of those" % [build["stocked_share"] * 100.0, build["stocked_share_built"] * 100.0]) + print("bot-builds: builders built in %.1f%% of %d fights and lost %.1f%% of them (%.1f%% when they built, %.1f%% when not); control lost %.1f%% of %d" % [ + build["fight_share_built"] * 100.0, build["fights"], build["fight_loss"] * 100.0, + build["loss_when_built"] * 100.0, build["loss_when_not"] * 100.0, + control["fight_loss"] * 100.0, control["fights"]]) + print("bot-builds: mean place builders %.2f, control %.2f; wins builders %d, control %d; worst p99 tick %d us" % [ + build["mean_place"], control["mean_place"], build["wins"], control["wins"], worst_p99]) + var problems: Array[String] = [] + if unfinished > 0: + problems.append("%d of %d matches didn't finish with a winner" % [unfinished, count]) + if worst_p99 > ServerLoop.TICK_BUDGET_USEC: + problems.append("a match's p99 tick was %d us, over the %d us budget" % [worst_p99, ServerLoop.TICK_BUDGET_USEC]) + if count < MIN_MATCHES: + print("bot-builds: %d matches are too few to judge (needs %d); only completion and timing were checked" % [count, MIN_MATCHES]) + else: + if build["stocked_share_built"] < STOCKED_SHARE: + problems.append("builders built in only %.1f%% of the fights they could afford to (needs %.0f%%)" % [build["stocked_share_built"] * 100.0, STOCKED_SHARE * 100.0]) + if build["loss_when_built"] >= build["loss_when_not"]: + problems.append("builders lost %.1f%% of the fights they built in, no fewer than the %.1f%% they didn't" % [build["loss_when_built"] * 100.0, build["loss_when_not"] * 100.0]) + if build["mean_place"] > control["mean_place"]: + problems.append("builders' mean place %.2f is worse than control's %.2f" % [build["mean_place"], control["mean_place"]]) + for problem: String in problems: + print("bot-builds FAIL: %s" % problem) + if problems.is_empty(): + print("bot-builds: ok (%d matches)" % count) + quit(0 if problems.is_empty() else 1) diff --git a/tools/bot_builds.gd.uid b/tools/bot_builds.gd.uid new file mode 100644 index 0000000..66878a7 --- /dev/null +++ b/tools/bot_builds.gd.uid @@ -0,0 +1 @@ +uid://js4sgnk27y5n diff --git a/tools/bot_builds.sh b/tools/bot_builds.sh new file mode 100755 index 0000000..feb394e --- /dev/null +++ b/tools/bot_builds.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# M6: plays bot-only matches in which every other bot builds, under nice, and compares +# builders with the rest. One JSON line per match goes to build/bot_builds/matches.jsonl. +# See tools/bot_builds.gd for what is checked. +# +# tools/bot_builds.sh seeds 501 to 540 +# BOT_BUILDS_SEEDS=5 BOT_BUILDS_FIRST=40 tools/bot_builds.sh +set -uo pipefail +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +count="${BOT_BUILDS_SEEDS:-40}" +first="${BOT_BUILDS_FIRST:-501}" +out="build/bot_builds" +mkdir -p "$out" +raw="$(nice -n 15 tools/godot.sh --script res://tools/bot_builds.gd -- --seeds="$count" --first="$first" --out="$PWD/$out/matches.jsonl" 2>&1)" +code=$? +grep -E '^bot-builds' <<<"$raw" +if grep -qE 'SCRIPT ERROR|^ERROR' <<<"$raw"; then + echo "bot-builds FAIL: engine or script errors" + grep -E -A3 'SCRIPT ERROR|^ERROR' <<<"$raw" | head -n 20 + code=1 +fi +if [[ "$code" -ne 0 ]] && ! grep -q '^bot-builds' <<<"$raw"; then + echo "bot-builds FAIL: the run exited $code" + tail -n 15 <<<"$raw" +fi +exit "$code"