Grounding: a saved land contact with the sampled hull-point normal loads

A hull point turning onto a slanted coast reports the normal sampled a
quarter hull width around it (axis steps, often diagonal), while the save
check demanded the nearest edge's normal, so a frigate aground there could
not be loaded (nightly soak, s8_meerenge). The check now accepts every normal
a detector gives on the coast and still refuses one pointing into the land.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtppCtPn57jRcgnW6cBnFR
This commit is contained in:
Claude committed 2026-10-07 11:13:30 +00:00
1 parent fb5d252b83
commit 8f8daebe84
2 files changed
+77 -21

No files matched your search

+51 -21
View File
@@ -215,6 +215,55 @@ def _land_normal(world, x, y):
return first
def _sampled_land_normal(world, x, y, hull):
"""Outward normal of a hull point on land, from the land samples a
quarter hull width to each side (axis steps, so often diagonal)."""
delta = max(min(hull.beam_m, hull.length_m) / METRES_PER_NM / 4.0, 1e-5)
nx = float(world.on_land(x - delta, y)) - float(world.on_land(x + delta, y))
ny = float(world.on_land(x, y - delta)) - float(world.on_land(x, y + delta))
length = math.hypot(nx, ny)
if length:
return nx / length, ny / length
return _land_normal(world, x, y)
def _land_contact_normal_ok(world, contact, hull, tolerance_nm) -> bool:
"""Whether a saved land contact lies on the coast with a normal one of
the detectors gives there: the nearest edge's (``_land_normal``), the
sampled one of a hull point on land, a crossed edge's (either side,
``first_physical_land_intersection``) or, at a coast vertex entering the
hull, any pointing off the land (the sweep uses the opposite of the
motion)."""
point = (contact.x_nm, contact.y_nm)
normal = (contact.normal_x, contact.normal_y)
probe = 1e-7
outward = not world.on_land(point[0] + normal[0] * probe,
point[1] + normal[1] * probe)
on_coast = False
for landmass in world.coast.landmasses:
count = len(landmass.points)
for index, first in enumerate(landmass.points):
second = landmass.points[(index + 1) % count]
if _point_segment_distance(point, first, second) > tolerance_nm:
continue
on_coast = True
if not outward:
continue
if (math.hypot(point[0] - first[0], point[1] - first[1]) <= tolerance_nm
or math.hypot(point[0] - second[0],
point[1] - second[1]) <= tolerance_nm):
return True
ex, ey = second[0] - first[0], second[1] - first[1]
length = math.hypot(ex, ey)
if length and abs(-ey * normal[0] + ex * normal[1]) / length >= .999999:
return True
if not on_coast:
return False
return any(normal[0] * expected[0] + normal[1] * expected[1] >= .999999
for expected in (_land_normal(world, *point),
_sampled_land_normal(world, *point, hull)))
def _turning_region(world, pose, t0, t1, hull):
first = pose(t0)
second = pose(t1)
@@ -373,17 +422,7 @@ def _find_contact(world, x_nm: float, y_nm: float, course_deg: float,
if world.on_land(px, py):
if not detail:
return _BLOCKED
delta = max(min(hull.beam_m, hull.length_m) / METRES_PER_NM / 4.0,
1e-5)
nx = float(world.on_land(px - delta, py)) - float(
world.on_land(px + delta, py))
ny = float(world.on_land(px, py - delta)) - float(
world.on_land(px, py + delta))
length = math.hypot(nx, ny)
if length:
nx, ny = nx / length, ny / length
else:
nx, ny = _land_normal(world, px, py)
nx, ny = _sampled_land_normal(world, px, py, hull)
return GroundingContact("land", px, py, nx, ny,
longitudinal, lateral)
corners = (points[0], points[2], points[8], points[6])
@@ -497,16 +536,7 @@ def grounding_contact_is_consistent(world, pose, hull, contact) -> bool:
and 0.0 <= contact.y_nm <= world.size_nm):
return False
if contact.kind == "land":
nearest = float("inf")
for landmass in world.coast.landmasses:
for index, first in enumerate(landmass.points):
second = landmass.points[(index + 1) % len(landmass.points)]
nearest = min(nearest, _point_segment_distance(
(contact.x_nm, contact.y_nm), first, second))
expected_normal = _land_normal(world, contact.x_nm, contact.y_nm)
normal_alignment = (contact.normal_x * expected_normal[0]
+ contact.normal_y * expected_normal[1])
return nearest <= tolerance_nm and normal_alignment >= .999999
return _land_contact_normal_ok(world, contact, hull, tolerance_nm)
if contact.kind == "shallow":
return (world.coast.has_bathymetry
and not world.on_land(contact.x_nm, contact.y_nm)
+26
View File
@@ -243,6 +243,32 @@ def test_astern_recovery_cannot_cross_a_reverse_obstacle():
assert ship.x < .24
def test_turning_into_a_slanted_coast_saves_a_consistent_contact():
# A hull point that turns onto land reports the sampled (often diagonal)
# normal; the save check demanded the nearest edge's, so a frigate
# aground on a slanted coast could not be loaded (nightly soak, s8).
slope = {"name": "slope", "nation": "ZIVIL",
"points": [[1.0, .2], [1.6, .2], [1.6, 1.8], [1.25, 1.8]]}
world = _world(landmasses=[slope])
hull = HullSpec(length_m=120.0, beam_m=15.0)
checked = 0
for course in range(60, 121, 5):
for turn in (0, 3, -3, 10):
result = swept_grounding(world, (.6, 1.0, course),
(1.5, 1.0, course + turn), hull)
assert result.contacted and result.contact.kind == "land"
pose = (result.safe_x_nm, result.safe_y_nm, result.safe_course_deg)
assert grounding_contact_is_consistent(world, pose, hull,
result.contact), (course, turn)
# A normal into the land is still refused.
assert not grounding_contact_is_consistent(
world, pose, hull,
replace(result.contact, normal_x=-result.contact.normal_x,
normal_y=-result.contact.normal_y))
checked += 1
assert checked == 52
def test_contact_save_split_recovery_continuation_is_deterministic():
game = Game(seed=919, start_menu=False, audio_enabled=False)
# W2: match the swapped world's own seed to the game's, exactly like