M5: Bot looting, combat and tide awareness
BotBrain plays a match with the commands a player sends: it picks a landing marker near the barge's route, loots points of interest (following their own walking routes into huts, lofts and piers), fights opponents it can see, bandages itself, and gets to ground the coming tide will leave dry. BotWorld is the map every bot shares: loot markers, routes, and each tide phase's safe ground, which is the dry land joined to the island's highest walkable ground. Three difficulties (Tuning.BOT_DIFFICULTIES) set reaction time, sight range and aim error. The server mixes them unless --bot-difficulty says otherwise. NavGrid labels connected regions when it is built, so a goal that is cut off is turned down without a search of the whole island (about 70 ms each). make bot-matches plays 20 bot-only matches and checks winners, drownings and difficulty. Seeds 101 to 120: 20 of 20 with a winner, 7 of 460 deaths were drownings (1.5%), easy bots placed worst (13.7 against 11.8 and 12.0) and hard bots took the most eliminations (186 against 137 and 132). Closes #28 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,765 @@
|
||||
## A bot that plays a match: it picks a landing spot, loots points of interest, fights
|
||||
## who it sees, patches itself up and keeps ahead of the tide. It decides where to go
|
||||
## and what to press; NavBot does the walking. Like a player, it only sends
|
||||
## PlayerCommands.
|
||||
##
|
||||
## What it knows is what a player would: the map (BotWorld's loot markers and safe
|
||||
## ground), the tide forecast, items lying close by, opponents in view with a clear line
|
||||
## to them, and who just hit it. Difficulty (Tuning.BOT_DIFFICULTIES) sets how quickly
|
||||
## it reacts, how far it sees and how steady its aim is.
|
||||
##
|
||||
## Each tick, in order of urgency: get to ground the coming tide will leave dry; fight
|
||||
## a visible opponent if armed; 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.
|
||||
class_name BotBrain
|
||||
extends RefCounted
|
||||
|
||||
const EASY: int = 0
|
||||
const NORMAL: int = 1
|
||||
const HARD: int = 2
|
||||
const TICKS: int = Tuning.TICK_RATE_HZ
|
||||
## Where a bot is on the way into a hut, loft or pier along its walking route.
|
||||
const OUTSIDE: int = 0
|
||||
const ENTERING: int = 1
|
||||
const INSIDE: int = 2
|
||||
const LEAVING: int = 3
|
||||
|
||||
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".
|
||||
var state: String = "idle"
|
||||
## The opponent the bot is fighting or looking for, or 0.
|
||||
var target_id: int = 0
|
||||
var target_visible: bool = false
|
||||
|
||||
var _reaction_ticks: int = 0
|
||||
var _aim_error_degrees: float = 0.0
|
||||
var _sight: float = 0.0
|
||||
var _lead: float = 0.0
|
||||
|
||||
var _acquired_tick: int = 0
|
||||
var _last_seen_tick: int = -100000
|
||||
var _last_seen: Vector3 = Vector3.ZERO
|
||||
var _aim_error: Vector2 = Vector2.ZERO
|
||||
var _aim_error_goal: Vector2 = Vector2.ZERO
|
||||
var _strafe: float = 1.0
|
||||
var _strafe_until: int = 0
|
||||
var _weapon_slot: int = -1
|
||||
var _weapon_checked: int = -100000
|
||||
|
||||
var _fleeing: bool = false
|
||||
var _flee_goal: Vector3 = Vector3.ZERO
|
||||
var _tide_checked: int = -100000
|
||||
|
||||
var _landing: int = -1
|
||||
var _marker: int = -1
|
||||
var _marker_since: int = 0
|
||||
var _visited: Dictionary = {}
|
||||
var _pickup_id: int = 0
|
||||
var _pickup_since: int = 0
|
||||
var _skipped_pickups: Dictionary = {}
|
||||
var _skipped_chests: Dictionary = {}
|
||||
var _loot_checked: int = -100000
|
||||
var _interact_last: bool = false
|
||||
var _hunt_goal: Vector3 = Vector3.ZERO
|
||||
var _hunt_until: int = 0
|
||||
var _goal_set: bool = false
|
||||
## Walking a point of interest's route: the stage, the route, the next point on it and
|
||||
## when the stage began.
|
||||
var _stage: int = OUTSIDE
|
||||
var _route: Array = []
|
||||
var _route_index: int = 0
|
||||
var _stage_since: int = 0
|
||||
## Walking straight at a point, without the path follower: the point, whether it's in
|
||||
## use this tick, and how long the bot has gone without getting closer.
|
||||
var _direct: Vector3 = Vector3.ZERO
|
||||
var _direct_goal: Vector3 = Vector3.ZERO
|
||||
var _direct_on: bool = false
|
||||
var _leave_tries: int = 0
|
||||
var _direct_best: float = INF
|
||||
var _direct_stalled: int = 0
|
||||
|
||||
|
||||
func _init(id: int, level: int = NORMAL) -> void:
|
||||
player_id = id
|
||||
difficulty = clampi(level, 0, Tuning.BOT_DIFFICULTIES.size() - 1)
|
||||
nav = NavBot.new(id)
|
||||
var stats: Dictionary = Tuning.BOT_DIFFICULTIES[difficulty]
|
||||
_reaction_ticks = roundi(float(stats["reaction"]) * TICKS)
|
||||
_aim_error_degrees = float(stats["aim_error"])
|
||||
_sight = float(stats["sight"])
|
||||
_lead = float(stats["lead"])
|
||||
|
||||
|
||||
func difficulty_name() -> String:
|
||||
return Tuning.BOT_DIFFICULTIES[difficulty]["name"]
|
||||
|
||||
|
||||
## Returns this bot's command for `sim.tick`. The grid must be synced and the world
|
||||
## must have observed this tick.
|
||||
func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
|
||||
var me: SimEntity = sim.entities.find(player_id)
|
||||
if me == null or me.eliminated:
|
||||
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0)
|
||||
if me.drop_state != Drop.NONE:
|
||||
return _drop(sim, grid, world)
|
||||
_sense(sim, world, me)
|
||||
_check_tide(sim, world, me)
|
||||
_update_aim_error(sim)
|
||||
var target: SimEntity = sim.entities.find(target_id) if target_id != 0 else null
|
||||
var distance: float = me.position.distance_to(target.position) if target != null else 30.0
|
||||
_choose_weapon(sim, me, distance)
|
||||
var armed: bool = _weapon_slot >= 0
|
||||
var selection: int = _weapon_slot + 1 if armed else Simulation.BOATHOOK_SLOT
|
||||
var buttons: int = 0
|
||||
var fighting: bool = target != null and target_visible and armed and not me.swimming
|
||||
var interact: bool = false
|
||||
var walk: bool = true
|
||||
|
||||
# Where to go.
|
||||
_direct_on = false
|
||||
var urgent: bool = _fleeing 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.
|
||||
if urgent and _stage != LEAVING:
|
||||
_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 _fleeing:
|
||||
state = "flee"
|
||||
_go(_flee_goal, 3.0)
|
||||
elif fighting:
|
||||
state = "fight"
|
||||
_stop()
|
||||
elif target != null and armed:
|
||||
state = "chase"
|
||||
_go(_last_seen, 3.0)
|
||||
if nav.status == NavBot.ARRIVED or nav.status == NavBot.UNREACHABLE:
|
||||
_forget_target()
|
||||
else:
|
||||
var heal_slot: int = _heal_slot(me)
|
||||
if heal_slot >= 0:
|
||||
state = "heal"
|
||||
selection = heal_slot + 1
|
||||
buttons |= PlayerCommand.BUTTON_FIRE
|
||||
walk = _goal_set
|
||||
else:
|
||||
interact = _loot(sim, grid, world, me)
|
||||
if state != "loot":
|
||||
_hunt(sim, world)
|
||||
|
||||
# Walking, from the path follower.
|
||||
var direction: Vector2 = Vector2.ZERO
|
||||
var yaw: float = float(me.yaw) / PlayerCommand.YAW_STEPS * TAU
|
||||
var pitch: float = 0.0
|
||||
if _direct_on:
|
||||
var to: Vector2 = Vector2(_direct.x - me.position.x, _direct.z - me.position.z)
|
||||
if to.length() > 0.05:
|
||||
direction = to.normalized()
|
||||
yaw = atan2(to.x, to.y)
|
||||
if _direct_stalled > 0 and _direct_stalled % 15 == 0:
|
||||
buttons |= PlayerCommand.BUTTON_JUMP
|
||||
elif _goal_set and walk:
|
||||
var step: PlayerCommand = nav.think(sim, grid)
|
||||
if nav.status == NavBot.UNREACHABLE and _fleeing:
|
||||
# No route the grid knows: head straight for it and swim if need be.
|
||||
var to: Vector2 = Vector2(_flee_goal.x - me.position.x, _flee_goal.z - me.position.z)
|
||||
direction = to.normalized()
|
||||
yaw = atan2(to.x, to.y)
|
||||
buttons |= PlayerCommand.BUTTON_SPRINT
|
||||
elif step.move_y != 0:
|
||||
yaw = step.yaw_radians()
|
||||
direction = Vector2(sin(yaw), cos(yaw))
|
||||
buttons |= step.buttons & (PlayerCommand.BUTTON_SPRINT | PlayerCommand.BUTTON_JUMP)
|
||||
if state == "heal":
|
||||
buttons &= ~PlayerCommand.BUTTON_SPRINT
|
||||
|
||||
# Fighting: face the target and fire, whatever the feet are doing.
|
||||
if target != null and target_visible and armed and not me.swimming:
|
||||
var def: ItemDef = ItemCatalog.get_def(me.slot_items[_weapon_slot])
|
||||
var aim: Vector2 = _aim_at(me, target, def)
|
||||
yaw = aim.x
|
||||
pitch = aim.y
|
||||
if not _fleeing and _stage == OUTSIDE:
|
||||
direction = _combat_move(sim, grid, me, target, def)
|
||||
buttons &= ~PlayerCommand.BUTTON_SPRINT
|
||||
var in_range: bool = distance <= _fire_range(def)
|
||||
if in_range and sim.tick - _acquired_tick >= _reaction_ticks and me.bloom < Tuning.BOT_BLOOM_PAUSE_DEGREES:
|
||||
buttons |= PlayerCommand.BUTTON_FIRE
|
||||
if distance > 10.0:
|
||||
buttons |= PlayerCommand.BUTTON_AIM
|
||||
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
|
||||
|
||||
if interact and not _interact_last:
|
||||
buttons |= PlayerCommand.BUTTON_INTERACT
|
||||
_interact_last = true
|
||||
else:
|
||||
_interact_last = false
|
||||
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)
|
||||
|
||||
|
||||
# Movement goals
|
||||
|
||||
func _go(goal: Vector3, radius: float) -> void:
|
||||
if not _goal_set or nav.goal.distance_to(goal) > 1.0:
|
||||
nav.arrive_radius = radius
|
||||
nav.set_goal(goal)
|
||||
_goal_set = true
|
||||
|
||||
|
||||
func _stop() -> void:
|
||||
if _goal_set:
|
||||
nav.clear_goal()
|
||||
_goal_set = false
|
||||
|
||||
|
||||
# The drop
|
||||
|
||||
## Rides the barge to within reach of a loot marker near its route, then glides to it.
|
||||
func _drop(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
|
||||
state = "drop"
|
||||
if _landing < 0 and not world.markers.is_empty() and sim.drop != null:
|
||||
var rng: RandomNumberGenerator = sim.rng.stream("bots")
|
||||
var a: Vector2 = Vector2(sim.drop.start.x, sim.drop.start.z)
|
||||
var b: Vector2 = Vector2(sim.drop.finish.x, sim.drop.finish.z)
|
||||
var near: Array[int] = []
|
||||
for index: int in world.markers.size():
|
||||
var at: Vector2 = Vector2(world.markers[index].x, world.markers[index].z)
|
||||
var closest: Vector2 = Geometry2D.get_closest_point_to_segment(at, a, b)
|
||||
if at.distance_to(closest) <= 120.0 and world.markers[index].y > sim.sea_level + 1.0:
|
||||
near.append(index)
|
||||
_landing = near[rng.randi_range(0, near.size() - 1)] if not near.is_empty() else rng.randi_range(0, world.markers.size() - 1)
|
||||
_marker = _landing
|
||||
_marker_since = sim.tick
|
||||
_go(world.markers[_landing], 2.0)
|
||||
return nav.think(sim, grid)
|
||||
|
||||
|
||||
# Senses
|
||||
|
||||
func _sense(sim: Simulation, world: BotWorld, me: SimEntity) -> void:
|
||||
var tick: int = sim.tick
|
||||
var target: SimEntity = sim.entities.find(target_id) if target_id != 0 else null
|
||||
if target != null and (target.eliminated or target.archetype != SimEntity.ARCHETYPE_PLAYER):
|
||||
target = null
|
||||
if target == null and target_id != 0:
|
||||
_forget_target()
|
||||
# Being hit tells a player roughly where it came from.
|
||||
var attacker_id: int = int(world.attackers.get(player_id, 0))
|
||||
if attacker_id != 0 and attacker_id != player_id and not target_visible:
|
||||
var attacker: SimEntity = sim.entities.find(attacker_id)
|
||||
if attacker != null and not attacker.eliminated:
|
||||
if target_id != attacker_id:
|
||||
_acquired_tick = tick
|
||||
target_id = attacker_id
|
||||
target = attacker
|
||||
_last_seen = attacker.position
|
||||
_last_seen_tick = tick
|
||||
if (tick + player_id) % Tuning.BOT_SENSE_TICKS != 0:
|
||||
return
|
||||
if target != null:
|
||||
var seen: bool = _can_see(sim, world, me, target, true)
|
||||
if seen and not target_visible and tick - _last_seen_tick > TICKS:
|
||||
_acquired_tick = tick
|
||||
target_visible = seen
|
||||
if seen:
|
||||
_last_seen = target.position
|
||||
_last_seen_tick = tick
|
||||
elif tick - _last_seen_tick > roundi(Tuning.BOT_MEMORY_SECONDS * TICKS):
|
||||
_forget_target()
|
||||
target = null
|
||||
if target != null and target_visible:
|
||||
return
|
||||
# Look for someone new, nearest first, with a few sight lines at most.
|
||||
var candidates: Array[Vector2] = []
|
||||
for other_id: int in world.player_ids:
|
||||
if other_id == player_id or other_id == target_id:
|
||||
continue
|
||||
var other: SimEntity = sim.entities.find(other_id)
|
||||
if other == null or other.eliminated or other.drop_state != Drop.NONE:
|
||||
continue
|
||||
var gap: float = me.position.distance_squared_to(other.position)
|
||||
if gap <= _sight * _sight:
|
||||
candidates.append(Vector2(gap, other_id))
|
||||
candidates.sort()
|
||||
for index: int in mini(candidates.size(), 3):
|
||||
var other: SimEntity = sim.entities.find(int(candidates[index].y))
|
||||
if _can_see(sim, world, me, other, false):
|
||||
target_id = other.id
|
||||
target_visible = true
|
||||
_acquired_tick = tick
|
||||
_last_seen = other.position
|
||||
_last_seen_tick = tick
|
||||
return
|
||||
|
||||
|
||||
## True if `other` is within sight range, in view (or close enough to hear, or already
|
||||
## being watched) and nothing solid is in the way.
|
||||
func _can_see(sim: Simulation, world: BotWorld, me: SimEntity, other: SimEntity, watching: bool) -> bool:
|
||||
if other.eliminated or other.drop_state != Drop.NONE:
|
||||
return false
|
||||
var to: Vector3 = other.position - me.position
|
||||
var gap: float = to.length()
|
||||
if gap > _sight:
|
||||
return false
|
||||
if not watching and gap > Tuning.BOT_HEARING_METRES:
|
||||
var facing: float = float(me.yaw) / PlayerCommand.YAW_STEPS * TAU
|
||||
var flat: Vector2 = Vector2(to.x, to.z).normalized()
|
||||
if flat.dot(Vector2(sin(facing), cos(facing))) < cos(deg_to_rad(Tuning.BOT_VIEW_HALF_ANGLE_DEGREES)):
|
||||
return false
|
||||
var chest: Vector3 = other.position + Vector3(0.0, Movement.body_height(other) * 0.62, 0.0)
|
||||
return world.clear_line(sim.ground, Harvest.eye(me), chest)
|
||||
|
||||
|
||||
func _forget_target() -> void:
|
||||
target_id = 0
|
||||
target_visible = false
|
||||
|
||||
|
||||
# Fighting
|
||||
|
||||
## The slot of the best weapon for a target `distance` away, kept for a second at a time
|
||||
## so the bot doesn't keep swapping.
|
||||
func _choose_weapon(sim: Simulation, me: SimEntity, distance: float) -> void:
|
||||
var usable: bool = _weapon_slot >= 0 and _weapon_score(me, _weapon_slot, distance) > 0.0
|
||||
if usable and sim.tick - _weapon_checked < TICKS:
|
||||
return
|
||||
_weapon_checked = sim.tick
|
||||
var best: int = -1
|
||||
var best_score: float = 0.0
|
||||
for slot: int in Inventory.slot_count():
|
||||
var score: float = _weapon_score(me, slot, distance)
|
||||
# Stay with the held weapon unless another is clearly better.
|
||||
if slot == _weapon_slot:
|
||||
score *= 1.25
|
||||
if score > best_score:
|
||||
best = slot
|
||||
best_score = score
|
||||
_weapon_slot = best
|
||||
|
||||
|
||||
## How good the weapon in `slot` is at `distance`: 0 if it can't be fired.
|
||||
static func _weapon_score(me: SimEntity, slot: int, distance: float) -> float:
|
||||
var def: ItemDef = ItemCatalog.get_def(me.slot_items[slot])
|
||||
if def == null or not def.is_weapon() or def.thrown or def.damage <= 0:
|
||||
return 0.0
|
||||
if me.slot_rounds[slot] <= 0 and (def.ammo_code < 0 or me.ammo[Inventory.ammo_index(def.ammo_code)] <= 0):
|
||||
return 0.0
|
||||
var score: float = def.damage_at(distance) * def.pellets * def.fire_rate
|
||||
if distance > def.range_max:
|
||||
score *= 0.05
|
||||
# A wide cone wastes most of its shot at range.
|
||||
var cone: float = distance * tan(deg_to_rad(def.spread_degrees * def.aim_spread_factor + 0.5))
|
||||
return score * clampf(0.5 / maxf(cone, 0.01), 0.1, 1.0)
|
||||
|
||||
|
||||
func _update_aim_error(sim: Simulation) -> void:
|
||||
if (sim.tick + player_id) % Tuning.BOT_AIM_WANDER_TICKS == 0:
|
||||
var rng: RandomNumberGenerator = sim.rng.stream("bots")
|
||||
_aim_error_goal = Vector2(rng.randf_range(-1.0, 1.0), rng.randf_range(-0.5, 0.5)) * _aim_error_degrees
|
||||
_aim_error = _aim_error.lerp(_aim_error_goal, 0.25)
|
||||
|
||||
|
||||
## Yaw and pitch, in radians, to hit `target` with `def`, off by the bot's aim error.
|
||||
func _aim_at(me: SimEntity, target: SimEntity, def: ItemDef) -> Vector2:
|
||||
var eye: Vector3 = Harvest.eye(me)
|
||||
var point: Vector3 = target.position + Vector3(0.0, Movement.body_height(target) * 0.62, 0.0)
|
||||
if def.projectile_speed > 0.0:
|
||||
var flight: float = eye.distance_to(point) / def.projectile_speed
|
||||
point += target.velocity * flight * _lead
|
||||
point.y += 0.5 * def.projectile_gravity * flight * flight
|
||||
var to: Vector3 = point - eye
|
||||
var flat: float = Vector2(to.x, to.z).length()
|
||||
return Vector2(atan2(to.x, to.z) + deg_to_rad(_aim_error.x), atan2(to.y, flat) + deg_to_rad(_aim_error.y))
|
||||
|
||||
|
||||
## How far off the bot opens fire with `def`: about where the weapon still hits, and
|
||||
## nearer for a bot whose aim wanders, so it doesn't empty its magazine at the horizon.
|
||||
func _fire_range(def: ItemDef) -> float:
|
||||
var reach: float = minf(maxf(def.range_full * 1.5, 12.0), def.range_max)
|
||||
return reach * clampf(3.0 / _aim_error_degrees, 0.45, 1.0)
|
||||
|
||||
|
||||
## Which way to step while fighting: closer when out of the weapon's good range, else
|
||||
## side to side. Never into deep water or off a cliff.
|
||||
func _combat_move(sim: Simulation, grid: NavGrid, me: SimEntity, target: SimEntity, def: ItemDef) -> Vector2:
|
||||
var to: Vector2 = Vector2(target.position.x - me.position.x, target.position.z - me.position.z)
|
||||
var gap: float = to.length()
|
||||
if gap < 0.01:
|
||||
return Vector2.ZERO
|
||||
to /= gap
|
||||
var wanted: float = minf(clampf(def.range_full * 0.8, 5.0, 30.0), _fire_range(def) * 0.8)
|
||||
if sim.tick >= _strafe_until:
|
||||
var rng: RandomNumberGenerator = sim.rng.stream("bots")
|
||||
_strafe = -_strafe
|
||||
_strafe_until = sim.tick + rng.randi_range(TICKS / 2, TICKS * 2)
|
||||
var side: Vector2 = Vector2(-to.y, to.x) * _strafe
|
||||
var step: Vector2 = side
|
||||
if gap > wanted * 1.3:
|
||||
step = (to + side * 0.4).normalized()
|
||||
elif gap < wanted * 0.4:
|
||||
step = (-to + side * 0.6).normalized()
|
||||
if _can_step(grid, me, step):
|
||||
return step
|
||||
if _can_step(grid, me, -side):
|
||||
_strafe = -_strafe
|
||||
return -side
|
||||
return Vector2.ZERO
|
||||
|
||||
|
||||
## True if the ground a couple of metres along `step` is open, dry and near level.
|
||||
static func _can_step(grid: NavGrid, me: SimEntity, step: Vector2) -> bool:
|
||||
var ahead: Vector3 = me.position + Vector3(step.x, 0.0, step.y) * 2.5
|
||||
var point: Vector2i = grid.point_of(ahead)
|
||||
if not grid.is_open(point) or grid.wet[grid.index_of(point)] != 0:
|
||||
return false
|
||||
return absf(grid.heights[grid.index_of(point)] - me.position.y) <= Tuning.BOT_NAV_MAX_RISE
|
||||
|
||||
|
||||
# Healing
|
||||
|
||||
## The slot of a consumable worth using now, or -1. Healing comes before armor.
|
||||
static func _heal_slot(me: SimEntity) -> int:
|
||||
if me.swimming:
|
||||
return -1
|
||||
var armor_slot: int = -1
|
||||
for slot: int in Inventory.slot_count():
|
||||
var def: ItemDef = ItemCatalog.get_def(me.slot_items[slot])
|
||||
if def == null or def.kind != ItemDef.CONSUMABLE or not Consumables.would_help(me, def):
|
||||
continue
|
||||
if def.heal > 0:
|
||||
var cap: int = mini(def.heal_cap, Tuning.PLAYER_MAX_HEALTH)
|
||||
# Not wasteful: most of the heal lands, or the bot is badly hurt.
|
||||
if me.health + def.heal / 2 <= cap or me.health < 40:
|
||||
return slot
|
||||
elif def.armor > 0 and me.armor + def.armor / 2 <= Tuning.PLAYER_MAX_ARMOR:
|
||||
armor_slot = slot
|
||||
return armor_slot
|
||||
|
||||
|
||||
# The tide
|
||||
|
||||
## Decides whether to leave for safe ground: at once if the water is rising or already
|
||||
## round the bot's legs, otherwise when the time left only just covers the walk.
|
||||
func _check_tide(sim: Simulation, world: BotWorld, me: SimEntity) -> void:
|
||||
if sim.tide == null:
|
||||
_fleeing = false
|
||||
return
|
||||
if sim.tick - _tide_checked < TICKS / 2 and not me.swimming:
|
||||
return
|
||||
_tide_checked = sim.tick
|
||||
var phase: int = sim.tide.phase_at(sim.tick)
|
||||
if world.is_safe(me.position, phase) and not me.swimming:
|
||||
_fleeing = false
|
||||
return
|
||||
var goal: Vector3 = world.nearest_safe(me.position, phase)
|
||||
if _fleeing and _goal_set and nav.status == NavBot.ARRIVED:
|
||||
# At the edge of safe ground but not on it: make for its top instead.
|
||||
goal = world.phase_tops[phase]
|
||||
var gap: float = Vector2(goal.x - me.position.x, goal.z - me.position.z).length()
|
||||
var left: float = float(sim.tide.ticks_until_rise(sim.tick)) / TICKS
|
||||
var wet: bool = me.swimming or sim.water_level_at(me.position) > me.position.y + 0.2
|
||||
if _fleeing or wet or sim.tide.stage_at(sim.tick) != Tide.HOLD or left < gap / Tuning.BOT_FLEE_SPEED + Tuning.BOT_FLEE_BUFFER_SECONDS:
|
||||
_fleeing = true
|
||||
_flee_goal = goal
|
||||
|
||||
|
||||
## True if a bot may go to `spot` for loot: it's above the water, and either there's
|
||||
## time before the next rise or the spot is on ground that rise will leave dry.
|
||||
func _spot_allowed(sim: Simulation, world: BotWorld, spot: Vector3) -> bool:
|
||||
if spot.y < sim.sea_level + 0.5:
|
||||
return false
|
||||
if sim.tide == null:
|
||||
return true
|
||||
var phase: int = sim.tide.phase_at(sim.tick)
|
||||
if world.is_safe(spot, phase):
|
||||
return true
|
||||
return sim.tide.stage_at(sim.tick) == Tide.HOLD and float(sim.tide.ticks_until_rise(sim.tick)) / TICKS > Tuning.BOT_LOOT_TIDE_SECONDS
|
||||
|
||||
|
||||
# Looting
|
||||
|
||||
## True if the bot has room for the pickup and a use for it. A bot never swaps.
|
||||
func wants(me: SimEntity, pickup: SimEntity) -> bool:
|
||||
if pickup == null or pickup.archetype != SimEntity.ARCHETYPE_PICKUP:
|
||||
return false
|
||||
var def: ItemDef = ItemCatalog.get_def(pickup.item)
|
||||
if def == null:
|
||||
return false
|
||||
if def.kind == ItemDef.AMMO:
|
||||
return me.ammo[Inventory.ammo_index(def.code)] < def.cap
|
||||
var free: int = 0
|
||||
var weapons: int = 0
|
||||
var others: int = 0
|
||||
var stackable: bool = false
|
||||
var same: bool = false
|
||||
for slot: int in Inventory.slot_count():
|
||||
var held: ItemDef = ItemCatalog.get_def(me.slot_items[slot])
|
||||
if held == null:
|
||||
free += 1
|
||||
continue
|
||||
if held.code == def.code:
|
||||
same = true
|
||||
if def.stack > 1 and me.slot_tiers[slot] == pickup.item_tier and me.slot_counts[slot] < def.stack:
|
||||
stackable = true
|
||||
if held.is_weapon():
|
||||
weapons += 1
|
||||
else:
|
||||
others += 1
|
||||
if def.kind == ItemDef.WEAPON:
|
||||
# Thrown weapons and tools that do no damage are no use to a bot yet.
|
||||
if def.thrown or def.damage <= 0 or same:
|
||||
return false
|
||||
return free > 0 and weapons < Tuning.BOT_MAX_WEAPONS
|
||||
return stackable or (free > 0 and others < Inventory.slot_count() - Tuning.BOT_MAX_WEAPONS)
|
||||
|
||||
|
||||
## Goes after useful items in view, then a chest in reach, then the next loot marker.
|
||||
## Sets `state` to "loot" when there's something to do. Returns true to press interact.
|
||||
func _loot(sim: Simulation, grid: NavGrid, world: BotWorld, me: SimEntity) -> bool:
|
||||
var tick: int = sim.tick
|
||||
state = "loot"
|
||||
# An item worth having.
|
||||
var pickup: SimEntity = sim.entities.find(_pickup_id) if _pickup_id != 0 else null
|
||||
if pickup != null and (not wants(me, pickup) or tick - _pickup_since > roundi(Tuning.BOT_PICKUP_TIMEOUT_SECONDS * TICKS)):
|
||||
if wants(me, pickup):
|
||||
_skipped_pickups[_pickup_id] = true
|
||||
pickup = null
|
||||
if pickup == null:
|
||||
_pickup_id = 0
|
||||
if tick - _loot_checked >= TICKS / 2:
|
||||
_loot_checked = tick
|
||||
_pickup_id = _find_pickup(sim, world, me)
|
||||
_pickup_since = tick
|
||||
pickup = sim.entities.find(_pickup_id) if _pickup_id != 0 else null
|
||||
if pickup != null:
|
||||
_go(pickup.position, 0.6)
|
||||
if me.position.distance_to(pickup.position) <= Tuning.PICKUP_RANGE - 0.3 and Harvest.can_swing(me):
|
||||
var nearest: int = Inventory.nearest_pickup(sim, me)
|
||||
if nearest == _pickup_id or wants(me, sim.entities.find(nearest)):
|
||||
return true
|
||||
if nav.status != NavBot.MOVING:
|
||||
_skipped_pickups[_pickup_id] = true
|
||||
_pickup_id = 0
|
||||
elif nav.status == NavBot.UNREACHABLE:
|
||||
_skipped_pickups[_pickup_id] = true
|
||||
_pickup_id = 0
|
||||
return false
|
||||
# A chest in reach.
|
||||
var chest: int = _chest_in_reach(sim, me)
|
||||
if chest >= 0 and world.in_routed_place(sim.loot.chest_positions[chest]):
|
||||
chest = -1
|
||||
if chest >= 0:
|
||||
var nearest: int = Inventory.nearest_pickup(sim, me)
|
||||
var blocked: bool = false
|
||||
if nearest != 0:
|
||||
var other: SimEntity = sim.entities.find(nearest)
|
||||
blocked = other.position.distance_to(me.position) < sim.loot.chest_positions[chest].distance_to(me.position) and not wants(me, other)
|
||||
if not blocked:
|
||||
_stop()
|
||||
return Harvest.can_swing(me)
|
||||
_go(sim.loot.chest_positions[chest], 0.5)
|
||||
if nav.status != NavBot.MOVING:
|
||||
_skipped_chests[chest] = true
|
||||
return false
|
||||
# The next marker on the map.
|
||||
if _marker >= 0 and not world.routes[_marker].is_empty():
|
||||
# A marker indoors or out on a pier: walk to the foot of its route, then along it.
|
||||
var foot: Vector3 = world.routes[_marker][0]
|
||||
var late: bool = tick - _marker_since > roundi(Tuning.BOT_LOOT_TIMEOUT_SECONDS * TICKS)
|
||||
var gave_up: bool = _goal_set and nav.status == NavBot.UNREACHABLE and nav.goal.distance_to(foot) < 1.0
|
||||
if late or gave_up:
|
||||
_visited[_marker] = true
|
||||
_marker = -1
|
||||
elif not _spot_allowed(sim, world, world.markers[_marker]):
|
||||
_marker = -1
|
||||
elif Vector2(foot.x - me.position.x, foot.z - me.position.z).length() <= 1.2 and absf(foot.y - me.position.y) < 1.5:
|
||||
_route = world.routes[_marker]
|
||||
_stop()
|
||||
_set_stage(sim, ENTERING, mini(1, _route.size() - 1))
|
||||
return false
|
||||
else:
|
||||
_go(foot, 0.8)
|
||||
return false
|
||||
if _marker >= 0:
|
||||
var spot: Vector3 = world.markers[_marker]
|
||||
var near: bool = Vector2(spot.x - me.position.x, spot.z - me.position.z).length() <= 2.5
|
||||
var late: bool = tick - _marker_since > roundi(Tuning.BOT_LOOT_TIMEOUT_SECONDS * TICKS)
|
||||
var gave_up: bool = _goal_set and nav.status != NavBot.MOVING and nav.goal.distance_to(spot) < 1.0
|
||||
if near or late or gave_up:
|
||||
_visited[_marker] = true
|
||||
_marker = -1
|
||||
elif not _spot_allowed(sim, world, spot):
|
||||
_marker = -1
|
||||
if _marker < 0 and tick % (TICKS / 2) == player_id % (TICKS / 2):
|
||||
_marker = _next_marker(sim, world, me)
|
||||
_marker_since = tick
|
||||
if _marker >= 0:
|
||||
_go(world.markers[_marker], 1.5)
|
||||
return false
|
||||
state = "idle"
|
||||
return false
|
||||
|
||||
|
||||
## The nearest closed chest in reach that the bot hasn't given up on, or -1.
|
||||
func _chest_in_reach(sim: Simulation, me: SimEntity) -> int:
|
||||
var chest: int = sim.loot.nearest_chest(me.position)
|
||||
if chest >= 0 and _skipped_chests.has(chest):
|
||||
return -1
|
||||
return chest
|
||||
|
||||
|
||||
# Routes into huts, lofts and piers
|
||||
|
||||
func _set_stage(sim: Simulation, stage: int, index: int) -> void:
|
||||
_stage = stage
|
||||
_route_index = index
|
||||
_stage_since = sim.tick
|
||||
_direct_best = INF
|
||||
_direct_stalled = 0
|
||||
|
||||
|
||||
## Heads for `point`. Returns true once the bot is within `radius` of it. With `from`
|
||||
## given, the bot keeps to the line from there to the point, steering at a spot on it
|
||||
## a metre ahead, so it stays on a stair or in a doorway rather than cutting the corner.
|
||||
func _walk_to(me: SimEntity, point: Vector3, radius: float, from: Vector3 = Vector3.INF) -> bool:
|
||||
var gap: float = Vector2(point.x - me.position.x, point.z - me.position.z).length()
|
||||
var aim: Vector3 = point
|
||||
if from != Vector3.INF:
|
||||
var a: Vector2 = Vector2(from.x, from.z)
|
||||
var b: Vector2 = Vector2(point.x, point.z)
|
||||
var length: float = a.distance_to(b)
|
||||
if length > 0.01:
|
||||
var along: float = (Vector2(me.position.x, me.position.z) - a).dot((b - a) / length)
|
||||
var ahead: Vector2 = a.lerp(b, clampf((along + 1.0) / length, 0.0, 1.0))
|
||||
aim = Vector3(ahead.x, point.y, ahead.y)
|
||||
if point.distance_to(_direct_goal) > 0.01:
|
||||
_direct_best = INF
|
||||
_direct_stalled = 0
|
||||
_direct_goal = point
|
||||
_direct = aim
|
||||
_direct_on = true
|
||||
if gap < _direct_best - 0.05:
|
||||
_direct_best = gap
|
||||
_direct_stalled = 0
|
||||
else:
|
||||
_direct_stalled += 1
|
||||
return gap <= radius and absf(point.y - me.position.y) <= 1.2
|
||||
|
||||
|
||||
## One tick on a route: in along its points, loot at the end, and back out the same
|
||||
## way. Each stage has a time limit, after which the bot turns back or gives up.
|
||||
## Returns true to press interact.
|
||||
func _walk_route(sim: Simulation, me: SimEntity) -> bool:
|
||||
var waited: float = float(sim.tick - _stage_since) / TICKS
|
||||
match _stage:
|
||||
ENTERING:
|
||||
if _walk_to(me, _route[_route_index], 0.5, _route[maxi(_route_index - 1, 0)]):
|
||||
if _route_index + 1 >= _route.size():
|
||||
_set_stage(sim, INSIDE, _route_index)
|
||||
else:
|
||||
_route_index += 1
|
||||
_stage_since = sim.tick
|
||||
elif waited > 8.0:
|
||||
_set_stage(sim, LEAVING, maxi(_route_index - 1, 0))
|
||||
INSIDE:
|
||||
if waited > 15.0 or not Harvest.can_swing(me):
|
||||
_set_stage(sim, LEAVING, _route.size() - 2)
|
||||
return false
|
||||
var nearest: SimEntity = sim.entities.find(Inventory.nearest_pickup(sim, me))
|
||||
var chest: int = sim.loot.nearest_chest(me.position)
|
||||
if chest >= 0:
|
||||
var nearer: bool = nearest != null and nearest.position.distance_to(me.position) < sim.loot.chest_positions[chest].distance_to(me.position)
|
||||
if not nearer or wants(me, nearest):
|
||||
return true
|
||||
if nearest != null and wants(me, nearest):
|
||||
return true
|
||||
# Something useful further in: step over to it.
|
||||
var end: Vector3 = _route[_route.size() - 1]
|
||||
for pickup_id: int in sim.pickups:
|
||||
var pickup: SimEntity = sim.entities.find(pickup_id)
|
||||
if pickup.position.distance_to(end) < 3.5 and wants(me, pickup) and not _skipped_pickups.has(pickup_id):
|
||||
if _walk_to(me, pickup.position, 0.4) or _direct_stalled > TICKS:
|
||||
_skipped_pickups[pickup_id] = true
|
||||
return false
|
||||
_set_stage(sim, LEAVING, _route.size() - 2)
|
||||
LEAVING:
|
||||
if waited > 8.0 and _route_index >= 0 and _leave_tries < 4:
|
||||
# Caught on something: start again from the nearest point of the route.
|
||||
_leave_tries += 1
|
||||
var nearest_index: int = 0
|
||||
for index: int in _route.size():
|
||||
if me.position.distance_to(_route[index]) < me.position.distance_to(_route[nearest_index]):
|
||||
nearest_index = index
|
||||
_set_stage(sim, LEAVING, nearest_index)
|
||||
elif _route_index < 0 or waited > 8.0:
|
||||
if _marker >= 0:
|
||||
_visited[_marker] = true
|
||||
_marker = -1
|
||||
_stage = OUTSIDE
|
||||
_leave_tries = 0
|
||||
_goal_set = false
|
||||
elif _walk_to(me, _route[_route_index], 0.5, _route[mini(_route_index + 1, _route.size() - 1)]):
|
||||
_route_index -= 1
|
||||
_stage_since = sim.tick
|
||||
return false
|
||||
|
||||
|
||||
## The nearest wanted item in view, or 0.
|
||||
func _find_pickup(sim: Simulation, world: BotWorld, me: SimEntity) -> int:
|
||||
var best: int = 0
|
||||
var best_distance: float = Tuning.BOT_LOOT_SIGHT_METRES * Tuning.BOT_LOOT_SIGHT_METRES
|
||||
for pickup_id: int in sim.pickups:
|
||||
var pickup: SimEntity = sim.entities.find(pickup_id)
|
||||
var gap: float = pickup.position.distance_squared_to(me.position)
|
||||
if gap >= best_distance or _skipped_pickups.has(pickup_id):
|
||||
continue
|
||||
if absf(pickup.position.y - me.position.y) > 3.0 or not wants(me, pickup):
|
||||
continue
|
||||
if world.in_routed_place(pickup.position):
|
||||
continue
|
||||
if not _spot_allowed(sim, world, pickup.position):
|
||||
continue
|
||||
best = pickup_id
|
||||
best_distance = gap
|
||||
return best
|
||||
|
||||
|
||||
## The nearest marker the bot hasn't been to and may go to. Armed bots don't trek
|
||||
## across the island for more.
|
||||
func _next_marker(sim: Simulation, world: BotWorld, me: SimEntity) -> int:
|
||||
var reach: float = 150.0 if _weapon_slot >= 0 else 400.0
|
||||
var best: int = -1
|
||||
var best_distance: float = reach * reach
|
||||
for index: int in world.markers.size():
|
||||
if _visited.has(index):
|
||||
continue
|
||||
var spot: Vector3 = world.markers[index]
|
||||
var gap: float = Vector2(spot.x - me.position.x, spot.z - me.position.z).length_squared()
|
||||
if gap < best_distance and _spot_allowed(sim, world, spot):
|
||||
best = index
|
||||
best_distance = gap
|
||||
return best
|
||||
|
||||
|
||||
## Nothing to loot: wander the ground the tide will leave, where the others will be.
|
||||
func _hunt(sim: Simulation, world: BotWorld) -> void:
|
||||
state = "hunt"
|
||||
var arrived: bool = _goal_set and nav.status != NavBot.MOVING
|
||||
if not _goal_set or arrived or sim.tick >= _hunt_until:
|
||||
var phase: int = sim.tide.phase_at(sim.tick) if sim.tide != null else 0
|
||||
_hunt_goal = world.random_safe(sim.rng.stream("bots"), phase)
|
||||
_hunt_until = sim.tick + 40 * TICKS
|
||||
_go(_hunt_goal, 4.0)
|
||||
else:
|
||||
_go(_hunt_goal, 4.0)
|
||||
Reference in New Issue
Block a user