Files
claudeandClaude Opus 5.5 cc652de46e M4: Bot navigation
NavGrid is the map bots plan on: a point per 2 m terrain sample, solid on cliffs and
where solids stand between step and head height, and costlier where the water is
deeper than wading. Path search uses Godot's AStarGrid2D. The grid syncs every tick:
new solids (walls, pieces) block points at once, removed ones are found by a rolling
scan, and points turn wet as the tide and flood reach them. Each change is stamped
with its tick.

NavBot follows a path with ordinary PlayerCommands: it steers at waypoints, sprints
on long dry stretches, jumps when stalled, and marks a point blocked after repeated
stalls. Every half second it checks its remaining path against the change stamps and
plans again if water or a wall now crosses it. One path is planned per server tick
across all bots.

The server builds the grid while the lobby waits and its bots now roam between
random reachable places, replacing the placeholder wander bot.

Tests: 150 of 150 random reachable targets reached on 50 islands in the sweep; the
test runs 2 per island and needs 95%. New walls and rising water both cause a new
path around them; a broken wall opens the grid again; with no dry way round, the bot
swims. 23 roaming bots on an island: p99 tick 1.6 ms against the 12 ms budget.

Closes #27

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 23:38:36 -05:00

58 lines
2.4 KiB
GDScript

## Walks a bot to random reachable targets on many islands and counts how many it
## reaches. Shared by the test and `make nav-sweep`.
class_name NavSweep
extends RefCounted
## Results: {"trials", "reached", "failures": [{"seed", "from", "to", "status", "left"}]}
static func run(seeds: Array[int], targets_per_seed: int) -> Dictionary:
var trials: int = 0
var reached: int = 0
var failures: Array[Dictionary] = []
for seed_value: int in seeds:
var sim: Simulation = Simulation.new(seed_value)
var grid: NavGrid = NavGrid.build(sim)
var player_id: int = sim.add_player()
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = seed_value
for _target: int in targets_per_seed:
var player: SimEntity = sim.entities.find(player_id)
var goal: Vector3 = _reachable_goal(grid, player.position, rng)
if goal.y <= -999.0:
continue
trials += 1
var bot: NavBot = NavBot.new(player_id)
bot.set_goal(goal)
var start: Vector3 = player.position
var limit: int = roundi((start.distance_to(goal) * 2.5 / Tuning.WALK_SPEED + 15.0) * Tuning.TICK_RATE_HZ)
for _tick: int in limit:
grid.sync(sim)
sim.submit_command(player_id, bot.think(sim, grid))
sim.step()
if bot.status != NavBot.MOVING:
break
var left: float = Vector2(goal.x - player.position.x, goal.z - player.position.z).length()
if bot.status == NavBot.ARRIVED:
reached += 1
else:
failures.append({"seed": seed_value, "from": start, "to": goal, "status": bot.status_name(), "left": left, "plans": bot.plans})
# Start the next trial from wherever this one ended, back on its feet.
player.health = Tuning.PLAYER_MAX_HEALTH
Water.reset(player)
return {"trials": trials, "reached": reached, "failures": failures}
## A dry open point 40 to 160 m away with a complete path from `from`, or y = -1000.
static func _reachable_goal(grid: NavGrid, from: Vector3, rng: RandomNumberGenerator) -> Vector3:
for _try: int in 40:
var angle: float = rng.randf() * TAU
var reach: float = rng.randf_range(40.0, 160.0)
var point: Vector2i = grid.point_of(from + Vector3(cos(angle), 0.0, sin(angle)) * reach)
if not grid.is_open(point) or grid.wet[grid.index_of(point)] != 0:
continue
var goal: Vector3 = grid.position_of(point)
if goal.y <= Tuning.ISLAND_BEACH_TOP:
continue
if not grid.find_path(from, goal).is_empty():
return goal
return Vector3(0.0, -1000.0, 0.0)