## Pattern-matching gun: searches movement history for a matching sequence, ## then plays it forward to predict future position. ## Reference: https://robowiki.net/wiki/Pattern_Matching ## Coordinate system: 0° = East, CCW positive (Tank Royale standard). import std/math import gun_harness/gun_interface const HistorySize* = 500 PatternLen* = 10 # ticks used as search key; ponytail: fixed, expose if tuning needed type MoveTick = object velocity: float ## signed speed (px/tick) headingDelta: float ## heading change in radians this tick PatternMatcherGun* = object buf: array[HistorySize, MoveTick] head: int ## next write index (circular) count: int ## filled entries (capped at HistorySize) prevHeading: float prevSpeed: float prevTick: int hasPrev: bool # Per-tick, speed-INDEPENDENT pattern state. Both the history search and the # replayed enemy path depend only on observed movement, never on bulletSpeed, # so they are built at most once per tick. The bullet lead (number of replay # steps + coast) is derived from this path on every call. cacheValid: bool cacheTick: int bestMatch: int ## -1 = no usable match (linear fallback) playStart: int playAvail: int pathX: array[HistorySize + 1, float] pathY: array[HistorySize + 1, float] pathHeading: array[HistorySize + 1, float] ## radians after s steps pathSpeed: array[HistorySize + 1, float] ## speed after s steps debugGraphics*: bool # --- circular buffer helpers --- proc write(g: var PatternMatcherGun, m: MoveTick) {.inline.} = g.buf[g.head] = m g.head = (g.head + 1) mod HistorySize if g.count < HistorySize: inc g.count proc readAt(g: PatternMatcherGun, i: int): MoveTick {.inline.} = ## i = 0 is oldest, i = count-1 is newest g.buf[(g.head - g.count + i + HistorySize * 2) mod HistorySize] # --- linear fallback (same style as linear.nim) --- proc linearPredict(state: WorldState, bulletSpeed: float): (float, float) = let dist = hypot(state.enemyX - state.selfX, state.enemyY - state.selfY) let t = dist / bulletSpeed let hRad = degToRad(state.enemyHeading) let ex = clamp(state.enemyX + cos(hRad) * state.enemySpeed * t, BotRadius, state.arenaWidth - BotRadius) let ey = clamp(state.enemyY + sin(hRad) * state.enemySpeed * t, BotRadius, state.arenaHeight - BotRadius) (ex, ey) # --- pattern search + play-forward --- proc findBestMatch(g: PatternMatcherGun): int = ## Speed-independent history search. Returns the start index of the best ## matching pattern, or -1 when there is not enough history. if g.count < PatternLen * 2: return -1 # key = last PatternLen entries let keyStart = g.count - PatternLen # scan backwards for best match (exclude the key itself) var bestScore = Inf var bestMatch = -1 let scanEnd = g.count - PatternLen - 1 # last valid match start for i in countdown(scanEnd, 0): var score = 0.0 for k in 0 ..< PatternLen: let a = g.readAt(keyStart + k) let b = g.readAt(i + k) let dv = a.velocity - b.velocity let dh = a.headingDelta - b.headingDelta score += dv * dv + dh * dh if score < bestScore: bestScore = score bestMatch = i bestMatch proc buildPath(g: var PatternMatcherGun, state: WorldState, bestMatch: int) = ## Precompute the matched pattern replayed forward from the current state. ## Only depends on observed movement, so it is valid for every power bin. g.playStart = bestMatch + PatternLen g.playAvail = g.count - 1 - g.playStart # ticks we can replay g.pathX[0] = state.enemyX g.pathY[0] = state.enemyY g.pathHeading[0] = degToRad(state.enemyHeading) g.pathSpeed[0] = state.enemySpeed for s in 1 .. g.playAvail: let m = g.readAt(g.playStart + s - 1) g.pathHeading[s] = g.pathHeading[s - 1] + m.headingDelta g.pathSpeed[s] = m.velocity g.pathX[s] = g.pathX[s - 1] + cos(g.pathHeading[s]) * g.pathSpeed[s] g.pathY[s] = g.pathY[s - 1] + sin(g.pathHeading[s]) * g.pathSpeed[s] proc projectFromPath(g: PatternMatcherGun, state: WorldState, bulletSpeed: float): (float, float) = ## Speed-dependent lead: walk the cached path as far as this bulletSpeed's ## estimated flight time reaches, then coast linearly for the remainder. # iterative time estimate let dist0 = hypot(state.enemyX - state.selfX, state.enemyY - state.selfY) var t = dist0 / bulletSpeed var ex = state.enemyX var ey = state.enemyY for _ in 0..4: let steps = min(int(t + 0.5), g.playAvail) ex = g.pathX[steps] ey = g.pathY[steps] # if we ran out of replay data, coast linearly from last simulated pos let remaining = t - steps.float if remaining > 0.0: ex += cos(g.pathHeading[steps]) * g.pathSpeed[steps] * remaining ey += sin(g.pathHeading[steps]) * g.pathSpeed[steps] * remaining let ndx = ex - state.selfX let ndy = ey - state.selfY t = sqrt(ndx * ndx + ndy * ndy) / bulletSpeed ex = clamp(ex, BotRadius, state.arenaWidth - BotRadius) ey = clamp(ey, BotRadius, state.arenaHeight - BotRadius) (ex, ey) # --- Gun interface --- proc predict*(g: var PatternMatcherGun, state: WorldState, bulletSpeed: float): GunPrediction = if bulletSpeed <= 0.0: return GunPrediction(x: state.enemyX, y: state.enemyY) # Roll history forward and (re)build the speed-independent pattern path at # most once per tick. The old code returned a single cached (x, y) per tick, # so all four power bins shared bin 0's lead. if not g.cacheValid or state.tick != g.cacheTick: if g.hasPrev: var dh = degToRad(state.enemyHeading) - degToRad(g.prevHeading) # wrap to [-π, π] while dh > PI: dh -= 2.0 * PI while dh < -PI: dh += 2.0 * PI g.write(MoveTick(velocity: g.prevSpeed, headingDelta: dh)) g.prevHeading = state.enemyHeading g.prevSpeed = state.enemySpeed g.prevTick = state.tick g.hasPrev = true g.cacheValid = true g.cacheTick = state.tick g.bestMatch = g.findBestMatch() if g.bestMatch >= 0: g.buildPath(state, g.bestMatch) if g.bestMatch < 0: let (px, py) = linearPredict(state, bulletSpeed) return GunPrediction(x: px, y: py) let (px, py) = g.projectFromPath(state, bulletSpeed) GunPrediction(x: px, y: py) proc onResult*(g: var PatternMatcherGun, e: FeedbackEvent) = discard # pattern matcher learns from movement observation, not feedback