diff --git a/docs/GDD.md b/docs/GDD.md index 6399bdd..7ca8130 100644 --- a/docs/GDD.md +++ b/docs/GDD.md @@ -66,6 +66,25 @@ seed alone, and compare `Island.heightmap_hash()`, so terrain is never sent. `tools/island_map.gd` draws a top-down map of any seed, with a contour at each tide level. +### Points of interest + +Every island gets 10 to 16 places worth landing at (`POI_KINDS`, `POI_MIN_TOTAL`, +`POI_MAX_TOTAL`), at least `POI_SPACING_METRES` apart, placed from the seed by +[`src/sim/poi_set.gd`](../src/sim/poi_set.gd): + +| Kind | Sited | Built from | +|---|---|---| +| Hilltop shrine | On a summit | Open-fronted stone shrine with a painted roof, cairns | +| Lighthouse | Near the coast, raised ground | Stone tower, keeper's room, stair to a loft, light chamber | +| Dock | On the shore, facing open water | Plank pier on posts reaching deep water, net shed | +| Salt works | Flat beach or marsh | Evaporation pans behind low walls, boiling house | +| Quarry | Highland | Cut stone blocks, lifting frame, tool shed | +| Fishing hamlet | Forest band | Three or four huts round a yard, net-drying rack | + +Each is assembled from modular parts (walls with doors, plinths, steps, roofs, stairs). +Loot spawn markers sit on their floors, for the loot system, and a test walks a player +from open ground into every interior. + ## Tide phases `TIDE_PHASES` is an ordered list of phases. Each one sets: diff --git a/src/client/poi/poi_view.gd b/src/client/poi/poi_view.gd new file mode 100644 index 0000000..9ea0b38 --- /dev/null +++ b/src/client/poi/poi_view.gd @@ -0,0 +1,54 @@ +## Draws an island's points of interest from their parts: one MultiMesh of boxes per +## look, so the whole set costs a handful of draw calls. Colours follow the weathered +## seafaring palette in docs/ORIGINALITY.md: tarred wood, grey planks, stone, faded red +## roofs, salt-faded paint and white salt. No textures; the art pass replaces these. +class_name PoiView +extends Node3D + +const LOOKS: Dictionary = { + Poi.LOOK_TARRED_WOOD: {"colour": Color(0.19, 0.16, 0.13), "roughness": 0.7}, + Poi.LOOK_PLANK: {"colour": Color(0.50, 0.45, 0.37), "roughness": 0.9}, + Poi.LOOK_STONE: {"colour": Color(0.56, 0.54, 0.49), "roughness": 0.95}, + Poi.LOOK_ROOF: {"colour": Color(0.56, 0.27, 0.20), "roughness": 0.85}, + Poi.LOOK_PAINT: {"colour": Color(0.33, 0.55, 0.56), "roughness": 0.8}, + Poi.LOOK_SALT: {"colour": Color(0.90, 0.90, 0.86), "roughness": 0.6}, + Poi.LOOK_LAMP: {"colour": Color(0.98, 0.86, 0.55), "roughness": 0.3, "glow": 1.6}, +} + + +func setup(placed: PoiSet) -> void: + var by_look: Dictionary = {} + for poi: Poi in placed.pois: + for part: Dictionary in poi.parts: + var look: String = part["look"] + if not by_look.has(look): + by_look[look] = [] + (by_look[look] as Array).append(part["box"]) + for look: String in by_look: + var boxes: Array = by_look[look] + var mesh: BoxMesh = BoxMesh.new() + mesh.size = Vector3.ONE + mesh.material = _material(look) + var multimesh: MultiMesh = MultiMesh.new() + multimesh.transform_format = MultiMesh.TRANSFORM_3D + multimesh.mesh = mesh + multimesh.instance_count = boxes.size() + for index: int in boxes.size(): + var box: AABB = boxes[index] + multimesh.set_instance_transform(index, Transform3D(Basis.from_scale(box.size), box.get_center())) + var instance: MultiMeshInstance3D = MultiMeshInstance3D.new() + instance.name = look + instance.multimesh = multimesh + add_child(instance) + + +func _material(look: String) -> StandardMaterial3D: + var spec: Dictionary = LOOKS.get(look, LOOKS[Poi.LOOK_PLANK]) + var material: StandardMaterial3D = StandardMaterial3D.new() + material.albedo_color = spec["colour"] + material.roughness = spec["roughness"] + if spec.has("glow"): + material.emission_enabled = true + material.emission = spec["colour"] + material.emission_energy_multiplier = spec["glow"] + return material diff --git a/src/client/poi/poi_view.gd.uid b/src/client/poi/poi_view.gd.uid new file mode 100644 index 0000000..0ea8dfc --- /dev/null +++ b/src/client/poi/poi_view.gd.uid @@ -0,0 +1 @@ +uid://dvntokn6rpcxs diff --git a/src/client/terrain/terrain.gdshader b/src/client/terrain/terrain.gdshader index 195e657..e65b8a6 100644 --- a/src/client/terrain/terrain.gdshader +++ b/src/client/terrain/terrain.gdshader @@ -10,7 +10,7 @@ uniform vec3 sand_colour : source_color = vec3(0.80, 0.71, 0.52); uniform vec3 marsh_colour : source_color = vec3(0.37, 0.39, 0.25); uniform vec3 grass_colour : source_color = vec3(0.31, 0.42, 0.22); uniform vec3 rock_colour : source_color = vec3(0.47, 0.44, 0.39); -uniform vec3 summit_colour : source_color = vec3(0.74, 0.72, 0.67); +uniform vec3 summit_colour : source_color = vec3(0.60, 0.58, 0.53); // Debug: tint each chunk by its level of detail (set per instance by TerrainView). instance uniform float lod_tint = -1.0; @@ -60,7 +60,7 @@ void fragment() { float strata = 0.5 + 0.5 * sin(world_position.y * 2.1 + broad * 5.0); vec3 rock = rock_colour * (0.82 + 0.22 * fine + 0.1 * strata); vec3 summit = summit_colour * (0.88 + 0.14 * fine); - rock = mix(rock, summit, smoothstep(summit_height - 4.0, summit_height + 4.0, world_position.y + 3.0 * broad)); + rock = mix(rock, summit, 0.8 * smoothstep(summit_height - 2.0, summit_height + 6.0, world_position.y + 3.0 * broad)); vec4 weights = max(COLOR, vec4(0.0)); float steep = 1.0 - smoothstep(max_slope_cos - 0.08, max_slope_cos + 0.12, world_normal.y); diff --git a/src/sim/movement.gd b/src/sim/movement.gd index bc4ba50..ea0d062 100644 --- a/src/sim/movement.gd +++ b/src/sim/movement.gd @@ -83,7 +83,13 @@ static func _move_horizontally(player: SimEntity, velocity: Vector3, ground: Sim static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround) -> bool: var step: float = Tuning.STEP_HEIGHT if player.on_ground else 0.0 - if ground.body_blocked(target, Tuning.PLAYER_RADIUS, body_height(player), step): + # On foot, the body is tested where it will stand: on whatever it steps up onto at + # the target. Otherwise the next riser of a stair narrower than the body would block + # a player who hasn't been lifted onto the current one yet. + var body: Vector3 = target + if player.on_ground: + body.y = maxf(target.y, ground.support_height(target.x, target.z, target.y + step)) + if ground.body_blocked(body, Tuning.PLAYER_RADIUS, body_height(player), step): return false # Moving uphill into terrain that's too steep is blocked, on foot or in the air, so # nobody can climb a cliff by pushing into it. Downhill is always allowed. diff --git a/src/sim/poi.gd b/src/sim/poi.gd new file mode 100644 index 0000000..6b70530 --- /dev/null +++ b/src/sim/poi.gd @@ -0,0 +1,174 @@ +## One placed point of interest: its solid parts, how each part looks, where loot can +## spawn, and walking routes that reach its interiors. +## +## Parts are built in a local frame (x right, z forward out of the front door, y up from +## `floor_y` at the origin) and turned in quarter turns, so every solid stays an +## axis-aligned box that SimGround can collide with. +class_name Poi +extends RefCounted + +## Part looks, for the renderer. The simulation only cares about the boxes. +const LOOK_TARRED_WOOD: String = "tarred_wood" +const LOOK_PLANK: String = "plank" +const LOOK_STONE: String = "stone" +const LOOK_ROOF: String = "roof" +const LOOK_PAINT: String = "paint" +const LOOK_SALT: String = "salt" +const LOOK_LAMP: String = "lamp" + +const WALL: float = 0.3 +const DOOR_WIDTH: float = 1.4 +const DOOR_HEIGHT: float = 2.2 +const STEP_RISE: float = 0.35 +const STEP_DEPTH: float = 0.6 + +var kind: String = "" +## World-space origin of the local frame, and its facing in quarter turns. +var origin: Vector3 = Vector3.ZERO +var facing: int = 0 +## Every solid box, in world space. +var solids: Array[AABB] = [] +## Render parts: {"box": AABB, "look": String}, in world space. +var parts: Array[Dictionary] = [] +## Loot spawn markers, in world space, standing on a floor. +var loot: Array[Vector3] = [] +## Walking routes, each a list of world points from open ground outside to somewhere +## inside. The traversal test walks every one. +var routes: Array[Array] = [] + +var _ground: SimGround + + +func _init(poi_kind: String, at: Vector3, quarter_turns: int, ground: SimGround) -> void: + kind = poi_kind + origin = at + facing = posmod(quarter_turns, 4) + _ground = ground + + +## Turns a local point into world space. +func to_world(local: Vector3) -> Vector3: + var x: float = local.x + var z: float = local.z + match facing: + 1: + var turned: float = x + x = z + z = -turned + 2: + x = -x + z = -z + 3: + var turned: float = x + x = -z + z = turned + return origin + Vector3(x, local.y, z) + + +## Adds a box given by its local minimum corner and size. +func box(local_min: Vector3, size: Vector3, look: String, solid: bool = true) -> AABB: + var a: Vector3 = to_world(local_min) + var b: Vector3 = to_world(local_min + size) + var world: AABB = AABB(a, b - a).abs() + parts.append({"box": world, "look": look}) + if solid: + solids.append(world) + return world + + +## Lowest and highest terrain under a local rectangle, sampled every metre. +func terrain_range(local_min: Vector2, local_size: Vector2) -> Vector2: + var low: float = INF + var high: float = -INF + var steps_x: int = maxi(ceili(local_size.x), 1) + var steps_z: int = maxi(ceili(local_size.y), 1) + for iz: int in steps_z + 1: + for ix: int in steps_x + 1: + var local: Vector3 = Vector3(local_min.x + local_size.x * ix / steps_x, 0.0, local_min.y + local_size.y * iz / steps_z) + var world: Vector3 = to_world(local) + var height: float = _ground.terrain_height(world.x, world.z) + low = minf(low, height) + high = maxf(high, height) + return Vector2(low, high) + + +## Terrain height under a local point. +func ground_at(local: Vector3) -> float: + var world: Vector3 = to_world(local) + return _ground.terrain_height(world.x, world.z) + + +## A plinth whose top is `top` (local height), filling down into the ground under it. +func plinth(local_min: Vector2, local_size: Vector2, top: float, look: String) -> void: + var under: Vector2 = terrain_range(local_min, local_size) + var bottom: float = minf(under.x - origin.y - 0.6, top - 0.3) + box(Vector3(local_min.x, bottom, local_min.y), Vector3(local_size.x, top - bottom, local_size.y), look) + + +## Steps leading down from a platform edge at local `from` (its height is the platform +## top), heading along local `direction` (a unit axis), `width` wide, until they meet the +## ground. Returns the local point on open ground at the foot of the steps. +func steps_down(from: Vector3, direction: Vector3, width: float, look: String) -> Vector3: + var top: float = from.y + var at: Vector3 = from + var side: Vector3 = Vector3(direction.z, 0.0, -direction.x).abs() * width + var guard: int = 0 + while top - (ground_at(at + direction * STEP_DEPTH) - origin.y) > STEP_RISE * 1.1 and guard < 40: + guard += 1 + top -= STEP_RISE + var corner_a: Vector3 = at - side * 0.5 + var corner_b: Vector3 = at + direction * STEP_DEPTH + side * 0.5 + var low: Vector3 = Vector3(minf(corner_a.x, corner_b.x), 0.0, minf(corner_a.z, corner_b.z)) + var size: Vector3 = Vector3(absf(corner_b.x - corner_a.x), 0.0, absf(corner_b.z - corner_a.z)) + var ground_low: float = minf(ground_at(at), ground_at(at + direction * STEP_DEPTH)) - origin.y - 0.5 + box(Vector3(low.x, ground_low, low.z), Vector3(size.x, top - ground_low, size.z), look) + at += direction * STEP_DEPTH + var foot: Vector3 = at + direction * 1.2 + foot.y = ground_at(foot) - origin.y + return foot + + +## A hut: four walls with a door in the front (+z) wall, a floor on a plinth, a roof, +## steps down from the door, one loot marker inside, and a route from outside to it. +## `centre` is local; the floor height is chosen from the ground under the footprint. +func hut(centre: Vector3, width: float, depth: float, height: float, wall_look: String, roof_look: String) -> void: + var min_corner: Vector2 = Vector2(centre.x - width * 0.5, centre.z - depth * 0.5) + var under: Vector2 = terrain_range(min_corner, Vector2(width, depth)) + var floor_y: float = under.y - origin.y + 0.15 + plinth(min_corner, Vector2(width, depth), floor_y, LOOK_STONE) + var x0: float = min_corner.x + var z0: float = min_corner.y + var top: float = floor_y + height + # Back, left and right walls. + box(Vector3(x0, floor_y, z0), Vector3(width, height, WALL), wall_look) + box(Vector3(x0, floor_y, z0), Vector3(WALL, height, depth), wall_look) + box(Vector3(x0 + width - WALL, floor_y, z0), Vector3(WALL, height, depth), wall_look) + # Front wall, either side of the door, and the lintel over it. + var front_z: float = z0 + depth - WALL + var side_width: float = (width - DOOR_WIDTH) * 0.5 + box(Vector3(x0, floor_y, front_z), Vector3(side_width, height, WALL), wall_look) + box(Vector3(x0 + side_width + DOOR_WIDTH, floor_y, front_z), Vector3(side_width, height, WALL), wall_look) + box(Vector3(x0 + side_width, floor_y + DOOR_HEIGHT, front_z), Vector3(DOOR_WIDTH, height - DOOR_HEIGHT, WALL), wall_look) + # Roof with an overhang, and a painted door frame for the folk-art look. + box(Vector3(x0 - 0.4, top, z0 - 0.4), Vector3(width + 0.8, 0.3, depth + 0.8), roof_look) + box(Vector3(x0 + side_width - 0.12, floor_y, z0 + depth), Vector3(0.12, DOOR_HEIGHT, 0.06), LOOK_PAINT, false) + box(Vector3(x0 + side_width + DOOR_WIDTH, floor_y, z0 + depth), Vector3(0.12, DOOR_HEIGHT, 0.06), LOOK_PAINT, false) + + var door: Vector3 = Vector3(centre.x, floor_y, z0 + depth) + var foot: Vector3 = steps_down(door, Vector3(0.0, 0.0, 1.0), DOOR_WIDTH + 0.4, LOOK_STONE) + var inside: Vector3 = Vector3(centre.x, floor_y, z0 + depth - 1.2) + var marker: Vector3 = Vector3(centre.x, floor_y, z0 + 1.2) + add_loot(marker) + add_route([foot + Vector3(0.0, 0.0, 1.5), foot, door, inside, marker]) + + +func add_loot(local: Vector3) -> void: + loot.append(to_world(local)) + + +## A route through local points, stored in world space. +func add_route(points: Array[Vector3]) -> void: + var world: Array = [] + for point: Vector3 in points: + world.append(to_world(point)) + routes.append(world) diff --git a/src/sim/poi.gd.uid b/src/sim/poi.gd.uid new file mode 100644 index 0000000..5cc294f --- /dev/null +++ b/src/sim/poi.gd.uid @@ -0,0 +1 @@ +uid://cany76y8w47r diff --git a/src/sim/poi_set.gd b/src/sim/poi_set.gd new file mode 100644 index 0000000..6f50d18 --- /dev/null +++ b/src/sim/poi_set.gd @@ -0,0 +1,378 @@ +## Places an island's points of interest from the match seed. Like the island itself, +## both ends build the same set from the seed alone. +## +## Kinds, all original to Tidewall: fishing hamlets, docks, lighthouses, a quarry, salt +## works and hilltop shrines. Each kind has a siting rule (a band of height, how flat, +## near the coast or a summit) and is assembled from Poi's modular parts. +class_name PoiSet +extends RefCounted + +## Kinds in placing order: the most particular sites are claimed first. +const ORDER: Array[String] = ["shrine", "lighthouse", "dock", "salt_works", "quarry", "hamlet"] +const CANDIDATES: int = 260 +const RELAX_STEPS: Array[float] = [1.0, 1.5, 2.2, 3.2] +const STRICT_ONLY: Array[float] = [1.0] +const PIER_DECK_MIN: float = 1.4 +const PIER_WATER_DEPTH: float = 1.5 +const PIER_PAST_WATER: float = 6.0 +const PIER_MAX_METRES: float = 36.0 +## Radius around a site whose terrain must lie within a kind's flatness, in metres. +const SITE_RADIUS: Dictionary = { + "shrine": 6.0, "lighthouse": 6.0, "dock": 5.0, "salt_works": 12.0, "quarry": 10.0, "hamlet": 14.0, +} + +var pois: Array[Poi] = [] + +var _island: Island +var _ground: SimGround + + +static func generate(island: Island) -> PoiSet: + var placed: PoiSet = PoiSet.new() + placed._island = island + placed._ground = island.ground + placed._place_all(SimRng.new(island.match_seed).stream("poi")) + return placed + + +## Every loot spawn marker, for the loot system: {"position": Vector3, "poi": index, +## "kind": String}, in placing order. +func loot_markers() -> Array[Dictionary]: + var markers: Array[Dictionary] = [] + for index: int in pois.size(): + for at: Vector3 in pois[index].loot: + markers.append({"position": at, "poi": index, "kind": pois[index].kind}) + return markers + + +## Adds every POI's solids to `ground`. +func add_solids_to(ground: SimGround) -> void: + for poi: Poi in pois: + for solid: AABB in poi.solids: + ground.add_solid(solid) + + +func count_of(kind: String) -> int: + var count: int = 0 + for poi: Poi in pois: + if poi.kind == kind: + count += 1 + return count + + +func _place_all(rng: RandomNumberGenerator) -> void: + var wanted: Dictionary = {} + var total: int = 0 + for kind: String in ORDER: + var limits: Dictionary = Tuning.POI_KINDS[kind] + wanted[kind] = rng.randi_range(limits["min"], limits["max"]) + total += wanted[kind] + while total > Tuning.POI_MAX_TOTAL: + for kind: String in ORDER: + if total > Tuning.POI_MAX_TOTAL and wanted[kind] > Tuning.POI_KINDS[kind]["min"]: + wanted[kind] -= 1 + total -= 1 + for kind: String in ORDER: + for index: int in wanted[kind]: + # Every kind's minimum must be placed, so loosen its siting rules step by step + # when the island is awkward. Extras only go on good sites. + var steps: Array[float] = RELAX_STEPS if index < Tuning.POI_KINDS[kind]["min"] else STRICT_ONLY + for relax: float in steps: + if _place_one(kind, rng, relax): + break + # If a strict siting rule left the island short, relax and top up with hamlets. + var relax: float = 1.5 + while pois.size() < Tuning.POI_MIN_TOTAL and relax < 4.0: + _place_one("hamlet", rng, relax) + relax += 0.25 + + +## Tries candidate sites for `kind` and builds the best. `relax` widens the siting rules. +func _place_one(kind: String, rng: RandomNumberGenerator, relax: float) -> bool: + var best: Vector2 = Vector2.ZERO + var best_score: float = INF + var size: float = Tuning.WORLD_SIZE_METRES + for _attempt: int in CANDIDATES: + var site: Vector2 + if kind == "shrine" and count_of("shrine") < _island.summits.size(): + var summit: Vector3 = _island.summits[count_of("shrine")] + site = Vector2(summit.x, summit.z) + Vector2(rng.randf_range(-25.0, 25.0), rng.randf_range(-25.0, 25.0)) + else: + site = Vector2(rng.randf_range(0.08, 0.92) * size, rng.randf_range(0.08, 0.92) * size) + var score: float = _site_score(kind, site, relax) + if score < best_score: + best_score = score + best = site + if best_score == INF: + return false + var height: float = _ground.terrain_height(best.x, best.y) + var poi: Poi = Poi.new(kind, Vector3(best.x, height, best.y), _facing_for(kind, best, rng), _ground) + call("_build_" + kind, poi, rng) + pois.append(poi) + return true + + +## Lower is better; INF means unsuitable. +func _site_score(kind: String, site: Vector2, relax: float) -> float: + for poi: Poi in pois: + if Vector2(poi.origin.x, poi.origin.z).distance_to(site) < Tuning.POI_SPACING_METRES: + return INF + var height: float = _ground.terrain_height(site.x, site.y) + var spread: float = _spread(site, SITE_RADIUS[kind]) + match kind: + "shrine": + if height < Tuning.ISLAND_FOREST_TOP or spread > 4.0 * relax: + return INF + return spread - height * 0.2 + "lighthouse": + var sea: float = _distance_to_sea(site, 80.0) + if height < 6.0 or height > 30.0 * relax or sea > 60.0 * relax or spread > 3.0 * relax: + return INF + return spread + sea * 0.05 - height * 0.05 + "dock": + if height < 0.4 or height > 2.4 * relax or spread > 2.5 * relax: + return INF + var turn: int = _seaward(site) + if turn < 0 or _pier_length(site, turn) == 0.0: + return INF + return spread + _pier_length(site, turn) * 0.05 + "salt_works": + if height < Tuning.ISLAND_BEACH_TOP * 0.5 or height > Tuning.ISLAND_MARSH_TOP * relax or spread > 2.0 * relax: + return INF + return spread + "quarry": + if height < Tuning.ISLAND_FOREST_TOP - 6.0 or height > Tuning.ISLAND_SUMMIT_BAND * relax or spread > 4.0 * relax: + return INF + return spread + _: + if height < Tuning.ISLAND_MARSH_TOP or height > Tuning.ISLAND_FOREST_TOP * relax or spread > 2.5 * relax: + return INF + return spread + return INF + + +## Highest minus lowest terrain within `radius` of `site`. +func _spread(site: Vector2, radius: float) -> float: + var low: float = INF + var high: float = -INF + for ring: float in [0.0, radius * 0.5, radius]: + for index: int in 8: + var angle: float = TAU * index / 8.0 + var at: Vector2 = site + Vector2(cos(angle), sin(angle)) * ring + var height: float = _ground.terrain_height(at.x, at.y) + low = minf(low, height) + high = maxf(high, height) + return high - low + + +func _distance_to_sea(site: Vector2, limit: float) -> float: + for distance: float in range(0, int(limit) + 1, 5): + for index: int in 8: + var angle: float = TAU * index / 8.0 + var at: Vector2 = site + Vector2(cos(angle), sin(angle)) * distance + if _ground.terrain_height(at.x, at.y) <= 0.0: + return distance + return INF + + +## The quarter turn whose front faces open sea within 30 m, or -1. +func _seaward(site: Vector2) -> int: + var directions: Array[Vector2] = [Vector2(0.0, 1.0), Vector2(1.0, 0.0), Vector2(0.0, -1.0), Vector2(-1.0, 0.0)] + var best: int = -1 + var deepest: float = -0.5 + for turn: int in 4: + var far: Vector2 = site + directions[turn] * 30.0 + var depth: float = _ground.terrain_height(far.x, far.y) + if depth < deepest: + deepest = depth + best = turn + return best + + +## Deck height for a pier at `site`, in world metres: a little above the shore, and +## always clear of the starting sea. +func _deck_top(site: Vector2) -> float: + return maxf(_ground.terrain_height(site.x, site.y) + 0.3, PIER_DECK_MIN) + + +## How long a pier from `site` facing quarter turn `turn` must be to reach water at +## least PIER_WATER_DEPTH deep, plus PIER_PAST_WATER. 0 if it can't: the sea is too far +## away, or the ground rises under the deck on the way. +func _pier_length(site: Vector2, turn: int) -> float: + var directions: Array[Vector2] = [Vector2(0.0, 1.0), Vector2(1.0, 0.0), Vector2(0.0, -1.0), Vector2(-1.0, 0.0)] + var deck: float = _deck_top(site) + for distance: float in range(4, int(PIER_MAX_METRES) + 1, 2): + var at: Vector2 = site + directions[turn] * distance + var height: float = _ground.terrain_height(at.x, at.y) + if height > deck - 0.4: + return 0.0 + if height < -PIER_WATER_DEPTH: + return minf(distance + PIER_PAST_WATER, PIER_MAX_METRES) + return 0.0 + + +func _facing_for(kind: String, site: Vector2, rng: RandomNumberGenerator) -> int: + if kind == "dock": + return _seaward(site) + return rng.randi_range(0, 3) + + +func _build_hamlet(poi: Poi, rng: RandomNumberGenerator) -> void: + # Huts around a yard, doors facing in, and a net-drying rack in the middle. + var huts: int = rng.randi_range(3, 4) + var spots: Array[Vector3] = [Vector3(0.0, 0.0, -9.0), Vector3(-9.0, 0.0, 0.0), Vector3(9.0, 0.0, 0.0), Vector3(0.0, 0.0, 9.0)] + for index: int in huts: + var hut: Poi = _child(poi, spots[index], index) + hut.hut(Vector3.ZERO, rng.randf_range(5.0, 6.5), rng.randf_range(4.5, 5.5), 2.8, Poi.LOOK_TARRED_WOOD, Poi.LOOK_ROOF) + _adopt(poi, hut) + for x: float in [-2.0, 2.0]: + poi.box(Vector3(x - 0.1, -0.5, -0.1), Vector3(0.2, 2.6, 0.2), Poi.LOOK_PLANK) + poi.box(Vector3(-2.2, 2.0, -0.08), Vector3(4.4, 0.12, 0.16), Poi.LOOK_PLANK) + poi.add_loot(Vector3(0.0, poi.ground_at(Vector3(0.0, 0.0, 1.5)) - poi.origin.y, 1.5)) + + +func _build_dock(poi: Poi, _rng: RandomNumberGenerator) -> void: + # A plank pier running out to sea on posts, with a net shed on the shore behind it. + var site: Vector2 = Vector2(poi.origin.x, poi.origin.z) + var deck_top: float = _deck_top(site) - poi.origin.y + var length: float = _pier_length(site, poi.facing) + poi.box(Vector3(-1.5, deck_top - 0.25, 0.0), Vector3(3.0, 0.25, length), Poi.LOOK_PLANK) + for z: float in range(2, int(length), 4): + for x: float in [-1.4, 1.2]: + poi.box(Vector3(x, -Tuning.ISLAND_SEA_DEPTH - poi.origin.y, z), Vector3(0.2, deck_top + Tuning.ISLAND_SEA_DEPTH + poi.origin.y + 0.9, 0.2), Poi.LOOK_TARRED_WOOD, false) + poi.box(Vector3(-1.6, deck_top, length - 0.2), Vector3(3.2, 0.9, 0.2), Poi.LOOK_PAINT) + var shed: Poi = _child(poi, Vector3(0.0, 0.0, -8.0), 0) + shed.hut(Vector3.ZERO, 5.0, 4.5, 2.6, Poi.LOOK_TARRED_WOOD, Poi.LOOK_ROOF) + _adopt(poi, shed) + # Steps down from the landward end of the deck to the shore. + var landing: Vector3 = poi.steps_down(Vector3(0.0, deck_top, 0.0), Vector3(0.0, 0.0, -1.0), 2.4, Poi.LOOK_PLANK) + var on_deck: Vector3 = Vector3(0.0, deck_top, 1.0) + var end: Vector3 = Vector3(0.0, deck_top, length - 1.5) + poi.add_loot(end) + poi.add_route([landing, on_deck, end]) + + +func _build_lighthouse(poi: Poi, _rng: RandomNumberGenerator) -> void: + # A square stone tower: a keeper's room, a stair along the back wall up to a loft, + # and the light chamber above. The loot is in the loft. + var side: float = 7.0 + var half: float = side * 0.5 + var under: Vector2 = poi.terrain_range(Vector2(-half, -half), Vector2(side, side)) + var floor_y: float = under.y - poi.origin.y + 0.15 + poi.plinth(Vector2(-half, -half), Vector2(side, side), floor_y, Poi.LOOK_STONE) + var loft: float = floor_y + 3.5 + var top: float = floor_y + 13.0 + var inner: float = side - 2.0 * Poi.WALL + var x0: float = -half + var z0: float = -half + poi.box(Vector3(x0, floor_y, z0), Vector3(side, top - floor_y, Poi.WALL), Poi.LOOK_STONE) + poi.box(Vector3(x0, floor_y, z0), Vector3(Poi.WALL, top - floor_y, side), Poi.LOOK_STONE) + poi.box(Vector3(half - Poi.WALL, floor_y, z0), Vector3(Poi.WALL, top - floor_y, side), Poi.LOOK_STONE) + var side_width: float = (side - Poi.DOOR_WIDTH) * 0.5 + poi.box(Vector3(x0, floor_y, half - Poi.WALL), Vector3(side_width, top - floor_y, Poi.WALL), Poi.LOOK_STONE) + poi.box(Vector3(x0 + side_width + Poi.DOOR_WIDTH, floor_y, half - Poi.WALL), Vector3(side_width, top - floor_y, Poi.WALL), Poi.LOOK_STONE) + poi.box(Vector3(x0 + side_width, floor_y + Poi.DOOR_HEIGHT, half - Poi.WALL), Vector3(Poi.DOOR_WIDTH, top - floor_y - Poi.DOOR_HEIGHT, Poi.WALL), Poi.LOOK_STONE) + # Painted bands, the light chamber and its cap. + for band: float in [4.5, 8.5]: + poi.box(Vector3(x0 - 0.05, floor_y + band, z0 - 0.05), Vector3(side + 0.1, 1.2, side + 0.1), Poi.LOOK_PAINT, false) + poi.box(Vector3(-2.0, top, -2.0), Vector3(4.0, 2.4, 4.0), Poi.LOOK_LAMP) + poi.box(Vector3(-2.5, top + 2.4, -2.5), Vector3(5.0, 0.4, 5.0), Poi.LOOK_ROOF) + # Stair along the back wall, rising towards -x, ten steps up to the loft. + var stair_width: float = 1.1 + var steps: int = ceili((loft - floor_y) / Poi.STEP_RISE) + var rise: float = (loft - floor_y) / steps + var tread: float = 0.5 + var stair_z: float = z0 + Poi.WALL + var start_x: float = half - Poi.WALL - 1.0 + for index: int in steps: + var step_top: float = floor_y + rise * (index + 1) + var x: float = start_x - tread * (index + 1) + poi.box(Vector3(x, floor_y, stair_z), Vector3(tread, step_top - floor_y, stair_width), Poi.LOOK_PLANK) + var stair_end_x: float = start_x - tread * steps + # The loft covers the room except the stair well above the flight. + var well_end: float = start_x + 0.2 + poi.box(Vector3(x0 + Poi.WALL, loft - 0.3, stair_z + stair_width), Vector3(inner, 0.3, inner - stair_width), Poi.LOOK_PLANK) + poi.box(Vector3(x0 + Poi.WALL, loft - 0.3, stair_z), Vector3(stair_end_x - x0 - Poi.WALL, 0.3, stair_width), Poi.LOOK_PLANK) + poi.box(Vector3(well_end, loft - 0.3, stair_z), Vector3(half - Poi.WALL - well_end, 0.3, stair_width), Poi.LOOK_PLANK) + var door: Vector3 = Vector3(0.0, floor_y, half) + var foot: Vector3 = poi.steps_down(door, Vector3(0.0, 0.0, 1.0), Poi.DOOR_WIDTH + 0.4, Poi.LOOK_STONE) + var room: Vector3 = Vector3(0.0, floor_y, half - 1.5) + var stair_foot: Vector3 = Vector3(start_x + 0.4, floor_y, stair_z + stair_width * 0.5) + var stair_top: Vector3 = Vector3(stair_end_x + 0.25, loft, stair_z + stair_width * 0.5) + var loft_spot: Vector3 = Vector3(stair_end_x + 0.6, loft, z0 + Poi.WALL + stair_width + 1.6) + poi.add_loot(Vector3(1.5, floor_y, half - 2.0)) + poi.add_loot(loft_spot) + poi.add_route([foot + Vector3(0.0, 0.0, 1.5), foot, door, room, stair_foot, stair_top, loft_spot]) + + +func _build_quarry(poi: Poi, rng: RandomNumberGenerator) -> void: + # Cut stone blocks, a lifting frame and a tool shed. + var shed: Poi = _child(poi, Vector3(0.0, 0.0, -6.0), rng.randi_range(0, 3)) + shed.hut(Vector3.ZERO, 5.0, 4.0, 2.6, Poi.LOOK_PLANK, Poi.LOOK_ROOF) + _adopt(poi, shed) + for index: int in rng.randi_range(5, 8): + var at: Vector3 = Vector3(rng.randf_range(-9.0, 9.0), 0.0, rng.randf_range(1.0, 10.0)) + var size: Vector3 = Vector3(rng.randf_range(1.2, 2.4), rng.randf_range(0.8, 1.8), rng.randf_range(1.2, 2.4)) + var base: float = poi.ground_at(at) - poi.origin.y - 0.3 + poi.box(Vector3(at.x, base, at.z), size + Vector3(0.0, 0.3, 0.0), Poi.LOOK_STONE) + var frame_z: float = 5.0 + for x: float in [-3.0, 3.0]: + var base: float = poi.ground_at(Vector3(x, 0.0, frame_z)) - poi.origin.y - 0.3 + poi.box(Vector3(x - 0.15, base, frame_z - 0.15), Vector3(0.3, 5.3, 0.3), Poi.LOOK_TARRED_WOOD) + poi.box(Vector3(-3.3, poi.ground_at(Vector3(0.0, 0.0, frame_z)) - poi.origin.y + 4.8, frame_z - 0.2), Vector3(6.6, 0.4, 0.4), Poi.LOOK_TARRED_WOOD) + poi.add_loot(Vector3(0.0, poi.ground_at(Vector3(0.0, 0.0, 12.0)) - poi.origin.y, 12.0)) + + +func _build_salt_works(poi: Poi, rng: RandomNumberGenerator) -> void: + # Shallow evaporation pans behind low walls you can step over, and a boiling house. + var house: Poi = _child(poi, Vector3(0.0, 0.0, -9.0), 0) + house.hut(Vector3.ZERO, 8.0, 6.0, 3.2, Poi.LOOK_TARRED_WOOD, Poi.LOOK_ROOF) + _adopt(poi, house) + poi.box(Vector3(2.4, house.origin.y - poi.origin.y - 0.5, -12.8), Vector3(0.8, 6.5, 0.8), Poi.LOOK_STONE) + for row: int in 2: + for column: int in 3: + var x0: float = -10.5 + column * 7.0 + var z0: float = 1.0 + row * 6.0 + var base: float = poi.ground_at(Vector3(x0 + 3.0, 0.0, z0 + 2.5)) - poi.origin.y + poi.box(Vector3(x0, base - 0.3, z0), Vector3(6.0, 0.35, 5.0), Poi.LOOK_SALT, false) + for wall: Array in [[Vector3(x0, 0, z0), Vector3(6.0, 0, 0.25)], [Vector3(x0, 0, z0 + 4.75), Vector3(6.0, 0, 0.25)], + [Vector3(x0, 0, z0), Vector3(0.25, 0, 5.0)], [Vector3(x0 + 5.75, 0, z0), Vector3(0.25, 0, 5.0)]]: + var at: Vector3 = wall[0] + var size: Vector3 = wall[1] + poi.box(Vector3(at.x, base - 0.3, at.z), Vector3(size.x, 0.6, size.z), Poi.LOOK_STONE) + poi.add_loot(Vector3(-7.5, poi.ground_at(Vector3(-7.5, 0.0, 3.5)) - poi.origin.y, 3.5)) + if rng.randf() < 0.5: + poi.add_loot(Vector3(6.5, poi.ground_at(Vector3(6.5, 0.0, 9.5)) - poi.origin.y, 9.5)) + + +func _build_shrine(poi: Poi, rng: RandomNumberGenerator) -> void: + # A small open-fronted stone shrine with a painted lintel, and cairns around it. + poi.hut(Vector3.ZERO, 4.0, 3.6, 2.6, Poi.LOOK_STONE, Poi.LOOK_PAINT) + for index: int in rng.randi_range(3, 5): + var angle: float = TAU * index / 5.0 + rng.randf() * 0.4 + var at: Vector3 = Vector3(cos(angle), 0.0, sin(angle)) * rng.randf_range(6.0, 9.0) + if at.z > 2.0 and absf(at.x) < 2.5: + continue # Keep the approach to the door clear. + var base: float = poi.ground_at(at) - poi.origin.y - 0.2 + for layer: int in 3: + var size: float = 1.2 - layer * 0.3 + poi.box(Vector3(at.x - size * 0.5, base + layer * 0.5, at.z - size * 0.5), Vector3(size, 0.5, size), Poi.LOOK_STONE) + + +## A sub-part with its own local frame at `offset` in the parent's frame, turned by a +## further `turns` quarter turns. +func _child(parent: Poi, offset: Vector3, turns: int) -> Poi: + var at: Vector3 = parent.to_world(offset) + at.y = _ground.terrain_height(at.x, at.z) + var facing: int = parent.facing + turns + if parent.kind == "hamlet": + # Spots around the yard: back, left, right, front. Turn each door to the yard. + facing = parent.facing + [0, 1, 3, 2][turns] + return Poi.new(parent.kind, at, facing, _ground) + + +func _adopt(parent: Poi, child: Poi) -> void: + parent.solids.append_array(child.solids) + parent.parts.append_array(child.parts) + parent.loot.append_array(child.loot) + parent.routes.append_array(child.routes) diff --git a/src/sim/poi_set.gd.uid b/src/sim/poi_set.gd.uid new file mode 100644 index 0000000..12cd711 --- /dev/null +++ b/src/sim/poi_set.gd.uid @@ -0,0 +1 @@ +uid://dbhv43jwkephj diff --git a/src/sim/sim_ground.gd b/src/sim/sim_ground.gd index 751a160..cdaeefe 100644 --- a/src/sim/sim_ground.gd +++ b/src/sim/sim_ground.gd @@ -10,6 +10,9 @@ extends RefCounted ## Ray-marching step for terrain, in metres. Small enough for camera-length rays. const RAY_STEP: float = 0.05 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 = [] 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), @@ -20,8 +23,13 @@ const _MARGIN_OFFSETS: Array[Vector2] = [ var heights: PackedFloat32Array = PackedFloat32Array() var samples: int = 0 var cell: float = 1.0 +## Every solid, in the order added. Add them with add_solid(), which indexes them. var solids: Array[AABB] = [] +## Solids indexed by the INDEX_CELL squares their footprints overlap, so queries only +## test nearby boxes. Vector2i -> PackedInt32Array of indices into `solids`. +var _index: Dictionary = {} + ## A flat world at `height`. static func flat(height: float = 0.0) -> SimGround: @@ -43,7 +51,37 @@ static func from_heights(values: PackedFloat32Array, side: int, spacing: float) func add_solid(box: AABB) -> void: - solids.append(box.abs()) + var normalised: AABB = box.abs() + var id: int = solids.size() + solids.append(normalised) + var low: Vector2i = _cell_of(normalised.position.x, normalised.position.z) + var high: Vector2i = _cell_of(normalised.end.x, normalised.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()) + bucket.append(id) + _index[key] = bucket + + +## Indices of solids whose footprint may overlap the rectangle. May repeat an index when +## the rectangle spans several cells, which every query tolerates. +func solids_near(min_x: float, min_z: float, max_x: float, max_z: float) -> PackedInt32Array: + if _index.is_empty(): + return _NO_SOLIDS + var low: Vector2i = _cell_of(min_x, min_z) + var high: Vector2i = _cell_of(max_x, max_z) + if low == high: + return _index.get(low, _NO_SOLIDS) + var found: PackedInt32Array = PackedInt32Array() + for cz: int in range(low.y, high.y + 1): + for cx: int in range(low.x, high.x + 1): + found.append_array(_index.get(Vector2i(cx, cz), _NO_SOLIDS)) + return found + + +func _cell_of(x: float, z: float) -> Vector2i: + return Vector2i(floori(x / INDEX_CELL), floori(z / INDEX_CELL)) ## Terrain height at (x, z), bilinearly interpolated. @@ -80,7 +118,8 @@ func walkable_terrain(x: float, z: float) -> bool: ## top of a solid. Solid tops are always walkable. func support_height(x: float, z: float, max_y: float) -> float: var best: float = terrain_height(x, z) - for box: AABB in solids: + for id: int in solids_near(x, z, x, z): + var box: AABB = solids[id] var top: float = box.end.y if top > best and top <= max_y and _inside_footprint(box, x, z, 0.0): best = top @@ -90,7 +129,8 @@ func support_height(x: float, z: float, max_y: float) -> float: ## True when the best support under (x, z) up to `max_y` is the top of a solid. func support_is_solid(x: float, z: float, max_y: float) -> bool: var terrain: float = terrain_height(x, z) - for box: AABB in solids: + for id: int in solids_near(x, z, x, z): + var box: AABB = solids[id] var top: float = box.end.y if top > terrain and top <= max_y and _inside_footprint(box, x, z, 0.0): return true @@ -100,7 +140,8 @@ func support_is_solid(x: float, z: float, max_y: float) -> bool: ## True if a player body at `feet` overlaps a solid. Solids whose top is within ## `step` of the feet don't count, so they can be stepped onto. func body_blocked(feet: Vector3, radius: float, height: float, step: float) -> bool: - for box: AABB in solids: + for id: int in solids_near(feet.x - radius, feet.z - radius, feet.x + radius, feet.z + radius): + var box: AABB = solids[id] if box.end.y <= feet.y + step or box.position.y >= feet.y + height: continue if _inside_footprint(box, feet.x, feet.z, radius): @@ -112,8 +153,8 @@ func body_blocked(feet: Vector3, radius: float, height: float, step: float) -> b ## to the first hit, or 1.0 if nothing is hit. A ray starting inside something returns 0. func raycast(from: Vector3, to: Vector3) -> float: var nearest: float = 1.0 - for box: AABB in solids: - nearest = minf(nearest, _ray_box(from, to, box)) + for id: int in solids_near(minf(from.x, to.x), minf(from.z, to.z), maxf(from.x, to.x), maxf(from.z, to.z)): + nearest = minf(nearest, _ray_box(from, to, solids[id])) return minf(nearest, _ray_terrain(from, to, nearest)) @@ -128,8 +169,8 @@ func point_inside(point: Vector3, margin: float = 0.0) -> bool: var probe: Vector2 = offset * margin if point.y < terrain_height(point.x + probe.x, point.z + probe.y): return true - for box: AABB in solids: - if box.grow(margin).has_point(point): + for id: int in solids_near(point.x - margin, point.z - margin, point.x + margin, point.z + margin): + if solids[id].grow(margin).has_point(point): return true return false diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index 0f9c296..5ed2982 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -15,6 +15,9 @@ var rng: SimRng var ground: SimGround ## The generated island, or null when a test supplied its own ground. 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() ## Player entity ID -> { command tick -> PlayerCommand } var _pending: Dictionary = {} @@ -29,6 +32,8 @@ func _init(match_seed: int, world: SimGround = null) -> void: if world == null: island = Island.generate(match_seed) world = island.ground + pois = PoiSet.generate(island) + pois.add_solids_to(world) ground = world @@ -41,7 +46,8 @@ func add_player() -> int: var z: float = spawn.randf() * size # Until the drop arrives (W7), spawn on dry land above the beach when there is any. for _attempt: int in SPAWN_ATTEMPTS: - if ground.terrain_height(x, z) > Tuning.ISLAND_BEACH_TOP: + var feet: Vector3 = Vector3(x, ground.terrain_height(x, z), z) + if feet.y > Tuning.ISLAND_BEACH_TOP and not ground.body_blocked(feet, 2.0, Tuning.PLAYER_HEIGHT, 0.0): break x = spawn.randf() * size z = spawn.randf() * size diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index b9b3387..20756a8 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -72,6 +72,21 @@ const ISLAND_MARSH_TOP: float = 7.0 const ISLAND_FOREST_TOP: float = 32.0 const ISLAND_SUMMIT_BAND: float = 50.0 +# Points of interest. Each kind places between its min and max on every island, and the +# total always lands between POI_MIN_TOTAL and POI_MAX_TOTAL. +const POI_KINDS: Dictionary = { + "shrine": {"min": 2, "max": 3}, + "lighthouse": {"min": 1, "max": 2}, + "dock": {"min": 2, "max": 3}, + "salt_works": {"min": 1, "max": 2}, + "quarry": {"min": 1, "max": 2}, + "hamlet": {"min": 3, "max": 4}, +} +const POI_MIN_TOTAL: int = 10 +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 + # 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. diff --git a/test/capture/poi_dock.tscn b/test/capture/poi_dock.tscn new file mode 100644 index 0000000..6ca5be6 --- /dev/null +++ b/test/capture/poi_dock.tscn @@ -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="CapturePoi" type="Node3D"] +script = ExtResource("1") +poi_kind = "dock" +poi_view = Vector3(16, 8, 4) diff --git a/test/capture/poi_hamlet.tscn b/test/capture/poi_hamlet.tscn new file mode 100644 index 0000000..3cf67bb --- /dev/null +++ b/test/capture/poi_hamlet.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/terrain_scene.gd" id="1"] + +[node name="CapturePoi" type="Node3D"] +script = ExtResource("1") +poi_kind = "hamlet" diff --git a/test/capture/poi_lighthouse.tscn b/test/capture/poi_lighthouse.tscn new file mode 100644 index 0000000..549befc --- /dev/null +++ b/test/capture/poi_lighthouse.tscn @@ -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="CapturePoi" type="Node3D"] +script = ExtResource("1") +poi_kind = "lighthouse" +poi_view = Vector3(10, 9, 30) diff --git a/test/capture/poi_quarry.tscn b/test/capture/poi_quarry.tscn new file mode 100644 index 0000000..1aa8c44 --- /dev/null +++ b/test/capture/poi_quarry.tscn @@ -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="CapturePoi" type="Node3D"] +script = ExtResource("1") +poi_kind = "quarry" +poi_view = Vector3(10, 12, 30) diff --git a/test/capture/poi_salt_works.tscn b/test/capture/poi_salt_works.tscn new file mode 100644 index 0000000..64dde49 --- /dev/null +++ b/test/capture/poi_salt_works.tscn @@ -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="CapturePoi" type="Node3D"] +script = ExtResource("1") +poi_kind = "salt_works" +poi_view = Vector3(10, 12, 30) diff --git a/test/capture/poi_shrine.tscn b/test/capture/poi_shrine.tscn new file mode 100644 index 0000000..21d15b7 --- /dev/null +++ b/test/capture/poi_shrine.tscn @@ -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="CapturePoi" type="Node3D"] +script = ExtResource("1") +poi_kind = "shrine" +poi_view = Vector3(10, 12, 30) diff --git a/test/capture/terrain_scene.gd b/test/capture/terrain_scene.gd index d91b7d2..fb6a57c 100644 --- a/test/capture/terrain_scene.gd +++ b/test/capture/terrain_scene.gd @@ -15,6 +15,10 @@ const CRACK_COLOUR: Color = Color(1.0, 0.0, 1.0) @export var fov: float = 70.0 ## Off only in terrain_cracks_control, which must show cracks. @export var skirts: bool = true +## When set, draw the island's points of interest and frame the first one of this kind: +## 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) func _ready() -> void: @@ -58,8 +62,20 @@ func _ready() -> void: camera.fov = fov camera.far = 2000.0 camera.position = Vector3(eye.x, island.ground.terrain_height(eye.x, eye.y) + lift, eye.y) + var look_at_point: Vector3 = target + if not poi_kind.is_empty(): + var placed: PoiSet = PoiSet.generate(island) + var view: PoiView = PoiView.new() + view.setup(placed) + add_child(view) + for poi: Poi in placed.pois: + if poi.kind == poi_kind: + camera.position = poi.to_world(poi_view) + 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 add_child(camera) - camera.look_at(target) + camera.look_at(look_at_point) camera.make_current() var terrain: TerrainView = TerrainView.new() diff --git a/test/sim/test_movement.gd b/test/sim/test_movement.gd index 5b0fd7d..734f9f6 100644 --- a/test/sim/test_movement.gd +++ b/test/sim/test_movement.gd @@ -273,3 +273,30 @@ func _rough_simulation() -> Simulation: for _index: int in 4: sim.add_player() return sim + + +func test_climbs_stairs_with_treads_narrower_than_the_body() -> void: + # Treads of 0.45 m are narrower than the 0.8 m body, so the next riser is under the + # body's front edge before its centre is lifted onto the current step. (Treads + # narrower than the 0.4 m radius can't be climbed: the second riser is reached + # before the centre is on the first.) + var ground: SimGround = SimGround.flat() + for index: int in 12: + ground.add_solid(AABB(Vector3(10.0 + index * 0.45, 0.0, 8.0), Vector3(0.45, 0.35 * (index + 1), 4.0))) + # A landing at the top, so the player stays up there. + ground.add_solid(AABB(Vector3(15.4, 0.0, 8.0), Vector3(20.0, 0.35 * 12, 4.0))) + var player: SimEntity = _player(Vector3(8.0, 0.0, 10.0)) + _run(player, ground, 90, Vector2(0.0, 1.0), EAST) + assert_near(player.position.y, 0.35 * 12, 0.01, "should stand on the top step") + assert_true(player.position.x > 15.0, "should have walked up the flight") + + +func test_a_step_does_not_let_the_body_through_a_wall_behind_it() -> void: + # A climbable 0.4 m step with a 1.5 m wall right behind it: stepping up must not + # carry the body into the wall. + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(10.0, 0.0, 8.0), Vector3(0.5, 0.4, 4.0))) + ground.add_solid(AABB(Vector3(10.5, 0.0, 8.0), Vector3(1.0, 1.5, 4.0))) + var player: SimEntity = _player(Vector3(8.0, 0.0, 10.0)) + _run(player, ground, 90, Vector2(0.0, 1.0), EAST) + assert_true(player.position.x < 10.5 - Tuning.PLAYER_RADIUS + 0.01, "walked into the wall to x %.2f" % player.position.x) diff --git a/test/sim/test_poi.gd b/test/sim/test_poi.gd new file mode 100644 index 0000000..988be3b --- /dev/null +++ b/test/sim/test_poi.gd @@ -0,0 +1,120 @@ +extends TestCase + +## Seeds for the placement checks, and the fewer, slower traversal walks. +const PLACEMENT_SEEDS: int = 30 +const TRAVERSAL_SEEDS: Array[int] = [1, 2, 3, 4] +## A route segment must be walked within this many seconds. +const SEGMENT_SECONDS: float = 12.0 +const ARRIVE_METRES: float = 0.45 + + +func test_every_island_gets_every_kind_well_spaced() -> void: + var kinds: int = Tuning.POI_KINDS.size() + assert_true(kinds >= 6, "issue W4 asks for at least 6 kinds") + for seed_value: int in range(1, PLACEMENT_SEEDS + 1): + var island: Island = Island.generate(seed_value) + var placed: PoiSet = PoiSet.generate(island) + var label: String = "seed %d" % seed_value + var total: int = placed.pois.size() + assert_true(total >= Tuning.POI_MIN_TOTAL and total <= Tuning.POI_MAX_TOTAL, "%s placed %d" % [label, total]) + for kind: String in Tuning.POI_KINDS: + var count: int = placed.count_of(kind) + assert_true(count >= Tuning.POI_KINDS[kind]["min"] and count <= Tuning.POI_KINDS[kind]["max"], + "%s: %d %s" % [label, count, kind]) + for a: int in total: + var here: Vector3 = placed.pois[a].origin + assert_true(here.y > 0.0, "%s: %s sited in the sea" % [label, placed.pois[a].kind]) + for b: int in range(a + 1, total): + var gap: float = Vector2(here.x, here.z).distance_to(Vector2(placed.pois[b].origin.x, placed.pois[b].origin.z)) + assert_true(gap >= Tuning.POI_SPACING_METRES - 0.001, "%s: two sites %.0f m apart" % [label, gap]) + _check_sites(placed, label) + + +func test_same_seed_places_the_same_points_of_interest() -> void: + var first: PoiSet = PoiSet.generate(Island.generate(9)) + var second: PoiSet = PoiSet.generate(Island.generate(9)) + assert_eq(first.pois.size(), second.pois.size()) + for index: int in first.pois.size(): + assert_eq(first.pois[index].origin, second.pois[index].origin) + assert_eq(first.pois[index].solids, second.pois[index].solids) + + +func test_loot_markers_are_exposed_and_stand_on_floors() -> void: + var sim: Simulation = Simulation.new(6) + var markers: Array[Dictionary] = sim.pois.loot_markers() + assert_true(markers.size() >= sim.pois.pois.size(), "every POI should offer loot, got %d markers" % markers.size()) + var kinds: Dictionary = {} + for marker: Dictionary in markers: + var at: Vector3 = marker["position"] + kinds[marker["kind"]] = true + var support: float = sim.ground.support_height(at.x, at.z, at.y + 0.5) + assert_near(support, at.y, 0.6, "%s marker at %s floats or is buried" % [marker["kind"], at]) + assert_false(sim.ground.body_blocked(at + Vector3.UP * 0.05, 0.3, 1.0, 0.0), "%s marker at %s is inside a solid" % [marker["kind"], at]) + assert_eq(kinds.size(), Tuning.POI_KINDS.size(), "every kind has loot") + + +func test_interiors_are_walkable() -> void: + # The automated traversal: a player walks every route of every POI, from open ground + # outside, through the door and up any stairs, to the loot inside, using the real + # Movement code against the real solids. + var walked: int = 0 + for seed_value: int in TRAVERSAL_SEEDS: + var sim: Simulation = Simulation.new(seed_value) + for poi: Poi in sim.pois.pois: + for route: Array in poi.routes: + var failure: String = _walk(sim.ground, route) + assert_eq(failure, "", "seed %d %s at %s" % [seed_value, poi.kind, poi.origin]) + walked += 1 + assert_true(walked >= 40, "walked only %d routes" % walked) + + +func test_players_do_not_spawn_inside_buildings() -> void: + var sim: Simulation = Simulation.new(3) + for _index: int in Tuning.SOLO_PLAYERS: + var player: SimEntity = sim.entities.find(sim.add_player()) + assert_false(sim.ground.body_blocked(player.position, Tuning.PLAYER_RADIUS, Tuning.PLAYER_HEIGHT, 0.0), + "spawned inside a solid at %s" % player.position) + + +func _check_sites(placed: PoiSet, label: String) -> void: + for poi: Poi in placed.pois: + var height: float = poi.origin.y + match poi.kind: + "shrine": + assert_true(height >= Tuning.ISLAND_FOREST_TOP * 0.8, "%s: shrine at %.1f m" % [label, height]) + "dock": + assert_true(height < 6.0, "%s: dock at %.1f m" % [label, height]) + "salt_works": + assert_true(height < Tuning.ISLAND_MARSH_TOP * 3.2, "%s: salt works at %.1f m" % [label, height]) + "quarry": + assert_true(height >= Tuning.ISLAND_FOREST_TOP - 6.0, "%s: quarry at %.1f m" % [label, height]) + + +## Walks a player along `route`. Returns "" on success or what went wrong. +func _walk(ground: SimGround, route: Array) -> String: + var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER) + var start: Vector3 = route[0] + player.position = Vector3(start.x, ground.support_height(start.x, start.z, start.y + 1.0), start.z) + player.health = Tuning.PLAYER_MAX_HEALTH + player.on_ground = true + if ground.body_blocked(player.position, Tuning.PLAYER_RADIUS, Tuning.PLAYER_HEIGHT, Tuning.STEP_HEIGHT): + return "route starts inside a solid at %s" % player.position + var tick: int = 0 + for index: int in range(1, route.size()): + var target: Vector3 = route[index] + var limit: int = tick + roundi(SEGMENT_SECONDS * Tuning.TICK_RATE_HZ) + while true: + var towards: Vector2 = Vector2(target.x - player.position.x, target.z - player.position.z) + if towards.length() < ARRIVE_METRES: + break + if tick >= limit: + return "stuck at %s heading for waypoint %d %s" % [player.position, index, target] + var speed: float = clampf(towards.length() / (Tuning.WALK_SPEED / Tuning.TICK_RATE_HZ), 0.2, 1.0) + var command: PlayerCommand = PlayerCommand.from_input(tick, Vector2(0.0, speed), atan2(towards.x, towards.y), 0.0) + Movement.step(player, command, ground) + tick += 1 + if absf(player.position.y - target.y) > 1.0: + return "reached waypoint %d at height %.2f, expected %.2f" % [index, player.position.y, target.y] + if player.health < Tuning.PLAYER_MAX_HEALTH: + return "took damage on the way" + return "" diff --git a/test/sim/test_poi.gd.uid b/test/sim/test_poi.gd.uid new file mode 100644 index 0000000..147a52c --- /dev/null +++ b/test/sim/test_poi.gd.uid @@ -0,0 +1 @@ +uid://be2rbacqxhrdk diff --git a/test/sim/test_sim_ground.gd b/test/sim/test_sim_ground.gd index 80fbeb4..3ab137f 100644 --- a/test/sim/test_sim_ground.gd +++ b/test/sim/test_sim_ground.gd @@ -83,3 +83,32 @@ func test_body_blocked_respects_step_and_height() -> void: assert_false(ground.body_blocked(Vector3(2.5, 0.0, 1.0), 0.4, 1.8, 0.0)) assert_false(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 1.8, 0.0), "fits under") assert_true(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 2.5, 0.0)) + + +func test_index_finds_solids_across_cells() -> void: + var ground: SimGround = SimGround.flat() + # Spans several index cells, including negative ones. + ground.add_solid(AABB(Vector3(-5.0, 0.0, -5.0), Vector3(30.0, 2.0, 3.0))) + assert_eq(ground.support_height(20.0, -4.0, 5.0), 2.0) + assert_eq(ground.support_height(-4.0, -4.0, 5.0), 2.0) + assert_eq(ground.support_height(20.0, 4.0, 5.0), 0.0) + assert_true(ground.body_blocked(Vector3(12.0, 0.0, -1.7), 0.4, 1.8, 0.0), "radius reaches into the next cell") + # The box's far side from the ray's start is at z = -2, 22 m along a 40 m ray. + assert_near(ground.raycast(Vector3(15.0, 1.0, 20.0), Vector3(15.0, 1.0, -20.0)), 22.0 / 40.0, 0.0001) + + +func test_index_gives_the_same_answers_as_a_full_scan() -> void: + var ground: SimGround = SimGround.flat() + var random: RandomNumberGenerator = RandomNumberGenerator.new() + random.seed = 5 + for _index: int in 200: + var at: Vector3 = Vector3(random.randf_range(0.0, 100.0), random.randf_range(0.0, 4.0), random.randf_range(0.0, 100.0)) + ground.add_solid(AABB(at, Vector3(random.randf_range(0.2, 12.0), random.randf_range(0.2, 3.0), random.randf_range(0.2, 12.0)))) + for _index: int in 500: + var x: float = random.randf_range(0.0, 100.0) + var z: float = random.randf_range(0.0, 100.0) + var expected: float = 0.0 + for box: AABB in ground.solids: + 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)