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:
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
@@ -0,0 +1 @@
|
||||
uid://ddj3xo7sqb4dq
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
@@ -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
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user