B1: Harvesting and materials
Boathook swings harvest props. The client only presses fire with the boathook selected; the server casts the swing from its own copy of the player's eye and look, so range and swing rate are enforced by construction. Each hit damages the prop and gives its material (driftwood, stone or salvage iron), capped per material. - Materials: material codes and add/spend helpers; carried stock lives on SimEntity.materials and is part of the state hash. - Harvest: swing interval from the boathook's fire rate, reach from its range plus slack, nearest prop along the ray, blocked by terrain or other solids. Emits harvest and prop_destroyed events. - Simulation: per-tick events list, a shared action cooldown, and fire with selection 0 outside build mode swings. - Tuning: HARVEST_DAMAGE, HARVEST_REACH_SLACK, HARVEST_AIM_SLACK. - 12 tests: yields per kind, rate limit, reach, blocking, nearest prop, caps, destruction clearing collision, state gating and determinism on a real island. Closes #15 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
## Boathook swings that harvest props. The client only presses fire; the server works
|
||||
## out what the swing hits from its own copy of the player's position and look, so
|
||||
## range and rate are validated by construction.
|
||||
##
|
||||
## A swing is a ray from the eye along the look direction, reaching the boathook's
|
||||
## range plus Tuning.HARVEST_REACH_SLACK. It hits the nearest standing prop whose box,
|
||||
## grown by Tuning.HARVEST_AIM_SLACK, the ray enters, unless terrain or another solid is
|
||||
## in the way first. Each hit does Tuning.HARVEST_DAMAGE to the prop and gives the
|
||||
## player Tuning.HARVEST_PER_HIT of the prop's material, up to Tuning.MATERIAL_CAP.
|
||||
class_name Harvest
|
||||
extends RefCounted
|
||||
|
||||
## The eye sits this far below the top of the body.
|
||||
const EYE_BELOW_TOP: float = 0.2
|
||||
|
||||
|
||||
## Ticks between swings, from the boathook's fire rate.
|
||||
static func swing_interval_ticks() -> int:
|
||||
return ceili(Tuning.TICK_RATE_HZ / float(Tuning.WEAPONS["boathook"]["fire_rate"]))
|
||||
|
||||
|
||||
static func reach() -> float:
|
||||
return float(Tuning.WEAPONS["boathook"]["range_max"]) + Tuning.HARVEST_REACH_SLACK
|
||||
|
||||
|
||||
static func eye(player: SimEntity) -> Vector3:
|
||||
return player.position + Vector3(0.0, Movement.body_height(player) - EYE_BELOW_TOP, 0.0)
|
||||
|
||||
|
||||
## The look direction for an entity's yaw and pitch: +z at yaw 0, positive pitch up.
|
||||
static func look_direction(player: SimEntity) -> Vector3:
|
||||
var yaw: float = float(player.yaw) / PlayerCommand.YAW_STEPS * TAU
|
||||
var pitch: float = float(player.pitch) / PlayerCommand.PITCH_MAX * (PI * 0.5)
|
||||
return Vector3(sin(yaw) * cos(pitch), sin(pitch), cos(yaw) * cos(pitch))
|
||||
|
||||
|
||||
## True if the player is in a state that allows swinging: on foot, alive, not swimming.
|
||||
static func can_swing(player: SimEntity) -> bool:
|
||||
return player.health > 0 and player.drop_state == Drop.NONE and not player.swimming
|
||||
|
||||
|
||||
## The prop a swing from the player's current pose would hit, or -1.
|
||||
static func target(player: SimEntity, ground: SimGround, props: PropSet) -> int:
|
||||
var from: Vector3 = eye(player)
|
||||
var to: Vector3 = from + look_direction(player) * reach()
|
||||
var slack: float = Tuning.HARVEST_AIM_SLACK
|
||||
var best: int = -1
|
||||
var best_t: float = 1.0
|
||||
for solid_id: int in ground.solids_near(
|
||||
minf(from.x, to.x) - slack, minf(from.z, to.z) - slack,
|
||||
maxf(from.x, to.x) + slack, maxf(from.z, to.z) + slack):
|
||||
var prop: int = props.prop_of_solid(solid_id)
|
||||
if prop < 0 or not props.is_standing(prop):
|
||||
continue
|
||||
var t: float = ground.ray_box(from, to, props.boxes[prop].grow(slack))
|
||||
if t < best_t or (t == best_t and best >= 0 and prop < best):
|
||||
best_t = t
|
||||
best = prop
|
||||
if best < 0:
|
||||
return -1
|
||||
# Anything nearer than the prop's grown box blocks the swing.
|
||||
if ground.raycast(from, to) < best_t - 0.0001:
|
||||
return -1
|
||||
return best
|
||||
|
||||
|
||||
## Swings if the player can and isn't cooling down. Returns the prop hit, or -1.
|
||||
## Appends "harvest" and "prop_destroyed" events to `events`.
|
||||
static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary]) -> int:
|
||||
if player.action_cooldown > 0 or not can_swing(player):
|
||||
return -1
|
||||
player.action_cooldown = swing_interval_ticks()
|
||||
var prop: int = target(player, ground, props)
|
||||
if prop < 0:
|
||||
return -1
|
||||
var code: int = Materials.code_of(props.material_of(prop))
|
||||
var gained: int = Materials.add(player, code, Tuning.HARVEST_PER_HIT)
|
||||
events.append({"type": "harvest", "player": player.id, "prop": prop, "material": code, "amount": gained})
|
||||
if props.damage(prop, Tuning.HARVEST_DAMAGE):
|
||||
events.append({"type": "prop_destroyed", "prop": prop})
|
||||
return prop
|
||||
@@ -0,0 +1 @@
|
||||
uid://b560mwnfowr3y
|
||||
@@ -0,0 +1,37 @@
|
||||
## The three building materials and the helpers for a player's carried stock. A player
|
||||
## carries each up to Tuning.MATERIAL_CAP; stock lives on SimEntity.materials.
|
||||
class_name Materials
|
||||
extends RefCounted
|
||||
|
||||
const DRIFTWOOD: int = 0
|
||||
const STONE: int = 1
|
||||
const SALVAGE_IRON: int = 2
|
||||
## Material codes index this list. Each name is a key of Tuning.MATERIALS.
|
||||
const NAMES: Array[String] = ["driftwood", "stone", "salvage_iron"]
|
||||
|
||||
|
||||
## The code for a material name, or -1 if there is no such material.
|
||||
static func code_of(material_name: String) -> int:
|
||||
return NAMES.find(material_name)
|
||||
|
||||
|
||||
static func is_valid(code: int) -> bool:
|
||||
return code >= 0 and code < NAMES.size()
|
||||
|
||||
|
||||
## Adds up to `amount` of a material, stopping at the cap. Returns how much was added.
|
||||
static func add(player: SimEntity, code: int, amount: int) -> int:
|
||||
if not is_valid(code) or amount <= 0:
|
||||
return 0
|
||||
var added: int = mini(amount, Tuning.MATERIAL_CAP - player.materials[code])
|
||||
added = maxi(added, 0)
|
||||
player.materials[code] += added
|
||||
return added
|
||||
|
||||
|
||||
## Spends `amount` if the player has it. Returns false, spending nothing, if not.
|
||||
static func spend(player: SimEntity, code: int, amount: int) -> bool:
|
||||
if not is_valid(code) or amount < 0 or player.materials[code] < amount:
|
||||
return false
|
||||
player.materials[code] -= amount
|
||||
return true
|
||||
@@ -0,0 +1 @@
|
||||
uid://isgf8ukj4ob0
|
||||
@@ -47,6 +47,8 @@ var solid_ids: PackedInt32Array = PackedInt32Array()
|
||||
var destroyed: PackedInt32Array = PackedInt32Array()
|
||||
|
||||
var _ground: SimGround
|
||||
## SimGround solid ID -> prop ID, for props whose box is in `_ground`.
|
||||
var _by_solid: Dictionary = {}
|
||||
|
||||
|
||||
## Places props for `island`, clear of `placed` points of interest.
|
||||
@@ -88,6 +90,13 @@ func add_solids_to(ground: SimGround) -> void:
|
||||
solid_ids.resize(count())
|
||||
for prop: int in count():
|
||||
solid_ids[prop] = ground.add_solid(boxes[prop]) if is_standing(prop) else -1
|
||||
if solid_ids[prop] >= 0:
|
||||
_by_solid[solid_ids[prop]] = prop
|
||||
|
||||
|
||||
## The prop whose box is ground solid `solid_id`, or -1 if that solid isn't a prop.
|
||||
func prop_of_solid(solid_id: int) -> int:
|
||||
return _by_solid.get(solid_id, -1)
|
||||
|
||||
|
||||
## Takes `amount` health from a standing prop. Returns true if this destroyed it, which
|
||||
@@ -101,6 +110,7 @@ func damage(prop: int, amount: int) -> bool:
|
||||
destroyed.append(prop)
|
||||
if _ground != null and solid_ids[prop] >= 0:
|
||||
_ground.remove_solid(solid_ids[prop])
|
||||
_by_solid.erase(solid_ids[prop])
|
||||
solid_ids[prop] = -1
|
||||
return true
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ var stamina_ticks: int = 0
|
||||
var drown_ticks: int = 0
|
||||
## Where the player is in the drop: Drop.NONE, ABOARD, FREEFALL or GLIDING.
|
||||
var drop_state: int = 0
|
||||
## Carried building materials, indexed like Materials.NAMES.
|
||||
var materials: PackedInt32Array = PackedInt32Array([0, 0, 0])
|
||||
## Ticks until the player may swing, fire or place again.
|
||||
var action_cooldown: int = 0
|
||||
|
||||
|
||||
func _init(entity_id: int, entity_archetype: int) -> void:
|
||||
|
||||
@@ -203,6 +203,12 @@ func point_inside(point: Vector3, margin: float = 0.0) -> bool:
|
||||
return false
|
||||
|
||||
|
||||
## Where the segment from `from` to `to` enters `box`, as a fraction of the way, or 1.0
|
||||
## for a miss. 0 if `from` is inside the box.
|
||||
func ray_box(from: Vector3, to: Vector3, box: AABB) -> float:
|
||||
return _ray_box(from, to, box)
|
||||
|
||||
|
||||
func _inside_footprint(box: AABB, x: float, z: float, radius: float) -> bool:
|
||||
return (
|
||||
x > box.position.x - radius and x < box.end.x + radius
|
||||
|
||||
@@ -7,6 +7,8 @@ extends RefCounted
|
||||
|
||||
const SECONDS_PER_TICK: float = 1.0 / Tuning.TICK_RATE_HZ
|
||||
const SPAWN_ATTEMPTS: int = 64
|
||||
## The command selection that holds the boathook, which every player always carries.
|
||||
const BOATHOOK_SLOT: int = 0
|
||||
|
||||
var tick: int = 0
|
||||
var entities: EntityRegistry = EntityRegistry.new()
|
||||
@@ -29,6 +31,10 @@ var drown_dps: int = Tuning.TIDE_PHASES[0]["drown_dps"]
|
||||
## The sky barge, from start_drop() on. Null in matches and tests that start on foot.
|
||||
var drop: Drop = null
|
||||
|
||||
## 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.
|
||||
var events: Array[Dictionary] = []
|
||||
|
||||
## Player entity ID -> { command tick -> PlayerCommand }
|
||||
var _pending: Dictionary = {}
|
||||
## Player entity IDs, ascending.
|
||||
@@ -102,6 +108,7 @@ func start_drop() -> void:
|
||||
|
||||
## Advances the simulation by one tick.
|
||||
func step() -> void:
|
||||
events.clear()
|
||||
for player_id: int in _players:
|
||||
var queued: Dictionary = _pending[player_id]
|
||||
var command: PlayerCommand = queued.get(tick)
|
||||
@@ -136,6 +143,14 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
||||
Movement.step(player, command, ground, sea_level)
|
||||
var sprinting: bool = command != null and command.is_pressed(PlayerCommand.BUTTON_SPRINT)
|
||||
Water.update(player, sea_level, drown_dps, sprinting)
|
||||
if player.action_cooldown > 0:
|
||||
player.action_cooldown -= 1
|
||||
if command == null or not command.is_pressed(PlayerCommand.BUTTON_FIRE):
|
||||
return
|
||||
if command.is_pressed(PlayerCommand.BUTTON_BUILD):
|
||||
return
|
||||
if command.selection == BOATHOOK_SLOT:
|
||||
Harvest.swing(player, ground, props, events)
|
||||
|
||||
|
||||
## Moves the sea surface. Takes effect on the next step.
|
||||
@@ -170,6 +185,8 @@ func state_hash() -> String:
|
||||
buffer.put_32(entity.stamina_ticks)
|
||||
buffer.put_32(entity.drown_ticks)
|
||||
buffer.put_u8(entity.drop_state)
|
||||
buffer.put_data(entity.materials.to_byte_array())
|
||||
buffer.put_32(entity.action_cooldown)
|
||||
for value: float in [entity.position.x, entity.position.y, entity.position.z]:
|
||||
buffer.put_64(roundi(value * 1000.0))
|
||||
for value: float in [entity.velocity.x, entity.velocity.y, entity.velocity.z]:
|
||||
|
||||
@@ -177,8 +177,17 @@ const TIDE_PHASES: Array[Dictionary] = [
|
||||
|
||||
# Building
|
||||
const GRID_CELL_METRES: float = 4.0
|
||||
## The most of each material a player can carry.
|
||||
const MATERIAL_CAP: int = 500
|
||||
## Material gained per boathook hit on a prop, whatever the prop.
|
||||
const HARVEST_PER_HIT: int = 15
|
||||
## Damage a boathook hit does to a prop. Players take the boathook's `damage` instead.
|
||||
const HARVEST_DAMAGE: int = 50
|
||||
## A swing reaches this far past the boathook's range, from the eye, so a prop the body
|
||||
## is touching is always in reach.
|
||||
const HARVEST_REACH_SLACK: float = 0.4
|
||||
## Props are hit if the swing passes within this distance of their box.
|
||||
const HARVEST_AIM_SLACK: float = 0.25
|
||||
## Material units each piece costs. Health and build time come from MATERIALS.
|
||||
const BUILD_PIECES: Dictionary = {
|
||||
"wall": {"cost": 10},
|
||||
|
||||
Reference in New Issue
Block a user