One reference for the whole simulator: every command a program can issue, a worked example for each idea the courses teach, and the rules every run is scored by. None of it needs an account. Read it before you write a line.
Concepts
A program is one async function
Every mission runs the same entry point: `async function main(drone)`. The simulator calls it once, hands it the drone, and the run ends when it returns. Declare whatever you like around it (helpers, constants, classes), but that function has to exist, or the run stops before it starts with "Define an async function named main(drone)".
Every command is awaited
Each `drone.*` call returns a promise that settles once the physics loop has actually flown the manoeuvre, not when the command is issued. Drop an `await` and the whole program runs in a single frame: the drone lands before it has taken off, and the run scores as though it never flew.
It is ordinary JavaScript
Loops, conditionals, functions, arrays and objects all work, because the code is real JavaScript in a real engine. A route is an array you iterate; a search is a `while` that reads a sensor. Nothing here is a scripting language pretending to be one.
Relative and absolute movement
`moveForward()` and `turn()` fly relative to wherever the nose points; `moveTo()` flies to a coordinate in the world whatever the heading. Briefings give waypoints as absolute coordinates, so those are flown with `moveTo()`, and `moveTo()` will happily fly you into the ground if you hand it a low `y`, so take off first.
Autocomplete in the editor
The editor knows every command the drone has, but only once it knows that `drone` is the drone. That is what the line above `main` in every starter program is for: `/** @param {Drone} drone */`. Keep it, or add it to a program written before it existed, and typing `drone.` lists every command with its parameters and notes, and the value a sensor resolves with completes too.
Where the code runs
Programs run in a dedicated Web Worker: no DOM, no `window`, and `fetch`, `XMLHttpRequest`, `WebSocket` and `EventSource` are removed before the first line executes. `console.log`, `console.warn` and `console.error` do work, and print to the console beside the editor, which is how you see what a program thought it was doing.
Worked examples
The shape of every program
Your code is one async function named `main`, and it is handed a `drone`. Every command is awaited, because each one resolves when the manoeuvre has actually been flown. Drop an `await` and the whole script runs in a single frame.
async function main(drone) {
await drone.takeoff();
await drone.hover(2);
await drone.land();
}
A square, with a loop
`moveForward()` goes wherever the nose points, so turning between legs is what draws a shape. Four identical legs means a loop rather than four pairs of lines, and changing the 4 to a 6 flies a hexagon.
async function main(drone) {
await drone.takeoff(3);
const sides = 4;
for (let i = 0; i < sides; i++) {
await drone.moveForward(6);
await drone.turn(360 / sides);
}
await drone.land();
}
Waypoints as data
Waypoint coordinates from a briefing are absolute, so they are flown with `moveTo()`. Keeping them in an array separates the route from the flying: to change the mission you edit the list, not the loop.
async function main(drone) {
// Lift off first. moveTo() will happily fly you into
// the ground if you hand it a low y.
await drone.takeoff();
const route = [
{ x: 8, y: 3, z: -6 },
{ x: 14, y: 3, z: -14 },
{ x: 2, y: 3, z: -18 },
];
for (const point of route) {
await drone.moveTo(point.x, point.y, point.z);
console.log('reached', point);
}
await drone.land();
}
Creeping up on an obstacle
The rangefinder looks straight along the nose and reports 20 when it sees nothing. Testing against a threshold, rather than against 20, is what makes this read as "keep going while the way is clear".
async function main(drone) {
await drone.takeoff(2);
// Nudge forward a meter at a time and re-check, so the
// drone never commits to a leg longer than it can see.
while ((await drone.getDistanceAhead()) > 2) {
await drone.moveForward(1);
}
console.log('stopped at', await drone.getDistanceAhead(), 'm');
// Something is in the way: go over it rather than through it.
await drone.setAltitude(6);
await drone.moveForward(6);
await drone.land();
}
Stepping up in fixed increments
`setAltitude()` is absolute, so a relative step means reading the current height and adding to it. Computing each rung from the base rather than from the last reading keeps a small hover drift from accumulating up the ladder.
async function main(drone) {
await drone.takeoff(2);
const base = await drone.getAltitude();
for (let step = 1; step <= 4; step++) {
await drone.setAltitude(base + step * 2);
// Let it settle before anything is measured at this rung.
await drone.hover(1);
console.log('rung', step, 'at', await drone.getAltitude());
}
await drone.land();
}
Slowing down for the tight part
Cruise speed is a setting, not an argument. It applies to every move that follows until it is changed. Cruising between the open stretches and slowing for the gates is usually faster overall than flying the whole route carefully.
async function main(drone) {
await drone.takeoff(3);
// Open ground: get it over with.
drone.setSpeed(8);
await drone.moveTo(10, 3, -10);
// Tight ground: buy accuracy with time.
drone.setSpeed(2);
await drone.moveTo(12, 2.4, -16);
await drone.moveTo(14, 2.4, -22);
await drone.land();
}
A slalom from a list of offsets
A weaving line is a straight run down one axis with an alternating offset on the other. Written this way the shape lives in two numbers, and widening the weave is a one-character edit.
async function main(drone) {
await drone.takeoff(3);
drone.setSpeed(4);
const gates = 6;
const spacing = 5; // meters between gates
const weave = 2.5; // how far off the centreline each way
for (let i = 0; i < gates; i++) {
const offset = i % 2 === 0 ? weave : -weave;
await drone.moveTo(offset, 3, -(i + 1) * spacing);
}
await drone.land();
}
Landing on a mark
Because `land()` drops straight down, a pinpoint landing is really a pinpoint hover: get the horizontal position right, let it stop moving, and only then descend.
async function main(drone) {
await drone.takeoff(4);
const pad = { x: 12, z: -18 };
// Arrive high and slow, directly over the pad.
drone.setSpeed(2);
await drone.moveTo(pad.x, 4, pad.z);
// Kill the drift before committing to the descent.
await drone.hover(1.5);
const here = await drone.getPosition();
console.log('offset', (here.x - pad.x).toFixed(2), (here.z - pad.z).toFixed(2));
await drone.land();
}
Probing left and right
The rangefinder only looks along the nose, so measuring in another direction means pointing the nose there. Turning back by the same amount afterwards leaves the drone as it was found, which keeps the probe from becoming part of the route.
async function main(drone) {
await drone.takeoff(2.5);
async function probe(degrees) {
await drone.turn(degrees);
const distance = await drone.getDistanceAhead();
await drone.turn(-degrees);
return distance;
}
const left = await probe(-90);
const right = await probe(90);
console.log('left', left, 'right', right);
// Take whichever side has more room.
await drone.turn(left > right ? -90 : 90);
await drone.moveForward(Math.min(left, right, 8) - 1);
await drone.land();
}
Reading the contact list
`scan()` returns everything within range, nearest first, with a bearing already relative to the nose. Logging the raw list once is the fastest way to learn what a mission has actually put around you.
async function main(drone) {
await drone.takeoff(4);
const contacts = await drone.scan(25);
for (const contact of contacts) {
console.log(
contact.kind,
contact.label ?? '(unlabelled)',
`${contact.distance}m`,
`bearing ${contact.bearingDeg}`,
);
}
console.log('nearest is', contacts[0]?.kind ?? 'nothing in range');
await drone.land();
}
Flying to something you found
A contact carries both a bearing and absolute coordinates, which gives you two ways to reach it. Turning by the bearing and flying the distance is the one that generalises. It works the same whether the target moved or you did.
async function main(drone) {
await drone.takeoff(3);
const contacts = await drone.scan(30);
const target = contacts.find((c) => c.label === 'north-tower');
if (!target) {
console.warn('nothing labelled north-tower in range');
await drone.land();
return;
}
// bearingDeg is already relative to the nose: 0 is straight
// ahead, positive is to the right. That is exactly what
// turn() wants, so it can be passed straight through.
await drone.turn(target.bearingDeg);
// Stop short of it rather than on top of it.
drone.setSpeed(3);
await drone.moveForward(target.distance - 4);
await drone.land();
}
A loop that always ends
A sensor loop that never satisfies its condition runs until the mission clock stops it, and the run is scored as a timeout. Counting the passes costs one variable and turns a hang into a log line you can read.
async function main(drone) {
await drone.takeoff(2);
const maxSteps = 40;
let steps = 0;
while ((await drone.getDistanceAhead()) > 2) {
if (steps++ >= maxSteps) {
console.warn('gave up after', maxSteps, 'steps');
break;
}
await drone.moveForward(1);
}
await drone.land();
}
A delivery run as a stop list
Every stop is the same four moves, so it is written once as a function and called per entry. The cruise altitude sits above the tallest thing on the route, and the drone only comes down when it is over a drop.
async function main(drone) {
const CRUISE = 12; // above the rooftops
const DROP = 2; // hand-off height
const SERVICE = 1.5; // seconds on station per stop
const stops = [
{ name: 'depot-north', x: 10, z: -8 },
{ name: 'tower-west', x: -6, z: -20 },
{ name: 'market', x: 14, z: -26 },
];
async function deliver(stop) {
await drone.moveTo(stop.x, CRUISE, stop.z);
await drone.setAltitude(DROP);
await drone.hover(SERVICE);
await drone.setAltitude(CRUISE);
console.log('delivered', stop.name);
}
await drone.takeoff(CRUISE);
drone.setSpeed(7);
for (const stop of stops) {
await deliver(stop);
}
await drone.moveTo(0, CRUISE, 0);
await drone.land();
}
Climbing once instead of per leg
The commonest way to lose a timed route is to yo-yo: descending to each stop and climbing back to cruise between every pair. Sorting the stops so the route does not double back costs nothing and is usually worth more than raising the speed.
async function main(drone) {
const CRUISE = 14;
const stops = [
{ name: 'market', x: 14, z: -26 },
{ name: 'depot-north', x: 10, z: -8 },
{ name: 'tower-west', x: -6, z: -20 },
];
// Nearest-first from the pad. Not the shortest possible
// tour, but it is one line and it beats the order the
// briefing happened to list them in.
const ordered = [...stops].sort(
(a, b) => Math.hypot(a.x, a.z) - Math.hypot(b.x, b.z),
);
await drone.takeoff(CRUISE);
drone.setSpeed(8);
for (const stop of ordered) {
await drone.moveTo(stop.x, CRUISE, stop.z);
await drone.hover(1);
}
await drone.moveTo(0, CRUISE, 0);
await drone.land();
}
Checking you can finish
Charge drains faster under throttle than in a hover, and the rate belongs to the airframe you picked. Reading it between stops turns a run that dies on the last leg into one that comes home early and lands.
async function main(drone) {
const CRUISE = 12;
const RESERVE = 25; // percent kept back for the trip home
const stops = [
{ x: 10, z: -8 },
{ x: -6, z: -20 },
{ x: 14, z: -26 },
];
await drone.takeoff(CRUISE);
drone.setSpeed(7);
for (const stop of stops) {
const charge = await drone.getBattery();
if (charge < RESERVE) {
console.warn('breaking off at', charge, '%');
break;
}
await drone.moveTo(stop.x, CRUISE, stop.z);
await drone.hover(1);
}
await drone.moveTo(0, CRUISE, 0);
await drone.land();
}
Sweeping a street and logging it
A sweep is a lawnmower pattern with a scan at each pass. Collecting contacts into a Map keyed by label is what stops the same parked van being counted once per pass.
async function main(drone) {
await drone.takeoff(6);
drone.setSpeed(5);
const found = new Map();
for (let lane = 0; lane < 4; lane++) {
await drone.moveTo(lane * 6, 6, -10);
await drone.moveTo(lane * 6, 6, -28);
for (const contact of await drone.scan(20)) {
const key = contact.label ?? `${contact.kind}@${contact.x},${contact.z}`;
if (!found.has(key)) {
found.set(key, contact);
console.log('new contact:', contact.kind, key);
}
}
}
console.log('swept', found.size, 'objects');
await drone.land();
}
Photographing a named target
Photo objectives are graded on where the drone was standing when the shutter fired, so the job is to get to a sensible stand-off and point the nose at the subject. The 0.4 s stabilise happens inside `takePhoto()`. It does not need a hover before it.
async function main(drone) {
await drone.takeoff(8);
const contacts = await drone.scan(35);
const subject = contacts.find((c) => c.label === 'clock-tower');
if (!subject) {
console.warn('clock-tower not in range');
await drone.land();
return;
}
// Stand off far enough to get the whole thing in frame.
drone.setSpeed(3);
await drone.turn(subject.bearingDeg);
await drone.moveForward(subject.distance - 10);
// Re-aim from the new position: the bearing changed on the way in.
const closer = await drone.scan(20);
const reacquired = closer.find((c) => c.label === 'clock-tower');
if (reacquired) {
await drone.turn(reacquired.bearingDeg);
}
await drone.takePhoto('clock-tower, north face');
await drone.land();
}
Flying the wash tunnel
A tunnel is graded at both mouths, so it only counts if the drone goes in one end and out the other. Approaching on the tunnel's own axis, at the height scan gives for it (halfway up its walls and roof, a little above the middle of the opening) and committing to a single straight leg is what keeps it from clipping a wall halfway through.
async function main(drone) {
await drone.takeoff(4);
const [wash] = (await drone.scan(40)).filter((c) => c.kind === 'carwash');
if (!wash) {
console.warn('no wash tunnel in range');
await drone.land();
return;
}
// Line up well short of the mouth, on the tunnel's centreline.
drone.setSpeed(2);
await drone.moveTo(wash.x, wash.y, wash.z + 10);
// Re-acquire from the new position and square up on it.
const aligned = (await drone.scan(40)).find((c) => c.kind === 'carwash');
if (aligned) {
await drone.turn(aligned.bearingDeg);
}
await drone.hover(1);
// One straight run, long enough to clear the far mouth.
await drone.moveForward(20);
await drone.land();
}
A shift, in pieces
A combined mission is long enough that debugging it as one block is painful. Splitting it into named phases lets you comment out three of them and fly the fourth until it works. That is how these get finished.
async function main(drone) {
const CRUISE = 10;
async function launch() {
await drone.takeoff(CRUISE);
drone.setSpeed(6);
}
async function survey() {
for (const z of [-10, -20, -30]) {
await drone.moveTo(0, CRUISE, z);
await drone.takePhoto(`survey z=${z}`);
}
}
async function inspect() {
const contacts = await drone.scan(30);
console.log('contacts on station:', contacts.length);
}
async function recover() {
await drone.moveTo(0, CRUISE, 0);
await drone.land();
}
await launch();
await survey();
await inspect();
await recover();
}