"""Static connectivity rejection; reachable A* paths remain unchanged.""" def invalidate_navigation(level): level.npc_topology_revision = getattr(level, 'npc_topology_revision', 0) + 1 level.recal_npc_paths = True class NavigationCache: def __init__(self): self.block = None self.signature = None self.labels = None self.builds = 0 self.rejections = 0 def disconnected(self, level, start, target, lower, upper): width, height = level.map_x, level.map_y # Legacy searches can extend outside map bounds; do not prune those. if not all(len(p) == 2 and all(isinstance(v, int) for v in p) and 0 <= p[0] < width and 0 <= p[1] < height for p in (start, target, lower, upper)): return False signature = (width, height, getattr(level, 'npc_topology_revision', 0)) if self.block is not level.block or self.signature != signature: self.block, self.signature = level.block, signature self._build(width, height) if self.labels[start[0]][start[1]] != self.labels[target[0]][target[1]]: self.rejections += 1 return True return False def _build(self, width, height): horizontal, vertical = self.block labels = [[-1] * height for _ in range(width)] component = 0 for x in range(width): for y in range(height): if labels[x][y] != -1: continue labels[x][y] = component pending = [(x, y)] while pending: cx, cy = pending.pop() for nx, ny, edge in ( (cx, cy - 1, horizontal[cx][cy]), (cx + 1, cy, vertical[cx + 1][cy]), (cx, cy + 1, horizontal[cx][cy + 1]), (cx - 1, cy, vertical[cx][cy])): if (edge <= 0 and 0 <= nx < width and 0 <= ny < height and labels[nx][ny] == -1): labels[nx][ny] = component pending.append((nx, ny)) component += 1 self.labels = labels self.builds += 1