From 3c1801b5bbb9bf9c8ef54d1f5a1e42d2ac20a7f8 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:21:34 +0000 Subject: [PATCH 1/3] fix: standing next to a house no longer counts as being inside it - room floors are the floor triangles, not their bounding box A room floor polygon marked every 0.5 m cell of its bounding box, so a slanted or L-shaped floor also took the street next to it. G2 Khorinis: at Harad's forge the hero stood on such cells (68 of 215 around the forge), counted as in the smithy (SCHMIED), and Harad and Brian sent him out. A cell now counts when its center lies in one of the polygon's triangles, at that triangle's height. Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Services/World/RoomService.cs | 75 +++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs b/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs index dfd3f5b85..2e5026634 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs @@ -52,8 +52,10 @@ public static long ToFloorCell(Vector3 position) } /// - /// World load (background thread, only touches floorCells): marks the floor cells a room polygon covers with its - /// sector if it is level - floors (and ceilings, never at the feet). Bounding box of the polygon. + /// World load (background thread, only touches floorCells): marks the floor cells a room polygon covers with + /// its sector if it is level - floors (and ceilings, never at the feet). A cell counts when its center lies in + /// one of the polygon's triangles: the polygon's bounding box also took the street next to a slanted or + /// L-shaped floor (G2 Khorinis: standing at Harad's forge counted as in his house, he sent the hero out). /// public static void MarkRoomFloor(ZenKit.IMesh mesh, int sector, IList positionIndices, Dictionary floorCells) @@ -61,23 +63,68 @@ public static void MarkRoomFloor(ZenKit.IMesh mesh, int sector, IList posit if (positionIndices.Count < 3) return; - var a = mesh.GetPosition(positionIndices[0]); - var b = mesh.GetPosition(positionIndices[1]); - var c = mesh.GetPosition(positionIndices[2]); - var normal = Vector3.Cross(new Vector3(b.X - a.X, b.Y - a.Y, b.Z - a.Z), - new Vector3(c.X - a.X, c.Y - a.Y, c.Z - a.Z)); + var corners = new Vector3[positionIndices.Count]; + for (var i = 0; i < corners.Length; i++) + { + var p = mesh.GetPosition(positionIndices[i]); + corners[i] = new Vector3(p.X, p.Y, p.Z) / 100f; + } + + var normal = Vector3.Cross(corners[1] - corners[0], corners[2] - corners[0]); if (normal.sqrMagnitude <= 0f || Mathf.Abs(normal.normalized.y) <= _floorMinNormalY) return; - var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); - var max = new Vector3(float.MinValue, float.MinValue, float.MinValue); - foreach (var index in positionIndices) + var min = corners[0]; + var max = corners[0]; + foreach (var corner in corners) + { + min = Vector3.Min(min, corner); + max = Vector3.Max(max, corner); + } + + var minX = Mathf.FloorToInt(min.x / FloorCellSize); + var minZ = Mathf.FloorToInt(min.z / FloorCellSize); + var maxX = Mathf.Min(Mathf.FloorToInt(max.x / FloorCellSize), minX + _maxFloorCellsPerAxis); + var maxZ = Mathf.Min(Mathf.FloorToInt(max.z / FloorCellSize), minZ + _maxFloorCellsPerAxis); + for (var x = minX; x <= maxX; x++) { - var p = mesh.GetPosition(index); - var position = new Vector3(p.X, p.Y, p.Z) / 100f; - min = Vector3.Min(min, position); - max = Vector3.Max(max, position); + for (var z = minZ; z <= maxZ; z++) + { + var center = new Vector2((x + 0.5f) * FloorCellSize, (z + 0.5f) * FloorCellSize); + // Polygons are convex fans around their first corner. + for (var i = 1; i + 1 < corners.Length; i++) + { + if (!TryGetHeightInTriangle(center, corners[0], corners[i], corners[i + 1], out var y)) + continue; + + floorCells[ToFloorCell(new Vector3(center.x, y, center.y))] = sector; + break; + } + } } + } + + /// + /// The point (x, z) lies in the triangle seen from above: its height on the triangle's plane. + /// + private static bool TryGetHeightInTriangle(Vector2 point, Vector3 a, Vector3 b, Vector3 c, out float height) + { + height = 0f; + var v0 = new Vector2(b.x - a.x, b.z - a.z); + var v1 = new Vector2(c.x - a.x, c.z - a.z); + var v2 = new Vector2(point.x - a.x, point.y - a.z); + var denominator = v0.x * v1.y - v1.x * v0.y; + if (Mathf.Abs(denominator) < 1e-6f) + return false; + + var u = (v2.x * v1.y - v1.x * v2.y) / denominator; + var v = (v0.x * v2.y - v2.x * v0.y) / denominator; + if (u < 0f || v < 0f || u + v > 1f) + return false; + + height = a.y + u * (b.y - a.y) + v * (c.y - a.y); + return true; + } var steps = Vector3Int.Min(Vector3Int.CeilToInt((max - min) / FloorCellSize), Vector3Int.one * _maxFloorCellsPerAxis); From 57021ff2430bb1736d639aa1e4fd2ddef42aad85 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:56:33 +0000 Subject: [PATCH 2/3] fix: the room floor change compiles - the old bounding box loop was left behind after the new method Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-Core/Scripts/Services/World/RoomService.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs b/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs index 2e5026634..8ad2c7f3c 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs @@ -126,15 +126,6 @@ private static bool TryGetHeightInTriangle(Vector2 point, Vector3 a, Vector3 b, return true; } - var steps = Vector3Int.Min(Vector3Int.CeilToInt((max - min) / FloorCellSize), - Vector3Int.one * _maxFloorCellsPerAxis); - for (var x = 0; x <= steps.x; x++) - for (var y = 0; y <= steps.y; y++) - for (var z = 0; z <= steps.z; z++) - floorCells[ToFloorCell(min + Vector3.Min(new Vector3(x, y, z) * FloorCellSize, max - min))] = - sector; - } - /// /// Floor cells (ToFloorCell) of the rooms and their sector. Set by the world mesh builder on every world load. /// From 28d9178c9a28bc46703df25c6e545ae52a0978e1 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 22:46:07 +0000 Subject: [PATCH 3/3] fix: sloped room floors mark the height buckets above and below too - a standing point on a slope no longer falls between the probes Co-Authored-By: Claude Sonnet 5.5 --- .../Gothic-Core/Scripts/Services/World/RoomService.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs b/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs index 8ad2c7f3c..4f94b2ee6 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/RoomService.cs @@ -25,6 +25,8 @@ public class RoomService // A room floor polygon: its normal at most ~45 degrees from up/down. private const float _floorMinNormalY = 0.7f; + // Below this normal.y a floor triangle is sloped: its cells also mark the buckets above and below. + private const float _levelFloorNormalY = 0.98f; // Huge floor polygons are marked up to 20 m per axis. private const int _maxFloorCellsPerAxis = 40; @@ -74,6 +76,9 @@ public static void MarkRoomFloor(ZenKit.IMesh mesh, int sector, IList posit if (normal.sqrMagnitude <= 0f || Mathf.Abs(normal.normalized.y) <= _floorMinNormalY) return; + // A slope changes the height within a cell: a standing point can land in the bucket above or below. + var isSloped = Mathf.Abs(normal.normalized.y) < _levelFloorNormalY; + var min = corners[0]; var max = corners[0]; foreach (var corner in corners) @@ -98,6 +103,11 @@ public static void MarkRoomFloor(ZenKit.IMesh mesh, int sector, IList posit continue; floorCells[ToFloorCell(new Vector3(center.x, y, center.y))] = sector; + if (isSloped) + { + floorCells[ToFloorCell(new Vector3(center.x, y + FloorCellSize, center.y))] = sector; + floorCells[ToFloorCell(new Vector3(center.x, y - FloorCellSize, center.y))] = sector; + } break; } }