## 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 var _rng: RandomNumberGenerator = null var _rng_seed: int = 0 ## This bot's own random stream, seeded from the match seed and its player ID. Bots ## never draw from the simulation's streams: a bot is a player, and nothing a player ## thinks may change the simulation's state, or a replay of the same commands with no ## bots thinking would end up with a different state hash. func stream(sim: Simulation) -> RandomNumberGenerator: if _rng == null or _rng_seed != sim.rng.match_seed: _rng = RandomNumberGenerator.new() _rng_seed = sim.rng.match_seed _rng.seed = _rng_seed ^ SimRng._hash_name("bot:%d" % player_id) return _rng 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 bot's own ## 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 = stream(sim) 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