From d5c8019d119d90fb70d851777b758145fd4cb65d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 21:10:48 -0500 Subject: [PATCH] H9: Boathook swings damage players; unarmed bots brawl - Harvest.swing traces the swing as a shot is traced (Weapons.trace, lag compensated). A player reached before terrain, a solid, a prop's grown box or a built piece takes the boathook's damage through Weapons.damage_player, so armor, the kill feed and stats count it. A level swing meets the head and takes the headshot multiplier, as a shot does. - Unarmed bots go for an opponent in view within BOT_BRAWL_METRES (6 m) and swing within BOT_SWING_METRES; inside a hut they swing back without walking in. - Tests: harvest +5 (hit, armor first, out of reach, wall between, player in front of a tree), bot brain +2 (closes in and lands hits; leaves a distant opponent alone). - test_world_replication: an unarmed bot now meets the scripted player early on seed 2, so the test gives it full armor on landing so it lives to build; all assertions unchanged. Closes #73 Co-Authored-By: Claude Opus 5.5 --- src/bots/bot_brain.gd | 22 ++++++- src/sim/harvest.gd | 31 ++++++++- src/sim/simulation.gd | 2 +- src/sim/tuning.gd | 4 ++ test/bots/test_bot_brain.gd | 31 +++++++++ test/server/test_world_replication.gd | 8 +++ test/sim/test_harvest.gd | 93 +++++++++++++++++++++++++++ 7 files changed, 186 insertions(+), 5 deletions(-) diff --git a/src/bots/bot_brain.gd b/src/bots/bot_brain.gd index feba449..830ca2f 100644 --- a/src/bots/bot_brain.gd +++ b/src/bots/bot_brain.gd @@ -9,7 +9,8 @@ ## 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 +## a visible opponent if armed, or go for one within Tuning.BOT_BRAWL_METRES with the +## 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. class_name BotBrain @@ -116,6 +117,9 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: 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 + # Inside a hut or loft too: someone beating on you there gets a swing back. + var brawling: bool = (target != null and target_visible and not armed and not me.swimming + and not _fleeing and distance <= Tuning.BOT_BRAWL_METRES) var interact: bool = false var walk: bool = true @@ -134,6 +138,9 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: elif fighting: state = "fight" _stop() + elif brawling: + state = "brawl" + _stop() elif target != null and armed: state = "chase" _go(_last_seen, 3.0) @@ -191,6 +198,19 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand: buttons |= PlayerCommand.BUTTON_FIRE if distance > 10.0: buttons |= PlayerCommand.BUTTON_AIM + elif brawling: + # No gun: walk in and swing the boathook. + var aim: Vector2 = _aim_at(me, target, ItemCatalog.boathook()) + yaw = aim.x + pitch = aim.y + var to: Vector2 = Vector2(target.position.x - me.position.x, target.position.z - me.position.z) + var gap: float = to.length() + if _stage == OUTSIDE: + direction = to / gap if gap > Tuning.PLAYER_RADIUS * 2.0 + 0.6 else Vector2.ZERO + buttons &= ~PlayerCommand.BUTTON_SPRINT + 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 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]) diff --git a/src/sim/harvest.gd b/src/sim/harvest.gd index 3268604..f853073 100644 --- a/src/sim/harvest.gd +++ b/src/sim/harvest.gd @@ -7,6 +7,11 @@ ## grown by Tuning.HARVEST_AIM_SLACK, the ray enters, unless terrain or another solid is ## in the way first. Each hit does Tuning.HARVEST_DAMAGE to the prop and gives the ## player Tuning.HARVEST_PER_HIT of the prop's material, up to Tuning.MATERIAL_CAP. +## +## A swing also fights. The same ray is traced as a shot is (Weapons.trace, with lag +## compensation), and another player it reaches before terrain, a solid, a prop's grown +## box or a built piece takes the boathook's `damage` through Weapons.damage_player, so +## armor, the kill feed and stats count it like any other hit. class_name Harvest extends RefCounted @@ -95,16 +100,19 @@ static func _prop_hit(player: SimEntity, ground: SimGround, props: PropSet) -> V ## Swings if the player can and isn't cooling down. Returns the prop hit, or -1. ## Appends "harvest" and "prop_destroyed" events to `events`. A built piece nearer than -## any prop takes the hit instead, losing Tuning.HARVEST_DAMAGE and giving nothing. -static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary], structures: StructureSet = null) -> int: +## any prop takes the hit instead, losing Tuning.HARVEST_DAMAGE and giving nothing. With +## `sim`, a player nearer than both takes the hit instead (see the class notes). +static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary], structures: StructureSet = null, sim: Simulation = null) -> int: if player.action_cooldown > 0 or not can_swing(player): return -1 player.action_cooldown = swing_interval_ticks() # Every swing, hit or miss, for whoever draws or hears it. events.append({"type": "swing", "player": player.id, "from": eye(player), "direction": look_direction(player)}) var hit: Vector2 = _prop_hit(player, ground, props) + var piece_hit: Vector2 = _piece_hit(player, ground, structures) if structures != null else Vector2(1.0, 0.0) + if sim != null and _hit_player(sim, player, minf(hit.x, piece_hit.x)): + return -1 if structures != null: - var piece_hit: Vector2 = _piece_hit(player, ground, structures) if piece_hit.y != 0.0 and piece_hit.x <= hit.x: events.append({"type": "piece_hit", "player": player.id, "piece": int(piece_hit.y), "amount": Tuning.HARVEST_DAMAGE}) structures.damage(int(piece_hit.y), Tuning.HARVEST_DAMAGE, "boathook", ground, events) @@ -118,3 +126,20 @@ static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: if props.damage(prop, Tuning.HARVEST_DAMAGE): events.append({"type": "prop_destroyed", "prop": prop}) return prop + + +## Traces the swing for players. If one is hit nearer than `nearest` (the fraction of +## the swing where a prop or piece would be hit), damages them and returns true. +static func _hit_player(sim: Simulation, player: SimEntity, nearest: float) -> bool: + var from: Vector3 = eye(player) + var to: Vector3 = from + look_direction(player) * reach() + var hit: Weapons.Hit = Weapons.trace(sim, from, to, player.id, Weapons.view_time(player, sim.tick)) + if hit.kind != Weapons.HIT_PLAYER or hit.fraction > nearest: + return false + var def: ItemDef = ItemCatalog.boathook() + var amount: int = def.damage + if hit.headshot: + amount = roundi(amount * def.headshot_multiplier) + sim.events.append({"type": "impact", "player": player.id, "item": def.code, "point": hit.point, "hit": hit.kind, "target": hit.target}) + Weapons.damage_player(sim, sim.entities.find(hit.target), amount, player.id, hit.headshot, def.code) + return true diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index 21ddf30..ffc9fa1 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -249,7 +249,7 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void: Building.act(self, player, command) return if player.selected == BOATHOOK_SLOT: - Harvest.swing(player, ground, props, events, structures) + Harvest.swing(player, ground, props, events, structures, self) else: var def: ItemDef = Inventory.held_def(player) if def != null and def.kind == ItemDef.CONSUMABLE: diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index f68e4d7..4e98c53 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -290,6 +290,10 @@ const BOT_AIM_WANDER_TICKS: int = 12 const BOT_BLOOM_PAUSE_DEGREES: float = 3.0 ## Bots notice items lying within this distance. const BOT_LOOT_SIGHT_METRES: float = 12.0 +## A bot with no gun goes for an opponent in view this close with its boathook, in +## metres, and swings once its eye is this far from the opponent's body. +const BOT_BRAWL_METRES: float = 6.0 +const BOT_SWING_METRES: float = 2.3 ## The most weapons a bot carries; its other slots are for consumables. const BOT_MAX_WEAPONS: int = 3 ## Ground counts as safe from the next tide level when it stands this far above it. diff --git a/test/bots/test_bot_brain.gd b/test/bots/test_bot_brain.gd index d29a8ef..ce28bf9 100644 --- a/test/bots/test_bot_brain.gd +++ b/test/bots/test_bot_brain.gd @@ -322,3 +322,34 @@ func test_a_bot_only_match_ends_with_a_winner_and_replays_exactly() -> void: var again: Dictionary = BotBatch.run_match(20261112, 10) assert_eq(again["state_hash"], result["state_hash"], "the same seed plays the same match") assert_eq(again["ticks"], result["ticks"]) + + +func test_an_unarmed_bot_closes_in_and_swings_its_boathook() -> void: + _setup() + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var victim: SimEntity = _player(HOME + Vector3(4.5, 0.0, 0.0)) + var start: int = victim.health + _ready_world() + var swings: Array[int] = [0] + for _tick: int in 6 * TICKS: + _run(1) + for event: Dictionary in sim.events: + if event["type"] == "player_hit" and event["victim"] == victim.id and event["player"] == brain.player_id: + swings[0] += 1 + if swings[0] >= 2: + break + assert_eq(brain.state, "brawl") + assert_true(swings[0] >= 2, "landed boathook hits (%d)" % swings[0]) + assert_true(victim.health < start) + var me: SimEntity = sim.entities.find(brain.player_id) + assert_true(Vector2(me.position.x - victim.position.x, me.position.z - victim.position.z).length() < 3.0, "closed in") + + +func test_an_unarmed_bot_leaves_a_distant_opponent_alone() -> void: + _setup() + var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) + var victim: SimEntity = _player(HOME + Vector3(Tuning.BOT_BRAWL_METRES + 8.0, 0.0, 0.0)) + _ready_world() + _run(3 * TICKS) + assert_ne(brain.state, "brawl") + assert_eq(victim.health, Tuning.PLAYER_MAX_HEALTH) diff --git a/test/server/test_world_replication.gd b/test/server/test_world_replication.gd index cf3fa91..0f39fa1 100644 --- a/test/server/test_world_replication.gd +++ b/test/server/test_world_replication.gd @@ -104,10 +104,18 @@ func test_a_clients_world_agrees_with_the_servers_through_a_scripted_match() -> var carried_checks: int = 0 var most_pending: int = 0 var most_pieces: int = 0 + var scripted: SimEntity = sim.entities.find(loop.scripted[0].player_id) + var shielded: bool = false while flow.phase != Match.RESULTS and loop.ticks < LIMIT_TICKS and differed == 0: _round() most_pending = maxi(most_pending, client.replica.pending_count()) most_pieces = maxi(most_pieces, sim.structures.count()) + if not shielded and scripted.drop_state == Drop.NONE and scripted.on_ground: + # Since H9 unarmed bots brawl, and on this seed one meets the scripted player + # early. Full armor keeps it alive to harvest and build, which this test needs + # to replicate; the fight itself isn't what's under test. + shielded = true + scripted.armor = Tuning.PLAYER_MAX_ARMOR if not landed and mine.drop_state == Drop.NONE and mine.on_ground and not mine.eliminated: # On the ground: hand it driftwood for two walls and a floor, and let it # try the walls. diff --git a/test/sim/test_harvest.gd b/test/sim/test_harvest.gd index 2ff06f1..9052d6d 100644 --- a/test/sim/test_harvest.gd +++ b/test/sim/test_harvest.gd @@ -226,3 +226,96 @@ func test_every_swing_reports_itself_hit_or_miss() -> void: Harvest.swing(player, sim.ground, sim.props, events, sim.structures) assert_eq(events.size(), 1, "not again until the boathook is ready") + + +# H9: swings hit players. + +## A sim with an attacker at (100, 0, 100) facing +z and a victim `gap` metres from +## the attacker's body along +z. +func _duel(gap: float) -> Simulation: + var sim: Simulation = _world([]) + var attacker: SimEntity = sim.entities.find(1) + attacker.drop_state = Drop.NONE + attacker.on_ground = true + var victim: SimEntity = sim.entities.find(sim.add_player()) + victim.drop_state = Drop.NONE + victim.on_ground = true + victim.position = attacker.position + Vector3(0.0, 0.0, Tuning.PLAYER_RADIUS * 2.0 + gap) + return sim + + +## Health and armor a boathook hit takes, headshot or not. +func _boathook(headshot: bool) -> int: + var def: ItemDef = ItemCatalog.boathook() + return roundi(def.damage * (def.headshot_multiplier if headshot else 1.0)) + + +func _hits_on(sim: Simulation, victim: int) -> Array[Dictionary]: + var hits: Array[Dictionary] = [] + for event: Dictionary in sim.events: + if event["type"] == "player_hit" and event["victim"] == victim: + hits.append(event) + return hits + + +func test_a_swing_damages_a_player_in_reach() -> void: + var sim: Simulation = _duel(1.0) + var victim: SimEntity = sim.entities.find(2) + var start: int = victim.health + _swing_for(sim, 1) + var hits: Array[Dictionary] = _hits_on(sim, 2) + assert_eq(hits.size(), 1, "one hit") + assert_true(hits[0]["headshot"], "a level swing from the eye meets the head") + assert_eq(victim.health, start - _boathook(true)) + assert_eq(hits[0]["player"], 1, "the kill feed knows who") + assert_eq(hits[0]["item"], ItemCatalog.boathook().code, "and with what") + assert_eq(victim.last_attacker, 1) + assert_eq(victim.last_cause, SimEntity.CAUSE_WEAPON) + + +func test_armor_takes_a_swing_first() -> void: + var sim: Simulation = _duel(1.0) + var victim: SimEntity = sim.entities.find(2) + victim.armor = 50 + var start: int = victim.health + # Looking down a little, at the body. + _swing_for(sim, 1, -0.35) + var hits: Array[Dictionary] = _hits_on(sim, 2) + assert_eq(hits.size(), 1) + assert_false(hits[0]["headshot"]) + assert_eq(victim.armor, 50 - _boathook(false)) + assert_eq(victim.health, start) + + +func test_a_player_out_of_reach_is_missed() -> void: + var sim: Simulation = _duel(Harvest.reach() + 0.3) + var victim: SimEntity = sim.entities.find(2) + var start: int = victim.health + _swing_for(sim, Harvest.swing_interval_ticks() * 2) + assert_eq(_hits_on(sim, 2).size(), 0) + assert_eq(victim.health, start) + + +func test_a_wall_between_stops_the_swing() -> void: + var sim: Simulation = _duel(1.5) + var attacker: SimEntity = sim.entities.find(1) + var victim: SimEntity = sim.entities.find(2) + var start: int = victim.health + # A fixed solid between them, as a point of interest's wall would be. + var wall_z: float = attacker.position.z + Tuning.PLAYER_RADIUS + 0.5 + sim.ground.add_solid(AABB(Vector3(95.0, -1.0, wall_z), Vector3(10.0, 4.0, 0.3))) + _swing_for(sim, Harvest.swing_interval_ticks() * 2) + assert_eq(_hits_on(sim, 2).size(), 0) + assert_eq(victim.health, start) + + +func test_a_player_in_front_of_a_tree_takes_the_swing_not_the_tree() -> void: + var sim: Simulation = _world([_pine_ahead(2.2)]) + sim.entities.find(1).drop_state = Drop.NONE + var victim: SimEntity = sim.entities.find(sim.add_player()) + victim.drop_state = Drop.NONE + victim.position = Vector3(100.0, 0.0, 100.0 + Tuning.PLAYER_RADIUS * 2.0 + 0.6) + var start: int = victim.health + assert_eq(_swing_for(sim, 1), 0, "no harvest") + assert_eq(victim.health, start - _boathook(true)) + assert_eq(sim.props.health[0], 200, "the tree is untouched")