OCR: 'ref: value' clue grammar, 16-point compass, grid-only coords, wedge overlay

New shapes, all genuinely new features (checked full git history, none
of this ever existed before):

- '<ref>: <value>' clue grammar (Spotter#2: 4.04km / Spotter#3: 298deg
  / Spotter#1: West), no keyword, no 'from', reference comes first.
  This collided hard with the existing 'Type#id:' header shape,
  'Spotter#2: 4.04km' is structurally identical to a real header like
  'AmmoCache#3:', so it was hijacking the block before any clues could
  attach to the entity above it. Fixed by checking whether a would-be
  header's trailing content is itself just a bare clue reading with
  nothing else (_BARE_CLUE_VALUE_RE); a real header's never is.

- 16-point compass words ('North Northwest'), alongside the existing
  8-point ones, longest-alternative-first in the regex so the compound
  form doesn't get cut off at the bare first word.

- A compass word names a whole sector, not a single ray, so a clue
  built from one now carries a bearing_tolerance_deg (11.25deg, half a
  16-point sector) and solve_location() deliberately never tries to
  triangulate it into an exact point, precise math on an imprecise
  reading would misrepresent the confidence. The map draws it as a
  wedge (two bounding rays + fill) instead of a single ray.

- 'Reported active in grid D10': large-grid-cell-only, no sub-grid x:y
  at all, defaults to the cell's rough middle (5:5).

Verified against the exact reported example end to end (parse ->
solver correctly resolving what it can and leaving the rest
unresolved -> map draw with the wedge overlay) plus the full existing
regression sweep across every previously-added format.
This commit is contained in:
2026-08-09 18:15:27 +02:00
parent 90a26b3f85
commit 9c7588eb12
4 changed files with 145 additions and 13 deletions
+96 -11
View File
@@ -195,6 +195,26 @@ def _extract_requested_time(text: str) -> str | None:
return m.group(1) if m else None
# "Reported active in grid D10": only the large-grid cell, no sub-grid
# x:y at all, unlike every other coord shape in this file. Tried last
# (after _extract_grid_coord, which requires the full x:y and so is
# strictly more precise when both would otherwise match). No sub-
# position is given, so it defaults to the cell's rough middle (5:5,
# there's no exact center on a 0-9 grid) rather than leaving it unset.
_LARGE_GRID_ONLY_RE = re.compile(rf"grid\s+([A-T])\s*({_DIGIT_CLASS}{{1,2}})\b", re.IGNORECASE)
def _extract_large_grid_only_coord(text: str) -> Coord | None:
m = _LARGE_GRID_ONLY_RE.search(text)
if not m:
return None
letter, y = m.groups()
try:
return Coord(X=letter.upper(), Y=int(_fix_digits(y)), x=5, y=5)
except ValueError:
return None
def _extract_coord(line: str) -> Coord | None:
m = _COORD_RE.search(line)
if not m:
@@ -449,13 +469,58 @@ _CLUE_BEARING_RE = re.compile(rf"Bearing\s*(\d{{1,3}})\s*°?{_GAP}from\s+(\S+)",
_COMPASS_BEARINGS = {
"NORTH": 0.0, "NORTHEAST": 45.0, "EAST": 90.0, "SOUTHEAST": 135.0,
"SOUTH": 180.0, "SOUTHWEST": 225.0, "WEST": 270.0, "NORTHWEST": 315.0,
# 16-point compass, only ever seen written with a space rather than a
# hyphen ('North Northwest', not 'North-Northwest'), unlike the
# 8-point diagonals above.
"NORTHNORTHEAST": 22.5, "EASTNORTHEAST": 67.5, "EASTSOUTHEAST": 112.5, "SOUTHSOUTHEAST": 157.5,
"SOUTHSOUTHWEST": 202.5, "WESTSOUTHWEST": 247.5, "WESTNORTHWEST": 292.5, "NORTHNORTHWEST": 337.5,
}
_COMPASS_WORD_RE = r"(North(?:[-\s]?East|[-\s]?West)?|South(?:[-\s]?East|[-\s]?West)?|East|West)"
# Longest/most specific alternatives first: alternation tries each in
# order and stops at the first that matches, so 'North Northwest' must
# reach the 16-point alternative before the bare 'North' one, or the
# latter would win and strand ' Northwest' unmatched.
_COMPASS_WORD_RE = (
r"(North[-\s]?North[-\s]?East|North[-\s]?East|East[-\s]?North[-\s]?East|"
r"South[-\s]?South[-\s]?East|South[-\s]?East|East[-\s]?South[-\s]?East|"
r"South[-\s]?South[-\s]?West|South[-\s]?West|West[-\s]?South[-\s]?West|"
r"North[-\s]?North[-\s]?West|North[-\s]?West|West[-\s]?North[-\s]?West|"
r"North|South|East|West)"
)
# Half the width of one 16-point compass sector (360/16 = 22.5 deg each),
# a compass WORD names a whole sector, not a single ray.
_COMPASS_TOLERANCE_DEG = 11.25
def _compass_to_bearing(word: str) -> float:
return _COMPASS_BEARINGS[re.sub(r"[-\s]", "", word).upper()]
_CLUE_DISTANCE_COMPASS_RE = re.compile(
rf"Distance\s*([\d.]+)\s*k?m?\s*{_COMPASS_WORD_RE}{_GAP}from\s+(\S+)", re.IGNORECASE
)
_CLUE_DISTANCE_RE = re.compile(rf"Distance\s*([\d.]+)\s*k?m?{_GAP}from\s+(\S+)", re.IGNORECASE)
# A different clue grammar entirely, reference FIRST then a colon then
# the bare reading, no 'Bearing'/'Distance' keyword and no 'from' at all:
# Spotter#2: 4.04km
# Spotter#3: 298°
# Spotter#1: West
# Distance tried before bearing/compass, same reasoning as _CLUE_PATTERNS
# generally: an ambiguous run of digits could otherwise let the bearing
# pattern's optional '°' swallow part of a distance reading.
_CLUE_REF_DISTANCE_RE = re.compile(rf"(\S+)\s*:\s*([\d.]+)\s*k?m", re.IGNORECASE)
_CLUE_REF_BEARING_RE = re.compile(rf"(\S+)\s*:\s*({_DIGIT_CLASS}{{1,3}})\s*°", re.IGNORECASE)
_CLUE_REF_COMPASS_RE = re.compile(rf"(\S+)\s*:\s*{_COMPASS_WORD_RE}\b", re.IGNORECASE)
# 'Spotter#2: 4.04km' has the exact same 'Word#digits:' shape as a real
# block header ('AmmoCache#3:'), genuinely indistinguishable from one by
# shape alone. The deciding signal is what follows the colon: a real
# header's trailing content is never JUST a bare clue reading with
# nothing else, so _NAMED_HEADER_RE's own inline-content group gets
# checked against this before deciding it's actually a new header.
_BARE_CLUE_VALUE_RE = re.compile(
rf"^(?:[\d.]+\s*k?m|{_DIGIT_CLASS}{{1,3}}\s*°|{_COMPASS_WORD_RE})\s*$", re.IGNORECASE
)
_TYPE_BY_SHORT = {t.short: t for t in TargetType}
# The game's typewriter has used "AmmoCache" for what's now modeled as
# SupplyCache, treat it as the same type rather than dropping the target.
@@ -497,15 +562,23 @@ def _clean_reference(raw: str) -> str:
# becomes one clue, not a spurious extra bearing-only one from the same
# text. `\s` already matches a literal newline, so this bridges an OCR
# line-wrap ("...Bearing 125°" / "from Spotter#1" split across two lines)
# without needing the caller to rejoin anything.
# without needing the caller to rejoin anything. Extract functions return
# (bearing_deg, distance_km, reference, bearing_tolerance_deg), the last
# one None except for the compass-word shapes.
_CLUE_PATTERNS = (
(_CLUE_COMBINED_RE, lambda m: (float(m.group(1)), float(m.group(2)), m.group(3))),
(_CLUE_INLINE_RE, lambda m: (float(m.group(1)), float(m.group(2)), m.group(3))),
(_CLUE_BEARING_RE, lambda m: (float(m.group(1)), None, m.group(2))),
(_CLUE_COMBINED_RE, lambda m: (float(m.group(1)), float(m.group(2)), m.group(3), None)),
(_CLUE_INLINE_RE, lambda m: (float(m.group(1)), float(m.group(2)), m.group(3), None)),
(_CLUE_BEARING_RE, lambda m: (float(m.group(1)), None, m.group(2), None)),
(_CLUE_DISTANCE_COMPASS_RE, lambda m: (
_COMPASS_BEARINGS[re.sub(r"[-\s]", "", m.group(2)).upper()], float(m.group(1)), m.group(3)
_compass_to_bearing(m.group(2)), float(m.group(1)), m.group(3), None
)),
(_CLUE_DISTANCE_RE, lambda m: (None, float(m.group(1)), m.group(2), None)),
# reference-first shape: '<ref>: <value>', no keyword, no 'from'
(_CLUE_REF_DISTANCE_RE, lambda m: (None, float(m.group(2)), m.group(1), None)),
(_CLUE_REF_BEARING_RE, lambda m: (float(_fix_digits(m.group(2))), None, m.group(1), None)),
(_CLUE_REF_COMPASS_RE, lambda m: (
_compass_to_bearing(m.group(2)), None, m.group(1), _COMPASS_TOLERANCE_DEG
)),
(_CLUE_DISTANCE_RE, lambda m: (None, float(m.group(1)), m.group(2))),
)
@@ -525,8 +598,9 @@ def _parse_all_clues(text: str) -> list[Clue]:
if overlaps(span):
continue
consumed.append(span)
bearing, distance, ref = extract(m)
clues.append(Clue(reference=_clean_reference(ref), bearing_deg=bearing, distance_km=distance))
bearing, distance, ref, tolerance = extract(m)
clues.append(Clue(reference=_clean_reference(ref), bearing_deg=bearing, distance_km=distance,
bearing_tolerance_deg=tolerance))
return clues
@@ -575,7 +649,10 @@ def parse_intel_blocks(text: str) -> list[dict]:
if current is not None:
joined = "\n".join(current["raw"])
current["clues"] = _parse_all_clues(joined)
current["coord"] = _extract_grid_coord(joined) or _extract_requested_on_coord(joined)
current["coord"] = (
_extract_grid_coord(joined) or _extract_requested_on_coord(joined)
or _extract_large_grid_only_coord(joined)
)
current["shell"] = _extract_shell_request(joined)
current["requested_time"] = _extract_requested_time(joined)
if (current["clues"] or current["coord"] is not None
@@ -632,8 +709,16 @@ def parse_intel_blocks(text: str) -> list[dict]:
named_m = next((m for c in candidates if (m := _NAMED_HEADER_RE.match(c))), None)
if named_m:
flush()
type_word, num, inline = named_m.groups()
if _BARE_CLUE_VALUE_RE.match(inline.strip()):
# Not actually a header, a '<ref>: <value>' clue line
# for whatever block is already open (see
# _BARE_CLUE_VALUE_RE), append rather than start a new
# block over it.
if current is not None:
current["raw"].append(line)
continue
flush()
num = _fix_id_digits(num)
current = {"kind": "named", "name": f"{type_word}#{num}", "type_word": type_word,
"id": num, "raw": [line], "clues": []}