Files
tidewall/test/sim/test_weapons.gd
T
claudeandClaude Opus 5.5 ed46b6e19f B6: Weapon framework and damage model
Weapons fire from the server's own copy of the shooter's eye and look, at the
weapon's fire rate, from the loaded magazine. An empty trigger pull or the
reload button starts a reload of the weapon's reload time from carried
ammunition; switching slots cancels it. Each shot is one or more pellets
scattered evenly inside a spread cone from the "weapons" stream. Spread is the
hip spread (smaller when aiming), plus bloom that builds in sustained fire,
plus a moving or airborne penalty.

Hitscan pellets trace at once in 16 m segments; projectile weapons (the
whaler's longgun and line thrower) spawn entities that fly under gravity and
trace each tick. A trace stops at the first of terrain, a solid (built pieces
and props take the damage) or another player's body. Players lose armor first,
then health; the top of the body takes the weapon's headshot multiplier; and
damage falls off linearly from range_full to range_max. Stats come from
ItemDef, so adding a weapon is a Tuning entry only.

Closes #20

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

314 lines
13 KiB
GDScript

extends TestCase
## The weapon framework from issue B6: fire-rate limits, magazines and reloads, spread,
## damage falloff, headshots, armor, and shots that stop at terrain, players and
## structures. Everything goes through commands, so the server's checks are tested.
const SHOOTER_FEET: Vector3 = Vector3(100.0, 0.0, 100.0)
func _code(id: String) -> int:
return ItemCatalog.code_of(id)
## A flat world with a shooter (ID 1) facing +z, holding `weapon` in slot 1.
func _sim(weapon: String, ammo: int = 0) -> Simulation:
var sim: Simulation = Simulation.new(5, SimGround.flat())
var shooter: SimEntity = sim.entities.find(sim.add_player())
shooter.position = SHOOTER_FEET
shooter.on_ground = true
Inventory.give(shooter, _code(weapon), 1)
var def: ItemDef = ItemCatalog.get_def(_code(weapon))
if ammo > 0:
Inventory.give(shooter, def.ammo_code, ammo)
shooter.selected = 1
return sim
## Adds a target standing `distance` metres in front of the shooter.
func _target_at(sim: Simulation, distance: float) -> SimEntity:
var target: SimEntity = sim.entities.find(sim.add_player())
target.position = SHOOTER_FEET + Vector3(0.0, 0.0, distance)
target.on_ground = true
return target
## The pitch, in radians, that aims the shooter's eye at `point`.
func _pitch_to(sim: Simulation, point: Vector3) -> float:
var eye: Vector3 = Harvest.eye(sim.entities.find(1))
var delta: Vector3 = point - eye
return atan2(delta.y, Vector2(delta.x, delta.z).length())
## Steps `ticks` ticks with the shooter holding `buttons` and looking at `pitch`.
## Returns how many shots were fired.
func _hold(sim: Simulation, ticks: int, buttons: int = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, pitch: float = 0.0) -> int:
var shots: int = 0
for _i: int in ticks:
var shooter: SimEntity = sim.entities.find(1)
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, pitch, buttons, shooter.selected))
for player_id: int in [2, 3]:
if sim.entities.contains(player_id):
sim.submit_command(player_id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, 0, 0))
sim.step()
for event: Dictionary in sim.events:
if event["type"] == "shot":
shots += 1
return shots
func _events_of(sim: Simulation, kind: String) -> Array[Dictionary]:
var found: Array[Dictionary] = []
for event: Dictionary in sim.events:
if event["type"] == kind:
found.append(event)
return found
func test_fire_rate_is_limited_by_the_server() -> void:
var sim: Simulation = _sim("dock_carbine")
var def: ItemDef = ItemCatalog.get_def(_code("dock_carbine"))
var shots: int = _hold(sim, Tuning.TICK_RATE_HZ * 2)
# 6 a second, minus the swap delay at the start.
var interval: int = def.fire_interval_ticks()
assert_eq(interval, 5)
assert_true(shots >= 11 and shots <= 12, "carbine fired %d shots in 2 s" % shots)
func test_each_shot_spends_a_round_and_an_empty_magazine_reloads_from_ammunition() -> void:
var sim: Simulation = _sim("signal_revolver", 4)
var shooter: SimEntity = sim.entities.find(1)
var def: ItemDef = ItemCatalog.get_def(_code("signal_revolver"))
assert_eq(shooter.slot_rounds[0], 6)
var shots: int = _hold(sim, Tuning.TICK_RATE_HZ * 4)
assert_eq(shots, 6, "six rounds, then dry")
assert_eq(shooter.slot_rounds[0], 0)
assert_true(shooter.reload_ticks > 0, "an empty trigger pull starts a reload")
_hold(sim, def.reload_ticks(), 0)
assert_eq(shooter.slot_rounds[0], 4, "only four rounds were carried")
assert_eq(shooter.ammo[Inventory.ammo_index(def.ammo_code)], 0)
func test_a_reload_takes_the_weapons_reload_time() -> void:
var sim: Simulation = _sim("dock_carbine", 100)
var shooter: SimEntity = sim.entities.find(1)
var def: ItemDef = ItemCatalog.get_def(_code("dock_carbine"))
shooter.slot_rounds[0] = 10
_hold(sim, 1, PlayerCommand.BUTTON_RELOAD)
_hold(sim, def.reload_ticks() - 2, 0)
assert_eq(shooter.slot_rounds[0], 10, "not done yet")
assert_eq(_hold(sim, 1), 0, "no firing while reloading")
_hold(sim, 2, 0)
assert_eq(shooter.slot_rounds[0], 30)
assert_eq(shooter.ammo[Inventory.ammo_index(def.ammo_code)], 80)
func test_without_ammunition_there_is_no_reload() -> void:
var sim: Simulation = _sim("dock_carbine")
var shooter: SimEntity = sim.entities.find(1)
shooter.slot_rounds[0] = 0
_hold(sim, 3, PlayerCommand.BUTTON_RELOAD)
assert_eq(shooter.reload_ticks, 0)
func test_switching_slots_cancels_a_reload() -> void:
var sim: Simulation = _sim("dock_carbine", 100)
var shooter: SimEntity = sim.entities.find(1)
shooter.slot_rounds[0] = 0
_hold(sim, 1, PlayerCommand.BUTTON_RELOAD)
assert_true(shooter.reload_ticks > 0)
shooter.selected = 1
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, 0, 2))
sim.step()
assert_eq(shooter.reload_ticks, 0)
assert_eq(shooter.slot_rounds[0], 0)
func test_a_body_shot_does_the_weapons_damage() -> void:
var sim: Simulation = _sim("signal_revolver")
var target: SimEntity = _target_at(sim, 10.0)
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH - 38)
assert_eq(target.last_attacker, 1)
func test_headshots_multiply_damage() -> void:
var sim: Simulation = _sim("signal_revolver")
var target: SimEntity = _target_at(sim, 10.0)
var head: Vector3 = target.position + Vector3(0.0, Tuning.PLAYER_HEIGHT - 0.1, 0.0)
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, head))
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH - roundi(38 * 2.0))
func test_damage_falls_off_with_distance() -> void:
var def: ItemDef = ItemCatalog.get_def(_code("signal_revolver"))
assert_near(def.damage_at(5.0), 38.0, 0.001, "full damage inside range_full")
assert_near(def.damage_at(30.0), 38.0, 0.001)
assert_near(def.damage_at(55.0), 38.0 * 0.75, 0.001, "halfway down the falloff")
assert_near(def.damage_at(80.0), 19.0, 0.001)
assert_near(def.damage_at(500.0), 19.0, 0.001, "never below falloff_min")
func test_a_far_hit_does_less_damage() -> void:
var sim: Simulation = _sim("signal_revolver")
var target: SimEntity = _target_at(sim, 55.0)
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
var expected: int = roundi(ItemCatalog.get_def(_code("signal_revolver")).damage_at(_distance_hit(sim, target)))
assert_true(Tuning.PLAYER_MAX_HEALTH - target.health <= expected + 1 and Tuning.PLAYER_MAX_HEALTH - target.health >= expected - 1,
"took %d, expected about %d" % [Tuning.PLAYER_MAX_HEALTH - target.health, expected])
assert_true(target.health > Tuning.PLAYER_MAX_HEALTH - 38)
func _distance_hit(sim: Simulation, target: SimEntity) -> float:
return Harvest.eye(sim.entities.find(1)).distance_to(target.position + Vector3(0.0, 0.9, -Tuning.PLAYER_RADIUS))
func test_armor_soaks_damage_before_health() -> void:
var sim: Simulation = _sim("signal_revolver")
var target: SimEntity = _target_at(sim, 10.0)
target.armor = 20
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
assert_eq(target.armor, 0)
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH - 18)
func test_lethal_damage_eliminates_and_credits_the_shooter() -> void:
var sim: Simulation = _sim("whalers_longgun")
var target: SimEntity = _target_at(sim, 20.0)
target.health = 50
var eliminated: Array[Dictionary] = []
var pitch: float = _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0))
for _i: int in 15:
_hold(sim, 1, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, pitch)
eliminated.append_array(_events_of(sim, "eliminated"))
assert_true(target.eliminated)
assert_eq(target.health, 0)
assert_eq(eliminated.size(), 1)
assert_eq(eliminated[0]["by"], 1)
func test_terrain_stops_a_shot() -> void:
var heights: PackedFloat32Array = PackedFloat32Array()
var side: int = 128
for z: int in side:
for x: int in side:
heights.append(3.0 if z == 55 else 0.0)
var sim: Simulation = Simulation.new(5, SimGround.from_heights(heights, side, 2.0))
var shooter: SimEntity = sim.entities.find(sim.add_player())
shooter.position = SHOOTER_FEET
Inventory.give(shooter, _code("signal_revolver"), 1)
shooter.selected = 1
var target: SimEntity = _target_at(sim, 20.0)
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH, "the ridge takes the shot")
func test_a_built_wall_stops_a_shot_and_takes_the_damage() -> void:
var sim: Simulation = _sim("signal_revolver")
var target: SimEntity = _target_at(sim, 12.0)
var wall: int = sim.structures.add(StructureSet.WALL, Materials.STONE, Vector4i(24, 0, 26, StructureSet.SLOT_WALL_Z), 0, 1, sim.tick, sim.ground).id
sim.structures.find(wall).build_ticks_left = 0
sim.structures.find(wall).health = 300
var before: int = sim.structures.find(wall).health
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH)
assert_eq(sim.structures.find(wall).health, before - 38)
func test_shots_never_hit_the_shooter() -> void:
var sim: Simulation = _sim("signal_revolver")
_hold(sim, 10, PlayerCommand.BUTTON_FIRE, -PI * 0.5)
assert_eq(sim.entities.find(1).health, Tuning.PLAYER_MAX_HEALTH)
func test_spread_grows_with_bloom_and_movement_and_shrinks_when_aiming() -> void:
var sim: Simulation = _sim("dock_carbine")
var shooter: SimEntity = sim.entities.find(1)
var def: ItemDef = ItemCatalog.get_def(_code("dock_carbine"))
var hip: float = Weapons.spread_degrees(shooter, def, false)
var aimed: float = Weapons.spread_degrees(shooter, def, true)
assert_near(hip, 1.2, 0.0001)
assert_near(aimed, 1.2 * Tuning.AIM_SPREAD_FACTOR, 0.0001)
shooter.velocity = Vector3(4.0, 0.0, 0.0)
assert_near(Weapons.spread_degrees(shooter, def, false), hip + Tuning.MOVING_SPREAD_DEGREES, 0.0001)
shooter.velocity = Vector3.ZERO
shooter.on_ground = false
assert_near(Weapons.spread_degrees(shooter, def, false), hip + Tuning.AIRBORNE_SPREAD_DEGREES, 0.0001)
shooter.on_ground = true
_hold(sim, 30)
assert_true(shooter.bloom > 0.0, "sustained fire blooms")
assert_true(shooter.bloom <= Tuning.MAX_BLOOM_DEGREES)
_hold(sim, 60, 0)
assert_eq(shooter.bloom, 0.0, "bloom decays when not firing")
func test_scatter_stays_inside_the_cone() -> void:
var stream: RandomNumberGenerator = RandomNumberGenerator.new()
stream.seed = 3
var look: Vector3 = Vector3(0.3, 0.2, 1.0).normalized()
for _i: int in 500:
var direction: Vector3 = Weapons.scatter(look, 5.0, stream)
assert_true(rad_to_deg(direction.angle_to(look)) <= 5.0001)
assert_near(direction.length(), 1.0, 0.0001)
assert_eq(Weapons.scatter(Vector3.UP, 0.0, stream), Vector3.UP)
assert_true(Weapons.scatter(Vector3.UP, 3.0, stream).angle_to(Vector3.UP) <= deg_to_rad(3.0001), "straight up works too")
func test_the_fowling_piece_fires_all_its_pellets() -> void:
var sim: Simulation = _sim("fowling_piece")
var target: SimEntity = _target_at(sim, 4.0)
var pellets: int = 0
for _i: int in 10:
_hold(sim, 1, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
pellets += _events_of(sim, "impact").size()
assert_eq(pellets, 9)
assert_true(target.health < Tuning.PLAYER_MAX_HEALTH - 9 * 3, "most pellets land at close range")
func test_projectiles_fly_drop_and_hit() -> void:
var sim: Simulation = _sim("whalers_longgun")
var target: SimEntity = _target_at(sim, 150.0)
var pitch: float = _pitch_to(sim, target.position + Vector3(0.0, 1.2, 0.0))
_hold(sim, 9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, pitch)
assert_eq(sim.projectiles.size(), 1, "the ball is in flight")
var shot: SimEntity = sim.entities.find(sim.projectiles[0])
assert_true(shot.velocity.y < sin(pitch) * 300.0, "gravity pulls it down")
_hold(sim, 30, 0)
assert_eq(sim.projectiles.size(), 0)
assert_true(target.health < Tuning.PLAYER_MAX_HEALTH, "it lands on the target")
func test_a_missed_projectile_expires_at_its_range() -> void:
var sim: Simulation = _sim("whalers_longgun")
_hold(sim, 9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, deg_to_rad(5.0))
assert_eq(sim.projectiles.size(), 1)
_hold(sim, 60, 0)
assert_eq(sim.projectiles.size(), 0)
func test_firing_is_deterministic() -> void:
var a: Simulation = _sim("dock_carbine", 60)
var b: Simulation = _sim("dock_carbine", 60)
_target_at(a, 30.0)
_target_at(b, 30.0)
_hold(a, 90, PlayerCommand.BUTTON_FIRE, 0.02)
_hold(b, 90, PlayerCommand.BUTTON_FIRE, 0.02)
assert_eq(a.state_hash(), b.state_hash())
func test_swimmers_cannot_fire() -> void:
var sim: Simulation = _sim("signal_revolver")
sim.set_sea_level(5.0)
assert_eq(_hold(sim, 30), 0)
func test_adding_a_weapon_is_data_only() -> void:
# Every weapon in the table becomes a definition with its stats, no code per weapon.
for id: String in Tuning.WEAPONS:
if id == ItemCatalog.BOATHOOK:
continue
var def: ItemDef = ItemCatalog.get_def(_code(id))
assert_not_null(def, id)
assert_eq(def.damage, int(Tuning.WEAPONS[id]["damage"]))
assert_eq(def.magazine, int(Tuning.WEAPONS[id]["magazine"]))