Files
tidewall/test/capture/build_scene.gd
T
claudeandClaude Opus 5.5 f35ebf7171 B2: Grid building and placement preview
Server-authoritative grid building with wall, floor, ramp, roof and
breakwater pieces in driftwood, stone and salvage iron, plus a client
placement ghost.

- StructureSet: pieces in grid slots (floor, two wall edges, cell
  volume), their collision boxes in SimGround, and touching-neighbour
  links kept up to date for B3. Ramps are stairs of 0.4 m steps; walls
  over lower ground get a skirt down to the terrain; roofs are a low
  stepped pyramid. Pieces are part of the state hash.
- Building: target() snaps look and position to a slot, and check()
  validates any slot as if a hostile client chose it: state, range,
  occupancy, trapping a player, cost and support. Fire in build mode
  places at most 10 pieces a second; players in a low step are lifted.
- Levels are half a cell (2 m) apart, so ground pieces sit within a
  metre of the builder's feet instead of being half buried.
- Client: StructureView draws pieces from their boxes in plank, course
  or panel strips; ramps draw as a sloped deck. BuildGhost shows sea
  green or rust red from the same target() and check().
- Tests: 15 sim tests (targeting, every rejection, overlap, support,
  stairs climbable, skirts, determinism, 24 builders at 10 pieces a
  second within the tick budget) and 5 client tests (ghost states,
  ghost agrees with the server, view sync, mesh orientation).
- Capture scenarios build_fort and build_fort_side.

Closes #16

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:23:29 -05:00

155 lines
6.9 KiB
GDScript

## Capture scenario for building (B2): a small fort of every piece kind in all three
## materials on real island terrain, with one valid and one invalid placement ghost.
extends Node3D
@export var island_seed: int = 2
## Camera offset from the fort's corner: x east, y up, z south.
@export var view: Vector3 = Vector3(-11.0, 8.5, -12.0)
func _ready() -> void:
var sim: Simulation = Simulation.new(island_seed)
var g: float = Tuning.GRID_CELL_METRES
var site: Vector2i = _find_site(sim)
var builder_id: int = sim.add_player()
var broke_id: int = sim.add_player()
var builder: SimEntity = sim.entities.find(builder_id)
var broke: SimEntity = sim.entities.find(broke_id)
for code: int in 3:
builder.materials[code] = Tuning.MATERIAL_CAP
var feet: Vector3 = Vector3((site.x + 0.5) * g, 0.0, (site.y + 0.5) * g)
# Fell the trees around the site, so the fort stands in a clearing.
for prop: int in sim.props.near(feet, 40.0):
sim.props.damage(prop, 100000)
feet.y = sim.ground.terrain_height(feet.x, feet.z)
var level: int = roundi(feet.y / Tuning.BUILD_LEVEL_METRES)
var up: int = roundi(g / Tuning.BUILD_LEVEL_METRES)
var placed: Array[String] = []
# A two-cell room: floor, walls in three materials, a roof, a ramp up its side and a
# breakwater on the seaward... well, the near side.
var plan: Array = [
[StructureSet.FLOOR, Vector4i(site.x, level, site.y, StructureSet.SLOT_FLOOR), 0, Materials.DRIFTWOOD],
[StructureSet.FLOOR, Vector4i(site.x + 1, level, site.y, StructureSet.SLOT_FLOOR), 0, Materials.STONE],
[StructureSet.WALL, Vector4i(site.x, level, site.y + 1, StructureSet.SLOT_WALL_Z), 0, Materials.DRIFTWOOD],
[StructureSet.WALL, Vector4i(site.x + 1, level, site.y + 1, StructureSet.SLOT_WALL_Z), 0, Materials.STONE],
[StructureSet.WALL, Vector4i(site.x + 2, level, site.y, StructureSet.SLOT_WALL_X), 0, Materials.SALVAGE_IRON],
[StructureSet.BREAKWATER, Vector4i(site.x + 1, level, site.y, StructureSet.SLOT_WALL_Z), 0, Materials.STONE],
[StructureSet.RAMP, Vector4i(site.x - 1, level, site.y, StructureSet.SLOT_VOLUME), 1, Materials.DRIFTWOOD],
[StructureSet.FLOOR, Vector4i(site.x, level + up, site.y, StructureSet.SLOT_FLOOR), 0, Materials.DRIFTWOOD],
[StructureSet.ROOF, Vector4i(site.x + 1, level + up, site.y, StructureSet.SLOT_VOLUME), 0, Materials.SALVAGE_IRON],
]
for entry: Array in plan:
var at: Building.Target = Building.Target.new()
at.kind = entry[0]
at.slot = entry[1]
at.facing = entry[2]
builder.position = feet + Vector3(0.0, 0.5, 0.0)
var result: Building.Result = Building.place(sim, builder, at, entry[3])
placed.append("%s %s" % [StructureSet.KINDS[at.kind], Building.Result.keys()[result]])
print("build_scene: site %s level %d: %s" % [site, level, ", ".join(placed)])
_environment()
var structures: StructureView = StructureView.new()
structures.setup(sim.structures)
add_child(structures)
var props: PropView = PropView.new()
props.setup(sim.props)
add_child(props)
# Valid ghost: the builder, in the second cell, facing +x for a wall on the far edge
# one level up... looking up, so it targets the level above the iron wall.
builder.position = Vector3((site.x + 0.5) * g, level * Tuning.BUILD_LEVEL_METRES + Tuning.FLOOR_THICKNESS, (site.y + 0.5) * g)
_face(builder, 180.0, 0.0)
var good: BuildGhost = BuildGhost.new()
good.update(sim, builder, StructureSet.WALL, Materials.DRIFTWOOD)
add_child(good)
# Invalid ghost: a player with no materials trying a breakwater beside the fort.
broke.position = Vector3((site.x + 2.5) * g, 0.0, (site.y + 0.5) * g)
broke.position.y = sim.ground.terrain_height(broke.position.x, broke.position.z)
_face(broke, 180.0, 0.0)
var bad: BuildGhost = BuildGhost.new()
bad.update(sim, broke, StructureSet.BREAKWATER, Materials.STONE)
add_child(bad)
print("build_scene: ghosts %s (%s) and %s (%s)" % [good.valid, Building.Result.keys()[good.result], bad.valid, Building.Result.keys()[bad.result]])
var camera: Camera3D = Camera3D.new()
camera.fov = 62.0
camera.far = 1500.0
var corner: Vector3 = Vector3(site.x * g, level * Tuning.BUILD_LEVEL_METRES, site.y * g)
camera.position = corner + view
camera.position.y = maxf(camera.position.y, sim.ground.terrain_height(camera.position.x, camera.position.z) + 2.0)
add_child(camera)
camera.look_at(corner + Vector3(g, 2.5, g * 0.5))
camera.make_current()
var terrain: TerrainView = TerrainView.new()
terrain.setup(sim.island)
terrain.focus = camera.position
add_child(terrain)
terrain.build_all_now()
var water: WaterView = WaterView.new()
water.set_level(sim.sea_level)
add_child(water)
func _face(player: SimEntity, yaw_degrees: float, pitch_degrees: float) -> void:
var command: PlayerCommand = PlayerCommand.from_input(0, Vector2.ZERO, deg_to_rad(yaw_degrees), deg_to_rad(pitch_degrees))
player.yaw = command.yaw
player.pitch = command.pitch
## A cell in open, gently sloping forest with nothing standing within a few cells.
func _find_site(sim: Simulation) -> Vector2i:
var g: float = Tuning.GRID_CELL_METRES
var cells: int = floori(Tuning.WORLD_SIZE_METRES / g)
for cz: int in range(cells / 2, cells - 4):
for cx: int in range(cells / 2, cells - 4):
var centre: Vector2 = Vector2((cx + 1.0) * g, (cz + 0.5) * g)
var h: float = sim.ground.terrain_height(centre.x, centre.y)
if h < Tuning.ISLAND_MARSH_TOP + 3.0 or h > Tuning.ISLAND_FOREST_TOP - 10.0:
continue
var low: float = INF
var high: float = -INF
for dz: float in [-1.0, 0.0, 1.0, 2.0]:
for dx: float in [-2.0, -1.0, 0.0, 1.0, 2.0, 3.0]:
var y: float = sim.ground.terrain_height(cx * g + dx * g, cz * g + dz * g)
low = minf(low, y)
high = maxf(high, y)
if high - low > 2.0 or sim.island.band_at(centre.x + 30.0, centre.y + 30.0) != Island.Band.FOREST:
continue
if not sim.pois.pois.is_empty() and _near_poi(sim, centre, 60.0):
continue
return Vector2i(cx, cz)
return Vector2i(cells / 2, cells / 2)
func _near_poi(sim: Simulation, at: Vector2, radius: float) -> bool:
for poi: Poi in sim.pois.pois:
if Vector2(poi.origin.x, poi.origin.z).distance_to(at) < radius:
return true
return false
func _environment() -> void:
var sky_material: ProceduralSkyMaterial = ProceduralSkyMaterial.new()
sky_material.sky_top_color = Color(0.32, 0.5, 0.68)
sky_material.sky_horizon_color = Color(0.82, 0.78, 0.68)
sky_material.ground_bottom_color = Color(0.18, 0.2, 0.2)
sky_material.ground_horizon_color = Color(0.82, 0.78, 0.68)
var sky: Sky = Sky.new()
sky.sky_material = sky_material
var environment: Environment = Environment.new()
environment.background_mode = Environment.BG_SKY
environment.sky = sky
environment.ambient_light_source = Environment.AMBIENT_SOURCE_SKY
environment.ambient_light_energy = 0.45
environment.tonemap_mode = Environment.TONE_MAPPER_FILMIC
var world: WorldEnvironment = WorldEnvironment.new()
world.environment = environment
add_child(world)
var sun: DirectionalLight3D = DirectionalLight3D.new()
sun.rotation_degrees = Vector3(-40.0, -130.0, 0.0)
sun.light_energy = 0.95
sun.shadow_enabled = true
add_child(sun)