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 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 21:10:48 -05:00
co-authored by Claude Opus 5.5
parent 923876ed1f
commit d5c8019d11
7 changed files with 186 additions and 5 deletions
+31
View File
@@ -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)
+8
View File
@@ -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.
+93
View File
@@ -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")