M6: Bots that build

Bots now gather materials with the boathook and build with the same build
command and Building.check a player's client goes through:

- Gathering: an armed bot with nothing more pressing gathers from props
  nearby (12 m while looting, 24 m once there's nothing left to loot) up to
  60 of a material, or 120 in the last three tide phases. Not within 10 s of
  seeing an opponent or being hit, and not when hurt.
- Cover: a wall toward a shooter who catches it reloading, from ambush or
  worn down to 60, then waits behind it to reload or heal.
- Wall and heal: a badly hurt bot that would break off walls itself off and
  heals where it stands instead, if it can afford a wall.
- Ramps: a staircase of ramps, each from the top of the last, up to an
  opponent on a cliff or a steep rise (at most 5 a fight).
- Breakwaters: late in the match, a bot the tide would catch before it
  reached dry ground, or one at the last stand, rings its cell with four
  breakwaters and stays inside while they hold.

Measured with the new `make bot-builds` (tools/bot_builds.gd): every other
bot builds and the rest are a control group. Over 40 matches, seeds 501 to
540: builders built in 53% of the fights they began with materials for a
wall (12% of all fights; most fights are in the landing, before anyone has
gathered), lost 58% of the fights they built in against 67% of the rest,
and placed 12.1 on average against the control's 12.8. Overall fight losses
(65.8% / 65.7%) and wins (20 / 20) came out level; docs/BALANCE.md has the
numbers and the earlier batches that led to the careful gathering. Worst
p99 tick 3.4 ms of the 12 ms budget.

Closes #29

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 01:13:25 -05:00
co-authored by Claude Opus 5.5
parent 4177bfff76
commit eb239c5424
15 changed files with 1135 additions and 16 deletions
+471 -8
View File
@@ -13,6 +13,14 @@
## boathook if not; 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.
##
## Bots build too (M6). An armed bot with nothing more pressing, and nobody about,
## gathers materials with the boathook. In a fight it puts a wall between itself and a
## shooter who catches it reloading, from ambush or worn down; walls itself off to heal
## instead of running when badly hurt; and builds ramps up to an opponent on a cliff or
## a steep rise. Late in the match, when the tide would catch it or at the last stand,
## it rings its cell with breakwaters. Every piece goes through the same build command
## a player sends, after the same Building.check the server makes.
class_name BotBrain
extends RefCounted
@@ -30,7 +38,8 @@ 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".
## "loot", "hunt", "hold", "idle", "retreat", "brawl", "gather", "cover", "ramp" or
## "breakwater".
var state: String = "idle"
## The opponent the bot is fighting or looking for, or 0.
var target_id: int = 0
@@ -92,6 +101,56 @@ var _leave_tries: int = 0
var _direct_best: float = INF
var _direct_stalled: int = 0
## M6: whether this bot gathers and builds. A batch turns it off for a control group.
var builds: bool = true
## M6: what this bot built and how its fights went, for batch measurement. A fight is
## a stretch with an opponent in which shots or swings were traded; it ends after
## Tuning.BOT_FIGHT_GAP_SECONDS with nobody to fight, or with the bot's fall ("lost").
var stats: Dictionary = {
"pieces": 0, "walls": 0, "ramps": 0, "breakwaters": 0, "harvested": 0,
"fights": 0, "fights_built": 0, "fights_lost": 0, "fights_built_lost": 0,
"fights_stocked": 0, "fights_stocked_built": 0,
}
var _in_fight: bool = false
var _fight_traded: bool = false
var _fight_built: bool = false
var _fight_stocked: bool = false
var _fight_seen: int = 0
var _stock_seen: int = 0
## Gathering: the prop, when to give up on it, when each search radius was last tried
## (near, far), props given up on, and this tick's swing.
var _gather_prop: int = -1
var _gather_deadline: int = 0
var _gather_checked: PackedInt32Array = PackedInt32Array([-100000, -100000])
var _gather_skipped: Dictionary = {}
var _swing: bool = false
var _swing_aim: Vector2 = Vector2.ZERO
## Cover: until when the bot stays behind it, which way the wall goes, whether it's up,
## when the cover began and when cover may be taken again.
var _covering_until: int = -100000
var _cover_yaw: float = 0.0
var _cover_walls: int = 0
var _cover_since: int = 0
var _cover_ready: int = 0
## Ramps placed this fight, until when the bot walks on up them, which way, and the
## slot of the last one, so the next continues from its top.
var _ramps: int = 0
var _ramp_until: int = -100000
var _ramp_direction: Vector2 = Vector2.ZERO
var _ramp_last: Vector4i = Vector4i.ZERO
## A ring of breakwaters: the cell it's round, its four slots, when it was begun (or
## -1), and the highest forecast level it's worth staying in for.
var _ring_cell: Vector2i = Vector2i.ZERO
var _ring_slots: Array[Vector4i] = []
var _ring_started: int = -1
var _ring_level: float = 0.0
## This tick's piece, if any: kind (-1 for none), facing yaw and material.
var _piece_kind: int = -1
var _piece_yaw: float = 0.0
var _piece_material: int = 0
## A stand-in for the bot, posed for Building.target() and Building.check().
var _probe: SimEntity = SimEntity.new(0, SimEntity.ARCHETYPE_PLAYER)
func _init(id: int, level: int = NORMAL) -> void:
player_id = id
@@ -114,9 +173,12 @@ func difficulty_name() -> String:
func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
var me: SimEntity = sim.entities.find(player_id)
if me == null or me.eliminated:
if _in_fight:
_end_fight(true)
return PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0)
if me.drop_state != Drop.NONE:
return _drop(sim, grid, world)
_count_building(world, me)
_sense(sim, world, me)
_check_tide(sim, world, me)
_update_aim_error(sim)
@@ -128,17 +190,25 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
var buttons: int = 0
_check_retreat(sim, world, me, target, armed)
var retreating: bool = sim.tick < _retreat_until
var fighting: bool = target != null and target_visible and armed and not me.swimming and not retreating
if _fleeing or me.swimming:
_covering_until = mini(_covering_until, sim.tick)
var covering: bool = sim.tick < _covering_until
if covering and target != null and target_visible and distance <= Tuning.BOT_BRAWL_METRES:
# They came round the wall: fight.
_covering_until = sim.tick
covering = false
var fighting: bool = target != null and target_visible and armed and not me.swimming and not retreating and not covering
# Inside a hut or loft too: someone beating on you there gets a swing back. An
# unarmed bot doesn't start a brawl; it keeps looking for a weapon (H3).
var provoked: bool = target != null and target.id == _hit_by and sim.tick - _hit_tick <= roundi(Tuning.BOT_MEMORY_SECONDS * TICKS)
var brawling: bool = (target != null and target_visible and not armed and not me.swimming
and not _fleeing and not retreating and distance <= Tuning.BOT_BRAWL_METRES and provoked)
and not _fleeing and not retreating and not covering and distance <= Tuning.BOT_BRAWL_METRES and provoked)
var interact: bool = false
var walk: bool = true
# Where to go.
_direct_on = false
_swing = false
var urgent: bool = _fleeing or retreating 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.
@@ -146,9 +216,15 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
_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 _in_ring(sim, me):
state = "breakwater"
_stop()
elif _fleeing:
state = "flee"
_go(_flee_goal, 3.0)
elif covering:
state = "cover"
_stop()
elif retreating:
state = "retreat"
_go(_retreat_goal, 3.0)
@@ -170,10 +246,11 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
selection = heal_slot + 1
buttons |= PlayerCommand.BUTTON_FIRE
walk = _goal_set
else:
elif not (builds and armed and _gather(sim, world, me, false)):
interact = _loot(sim, grid, world, me)
if state != "loot":
if state != "loot" and not (builds and armed and _gather(sim, world, me, true)):
_hunt(sim, world)
_plan_build(sim, grid, world, me, target, distance, fighting)
# Walking, from the path follower.
var direction: Vector2 = Vector2.ZERO
@@ -200,6 +277,12 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
buttons |= step.buttons & (PlayerCommand.BUTTON_SPRINT | PlayerCommand.BUTTON_JUMP)
if state == "heal":
buttons &= ~PlayerCommand.BUTTON_SPRINT
if _swing:
yaw = _swing_aim.x
pitch = _swing_aim.y
direction = Vector2.ZERO
selection = Simulation.BOATHOOK_SLOT
buttons |= PlayerCommand.BUTTON_FIRE
# Fighting: face the target and fire, whatever the feet are doing.
if fighting or (target != null and target_visible and armed and not me.swimming and _fleeing):
@@ -207,7 +290,7 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
var aim: Vector2 = _aim_at(me, target, def)
yaw = aim.x
pitch = aim.y
if not _fleeing and _stage == OUTSIDE:
if not _fleeing and _stage == OUTSIDE and state != "breakwater":
direction = _combat_move(sim, grid, me, target, def)
buttons &= ~PlayerCommand.BUTTON_SPRINT
var in_range: bool = distance <= _fire_range(def)
@@ -228,21 +311,61 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
var reach: float = Harvest.eye(me).distance_to(target.position + Vector3(0.0, Movement.body_height(target) * 0.62, 0.0))
if reach <= Tuning.BOT_SWING_METRES and sim.tick - _acquired_tick >= _reaction_ticks:
buttons |= PlayerCommand.BUTTON_FIRE
elif covering:
# Behind the wall: heal, or finish reloading.
var heal_slot: int = _heal_slot(me)
if heal_slot >= 0:
selection = heal_slot + 1
buttons |= PlayerCommand.BUTTON_FIRE
elif me.reload_ticks > 0:
# Stay behind it until the magazine's full.
_covering_until = maxi(_covering_until, sim.tick + 2)
elif armed:
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
elif sim.tick - _cover_since > roundi(Tuning.BOT_COVER_HOLD_SECONDS * TICKS):
_covering_until = sim.tick
elif sim.tick - _cover_since > roundi(Tuning.BOT_COVER_HOLD_SECONDS * TICKS):
_covering_until = sim.tick
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
# Up a ramp toward someone above: keep walking, still shooting, until off the top of
# the last one.
var g: float = StructureSet.cell_size()
var on_last: bool = (_ramps > 0 and Vector2i(floori(me.position.x / g), floori(me.position.z / g)) == Vector2i(_ramp_last.x, _ramp_last.z)
and me.position.y > _ramp_last.y * Tuning.BUILD_LEVEL_METRES - 0.5)
if on_last:
_ramp_until = maxi(_ramp_until, sim.tick + TICKS / 2)
if sim.tick < _ramp_until and target != null and not _fleeing and not covering and _stage == OUTSIDE:
state = "ramp"
direction = _ramp_direction
if interact and not _interact_last:
buttons |= PlayerCommand.BUTTON_INTERACT
_interact_last = true
else:
_interact_last = false
var chosen_material: int = 0
if _piece_kind >= 0:
# This tick places a piece: build mode, facing where it goes, level.
yaw = _piece_yaw
pitch = 0.0
var cleared: int = PlayerCommand.BUTTON_AIM | PlayerCommand.BUTTON_RELOAD | PlayerCommand.BUTTON_INTERACT | PlayerCommand.BUTTON_SPRINT
buttons = (buttons & ~cleared) | PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE
selection = _piece_kind
chosen_material = _piece_material
if _piece_kind != StructureSet.RAMP:
direction = Vector2.ZERO
_track_fight(sim, world, me, buttons)
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)
return PlayerCommand.from_input(sim.tick, move, yaw, pitch, buttons, selection, chosen_material)
# Movement goals
@@ -382,7 +505,7 @@ func _can_see(sim: Simulation, world: BotWorld, me: SimEntity, other: SimEntity,
## BOT_RETREAT_SECONDS, then heals if it can. Any bot without a gun runs from one held
## in view; a bot badly hurt with a way to heal breaks off too, except easy ones.
func _check_retreat(sim: Simulation, world: BotWorld, me: SimEntity, target: SimEntity, armed: bool) -> void:
if target == null or _fleeing or sim.tick < _retreat_until or me.swimming:
if target == null or _fleeing or sim.tick < _retreat_until or sim.tick < _covering_until or me.swimming:
return
var outgunned: bool = not armed and target_visible and _holds_gun(target)
var hurt: bool = _retreat_health > 0 and me.health + me.armor <= _retreat_health and _heal_slot(me) >= 0
@@ -390,6 +513,10 @@ func _check_retreat(sim: Simulation, world: BotWorld, me: SimEntity, target: Sim
return
if sim.tick - _retreat_until < roundi(Tuning.BOT_RETREAT_SECONDS * TICKS):
return
if hurt and not outgunned and builds and sim.tick >= _cover_ready and _build_material(me, StructureSet.WALL) >= 0:
# M6: wall off and heal where it stands instead of running across open ground.
_take_cover(sim, me, target, Tuning.BOT_COVER_HEAL_SECONDS)
return
var away: Vector3 = me.position - target.position
away.y = 0.0
away = away.normalized() if away.length() > 0.1 else Vector3(1.0, 0.0, 0.0)
@@ -859,3 +986,339 @@ func _hunt(sim: Simulation, world: BotWorld) -> void:
_stop()
else:
_go(_hunt_goal, 4.0)
# Building (M6)
## Counts what the bot placed and gathered since it last thought.
func _count_building(world: BotWorld, me: SimEntity) -> void:
for kind: int in world.placed.get(player_id, []):
stats["pieces"] += 1
match kind:
StructureSet.WALL:
stats["walls"] += 1
StructureSet.RAMP:
stats["ramps"] += 1
StructureSet.BREAKWATER:
stats["breakwaters"] += 1
if _in_fight:
_fight_built = true
var stock: int = 0
for amount: int in me.materials:
stock += amount
if stock > _stock_seen:
stats["harvested"] += stock - _stock_seen
_stock_seen = stock
## Starts or ends the fight the bot is in, for the measurement.
func _track_fight(sim: Simulation, world: BotWorld, me: SimEntity, buttons: int) -> void:
var engaged: bool = target_id != 0 or state == "cover" or state == "retreat"
if engaged:
if not _in_fight:
_in_fight = true
_fight_traded = false
_fight_built = false
_fight_stocked = builds and _build_material(me, StructureSet.WALL) >= 0
_ramps = 0
_fight_seen = sim.tick
var shooting: bool = (buttons & PlayerCommand.BUTTON_FIRE) != 0 and (state == "fight" or state == "brawl" or state == "ramp")
if shooting or world.attackers.has(player_id):
_fight_traded = true
elif _in_fight and sim.tick - _fight_seen > roundi(Tuning.BOT_FIGHT_GAP_SECONDS * TICKS):
_end_fight(false)
func _end_fight(lost: bool) -> void:
_in_fight = false
if not _fight_traded:
return
stats["fights"] += 1
if _fight_built:
stats["fights_built"] += 1
if _fight_stocked:
stats["fights_stocked"] += 1
if _fight_built:
stats["fights_stocked_built"] += 1
if lost:
stats["fights_lost"] += 1
if _fight_built:
stats["fights_built_lost"] += 1
## Goes behind a wall, toward `target`, for `seconds`.
func _take_cover(sim: Simulation, me: SimEntity, target: SimEntity, seconds: float) -> void:
_covering_until = sim.tick + roundi(seconds * TICKS)
_cover_since = sim.tick
_cover_yaw = atan2(target.position.x - me.position.x, target.position.z - me.position.z)
_cover_walls = 0
_cover_ready = sim.tick + roundi(Tuning.BOT_COVER_SECONDS * TICKS)
## Chooses the piece, if any, this tick places. Sets `_piece_kind`.
func _plan_build(sim: Simulation, grid: NavGrid, world: BotWorld, me: SimEntity, target: SimEntity, distance: float, fighting: bool) -> void:
_piece_kind = -1
if not builds or me.swimming or not me.on_ground or me.action_cooldown > 0 or _stage != OUTSIDE:
return
var stranded: bool = _stranded(sim, world, me)
if _in_ring(sim, me) or stranded or _ring_due(sim, world, me):
if not _in_ring(sim, me):
_begin_ring(sim, me, stranded)
for facing: int in 4:
if _try_place(sim, me, StructureSet.BREAKWATER, facing * PI * 0.5):
state = "breakwater"
return
if state == "cover":
if _cover_walls == 0:
if _try_place(sim, me, StructureSet.WALL, _cover_yaw):
_cover_walls = 1
elif sim.tick - _cover_since > TICKS / 2:
# Nowhere to put a wall: give up on cover, and break off as before.
_covering_until = sim.tick
return
if target == null:
return
var toward: float = atan2(target.position.x - me.position.x, target.position.z - me.position.z)
var rise: float = target.position.y - me.position.y
var across: float = Vector2(target.position.x - me.position.x, target.position.z - me.position.z).length()
var climbing: bool = sim.tick < _ramp_until
var ramp_wanted: bool = ((fighting or state == "chase" or state == "ramp") and _ramps < Tuning.BOT_RAMP_MAX
and rise >= Tuning.BOT_RAMP_RISE_METRES and across <= Tuning.BOT_RAMP_REACH_METRES
and (climbing or rise / maxf(across, 0.1) >= Tuning.BOT_RAMP_SLOPE or _steep_between(grid, me, target))
and _build_material(me, StructureSet.RAMP) >= 0)
# Shot at while reloading, from ambush or when worn down: a wall to shelter behind,
# unless it's from up a cliff, where a wall would only block the way up.
var shot_at: bool = _hit_by == target.id and sim.tick - _hit_tick <= TICKS / 2
var exposed: bool = me.reload_ticks > 0 or sim.tick - _acquired_tick <= TICKS or me.health + me.armor <= Tuning.BOT_COVER_HEALTH
if not ramp_wanted and (fighting or state == "chase") and shot_at and exposed and distance >= Tuning.BOT_COVER_MIN_METRES and sim.tick >= _cover_ready:
if _try_place(sim, me, StructureSet.WALL, toward):
_take_cover(sim, me, target, Tuning.BOT_COVER_HOLD_SECONDS)
_cover_walls = 1
state = "cover"
return
# Someone up a steep slope or a cliff: ramp up to them.
if ramp_wanted:
# On the way up, the next ramp goes on from the top of the last: one cell on,
# one cell up. Until the bot is there, it keeps walking.
var facing: int = _ramp_last.w if climbing else posmod(roundi(toward / (PI * 0.5)), 4)
var yaw: float = facing * PI * 0.5
if not climbing:
# A ramp of its own already on the way (it broke off to take cover, say):
# walk on up it.
_probe.position = me.position
_probe.yaw = posmod(roundi(yaw / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS)
_probe.pitch = 0
var first: Building.Target = Building.target(_probe, StructureSet.RAMP)
for dy: int in [-1, 0, 1]:
var slot: Vector4i = Vector4i(first.slot.x, first.slot.y + dy, first.slot.z, first.slot.w)
var piece: StructureSet.Piece = sim.structures.find(sim.structures.at_slot(slot))
if piece != null and piece.kind == StructureSet.RAMP and piece.owner == player_id and piece.facing == facing:
_ramp_last = Vector4i(slot.x, slot.y, slot.z, facing)
var ahead: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
_ramp_direction = Vector2(ahead.x, ahead.y)
_ramp_until = sim.tick + roundi(Tuning.BOT_RAMP_WALK_SECONDS * TICKS)
return
if climbing:
_probe.position = me.position
_probe.yaw = posmod(roundi(yaw / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS)
_probe.pitch = 0
var next: Building.Target = Building.target(_probe, StructureSet.RAMP)
var step: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
var level_step: int = roundi(StructureSet.cell_size() / Tuning.BUILD_LEVEL_METRES)
if next.slot.x != _ramp_last.x + step.x or next.slot.z != _ramp_last.z + step.y or next.slot.y != _ramp_last.y + level_step:
return
if _try_place(sim, me, StructureSet.RAMP, yaw):
var placed: Building.Target = Building.target(_probe, StructureSet.RAMP)
_ramp_last = Vector4i(placed.slot.x, placed.slot.y, placed.slot.z, facing)
var step: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
_ramp_direction = Vector2(step.x, step.y)
_ramps += 1
_ramp_until = sim.tick + roundi(Tuning.BOT_RAMP_WALK_SECONDS * TICKS)
## True if the ground on the line to `target` is a cliff, or a solid that `target`
## stands up on, somewhere a bot can't walk straight up.
static func _steep_between(grid: NavGrid, me: SimEntity, target: SimEntity) -> bool:
var from: Vector2 = Vector2(me.position.x, me.position.z)
var to: Vector2 = Vector2(target.position.x, target.position.z)
var samples: int = ceili(from.distance_to(to) / Tuning.BOT_NAV_CELL_METRES)
for index: int in range(1, samples + 1):
var at: Vector2 = from.lerp(to, float(index) / samples)
var point: Vector2i = grid.point_of(Vector3(at.x, 0.0, at.y))
var cell: int = grid.index_of(point)
if grid.steep[cell] != 0:
return true
if grid.blocked[cell] > 0 and target.position.y > grid.heights[cell] + Tuning.BOT_RAMP_RISE_METRES * 0.5:
return true
return false
## Starts a ring of breakwaters round the bot's cell. Stranded, it's worth staying in
## while the coming level is within what a breakwater holds back; at the last stand,
## whatever comes.
func _begin_ring(sim: Simulation, me: SimEntity, stranded: bool) -> void:
var g: float = StructureSet.cell_size()
_ring_cell = Vector2i(floori(me.position.x / g), floori(me.position.z / g))
_ring_started = sim.tick
_ring_level = me.position.y + Tuning.BREAKWATER_MAX_HEAD - 1.0 if stranded else INF
_ring_slots.clear()
_probe.position = me.position
_probe.pitch = 0
for facing: int in 4:
_probe.yaw = posmod(roundi(facing * 0.25 * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS)
_ring_slots.append(Building.target(_probe, StructureSet.BREAKWATER).slot)
## True while the bot is in the cell it began ringing: for two seconds while the ring
## goes up, then for as long as all four stand and the coming level is one it holds.
func _in_ring(sim: Simulation, me: SimEntity) -> bool:
if _ring_started < 0:
return false
var g: float = StructureSet.cell_size()
if Vector2i(floori(me.position.x / g), floori(me.position.z / g)) != _ring_cell or me.swimming:
_ring_started = -1
return false
if sim.tide != null and sim.tide.forecast_at(sim.tick) > _ring_level:
_ring_started = -1
return false
if sim.tick - _ring_started < 2 * TICKS:
return true
for slot: Vector4i in _ring_slots:
if not sim.structures.occupied(slot):
_ring_started = -1
return false
return true
## True when the bot is running from the tide late in the match (the last
## BOT_STRAND_PHASES phases) and can't make it: the sea will reach its feet before it
## could walk to dry ground, though no higher than a breakwater holds back, and it can
## afford a ring.
func _stranded(sim: Simulation, world: BotWorld, me: SimEntity) -> bool:
if not _fleeing or sim.tide == null or world.phase_count() == 0:
return false
var material: int = _build_material(me, StructureSet.BREAKWATER)
if material < 0 or me.materials[material] < 4 * StructureSet.cost_of(StructureSet.BREAKWATER):
return false
if sim.tide.phase_at(sim.tick) < world.phase_count() - Tuning.BOT_STRAND_PHASES:
return false
var feet: float = me.position.y
if sim.tide.forecast_at(sim.tick) > feet + Tuning.BREAKWATER_MAX_HEAD - 1.0:
return false
if sim.water_level_at(me.position) > feet - 0.3:
return false
var walk: float = Vector2(_flee_goal.x - me.position.x, _flee_goal.z - me.position.z).length() / Tuning.BOT_FLEE_SPEED
for second: int in range(0, ceili(walk) + 1):
if sim.tide.level_at(sim.tick + second * TICKS) >= feet - 0.3:
return true
return false
## True at the last stand: the tide's phase leaves no dry ground, the bot is on the
## last high point, and the rise has begun or is BOT_RING_SECONDS off.
func _ring_due(sim: Simulation, world: BotWorld, me: SimEntity) -> bool:
if sim.tide == null or world.phase_count() == 0:
return false
var phase: int = clampi(sim.tide.phase_at(sim.tick), 0, world.phase_count() - 1)
if world.phase_cells[phase] != 0 or not world.is_safe(me.position, phase):
return false
return sim.tide.stage_at(sim.tick) != Tide.HOLD or sim.tide.ticks_until_rise(sim.tick) <= roundi(Tuning.BOT_RING_SECONDS * TICKS)
## The material to build `kind` in: whichever the bot has most of, if that covers the
## cost, or -1.
static func _build_material(me: SimEntity, kind: int) -> int:
var best: int = 0
for code: int in Materials.NAMES.size():
if me.materials[code] > me.materials[best]:
best = code
return best if me.materials[best] >= StructureSet.cost_of(kind) else -1
## Sets this tick's piece to `kind`, facing `yaw`, if the server would accept it from
## where the bot stands. Returns whether it did.
func _try_place(sim: Simulation, me: SimEntity, kind: int, yaw: float) -> bool:
var material: int = _build_material(me, kind)
if material < 0:
return false
_probe.position = me.position
_probe.yaw = posmod(roundi(yaw / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS)
_probe.pitch = 0
_probe.health = me.health
_probe.crouching = me.crouching
_probe.swimming = me.swimming
_probe.drop_state = me.drop_state
_probe.materials = me.materials
var at: Building.Target = Building.target(_probe, kind)
if Building.check(sim, _probe, at, material) != Building.Result.OK:
return false
_piece_kind = kind
_piece_yaw = yaw
_piece_material = material
return true
## Gathers materials with the boathook: carries on with the prop it's at, or, at most
## once a second and only when it's been quiet a while and the bot isn't hurt, looks
## for one within BOT_HARVEST_NEAR_METRES (`far` false) or
## BOT_HARVEST_METRES. Sets `state` to "gather" and returns true while it has one.
func _gather(sim: Simulation, world: BotWorld, me: SimEntity, far: bool) -> bool:
var stock: int = 0
for amount: int in me.materials:
stock = maxi(stock, amount)
var wanted: int = Tuning.BOT_BUILD_STOCK
if sim.tide != null and world.phase_count() > 0 and sim.tide.phase_at(sim.tick) >= world.phase_count() - Tuning.BOT_STRAND_PHASES:
# Late in the match: enough for a ring of breakwaters.
wanted = 4 * StructureSet.cost_of(StructureSet.BREAKWATER)
if stock >= wanted or not Harvest.can_swing(me):
_gather_prop = -1
return false
if _gather_prop >= 0 and (not sim.props.is_standing(_gather_prop) or sim.tick > _gather_deadline):
if sim.props.is_standing(_gather_prop):
_gather_skipped[_gather_prop] = true
_gather_prop = -1
if _gather_prop < 0:
var quiet: int = roundi(Tuning.BOT_GATHER_QUIET_SECONDS * TICKS)
if sim.tick - _last_seen_tick < quiet or sim.tick - _hit_tick < quiet or me.health + me.armor < Tuning.BOT_GATHER_HEALTH:
return false
var index: int = 1 if far else 0
if sim.tick - _gather_checked[index] < TICKS:
return false
_gather_checked[index] = sim.tick
_gather_prop = _find_gather_prop(sim, world, me, Tuning.BOT_HARVEST_METRES if far else Tuning.BOT_HARVEST_NEAR_METRES)
if _gather_prop < 0:
return false
var gap: float = PropSet._distance_to_box(sim.props.boxes[_gather_prop], me.position)
_gather_deadline = sim.tick + roundi((Tuning.BOT_HARVEST_SECONDS + gap / 3.0) * TICKS)
state = "gather"
_stop()
var box: AABB = sim.props.boxes[_gather_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
if PropSet._distance_to_box(box, eye) <= Harvest.reach() - 0.6:
_swing = true
_swing_aim = Vector2(atan2(to.x, to.z), atan2(to.y, Vector2(to.x, to.z).length()))
return true
_walk_to(me, Vector3(centre.x, box.position.y, centre.z), 0.0)
if _direct_stalled > 3 * TICKS:
_gather_skipped[_gather_prop] = true
_gather_prop = -1
return true
## The nearest standing prop of a building material within `radius`, on dry ground
## about level with the bot and outside huts and lofts, that it hasn't given up on.
func _find_gather_prop(sim: Simulation, world: BotWorld, me: SimEntity, radius: float) -> int:
for prop: int in sim.props.near(me.position, radius):
if _gather_skipped.has(prop) or not Materials.is_valid(Materials.code_of(sim.props.material_of(prop))):
continue
var box: AABB = sim.props.boxes[prop]
var foot: Vector3 = Vector3(box.get_center().x, box.position.y, box.get_center().z)
if absf(box.position.y - me.position.y) > 3.0 or not _spot_allowed(sim, world, foot):
continue
if world.in_routed_place(foot):
continue
return prop
return -1
+10 -1
View File
@@ -46,6 +46,8 @@ var walkable: PackedByteArray = PackedByteArray()
var landings: Array[Vector3] = []
## Victim entity ID -> attacker entity ID, for hits landed during the last tick.
var attackers: Dictionary = {}
## Builder entity ID -> the kinds of the pieces it placed during the last tick (M6).
var placed: Dictionary = {}
## `grid` is the match's navigation grid. Loot markers and safe ground that nothing can
@@ -120,12 +122,19 @@ func in_routed_place(position: Vector3) -> bool:
return false
## Notes who hit whom during the last tick. Call once per tick, before bots think.
## Notes who hit whom, and who placed what, during the last tick. Call once per tick,
## before bots think.
func observe(sim: Simulation) -> void:
attackers.clear()
placed.clear()
for event: Dictionary in sim.events:
if event["type"] == "player_hit" and int(event["player"]) != 0:
attackers[int(event["victim"])] = int(event["player"])
elif event["type"] == "piece_placed":
var builder: int = int(event["player"])
if not placed.has(builder):
placed[builder] = []
placed[builder].append(int(event["kind"]))
func phase_count() -> int:
+41
View File
@@ -324,6 +324,47 @@ const BOT_LOOT_TIDE_SECONDS: float = 30.0
## Bots give up on a loot spot or an item they can't get to after this long.
const BOT_LOOT_TIMEOUT_SECONDS: float = 45.0
const BOT_PICKUP_TIMEOUT_SECONDS: float = 8.0
## M6, bots that build. An armed bot with nothing more pressing gathers with the
## boathook until its best material reaches BOT_BUILD_STOCK (in the last
## BOT_STRAND_PHASES tide phases, enough for four breakwaters): from a prop within
## BOT_HARVEST_NEAR_METRES while it loots, or BOT_HARVEST_METRES once it has nothing
## left to loot. It gives up on a prop after BOT_HARVEST_SECONDS. It doesn't start
## within BOT_GATHER_QUIET_SECONDS of last seeing an opponent or being hit, or with
## health and armor under BOT_GATHER_HEALTH.
const BOT_BUILD_STOCK: int = 60
const BOT_GATHER_QUIET_SECONDS: float = 10.0
const BOT_GATHER_HEALTH: int = 60
const BOT_HARVEST_NEAR_METRES: float = 12.0
const BOT_HARVEST_METRES: float = 24.0
const BOT_HARVEST_SECONDS: float = 12.0
## Cover: a bot shot while it reloads, within a second of first seeing its shooter, or
## with health and armor down to BOT_COVER_HEALTH, puts a wall between them, and waits
## behind it at least BOT_COVER_HOLD_SECONDS and until any reload is done. Only for a
## shooter at least BOT_COVER_MIN_METRES off, and no more often than BOT_COVER_SECONDS.
const BOT_COVER_HOLD_SECONDS: float = 2.0
const BOT_COVER_HEALTH: int = 60
const BOT_COVER_MIN_METRES: float = 6.0
const BOT_COVER_SECONDS: float = 5.0
## A badly hurt bot that could break off (BOT_DIFFICULTIES "retreat") and can afford a
## wall walls itself off and heals there instead, for up to this long.
const BOT_COVER_HEAL_SECONDS: float = 7.0
## Ramps: up toward an opponent at least BOT_RAMP_RISE_METRES higher, within
## BOT_RAMP_REACH_METRES across, and up ground at least this steep (rise over run).
## At most BOT_RAMP_MAX ramps a fight; the bot walks on BOT_RAMP_WALK_SECONDS after each.
const BOT_RAMP_RISE_METRES: float = 3.0
const BOT_RAMP_REACH_METRES: float = 24.0
const BOT_RAMP_SLOPE: float = 0.5
const BOT_RAMP_MAX: int = 5
const BOT_RAMP_WALK_SECONDS: float = 1.5
## At the last stand (a tide phase that leaves no dry ground), a bot on the last high
## point rings its own cell with breakwaters once the rise is this close.
const BOT_RING_SECONDS: float = 30.0
## In the last this many tide phases, a bot running from the tide that can't reach dry
## ground before the sea reaches its feet rings its cell with breakwaters instead, if
## the coming level is one they hold back (BREAKWATER_MAX_HEAD, less a metre).
const BOT_STRAND_PHASES: int = 3
## A fight ends for the measurement (M6) once the bot has had nobody to fight this long.
const BOT_FIGHT_GAP_SECONDS: float = 4.0
# Building
const GRID_CELL_METRES: float = 4.0