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
+2 -2
View File
@@ -16,7 +16,7 @@ What works and is tested automatically:
- Whole matches, lobby to results, offline against bots or on a dedicated server, with up to 24 players and bots.
- The rising tide on every generated island, with flooding that follows the terrain, breakwaters that hold water back, storm surges and sudden death.
- Building walls, floors, ramps, roofs and breakwaters in driftwood, stone and salvage iron, with collapse; harvesting with the boathook; seven weapons, including a harpoon gun and a line thrower, in four tiers; healing and armor; sea chests and crates.
- Building walls, floors, ramps, roofs and breakwaters in driftwood, stone and salvage iron, with collapse; harvesting with the boathook; seven weapons, including a harpoon gun and a line thrower, and thrown powder kegs that blast through walls and breakwaters, in four tiers; healing and armor; sea chests and crates.
- Bots at three difficulties. Balance was tuned over 200 bot matches (median match 13 minutes; see [docs/BALANCE.md](docs/BALANCE.md)).
- Multiplayer with client prediction, tested with 24 network clients over two matches under simulated latency, jitter and loss; LAN discovery; list servers; joining by address.
- Menus, HUD, chart, settings with rebindable keys, generated art and synthesized sound and music.
@@ -28,7 +28,7 @@ All of this is checked headless or with **software rendering in a container**. N
- **Untested on real hardware.** No real GPU, Wayland session or hybrid-graphics machine has run it yet ([docs/PLAY.md](docs/PLAY.md#tested-configurations)).
- **Solo only.** Duos and reviving ([#30](https://gitea.eom.dev/claude/tidewall/issues/30)) aren't in this release.
- **The powder keg can be carried but not thrown** yet ([#22](https://gitea.eom.dev/claude/tidewall/issues/22)), and there are no particle effects such as muzzle flashes, tracers or splashes ([#47](https://gitea.eom.dev/claude/tidewall/issues/47)).
- **No particle effects** yet, such as muzzle flashes, tracers, splashes or a keg's blast ([#47](https://gitea.eom.dev/claude/tidewall/issues/47)); a keg's blast is heard, not seen. Bots carry powder kegs but don't throw them.
- **Bots don't build** ([#29](https://gitea.eom.dev/claude/tidewall/issues/29)), and most of them fall early in a match ([#74](https://gitea.eom.dev/claude/tidewall/issues/74)).
- **No gamepad support or tutorial** ([#53](https://gitea.eom.dev/claude/tidewall/issues/53), [#52](https://gitea.eom.dev/claude/tidewall/issues/52)). No character customization ([#43](https://gitea.eom.dev/claude/tidewall/issues/43)) or editing of placed pieces ([#18](https://gitea.eom.dev/claude/tidewall/issues/18)).
- **The Join menu only searches the LAN.** List servers work from the command line (`--browse --list=...`); in the menu, join other servers by address.
+15
View File
@@ -255,6 +255,21 @@ ammunition and rarity. Every weapon besides the boathook takes an inventory slot
| Line Thrower | Fires a line and yanks you towards where it lands |
| Powder Keg | Thrown explosive; damages structures heavily |
**Powder kegs** (B8, [`src/sim/explosives.gd`](../src/sim/explosives.gd)) are thrown,
not fired. Fire throws one from the stack, lobbed (`THROW_LIFT`) at the keg's
`projectile_speed`. It drops down anything it strikes, rests where it lands, falls again
if what it rests on goes, and blows `fuse` seconds after the throw. The blast reaches
`blast_radius`, measured to the nearest point of each body, piece or prop:
- terrain, built pieces, props and fixed solids shield what is behind them; players
don't, and a player whose body middle or head is in sight is hit;
- damage falls off linearly from all of it at the middle to `BLAST_EDGE_FACTOR` at the
edge, times the keg's 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 a keg can open a breakwater and let the
sea in.
Weapons come in four quality tiers, from `WEAPON_TIERS`: Rusted, Seaworthy,
Brass-Fitted and Captain's. Better tiers hit harder and reload faster.
+5
View File
@@ -102,6 +102,11 @@ Gamepads aren't supported in this release.
and you can't fire or build. Underwater you run out of breath and start to drown.
After the last rise everyone left loses health until one remains.
**Powder kegs** are thrown with fire. One lands, lies there and blows three seconds
after the throw, hurting everyone in sight within six metres (you too, a little less)
and smashing built pieces, props and breakwaters. Cover between you and the keg
protects you.
Weapons come in four tiers: Rusted, Seaworthy, Brass-Fitted and Captain's. Bandages and
tonic restore health; tar patches and cork vests add armor, which takes damage first.
+11 -3
View File
@@ -1,5 +1,5 @@
## Draws what moves in a match: players with what they hold, the sky barge while it
## crosses, items lying in the world and sea chests. `sync()` matches everything to the
## crosses, items lying in the world, thrown kegs and sea chests. `sync()` matches everything to the
## simulation as it is now, and `animate()` steps the players' figures.
##
## Players are FigureRigs (A2): jointed islanders in oilskins, animated from what the
@@ -188,10 +188,18 @@ func _hold(figure: FigureRig, entity: SimEntity) -> void:
figure.held().mesh = mesh
## Items lying in the world, and thrown charges (B8) in flight or waiting to blow,
## which are drawn with the same models.
func _sync_pickups() -> void:
var groups: Dictionary = {}
for index: int in _sim.pickups.size():
var pickup: SimEntity = _sim.entities.find(_sim.pickups[index])
var shown: Array[int] = []
shown.append_array(_sim.pickups)
for projectile_id: int in _sim.projectiles:
var projectile: SimEntity = _sim.entities.find(projectile_id)
if projectile != null and Explosives.is_charge(ItemCatalog.get_def(projectile.item)):
shown.append(projectile_id)
for entity_id: int in shown:
var pickup: SimEntity = _sim.entities.find(entity_id)
if pickup == null:
continue
var id: String = ItemModels.id_of(pickup.item)
+266
View File
@@ -0,0 +1,266 @@
## Thrown charges (B8): the powder keg, and any weapon with a `blast_radius`.
##
## Fire with a charge held throws one from the stack, along the look direction lifted by
## Tuning.THROW_LIFT, at the weapon's projectile_speed. The charge is a projectile
## entity that flies under projectile_gravity. When it strikes something it loses its
## sideways speed and drops; when it lands it rests (`on_ground`), and if what it rests
## on goes, it falls again. Its fuse, in `use_ticks`, runs from the throw, and when it
## runs out the charge blows wherever it is. It never vanishes unexploded, except below
## the world.
##
## A blast reaches everything within `blast_radius` of its middle, measured to the
## nearest point of a player's body, a piece or a prop:
## - Line of sight: terrain, pieces, props and fixed solids in the way shield what is
## behind them. Players don't. A player counts as seen if the middle of their body or
## their head is in sight.
## - Falloff: full damage at the middle, down linearly to Tuning.BLAST_EDGE_FACTOR of it
## at the edge, times the charge's tier.
## - Players take `damage`, armor first, credited to the thrower. The thrower takes
## Tuning.BLAST_SELF_FACTOR of theirs. Everyone else takes it all: there are no
## teammates in solo, and duos (M7) will apply one team rule to every weapon.
## - Pieces and props take `structure_damage`. A breakwater is a piece, so a charge can
## breach one and let the sea in.
## Everything a blast reaches is worked out before any of it is applied, in ID order,
## so what it destroys can't change what else it reaches.
class_name Explosives
extends RefCounted
## How high above where it lies a charge's blast is measured from.
const BLAST_LIFT: float = 0.3
## A thrown charge stops this far short of what it hits.
const STANDOFF: float = 0.05
## Below this sideways speed, a charge that hits something has landed.
const LANDED_SPEED: float = 0.01
## Throws one charge from the held stack if the player is ready. Returns true if one was
## thrown. Called by Simulation for fire with a thrown weapon held.
static func throw(sim: Simulation, player: SimEntity) -> bool:
var def: ItemDef = Inventory.held_def(player)
if def == null or not def.thrown or player.action_cooldown > 0:
return false
var slot: int = Inventory.held_slot(player)
if player.slot_counts[slot] <= 0:
return false
var tier: int = player.slot_tiers[slot]
Inventory.take_from_slot(player, slot, 1)
player.action_cooldown = def.fire_interval_ticks()
var from: Vector3 = Harvest.eye(player)
var look: Vector3 = Harvest.look_direction(player)
var direction: Vector3 = (look + Vector3(0.0, Tuning.THROW_LIFT, 0.0)).normalized()
var charge: SimEntity = sim.entities.create(SimEntity.ARCHETYPE_PROJECTILE)
charge.position = from
charge.origin = from
charge.velocity = direction * def.projectile_speed
charge.owner_id = player.id
charge.item = def.code
charge.item_tier = tier
charge.use_ticks = fuse_ticks(def)
sim.projectiles.append(charge.id)
sim.events.append({"type": "shot", "player": player.id, "item": def.code, "from": from, "direction": direction})
return true
## The fuse in ticks, at least one.
static func fuse_ticks(def: ItemDef) -> int:
return maxi(roundi(def.fuse_seconds * Tuning.TICK_RATE_HZ), 1)
## True for a projectile that is a charge rather than a shot.
static func is_charge(def: ItemDef) -> bool:
return def != null and def.blast_radius > 0.0
## One tick of a charge: fly, drop or lie still, then count down the fuse. Returns false
## once the charge is gone. Called by Weapons.advance_projectiles().
static func advance(sim: Simulation, charge: SimEntity, def: ItemDef) -> bool:
var dt: float = Simulation.SECONDS_PER_TICK
if charge.on_ground and not _supported(sim, charge.position):
charge.on_ground = false
if not charge.on_ground:
charge.velocity.y -= def.projectile_gravity * dt
var to: Vector3 = charge.position + charge.velocity * dt
var hit: Weapons.Hit
if Vector2(charge.velocity.x, charge.velocity.z).length() <= LANDED_SPEED:
# Dropping straight down: only the ground and solids stop it, so it can't
# come to rest on someone's head.
hit = Weapons.Hit.new()
hit.fraction = sim.ground.raycast(charge.position, to)
hit.kind = Weapons.HIT_TERRAIN if hit.fraction < 1.0 else Weapons.HIT_NONE
hit.point = charge.position.lerp(to, hit.fraction)
else:
# Its thrower's own body doesn't stop it as it leaves their hand.
hit = Weapons.trace(sim, charge.position, to, charge.owner_id if charge.use_ticks > fuse_ticks(def) - 6 else 0)
if hit.kind == Weapons.HIT_NONE:
charge.position = to
else:
var travel: Vector3 = to - charge.position
var back: float = minf(STANDOFF, travel.length() * hit.fraction)
charge.position = hit.point - travel.normalized() * back
var sideways: float = Vector2(charge.velocity.x, charge.velocity.z).length()
if sideways <= LANDED_SPEED or charge.velocity.y <= 0.0 and _supported(sim, charge.position):
charge.velocity = Vector3.ZERO
charge.on_ground = true
else:
# Struck something side-on: drop down it.
charge.velocity = Vector3(0.0, minf(charge.velocity.y, 0.0), 0.0)
charge.use_ticks -= 1
if charge.use_ticks <= 0:
blast(sim, charge.owner_id, def, charge.item_tier, charge.position + Vector3(0.0, BLAST_LIFT, 0.0))
return false
return charge.position.y >= Tuning.BUILD_MIN_LEVEL * Tuning.BUILD_LEVEL_METRES - 10.0
## True if something is just under `point` to lie on.
static func _supported(sim: Simulation, point: Vector3) -> bool:
return sim.ground.raycast(point, point - Vector3(0.0, STANDOFF * 3.0, 0.0)) < 1.0
## Damage at `distance` from the middle of a blast of `full` reaching `radius`.
static func falloff(full: float, distance: float, radius: float) -> float:
if distance > radius:
return 0.0
var t: float = clampf(distance / radius, 0.0, 1.0) if radius > 0.0 else 0.0
return full * lerpf(1.0, Tuning.BLAST_EDGE_FACTOR, t)
## Blows a charge of `def` thrown by `thrower` at `center`. Returns how many players,
## pieces and props it damaged, for tests.
static func blast(sim: Simulation, thrower: int, def: ItemDef, tier: int, center: Vector3) -> int:
var radius: float = def.blast_radius
var scale: float = Weapons.tier_multiplier(tier)
sim.events.append({"type": "explosion", "player": thrower, "item": def.code, "point": center, "radius": radius})
# What the sound and effects hear as where it landed.
sim.events.append({"type": "impact", "player": thrower, "item": def.code, "point": center, "hit": Weapons.HIT_NONE, "target": -1})
var sight: Sight = Sight.new(sim, center, radius)
# Players.
var hurt: Array[Vector3i] = []
for entity: SimEntity in sim.entities.entities():
if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.eliminated or entity.drop_state == Drop.ABOARD:
continue
var box: AABB = Weapons.body_box(entity)
var distance: float = _distance_to_box(box, center)
if distance > radius:
continue
var middle: Vector3 = box.get_center()
var head: Vector3 = Vector3(middle.x, box.end.y - Tuning.HEAD_HEIGHT * 0.5, middle.z)
if not (sight.clear(middle, -1, -1) or sight.clear(head, -1, -1)):
continue
var amount: float = falloff(def.damage * scale, distance, radius)
if entity.id == thrower:
amount *= Tuning.BLAST_SELF_FACTOR
hurt.append(Vector3i(entity.id, roundi(amount), 0))
# Pieces and props, each once, at their nearest solid in sight.
var pieces: Dictionary = {}
var props: Dictionary = {}
for index: int in sight.boxes.size():
var box: AABB = sight.boxes[index]
var distance: float = _distance_to_box(box, center)
if distance > radius:
continue
var piece: int = sight.pieces[index]
var prop: int = sight.props[index]
if piece == 0 and prop < 0:
continue
var table: Dictionary = pieces if piece != 0 else props
var key: int = piece if piece != 0 else prop
if distance >= float(table.get(key, INF)):
continue
var target: Vector3 = _closest_point(box, center).lerp(box.get_center(), 0.05)
if sight.touching[index] or sight.clear(target, piece, prop):
table[key] = distance
var count: int = 0
for entry: Vector3i in hurt:
if Weapons.damage_player(sim, sim.entities.find(entry.x), entry.y, thrower, false, def.code) > 0:
count += 1
var piece_ids: Array = pieces.keys()
piece_ids.sort()
for piece_id: int in piece_ids:
if sim.structures.find(piece_id) == null:
continue
sim.damage_piece(piece_id, roundi(falloff(def.structure_damage * scale, pieces[piece_id], radius)), "blast")
count += 1
var prop_ids: Array = props.keys()
prop_ids.sort()
for prop: int in prop_ids:
if not sim.props.is_standing(prop):
continue
if sim.damage_prop(prop, roundi(falloff(def.structure_damage * scale, props[prop], radius))):
sim.events.append({"type": "prop_destroyed", "prop": prop})
count += 1
return count
## What can block a blast: every solid near it once, with the piece or prop it belongs
## to, and the highest terrain under it.
class Sight:
extends RefCounted
## Terrain is checked along a ray this often, in metres. Coarser than a shot's trace:
## a blast only needs to know whether a ridge is in the way.
const TERRAIN_STEP: float = 0.5
## Points this close above the terrain still count as above it.
const TERRAIN_SLACK: float = 0.05
var ground: SimGround
var center: Vector3
var boxes: Array[AABB] = []
var pieces: PackedInt32Array = PackedInt32Array()
var props: PackedInt32Array = PackedInt32Array()
## True for a solid the middle of the blast is inside or touching. Those shield
## nothing.
var touching: Array[bool] = []
var highest_terrain: float = 0.0
func _init(sim: Simulation, at: Vector3, radius: float) -> void:
ground = sim.ground
center = at
var seen: Dictionary = {}
for solid_id: int in ground.solids_near(at.x - radius, at.z - radius, at.x + radius, at.z + radius):
if seen.has(solid_id):
continue
seen[solid_id] = true
var box: AABB = ground.solids[solid_id]
if box.size == Vector3.ZERO or _gap(box, at) > radius:
continue
boxes.append(box)
pieces.append(sim.structures.piece_of_solid(solid_id))
props.append(sim.props.prop_of_solid(solid_id))
touching.append(box.grow(0.01).has_point(at))
highest_terrain = ground.max_height_in(at.x - radius, at.z - radius, at.x + radius, at.z + radius)
## True if nothing lies between the middle and `to` except solids of `piece` or `prop`
## (-1 for none) and solids the middle touches.
func clear(to: Vector3, piece: int, prop: int) -> bool:
var low: Vector3 = center.min(to)
var high: Vector3 = center.max(to)
for index: int in boxes.size():
if touching[index] or (piece > 0 and pieces[index] == piece) or (prop >= 0 and props[index] == prop):
continue
var box: AABB = boxes[index]
if box.end.x < low.x or box.position.x > high.x or box.end.y < low.y or box.position.y > high.y or box.end.z < low.z or box.position.z > high.z:
continue
if ground.ray_box(center, to, box) < 0.999:
return false
return _terrain_clear(to)
func _terrain_clear(to: Vector3) -> bool:
if minf(center.y, to.y) >= highest_terrain:
return true
var length: float = center.distance_to(to)
var steps: int = maxi(ceili(length / TERRAIN_STEP), 1)
for step: int in range(1, steps):
var point: Vector3 = center.lerp(to, float(step) / steps)
if point.y < ground.terrain_height(point.x, point.z) - TERRAIN_SLACK:
return false
return true
static func _gap(box: AABB, point: Vector3) -> float:
return point.distance_to(point.clamp(box.position, box.end))
static func _closest_point(box: AABB, point: Vector3) -> Vector3:
return point.clamp(box.position, box.end)
static func _distance_to_box(box: AABB, point: Vector3) -> float:
return point.distance_to(_closest_point(box, point))
+1
View File
@@ -0,0 +1 @@
uid://ddj3xo7sqb4dq
+2
View File
@@ -105,5 +105,7 @@ static func _weapon_def(id: String, stats: Dictionary, ammo_code: int) -> ItemDe
def.pull_speed = float(stats.get("pull_speed", 0.0))
def.projectile_speed = float(stats.get("projectile_speed", 0.0))
def.projectile_gravity = float(stats.get("projectile_gravity", 0.0))
def.blast_radius = float(stats.get("blast_radius", 0.0))
def.fuse_seconds = float(stats.get("fuse", 0.0))
def.ammo_code = ammo_code
return def
+3
View File
@@ -37,6 +37,9 @@ const AMMO: int = 2
## 0 means hitscan.
@export var projectile_speed: float = 0.0
@export var projectile_gravity: float = 0.0
## Thrown charges (B8): the blast's reach in metres, and the fuse from the throw.
@export var blast_radius: float = 0.0
@export var fuse_seconds: float = 0.0
## ItemCatalog code of the ammunition this weapon loads, or -1 if none.
@export var ammo_code: int = -1
## Thrown weapons are their own ammunition: the slot count is what's left.
+16
View File
@@ -195,6 +195,22 @@ func raycast(from: Vector3, to: Vector3) -> float:
return minf(nearest, _ray_terrain(from, to, nearest))
## The highest the terrain gets over the rectangle. Bilinear heights peak at grid
## samples, so the samples covering it are enough.
func max_height_in(min_x: float, min_z: float, max_x: float, max_z: float) -> float:
var limit: int = samples - 1
var x0: int = clampi(floori(min_x / cell), 0, limit)
var x1: int = clampi(ceili(max_x / cell), 0, limit)
var z0: int = clampi(floori(min_z / cell), 0, limit)
var z1: int = clampi(ceili(max_z / cell), 0, limit)
var highest: float = -INF
for z: int in range(z0, z1 + 1):
var row: int = z * samples
for x: int in range(x0, x1 + 1):
highest = maxf(highest, heights[row + x])
return highest
## True if `point` is inside the terrain or a solid, or within `margin` of one. For the
## terrain, heights are also checked `margin` away on each side, so steep ground counts
## its sideways distance and not just the height under the point.
+2
View File
@@ -254,6 +254,8 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
var def: ItemDef = Inventory.held_def(player)
if def != null and def.kind == ItemDef.CONSUMABLE:
Consumables.start(self, player)
elif def != null and def.thrown:
Explosives.throw(self, player)
else:
Weapons.fire(self, player, command.is_pressed(PlayerCommand.BUTTON_AIM))
+13 -1
View File
@@ -451,13 +451,25 @@ const WEAPONS: Dictionary = {
"spread": 0.6, "bloom": 0.0, "ammo": "harpoons", "headshot": 1.5,
"projectile_speed": 120.0, "projectile_gravity": 6.0, "structure_factor": 2.5,
},
# Thrown (B8): lobbed at projectile_speed, it comes to rest where it lands and blows
# `fuse` seconds after the throw. `damage` to players and `structure_damage` to
# pieces and props at the middle of the blast, down to BLAST_EDGE_FACTOR of them at
# `blast_radius`. See Explosives.
"powder_keg": {
"damage": 80, "structure_damage": 400, "blast_radius": 6.0, "fuse": 3.0,
"fire_rate": 0.8, "magazine": 1, "reload": 0.0, "stack": 3,
"range_full": 0.0, "range_max": 30.0, "falloff_min": 1.0, "rarity": 2,
"spread": 0.0, "bloom": 0.0,
"spread": 0.0, "bloom": 0.0, "projectile_speed": 17.0, "projectile_gravity": 12.0,
},
}
## A blast does this share of its damage at the edge of its radius, rising linearly to
## all of it at the middle.
const BLAST_EDGE_FACTOR: float = 0.3
## The share of a blast's damage its own thrower takes.
const BLAST_SELF_FACTOR: float = 0.5
## A throw leaves this much above the look direction (added to its y, then normalised),
## so a level throw lobs.
const THROW_LIFT: float = 0.25
## A network client sees other players late. The server judges a hitscan shot against
## where they were when the shooter saw them, going back at most this many ticks (half
## a second). Past that the shooter has to lead the target.
+6 -1
View File
@@ -105,7 +105,7 @@ static func view_time(player: SimEntity, tick: int) -> float:
## Fires the held weapon if it's ready. Returns true if a shot was fired. An empty
## magazine starts a reload instead. Thrown weapons are handled by explosives (B8).
## magazine starts a reload instead. Thrown weapons go to Explosives.throw().
static func fire(sim: Simulation, player: SimEntity, aiming: bool) -> bool:
var def: ItemDef = Inventory.held_def(player)
if def == null or not def.is_weapon() or def.thrown:
@@ -304,6 +304,11 @@ static func advance_projectiles(sim: Simulation) -> void:
for shot_id: int in sim.projectiles.duplicate():
var shot: SimEntity = sim.entities.find(shot_id)
var def: ItemDef = ItemCatalog.get_def(shot.item)
if Explosives.is_charge(def):
if not Explosives.advance(sim, shot, def):
sim.entities.remove(shot_id)
sim.projectiles.erase(shot_id)
continue
shot.velocity.y -= def.projectile_gravity * dt
var to: Vector3 = shot.position + shot.velocity * dt
var hit: Hit = trace(sim, shot.position, to, shot.owner_id)
+7
View File
@@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/items_scene.gd" id="1"]
[node name="CaptureKegs" type="Node3D"]
script = ExtResource("1")
stage = "kegs"
+27
View File
@@ -6,6 +6,8 @@
## the game's camera has it, aiming down the sight line.
## - ground: every item lying on the ground, at a mix of tiers, from above.
## - tiers: the dock carbine at tiers 0 to 3, side by side, held.
## - kegs: B8's thrown powder kegs: a player holding one, one in flight, and one lying
## lit against a stone wall, from behind the thrower.
extends Node3D
@export var stage: String = "held"
@@ -32,6 +34,8 @@ func _ready() -> void:
_row(spot, ["whalers_longgun"], [])
"ground":
_lay_out(spot)
"kegs":
_kegs(spot)
view = MatchView.new()
view.setup(sim, 0)
add_child(view)
@@ -50,6 +54,8 @@ func _ready() -> void:
view.follow(id, deg_to_rad(4.0))
"ground":
view.look_from(spot + Vector3(0.0, 5.0, -3.6), spot + Vector3(0.0, 0.0, 1.8))
"kegs":
view.look_from(spot + Vector3(-2.2, 2.4, -3.2), spot + Vector3(0.0, 1.0, 3.5))
view.settle()
@@ -90,6 +96,27 @@ func _lay_out(spot: Vector3) -> void:
Inventory.spawn_pickup(sim, at, code, 1, -1, index % 4)
func _kegs(spot: Vector3) -> void:
_row(spot + Vector3(0.0, 0.0, 6.0), ["powder_keg"], [])
var keg: int = ItemCatalog.code_of("powder_keg")
var g: float = Tuning.GRID_CELL_METRES
var cell: Vector3i = Vector3i(floori(spot.x / g), floori(spot.y / Tuning.BUILD_LEVEL_METRES), floori((spot.z + 8.0) / g))
var wall: StructureSet.Piece = sim.structures.add(StructureSet.WALL, Materials.STONE, Vector4i(cell.x, cell.y, cell.z, StructureSet.SLOT_WALL_Z), 0, 0, sim.tick, sim.ground)
wall.build_ticks_left = 0
wall.health = wall.max_health
var lying: Vector3 = Vector3(spot.x + 0.6, 0.0, cell.z * g - 0.4)
lying.y = sim.ground.terrain_height(lying.x, lying.z)
var flying: Vector3 = spot + Vector3(0.2, 2.1, 3.0)
for at: Vector3 in [lying, flying]:
var charge: SimEntity = sim.entities.create(SimEntity.ARCHETYPE_PROJECTILE)
charge.item = keg
charge.position = at
charge.origin = at
charge.use_ticks = 60
charge.on_ground = at == lying
sim.projectiles.append(charge.id)
## Fairly flat, open ground above the sea near the middle of the island.
func _open_ground() -> Vector3:
var size: float = Tuning.WORLD_SIZE_METRES
+25
View File
@@ -132,3 +132,28 @@ func test_pines_between_the_shoulder_camera_and_the_player_are_hidden() -> void:
for prop: int in hidden_at_some_yaw:
assert_true(view.props.is_drawn(prop))
view.free()
## B8: a thrown keg is drawn in flight and lying, with the item models, and stops being
## drawn once it has blown.
func test_thrown_kegs_are_drawn_until_they_blow() -> void:
var sim: Simulation = Simulation.new(5, SimGround.flat())
var thrower: SimEntity = sim.entities.find(sim.add_player())
thrower.position = Vector3(50.0, 0.0, 50.0)
thrower.on_ground = true
Inventory.give(thrower, ItemCatalog.code_of("powder_keg"), 2)
thrower.selected = 1
var view: MatchView = MatchView.new()
view.setup(sim, 1)
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, PlayerCommand.BUTTON_FIRE, 1))
sim.step()
view.sync()
assert_eq(sim.projectiles.size(), 1)
assert_eq(view.entities.pickup_count(), 1, "the keg in flight")
for _i: int in Explosives.fuse_ticks(ItemCatalog.get_def(ItemCatalog.code_of("powder_keg"))):
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, 0, 1))
sim.step()
view.sync()
assert_eq(sim.projectiles.size(), 0)
assert_eq(view.entities.pickup_count(), 0, "gone once it blew")
view.free()
+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