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>
This commit is contained in:
2026-11-11 14:23:29 -05:00
co-authored by Claude Opus 5.5
parent b847ba9695
commit f35ebf7171
21 changed files with 1327 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/build_scene.gd" id="1"]
[node name="CaptureBuild" type="Node3D"]
script = ExtResource("1")
+7
View File
@@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3]
[ext_resource type="Script" path="res://test/capture/build_scene.gd" id="1"]
[node name="CaptureBuild" type="Node3D"]
script = ExtResource("1")
view = Vector3(-14, 6, 16)
+154
View File
@@ -0,0 +1,154 @@
## 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)
+1
View File
@@ -0,0 +1 @@
uid://ck6qnglo16at
+108
View File
@@ -0,0 +1,108 @@
extends TestCase
## Client side of issue B2: the placement ghost and the structure view.
func _sim() -> Simulation:
var sim: Simulation = Simulation.new(3, SimGround.flat())
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(10.0, 0.0, 10.0)
player.materials[0] = 100
return sim
func test_ghost_shows_valid_and_invalid_states() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var ghost: BuildGhost = BuildGhost.new()
ghost.update(sim, player, StructureSet.WALL, Materials.DRIFTWOOD)
assert_true(ghost.valid)
assert_true(ghost.visible)
assert_eq(ghost.colour(), BuildGhost.VALID_COLOUR)
assert_eq(ghost.target.slot, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z))
# Unaffordable in stone.
ghost.update(sim, player, StructureSet.WALL, Materials.STONE)
assert_false(ghost.valid)
assert_eq(ghost.result, Building.Result.UNAFFORDABLE)
assert_eq(ghost.colour(), BuildGhost.INVALID_COLOUR)
# Occupied once the server has placed it.
assert_eq(Building.place(sim, player, Building.target(player, StructureSet.WALL), 0), Building.Result.OK)
ghost.update(sim, player, StructureSet.WALL, Materials.DRIFTWOOD)
assert_eq(ghost.result, Building.Result.OCCUPIED)
assert_eq(ghost.colour(), BuildGhost.INVALID_COLOUR)
ghost.update(sim, player, 42, 0)
assert_false(ghost.visible)
ghost.free()
func test_ghost_agrees_with_the_server() -> void:
# Whatever the ghost says, the server does the same with the same state.
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var ghost: BuildGhost = BuildGhost.new()
for yaw_step: int in 8:
for kind: int in StructureSet.KINDS.size():
var command: PlayerCommand = PlayerCommand.from_input(sim.tick, Vector2.ZERO, yaw_step * PI / 4.0, 0.0,
PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, 0)
player.yaw = command.yaw
player.action_cooldown = 0
ghost.update(sim, player, kind, 0)
var before: int = sim.structures.count()
sim.submit_command(1, command)
sim.step()
assert_eq(sim.structures.count() > before, ghost.valid, "yaw step %d kind %d (%s)" % [yaw_step, kind, ghost.result])
ghost.free()
func test_structure_view_follows_the_simulation() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var view: StructureView = StructureView.new()
view.setup(sim.structures)
assert_eq(view.drawn_count(), 0)
Building.place(sim, player, Building.target(player, StructureSet.WALL), 0)
Building.place(sim, player, Building.target(player, StructureSet.RAMP), 0)
view.sync()
assert_eq(view.drawn_count(), 2)
sim.structures.remove(1, sim.ground)
view.sync()
assert_eq(view.drawn_count(), 1)
view.free()
func test_piece_meshes_face_outward_and_match_the_boxes() -> void:
var box: AABB = AABB(Vector3(0.0, 0.0, 0.0), Vector3(4.0, 4.0, 0.3))
var mesh: ArrayMesh = PieceMesher.box_mesh([box], Color.WHITE)
var arrays: Array = mesh.surface_get_arrays(0)
var vertices: PackedVector3Array = arrays[Mesh.ARRAY_VERTEX]
var normals: PackedVector3Array = arrays[Mesh.ARRAY_NORMAL]
assert_eq(vertices.size(), 36)
var centre: Vector3 = box.get_center()
for index: int in vertices.size():
var outward: Vector3 = vertices[index] - centre
assert_true(normals[index].dot(outward) > 0.0, "normal %s at %s points inward" % [normals[index], vertices[index]])
for material: int in 3:
var built: ArrayMesh = PieceMesher.build(StructureSet.boxes_for(StructureSet.WALL, Vector4i(0, 0, 0, StructureSet.SLOT_WALL_X), 0), material, 1)
var bounds: AABB = built.get_aabb()
var wall: AABB = StructureSet.boxes_for(StructureSet.WALL, Vector4i(0, 0, 0, StructureSet.SLOT_WALL_X), 0)[0]
assert_true(wall.grow(0.001).encloses(bounds), "mesh stays within the wall's box")
assert_true(bounds.size.y > wall.size.y - 0.1, "mesh fills the wall's height")
func test_ramp_meshes_cover_their_stairs() -> void:
for facing: int in 4:
var slot: Vector4i = Vector4i(3, 5, 7, StructureSet.SLOT_VOLUME)
var stairs: Array[AABB] = StructureSet.boxes_for(StructureSet.RAMP, slot, facing)
var whole: AABB = stairs[0]
for box: AABB in stairs:
whole = whole.merge(box)
var bounds: AABB = PieceMesher.build_ramp(slot, facing, 0, 1).get_aabb()
assert_true(whole.grow(0.05).encloses(bounds), "facing %d: mesh %s outside stairs %s" % [facing, bounds, whole])
assert_true(bounds.grow(0.05).encloses(whole), "facing %d: mesh %s doesn't cover stairs %s" % [facing, bounds, whole])
# The deck's high side is the way the ramp faces.
var top_step: AABB = stairs[-1]
var direction: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
var centre: Vector3 = whole.get_center()
var toward: Vector3 = top_step.get_center() - centre
assert_true(toward.x * direction.x + toward.z * direction.y > 0.0, "facing %d: top step on the wrong side" % facing)
+1
View File
@@ -0,0 +1 @@
uid://cpynfjs2mk0ba
+290
View File
@@ -0,0 +1,290 @@
extends TestCase
## Grid building from issue B2: targeting, validation and placement on the server.
const W: int = StructureSet.WALL
const F: int = StructureSet.FLOOR
const R: int = StructureSet.RAMP
const ROOF: int = StructureSet.ROOF
const BW: int = StructureSet.BREAKWATER
func _sim(feet: Vector3 = Vector3(10.0, 0.0, 10.0), stock: int = 200) -> Simulation:
var sim: Simulation = Simulation.new(3, SimGround.flat())
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = feet
for code: int in Materials.NAMES.size():
player.materials[code] = stock
return sim
func _player(sim: Simulation, id: int = 1) -> SimEntity:
return sim.entities.find(id)
## Sets the player's look from degrees.
func _look(player: SimEntity, yaw_degrees: float, pitch_degrees: float = 0.0) -> 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
func _slot(x: int, level: int, z: int, type: int) -> Building.Target:
var at: Building.Target = Building.Target.new()
at.slot = Vector4i(x, level, z, type)
return at
func _build(sim: Simulation, kind: int, material: int, ticks: int = 1, yaw_degrees: float = 0.0, pitch_degrees: float = 0.0) -> int:
var placed: int = 0
for _i: int in ticks:
var command: PlayerCommand = PlayerCommand.from_input(sim.tick, Vector2.ZERO, deg_to_rad(yaw_degrees),
deg_to_rad(pitch_degrees), PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, material)
sim.submit_command(1, command)
sim.step()
for event: Dictionary in sim.events:
if event["type"] == "piece_placed":
placed += 1
return placed
func test_targeting_snaps_to_the_grid() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
# Player in cell (2, 2) at level 0, facing +z.
_look(player, 0.0)
assert_eq(Building.target(player, W).slot, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z))
assert_eq(Building.target(player, F).slot, Vector4i(2, 0, 3, StructureSet.SLOT_FLOOR))
assert_eq(Building.target(player, R).slot, Vector4i(2, 0, 3, StructureSet.SLOT_VOLUME))
_look(player, 90.0)
assert_eq(Building.target(player, BW).slot, Vector4i(3, 0, 2, StructureSet.SLOT_WALL_X))
assert_eq(Building.target(player, R).facing, 1)
_look(player, 180.0)
assert_eq(Building.target(player, W).slot, Vector4i(2, 0, 2, StructureSet.SLOT_WALL_Z))
_look(player, -90.0)
assert_eq(Building.target(player, W).slot, Vector4i(2, 0, 2, StructureSet.SLOT_WALL_X))
_look(player, 0.0, 60.0)
assert_eq(Building.target(player, W).slot.y, 2, "looking up targets a cell higher")
player.position.y = 0.9
assert_eq(Building.target(player, W).slot.y, 2, "levels are half a cell")
player.position.y = 1.1
_look(player, 0.0)
assert_eq(Building.target(player, W).slot.y, 1, "the level nearest the feet")
player.position.y = 0.0
_look(player, 0.0, -60.0)
assert_eq(Building.target(player, F).slot, Vector4i(2, 0, 2, StructureSet.SLOT_FLOOR), "looking down builds under you")
assert_null(Building.target(player, 99))
func test_placing_spends_material_and_adds_collision() -> void:
var sim: Simulation = _sim()
assert_eq(_build(sim, W, Materials.STONE), 1)
assert_eq(sim.structures.count(), 1)
assert_eq(_player(sim).materials[Materials.STONE], 200 - Tuning.BUILD_PIECES["wall"]["cost"])
var piece: StructureSet.Piece = sim.structures.find(1)
assert_eq(piece.material, Materials.STONE)
assert_eq(piece.health, Tuning.MATERIALS["stone"]["health"])
assert_true(piece.grounded)
# The wall stands at z = 12 across x 8..12 and blocks walking.
for _i: int in 60:
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), 0.0, 0.0))
sim.step()
assert_true(_player(sim).position.z < 12.0 - Tuning.PLAYER_RADIUS + 0.01, "the wall should stop the player")
func test_rejects_unaffordable() -> void:
var sim: Simulation = _sim(Vector3(10.0, 0.0, 10.0), 9)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), W), Materials.DRIFTWOOD), Building.Result.UNAFFORDABLE)
assert_eq(_build(sim, W, Materials.DRIFTWOOD), 0)
assert_eq(_player(sim).materials[Materials.DRIFTWOOD], 9, "nothing spent")
_player(sim).materials[Materials.DRIFTWOOD] = 29
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), BW), Materials.DRIFTWOOD), Building.Result.UNAFFORDABLE, "breakwaters cost 30")
func test_rejects_out_of_range_slots() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
var far: Building.Target = _slot(5, 0, 2, StructureSet.SLOT_FLOOR)
far.kind = F
assert_eq(Building.check(sim, player, far, 0), Building.Result.OUT_OF_RANGE)
var high: Building.Target = _slot(2, 6, 3, StructureSet.SLOT_FLOOR)
high.kind = F
assert_eq(Building.check(sim, player, high, 0), Building.Result.OUT_OF_RANGE)
var near: Building.Target = _slot(3, 0, 2, StructureSet.SLOT_FLOOR)
near.kind = F
assert_eq(Building.check(sim, player, near, 0), Building.Result.OK)
var outside: Building.Target = _slot(-1, 0, 2, StructureSet.SLOT_FLOOR)
outside.kind = F
player.position = Vector3(1.0, 0.0, 10.0)
assert_eq(Building.check(sim, player, outside, 0), Building.Result.OUT_OF_WORLD)
var bad: Building.Target = _slot(2, 0, 3, StructureSet.SLOT_FLOOR)
bad.kind = W
assert_eq(Building.check(sim, player, bad, 0), Building.Result.BAD_REQUEST, "a wall can't take a floor slot")
assert_eq(Building.check(sim, player, near, 7), Building.Result.BAD_REQUEST, "unknown material")
func test_rejects_overlapping_pieces() -> void:
var sim: Simulation = _sim()
assert_eq(_build(sim, W, 0), 1)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), W), 0), Building.Result.OCCUPIED)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), BW), 0), Building.Result.OCCUPIED, "a breakwater shares the wall slot")
# Holding fire for a second on the same slot places nothing more.
assert_eq(_build(sim, W, 0, 30), 0)
assert_eq(sim.structures.count(), 1)
# A wall half a cell up would overlap it too, but a whole cell up is free.
var half_up: Building.Target = _slot(2, 1, 3, StructureSet.SLOT_WALL_Z)
half_up.kind = W
assert_eq(Building.check(sim, _player(sim), half_up, 0), Building.Result.OCCUPIED)
var full_up: Building.Target = _slot(2, 2, 3, StructureSet.SLOT_WALL_Z)
full_up.kind = W
assert_eq(Building.check(sim, _player(sim), full_up, 0), Building.Result.OK)
# A ramp and a roof share the volume slot.
assert_eq(_build(sim, R, 0, 1, 90.0), 1)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), ROOF), 0), Building.Result.OCCUPIED)
func test_rejects_pieces_that_would_trap_a_player() -> void:
var sim: Simulation = _sim()
var other: int = sim.add_player()
# Another player stands right on the edge where the wall would go.
sim.entities.find(other).position = Vector3(10.0, 0.0, 12.0)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), W), 0), Building.Result.BLOCKED_BY_PLAYER)
# A floor under a player's feet is only a step, so it lifts them instead.
sim.entities.find(other).position = Vector3(10.0, 0.0, 14.0)
assert_eq(_build(sim, F, 0), 1)
assert_near(sim.entities.find(other).position.y, Tuning.FLOOR_THICKNESS, 0.001)
func test_rejects_floating_pieces_but_accepts_attached_ones() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
var floating: Building.Target = _slot(2, 2, 3, StructureSet.SLOT_FLOOR)
floating.kind = F
assert_eq(Building.check(sim, player, floating, 0), Building.Result.UNSUPPORTED)
# A wall below holds it up.
assert_eq(_build(sim, W, 0), 1)
assert_eq(Building.check(sim, player, floating, 0), Building.Result.OK)
assert_eq(Building.place(sim, player, floating, 0), Building.Result.OK)
var piece: StructureSet.Piece = sim.structures.find(2)
assert_false(piece.grounded)
assert_eq(sim.structures.neighbours[2], PackedInt32Array([1]))
assert_eq(sim.structures.neighbours[1], PackedInt32Array([2]))
func test_no_building_while_swimming_or_falling() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
player.swimming = true
assert_eq(Building.check(sim, player, Building.target(player, W), 0), Building.Result.NOT_ALLOWED)
player.swimming = false
player.drop_state = Drop.FREEFALL
assert_eq(Building.check(sim, player, Building.target(player, W), 0), Building.Result.NOT_ALLOWED)
func test_ramps_are_climbable_stairs() -> void:
var sim: Simulation = _sim(Vector3(10.0, 0.0, 11.0))
assert_eq(_build(sim, R, 0), 1)
var ramp: StructureSet.Piece = sim.structures.find(1)
assert_eq(ramp.boxes.size(), Tuning.RAMP_STEPS)
assert_true(Tuning.GRID_CELL_METRES / Tuning.RAMP_STEPS <= Tuning.STEP_HEIGHT, "each riser must be a step")
assert_true(Tuning.GRID_CELL_METRES / Tuning.RAMP_STEPS >= Tuning.PLAYER_RADIUS, "each tread must hold a foot")
# Walk up it, then onto a floor at the next level.
var top: Building.Target = _slot(2, 2, 4, StructureSet.SLOT_FLOOR)
top.kind = F
assert_eq(Building.check(sim, _player(sim), top, 0), Building.Result.OK)
Building.place(sim, _player(sim), top, 0)
# 2 s at walking pace: 8 m, from z = 11 to about 19, short of the floor's far edge.
for _i: int in 60:
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), 0.0, 0.0))
sim.step()
var player: SimEntity = _player(sim)
assert_true(player.position.z > 16.5 and player.position.z < 20.0, "should have walked up and along, z=%f" % player.position.z)
assert_near(player.position.y, Tuning.GRID_CELL_METRES + Tuning.FLOOR_THICKNESS, 0.05, "standing on the upper floor")
func test_walls_meet_at_corners_without_gaps() -> void:
var g: float = Tuning.GRID_CELL_METRES
var x_wall: AABB = StructureSet.boxes_for(W, Vector4i(1, 0, 0, StructureSet.SLOT_WALL_X), 0)[0]
var z_wall: AABB = StructureSet.boxes_for(W, Vector4i(0, 0, 0, StructureSet.SLOT_WALL_Z), 0)[0]
assert_true(x_wall.intersects(z_wall), "corner walls should overlap")
assert_true(x_wall.has_point(Vector3(g, 1.0, -Tuning.WALL_THICKNESS * 0.49)))
func test_walls_on_low_ground_get_a_skirt() -> void:
# A slope: terrain height rises 1 m per 4 m along x.
var values: PackedFloat32Array = PackedFloat32Array()
var side: int = 65
for z: int in side:
for x: int in side:
values.append(x * 4.0)
var ground: SimGround = SimGround.from_heights(values, side, 16.0)
var sim: Simulation = Simulation.new(3, ground)
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(10.0, ground.terrain_height(10.0, 10.0), 10.0)
player.materials[0] = 100
_look(player, 0.0)
var at: Building.Target = Building.target(player, W)
assert_eq(Building.place(sim, player, at, 0), Building.Result.OK)
var piece: StructureSet.Piece = sim.structures.find(1)
assert_eq(piece.boxes.size(), 2, "the wall should get a skirt")
var skirt: AABB = piece.boxes[1]
assert_true(skirt.position.y <= ground.terrain_height(8.0, 12.0), "the skirt reaches the ground")
func test_rapid_placement_stays_within_the_tick_budget() -> void:
# 24 builders, each holding fire in build mode and turning, for three seconds.
var sim: Simulation = Simulation.new(3, SimGround.flat())
for index: int in 24:
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(40.0 + (index % 6) * 40.0, 0.0, 40.0 + (index / 6) * 40.0)
for code: int in 3:
player.materials[code] = Tuning.MATERIAL_CAP
var kinds: Array[int] = [W, F, R, W, BW, ROOF]
var times: Array[int] = []
var placed: int = 0
for _tick: int in 90:
for id: int in range(1, 25):
var turn: float = float((sim.tick / Tuning.BUILD_INTERVAL_TICKS) % 4) * 90.0
var pitch: float = 50.0 if (sim.tick / 12) % 2 == 1 else 0.0
var kind: int = kinds[(sim.tick / Tuning.BUILD_INTERVAL_TICKS + id) % kinds.size()]
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, deg_to_rad(turn), deg_to_rad(pitch),
PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, id % 3))
var started: int = Time.get_ticks_usec()
sim.step()
times.append(Time.get_ticks_usec() - started)
for event: Dictionary in sim.events:
if event["type"] == "piece_placed":
placed += 1
times.sort()
var p95: int = times[int(times.size() * 0.95)]
print("rapid placement: %d pieces in 3 s by 24 builders, tick p50 %d us p95 %d us max %d us" % [placed, times[45], p95, times[-1]])
assert_true(placed >= 24 * 10, "builders should place many pieces, placed %d" % placed)
assert_true(p95 < 12000, "tick p95 %d us is over the 12 ms budget" % p95)
# One builder can't beat 10 per second.
var solo: Simulation = _sim(Vector3(10.0, 0.0, 10.0), Tuning.MATERIAL_CAP)
var solo_placed: int = 0
for tick: int in 30:
var turn: float = float(tick % 4) * 90.0
var kind: int = [W, F, R][tick % 3]
solo.submit_command(1, PlayerCommand.from_input(solo.tick, Vector2.ZERO, deg_to_rad(turn), 0.0,
PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, 0))
solo.step()
solo_placed += solo.events.filter(func(e: Dictionary) -> bool: return e["type"] == "piece_placed").size()
assert_true(solo_placed <= 10, "no more than 10 pieces a second, placed %d" % solo_placed)
func test_building_is_deterministic_and_hashed() -> void:
var hashes: Array[String] = []
for _run: int in 2:
var sim: Simulation = _sim()
_build(sim, W, 1)
_build(sim, R, 2, 4, 90.0)
hashes.append(sim.state_hash())
assert_eq(hashes[0], hashes[1])
var other: Simulation = _sim()
_build(other, W, 0)
_build(other, R, 2, 4, 90.0)
assert_ne(other.state_hash(), hashes[0], "the material is part of the hash")
+1
View File
@@ -0,0 +1 @@
uid://bbpwsvc84egyd