> ## Documentation Index
> Fetch the complete documentation index at: https://docs.czautoz.co.uk/llms.txt
> Use this file to discover all available pages before exploring further.

# Scripting API (for mod makers)

> Server calls and events that let other mods read a helicopter's state, start and stop it, and fly it with our autopilot or with their own controls.

> [!NOTE]
>
> This page is for **mod makers** who write server mods such as air patrols, rescue missions or military events. Server owners don't need to do anything: on its own the API does nothing for players, and if no other mod uses it, nothing changes on your server.

Other server mods can read a helicopter's state, start and stop it, and fly it, with our own autopilot or with their own controls. Everything runs on the **server**. Calls made on a player's game do nothing and return `false`.

## Checking the mod is there

Add `CZAutozHelicopter_Scripts` to your mod's `requiredAddons`, then check the version:

```c theme={null}
if (CZAutoz_HeliAPI.GetApiVersion() >= 1) { /* version 1 calls are there */ }
```

The version only goes up when a call changes in a way that could break your mod. New calls are added without changing it.

## IsUsable

```c theme={null}
bool ok = CZAutoz_HeliAPI.IsUsable(heli);   // heli: EntityAI
```

`true` when `heli` is one of our helicopters, is not destroyed, and can be used on this server right now. **Every other call returns `false` (and does nothing) when this is `false`.**

## Reading state

Each getter returns `true` and fills the value, or `false` (value 0) when the helicopter is not usable.

| Call | Value |
| - | - |
| `IsEngineOn(heli)` | engine running or starting |
| `GetEngineStage(heli, out int stage)` | 0 off, 1 starting, 2 running, 3 engine off in flight with the rotor still turning (autorotation) |
| `GetEngineRpm01(heli, out float v)` / `GetRotorRpm01(heli, out float v)` | 0 to 1 |
| `GetFuelLitres(heli, out float l)` / `GetFuelCapacity(heli, out float l)` | litres |
| `GetBattery01(heli, out float v)` | 0 to 1, the charge of the fitted battery (0 if there is none or it is ruined) |
| `GetHealth01(heli, out float v)` / `GetZoneHealth01(heli, string zone, out float v)` | 0 to 1 |
| `IsMainRotorOk(heli)` / `IsTailRotorOk(heli)` | rotor not destroyed or snapped off |
| `GetAltitudeAglM(heli, out float m)` / `GetAltitudeAslM(heli, out float m)` | metres above the ground (or roof, deck or sea) / above sea level |
| `GetSpeedKmh(heli, out float v)` / `GetVerticalSpeedMs(heli, out float v)` | speed in km/h / climb or sink in m/s |
| `GetHeadingDeg(heli, out float d)` | 0 to 360, 0 = north |
| `GetPitchRollDeg(heli, out float p, out float r)` | attitude in degrees |
| `IsGrounded(heli)`, `IsAutoHoverOn(heli)`, `IsAutopilotFlying(heli)` | state |
| `GetPilot(heli, out Man m)` / `GetCrewCount(heli, out int n)` | who is in the pilot seat (or null) / how many people are seated |

## Controlling it

Each call does what a player can do, with the same rules: fuel, battery, parts, damage and the server's own settings. It returns `true` if it was done. No player has to be on board.

A script call has no player, so checks made on a player (for example a vehicle key or lock mod) do not apply to it: your mod decides who may use the helicopter.

| Call | Does |
| - | - |
| `StartEngine(heli)` / `StopEngine(heli)` | start or stop the engine (a start needs fuel, a charged battery and working parts; a stop works on the ground or below 5 m/s) |
| `SetAutoHover(heli, bool on)` | Autohover on or off, with the same rules as the pilot's key (returns `false` for a helicopter lying on its side: a script never rights it) |
| `FlyTo(heli, float x, float z)` | our autopilot to a map point |
| `FlyRoute(heli, array<vector> points)` | our autopilot through each point in turn (y is ignored) |
| `CancelAutopilot(heli)` | stop the autopilot (it holds a hover where it is, as when a pilot cancels) |
| `SetAiInput(heli, float collective, float pitch, float roll, float yaw)` | fly it yourself; every value -1 to 1 (see below) |
| `ClearAiInput(heli)` | let go of the controls |

**Autopilot.** It needs the server's autopilot switched on. From the ground it only takes off if the server allows automatic take-off; otherwise start the engine and get it into the air first (for example with `SetAiInput`). At the last point it lands or holds a hover, as the server's autopilot settings say. On a route it turns for the next point about 1.2 km before each point, so keep points at least about 1.5 km apart.

Points are map coordinates in metres, kept inside the map (0 to 32768). A point that is not a finite number refuses the whole call. The autopilot plans its flight each time it is given a point, so `FlyTo` / `FlyRoute` on the same helicopter are accepted at most once every 0.5 s (a call sooner returns `false` and changes nothing). Call them once, not every frame. A player who takes the pilot seat or uses the Pilot Panel ends your route.

**Your own controls.** `SetAiInput` only works while no player is flying the helicopter. It switches the autopilot and Autohover off. Pitch, roll and yaw from -1 to 1 are a player's full stick and pedals. Collective from -1 to 1 is the gentle range a pilot uses to hold and adjust height (about a third of a player's full collective), enough to climb and descend. Send it at least every 0.5 s: if you stop, the controls go back to neutral, and the server then treats the helicopter like any other one nobody is flying (its abandoned-aircraft settings apply). A player who sits in the pilot seat always takes over.

The helicopter still flies by its normal physics. One flown with nobody in the pilot seat is worked out by the server, so watching players may see small corrections in its movement that they would not see with a player flying it.

## Events

Server only. Subscribe in your mission start and remove your handlers in your teardown. Events fire while the server can use the helicopters. Each one arrives on the next server tick after it happens, when the helicopter's state is settled, so you can call any API function from your handler (for example `FlyTo` from `OnAutopilotArrived`).

```c theme={null}
CZAutoz_HeliAPI.OnEngineStarted.Insert(MyOnEngineStarted);   // void MyOnEngineStarted(EntityAI heli)
```

| Event | Parameters | Fires when |
| - | - | - |
| `OnEngineStarted` / `OnEngineStopped` | `(EntityAI heli)` | the engine reaches running / is fully off |
| `OnTookOff` / `OnLanded` | `(EntityAI heli)` | it leaves the ground / settles on something |
| `OnDamaged` | `(EntityAI heli, float health01)` | its overall health level changes |
| `OnCrashed` | `(EntityAI heli)` | a hard impact (at most once every 2 s) |
| `OnDestroyed` | `(EntityAI heli)` | it is destroyed |
| `OnAutopilotArrived` | `(EntityAI heli)` | the autopilot reaches its point (hover or landed) |
| `OnPilotChanged` | `(EntityAI heli, Man pilotOrNull)` | a player takes or leaves the controls |

## Example: start a helicopter, climb, and fly to a point

```c theme={null}
class MyPatrol
{
    EntityAI m_Heli;
    int m_N;

    void Go(EntityAI heli)
    {
        m_Heli = heli;
        if (!CZAutoz_HeliAPI.StartEngine(m_Heli)) return;
        GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(StartClimb, 30000, false);   // let the engine and rotor spool up
    }

    void StartClimb()
    {
        GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(Climb, 250, true);
    }

    void Climb()
    {
        m_N++;
        CZAutoz_HeliAPI.SetAiInput(m_Heli, 1.0, 0.0, 0.0, 0.0);
        if (m_N < 40) return;
        GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).Remove(Climb);
        CZAutoz_HeliAPI.ClearAiInput(m_Heli);
        CZAutoz_HeliAPI.FlyTo(m_Heli, 4500.0, 10200.0);
    }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.