Files
tidewall/test/sim/test_harvest.gd
T
claudeandClaude Opus 5.5 d5c8019d11 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>
2026-11-13 21:10:48 -05:00

322 lines
13 KiB
GDScript

extends TestCase
## Harvesting from issue B1: server-side range and swing rate, yields, caps, and props
## destroyed by harvesting.
const PINE: int = 0
const BOULDER: int = 2
const WRECK: int = 3
## A flat world with the given props, as [kind code, AABB] pairs, and one player at
## `feet` looking along +z.
func _world(entries: Array, feet: Vector3 = Vector3(100.0, 0.0, 100.0)) -> Simulation:
var ground: SimGround = SimGround.flat()
var sim: Simulation = Simulation.new(7, ground)
var props: PropSet = PropSet.new()
for entry: Array in entries:
var box: AABB = entry[1]
props.kinds.append(entry[0])
props.positions.append(Vector3(box.get_center().x, 0.0, box.get_center().z))
props.yaws.append(0.0)
props.boxes.append(box)
props.health.append(Tuning.PROP_KINDS[PropSet.KINDS[entry[0]]]["health"])
props.add_solids_to(ground)
sim.props = props
var id: int = sim.add_player()
sim.entities.find(id).position = feet
return sim
## A pine trunk whose near face is `gap` metres in front of the player's body (+z).
func _pine_ahead(gap: float) -> Array:
var near_z: float = 100.0 + Tuning.PLAYER_RADIUS + gap
return [PINE, AABB(Vector3(99.7, -0.25, near_z), Vector3(0.6, 9.0, 0.6))]
## Holds fire with the boathook for `ticks` ticks, looking along `pitch` radians.
func _swing_for(sim: Simulation, ticks: int, pitch: float = 0.0, buttons: int = PlayerCommand.BUTTON_FIRE, selection: int = 0) -> int:
var hits: int = 0
for _i: int in ticks:
var command: PlayerCommand = PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, pitch, buttons, selection)
sim.submit_command(1, command)
sim.step()
for event: Dictionary in sim.events:
if event["type"] == "harvest":
hits += 1
return hits
func test_a_hit_yields_the_props_material_and_damages_it() -> void:
var sim: Simulation = _world([_pine_ahead(0.5)])
var player: SimEntity = sim.entities.find(1)
assert_eq(_swing_for(sim, 1), 1, "one swing should land")
assert_eq(player.materials[Materials.DRIFTWOOD], Tuning.HARVEST_PER_HIT)
assert_eq(player.materials[Materials.STONE], 0)
assert_eq(sim.props.health[0], 200 - Tuning.HARVEST_DAMAGE)
assert_eq(_harvest(sim)["material"], Materials.DRIFTWOOD)
func test_each_prop_kind_gives_its_own_material() -> void:
for entry: Array in [[BOULDER, Materials.STONE], [WRECK, Materials.SALVAGE_IRON], [PINE, Materials.DRIFTWOOD]]:
var prop: Array = _pine_ahead(0.3)
prop[0] = entry[0]
var sim: Simulation = _world([prop])
_swing_for(sim, 1)
var player: SimEntity = sim.entities.find(1)
assert_eq(player.materials[entry[1]], Tuning.HARVEST_PER_HIT, PropSet.KINDS[entry[0]])
func test_swing_rate_is_enforced_by_the_server() -> void:
var sim: Simulation = _world([_pine_ahead(0.5)])
var interval: int = Harvest.swing_interval_ticks()
assert_eq(interval, ceili(30.0 / 1.5))
# Holding fire for three intervals swings exactly three times, however often the
# client says fire.
assert_eq(_swing_for(sim, interval * 3), 3)
assert_eq(sim.entities.find(1).materials[Materials.DRIFTWOOD], Tuning.HARVEST_PER_HIT * 3)
func test_out_of_range_props_are_not_hit() -> void:
var reach_gap: float = Harvest.reach() - Tuning.PLAYER_RADIUS
var far: Simulation = _world([_pine_ahead(reach_gap + Tuning.HARVEST_AIM_SLACK + 0.3)])
assert_eq(_swing_for(far, 1), 0, "a pine beyond reach must not be hit")
assert_eq(far.entities.find(1).materials[Materials.DRIFTWOOD], 0)
assert_eq(far.props.health[0], 200)
var near: Simulation = _world([_pine_ahead(reach_gap - 0.2)])
assert_eq(_swing_for(near, 1), 1, "a pine just inside reach should be hit")
func test_looking_away_misses() -> void:
var sim: Simulation = _world([_pine_ahead(0.5)])
var hits: int = 0
for _i: int in 1:
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, PI, 0.0, PlayerCommand.BUTTON_FIRE))
sim.step()
hits += sim.events.filter(func(event: Dictionary) -> bool: return event["type"] != "swing").size()
assert_eq(hits, 0)
func test_a_solid_in_the_way_blocks_the_swing() -> void:
var sim: Simulation = _world([_pine_ahead(1.2)])
# A wall between the player and the pine.
sim.ground.add_solid(AABB(Vector3(98.0, 0.0, 100.0 + Tuning.PLAYER_RADIUS + 0.4), Vector3(4.0, 3.0, 0.2)))
assert_eq(_swing_for(sim, 1), 0)
assert_eq(sim.props.health[0], 200)
func test_nearest_prop_takes_the_hit() -> void:
var near: Array = _pine_ahead(0.3)
var far: Array = [BOULDER, AABB(Vector3(99.0, -0.25, 101.5), Vector3(2.0, 2.0, 1.0))]
var sim: Simulation = _world([far, near])
_swing_for(sim, 1)
assert_eq(sim.props.health[1], 200 - Tuning.HARVEST_DAMAGE)
assert_eq(sim.props.health[0], 400)
func test_carry_cap_stops_gains_but_not_damage() -> void:
var sim: Simulation = _world([[BOULDER, AABB(Vector3(99.0, -0.25, 100.6), Vector3(2.0, 2.0, 2.0))]])
var player: SimEntity = sim.entities.find(1)
player.materials[Materials.STONE] = Tuning.MATERIAL_CAP - 5
_swing_for(sim, 1)
assert_eq(player.materials[Materials.STONE], Tuning.MATERIAL_CAP)
assert_eq(_harvest(sim)["amount"], 5, "the event reports what was actually gained")
_swing_for(sim, Harvest.swing_interval_ticks())
assert_eq(player.materials[Materials.STONE], Tuning.MATERIAL_CAP, "never over the cap")
assert_eq(sim.props.health[0], 400 - Tuning.HARVEST_DAMAGE * 2, "capped players still damage props")
func test_materials_add_and_spend() -> void:
var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER)
assert_eq(Materials.add(player, Materials.STONE, 30), 30)
assert_eq(Materials.add(player, Materials.STONE, Tuning.MATERIAL_CAP), Tuning.MATERIAL_CAP - 30)
assert_eq(Materials.add(player, 9, 10), 0, "unknown material")
assert_false(Materials.spend(player, Materials.DRIFTWOOD, 1))
assert_true(Materials.spend(player, Materials.STONE, 100))
assert_eq(player.materials[Materials.STONE], Tuning.MATERIAL_CAP - 100)
assert_false(Materials.spend(player, Materials.STONE, -5), "negative spends are refused")
for name: String in Materials.NAMES:
assert_true(Tuning.MATERIALS.has(name), name + " must be in Tuning.MATERIALS")
func test_harvesting_destroys_a_prop_and_clears_its_collision() -> void:
var sim: Simulation = _world([_pine_ahead(0.5)])
var hits_needed: int = ceili(200.0 / Tuning.HARVEST_DAMAGE)
var destroyed: int = 0
for _i: int in hits_needed * Harvest.swing_interval_ticks():
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, PlayerCommand.BUTTON_FIRE))
sim.step()
for event: Dictionary in sim.events:
if event["type"] == "prop_destroyed":
destroyed += 1
assert_eq(destroyed, 1)
assert_false(sim.props.is_standing(0))
assert_eq(sim.entities.find(1).materials[Materials.DRIFTWOOD], Tuning.HARVEST_PER_HIT * hits_needed)
assert_eq(sim.props.prop_of_solid(0), -1)
# The player can now walk through where the trunk stood.
for _i: int in 30:
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), 0.0, 0.0))
sim.step()
assert_true(sim.entities.find(1).position.z > 102.0, "the felled pine should no longer block")
# Further swings at the empty spot do nothing.
assert_eq(_swing_for(sim, Harvest.swing_interval_ticks()), 0)
func test_no_harvesting_in_build_mode_with_another_slot_or_while_swimming() -> void:
var sim: Simulation = _world([_pine_ahead(0.5)])
assert_eq(_swing_for(sim, 1, 0.0, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD), 0, "build mode")
assert_eq(_swing_for(sim, 1, 0.0, PlayerCommand.BUTTON_FIRE, 2), 0, "another slot")
sim.entities.find(1).swimming = true
assert_false(Harvest.can_swing(sim.entities.find(1)))
sim.entities.find(1).swimming = false
sim.entities.find(1).drop_state = Drop.GLIDING
assert_false(Harvest.can_swing(sim.entities.find(1)))
func test_harvesting_on_a_real_island_is_deterministic() -> void:
var hashes: Array[String] = []
for _run: int in 2:
var sim: Simulation = Simulation.new(11)
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
# Stand just west of the first pine and face it.
var tree: int = -1
for prop: int in sim.props.count():
if sim.props.kind_name(prop) == "pine":
var box: AABB = sim.props.boxes[prop]
var feet: Vector3 = Vector3(box.position.x - 0.8, 0.0, box.get_center().z)
feet.y = sim.ground.terrain_height(feet.x, feet.z)
if not sim.ground.body_blocked(feet, Tuning.PLAYER_RADIUS, Tuning.PLAYER_HEIGHT, Tuning.STEP_HEIGHT):
player.position = feet
tree = prop
break
assert_true(tree >= 0)
var gained: int = 0
for _i: int in Harvest.swing_interval_ticks() * 2:
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, PI * 0.5, 0.0, PlayerCommand.BUTTON_FIRE))
sim.step()
gained = player.materials[Materials.DRIFTWOOD]
assert_eq(gained, Tuning.HARVEST_PER_HIT * 2, "two swings at a real pine")
hashes.append(sim.state_hash())
assert_eq(hashes[0], hashes[1])
## This tick's harvest event; every swing also makes a "swing" event before it.
func _harvest(sim: Simulation) -> Dictionary:
for event: Dictionary in sim.events:
if event["type"] == "harvest":
return event
return {}
func test_every_swing_reports_itself_hit_or_miss() -> void:
var sim: Simulation = Simulation.new(5, SimGround.flat(0.0))
var player: SimEntity = sim.entities.find(sim.add_player())
player.drop_state = Drop.NONE
player.on_ground = true
player.position = Vector3(10.0, 0.0, 10.0)
player.pitch = PlayerCommand.PITCH_MAX / 2
var events: Array[Dictionary] = []
Harvest.swing(player, sim.ground, sim.props, events, sim.structures)
assert_eq(events.size(), 1, "a miss: %s" % [events])
assert_eq(events[0]["type"], "swing")
assert_eq(events[0]["player"], player.id)
var direction: Vector3 = events[0]["direction"]
assert_true(direction.y > 0.5, "along the look")
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")