Files
tidewall/test/sim/test_harvest.gd
T
claudeandClaude Opus 5.5 07738330b5 A2: Player figure and animation
The islander is built in headless Blender (tools/models/figure.py,
`make figure`, `make art`) as twelve rigid parts with their origins at
their joints: an oilskin coat, a sou'wester, sea boots, a bedding roll
and the canvas wing-sail. Debian's Blender can't write glTF, so the
figure is jointed rather than skinned.

FigureRig hangs the parts on pivots, paints them from Palette with the
coat in the player's own oilskin, and plays FigurePoses' hand-written
key poses through one AnimationTree: idle, walk and run by speed,
crouch and creep, air, skydive, glide, swim and aboard; long, short and
carried holds; reload and use layers; fire, boathook swing, keg throw
and building as one-shots; and a flinch when health drops. The chest
takes a third of the pitch and the held item all of it.

Figures animate from replicated state only: positions, look, health,
the held item and the FLAG_ bits (reloading and using now reach
mirrored players), with actions from FX. Every boathook swing is now a
sim "swing" event, sent as a SHOT with no item, so misses are seen and
heard; the new FX kind PLACE shows pieces placed. Protocol version 4.

Tests: test_figure_rig (15), plus swing and PLACE cases in test_harvest
and test_net_fx. make check passes (604 tests); make client-check
passes. Captures: figure_states, figure_actions, figure_close
(make figure-captures), client_glide and client_landed.

Closes #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 17:35:53 -05:00

229 lines
9.7 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")