B1: Harvesting and materials
Boathook swings harvest props. The client only presses fire with the boathook selected; the server casts the swing from its own copy of the player's eye and look, so range and swing rate are enforced by construction. Each hit damages the prop and gives its material (driftwood, stone or salvage iron), capped per material. - Materials: material codes and add/spend helpers; carried stock lives on SimEntity.materials and is part of the state hash. - Harvest: swing interval from the boathook's fire rate, reach from its range plus slack, nearest prop along the ray, blocked by terrain or other solids. Emits harvest and prop_destroyed events. - Simulation: per-tick events list, a shared action cooldown, and fire with selection 0 outside build mode swings. - Tuning: HARVEST_DAMAGE, HARVEST_REACH_SLACK, HARVEST_AIM_SLACK. - 12 tests: yields per kind, rate limit, reach, blocking, nearest prop, caps, destruction clearing collision, state gating and determinism on a real island. Closes #15 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
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(sim.events[0]["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.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(sim.events[0]["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])
|
||||
Reference in New Issue
Block a user