mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-04 16:22:48 +08:00
fix(clearance): generate octal squawks and skip reserved codes
genSquawk() drew a decimal number in 1000-8999, so clearances could contain the digits 8 or 9 — codes no transponder can dial. Replace it with a shared generateSquawk() that draws four octal digits and re-rolls the reserved codes (7500/7600/7700, 7000, 2000, 1200, 0000). shared/learn/scenario.ts had its own octal generator that could still draw an emergency code; it now uses the same helper. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import type {
|
||||
} from '../types/decision'
|
||||
import type { FlowActivationInstruction, FlowActivationMode, LLMDecisionTrace } from '../types/llm'
|
||||
import { normalizeRadioPhrase, DEFAULT_AIRLINE_TELEPHONY } from './radioSpeech'
|
||||
import { generateSquawk } from './transponder'
|
||||
|
||||
// --- DecisionTree runtime types ---
|
||||
type Role = 'pilot' | 'atc' | 'system'
|
||||
@@ -783,7 +784,7 @@ export default function useCommunicationsEngine() {
|
||||
dest: fpl.arr || fpl.arrival || 'EDDM',
|
||||
stand: genStand(),
|
||||
runway: genRunway(),
|
||||
squawk: fpl.assignedsquawk || genSquawk(),
|
||||
squawk: fpl.assignedsquawk || generateSquawk(),
|
||||
atis_code: genATIS(),
|
||||
sid: genSID(fpl.route || ''),
|
||||
transition: 'DCT',
|
||||
@@ -1509,10 +1510,6 @@ export default function useCommunicationsEngine() {
|
||||
return arr[Math.floor(Math.random() * arr.length)]
|
||||
}
|
||||
|
||||
function genSquawk() {
|
||||
return String(Math.floor(Math.random() * 8000 + 1000)).padStart(4, '0')
|
||||
}
|
||||
|
||||
function genATIS() {
|
||||
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
return letters[Math.floor(Math.random() * letters.length)]
|
||||
|
||||
39
shared/utils/transponder.ts
Normal file
39
shared/utils/transponder.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Transponder (SSR) code generation.
|
||||
*
|
||||
* A squawk is an *octal* four-digit code — every digit is 0–7, because each
|
||||
* digit is encoded in three pulse positions. A code containing 8 or 9 cannot be
|
||||
* dialled into a real transponder, so it must never reach a clearance.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Codes ATC will not assign as a discrete code.
|
||||
*
|
||||
* 7500/7600/7700 are the ICAO emergency codes (unlawful interference, radio
|
||||
* failure, general emergency); 7000 is the European VFR conspicuity code and
|
||||
* 1200 its US equivalent; 2000 is the code for entering from a non-SSR area;
|
||||
* 0000 is a non-code used to indicate a transponder fault.
|
||||
*/
|
||||
export const RESERVED_SQUAWKS = new Set([
|
||||
'7500', '7600', '7700', '7000', '2000', '1200', '0000',
|
||||
])
|
||||
|
||||
/** True when `code` is a well-formed, assignable discrete squawk. */
|
||||
export function isValidSquawk(code: string): boolean {
|
||||
if (!/^[0-7]{4}$/.test(code)) return false
|
||||
return !RESERVED_SQUAWKS.has(code)
|
||||
}
|
||||
|
||||
/** A random assignable discrete squawk: four octal digits, no reserved code. */
|
||||
export function generateSquawk(): string {
|
||||
// The reserved set is tiny next to the 4096-code space, so re-rolling
|
||||
// terminates immediately in practice; the bound just makes that guaranteed.
|
||||
for (let attempt = 0; attempt < 20; attempt++) {
|
||||
let code = ''
|
||||
for (let digit = 0; digit < 4; digit++) {
|
||||
code += String(Math.floor(Math.random() * 8))
|
||||
}
|
||||
if (isValidSquawk(code)) return code
|
||||
}
|
||||
return '1000'
|
||||
}
|
||||
Reference in New Issue
Block a user