B6: Weapon framework and damage model
Weapons fire from the server's own copy of the shooter's eye and look, at the weapon's fire rate, from the loaded magazine. An empty trigger pull or the reload button starts a reload of the weapon's reload time from carried ammunition; switching slots cancels it. Each shot is one or more pellets scattered evenly inside a spread cone from the "weapons" stream. Spread is the hip spread (smaller when aiming), plus bloom that builds in sustained fire, plus a moving or airborne penalty. Hitscan pellets trace at once in 16 m segments; projectile weapons (the whaler's longgun and line thrower) spawn entities that fly under gravity and trace each tick. A trace stops at the first of terrain, a solid (built pieces and props take the damage) or another player's body. Players lose armor first, then health; the top of the body takes the weapon's headshot multiplier; and damage falls off linearly from range_full to range_max. Stats come from ItemDef, so adding a weapon is a Tuning entry only. Closes #20 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -35,6 +35,8 @@ var drop: Drop = null
|
|||||||
|
|
||||||
## Items lying in the world, as entity IDs in ascending order. See Inventory.
|
## Items lying in the world, as entity IDs in ascending order. See Inventory.
|
||||||
var pickups: Array[int] = []
|
var pickups: Array[int] = []
|
||||||
|
## Shots in flight, as entity IDs in ascending order. See Weapons.
|
||||||
|
var projectiles: Array[int] = []
|
||||||
|
|
||||||
## What happened during the last step(): harvests, destroyed props, placed pieces.
|
## What happened during the last step(): harvests, destroyed props, placed pieces.
|
||||||
## Each is a Dictionary with a "type". Cleared at the start of every step.
|
## Each is a Dictionary with a "type". Cleared at the start of every step.
|
||||||
@@ -130,6 +132,7 @@ func step() -> void:
|
|||||||
_ride(player, command)
|
_ride(player, command)
|
||||||
else:
|
else:
|
||||||
_apply_command(player, command)
|
_apply_command(player, command)
|
||||||
|
Weapons.advance_projectiles(self)
|
||||||
_check_eliminations()
|
_check_eliminations()
|
||||||
if drop != null and not drop.finished and drop.is_over(tick + 1):
|
if drop != null and not drop.finished and drop.is_over(tick + 1):
|
||||||
# The end of the route: everyone still aboard goes over the side.
|
# The end of the route: everyone still aboard goes over the side.
|
||||||
@@ -158,6 +161,7 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
|||||||
Water.update(player, sea_level, drown_dps, sprinting)
|
Water.update(player, sea_level, drown_dps, sprinting)
|
||||||
if player.action_cooldown > 0:
|
if player.action_cooldown > 0:
|
||||||
player.action_cooldown -= 1
|
player.action_cooldown -= 1
|
||||||
|
Weapons.update(player)
|
||||||
var held: int = command.buttons if command != null else 0
|
var held: int = command.buttons if command != null else 0
|
||||||
var pressed: int = held & ~player.prev_buttons
|
var pressed: int = held & ~player.prev_buttons
|
||||||
player.prev_buttons = held
|
player.prev_buttons = held
|
||||||
@@ -168,6 +172,8 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
|||||||
Inventory.interact(self, player)
|
Inventory.interact(self, player)
|
||||||
if pressed & PlayerCommand.BUTTON_DROP:
|
if pressed & PlayerCommand.BUTTON_DROP:
|
||||||
Inventory.drop_held(self, player)
|
Inventory.drop_held(self, player)
|
||||||
|
if command != null and command.is_pressed(PlayerCommand.BUTTON_RELOAD):
|
||||||
|
Weapons.start_reload(player)
|
||||||
if command == null or not command.is_pressed(PlayerCommand.BUTTON_FIRE):
|
if command == null or not command.is_pressed(PlayerCommand.BUTTON_FIRE):
|
||||||
return
|
return
|
||||||
if not Building.is_allowed(player):
|
if not Building.is_allowed(player):
|
||||||
@@ -177,6 +183,8 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
|||||||
return
|
return
|
||||||
if player.selected == BOATHOOK_SLOT:
|
if player.selected == BOATHOOK_SLOT:
|
||||||
Harvest.swing(player, ground, props, events, structures)
|
Harvest.swing(player, ground, props, events, structures)
|
||||||
|
else:
|
||||||
|
Weapons.fire(self, player, command.is_pressed(PlayerCommand.BUTTON_AIM))
|
||||||
|
|
||||||
|
|
||||||
## Marks players whose health reached 0 as eliminated, and drops what they carried.
|
## Marks players whose health reached 0 as eliminated, and drops what they carried.
|
||||||
|
|||||||
@@ -0,0 +1,275 @@
|
|||||||
|
## Firing, reloading and damage for slot weapons. Stats come from ItemDef, built from
|
||||||
|
## Tuning.WEAPONS, so a new weapon is a table entry and needs no code.
|
||||||
|
##
|
||||||
|
## The client only presses fire, aim and reload. The server fires from its own copy of
|
||||||
|
## the player's eye and look, at the weapon's fire rate, from the loaded magazine.
|
||||||
|
##
|
||||||
|
## A shot is one or more pellets, each turned by a random angle inside the spread cone
|
||||||
|
## from the "weapons" stream. Spread is the weapon's hip spread (times its aim factor
|
||||||
|
## when aiming), plus bloom from recent shots, plus a penalty for moving or being in the
|
||||||
|
## air. Hitscan pellets trace at once to the weapon's range; projectile pellets become
|
||||||
|
## entities that fly under gravity and trace each tick's path.
|
||||||
|
##
|
||||||
|
## A trace stops at the first of terrain, a solid (a built piece, a prop or a point of
|
||||||
|
## interest) or another player's body. Players lose armor first, then health; the top
|
||||||
|
## Tuning.HEAD_HEIGHT of the body takes the weapon's headshot multiplier. Damage falls
|
||||||
|
## off linearly with distance, see ItemDef.damage_at().
|
||||||
|
class_name Weapons
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
## What a trace hit.
|
||||||
|
const HIT_NONE: int = 0
|
||||||
|
const HIT_TERRAIN: int = 1
|
||||||
|
const HIT_SOLID: int = 2
|
||||||
|
const HIT_PIECE: int = 3
|
||||||
|
const HIT_PROP: int = 4
|
||||||
|
const HIT_PLAYER: int = 5
|
||||||
|
## Traces are checked in pieces this long, so long shots stop early and don't query a
|
||||||
|
## huge area of solids at once.
|
||||||
|
const TRACE_SEGMENT_METRES: float = 16.0
|
||||||
|
|
||||||
|
|
||||||
|
## The result of a trace.
|
||||||
|
class Hit:
|
||||||
|
extends RefCounted
|
||||||
|
var kind: int = HIT_NONE
|
||||||
|
## Fraction of the traced segment, 1.0 when nothing was hit.
|
||||||
|
var fraction: float = 1.0
|
||||||
|
var point: Vector3 = Vector3.ZERO
|
||||||
|
## Piece ID, prop index or player entity ID.
|
||||||
|
var target: int = -1
|
||||||
|
var headshot: bool = false
|
||||||
|
|
||||||
|
|
||||||
|
## Counts down reloads and bloom. Called every tick for a player on foot, after the
|
||||||
|
## action cooldown. Bloom only recovers on ticks the weapon is ready to fire, so
|
||||||
|
## sustained fire builds it up and a pause clears it.
|
||||||
|
static func update(player: SimEntity) -> void:
|
||||||
|
if player.action_cooldown == 0:
|
||||||
|
player.bloom = maxf(player.bloom - Tuning.BLOOM_DECAY_DEGREES_PER_SECOND / Tuning.TICK_RATE_HZ, 0.0)
|
||||||
|
if player.reload_ticks > 0:
|
||||||
|
player.reload_ticks -= 1
|
||||||
|
if player.reload_ticks == 0:
|
||||||
|
_finish_reload(player)
|
||||||
|
|
||||||
|
|
||||||
|
## Starts reloading the held weapon if it has room and there's ammunition. Returns true
|
||||||
|
## if a reload started.
|
||||||
|
static func start_reload(player: SimEntity) -> bool:
|
||||||
|
var def: ItemDef = Inventory.held_def(player)
|
||||||
|
if def == null or not def.uses_magazine() or player.reload_ticks > 0:
|
||||||
|
return false
|
||||||
|
var slot: int = Inventory.held_slot(player)
|
||||||
|
if player.slot_rounds[slot] >= def.magazine:
|
||||||
|
return false
|
||||||
|
if player.ammo[Inventory.ammo_index(def.ammo_code)] <= 0:
|
||||||
|
return false
|
||||||
|
player.reload_ticks = maxi(def.reload_ticks(), 1)
|
||||||
|
return true
|
||||||
|
|
||||||
|
|
||||||
|
static func _finish_reload(player: SimEntity) -> void:
|
||||||
|
var def: ItemDef = Inventory.held_def(player)
|
||||||
|
if def == null or not def.uses_magazine():
|
||||||
|
return
|
||||||
|
var slot: int = Inventory.held_slot(player)
|
||||||
|
var index: int = Inventory.ammo_index(def.ammo_code)
|
||||||
|
var loaded: int = mini(def.magazine - player.slot_rounds[slot], player.ammo[index])
|
||||||
|
player.slot_rounds[slot] += loaded
|
||||||
|
player.ammo[index] -= loaded
|
||||||
|
|
||||||
|
|
||||||
|
## 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).
|
||||||
|
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:
|
||||||
|
return false
|
||||||
|
if player.action_cooldown > 0 or player.reload_ticks > 0:
|
||||||
|
return false
|
||||||
|
var slot: int = Inventory.held_slot(player)
|
||||||
|
if def.uses_magazine():
|
||||||
|
if player.slot_rounds[slot] <= 0:
|
||||||
|
start_reload(player)
|
||||||
|
return false
|
||||||
|
player.slot_rounds[slot] -= 1
|
||||||
|
player.action_cooldown = def.fire_interval_ticks()
|
||||||
|
var cone: float = spread_degrees(player, def, aiming)
|
||||||
|
player.bloom = minf(player.bloom + def.bloom_degrees, Tuning.MAX_BLOOM_DEGREES)
|
||||||
|
var from: Vector3 = Harvest.eye(player)
|
||||||
|
var look: Vector3 = Harvest.look_direction(player)
|
||||||
|
var stream: RandomNumberGenerator = sim.rng.stream("weapons")
|
||||||
|
sim.events.append({"type": "shot", "player": player.id, "item": def.code, "from": from, "direction": look})
|
||||||
|
for _pellet: int in def.pellets:
|
||||||
|
var direction: Vector3 = scatter(look, cone, stream)
|
||||||
|
if def.projectile_speed > 0.0:
|
||||||
|
_launch(sim, player, def, slot, from, direction)
|
||||||
|
else:
|
||||||
|
var to: Vector3 = from + direction * def.range_max
|
||||||
|
var hit: Hit = trace(sim, from, to, player.id)
|
||||||
|
_apply_hit(sim, player.id, def, player.slot_tiers[slot], hit, from.distance_to(hit.point))
|
||||||
|
return true
|
||||||
|
|
||||||
|
|
||||||
|
## The cone half-angle a shot would have now, in degrees.
|
||||||
|
static func spread_degrees(player: SimEntity, def: ItemDef, aiming: bool) -> float:
|
||||||
|
var cone: float = def.spread_degrees * (def.aim_spread_factor if aiming else 1.0)
|
||||||
|
cone += player.bloom
|
||||||
|
if not player.on_ground or player.swimming:
|
||||||
|
cone += Tuning.AIRBORNE_SPREAD_DEGREES
|
||||||
|
elif Vector2(player.velocity.x, player.velocity.z).length() > 0.5:
|
||||||
|
cone += Tuning.MOVING_SPREAD_DEGREES
|
||||||
|
return cone
|
||||||
|
|
||||||
|
|
||||||
|
## A direction within `cone_degrees` of `look`, spread evenly over the cone's area.
|
||||||
|
static func scatter(look: Vector3, cone_degrees: float, stream: RandomNumberGenerator) -> Vector3:
|
||||||
|
var u: float = stream.randf()
|
||||||
|
var v: float = stream.randf()
|
||||||
|
if cone_degrees <= 0.0:
|
||||||
|
return look
|
||||||
|
var angle: float = deg_to_rad(cone_degrees) * sqrt(u)
|
||||||
|
var around: float = TAU * v
|
||||||
|
var side: Vector3 = look.cross(Vector3.UP)
|
||||||
|
if side.length_squared() < 0.000001:
|
||||||
|
side = Vector3.RIGHT
|
||||||
|
side = side.normalized()
|
||||||
|
var up: Vector3 = side.cross(look).normalized()
|
||||||
|
var offset: Vector3 = (side * cos(around) + up * sin(around)) * sin(angle)
|
||||||
|
return (look * cos(angle) + offset).normalized()
|
||||||
|
|
||||||
|
|
||||||
|
## The first thing between `from` and `to`, skipping the player `ignore_id`.
|
||||||
|
static func trace(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int = 0) -> Hit:
|
||||||
|
var length: float = from.distance_to(to)
|
||||||
|
var pieces: int = maxi(ceili(length / TRACE_SEGMENT_METRES), 1)
|
||||||
|
for index: int in pieces:
|
||||||
|
var start: float = float(index) / pieces
|
||||||
|
var end: float = float(index + 1) / pieces
|
||||||
|
var hit: Hit = _trace_segment(sim, from.lerp(to, start), from.lerp(to, end), ignore_id)
|
||||||
|
if hit.kind != HIT_NONE:
|
||||||
|
hit.fraction = lerpf(start, end, hit.fraction)
|
||||||
|
return hit
|
||||||
|
var miss: Hit = Hit.new()
|
||||||
|
miss.point = to
|
||||||
|
return miss
|
||||||
|
|
||||||
|
|
||||||
|
static func _trace_segment(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int) -> Hit:
|
||||||
|
var hit: Hit = Hit.new()
|
||||||
|
var ground: SimGround = sim.ground
|
||||||
|
# The nearest solid, and what it belongs to.
|
||||||
|
for solid_id: int in ground.solids_near(minf(from.x, to.x), minf(from.z, to.z), maxf(from.x, to.x), maxf(from.z, to.z)):
|
||||||
|
var t: float = ground.ray_box(from, to, ground.solids[solid_id])
|
||||||
|
if t < hit.fraction or (t == hit.fraction and hit.kind != HIT_NONE and solid_id < hit.target):
|
||||||
|
hit.fraction = t
|
||||||
|
hit.kind = HIT_SOLID
|
||||||
|
hit.target = solid_id
|
||||||
|
if hit.kind == HIT_SOLID:
|
||||||
|
var piece: int = sim.structures.piece_of_solid(hit.target)
|
||||||
|
var prop: int = sim.props.prop_of_solid(hit.target)
|
||||||
|
if piece != 0:
|
||||||
|
hit.kind = HIT_PIECE
|
||||||
|
hit.target = piece
|
||||||
|
elif prop >= 0:
|
||||||
|
hit.kind = HIT_PROP
|
||||||
|
hit.target = prop
|
||||||
|
# Terrain in front of the solid.
|
||||||
|
var terrain: float = ground.raycast(from, to)
|
||||||
|
if terrain < hit.fraction - 0.0001:
|
||||||
|
hit.fraction = terrain
|
||||||
|
hit.kind = HIT_TERRAIN
|
||||||
|
hit.target = -1
|
||||||
|
# Players in front of both.
|
||||||
|
for entity: SimEntity in sim.entities.entities():
|
||||||
|
if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.id == ignore_id or entity.eliminated:
|
||||||
|
continue
|
||||||
|
if entity.drop_state == Drop.ABOARD:
|
||||||
|
continue
|
||||||
|
var box: AABB = body_box(entity)
|
||||||
|
var t: float = ground.ray_box(from, to, box)
|
||||||
|
if t < hit.fraction:
|
||||||
|
hit.fraction = t
|
||||||
|
hit.kind = HIT_PLAYER
|
||||||
|
hit.target = entity.id
|
||||||
|
var point: Vector3 = from.lerp(to, t)
|
||||||
|
hit.headshot = point.y >= box.end.y - Tuning.HEAD_HEIGHT
|
||||||
|
hit.point = from.lerp(to, hit.fraction)
|
||||||
|
return hit
|
||||||
|
|
||||||
|
|
||||||
|
## The box a player's body occupies.
|
||||||
|
static func body_box(player: SimEntity) -> AABB:
|
||||||
|
var radius: float = Tuning.PLAYER_RADIUS
|
||||||
|
return AABB(player.position - Vector3(radius, 0.0, radius), Vector3(radius * 2.0, Movement.body_height(player), radius * 2.0))
|
||||||
|
|
||||||
|
|
||||||
|
## Applies a pellet's hit. `distance` is from where the shot started.
|
||||||
|
static func _apply_hit(sim: Simulation, attacker: int, def: ItemDef, tier: int, hit: Hit, distance: float) -> void:
|
||||||
|
sim.events.append({"type": "impact", "player": attacker, "item": def.code, "point": hit.point, "hit": hit.kind, "target": hit.target})
|
||||||
|
if def.damage <= 0:
|
||||||
|
return
|
||||||
|
var amount: float = def.damage_at(distance) * tier_multiplier(tier)
|
||||||
|
match hit.kind:
|
||||||
|
HIT_PLAYER:
|
||||||
|
if hit.headshot:
|
||||||
|
amount *= def.headshot_multiplier
|
||||||
|
damage_player(sim, sim.entities.find(hit.target), roundi(amount), attacker, hit.headshot)
|
||||||
|
HIT_PIECE:
|
||||||
|
sim.damage_piece(hit.target, roundi(amount * Tuning.WEAPON_STRUCTURE_FACTOR), "weapon")
|
||||||
|
HIT_PROP:
|
||||||
|
if sim.damage_prop(hit.target, roundi(amount * Tuning.WEAPON_STRUCTURE_FACTOR)):
|
||||||
|
sim.events.append({"type": "prop_destroyed", "prop": hit.target})
|
||||||
|
|
||||||
|
|
||||||
|
## Damage multiplier for a quality tier. Tiers arrive with the weapon roster (B7).
|
||||||
|
static func tier_multiplier(_tier: int) -> float:
|
||||||
|
return 1.0
|
||||||
|
|
||||||
|
|
||||||
|
## Takes `amount` from armor, then health. Returns the damage actually done.
|
||||||
|
static func damage_player(sim: Simulation, victim: SimEntity, amount: int, attacker: int, headshot: bool = false) -> int:
|
||||||
|
if victim == null or victim.eliminated or amount <= 0:
|
||||||
|
return 0
|
||||||
|
var to_armor: int = mini(victim.armor, amount)
|
||||||
|
victim.armor -= to_armor
|
||||||
|
var to_health: int = mini(victim.health, amount - to_armor)
|
||||||
|
victim.health -= to_health
|
||||||
|
if attacker != 0 and attacker != victim.id:
|
||||||
|
victim.last_attacker = attacker
|
||||||
|
sim.events.append({
|
||||||
|
"type": "player_hit", "player": attacker, "victim": victim.id,
|
||||||
|
"armor": to_armor, "health": to_health, "headshot": headshot,
|
||||||
|
})
|
||||||
|
return to_armor + to_health
|
||||||
|
|
||||||
|
|
||||||
|
static func _launch(sim: Simulation, player: SimEntity, def: ItemDef, slot: int, from: Vector3, direction: Vector3) -> void:
|
||||||
|
var shot: SimEntity = sim.entities.create(SimEntity.ARCHETYPE_PROJECTILE)
|
||||||
|
shot.position = from
|
||||||
|
shot.origin = from
|
||||||
|
shot.velocity = direction * def.projectile_speed
|
||||||
|
shot.owner_id = player.id
|
||||||
|
shot.item = def.code
|
||||||
|
shot.item_tier = player.slot_tiers[slot]
|
||||||
|
sim.projectiles.append(shot.id)
|
||||||
|
|
||||||
|
|
||||||
|
## Moves every projectile one tick and resolves what they hit.
|
||||||
|
static func advance_projectiles(sim: Simulation) -> void:
|
||||||
|
var dt: float = Simulation.SECONDS_PER_TICK
|
||||||
|
for shot_id: int in sim.projectiles.duplicate():
|
||||||
|
var shot: SimEntity = sim.entities.find(shot_id)
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(shot.item)
|
||||||
|
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)
|
||||||
|
var travelled: float = shot.origin.distance_to(hit.point)
|
||||||
|
var out_of_world: bool = to.y < Tuning.BUILD_MIN_LEVEL * Tuning.BUILD_LEVEL_METRES - 10.0
|
||||||
|
if hit.kind != HIT_NONE:
|
||||||
|
_apply_hit(sim, shot.owner_id, def, shot.item_tier, hit, travelled)
|
||||||
|
if hit.kind != HIT_NONE or travelled >= def.range_max or out_of_world:
|
||||||
|
sim.entities.remove(shot_id)
|
||||||
|
sim.projectiles.erase(shot_id)
|
||||||
|
else:
|
||||||
|
shot.position = to
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://bgxap6khktrsc
|
||||||
@@ -0,0 +1,313 @@
|
|||||||
|
extends TestCase
|
||||||
|
|
||||||
|
## The weapon framework from issue B6: fire-rate limits, magazines and reloads, spread,
|
||||||
|
## damage falloff, headshots, armor, and shots that stop at terrain, players and
|
||||||
|
## structures. Everything goes through commands, so the server's checks are tested.
|
||||||
|
|
||||||
|
const SHOOTER_FEET: Vector3 = Vector3(100.0, 0.0, 100.0)
|
||||||
|
|
||||||
|
|
||||||
|
func _code(id: String) -> int:
|
||||||
|
return ItemCatalog.code_of(id)
|
||||||
|
|
||||||
|
|
||||||
|
## A flat world with a shooter (ID 1) facing +z, holding `weapon` in slot 1.
|
||||||
|
func _sim(weapon: String, ammo: int = 0) -> Simulation:
|
||||||
|
var sim: Simulation = Simulation.new(5, SimGround.flat())
|
||||||
|
var shooter: SimEntity = sim.entities.find(sim.add_player())
|
||||||
|
shooter.position = SHOOTER_FEET
|
||||||
|
shooter.on_ground = true
|
||||||
|
Inventory.give(shooter, _code(weapon), 1)
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code(weapon))
|
||||||
|
if ammo > 0:
|
||||||
|
Inventory.give(shooter, def.ammo_code, ammo)
|
||||||
|
shooter.selected = 1
|
||||||
|
return sim
|
||||||
|
|
||||||
|
|
||||||
|
## Adds a target standing `distance` metres in front of the shooter.
|
||||||
|
func _target_at(sim: Simulation, distance: float) -> SimEntity:
|
||||||
|
var target: SimEntity = sim.entities.find(sim.add_player())
|
||||||
|
target.position = SHOOTER_FEET + Vector3(0.0, 0.0, distance)
|
||||||
|
target.on_ground = true
|
||||||
|
return target
|
||||||
|
|
||||||
|
|
||||||
|
## The pitch, in radians, that aims the shooter's eye at `point`.
|
||||||
|
func _pitch_to(sim: Simulation, point: Vector3) -> float:
|
||||||
|
var eye: Vector3 = Harvest.eye(sim.entities.find(1))
|
||||||
|
var delta: Vector3 = point - eye
|
||||||
|
return atan2(delta.y, Vector2(delta.x, delta.z).length())
|
||||||
|
|
||||||
|
|
||||||
|
## Steps `ticks` ticks with the shooter holding `buttons` and looking at `pitch`.
|
||||||
|
## Returns how many shots were fired.
|
||||||
|
func _hold(sim: Simulation, ticks: int, buttons: int = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, pitch: float = 0.0) -> int:
|
||||||
|
var shots: int = 0
|
||||||
|
for _i: int in ticks:
|
||||||
|
var shooter: SimEntity = sim.entities.find(1)
|
||||||
|
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, pitch, buttons, shooter.selected))
|
||||||
|
for player_id: int in [2, 3]:
|
||||||
|
if sim.entities.contains(player_id):
|
||||||
|
sim.submit_command(player_id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, 0, 0))
|
||||||
|
sim.step()
|
||||||
|
for event: Dictionary in sim.events:
|
||||||
|
if event["type"] == "shot":
|
||||||
|
shots += 1
|
||||||
|
return shots
|
||||||
|
|
||||||
|
|
||||||
|
func _events_of(sim: Simulation, kind: String) -> Array[Dictionary]:
|
||||||
|
var found: Array[Dictionary] = []
|
||||||
|
for event: Dictionary in sim.events:
|
||||||
|
if event["type"] == kind:
|
||||||
|
found.append(event)
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
func test_fire_rate_is_limited_by_the_server() -> void:
|
||||||
|
var sim: Simulation = _sim("dock_carbine")
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code("dock_carbine"))
|
||||||
|
var shots: int = _hold(sim, Tuning.TICK_RATE_HZ * 2)
|
||||||
|
# 6 a second, minus the swap delay at the start.
|
||||||
|
var interval: int = def.fire_interval_ticks()
|
||||||
|
assert_eq(interval, 5)
|
||||||
|
assert_true(shots >= 11 and shots <= 12, "carbine fired %d shots in 2 s" % shots)
|
||||||
|
|
||||||
|
|
||||||
|
func test_each_shot_spends_a_round_and_an_empty_magazine_reloads_from_ammunition() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver", 4)
|
||||||
|
var shooter: SimEntity = sim.entities.find(1)
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code("signal_revolver"))
|
||||||
|
assert_eq(shooter.slot_rounds[0], 6)
|
||||||
|
var shots: int = _hold(sim, Tuning.TICK_RATE_HZ * 4)
|
||||||
|
assert_eq(shots, 6, "six rounds, then dry")
|
||||||
|
assert_eq(shooter.slot_rounds[0], 0)
|
||||||
|
assert_true(shooter.reload_ticks > 0, "an empty trigger pull starts a reload")
|
||||||
|
_hold(sim, def.reload_ticks(), 0)
|
||||||
|
assert_eq(shooter.slot_rounds[0], 4, "only four rounds were carried")
|
||||||
|
assert_eq(shooter.ammo[Inventory.ammo_index(def.ammo_code)], 0)
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_reload_takes_the_weapons_reload_time() -> void:
|
||||||
|
var sim: Simulation = _sim("dock_carbine", 100)
|
||||||
|
var shooter: SimEntity = sim.entities.find(1)
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code("dock_carbine"))
|
||||||
|
shooter.slot_rounds[0] = 10
|
||||||
|
_hold(sim, 1, PlayerCommand.BUTTON_RELOAD)
|
||||||
|
_hold(sim, def.reload_ticks() - 2, 0)
|
||||||
|
assert_eq(shooter.slot_rounds[0], 10, "not done yet")
|
||||||
|
assert_eq(_hold(sim, 1), 0, "no firing while reloading")
|
||||||
|
_hold(sim, 2, 0)
|
||||||
|
assert_eq(shooter.slot_rounds[0], 30)
|
||||||
|
assert_eq(shooter.ammo[Inventory.ammo_index(def.ammo_code)], 80)
|
||||||
|
|
||||||
|
|
||||||
|
func test_without_ammunition_there_is_no_reload() -> void:
|
||||||
|
var sim: Simulation = _sim("dock_carbine")
|
||||||
|
var shooter: SimEntity = sim.entities.find(1)
|
||||||
|
shooter.slot_rounds[0] = 0
|
||||||
|
_hold(sim, 3, PlayerCommand.BUTTON_RELOAD)
|
||||||
|
assert_eq(shooter.reload_ticks, 0)
|
||||||
|
|
||||||
|
|
||||||
|
func test_switching_slots_cancels_a_reload() -> void:
|
||||||
|
var sim: Simulation = _sim("dock_carbine", 100)
|
||||||
|
var shooter: SimEntity = sim.entities.find(1)
|
||||||
|
shooter.slot_rounds[0] = 0
|
||||||
|
_hold(sim, 1, PlayerCommand.BUTTON_RELOAD)
|
||||||
|
assert_true(shooter.reload_ticks > 0)
|
||||||
|
shooter.selected = 1
|
||||||
|
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, 0, 2))
|
||||||
|
sim.step()
|
||||||
|
assert_eq(shooter.reload_ticks, 0)
|
||||||
|
assert_eq(shooter.slot_rounds[0], 0)
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_body_shot_does_the_weapons_damage() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
var target: SimEntity = _target_at(sim, 10.0)
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
|
||||||
|
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH - 38)
|
||||||
|
assert_eq(target.last_attacker, 1)
|
||||||
|
|
||||||
|
|
||||||
|
func test_headshots_multiply_damage() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
var target: SimEntity = _target_at(sim, 10.0)
|
||||||
|
var head: Vector3 = target.position + Vector3(0.0, Tuning.PLAYER_HEIGHT - 0.1, 0.0)
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, head))
|
||||||
|
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH - roundi(38 * 2.0))
|
||||||
|
|
||||||
|
|
||||||
|
func test_damage_falls_off_with_distance() -> void:
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code("signal_revolver"))
|
||||||
|
assert_near(def.damage_at(5.0), 38.0, 0.001, "full damage inside range_full")
|
||||||
|
assert_near(def.damage_at(30.0), 38.0, 0.001)
|
||||||
|
assert_near(def.damage_at(55.0), 38.0 * 0.75, 0.001, "halfway down the falloff")
|
||||||
|
assert_near(def.damage_at(80.0), 19.0, 0.001)
|
||||||
|
assert_near(def.damage_at(500.0), 19.0, 0.001, "never below falloff_min")
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_far_hit_does_less_damage() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
var target: SimEntity = _target_at(sim, 55.0)
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
|
||||||
|
var expected: int = roundi(ItemCatalog.get_def(_code("signal_revolver")).damage_at(_distance_hit(sim, target)))
|
||||||
|
assert_true(Tuning.PLAYER_MAX_HEALTH - target.health <= expected + 1 and Tuning.PLAYER_MAX_HEALTH - target.health >= expected - 1,
|
||||||
|
"took %d, expected about %d" % [Tuning.PLAYER_MAX_HEALTH - target.health, expected])
|
||||||
|
assert_true(target.health > Tuning.PLAYER_MAX_HEALTH - 38)
|
||||||
|
|
||||||
|
|
||||||
|
func _distance_hit(sim: Simulation, target: SimEntity) -> float:
|
||||||
|
return Harvest.eye(sim.entities.find(1)).distance_to(target.position + Vector3(0.0, 0.9, -Tuning.PLAYER_RADIUS))
|
||||||
|
|
||||||
|
|
||||||
|
func test_armor_soaks_damage_before_health() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
var target: SimEntity = _target_at(sim, 10.0)
|
||||||
|
target.armor = 20
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
|
||||||
|
assert_eq(target.armor, 0)
|
||||||
|
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH - 18)
|
||||||
|
|
||||||
|
|
||||||
|
func test_lethal_damage_eliminates_and_credits_the_shooter() -> void:
|
||||||
|
var sim: Simulation = _sim("whalers_longgun")
|
||||||
|
var target: SimEntity = _target_at(sim, 20.0)
|
||||||
|
target.health = 50
|
||||||
|
var eliminated: Array[Dictionary] = []
|
||||||
|
var pitch: float = _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0))
|
||||||
|
for _i: int in 15:
|
||||||
|
_hold(sim, 1, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, pitch)
|
||||||
|
eliminated.append_array(_events_of(sim, "eliminated"))
|
||||||
|
assert_true(target.eliminated)
|
||||||
|
assert_eq(target.health, 0)
|
||||||
|
assert_eq(eliminated.size(), 1)
|
||||||
|
assert_eq(eliminated[0]["by"], 1)
|
||||||
|
|
||||||
|
|
||||||
|
func test_terrain_stops_a_shot() -> void:
|
||||||
|
var heights: PackedFloat32Array = PackedFloat32Array()
|
||||||
|
var side: int = 128
|
||||||
|
for z: int in side:
|
||||||
|
for x: int in side:
|
||||||
|
heights.append(3.0 if z == 55 else 0.0)
|
||||||
|
var sim: Simulation = Simulation.new(5, SimGround.from_heights(heights, side, 2.0))
|
||||||
|
var shooter: SimEntity = sim.entities.find(sim.add_player())
|
||||||
|
shooter.position = SHOOTER_FEET
|
||||||
|
Inventory.give(shooter, _code("signal_revolver"), 1)
|
||||||
|
shooter.selected = 1
|
||||||
|
var target: SimEntity = _target_at(sim, 20.0)
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
|
||||||
|
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH, "the ridge takes the shot")
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_built_wall_stops_a_shot_and_takes_the_damage() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
var target: SimEntity = _target_at(sim, 12.0)
|
||||||
|
var wall: int = sim.structures.add(StructureSet.WALL, Materials.STONE, Vector4i(24, 0, 26, StructureSet.SLOT_WALL_Z), 0, 1, sim.tick, sim.ground).id
|
||||||
|
sim.structures.find(wall).build_ticks_left = 0
|
||||||
|
sim.structures.find(wall).health = 300
|
||||||
|
var before: int = sim.structures.find(wall).health
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
|
||||||
|
assert_eq(target.health, Tuning.PLAYER_MAX_HEALTH)
|
||||||
|
assert_eq(sim.structures.find(wall).health, before - 38)
|
||||||
|
|
||||||
|
|
||||||
|
func test_shots_never_hit_the_shooter() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
_hold(sim, 10, PlayerCommand.BUTTON_FIRE, -PI * 0.5)
|
||||||
|
assert_eq(sim.entities.find(1).health, Tuning.PLAYER_MAX_HEALTH)
|
||||||
|
|
||||||
|
|
||||||
|
func test_spread_grows_with_bloom_and_movement_and_shrinks_when_aiming() -> void:
|
||||||
|
var sim: Simulation = _sim("dock_carbine")
|
||||||
|
var shooter: SimEntity = sim.entities.find(1)
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code("dock_carbine"))
|
||||||
|
var hip: float = Weapons.spread_degrees(shooter, def, false)
|
||||||
|
var aimed: float = Weapons.spread_degrees(shooter, def, true)
|
||||||
|
assert_near(hip, 1.2, 0.0001)
|
||||||
|
assert_near(aimed, 1.2 * Tuning.AIM_SPREAD_FACTOR, 0.0001)
|
||||||
|
shooter.velocity = Vector3(4.0, 0.0, 0.0)
|
||||||
|
assert_near(Weapons.spread_degrees(shooter, def, false), hip + Tuning.MOVING_SPREAD_DEGREES, 0.0001)
|
||||||
|
shooter.velocity = Vector3.ZERO
|
||||||
|
shooter.on_ground = false
|
||||||
|
assert_near(Weapons.spread_degrees(shooter, def, false), hip + Tuning.AIRBORNE_SPREAD_DEGREES, 0.0001)
|
||||||
|
shooter.on_ground = true
|
||||||
|
_hold(sim, 30)
|
||||||
|
assert_true(shooter.bloom > 0.0, "sustained fire blooms")
|
||||||
|
assert_true(shooter.bloom <= Tuning.MAX_BLOOM_DEGREES)
|
||||||
|
_hold(sim, 60, 0)
|
||||||
|
assert_eq(shooter.bloom, 0.0, "bloom decays when not firing")
|
||||||
|
|
||||||
|
|
||||||
|
func test_scatter_stays_inside_the_cone() -> void:
|
||||||
|
var stream: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||||
|
stream.seed = 3
|
||||||
|
var look: Vector3 = Vector3(0.3, 0.2, 1.0).normalized()
|
||||||
|
for _i: int in 500:
|
||||||
|
var direction: Vector3 = Weapons.scatter(look, 5.0, stream)
|
||||||
|
assert_true(rad_to_deg(direction.angle_to(look)) <= 5.0001)
|
||||||
|
assert_near(direction.length(), 1.0, 0.0001)
|
||||||
|
assert_eq(Weapons.scatter(Vector3.UP, 0.0, stream), Vector3.UP)
|
||||||
|
assert_true(Weapons.scatter(Vector3.UP, 3.0, stream).angle_to(Vector3.UP) <= deg_to_rad(3.0001), "straight up works too")
|
||||||
|
|
||||||
|
|
||||||
|
func test_the_fowling_piece_fires_all_its_pellets() -> void:
|
||||||
|
var sim: Simulation = _sim("fowling_piece")
|
||||||
|
var target: SimEntity = _target_at(sim, 4.0)
|
||||||
|
var pellets: int = 0
|
||||||
|
for _i: int in 10:
|
||||||
|
_hold(sim, 1, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, _pitch_to(sim, target.position + Vector3(0.0, 0.9, 0.0)))
|
||||||
|
pellets += _events_of(sim, "impact").size()
|
||||||
|
assert_eq(pellets, 9)
|
||||||
|
assert_true(target.health < Tuning.PLAYER_MAX_HEALTH - 9 * 3, "most pellets land at close range")
|
||||||
|
|
||||||
|
|
||||||
|
func test_projectiles_fly_drop_and_hit() -> void:
|
||||||
|
var sim: Simulation = _sim("whalers_longgun")
|
||||||
|
var target: SimEntity = _target_at(sim, 150.0)
|
||||||
|
var pitch: float = _pitch_to(sim, target.position + Vector3(0.0, 1.2, 0.0))
|
||||||
|
_hold(sim, 9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, pitch)
|
||||||
|
assert_eq(sim.projectiles.size(), 1, "the ball is in flight")
|
||||||
|
var shot: SimEntity = sim.entities.find(sim.projectiles[0])
|
||||||
|
assert_true(shot.velocity.y < sin(pitch) * 300.0, "gravity pulls it down")
|
||||||
|
_hold(sim, 30, 0)
|
||||||
|
assert_eq(sim.projectiles.size(), 0)
|
||||||
|
assert_true(target.health < Tuning.PLAYER_MAX_HEALTH, "it lands on the target")
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_missed_projectile_expires_at_its_range() -> void:
|
||||||
|
var sim: Simulation = _sim("whalers_longgun")
|
||||||
|
_hold(sim, 9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_AIM, deg_to_rad(5.0))
|
||||||
|
assert_eq(sim.projectiles.size(), 1)
|
||||||
|
_hold(sim, 60, 0)
|
||||||
|
assert_eq(sim.projectiles.size(), 0)
|
||||||
|
|
||||||
|
|
||||||
|
func test_firing_is_deterministic() -> void:
|
||||||
|
var a: Simulation = _sim("dock_carbine", 60)
|
||||||
|
var b: Simulation = _sim("dock_carbine", 60)
|
||||||
|
_target_at(a, 30.0)
|
||||||
|
_target_at(b, 30.0)
|
||||||
|
_hold(a, 90, PlayerCommand.BUTTON_FIRE, 0.02)
|
||||||
|
_hold(b, 90, PlayerCommand.BUTTON_FIRE, 0.02)
|
||||||
|
assert_eq(a.state_hash(), b.state_hash())
|
||||||
|
|
||||||
|
|
||||||
|
func test_swimmers_cannot_fire() -> void:
|
||||||
|
var sim: Simulation = _sim("signal_revolver")
|
||||||
|
sim.set_sea_level(5.0)
|
||||||
|
assert_eq(_hold(sim, 30), 0)
|
||||||
|
|
||||||
|
|
||||||
|
func test_adding_a_weapon_is_data_only() -> void:
|
||||||
|
# Every weapon in the table becomes a definition with its stats, no code per weapon.
|
||||||
|
for id: String in Tuning.WEAPONS:
|
||||||
|
if id == ItemCatalog.BOATHOOK:
|
||||||
|
continue
|
||||||
|
var def: ItemDef = ItemCatalog.get_def(_code(id))
|
||||||
|
assert_not_null(def, id)
|
||||||
|
assert_eq(def.damage, int(Tuning.WEAPONS[id]["damage"]))
|
||||||
|
assert_eq(def.magazine, int(Tuning.WEAPONS[id]["magazine"]))
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://baumbnflplfnj
|
||||||
Reference in New Issue
Block a user