B8: Thrown powder kegs with line-of-sight blasts

Fire with a powder keg held now throws one (Explosives.throw): lobbed along
the look direction, it drops down anything it strikes, rests where it lands,
falls again if its support goes, and blows when its fuse runs out (3 s).

The blast (Explosives.blast) reaches blast_radius, measured to the nearest
point of each body, piece or prop:
- terrain, pieces, props and fixed solids shield what is behind them;
  players are hit if their body middle or head is in sight;
- damage falls off linearly to BLAST_EDGE_FACTOR at the edge, times tier;
- players take damage armor first, credited to the thrower, who takes
  BLAST_SELF_FACTOR of their own;
- pieces and props take structure_damage, so kegs can breach breakwaters.
Everything reached is worked out before any damage is applied.

Line of sight uses a per-blast Sight: the nearby solids once, a cheap box
pre-check per ray, and a coarse terrain check skipped above the highest
ground in reach. Four kegs blowing on one tick inside a 5x5x3 stone fort
take about 5 ms, under the 12 ms tick budget (tested).

Kegs in flight and lying lit are drawn with the item models (EntityView);
capture scenario items_kegs. A blast emits "explosion" for A7's effects and
an impact the sound director already plays as keg_blast.

The launcher and bots that throw are follow-up #83.

Closes #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 09:04:54 -05:00
co-authored by Claude Opus 5.5
parent 36d474695a
commit 238e8e4463
17 changed files with 690 additions and 7 deletions
+288
View File
@@ -0,0 +1,288 @@
extends TestCase
## Issue B8: thrown powder kegs. A keg is thrown from the stack, rests where it lands,
## and blows when its fuse runs out. The blast falls off with distance, respects line of
## sight, hurts its own thrower less, and damages pieces and props, all within the
## server's tick budget in a dense fort.
const FEET: Vector3 = Vector3(100.0, 0.0, 100.0)
func _keg() -> ItemDef:
return ItemCatalog.get_def(ItemCatalog.code_of("powder_keg"))
## A flat world with a thrower (ID 1) at FEET facing +z with `kegs` kegs in slot 1.
func _sim(kegs: int = 3) -> Simulation:
var sim: Simulation = Simulation.new(7, SimGround.flat())
var thrower: SimEntity = sim.entities.find(sim.add_player())
thrower.position = FEET
thrower.on_ground = true
Inventory.give(thrower, _keg().code, kegs)
thrower.selected = 1
return sim
func _player_at(sim: Simulation, feet: Vector3) -> SimEntity:
var player: SimEntity = sim.entities.find(sim.add_player())
player.position = feet
player.on_ground = true
return player
## A finished piece of `kind` in `slot`.
func _piece(sim: Simulation, kind: int, material: int, slot: Vector4i, facing: int = 0) -> StructureSet.Piece:
var piece: StructureSet.Piece = sim.structures.add(kind, material, slot, facing, 0, sim.tick, sim.ground)
piece.build_ticks_left = 0
piece.health = piece.max_health
return piece
## Steps with every player idle except the thrower, who holds `buttons`.
func _step(sim: Simulation, ticks: int, buttons: int = 0, pitch: float = 0.0) -> Array[Dictionary]:
var seen: Array[Dictionary] = []
for _i: int in ticks:
for entity: SimEntity in sim.entities.entities():
if entity.archetype == SimEntity.ARCHETYPE_PLAYER and not entity.eliminated:
var held: int = buttons if entity.id == 1 else 0
sim.submit_command(entity.id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, pitch if entity.id == 1 else 0.0, held, entity.selected))
sim.step()
seen.append_array(sim.events)
return seen
func _count(events: Array[Dictionary], kind: String) -> int:
return events.filter(func(event: Dictionary) -> bool: return event["type"] == kind).size()
func test_a_throw_takes_one_keg_lands_and_blows_when_the_fuse_runs_out() -> void:
var sim: Simulation = _sim(3)
var thrower: SimEntity = sim.entities.find(1)
var events: Array[Dictionary] = _step(sim, 1, PlayerCommand.BUTTON_FIRE)
assert_eq(_count(events, "shot"), 1)
assert_eq(thrower.slot_counts[0], 2, "one keg left the stack")
assert_eq(sim.projectiles.size(), 1)
var keg: SimEntity = sim.entities.find(sim.projectiles[0])
var fuse: int = Explosives.fuse_ticks(_keg())
assert_eq(fuse, roundi(3.0 * Tuning.TICK_RATE_HZ))
# Let go of fire and wait until just before the fuse ends.
events = _step(sim, fuse - 2)
assert_eq(_count(events, "explosion"), 0)
assert_true(keg.on_ground, "the keg came to rest")
assert_near(keg.position.y, 0.0, 0.1, "on the ground")
var thrown: float = Vector2(keg.position.x - FEET.x, keg.position.z - FEET.z).length()
assert_true(thrown > 8.0 and thrown < 30.0, "a level throw lobs it %.1f m" % thrown)
assert_near(keg.position.x, FEET.x, 0.01, "straight ahead")
events = _step(sim, 1)
assert_eq(_count(events, "explosion"), 1, "it blows on the fuse's last tick")
assert_eq(sim.projectiles.size(), 0)
assert_false(sim.entities.contains(keg.id))
assert_eq(thrower.health, Tuning.PLAYER_MAX_HEALTH, "the thrower was out of reach")
func test_holding_fire_throws_at_the_fire_rate_until_the_stack_is_gone() -> void:
var sim: Simulation = _sim(3)
var events: Array[Dictionary] = _step(sim, 3 * _keg().fire_interval_ticks() + 5, PlayerCommand.BUTTON_FIRE, -0.6)
assert_eq(_count(events, "shot"), 3)
assert_eq(sim.entities.find(1).slot_items[0], -1, "the slot is empty")
func test_damage_falls_off_with_distance_to_the_edge() -> void:
var sim: Simulation = _sim()
var keg: ItemDef = _keg()
var center: Vector3 = Vector3(60.0, 0.3, 60.0)
var near: SimEntity = _player_at(sim, center + Vector3(0.0, -0.3, 1.0))
var middle: SimEntity = _player_at(sim, center + Vector3(3.0, -0.3, 0.0))
var edge: SimEntity = _player_at(sim, center + Vector3(0.0, -0.3, -5.5))
var out: SimEntity = _player_at(sim, center + Vector3(-7.0, -0.3, 0.0))
Explosives.blast(sim, 1, keg, 0, center)
var radius: float = keg.blast_radius
for pair: Array in [[near, 1.0], [middle, 3.0], [edge, 5.5]]:
var player: SimEntity = pair[0]
var gap: float = float(pair[1]) - Tuning.PLAYER_RADIUS
var expected: int = roundi(Explosives.falloff(keg.damage, gap, radius))
assert_eq(Tuning.PLAYER_MAX_HEALTH - player.health, expected, "at %.1f m" % gap)
assert_eq(player.last_attacker, 1, "credited to the thrower")
assert_true(near.health < middle.health and middle.health < edge.health)
assert_eq(out.health, Tuning.PLAYER_MAX_HEALTH, "outside the radius")
assert_near(Explosives.falloff(100.0, 0.0, 6.0), 100.0, 0.001)
assert_near(Explosives.falloff(100.0, 6.0, 6.0), 100.0 * Tuning.BLAST_EDGE_FACTOR, 0.001)
assert_near(Explosives.falloff(100.0, 6.01, 6.0), 0.0, 0.001)
func test_armor_takes_it_first_and_a_better_tier_hits_harder() -> void:
var sim: Simulation = _sim()
var center: Vector3 = Vector3(60.0, 0.3, 60.0)
var plain: SimEntity = _player_at(sim, center + Vector3(0.0, -0.3, 2.0))
var armored: SimEntity = _player_at(sim, center + Vector3(0.0, -0.3, -2.0))
armored.armor = 50
Explosives.blast(sim, 1, _keg(), 3, center)
var expected: int = roundi(Explosives.falloff(_keg().damage * Weapons.tier_multiplier(3), 2.0 - Tuning.PLAYER_RADIUS, _keg().blast_radius))
assert_eq(Tuning.PLAYER_MAX_HEALTH - plain.health, expected, "captain's tier")
assert_eq(armored.armor, 0)
assert_eq(Tuning.PLAYER_MAX_HEALTH - armored.health, expected - 50)
func test_the_thrower_takes_a_share_and_never_credits_themselves() -> void:
var sim: Simulation = _sim()
var thrower: SimEntity = sim.entities.find(1)
var other: SimEntity = _player_at(sim, FEET + Vector3(0.0, 0.0, 2.0))
Explosives.blast(sim, 1, _keg(), 0, FEET + Vector3(0.0, 0.3, 1.0))
var full: int = roundi(Explosives.falloff(_keg().damage, 1.0 - Tuning.PLAYER_RADIUS, _keg().blast_radius))
assert_eq(Tuning.PLAYER_MAX_HEALTH - other.health, full)
assert_eq(Tuning.PLAYER_MAX_HEALTH - thrower.health, roundi(full * Tuning.BLAST_SELF_FACTOR))
assert_eq(thrower.last_attacker, 0)
assert_eq(other.last_attacker, 1)
assert_eq(thrower.last_cause, SimEntity.CAUSE_WEAPON)
func test_a_wall_shields_a_player_and_takes_the_blast_itself() -> void:
var sim: Simulation = _sim()
var g: float = Tuning.GRID_CELL_METRES
# A stone wall along z = 26 cells, from x = 24 to 25 cells.
var wall: StructureSet.Piece = _piece(sim, StructureSet.WALL, Materials.STONE, Vector4i(24, 0, 26, StructureSet.SLOT_WALL_Z))
sim.entities.find(1).position = Vector3(20.0, 0.0, 20.0)
var center: Vector3 = Vector3(24.5 * g, 0.3, 26.0 * g - 1.5)
var behind: SimEntity = _player_at(sim, Vector3(24.5 * g, 0.0, 26.0 * g + 1.5))
var beside: SimEntity = _player_at(sim, Vector3(24.5 * g + 3.0, 0.0, 26.0 * g - 1.5))
var damaged: int = Explosives.blast(sim, 1, _keg(), 0, center)
assert_eq(behind.health, Tuning.PLAYER_MAX_HEALTH, "the wall is in the way")
assert_true(beside.health < Tuning.PLAYER_MAX_HEALTH, "in the open")
var wall_gap: float = 1.5 - Tuning.WALL_THICKNESS * 0.5
var expected: int = roundi(Explosives.falloff(_keg().structure_damage, wall_gap, _keg().blast_radius))
assert_eq(wall.max_health - wall.health, mini(expected, wall.max_health))
assert_eq(damaged, 2, "the wall and the player beside it")
func test_terrain_shields_too() -> void:
# A ridge 2 m high between the blast and a player.
var side: int = 64
var heights: PackedFloat32Array = PackedFloat32Array()
heights.resize(side * side)
for z: int in side:
for x: int in side:
heights[z * side + x] = 3.0 if x == 30 else 0.0
var sim: Simulation = Simulation.new(7, SimGround.from_heights(heights, side, 2.0))
sim.entities.find(sim.add_player()).position = Vector3(10.0, 0.0, 10.0)
var far_side: SimEntity = _player_at(sim, Vector3(62.5, 0.0, 40.0))
var near_side: SimEntity = _player_at(sim, Vector3(57.0, 0.0, 40.0))
Explosives.blast(sim, 1, _keg(), 0, Vector3(57.0, 0.3, 37.0))
assert_true(near_side.health < Tuning.PLAYER_MAX_HEALTH)
assert_eq(far_side.health, Tuning.PLAYER_MAX_HEALTH, "behind the ridge")
func test_a_head_in_sight_over_a_wall_is_enough() -> void:
var sim: Simulation = _sim()
var g: float = Tuning.GRID_CELL_METRES
sim.entities.find(1).position = Vector3(20.0, 0.0, 20.0)
# A wall one level down, so its top is 2 m up. A blast high on the far side sees
# over it to the head, not the middle of the body.
var wall: StructureSet.Piece = _piece(sim, StructureSet.WALL, Materials.STONE, Vector4i(24, -1, 26, StructureSet.SLOT_WALL_Z))
var behind: SimEntity = _player_at(sim, Vector3(24.5 * g, 0.0, 26.0 * g + 1.0))
var center: Vector3 = Vector3(24.5 * g, 2.9, 26.0 * g - 1.0)
var sight: Explosives.Sight = Explosives.Sight.new(sim, center, _keg().blast_radius)
var box: AABB = Weapons.body_box(behind)
assert_false(sight.clear(box.get_center(), -1, -1), "the middle of the body is hidden")
assert_true(sight.clear(Vector3(box.get_center().x, box.end.y - Tuning.HEAD_HEIGHT * 0.5, box.get_center().z), -1, -1), "the head isn't")
assert_near(wall.boxes[0].end.y, 2.0, 0.001)
Explosives.blast(sim, 1, _keg(), 0, center)
assert_true(behind.health < Tuning.PLAYER_MAX_HEALTH, "the head was in sight")
func test_pieces_behind_pieces_and_props_are_judged_the_same_way() -> void:
var sim: Simulation = _sim()
var g: float = Tuning.GRID_CELL_METRES
var front: StructureSet.Piece = _piece(sim, StructureSet.WALL, Materials.DRIFTWOOD, Vector4i(24, 0, 26, StructureSet.SLOT_WALL_Z))
var back: StructureSet.Piece = _piece(sim, StructureSet.WALL, Materials.STONE, Vector4i(24, 0, 27, StructureSet.SLOT_WALL_Z))
var center: Vector3 = Vector3(24.5 * g, 0.3, 26.0 * g - 1.0)
Explosives.blast(sim, 1, _keg(), 0, center)
assert_null(sim.structures.find(front.id), "the driftwood wall went")
assert_eq(back.health, back.max_health, "the wall behind it was shielded when the blast was worked out")
func test_a_keg_breaches_a_breakwater() -> void:
var sim: Simulation = _sim()
var g: float = Tuning.GRID_CELL_METRES
var breakwater: StructureSet.Piece = _piece(sim, StructureSet.BREAKWATER, Materials.DRIFTWOOD, Vector4i(24, 0, 26, StructureSet.SLOT_WALL_Z))
var keg: ItemDef = _keg()
var hits: int = 0
while sim.structures.find(breakwater.id) != null and hits < 10:
Explosives.blast(sim, 1, keg, 0, Vector3(24.5 * g, 0.3, 26.0 * g - 0.6))
hits += 1
assert_null(sim.structures.find(breakwater.id))
assert_true(hits <= 2, "a driftwood breakwater takes %d kegs" % hits)
func test_a_resting_keg_falls_when_its_floor_goes() -> void:
var sim: Simulation = _sim()
var g: float = Tuning.GRID_CELL_METRES
var floor_piece: StructureSet.Piece = _piece(sim, StructureSet.FLOOR, Materials.DRIFTWOOD, Vector4i(25, 2, 25, StructureSet.SLOT_FLOOR))
var top: float = floor_piece.boxes[0].end.y
var keg: SimEntity = sim.entities.create(SimEntity.ARCHETYPE_PROJECTILE)
keg.item = _keg().code
keg.owner_id = 1
keg.position = Vector3(25.5 * g, top + 1.0, 25.5 * g)
keg.origin = keg.position
keg.use_ticks = 1000
sim.projectiles.append(keg.id)
_step(sim, 30)
assert_true(keg.on_ground)
assert_near(keg.position.y, top, 0.1, "it lies on the floor")
sim.structures.destroy(floor_piece.id, "test", sim.ground, sim.events)
_step(sim, 45)
assert_true(keg.on_ground)
assert_near(keg.position.y, 0.0, 0.1, "it dropped to the ground")
func test_a_keg_thrown_at_a_wall_drops_at_its_foot() -> void:
var sim: Simulation = _sim(1)
var g: float = Tuning.GRID_CELL_METRES
# A wall 4 m ahead of the thrower, across their path.
_piece(sim, StructureSet.WALL, Materials.STONE, Vector4i(24, 0, 26, StructureSet.SLOT_WALL_Z))
sim.entities.find(1).position = Vector3(24.5 * g, 0.0, 26.0 * g - 4.0)
_step(sim, 1, PlayerCommand.BUTTON_FIRE)
var keg: SimEntity = sim.entities.find(sim.projectiles[0])
_step(sim, 60)
assert_true(keg.on_ground)
assert_true(keg.position.z < 26.0 * g, "it stayed on the thrower's side")
assert_near(keg.position.y, 0.0, 0.1)
func test_large_blasts_in_a_dense_fort_stay_within_the_tick_budget() -> void:
var sim: Simulation = _sim()
# A fort three levels high over 5 by 5 cells: every wall, floor and roof slot.
var placed: int = 0
for level: int in 3:
for x: int in range(23, 28):
for z: int in range(23, 28):
_piece(sim, StructureSet.FLOOR, Materials.STONE, Vector4i(x, level * 2, z, StructureSet.SLOT_FLOOR))
_piece(sim, StructureSet.WALL, Materials.STONE, Vector4i(x, level * 2, z, StructureSet.SLOT_WALL_X))
_piece(sim, StructureSet.WALL, Materials.STONE, Vector4i(x, level * 2, z, StructureSet.SLOT_WALL_Z))
placed += 3
assert_eq(sim.structures.count(), placed)
for i: int in 8:
_player_at(sim, Vector3(95.0 + i, 0.0, 98.0))
var g: float = Tuning.GRID_CELL_METRES
var started: int = Time.get_ticks_usec()
var damaged: int = 0
# Four kegs blowing on the same tick, in the middle of the fort.
for i: int in 4:
damaged += Explosives.blast(sim, 1, _keg(), 3, Vector3(25.5 * g + i * 0.5, 4.3, 25.5 * g))
var took: int = Time.get_ticks_usec() - started
assert_true(damaged > 4, "the blasts reached the fort (%d)" % damaged)
assert_true(took < ServerLoop.TICK_BUDGET_USEC, "four blasts took %d us" % took)
func test_a_keg_dropping_onto_a_head_falls_past_it_to_the_ground() -> void:
var sim: Simulation = _sim()
var below: SimEntity = _player_at(sim, Vector3(60.0, 0.0, 60.0))
var keg: SimEntity = sim.entities.create(SimEntity.ARCHETYPE_PROJECTILE)
keg.item = _keg().code
keg.owner_id = 1
keg.position = below.position + Vector3(0.0, 3.0, 0.0)
keg.origin = keg.position
keg.use_ticks = 1000
sim.projectiles.append(keg.id)
_step(sim, 40)
assert_true(keg.on_ground)
assert_near(keg.position.y, 0.0, 0.1, "at the player's feet, not on their head")
+1
View File
@@ -0,0 +1 @@
uid://c6d7lxxp42ojo