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:
@@ -0,0 +1,262 @@
|
||||
## The island's scattered props: pines, scrub, boulders, wrecks and crates. Like the
|
||||
## island and its points of interest, both ends build the same set from the seed alone,
|
||||
## so props are never sent over the network. Only damage to them is match state.
|
||||
##
|
||||
## Placement walks a jittered grid of CELL-metre squares. Every square draws the same
|
||||
## number of random values whatever it ends up holding, so a change in one square's
|
||||
## outcome never shifts the next. A kind's chance in a square comes from its density in
|
||||
## the square's elevation band (Tuning.PROP_DENSITY), scaled by a grove noise for pines
|
||||
## and scrub, so forests have thickets and clearings.
|
||||
##
|
||||
## Each prop is one axis-aligned collision box in SimGround. Destroying a prop removes
|
||||
## its box, so players can walk where it stood. Props are stored in packed arrays,
|
||||
## because there are thousands of them.
|
||||
class_name PropSet
|
||||
extends RefCounted
|
||||
|
||||
## Side of the placement squares, in metres. At most one prop per square.
|
||||
const CELL: float = 4.0
|
||||
## Random values drawn per square: pick, jitter x, jitter z, size, height, length, turn.
|
||||
const DRAWS: int = 7
|
||||
## Kind codes, in the order kinds are tried within a square.
|
||||
const KINDS: Array[String] = ["pine", "scrub", "boulder", "wreck", "crate"]
|
||||
## Band names, indexed by Island.Band.
|
||||
const BAND_NAMES: Array[String] = ["sea", "beach", "marsh", "forest", "highland", "summit"]
|
||||
## Grove noise scale, in metres, and how strongly it thins or thickens growth.
|
||||
const GROVE_METRES: float = 70.0
|
||||
const GROVE_STRENGTH: float = 1.6
|
||||
## Lowest ground props stand on, in metres above the starting sea. Wrecks lie lower.
|
||||
const MIN_HEIGHT: float = 0.4
|
||||
const WRECK_MIN_HEIGHT: float = 0.1
|
||||
## Most ground rise across a footprint each kind accepts, in metres.
|
||||
const TILT_LIMITS: Dictionary = {"pine": 1.6, "boulder": 2.0, "wreck": 1.2, "scrub": 0.6, "crate": 0.6}
|
||||
## Boxes sink this far into the ground at their lowest corner, so none float on slopes.
|
||||
const SINK: float = 0.25
|
||||
|
||||
## Per prop, indexed by prop ID (0 .. count() - 1).
|
||||
var kinds: PackedByteArray = PackedByteArray()
|
||||
## Where the prop meets the ground, at its footprint centre.
|
||||
var positions: PackedVector3Array = PackedVector3Array()
|
||||
## Visual yaw in radians. Collision is the box in `boxes` whatever the yaw.
|
||||
var yaws: PackedFloat32Array = PackedFloat32Array()
|
||||
var boxes: Array[AABB] = []
|
||||
var health: PackedInt32Array = PackedInt32Array()
|
||||
## The SimGround solid ID of each prop's box, or -1 when it isn't in a ground.
|
||||
var solid_ids: PackedInt32Array = PackedInt32Array()
|
||||
## Prop IDs in the order they were destroyed, for views to catch up on.
|
||||
var destroyed: PackedInt32Array = PackedInt32Array()
|
||||
|
||||
var _ground: SimGround
|
||||
|
||||
|
||||
## Places props for `island`, clear of `placed` points of interest.
|
||||
static func generate(island: Island, placed: PoiSet) -> PropSet:
|
||||
var props: PropSet = PropSet.new()
|
||||
props._place(island, placed)
|
||||
return props
|
||||
|
||||
|
||||
func count() -> int:
|
||||
return kinds.size()
|
||||
|
||||
|
||||
func kind_name(prop: int) -> String:
|
||||
return KINDS[kinds[prop]]
|
||||
|
||||
|
||||
func material_of(prop: int) -> String:
|
||||
return Tuning.PROP_KINDS[kind_name(prop)]["material"]
|
||||
|
||||
|
||||
func is_standing(prop: int) -> bool:
|
||||
return health[prop] > 0
|
||||
|
||||
|
||||
func count_of(kind: String) -> int:
|
||||
var code: int = KINDS.find(kind)
|
||||
var total: int = 0
|
||||
for value: int in kinds:
|
||||
if value == code:
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
## Adds every prop's box to `ground` and remembers the solid IDs, so destroying a prop
|
||||
## removes its collision. Call once.
|
||||
func add_solids_to(ground: SimGround) -> void:
|
||||
_ground = ground
|
||||
solid_ids.resize(count())
|
||||
for prop: int in count():
|
||||
solid_ids[prop] = ground.add_solid(boxes[prop]) if is_standing(prop) else -1
|
||||
|
||||
|
||||
## Takes `amount` health from a standing prop. Returns true if this destroyed it, which
|
||||
## also removes its collision. Damage to an already destroyed prop does nothing.
|
||||
func damage(prop: int, amount: int) -> bool:
|
||||
if prop < 0 or prop >= count() or not is_standing(prop) or amount <= 0:
|
||||
return false
|
||||
health[prop] = maxi(health[prop] - amount, 0)
|
||||
if health[prop] > 0:
|
||||
return false
|
||||
destroyed.append(prop)
|
||||
if _ground != null and solid_ids[prop] >= 0:
|
||||
_ground.remove_solid(solid_ids[prop])
|
||||
solid_ids[prop] = -1
|
||||
return true
|
||||
|
||||
|
||||
## Standing props whose boxes come within `radius` of `point`, nearest first.
|
||||
func near(point: Vector3, radius: float) -> PackedInt32Array:
|
||||
var found: Array[Vector2] = []
|
||||
for prop: int in count():
|
||||
if not is_standing(prop):
|
||||
continue
|
||||
var gap: float = _distance_to_box(boxes[prop], point)
|
||||
if gap <= radius:
|
||||
found.append(Vector2(gap, prop))
|
||||
found.sort()
|
||||
var ids: PackedInt32Array = PackedInt32Array()
|
||||
for entry: Vector2 in found:
|
||||
ids.append(int(entry.y))
|
||||
return ids
|
||||
|
||||
|
||||
## SHA-256 hex digest of every prop's kind and box, millimetre-rounded. Two machines
|
||||
## that agree on this see the same props in the same places.
|
||||
func layout_hash() -> String:
|
||||
var buffer: StreamPeerBuffer = StreamPeerBuffer.new()
|
||||
buffer.big_endian = false
|
||||
buffer.put_32(count())
|
||||
buffer.put_data(kinds)
|
||||
for box: AABB in boxes:
|
||||
for value: float in [box.position.x, box.position.y, box.position.z, box.size.x, box.size.y, box.size.z]:
|
||||
buffer.put_32(roundi(value * 1000.0))
|
||||
for yaw: float in yaws:
|
||||
buffer.put_32(roundi(yaw * 1000.0))
|
||||
var hasher: HashingContext = HashingContext.new()
|
||||
hasher.start(HashingContext.HASH_SHA256)
|
||||
hasher.update(buffer.data_array)
|
||||
return hasher.finish().hex_encode()
|
||||
|
||||
|
||||
func _place(island: Island, placed: PoiSet) -> void:
|
||||
var ground: SimGround = island.ground
|
||||
var rng: RandomNumberGenerator = SimRng.new(island.match_seed).stream("props")
|
||||
var grove: FastNoiseLite = FastNoiseLite.new()
|
||||
grove.noise_type = FastNoiseLite.TYPE_SIMPLEX_SMOOTH
|
||||
grove.seed = rng.randi()
|
||||
grove.frequency = 1.0 / GROVE_METRES
|
||||
var blocked: SimGround = _poi_keep_out(placed)
|
||||
var mine: SimGround = SimGround.flat()
|
||||
var per_side: int = floori(Tuning.WORLD_SIZE_METRES / CELL)
|
||||
var cell_area_hectares: float = CELL * CELL / 10000.0
|
||||
var values: PackedFloat32Array = PackedFloat32Array()
|
||||
values.resize(DRAWS)
|
||||
for gz: int in per_side:
|
||||
for gx: int in per_side:
|
||||
for draw: int in DRAWS:
|
||||
values[draw] = rng.randf()
|
||||
var x: float = (gx + 0.15 + 0.7 * values[1]) * CELL
|
||||
var z: float = (gz + 0.15 + 0.7 * values[2]) * CELL
|
||||
var band: int = island.band_at(x, z)
|
||||
if band == Island.Band.SEA:
|
||||
continue
|
||||
var densities: Dictionary = Tuning.PROP_DENSITY[BAND_NAMES[band]]
|
||||
var growth: float = clampf(1.0 + grove.get_noise_2d(x, z) * GROVE_STRENGTH, 0.0, 2.2)
|
||||
var pick: float = values[0]
|
||||
var chosen: int = -1
|
||||
for code: int in KINDS.size():
|
||||
var kind: String = KINDS[code]
|
||||
var chance: float = float(densities.get(kind, 0.0)) * cell_area_hectares
|
||||
if kind == "pine" or kind == "scrub":
|
||||
chance *= growth
|
||||
if pick < chance:
|
||||
chosen = code
|
||||
break
|
||||
pick -= chance
|
||||
if chosen < 0:
|
||||
continue
|
||||
var box: AABB = _box_for(chosen, Vector2(x, z), values, ground)
|
||||
if box.size == Vector3.ZERO:
|
||||
continue
|
||||
if _overlaps(blocked, box, Tuning.PROP_POI_CLEARANCE) or _overlaps(mine, box, 0.3):
|
||||
continue
|
||||
mine.add_solid(box)
|
||||
kinds.append(chosen)
|
||||
positions.append(Vector3(x, ground.terrain_height(x, z), z))
|
||||
yaws.append(values[6] * TAU)
|
||||
boxes.append(box)
|
||||
health.append(Tuning.PROP_KINDS[KINDS[chosen]]["health"])
|
||||
|
||||
|
||||
## The prop's collision box, or an empty box if the spot doesn't suit the kind.
|
||||
func _box_for(code: int, at: Vector2, values: PackedFloat32Array, ground: SimGround) -> AABB:
|
||||
var kind: String = KINDS[code]
|
||||
var spec: Dictionary = Tuning.PROP_KINDS[kind]
|
||||
var width: float = lerpf(spec["width"][0], spec["width"][1], values[3])
|
||||
var height: float = lerpf(spec["height"][0], spec["height"][1], values[4])
|
||||
var size: Vector3 = Vector3(width, height, width)
|
||||
if kind == "wreck":
|
||||
var length: float = lerpf(spec["length"][0], spec["length"][1], values[5])
|
||||
# Wrecks lie along x or z, so the box stays axis-aligned.
|
||||
size = Vector3(length, height, width) if values[6] < 0.5 else Vector3(width, height, length)
|
||||
var half: Vector2 = Vector2(size.x, size.z) * 0.5
|
||||
var lowest: float = INF
|
||||
var highest: float = -INF
|
||||
for corner: Vector2 in [Vector2(-1, -1), Vector2(1, -1), Vector2(-1, 1), Vector2(1, 1), Vector2.ZERO]:
|
||||
var h: float = ground.terrain_height(at.x + corner.x * half.x, at.y + corner.y * half.y)
|
||||
lowest = minf(lowest, h)
|
||||
highest = maxf(highest, h)
|
||||
var floor_min: float = WRECK_MIN_HEIGHT if kind == "wreck" else MIN_HEIGHT
|
||||
if lowest < floor_min:
|
||||
return AABB()
|
||||
# Boulders and pines take slopes; the rest need somewhere flattish.
|
||||
var tilt: float = highest - lowest
|
||||
var max_tilt: float = TILT_LIMITS.get(kind, 0.6)
|
||||
if tilt > max_tilt:
|
||||
return AABB()
|
||||
var base: float = lowest - SINK
|
||||
# The box grows by the tilt, so its top stands `height - SINK` above the uphill
|
||||
# corner too. Boulders sink deeper on slopes, so the uphill side isn't a wall.
|
||||
if kind == "boulder":
|
||||
base -= tilt * 0.4
|
||||
size.y += tilt * 0.4
|
||||
size.y += tilt if kind != "pine" else 0.0
|
||||
return AABB(Vector3(at.x - half.x, base, at.y - half.y), size)
|
||||
|
||||
|
||||
## A ground holding every POI solid, plus small boxes at loot markers and along walking
|
||||
## routes, for keeping props out of the way.
|
||||
func _poi_keep_out(placed: PoiSet) -> SimGround:
|
||||
var keep_out: SimGround = SimGround.flat()
|
||||
for poi: Poi in placed.pois:
|
||||
for solid: AABB in poi.solids:
|
||||
keep_out.add_solid(solid)
|
||||
for at: Vector3 in poi.loot:
|
||||
keep_out.add_solid(AABB(at - Vector3(0.5, 0.0, 0.5), Vector3(1.0, 2.0, 1.0)))
|
||||
for route: Array in poi.routes:
|
||||
for index: int in range(1, route.size()):
|
||||
var a: Vector3 = route[index - 1]
|
||||
var b: Vector3 = route[index]
|
||||
var steps: int = maxi(ceili(a.distance_to(b) / 1.0), 1)
|
||||
for step: int in steps + 1:
|
||||
var at: Vector3 = a.lerp(b, float(step) / steps)
|
||||
keep_out.add_solid(AABB(at - Vector3(0.5, 0.0, 0.5), Vector3(1.0, 2.0, 1.0)))
|
||||
return keep_out
|
||||
|
||||
|
||||
## True if `box`, grown sideways by `margin`, overlaps any solid in `ground`. Height is
|
||||
## ignored: a prop under a pier deck still blocks the way to it.
|
||||
func _overlaps(ground: SimGround, box: AABB, margin: float) -> bool:
|
||||
var low: Vector2 = Vector2(box.position.x - margin, box.position.z - margin)
|
||||
var high: Vector2 = Vector2(box.end.x + margin, box.end.z + margin)
|
||||
for id: int in ground.solids_near(low.x, low.y, high.x, high.y):
|
||||
var other: AABB = ground.solids[id]
|
||||
if other.position.x < high.x and other.end.x > low.x and other.position.z < high.y and other.end.z > low.y:
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
static func _distance_to_box(box: AABB, point: Vector3) -> float:
|
||||
var closest: Vector3 = point.clamp(box.position, box.end)
|
||||
return closest.distance_to(point)
|
||||
@@ -0,0 +1 @@
|
||||
uid://mdvwhfhnp6nk
|
||||
+29
-1
@@ -13,6 +13,8 @@ const RAY_REFINE_STEPS: int = 10
|
||||
## Side of the squares the solid index buckets by, in metres.
|
||||
const INDEX_CELL: float = 8.0
|
||||
const _NO_SOLIDS: PackedInt32Array = []
|
||||
## What a removed solid's slot holds: an empty box far below anything in the world.
|
||||
const REMOVED: AABB = AABB(Vector3(0.0, -1.0e6, 0.0), Vector3.ZERO)
|
||||
const _MARGIN_OFFSETS: Array[Vector2] = [
|
||||
Vector2(1.0, 0.0), Vector2(-1.0, 0.0), Vector2(0.0, 1.0), Vector2(0.0, -1.0),
|
||||
Vector2(0.7071, 0.7071), Vector2(0.7071, -0.7071), Vector2(-0.7071, 0.7071), Vector2(-0.7071, -0.7071),
|
||||
@@ -50,7 +52,8 @@ static func from_heights(values: PackedFloat32Array, side: int, spacing: float)
|
||||
return ground
|
||||
|
||||
|
||||
func add_solid(box: AABB) -> void:
|
||||
## Adds a solid and returns its ID, which stays valid until remove_solid().
|
||||
func add_solid(box: AABB) -> int:
|
||||
var normalised: AABB = box.abs()
|
||||
var id: int = solids.size()
|
||||
solids.append(normalised)
|
||||
@@ -62,6 +65,31 @@ func add_solid(box: AABB) -> void:
|
||||
var bucket: PackedInt32Array = _index.get(key, PackedInt32Array())
|
||||
bucket.append(id)
|
||||
_index[key] = bucket
|
||||
return id
|
||||
|
||||
|
||||
## Removes a solid so no query sees it again. IDs are never reused: the slot in
|
||||
## `solids` becomes REMOVED, a box far below the world, so other IDs keep their meaning.
|
||||
## Returns false if the ID is unknown or already removed.
|
||||
func remove_solid(id: int) -> bool:
|
||||
if id < 0 or id >= solids.size() or solids[id] == REMOVED:
|
||||
return false
|
||||
var box: AABB = solids[id]
|
||||
var low: Vector2i = _cell_of(box.position.x, box.position.z)
|
||||
var high: Vector2i = _cell_of(box.end.x, box.end.z)
|
||||
for cz: int in range(low.y, high.y + 1):
|
||||
for cx: int in range(low.x, high.x + 1):
|
||||
var key: Vector2i = Vector2i(cx, cz)
|
||||
var bucket: PackedInt32Array = _index.get(key, PackedInt32Array())
|
||||
var at: int = bucket.find(id)
|
||||
if at >= 0:
|
||||
bucket.remove_at(at)
|
||||
if bucket.is_empty():
|
||||
_index.erase(key)
|
||||
else:
|
||||
_index[key] = bucket
|
||||
solids[id] = REMOVED
|
||||
return true
|
||||
|
||||
|
||||
## Indices of solids whose footprint may overlap the rectangle. May repeat an index when
|
||||
|
||||
@@ -18,6 +18,9 @@ var island: Island
|
||||
## The island's points of interest, whose solids are part of `ground`. Empty when a
|
||||
## test supplied its own ground.
|
||||
var pois: PoiSet = PoiSet.new()
|
||||
## Trees, scrub, boulders, wrecks and crates, whose boxes are part of `ground`. Empty
|
||||
## when a test supplied its own ground.
|
||||
var props: PropSet = PropSet.new()
|
||||
|
||||
## Player entity ID -> { command tick -> PlayerCommand }
|
||||
var _pending: Dictionary = {}
|
||||
@@ -34,6 +37,8 @@ func _init(match_seed: int, world: SimGround = null) -> void:
|
||||
world = island.ground
|
||||
pois = PoiSet.generate(island)
|
||||
pois.add_solids_to(world)
|
||||
props = PropSet.generate(island, pois)
|
||||
props.add_solids_to(world)
|
||||
ground = world
|
||||
|
||||
|
||||
@@ -92,6 +97,11 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
||||
Movement.step(player, command, ground)
|
||||
|
||||
|
||||
## Damages a prop. Returns true if this destroyed it; its collision goes at once.
|
||||
func damage_prop(prop: int, amount: int) -> bool:
|
||||
return props.damage(prop, amount)
|
||||
|
||||
|
||||
## SHA-256 hex digest of a canonical encoding of the whole state.
|
||||
func state_hash() -> String:
|
||||
var buffer: StreamPeerBuffer = StreamPeerBuffer.new()
|
||||
@@ -115,6 +125,9 @@ func state_hash() -> String:
|
||||
for value: float in [entity.velocity.x, entity.velocity.y, entity.velocity.z]:
|
||||
buffer.put_64(roundi(value * 1000.0))
|
||||
|
||||
buffer.put_64(props.count())
|
||||
buffer.put_data(props.health.to_byte_array())
|
||||
|
||||
var hasher: HashingContext = HashingContext.new()
|
||||
hasher.start(HashingContext.HASH_SHA256)
|
||||
hasher.update(buffer.data_array)
|
||||
|
||||
@@ -87,6 +87,27 @@ const POI_MAX_TOTAL: int = 16
|
||||
## No two points of interest are closer than this, centre to centre.
|
||||
const POI_SPACING_METRES: float = 85.0
|
||||
|
||||
# Props: trees, scrub, boulders, wrecks and crates scattered over the island. Each kind
|
||||
# has health, the material it gives when harvested, and a collision footprint range in
|
||||
# metres (`width` x `height`; wrecks are `length` long). Density is props per hectare
|
||||
# in each elevation band; groves and clearings modulate it.
|
||||
const PROP_KINDS: Dictionary = {
|
||||
"pine": {"health": 200, "material": "driftwood", "width": [0.5, 0.7], "height": [7.0, 12.0]},
|
||||
"scrub": {"health": 60, "material": "driftwood", "width": [1.1, 1.8], "height": [0.8, 1.3]},
|
||||
"boulder": {"health": 400, "material": "stone", "width": [1.6, 3.4], "height": [1.0, 2.4]},
|
||||
"wreck": {"health": 500, "material": "salvage_iron", "width": [2.2, 2.8], "height": [1.4, 2.0], "length": [5.5, 8.0]},
|
||||
"crate": {"health": 80, "material": "driftwood", "width": [0.9, 1.2], "height": [0.9, 1.2]},
|
||||
}
|
||||
const PROP_DENSITY: Dictionary = {
|
||||
"beach": {"wreck": 5.0, "crate": 6.0, "scrub": 2.0},
|
||||
"marsh": {"scrub": 30.0, "crate": 1.0, "boulder": 1.0},
|
||||
"forest": {"pine": 70.0, "scrub": 16.0, "boulder": 4.0, "crate": 0.3},
|
||||
"highland": {"pine": 14.0, "boulder": 16.0, "scrub": 10.0},
|
||||
"summit": {"boulder": 10.0, "scrub": 3.0},
|
||||
}
|
||||
## Props keep this far from any point-of-interest solid, loot marker or walking route.
|
||||
const PROP_POI_CLEARANCE: float = 3.0
|
||||
|
||||
# The tide. `hold` and `rise` are seconds, `level` is metres above the starting sea,
|
||||
# `drown_dps` is damage per second underwater once breath runs out. The last level
|
||||
# must exceed ISLAND_MAX_HEIGHT, so that every match ends.
|
||||
|
||||
Reference in New Issue
Block a user