|
|
|
@@ -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))
|