W5: Vegetation and harvestable props

PropSet places about 3,000 pines, scrub, boulders, wrecks and crates per
island from the seed, by elevation band with grove noise, clear of points
of interest. Each prop is one collision box in SimGround with health from
Tuning.PROP_KINDS; destroying it removes the box at once
(SimGround.remove_solid). Prop health is part of the state hash.

PropView draws them as one MultiMesh per kind per 128 m square (about 150
instanced draws), with per-kind visibility ranges, procedural flat-shaded
meshes fitted to the collision boxes, and cheap sync of felled props.

Closes #12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 12:17:42 -05:00
co-authored by Claude Opus 5.5
parent 283d0e966e
commit 5db53de4e8
20 changed files with 1116 additions and 1 deletions
+10
View File
@@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"]
[node name="CaptureProps" type="Node3D"]
script = ExtResource("1")
island_seed = 2
prop_kind = "wreck"
prop_crowd = 1
prop_view = Vector3(9, 4, 9)
+10
View File
@@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"]
[node name="CaptureProps" type="Node3D"]
script = ExtResource("1")
island_seed = 2
prop_kind = "crate"
prop_crowd = 2
prop_view = Vector3(5, 2.5, 6)
+11
View File
@@ -0,0 +1,11 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"]
[node name="CaptureProps" type="Node3D"]
script = ExtResource("1")
island_seed = 2
prop_kind = "pine"
prop_crowd = 30
prop_view = Vector3(-20, 7, 28)
knock_out = 2
+10
View File
@@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"]
[node name="CaptureProps" type="Node3D"]
script = ExtResource("1")
island_seed = 2
prop_kind = "pine"
prop_crowd = 30
prop_view = Vector3(-20, 7, 28)
+10
View File
@@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"]
[node name="CaptureProps" type="Node3D"]
script = ExtResource("1")
island_seed = 2
prop_kind = "boulder"
prop_crowd = 6
prop_view = Vector3(4, 2.2, 5)
+8
View File
@@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"]
[node name="CaptureProps" type="Node3D"]
script = ExtResource("1")
island_seed = 2
show_props = true
+39
View File
@@ -19,6 +19,16 @@ const CRACK_COLOUR: Color = Color(1.0, 0.0, 1.0)
## the camera stands `poi_view` (x right, y up, z out of the front) from its origin.
@export var poi_kind: String = ""
@export var poi_view: Vector3 = Vector3(6.0, 7.0, 22.0)
## Draw the island's props too.
@export var show_props: bool = false
## When set, frame the first prop of this kind whose surroundings hold at least
## `prop_crowd` others within 25 m: the camera stands `prop_view` from it (x east, y up,
## z south) and looks at it.
@export var prop_kind: String = ""
@export var prop_crowd: int = 0
@export var prop_view: Vector3 = Vector3(10.0, 4.0, 14.0)
## Destroy every `knock_out`th prop near the framed one, to show felled props vanish.
@export var knock_out: int = 0
func _ready() -> void:
@@ -74,6 +84,35 @@ func _ready() -> void:
camera.position.y = maxf(camera.position.y, island.ground.terrain_height(camera.position.x, camera.position.z) + 2.0)
look_at_point = poi.origin + Vector3.UP * 2.5
break
if show_props or not prop_kind.is_empty():
var sim_props: PropSet = PropSet.generate(island, PoiSet.generate(island))
var prop_view_node: PropView = PropView.new()
prop_view_node.setup(sim_props)
add_child(prop_view_node)
if not prop_kind.is_empty():
for prop: int in sim_props.count():
if sim_props.kind_name(prop) != prop_kind:
continue
var at: Vector3 = sim_props.boxes[prop].get_center()
var crowd: PackedInt32Array = sim_props.near(at, 25.0)
if crowd.size() <= prop_crowd:
continue
var base: Vector3 = Vector3(at.x, island.ground.terrain_height(at.x, at.z), at.z)
# Back away along the view offset until no canopy is in the camera's face.
var offset: Vector3 = prop_view
for _try: int in 12:
camera.position = base + offset
camera.position.y = maxf(camera.position.y, island.ground.terrain_height(camera.position.x, camera.position.z) + 1.6)
if sim_props.near(Vector3(camera.position.x, island.ground.terrain_height(camera.position.x, camera.position.z), camera.position.z), 6.0).is_empty():
break
offset *= 1.15
look_at_point = base + Vector3.UP * minf(sim_props.boxes[prop].size.y * 0.5, 3.0)
if knock_out > 0:
for index: int in crowd.size():
if index % knock_out == 1:
sim_props.damage(crowd[index], 100000)
prop_view_node.sync()
break
add_child(camera)
camera.look_at(look_at_point)
camera.make_current()
+125
View File
@@ -0,0 +1,125 @@
extends TestCase
## The client side of W5: PropView draws every prop, matches collision, hides felled
## props cheaply, and keeps the draw-call count bounded.
const SEED: int = 2
var _island: Island
var _props: PropSet
func before_each() -> void:
if _island == null:
_island = Island.generate(SEED)
_props = PropSet.generate(_island, PoiSet.generate(_island))
func test_every_prop_has_exactly_one_instance() -> void:
var view: PropView = PropView.new()
view.setup(_props)
var seen: Dictionary = {}
var total: int = 0
for child: Node in view.get_children():
total += (child as MultiMeshInstance3D).multimesh.instance_count
assert_eq(total, _props.count())
assert_eq(view.drawn, _props.count())
for prop: int in _props.count():
var key: Vector2i = Vector2i(view.node_for(prop).get_index(), view.instance_index(prop))
assert_false(seen.has(key), "two props share an instance")
seen[key] = true
assert_true(view.node_for(prop).name.begins_with(_props.kind_name(prop)), "prop %d is drawn as the wrong kind" % prop)
view.free()
func test_instances_fit_their_collision_boxes() -> void:
# The unit meshes span -0.5..0.5 and 0..1, so the transformed footprint must match
# the box the simulation collides with (pines' canopies excepted: only the trunk).
for prop: int in _props.count():
var box: AABB = _props.boxes[prop]
var xf: Transform3D = PropView.instance_transform(_props, prop)
assert_near(xf.origin.y, box.position.y, 0.001)
assert_near(xf.origin.x, box.get_center().x, 0.001)
assert_near(xf.origin.z, box.get_center().z, 0.001)
var top: Vector3 = xf * Vector3(0.0, 1.0, 0.0)
assert_near(top.y, box.end.y, 0.001, "prop %d height" % prop)
# Round kinds are checked around their rim, boxy ones at their corners.
var outline: Array[Vector3] = [Vector3(0.5, 0.0, 0.5), Vector3(-0.5, 0.0, 0.5), Vector3(0.5, 0.0, -0.5), Vector3(-0.5, 0.0, -0.5)]
if _props.kind_name(prop) in ["pine", "scrub", "boulder"]:
outline.clear()
for step: int in 12:
outline.append(Vector3(cos(TAU * step / 12.0), 0.0, sin(TAU * step / 12.0)) * 0.5)
for corner: Vector3 in outline:
var at: Vector3 = xf * corner
var spill: float = maxf(maxf(box.position.x - at.x, at.x - box.end.x), maxf(box.position.z - at.z, at.z - box.end.z))
# Crates may turn up to 8 degrees, which pushes a corner out by about 10%.
var allowed: float = 0.12 * maxf(box.size.x, box.size.z) if _props.kind_name(prop) == "crate" else 0.01
assert_true(spill <= allowed, "%s %d footprint spills %.2f m past its box" % [_props.kind_name(prop), prop, spill])
func test_meshes_face_outwards_and_stay_in_the_unit_footprint() -> void:
for kind: String in PropSet.KINDS:
var mesh: ArrayMesh = PropView.build_mesh(kind)
var arrays: Array = mesh.surface_get_arrays(0)
var vertices: PackedVector3Array = arrays[Mesh.ARRAY_VERTEX]
var normals: PackedVector3Array = arrays[Mesh.ARRAY_NORMAL]
assert_true(vertices.size() >= 36, kind + " has too few vertices")
var bounds: AABB = AABB(vertices[0], Vector3.ZERO)
var centre: Vector3 = Vector3.ZERO
for vertex: Vector3 in vertices:
bounds = bounds.expand(vertex)
centre += vertex
centre /= vertices.size()
assert_true(bounds.position.y >= -0.001 and bounds.end.y <= 1.001, "%s spans y %s" % [kind, bounds])
if kind != "pine":
assert_true(bounds.position.x >= -0.501 and bounds.end.x <= 0.501, "%s spans x %s" % [kind, bounds])
assert_true(bounds.position.z >= -0.501 and bounds.end.z <= 0.501, "%s spans z %s" % [kind, bounds])
# Most faces point away from the middle; inside-out winding would flip this.
var outward: int = 0
for index: int in vertices.size():
if normals[index].dot(vertices[index] - centre) > 0.0:
outward += 1
assert_true(outward > vertices.size() * 0.6, "%s: only %d of %d normals face out" % [kind, outward, vertices.size()])
func test_felled_props_vanish_and_the_rest_stay() -> void:
var view: PropView = PropView.new()
view.setup(_props)
var victims: Array[int] = [0, 17, _props.count() - 1]
for prop: int in victims:
while _props.is_standing(prop):
_props.damage(prop, 1000)
assert_eq(view.sync(), victims.size())
assert_eq(view.sync(), 0, "a second sync has nothing new")
assert_eq(view.drawn, _props.count() - victims.size())
assert_eq(view.hidden_count, victims.size())
for prop: int in [0, 1, 17, 18, _props.count() - 1]:
assert_eq(view.is_drawn(prop), not victims.has(prop))
view.free()
func test_draw_calls_and_main_thread_cost_stay_bounded() -> void:
# Software rendering can't time the GPU. What can be checked here: the number of
# instanced draws is bounded by culling squares times kinds, every draw has a
# visibility range, building takes a loading-screen moment, and applying a burst of
# destruction costs well under a millisecond of the frame.
var started: int = Time.get_ticks_usec()
var view: PropView = PropView.new()
view.setup(_props)
var build_ms: int = (Time.get_ticks_usec() - started) / 1000
var squares: int = ceili(Tuning.WORLD_SIZE_METRES / PropView.CHUNK)
assert_true(view.node_count() <= squares * squares * PropSet.KINDS.size())
assert_true(view.node_count() < _props.count() / 10, "%d draws for %d props" % [view.node_count(), _props.count()])
for child: Node in view.get_children():
var instance: MultiMeshInstance3D = child
assert_true(instance.visibility_range_end > 0.0 and instance.visibility_range_end <= 600.0, instance.name)
var burst: int = 0
for prop: int in range(0, _props.count(), 60):
_props.damage(prop, 100000)
burst += 1
view.sync()
print(" prop view: %d props in %d draws, built in %d ms, %d-prop destruction sync %d us"
% [_props.count(), view.node_count(), build_ms, burst, view.last_sync_usec])
assert_true(build_ms < 4000, "setup took %d ms" % build_ms)
assert_true(view.last_sync_usec < 1000, "sync of %d felled props took %d us" % [burst, view.last_sync_usec])
view.free()
+1
View File
@@ -0,0 +1 @@
uid://cd4w660y6qjeg
+176
View File
@@ -0,0 +1,176 @@
extends TestCase
## Props from issue W5: deterministic placement, health, destruction and cost.
const SEEDS: Array[int] = [1, 2, 3, 4, 5, 6]
func test_client_and_server_place_identical_props() -> void:
# The server builds props inside Simulation; a client builds them from the seed alone.
for seed_value: int in [4, 77]:
var server: Simulation = Simulation.new(seed_value)
var island: Island = Island.generate(seed_value)
var client: PropSet = PropSet.generate(island, PoiSet.generate(island))
assert_eq(client.count(), server.props.count(), "seed %d count" % seed_value)
assert_eq(client.layout_hash(), server.props.layout_hash(), "seed %d layout" % seed_value)
var other: PropSet = PropSet.generate(Island.generate(5), PoiSet.generate(Island.generate(5)))
assert_ne(other.layout_hash(), Simulation.new(4).props.layout_hash(), "different seeds should differ")
func test_every_island_gets_thousands_of_props_of_every_kind() -> void:
for seed_value: int in SEEDS:
var island: Island = Island.generate(seed_value)
var props: PropSet = PropSet.generate(island, PoiSet.generate(island))
assert_true(props.count() >= 2000, "seed %d placed only %d props" % [seed_value, props.count()])
for kind: String in PropSet.KINDS:
assert_true(props.count_of(kind) >= 5, "seed %d has %d %s" % [seed_value, props.count_of(kind), kind])
for prop: int in props.count():
assert_eq(props.health[prop], Tuning.PROP_KINDS[props.kind_name(prop)]["health"])
func test_props_sit_on_land_and_keep_clear_of_points_of_interest() -> void:
for seed_value: int in [1, 2, 3]:
var sim: Simulation = Simulation.new(seed_value)
var props: PropSet = sim.props
for prop: int in props.count():
var box: AABB = props.boxes[prop]
var label: String = "seed %d %s at %s" % [seed_value, props.kind_name(prop), box]
var centre: Vector3 = box.get_center()
assert_true(sim.island.band_at(centre.x, centre.z) != Island.Band.SEA, label + " is in the sea")
for corner: Vector2 in [Vector2(box.position.x, box.position.z), Vector2(box.end.x, box.position.z),
Vector2(box.position.x, box.end.z), Vector2(box.end.x, box.end.z)]:
var floor_height: float = sim.ground.terrain_height(corner.x, corner.y)
assert_true(box.position.y <= floor_height + 0.001, label + " floats over a corner")
assert_true(box.end.y > floor_height + 0.3, label + " is buried")
for poi: Poi in sim.pois.pois:
for solid: AABB in poi.solids:
var grown: AABB = solid.grow(Tuning.PROP_POI_CLEARANCE - 0.01)
assert_false(grown.position.x < box.end.x and grown.end.x > box.position.x
and grown.position.z < box.end.z and grown.end.z > box.position.z, label + " crowds a " + poi.kind)
func test_props_block_players_until_destroyed() -> void:
var sim: Simulation = Simulation.new(3)
var props: PropSet = sim.props
var tree: int = _first_open(sim, "pine")
if tree < 0:
fail("no pine with open ground to its west")
return
var box: AABB = props.boxes[tree]
var start: Vector2 = Vector2(box.position.x - 6.0, box.get_center().z)
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(start.x, sim.ground.terrain_height(start.x, start.y), start.y)
# Walk due east (+x, yaw 90 degrees) into the trunk for three seconds.
_walk(sim, id, PI * 0.5, 90)
assert_true(player.position.x < box.position.x, "walked through a standing pine to %s" % player.position)
assert_true(player.position.x > box.position.x - Tuning.PLAYER_RADIUS - 0.3, "never reached the trunk, stopped at %s" % player.position)
var hits: int = 0
var full: int = props.health[tree]
while props.is_standing(tree):
var destroyed_now: bool = sim.damage_prop(tree, 45)
hits += 1
assert_eq(destroyed_now, not props.is_standing(tree))
if hits >= 100:
fail("pine never fell")
return
assert_eq(hits, ceili(float(full) / 45.0))
assert_eq(props.health[tree], 0)
assert_eq(props.destroyed, PackedInt32Array([tree]))
assert_false(sim.damage_prop(tree, 45), "a fallen pine can't fall again")
assert_eq(props.destroyed.size(), 1)
_walk(sim, id, PI * 0.5, 90)
assert_true(player.position.x > box.end.x + 1.0, "still blocked where the pine stood, at %s" % player.position)
func test_damage_is_part_of_the_state_hash_and_deterministic() -> void:
var a: Simulation = Simulation.new(8)
var b: Simulation = Simulation.new(8)
assert_eq(a.state_hash(), b.state_hash())
for prop: int in [3, 40, 41, 900]:
a.damage_prop(prop, 30)
b.damage_prop(prop, 30)
assert_eq(a.state_hash(), b.state_hash(), "same damage, same hash")
b.damage_prop(5, 1)
assert_ne(a.state_hash(), b.state_hash(), "a single point of prop damage must change the hash")
assert_false(a.damage_prop(-1, 10), "unknown props are ignored")
assert_false(a.damage_prop(a.props.count(), 10), "unknown props are ignored")
assert_false(a.damage_prop(7, 0), "zero damage does nothing")
assert_false(a.damage_prop(7, -5), "negative damage does nothing")
assert_eq(a.props.health[7], Tuning.PROP_KINDS[a.props.kind_name(7)]["health"])
func test_near_finds_standing_props_nearest_first() -> void:
var sim: Simulation = Simulation.new(2)
var props: PropSet = sim.props
var target: int = props.count() / 2
var at: Vector3 = props.boxes[target].get_center()
var found: PackedInt32Array = props.near(at, 3.0)
assert_eq(found[0], target)
while props.is_standing(target):
sim.damage_prop(target, 1000)
assert_false(props.near(at, 3.0).has(target), "destroyed props aren't near anything")
func test_simulation_with_props_stays_within_tick_budget() -> void:
# 24 players walking through the thickest forest: collision against thousands of
# boxes must stay cheap. Generous bound; the smoke run reports the real p95.
var sim: Simulation = Simulation.new(1)
var random: RandomNumberGenerator = RandomNumberGenerator.new()
random.seed = 1
var ids: Array[int] = []
var pines: PackedInt32Array = PackedInt32Array()
for prop: int in sim.props.count():
if sim.props.kind_name(prop) == "pine":
pines.append(prop)
for index: int in Tuning.SOLO_PLAYERS:
var id: int = sim.add_player()
var near: AABB = sim.props.boxes[pines[(index * 97) % pines.size()]]
var spot: Vector2 = Vector2(near.position.x - 1.5, near.position.z - 1.5)
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(spot.x, sim.ground.terrain_height(spot.x, spot.y), spot.y)
ids.append(id)
var worst: int = 0
var total: int = 0
var ticks: int = 300
for _tick: int in ticks:
for id: int in ids:
var move: Vector2 = Vector2(random.randf_range(-1.0, 1.0), 1.0)
sim.submit_command(id, PlayerCommand.from_input(sim.tick, move, random.randf() * TAU, 0.0, PlayerCommand.BUTTON_SPRINT))
var started: int = Time.get_ticks_usec()
sim.step()
var spent: int = Time.get_ticks_usec() - started
total += spent
worst = maxi(worst, spent)
print(" props: 24 players in forest, mean tick %d us, worst %d us" % [total / ticks, worst])
assert_true(total / ticks < 6000, "mean tick %d us" % (total / ticks))
func _walk(sim: Simulation, id: int, yaw: float, ticks: int) -> void:
for _tick: int in ticks:
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), yaw, 0.0, 0))
sim.step()
## The first standing prop of `kind` with 8 m of open, gentle ground to its west.
func _first_open(sim: Simulation, kind: String) -> int:
for prop: int in sim.props.count():
if sim.props.kind_name(prop) != kind:
continue
var box: AABB = sim.props.boxes[prop]
var z: float = box.get_center().z
var clear: bool = true
var x: float = box.position.x - 8.0
while x < box.end.x + 3.0 and clear:
# Skip the strip where a body would touch the trunk itself.
var reach: float = Tuning.PLAYER_RADIUS + 0.2
if x < box.position.x - reach or x > box.end.x + reach:
var feet: Vector3 = Vector3(x, sim.ground.terrain_height(x, z), z)
if sim.ground.body_blocked(feet, Tuning.PLAYER_RADIUS + 0.1, Tuning.PLAYER_HEIGHT, Tuning.STEP_HEIGHT) or not sim.ground.walkable_terrain(x, z):
clear = false
x += 0.25
var rise: float = absf(sim.ground.terrain_height(box.end.x + 3.0, z) - sim.ground.terrain_height(box.position.x - 8.0, z))
if clear and rise < 1.5:
return prop
return -1
+1
View File
@@ -0,0 +1 @@
uid://byhrm80nv3nm0
+18
View File
@@ -112,3 +112,21 @@ func test_index_gives_the_same_answers_as_a_full_scan() -> void:
if box.end.y <= 6.0 and x > box.position.x and x < box.end.x and z > box.position.z and z < box.end.z:
expected = maxf(expected, box.end.y)
assert_eq(ground.support_height(x, z, 6.0), expected)
func test_removed_solids_vanish_from_every_query() -> void:
var ground: SimGround = SimGround.flat()
var keep: int = ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 1.0, 2.0)))
var gone: int = ground.add_solid(AABB(Vector3(10.0, 0.0, 10.0), Vector3(20.0, 3.0, 2.0)))
assert_eq(gone, keep + 1)
assert_true(ground.body_blocked(Vector3(15.0, 0.0, 11.0), 0.4, 1.8, 0.0))
assert_true(ground.remove_solid(gone))
assert_false(ground.remove_solid(gone), "removing twice reports false")
assert_false(ground.remove_solid(99), "unknown IDs report false")
assert_false(ground.body_blocked(Vector3(15.0, 0.0, 11.0), 0.4, 1.8, 0.0))
assert_eq(ground.support_height(25.0, 11.0, 10.0), 0.0)
assert_eq(ground.raycast(Vector3(5.0, 1.0, 11.0), Vector3(35.0, 1.0, 11.0)), 1.0)
assert_false(ground.point_inside(Vector3(15.0, 1.0, 11.0)))
# The other solid keeps its ID and still collides, and new IDs never reuse old ones.
assert_eq(ground.support_height(1.0, 1.0, 5.0), 1.0)
assert_eq(ground.add_solid(AABB(Vector3(40.0, 0.0, 40.0), Vector3.ONE)), gone + 1)