Your first script: automating altitude assignments


If you’ve been controlling aircraft manually in radarcontrol.io, you might have wondered: can I automate some of this? The answer is yes. Code mode lets you write JavaScript to control aircraft programmatically, opening up possibilities from simple automation to complex traffic management algorithms.

This guide will walk you through your first script: automatically assigning cruise altitude to new aircraft. By the end, you’ll understand the core concepts of code mode and have a working script you can try immediately.

Understanding code mode

Code mode in radarcontrol.io gives you three main hooks into the simulation:

onSpawn(ac): Called whenever a new aircraft appears in your airspace. This is where you typically set initial parameters like altitude, speed, or routing.

onTick(): Called every simulation tick (usually 60 times per second). Use this for continuous monitoring and decision-making based on changing conditions.

onConflict(ac1, ac2): Called when two aircraft are predicted to violate separation standards. This lets you implement automated conflict resolution.

For our first script, we only need onSpawn. We want to assign each new aircraft a cruise altitude as soon as it enters our airspace.

The basic structure

Every code mode script is just a JavaScript file with one or more of these functions defined. Here’s the absolute minimum:

function onSpawn(ac) {
  // Your code here runs whenever a new aircraft spawns
}

The ac parameter is an aircraft object with properties like callsign, altitude, speed, heading, and methods like cfl(), speed(), heading(), and more. These methods correspond to the commands you’d type manually.

Building the auto-altitude script

Let’s build a script that assigns FL350 (35,000 feet) to every new arrival. Here’s the complete code:

function onSpawn(ac) {
  // Assign cruise altitude FL350 to all new aircraft
  ac.cfl(35000);

  // Log to console so we can see it working
  console.log(ac.callsign + " assigned FL350");
}
Load into simulator →

That’s it. This script will automatically assign FL350 to every aircraft that spawns in your airspace. The cfl() method takes altitude in feet, so 35000 represents flight level 350.

How it works

When you enable this script in code mode:

  1. A new aircraft spawns (for example, UAL123)
  2. The simulator calls onSpawn(ac) with that aircraft object
  3. Your code runs: ac.cfl(35000) issues the altitude instruction
  4. The console logs “UAL123 assigned FL350”
  5. The aircraft begins climbing or descending to FL350

The beauty of code mode is that this happens instantly and automatically. You don’t need to notice the aircraft spawning, type the command, or even think about it. The script handles it.

Making it smarter

The basic script works, but let’s make it more practical. Real-world traffic doesn’t all fly at the same altitude. Let’s modify it to assign different altitudes based on whether the aircraft is an arrival or departure:

function onSpawn(ac) {
  // Check if aircraft is arriving or departing
  if (ac.isArrival) {
    // Arrivals start high, we'll descend them later
    ac.cfl(36000);
    console.log(ac.callsign + " arrival assigned FL360");
  } else {
    // Departures climb to standard cruise altitude
    ac.cfl(35000);
    console.log(ac.callsign + " departure assigned FL350");
  }
}

Now arrivals get FL360 and departures get FL350. This creates natural vertical separation between opposing traffic flows.

Adding speed control

Let’s expand further. We can also set speed at spawn:

function onSpawn(ac) {
  if (ac.isArrival) {
    ac.cfl(36000);
    ac.speed(280); // Slow arrivals to 280 knots
    console.log(ac.callsign + " arrival: FL360, 280 knots");
  } else {
    ac.cfl(35000);
    ac.speed(320); // Departures cruise at 320 knots
    console.log(ac.callsign + " departure: FL350, 320 knots");
  }
}

Now we’re controlling both altitude and speed automatically. This is already more sophisticated than many manual controllers achieve, and it happens instantly for every aircraft.

Common patterns

As you write more scripts, you’ll see common patterns emerge:

Conditional logic: Different actions for different aircraft types, altitudes, or positions.

if (ac.altitude > 30000) {
  // High altitude logic
} else {
  // Low altitude logic
}

Accessing aircraft properties: The aircraft object has many useful properties:

  • ac.callsign - The aircraft’s callsign (e.g., “UAL123”)
  • ac.altitude - Current altitude in feet
  • ac.speed - Current speed in knots
  • ac.heading - Current heading in degrees
  • ac.latitude and ac.longitude - Position
  • ac.isArrival - Boolean, true if arrival
  • ac.destination - Destination airport code

Chaining commands: You can issue multiple commands to the same aircraft:

ac.cfl(28000);
ac.speed(250);
ac.heading(180);

Debugging your scripts

The console.log statements aren’t just for show. When learning code mode, liberal use of console.log helps you understand what’s happening:

function onSpawn(ac) {
  console.log("New aircraft:", ac.callsign);
  console.log("Altitude:", ac.altitude);
  console.log("Is arrival:", ac.isArrival);

  ac.cfl(35000);
}

Open your browser’s developer console (F12) to see these messages. This is essential for debugging when your script doesn’t behave as expected.

Safety considerations

Code mode is powerful, which means you can also create dangerous situations if you’re not careful. A few safety tips:

Don’t issue conflicting commands: If you set altitude in onSpawn, don’t also set it in onTick unless you have a good reason. The aircraft will constantly receive new instructions.

Check before commanding: Before issuing an altitude, make sure it makes sense:

function onSpawn(ac) {
  // Don't descend aircraft that are already low
  if (ac.altitude > 20000) {
    ac.cfl(35000);
  }
}

Be careful with onTick: This function runs 60 times per second. Don’t issue new commands every tick unless you really mean to:

// BAD - issues command 60 times per second
function onTick() {
  let aircraft = getAircraft(); // hypothetical function
  aircraft.forEach(ac => ac.cfl(35000));
}

// GOOD - only issue command when needed
function onSpawn(ac) {
  ac.cfl(35000);
}

Next steps

This simple auto-altitude script introduces the core concepts of code mode. From here, you can:

  • Add routing: Use ac.direct(waypoint) to route aircraft
  • Implement auto-descent logic (we’ll cover this in a future post)
  • Create custom separation logic in onConflict
  • Build sophisticated traffic flow algorithms

The full command set available in code mode mirrors the manual commands. Check the approach commands documentation and the API reference to see everything you can automate.

Try it yourself

Click the “Try it” button above to load this script into radarcontrol.io and see it in action. Start a session, enable code mode, paste the script, and watch as each new aircraft is automatically assigned FL350. Then experiment: change the altitude, add speed control, implement arrival vs. departure logic.

Code mode transforms radarcontrol.io from a manual control simulator into a playground for traffic management algorithms. Your first script might be simple, but it opens the door to sophisticated automation that would be impossible to achieve manually.

Start simple, experiment often, and gradually build complexity. Before long, you’ll be writing scripts that manage entire traffic flows with minimal manual intervention. Welcome to code mode.