P2 stone: stonecutter, cut stone, stone floors/walls/pillars/stairs/windows/doorway, hearth; wooden stairs, windows, posts, palisade and gate; stone support rules

This commit is contained in:
Paul Bailey
2026-11-05 01:14:07 +00:00
parent 24c280d885
commit 411bfef34a
16 changed files with 1123 additions and 21 deletions

View File

@@ -94,21 +94,35 @@ func _grounded(points: Array) -> bool:
return true
return false
static func _loss(from_center: Vector3, to_center: Vector3) -> float:
const STONE_VERTICAL_LOSS := 0.05 ## stone stacks tall (P2)
## Support lost passing from a piece centred at `from_center` to one of
## `material` centred at `to_center`. Through a pillar (`via_pillar`: either
## piece is one) any real step up or down counts as vertical: a pillar under a
## floor's corner carries it straight down to the ground.
static func _loss(from_center: Vector3, to_center: Vector3, material: String = "wood", via_pillar: bool = false) -> float:
var d := to_center - from_center
var horiz := Vector2(d.x, d.z).length()
return VERTICAL_LOSS if absf(d.y) >= horiz * 0.99 else HORIZONTAL_LOSS
if absf(d.y) >= horiz * 0.99 or (via_pillar and absf(d.y) > 0.5):
return STONE_VERTICAL_LOSS if material == "stone" else VERTICAL_LOSS
return HORIZONTAL_LOSS
## Support a piece of `kind` would have at `xf`, given the current structure.
func support_for(kind: String, xf: Transform3D, exclude: Node = null) -> float:
## Wood can't hold stone up (stone on wood has no support from it).
static func _carries(below: Node, material: String) -> bool:
return not (material == "stone" and below.material == "wood")
## Support a piece of `kind` (and `material`) would have at `xf`.
func support_for(kind: String, xf: Transform3D, exclude: Node = null, material: String = "wood") -> float:
var pts := _world_snaps(kind, xf)
if _grounded(pts):
return 1.0
var center: Vector3 = xf * BuildPiece.kinds()[kind].center
var best := 0.0
for n in _touching(pts, exclude):
if not _carries(n, material):
continue
var nc: Vector3 = n.global_transform * BuildPiece.kinds()[n.kind].center
best = maxf(best, n.support * (1.0 - _loss(nc, center)))
best = maxf(best, n.support * (1.0 - _loss(nc, center, material, kind == "pillar" or n.kind == "pillar")))
return best
## Recomputes every piece's support from the ground up. Returns the pieces now
@@ -132,7 +146,8 @@ func recompute() -> Array:
continue
var best := 0.0
for n in nbrs[p]:
best = maxf(best, n.support * (1.0 - _loss(centers[n], centers[p])))
if _carries(n, p.material):
best = maxf(best, n.support * (1.0 - _loss(centers[n], centers[p], p.material, p.kind == "pillar" or n.kind == "pillar")))
if best > p.support + 0.0001:
p.support = best
changed = true
@@ -170,10 +185,19 @@ func check(def: Dictionary, xf: Transform3D, exclude: Node = null) -> String:
if not hits.is_empty():
last_blocker = hits[0].collider
return "Something's in the way"
if support_for(kind, xf, exclude) < MIN_SUPPORT:
var mat := BuildPiece.material_of(def)
if support_for(kind, xf, exclude, mat) < MIN_SUPPORT:
if mat == "stone" and _rests_on_wood(kind, xf, exclude):
return "Stone needs stone or the ground under it (wood can't hold it)"
return "Not enough support"
return ""
func _rests_on_wood(kind: String, xf: Transform3D, exclude: Node) -> bool:
for n in _touching(_world_snaps(kind, xf), exclude):
if n.material == "wood":
return true
return false
## Where a building piece goes when the player aims at `hit` (a ray result
## dictionary: position, normal, collider). `yaw` is the player's preferred
## rotation. Returns {xf, ok, reason, snapped}.
@@ -246,6 +270,8 @@ func _free_xf(kind: String, hp: Vector3, yaw: float, on_terrain: bool) -> Transf
match kind:
"floor":
probe = [Vector3(-1, 0, -1), Vector3(1, 0, -1), Vector3(-1, 0, 1), Vector3(1, 0, 1), Vector3.ZERO]
"pillar":
probe = [Vector3.ZERO]
_:
probe = [Vector3(-1, 0, 0), Vector3.ZERO, Vector3(1, 0, 0)]
var hs: Array[float] = []
@@ -271,9 +297,13 @@ func roofed_at(pos: Vector3) -> bool:
hits += 1
return hits >= 3
## Height of the sideways shelter rays: under a window's sill (1 m), so a
## room with windows still counts as walled in.
const WALL_RAY_Y := 0.6
## 0..1 fraction of 8 horizontal directions blocked by walls within 8 m.
func enclosure_at(pos: Vector3) -> float:
var from := pos + Vector3(0, 1.2, 0)
var from := pos + Vector3(0, WALL_RAY_Y, 0)
var hits := 0
for i in range(8):
var a := TAU * i / 8.0
@@ -284,6 +314,25 @@ func enclosure_at(pos: Vector3) -> float:
func sheltered_at(pos: Vector3) -> bool:
return roofed_at(pos) and enclosure_at(pos) >= 0.74
## 0..1 fraction of 8 horizontal directions whose nearest wall (within 8 m)
## is stone. A stone room keeps you a little warmer (survival.gd).
func stone_walls_at(pos: Vector3) -> float:
var from := pos + Vector3(0, WALL_RAY_Y, 0)
var space := world.get_world_3d().direct_space_state
var hits := 0
for i in range(8):
var a := TAU * i / 8.0
var q := PhysicsRayQueryParameters3D.create(from, from + Vector3(cos(a), 0, sin(a)) * 8.0, BuildPiece.BUILD_LAYER)
var hit := space.intersect_ray(q)
if hit.is_empty():
continue
var c: Object = hit.collider
if c != null and c.has_meta("owner_piece"):
c = c.get_meta("owner_piece")
if c != null and c.get("material") == "stone":
hits += 1
return hits / 8.0
# --- dev helper ---------------------------------------------------------------
## Builds a 4 x 4 m hut through the same snapping a player uses, aiming at the
@@ -291,24 +340,29 @@ func sheltered_at(pos: Vector3) -> bool:
## in hut space), 4 roofs meeting at a ridge, 4 gables. Used by the tests and
## the screenshot views. A coroutine (await it): like a player, it lets a
## physics step pass after each piece so the next overlap check sees it.
## `mat` "stone" builds the floors and walls in stone (a stone doorway, a
## wooden roof on top).
## Returns {"placed": n, "failed": [...], "pieces": [...]}.
func debug_build_hut(center: Vector3, yaw: float) -> Dictionary:
func debug_build_hut(center: Vector3, yaw: float, mat: String = "wood") -> Dictionary:
var out := {"placed": 0, "failed": [], "pieces": []}
var floor_id := mat + "_floor"
var wall_id := mat + "_wall"
var door_id := "stone_doorway" if mat == "stone" else "wood_door"
# Floors: the first sits on the ground, the rest snap to it.
var f0: Node3D = await _hut_place(out, center, yaw, "wood_floor", Vector3(-1, 0, -1), 0.0, null)
var f0: Node3D = await _hut_place(out, center, yaw, floor_id, Vector3(-1, 0, -1), 0.0, null)
if f0 == null:
return out
var top: float = f0.global_position.y - center.y
for l in [Vector3(1, 0, -1), Vector3(-1, 0, 1), Vector3(1, 0, 1)]:
await _hut_place(out, center, yaw, "wood_floor", l, 0.0, null)
await _hut_place(out, center, yaw, floor_id, l, 0.0, null)
# Walls: aim at the floor just inside each edge.
var q := PI * 0.5
for x in [-1.0, 1.0]:
await _hut_place(out, center, yaw, "wood_wall", Vector3(x, top, -1.9), 0.0, f0) # north
await _hut_place(out, center, yaw, "wood_door" if x > 0 else "wood_wall", Vector3(x, top, 1.9), 0.0, f0) # south
await _hut_place(out, center, yaw, wall_id, Vector3(x, top, -1.9), 0.0, f0) # north
await _hut_place(out, center, yaw, door_id if x > 0 else wall_id, Vector3(x, top, 1.9), 0.0, f0) # south
for z in [-1.0, 1.0]:
await _hut_place(out, center, yaw, "wood_wall", Vector3(1.9, top, z), q, f0) # east
await _hut_place(out, center, yaw, "wood_wall", Vector3(-1.9, top, z), q, f0) # west
await _hut_place(out, center, yaw, wall_id, Vector3(1.9, top, z), q, f0) # east
await _hut_place(out, center, yaw, wall_id, Vector3(-1.9, top, z), q, f0) # west
# Roofs on the north and south wall tops, rising to a ridge along X.
for x in [-1.0, 1.0]:
await _hut_place(out, center, yaw, "wood_roof", Vector3(x, top + 2.0, 2.0), 0.0, f0)