## A stand-in for a person at the controls, for whole-match runs with no network and ## nobody watching. It plays a human seat through the same door as everyone else: one ## PlayerCommand a tick. ## ## It drops, loots, fights and keeps ahead of the tide as a normal bot does, and adds ## what bots don't do yet: once it has a weapon and nothing more pressing, it cuts ## driftwood with the boathook and builds a breakwater and a wall where it stands. So ## one scripted match exercises the drop, loot, combat, harvesting, building and the ## tide from a player's seat. class_name ScriptedPlayer extends BotBrain const WAIT: int = 0 const HARVEST: int = 1 const BUILD: int = 2 const DONE: int = 3 const STEP_NAMES: Array[String] = ["wait", "harvest", "build", "done"] ## Driftwood to gather before building: a breakwater and a wall, and a swing to spare. const WANTED_DRIFTWOOD: int = 45 ## How far to look for something to cut, in metres. const SEARCH_RADIUS: float = 45.0 ## Starts the script this long after landing even with no weapon found. const UNARMED_WAIT_SECONDS: float = 45.0 ## Gives up on one prop, and on the whole script, after this long. const PROP_TIMEOUT_SECONDS: float = 25.0 const SCRIPT_TIMEOUT_SECONDS: float = 150.0 ## Inside this distance the script walks straight at the prop instead of path-finding. const DIRECT_METRES: float = 6.0 var step: int = WAIT ## Driftwood gained and pieces placed by the script so far. var harvested: int = 0 var built: int = 0 var _landed_tick: int = -1 var _script_since: int = 0 var _driving: bool = false var _prop: int = -1 var _prop_since: int = 0 var _skipped_props: Dictionary = {} var _to_build: Array[int] = [StructureSet.BREAKWATER, StructureSet.WALL] var _build_yaw: float = 0.0 var _build_tries: int = 0 var _stock_before: int = -1 var _wood_seen: int = 0 func _init(id: int, level: int = NORMAL) -> void: super(id, level) func step_name() -> String: return STEP_NAMES[step] func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: var me: SimEntity = sim.entities.find(player_id) if me == null or me.eliminated or me.drop_state != Drop.NONE or step == DONE: return super(sim, grid, world) if _landed_tick < 0: _landed_tick = sim.tick var wood: int = me.materials[Materials.DRIFTWOOD] if wood > _wood_seen: harvested += wood - _wood_seen _wood_seen = wood var busy: bool = _fleeing or target_id != 0 or _stage != OUTSIDE or me.swimming or not me.on_ground or _heal_slot(me) >= 0 if step == WAIT and not busy: if _weapon_slot >= 0 or sim.tick - _landed_tick >= roundi(UNARMED_WAIT_SECONDS * TICKS): step = HARVEST _script_since = sim.tick if step != WAIT and sim.tick - _script_since > roundi(SCRIPT_TIMEOUT_SECONDS * TICKS): step = DONE if busy or step == WAIT or step == DONE: _release() return super(sim, grid, world) # The script has the controls this tick. Keep the senses going, and hand back to # the brain on the next tick if they turn anything up. _driving = true _sense(sim, world, me) _check_tide(sim, world, me) if target_id != 0 or _fleeing: return _hold(sim, me) if step == HARVEST: state = "harvest" return _harvest(sim, grid, me) state = "build" return _build(sim, me) ## Gives the path follower back to the brain with no goal of the script's left in it. func _release() -> void: if _driving: _stop() _driving = false _stock_before = -1 func _hold(sim: Simulation, me: SimEntity) -> PlayerCommand: return PlayerCommand.from_input(sim.tick, Vector2.ZERO, float(me.yaw) / PlayerCommand.YAW_STEPS * TAU, 0.0) ## Walks to the nearest driftwood prop and swings at it until there's enough to build. func _harvest(sim: Simulation, grid: NavGrid, me: SimEntity) -> PlayerCommand: if me.materials[Materials.DRIFTWOOD] >= WANTED_DRIFTWOOD: step = BUILD _stop() _build_yaw = float(me.yaw) / PlayerCommand.YAW_STEPS * TAU return _hold(sim, me) var standing: bool = _prop >= 0 and sim.props.is_standing(_prop) if not standing or sim.tick - _prop_since > roundi(PROP_TIMEOUT_SECONDS * TICKS): if standing: _skipped_props[_prop] = true _prop = _find_prop(sim, me) _prop_since = sim.tick if _prop < 0: step = DONE return _hold(sim, me) var box: AABB = sim.props.boxes[_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 var flat: float = Vector2(to.x, to.z).length() var yaw: float = atan2(to.x, to.z) if PropSet._distance_to_box(box, eye) <= Harvest.reach() - 0.6: _stop() return PlayerCommand.from_input(sim.tick, Vector2.ZERO, yaw, atan2(to.y, flat), PlayerCommand.BUTTON_FIRE, Simulation.BOATHOOK_SLOT) if flat <= DIRECT_METRES: _stop() var buttons: int = PlayerCommand.BUTTON_JUMP if (sim.tick - _prop_since) % (2 * TICKS) == 2 * TICKS - 1 else 0 return PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), yaw, 0.0, buttons) _go(Vector3(centre.x, box.position.y, centre.z), DIRECT_METRES - 1.0) var walk: PlayerCommand = nav.think(sim, grid) if nav.status == NavBot.UNREACHABLE: _skipped_props[_prop] = true _prop = -1 return walk ## The nearest standing driftwood prop on dry ground that hasn't been given up on. func _find_prop(sim: Simulation, me: SimEntity) -> int: for prop: int in sim.props.near(me.position, SEARCH_RADIUS): if _skipped_props.has(prop) or sim.props.material_of(prop) != Materials.NAMES[Materials.DRIFTWOOD]: continue var box: AABB = sim.props.boxes[prop] if box.position.y > sim.sea_level + 0.5 and absf(box.position.y - me.position.y) < 8.0: return prop return -1 ## Places each piece on the list where the player stands, trying each quarter turn. func _build(sim: Simulation, me: SimEntity) -> PlayerCommand: var stock: int = me.materials[Materials.DRIFTWOOD] if _stock_before >= 0: if stock < _stock_before: built += 1 _to_build.pop_front() _build_tries = 0 else: _build_tries += 1 _build_yaw += PI * 0.5 if _build_tries >= 4: _to_build.pop_front() _build_tries = 0 _stock_before = -1 while not _to_build.is_empty() and stock < StructureSet.cost_of(_to_build[0]): _to_build.pop_front() if _to_build.is_empty(): step = DONE return _hold(sim, me) if me.action_cooldown > 0: return PlayerCommand.from_input(sim.tick, Vector2.ZERO, _build_yaw, 0.0) _stock_before = stock return PlayerCommand.from_input( sim.tick, Vector2.ZERO, _build_yaw, 0.0, PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, _to_build[0], Materials.DRIFTWOOD)