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>
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
## A bot body that walks to a goal: it plans a path on the shared NavGrid and steers
|
||||
## along it with the same PlayerCommands a human sends. It plans again when its path
|
||||
## floods or a wall goes up across it, jumps when it's stuck, and gives up on a point it
|
||||
## can't get through after a few tries. M5's brains choose the goals; on its own, with
|
||||
## `roam` set, it wanders between random reachable places.
|
||||
class_name NavBot
|
||||
extends RefCounted
|
||||
|
||||
const IDLE: int = 0
|
||||
const MOVING: int = 1
|
||||
const ARRIVED: int = 2
|
||||
const UNREACHABLE: int = 3
|
||||
const STATUS_NAMES: Array[String] = ["idle", "moving", "arrived", "unreachable"]
|
||||
## Roaming goals are this far away at most, in metres.
|
||||
const ROAM_RANGE: float = 150.0
|
||||
## Tries at a random roaming goal before waiting for the next tick.
|
||||
const ROAM_TRIES: int = 8
|
||||
|
||||
var player_id: int = 0
|
||||
var status: int = IDLE
|
||||
var goal: Vector3 = Vector3.ZERO
|
||||
## Within this horizontal distance of the goal counts as there.
|
||||
var arrive_radius: float = 1.5
|
||||
## Pick a new random goal whenever idle, arrived or stuck for good.
|
||||
var roam: bool = false
|
||||
## The planned grid points, and the one being walked to.
|
||||
var path: Array[Vector2i] = []
|
||||
var path_index: int = 0
|
||||
## How many times a path has been planned, for tests and logs.
|
||||
var plans: int = 0
|
||||
|
||||
var _needs_plan: bool = false
|
||||
var _planned_tick: int = -1
|
||||
var _best_distance: float = INF
|
||||
var _stuck_ticks: int = 0
|
||||
var _stuck_tries: int = 0
|
||||
var _jump_next: bool = false
|
||||
var _leap_tick: int = -1
|
||||
|
||||
|
||||
func _init(id: int) -> void:
|
||||
player_id = id
|
||||
|
||||
|
||||
func set_goal(target: Vector3) -> void:
|
||||
goal = target
|
||||
status = MOVING
|
||||
path.clear()
|
||||
path_index = 0
|
||||
_needs_plan = true
|
||||
_reset_stuck()
|
||||
|
||||
|
||||
func clear_goal() -> void:
|
||||
status = IDLE
|
||||
path.clear()
|
||||
_needs_plan = false
|
||||
|
||||
|
||||
func status_name() -> String:
|
||||
return STATUS_NAMES[status]
|
||||
|
||||
|
||||
## Returns this bot's command for `sim.tick`. The grid must be synced for this tick.
|
||||
func think(sim: Simulation, grid: NavGrid) -> PlayerCommand:
|
||||
var player: SimEntity = sim.entities.find(player_id)
|
||||
if player == null or player.eliminated:
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0)
|
||||
if roam and status != MOVING:
|
||||
_pick_roam_goal(sim, grid, player)
|
||||
if player.drop_state == Drop.ABOARD:
|
||||
return _ride(sim, player)
|
||||
if player.drop_state != Drop.NONE:
|
||||
# Gliding down: head for the goal.
|
||||
return _steer(sim, player, goal, 0)
|
||||
if status != MOVING:
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, deg_to_rad(player.yaw * 360.0 / PlayerCommand.YAW_STEPS), 0.0)
|
||||
if not _needs_plan and (sim.tick + player_id) % Tuning.BOT_PATH_CHECK_TICKS == 0:
|
||||
if not grid.path_still_valid(path, path_index, _planned_tick):
|
||||
_needs_plan = true
|
||||
if _needs_plan and grid.can_plan():
|
||||
_plan(sim, grid, player)
|
||||
if status != MOVING:
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0)
|
||||
var horizontal: Vector2 = Vector2(goal.x - player.position.x, goal.z - player.position.z)
|
||||
if horizontal.length() <= arrive_radius:
|
||||
status = ARRIVED
|
||||
path.clear()
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, atan2(horizontal.x, horizontal.y), 0.0)
|
||||
var target: Vector3 = goal
|
||||
if not path.is_empty():
|
||||
_advance_waypoints(grid, player)
|
||||
target = goal if path_index >= path.size() - 1 else grid.position_of(path[path_index])
|
||||
_check_stuck(sim, grid, player, target)
|
||||
var buttons: int = 0
|
||||
if _jump_next:
|
||||
buttons |= PlayerCommand.BUTTON_JUMP
|
||||
_jump_next = false
|
||||
if not player.swimming and horizontal.length() > 8.0:
|
||||
buttons |= PlayerCommand.BUTTON_SPRINT
|
||||
return _steer(sim, player, target, buttons)
|
||||
|
||||
|
||||
func _plan(sim: Simulation, grid: NavGrid, player: SimEntity) -> void:
|
||||
_needs_plan = false
|
||||
_planned_tick = sim.tick
|
||||
plans += 1
|
||||
path = grid.find_path(player.position, goal)
|
||||
path_index = 0
|
||||
_reset_stuck()
|
||||
if path.is_empty():
|
||||
status = UNREACHABLE
|
||||
|
||||
|
||||
## Skips waypoints already reached, or that the bot is already nearer the next one than.
|
||||
func _advance_waypoints(grid: NavGrid, player: SimEntity) -> void:
|
||||
var feet: Vector2 = Vector2(player.position.x, player.position.z)
|
||||
while path_index < path.size() - 1:
|
||||
var here: Vector3 = grid.position_of(path[path_index])
|
||||
var reached: bool = feet.distance_to(Vector2(here.x, here.z)) <= Tuning.BOT_WAYPOINT_RADIUS
|
||||
if not reached:
|
||||
break
|
||||
path_index += 1
|
||||
_reset_stuck()
|
||||
|
||||
|
||||
## Watches progress toward the current waypoint: jump when stalled, and after a few
|
||||
## stalled jumps mark the waypoint blocked and plan around it.
|
||||
func _check_stuck(sim: Simulation, grid: NavGrid, player: SimEntity, target: Vector3) -> void:
|
||||
var distance: float = Vector2(target.x - player.position.x, target.z - player.position.z).length()
|
||||
if distance < _best_distance - Tuning.BOT_STUCK_PROGRESS:
|
||||
_best_distance = distance
|
||||
_stuck_ticks = 0
|
||||
return
|
||||
_stuck_ticks += 1
|
||||
if _stuck_ticks < Tuning.BOT_STUCK_TICKS:
|
||||
return
|
||||
_stuck_ticks = 0
|
||||
_stuck_tries += 1
|
||||
_jump_next = true
|
||||
if _stuck_tries >= Tuning.BOT_STUCK_REPATHS and not path.is_empty():
|
||||
var point: Vector2i = path[mini(path_index, path.size() - 1)]
|
||||
if grid.point_of(player.position) != point:
|
||||
grid.mark_blocked(point, sim.tick)
|
||||
_needs_plan = true
|
||||
_stuck_tries = 0
|
||||
|
||||
|
||||
func _reset_stuck() -> void:
|
||||
_best_distance = INF
|
||||
_stuck_ticks = 0
|
||||
_stuck_tries = 0
|
||||
|
||||
|
||||
func _steer(sim: Simulation, player: SimEntity, target: Vector3, buttons: int) -> PlayerCommand:
|
||||
var to: Vector2 = Vector2(target.x - player.position.x, target.z - player.position.z)
|
||||
if to.length_squared() < 0.0001:
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, buttons)
|
||||
return PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), atan2(to.x, to.y), 0.0, buttons)
|
||||
|
||||
|
||||
## Aboard the barge: leap once the goal is within gliding reach, or at a time set by
|
||||
## the player ID, whichever comes first. The end of the route puts everyone off anyway.
|
||||
func _ride(sim: Simulation, player: SimEntity) -> PlayerCommand:
|
||||
if _leap_tick < 0 and sim.drop != null:
|
||||
_leap_tick = sim.drop.began_tick + Tuning.TICK_RATE_HZ * 2 + (player_id * 53) % (Tuning.TICK_RATE_HZ * 20)
|
||||
var leap: bool = sim.tick >= _leap_tick
|
||||
if status == MOVING:
|
||||
var gap: float = Vector2(goal.x - player.position.x, goal.z - player.position.z).length()
|
||||
leap = leap or gap < 0.6 * (player.position.y - goal.y)
|
||||
var buttons: int = PlayerCommand.BUTTON_JUMP if leap else 0
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, buttons)
|
||||
|
||||
|
||||
## A random open dry point within ROAM_RANGE that a path reaches, from the "bots"
|
||||
## stream. Plans the path at once, so it uses the tick's planning budget.
|
||||
func _pick_roam_goal(sim: Simulation, grid: NavGrid, player: SimEntity) -> void:
|
||||
if not grid.can_plan():
|
||||
return
|
||||
var rng: RandomNumberGenerator = sim.rng.stream("bots")
|
||||
var centre: Vector3 = player.position
|
||||
if player.drop_state == Drop.ABOARD:
|
||||
centre = Vector3(Tuning.WORLD_SIZE_METRES * 0.5, 0.0, Tuning.WORLD_SIZE_METRES * 0.5)
|
||||
for _try: int in ROAM_TRIES:
|
||||
var angle: float = rng.randf() * TAU
|
||||
var reach: float = rng.randf_range(20.0, ROAM_RANGE)
|
||||
var point: Vector2i = grid.point_of(centre + Vector3(cos(angle), 0.0, sin(angle)) * reach)
|
||||
if grid.is_open(point) and grid.wet[grid.index_of(point)] == 0:
|
||||
set_goal(grid.position_of(point))
|
||||
if player.drop_state == Drop.NONE:
|
||||
_plan(sim, grid, player)
|
||||
return
|
||||
@@ -0,0 +1 @@
|
||||
uid://b7uhmvb00i3uu
|
||||
@@ -0,0 +1,341 @@
|
||||
## The grid bots plan paths on: one point per terrain sample, 2 m apart, built from the
|
||||
## island's heightfield and solids and kept in step with the match. Points become solid
|
||||
## where the ground is a cliff or a solid stands in the way, and cost more where the
|
||||
## water is too deep to wade. Flooding, new walls and broken props change points while
|
||||
## the match runs; each change is stamped with the tick it happened, so bots can tell
|
||||
## when their path crosses something new and plan again.
|
||||
##
|
||||
## The path search itself is Godot's AStarGrid2D, which is deterministic and fast. It
|
||||
## only reads the simulation; it never changes it.
|
||||
class_name NavGrid
|
||||
extends RefCounted
|
||||
|
||||
const CELL: float = Tuning.BOT_NAV_CELL_METRES
|
||||
## How many solids to re-check for removal each tick: every solid is seen again within
|
||||
## a few seconds, without a spike.
|
||||
const SOLID_SCAN_PER_TICK: int = 256
|
||||
|
||||
## Points per side.
|
||||
var side: int = 0
|
||||
## Terrain height at each point, row-major (index = z * side + x).
|
||||
var heights: PackedFloat32Array = PackedFloat32Array()
|
||||
## 1 where the terrain itself is impassable on foot (a cliff or too steep).
|
||||
var steep: PackedByteArray = PackedByteArray()
|
||||
## How many solids block each point.
|
||||
var blocked: PackedInt32Array = PackedInt32Array()
|
||||
## 1 where the water over the point is deeper than wading depth.
|
||||
var wet: PackedByteArray = PackedByteArray()
|
||||
## The tick each point last changed, or -1.
|
||||
var changed_tick: PackedInt32Array = PackedInt32Array()
|
||||
## Paths planned so far this tick, for the per-tick limit.
|
||||
var paths_this_tick: int = 0
|
||||
var astar: AStarGrid2D = AStarGrid2D.new()
|
||||
|
||||
## Solid ID -> the points it blocks, for every solid that blocks any.
|
||||
var _solid_points: Dictionary = {}
|
||||
## How many of ground.solids have been seen.
|
||||
var _solids_seen: int = 0
|
||||
var _scan_at: int = 0
|
||||
## Point indices ordered by height, and how far up them the water has been checked.
|
||||
var _by_height: PackedInt32Array = PackedInt32Array()
|
||||
var _wet_checked: int = 0
|
||||
## Points under the sea level that the flood hadn't reached when last checked.
|
||||
var _dry_below: Dictionary = {}
|
||||
var _flooded_seen: int = -1
|
||||
var _last_tick: int = -1
|
||||
|
||||
|
||||
## Builds the grid for a simulation's ground, as it is now.
|
||||
static func build(sim: Simulation) -> NavGrid:
|
||||
var grid: NavGrid = NavGrid.new()
|
||||
grid._build(sim)
|
||||
return grid
|
||||
|
||||
|
||||
func _build(sim: Simulation) -> void:
|
||||
var ground: SimGround = sim.ground
|
||||
side = roundi(Tuning.WORLD_SIZE_METRES / CELL) + 1
|
||||
var count: int = side * side
|
||||
heights.resize(count)
|
||||
if is_equal_approx(ground.cell, CELL) and ground.samples == side:
|
||||
heights = ground.heights.duplicate()
|
||||
else:
|
||||
for z: int in side:
|
||||
for x: int in side:
|
||||
heights[z * side + x] = ground.terrain_height(x * CELL, z * CELL)
|
||||
steep.resize(count)
|
||||
steep.fill(0)
|
||||
blocked.resize(count)
|
||||
blocked.fill(0)
|
||||
wet.resize(count)
|
||||
wet.fill(0)
|
||||
changed_tick.resize(count)
|
||||
changed_tick.fill(-1)
|
||||
astar.region = Rect2i(0, 0, side, side)
|
||||
astar.cell_size = Vector2(CELL, CELL)
|
||||
astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES
|
||||
astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_OCTILE
|
||||
astar.default_estimate_heuristic = AStarGrid2D.HEURISTIC_OCTILE
|
||||
astar.update()
|
||||
_mark_steep()
|
||||
_solids_seen = 0
|
||||
_take_new_solids(ground, -1)
|
||||
_by_height = _order_by_height()
|
||||
_update_water(sim, -1)
|
||||
for index: int in count:
|
||||
if steep[index] != 0 or blocked[index] != 0 or wet[index] != 0:
|
||||
_apply(index, -1)
|
||||
|
||||
|
||||
## Brings the grid up to date with the simulation: new and removed solids and the
|
||||
## water. Call once per tick, before bots plan; repeated calls in a tick do nothing.
|
||||
func sync(sim: Simulation) -> void:
|
||||
if sim.tick == _last_tick:
|
||||
return
|
||||
_last_tick = sim.tick
|
||||
paths_this_tick = 0
|
||||
_take_new_solids(sim.ground, sim.tick)
|
||||
_scan_removed_solids(sim.ground, sim.tick)
|
||||
_update_water(sim, sim.tick)
|
||||
|
||||
|
||||
## The grid point nearest a world position.
|
||||
func point_of(position: Vector3) -> Vector2i:
|
||||
return Vector2i(clampi(roundi(position.x / CELL), 0, side - 1), clampi(roundi(position.z / CELL), 0, side - 1))
|
||||
|
||||
|
||||
func index_of(point: Vector2i) -> int:
|
||||
return point.y * side + point.x
|
||||
|
||||
|
||||
## The world position of a grid point, on the terrain.
|
||||
func position_of(point: Vector2i) -> Vector3:
|
||||
return Vector3(point.x * CELL, heights[index_of(point)], point.y * CELL)
|
||||
|
||||
|
||||
func is_open(point: Vector2i) -> bool:
|
||||
return astar.is_in_boundsv(point) and not astar.is_point_solid(point)
|
||||
|
||||
|
||||
## The open point nearest `point`, searching outward up to `radius` points, or (-1, -1).
|
||||
func nearest_open(point: Vector2i, radius: int = 4) -> Vector2i:
|
||||
if is_open(point):
|
||||
return point
|
||||
for ring: int in range(1, radius + 1):
|
||||
for dz: int in range(-ring, ring + 1):
|
||||
for dx: int in range(-ring, ring + 1):
|
||||
if maxi(absi(dx), absi(dz)) != ring:
|
||||
continue
|
||||
var candidate: Vector2i = point + Vector2i(dx, dz)
|
||||
if is_open(candidate):
|
||||
return candidate
|
||||
return Vector2i(-1, -1)
|
||||
|
||||
|
||||
## Whether this tick's planning budget has room for another path.
|
||||
func can_plan() -> bool:
|
||||
return paths_this_tick < Tuning.BOT_NAV_PATHS_PER_TICK
|
||||
|
||||
|
||||
## Plans a path of grid points from one world position to another. Empty if either end
|
||||
## has no open point nearby or no route joins them; with `partial`, the path to the
|
||||
## reachable point closest to the goal instead. Counts against the tick's budget.
|
||||
func find_path(from: Vector3, to: Vector3, partial: bool = false) -> Array[Vector2i]:
|
||||
paths_this_tick += 1
|
||||
var start: Vector2i = nearest_open(point_of(from))
|
||||
var goal: Vector2i = nearest_open(point_of(to))
|
||||
if start.x < 0 or goal.x < 0:
|
||||
return []
|
||||
return astar.get_id_path(start, goal, partial)
|
||||
|
||||
|
||||
## True if no point of `path` from `from_index` on has changed since `since_tick`.
|
||||
func path_still_valid(path: Array[Vector2i], from_index: int, since_tick: int) -> bool:
|
||||
for at: int in range(from_index, path.size()):
|
||||
var point: Vector2i = path[at]
|
||||
if changed_tick[index_of(point)] > since_tick and astar.is_point_solid(point):
|
||||
return false
|
||||
if changed_tick[index_of(point)] > since_tick and wet[index_of(point)] != 0:
|
||||
return false
|
||||
return true
|
||||
|
||||
|
||||
## Marks a point as blocked by something the grid can't see (a bot found it couldn't
|
||||
## get through). Counts as one more blocking solid.
|
||||
func mark_blocked(point: Vector2i, tick: int) -> void:
|
||||
if not astar.is_in_boundsv(point):
|
||||
return
|
||||
var index: int = index_of(point)
|
||||
blocked[index] += 1
|
||||
_apply(index, tick)
|
||||
|
||||
|
||||
func _mark_steep() -> void:
|
||||
var max_rise: float = Tuning.BOT_NAV_MAX_RISE
|
||||
var min_normal_y: float = cos(deg_to_rad(Tuning.MAX_SLOPE_DEGREES))
|
||||
for z: int in side:
|
||||
var row: int = z * side
|
||||
for x: int in side:
|
||||
var index: int = row + x
|
||||
var h: float = heights[index]
|
||||
var too_steep: bool = false
|
||||
if x + 1 < side and absf(heights[index + 1] - h) > max_rise:
|
||||
too_steep = true
|
||||
steep[index + 1] = 1
|
||||
if z + 1 < side and absf(heights[index + side] - h) > max_rise:
|
||||
too_steep = true
|
||||
steep[index + side] = 1
|
||||
if not too_steep and x > 0 and x + 1 < side and z > 0 and z + 1 < side:
|
||||
var dx: float = heights[index + 1] - heights[index - 1]
|
||||
var dz: float = heights[index + side] - heights[index - side]
|
||||
var normal_y: float = 2.0 * CELL / sqrt(dx * dx + dz * dz + 4.0 * CELL * CELL)
|
||||
too_steep = normal_y < min_normal_y
|
||||
if too_steep:
|
||||
steep[index] = 1
|
||||
|
||||
|
||||
## Points a solid blocks: those whose central square metre the solid, widened by a
|
||||
## player's radius, overlaps, where the solid stands between step height and head
|
||||
## height above the terrain.
|
||||
func _points_of_solid(box: AABB) -> PackedInt32Array:
|
||||
var points: PackedInt32Array = PackedInt32Array()
|
||||
var grow: float = Tuning.PLAYER_RADIUS + 0.5
|
||||
var low_x: int = maxi(ceili((box.position.x - grow) / CELL), 0)
|
||||
var high_x: int = mini(floori((box.end.x + grow) / CELL), side - 1)
|
||||
var low_z: int = maxi(ceili((box.position.z - grow) / CELL), 0)
|
||||
var high_z: int = mini(floori((box.end.z + grow) / CELL), side - 1)
|
||||
for z: int in range(low_z, high_z + 1):
|
||||
for x: int in range(low_x, high_x + 1):
|
||||
var index: int = z * side + x
|
||||
var h: float = heights[index]
|
||||
if box.end.y > h + Tuning.STEP_HEIGHT and box.position.y < h + Tuning.PLAYER_HEIGHT:
|
||||
points.append(index)
|
||||
return points
|
||||
|
||||
|
||||
func _take_new_solids(ground: SimGround, tick: int) -> void:
|
||||
while _solids_seen < ground.solids.size():
|
||||
var id: int = _solids_seen
|
||||
_solids_seen += 1
|
||||
var box: AABB = ground.solids[id]
|
||||
if box == SimGround.REMOVED:
|
||||
continue
|
||||
var points: PackedInt32Array = _points_of_solid(box)
|
||||
if points.is_empty():
|
||||
continue
|
||||
_solid_points[id] = points
|
||||
for index: int in points:
|
||||
blocked[index] += 1
|
||||
if tick >= 0:
|
||||
_apply(index, tick)
|
||||
|
||||
|
||||
## Re-checks a slice of the blocking solids each tick and frees the points of any that
|
||||
## are gone.
|
||||
func _scan_removed_solids(ground: SimGround, tick: int) -> void:
|
||||
var ids: Array = _solid_points.keys()
|
||||
if ids.is_empty():
|
||||
return
|
||||
var count: int = mini(SOLID_SCAN_PER_TICK, ids.size())
|
||||
for _step: int in count:
|
||||
_scan_at = _scan_at % ids.size()
|
||||
var id: int = ids[_scan_at]
|
||||
_scan_at += 1
|
||||
if ground.solids[id] != SimGround.REMOVED:
|
||||
continue
|
||||
for index: int in _solid_points[id]:
|
||||
blocked[index] -= 1
|
||||
_apply(index, tick)
|
||||
_solid_points.erase(id)
|
||||
|
||||
|
||||
## Removes a solid's blocking at once, rather than waiting for the scan. For callers
|
||||
## that know a solid just went (a broken wall in front of a bot).
|
||||
func solid_removed(id: int, tick: int) -> void:
|
||||
if not _solid_points.has(id):
|
||||
return
|
||||
for index: int in _solid_points[id]:
|
||||
blocked[index] -= 1
|
||||
_apply(index, tick)
|
||||
_solid_points.erase(id)
|
||||
|
||||
|
||||
func _order_by_height() -> PackedInt32Array:
|
||||
# A bucket sort on 5 cm steps: the order within a bucket doesn't matter, because
|
||||
# water is checked against the bucket's whole range a tick at most early.
|
||||
var low: float = INF
|
||||
var high: float = -INF
|
||||
for h: float in heights:
|
||||
low = minf(low, h)
|
||||
high = maxf(high, h)
|
||||
var step: float = 0.05
|
||||
var buckets: int = floori((high - low) / step) + 1
|
||||
var counts: PackedInt32Array = PackedInt32Array()
|
||||
counts.resize(buckets + 1)
|
||||
counts.fill(0)
|
||||
var bucket_of: PackedInt32Array = PackedInt32Array()
|
||||
bucket_of.resize(heights.size())
|
||||
for index: int in heights.size():
|
||||
var bucket: int = floori((heights[index] - low) / step)
|
||||
bucket_of[index] = bucket
|
||||
counts[bucket + 1] += 1
|
||||
for bucket: int in buckets:
|
||||
counts[bucket + 1] += counts[bucket]
|
||||
var order: PackedInt32Array = PackedInt32Array()
|
||||
order.resize(heights.size())
|
||||
for index: int in heights.size():
|
||||
var bucket: int = bucket_of[index]
|
||||
order[counts[bucket]] = index
|
||||
counts[bucket] += 1
|
||||
return order
|
||||
|
||||
|
||||
## Marks points wet as the sea rises over them: wherever the flood reaches and the
|
||||
## water is deeper than wading depth, which slows a walker and soon means swimming. Points the flood doesn't reach yet (behind a
|
||||
## breakwater, in a basin) are re-checked whenever the flood spreads.
|
||||
func _update_water(sim: Simulation, tick: int) -> void:
|
||||
var limit: float = sim.sea_level - Tuning.WADE_DEPTH
|
||||
while _wet_checked < _by_height.size() and heights[_by_height[_wet_checked]] < limit:
|
||||
var index: int = _by_height[_wet_checked]
|
||||
_wet_checked += 1
|
||||
if _flood_reaches(sim, index):
|
||||
_set_wet(index, tick)
|
||||
else:
|
||||
_dry_below[index] = true
|
||||
if sim.flood == null or _dry_below.is_empty():
|
||||
return
|
||||
var flooded: int = sim.flood.flooded_count() if tick % Tuning.TICK_RATE_HZ == 0 or tick < 0 else _flooded_seen
|
||||
if flooded == _flooded_seen:
|
||||
return
|
||||
_flooded_seen = flooded
|
||||
for index: int in _dry_below.keys():
|
||||
if _flood_reaches(sim, index):
|
||||
_dry_below.erase(index)
|
||||
_set_wet(index, tick)
|
||||
|
||||
|
||||
func _flood_reaches(sim: Simulation, index: int) -> bool:
|
||||
if sim.flood == null:
|
||||
return true
|
||||
return sim.flood.is_flooded((index % side) * CELL, (index / side) * CELL)
|
||||
|
||||
|
||||
func _set_wet(index: int, tick: int) -> void:
|
||||
if wet[index] != 0:
|
||||
return
|
||||
wet[index] = 1
|
||||
if tick >= 0:
|
||||
_apply(index, tick)
|
||||
|
||||
|
||||
## Pushes a point's state into the path search and stamps the change.
|
||||
func _apply(index: int, tick: int) -> void:
|
||||
var point: Vector2i = Vector2i(index % side, index / side)
|
||||
var solid: bool = blocked[index] > 0 or (steep[index] != 0 and wet[index] == 0)
|
||||
if solid != astar.is_point_solid(point):
|
||||
astar.set_point_solid(point, solid)
|
||||
var weight: float = Tuning.BOT_NAV_WATER_WEIGHT if wet[index] != 0 else 1.0
|
||||
if not is_equal_approx(weight, astar.get_point_weight_scale(point)):
|
||||
astar.set_point_weight_scale(point, weight)
|
||||
if tick >= 0:
|
||||
changed_tick[index] = tick
|
||||
@@ -0,0 +1 @@
|
||||
uid://v6gxn1p82fkf
|
||||
@@ -1,33 +0,0 @@
|
||||
## A placeholder bot that walks in a seeded direction and turns now and then.
|
||||
## It exists so headless servers and smoke runs carry realistic load before the real
|
||||
## bot brains arrive in v0.4. Like every bot, it only produces PlayerCommands.
|
||||
class_name WanderBot
|
||||
extends RefCounted
|
||||
|
||||
## How often, in ticks, the bot may pick a new heading.
|
||||
const TURN_INTERVAL_TICKS: int = 45
|
||||
|
||||
var player_id: int = 0
|
||||
|
||||
var _yaw: float = 0.0
|
||||
var _sprint: bool = false
|
||||
|
||||
|
||||
func _init(id: int) -> void:
|
||||
player_id = id
|
||||
|
||||
|
||||
## Returns this bot's command for `sim.tick`. Draws only from the "bots" stream.
|
||||
func think(sim: Simulation) -> PlayerCommand:
|
||||
var rng: RandomNumberGenerator = sim.rng.stream("bots")
|
||||
if sim.tick % TURN_INTERVAL_TICKS == 0:
|
||||
_yaw = rng.randf() * TAU
|
||||
_sprint = rng.randf() < 0.3
|
||||
var buttons: int = PlayerCommand.BUTTON_SPRINT if _sprint else 0
|
||||
# Leap from the barge at a time fixed by the player ID, between 2 and 22 seconds in.
|
||||
var player: SimEntity = sim.entities.find(player_id)
|
||||
if player != null and player.drop_state == Drop.ABOARD and sim.drop != null:
|
||||
var leap_tick: int = sim.drop.began_tick + Tuning.TICK_RATE_HZ * 2 + (player_id * 53) % (Tuning.TICK_RATE_HZ * 20)
|
||||
if sim.tick >= leap_tick:
|
||||
buttons = PlayerCommand.BUTTON_JUMP
|
||||
return PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), _yaw, 0.0, buttons)
|
||||
@@ -1 +0,0 @@
|
||||
uid://cfc5ckk0du602
|
||||
@@ -14,7 +14,9 @@ var sim: Simulation:
|
||||
return match_flow.sim
|
||||
## Entity IDs of everyone in the current match, humans first.
|
||||
var player_ids: Array[int] = []
|
||||
var bots: Array[WanderBot] = []
|
||||
var bots: Array[NavBot] = []
|
||||
## The current or next match's navigation grid, built while the lobby waits.
|
||||
var nav: NavGrid = null
|
||||
## Wall-clock duration of every tick run, in microseconds.
|
||||
var tick_usec: PackedInt64Array = PackedInt64Array()
|
||||
## Server ticks run, across every match.
|
||||
@@ -32,6 +34,7 @@ func _init(match_seed: int, human_players: int, bot_count: int, rules: MatchRule
|
||||
# With only bots there's nobody to wait for, so the lobby starts with no one in it.
|
||||
rules.min_players = 0 if human_players == 0 and bot_count > 0 else 1
|
||||
match_flow = Match.new(match_seed, rules)
|
||||
nav = NavGrid.build(match_flow.sim)
|
||||
for handle: int in range(1, human_players + 1):
|
||||
match_flow.join(handle)
|
||||
if rules.lobby_ticks == 0 and rules.countdown_ticks == 0:
|
||||
@@ -43,8 +46,9 @@ func _init(match_seed: int, human_players: int, bot_count: int, rules: MatchRule
|
||||
func run_tick() -> void:
|
||||
var started: int = Time.get_ticks_usec()
|
||||
if match_flow.in_match():
|
||||
for bot: WanderBot in bots:
|
||||
sim.submit_command(bot.player_id, bot.think(sim))
|
||||
nav.sync(sim)
|
||||
for bot: NavBot in bots:
|
||||
sim.submit_command(bot.player_id, bot.think(sim, nav))
|
||||
_advance()
|
||||
ticks += 1
|
||||
tick_usec.append(Time.get_ticks_usec() - started)
|
||||
@@ -58,6 +62,8 @@ func _advance() -> void:
|
||||
elif event["type"] == "phase" and event["to"] == "lobby":
|
||||
player_ids.clear()
|
||||
bots.clear()
|
||||
if event["from"] == "results":
|
||||
nav = NavGrid.build(match_flow.sim)
|
||||
|
||||
|
||||
func _track_players() -> void:
|
||||
@@ -67,7 +73,9 @@ func _track_players() -> void:
|
||||
var entity_id: int = match_flow.entity_of[handle]
|
||||
player_ids.append(entity_id)
|
||||
if match_flow.bot_handles.has(handle):
|
||||
bots.append(WanderBot.new(entity_id))
|
||||
var bot: NavBot = NavBot.new(entity_id)
|
||||
bot.roam = true
|
||||
bots.append(bot)
|
||||
|
||||
|
||||
## Timing statistics over every tick run so far, in microseconds.
|
||||
|
||||
@@ -203,6 +203,26 @@ const TIDE_SUDDEN_DEATH_DPS: int = 5
|
||||
## line up with cell edges.
|
||||
const FLOOD_CELL_METRES: float = 4.0
|
||||
|
||||
# Bot navigation. Bots path over a grid of terrain samples and steer along it with the
|
||||
# same commands players send.
|
||||
## Spacing of the navigation grid, in metres. Matches the terrain heightfield.
|
||||
const BOT_NAV_CELL_METRES: float = 2.0
|
||||
## Neighbouring nav points further apart in height than this are a cliff, not a slope.
|
||||
const BOT_NAV_MAX_RISE: float = 1.9
|
||||
## Path cost multiplier for water too deep to wade: bots swim only when they must.
|
||||
const BOT_NAV_WATER_WEIGHT: float = 8.0
|
||||
## Most paths the shared grid plans in one server tick; other requests wait their turn.
|
||||
const BOT_NAV_PATHS_PER_TICK: int = 1
|
||||
## A bot moves on to the next waypoint within this horizontal distance of it.
|
||||
const BOT_WAYPOINT_RADIUS: float = 1.3
|
||||
## A bot that gains less than this toward its waypoint in BOT_STUCK_TICKS jumps, and
|
||||
## after BOT_STUCK_REPATHS tries it marks the waypoint blocked and plans again.
|
||||
const BOT_STUCK_PROGRESS: float = 0.5
|
||||
const BOT_STUCK_TICKS: int = 20
|
||||
const BOT_STUCK_REPATHS: int = 3
|
||||
## Each bot re-checks its remaining path against grid changes this often, in ticks.
|
||||
const BOT_PATH_CHECK_TICKS: int = 15
|
||||
|
||||
# Building
|
||||
const GRID_CELL_METRES: float = 4.0
|
||||
## The most of each material a player can carry.
|
||||
|
||||
Reference in New Issue
Block a user