# Starbases
Documentation: 0.7.0 (source: v0.7.0).

> Inspect a player's Starbase state and plan first-time registration at a Starbase.

Markdown source of https://docs-v0-7-0.atlas-kit-docs.pages.dev/guides/starbases/ — see https://docs-v0-7-0.atlas-kit-docs.pages.dev/ai/ for the full machine-readable surface.

A starbase is where fleets dock, cargo is stored, and crafting happens. The SDK
splits it in two, and knowing which half you want saves confusion.

- **The starbase itself** — where it is, what level it is — is shared
  infrastructure and belongs to [world](/guides/world/).
- **Your state at that starbase** — your cargo, your escrowed ships, your
  upgrades — is yours, and lives here.

:::game[A starbase anchors a system]
A starbase is the main harbour and service centre of a star system. Players
from the faction that controls it can dock fleets, store cargo, trade on the
local market, craft goods, make repairs, and support work on nearby planets.
It also marks who controls the system. Rival factions cannot simply use it as
their own base, but they can fight to take control of it.
:::

## Reading your state at a starbase

```ts
const bases = await character.starbases.all();
```

Or directly, when you know the system:

```ts
const base = await sage.starbases.forCharacterAtSystem(
  characterAddress,
  systemAddress,
);
```

A character can have state at many starbases, or none. A character who has
never docked anywhere returns an empty array, which is a valid answer rather
than a failed read.

> **Runnable example — Read a character's starbases.** List the starbases where a character has presence, and one base upgrades list. Run it in the browser at https://docs-v0-7-0.atlas-kit-docs.pages.dev/guides/starbases/.

## What is in it

**Cargo.** What you have stored there, as `StarbaseCargoInventory`. This is
distinct from what a fleet is carrying — cargo moves between the two, and
reading one tells you nothing about the other.

**Escrowed ships.** Ships you own that are held at the starbase rather than
assigned to a fleet.

**Respawn state.** Where destroyed ships come back.

**Upgrades in progress.** Starbase upgrades take real time, so an upgrade is a
process with a lifecycle rather than an instant change:

```ts
const upgrades = await base.upgrades.all();
```

:::game[Higher tiers expand a starbase's reach]
A higher-tier starbase can support more activity in its system. Upgrades can
strengthen the base, improve the services around it, and allow more Claim
Stakes to operate on nearby planets. They can also make the system more useful
as part of a travel and trade route. Bigger starbases need more materials and
upkeep, so the controlling faction must keep supporting them after the upgrade
is finished.
:::

## Reading the shared starbase

The starbase itself lives on the system, so its level, hull, shield and shared
upgrade progress come from one derived read when you know the system:

```ts
import { getStarbaseStatus } from '@aephia/atlas-kit/world';

const status = await getStarbaseStatus(ctx, systemAddress);
if (status !== undefined) {
  console.log(
    status.level,
    status.current.hullPoints,
    status.current.maxHullPoints,
  );
  console.log(status.current.shieldPoints, status.current.maxShieldPoints);
  console.log(status.upgrade?.toLevel, status.upgrade?.materials.length);
}
```

`undefined` means the system has no starbase; a missing system still throws.
`current` pairs the stored points with the maximums the current level
configures, `services` says whether the local market and repairs are unlocked,
and `upgrade` lists every material still needed for the next level with its
resolved cargo definition. A starbase at its maximum level has no `upgrade`,
and that read never loads the Game definitions.

When you already hold a loaded system, derive the same status without a
network round-trip:

```ts
if (system.starbase !== undefined) {
  const status = deriveStarbaseStatus(system.starbase);
  console.log(status.atMaxLevel, status.upgrade?.toLevel);
  const now = BigInt(Math.floor(Date.now() / 1000));
  const points = projectStarbasePoints(status, now);
  console.log(points.hullPoints, points.shieldPoints);
}
```

The pure form keeps numeric cargo ids on its materials.

### Hull and shield right now

`current` holds the points as the game last wrote them, and the game writes
them only when combat or a repair touches the starbase. In between it
regenerates them each time it reads them: paid repairs (`pendingHullPoints`)
flow into the hull at the level's repair rate, and once the shield break delay
is over the shield recharges at the level's recharge rate, both up to the
maximum. Stored points can therefore be days out of date. On the test realm
most starbases store a damaged shield that the game already treats as full.

`projectStarbasePoints(status, atUnixSeconds)` applies that rule for any moment,
without a network read, and returns the whole points the game would store then;
combat keeps the fraction, so it can be less than one point higher. A hull below
its maximum with no pending repair stays there until someone repairs it.

### Following a starbase live

`watchStarSystem` follows one system, and the starbase inside it, through the
context's subscription provider: it delivers the current system first and then
each change the game writes. Between changes, project the points yourself:

```ts
import {
  deriveStarbaseStatus,
  projectStarbasePoints,
  watchStarSystem,
} from '@aephia/atlas-kit/world';

const subscription = await watchStarSystem(ctx, systemAddress, {
  onSnapshot: (system) => {
    if (system.starbase === undefined) return;
    const status = deriveStarbaseStatus(system.starbase);
    const now = BigInt(Math.floor(Date.now() / 1000));
    render(projectStarbasePoints(status, now));
  },
  onError: (error) => console.error(error.code, error.message),
});
// Later, when the panel closes:
await subscription.unsubscribe();
```

`render` stands for your own drawing code. Call `projectStarbasePoints` again on
a timer to animate regeneration between changes; it costs no reads.

### Following every starbase

A galaxy map that shows every starbase does not need to re-read all 945 systems
on a timer. `watchStarSystems` subscribes once to every system of the game: it
delivers each system first, then only the systems the game writes. On the test
realm that was 29 updates from six systems in ten minutes.

```ts
import type { Address } from '@aephia/atlas-kit/client';
import {
  deriveStarbaseStatus,
  projectStarbasePoints,
  watchStarSystems,
  type StarbaseStatus,
} from '@aephia/atlas-kit/world';

const statuses = new Map<Address, StarbaseStatus>();
const subscription = await watchStarSystems(ctx, {
  onSnapshot: (system) => {
    if (system.starbase === undefined) return;
    statuses.set(system.address, deriveStarbaseStatus(system.starbase));
  },
  onError: (error) => console.error(error.code, error.address, error.message),
});
// Each frame, project every stored status to now:
const now = BigInt(Math.floor(Date.now() / 1000));
for (const [address, status] of statuses) {
  draw(address, projectStarbasePoints(status, now));
}
// Later, when the map closes:
await subscription.unsubscribe();
```

`draw` stands for your own drawing code. The context's subscription provider
must support program subscriptions; `createRpcSubscriptionProvider` does when
given Kit's `createSolanaRpcSubscriptions`. A reconnect re-reads every system
once to catch up, and a busy fight sends updates for the systems involved,
which arrive coalesced when your callback is slower than the game.
Your own contributions to that upgrade are the `upgrades` processes above; the
shared progress is what everyone has deposited so far.

## Registering at a starbase

Your state at a starbase does not appear on its own: before a starbase can
hold anything of yours, you register there once. That is a write — if it
would be your first, read
[How Atlas Kit changes the game](/start-here/how-it-acts/) before continuing.

`planRegisterStarbasePlayer` plans that one-time registration for a system
with a starbase where you have no state yet:

```ts
import { planRegisterStarbasePlayer } from '@aephia/atlas-kit/starbases/actions';
import { getStarSystemById } from '@aephia/atlas-kit/world';

const registered = new Set(
  (await character.starbases.all()).map((state) => state.system.address),
);
for (const connection of system.connections) {
  const destination = await getStarSystemById(ctx, connection.systemId);
  if (destination.starbase === undefined) continue;
  if (registered.has(destination.address)) continue;
  const plan = await planRegisterStarbasePlayer(ctx, character, destination, {
    funder: walletAddress,
  });
  console.table(plan.describe());
  break;
}
```

The `funder` is the address that pays the small one-time creation cost; using
the wallet that will later sign is the convenient choice, but the planner does
not require it. The planner refuses a system without a shared starbase, and
refuses to register where your state already exists — registration is once per
starbase, which is exactly why the snippet checks what you already have first.

The resulting Plan signs and executes like every other; see [Warp &
subwarp](/guides/moving-a-fleet/#sign-and-execute-once) for that flow.

## Recovering after a Starbase falls

Destroying a Starbase at Level 1 or below hands it to the attacker and advances
its Star System's sequence. A higher-level Starbase only loses a level. Your
state at that Starbase stays on the old sequence and Starbase actions refuse it
until it is recovered. `isStarbasePlayerStale` from `starbases` tells you when
that has happened, and `planDestroyStarbasePlayer` from `starbases/actions` plans
the recovery. It takes the loaded StarbasePlayer, its Star System and a `funder`.

**Recovery cannot be undone.** Your crew and ships at that Starbase move to their
respawn queues, respawn-protected cargo moves to the cargo respawn queue, and all
other cargo there is lost. The Plan's description states the crew and ship
counts and the total cargo, but not which cargo types are protected. The program
accepts any funder as the only signer, so someone else can recover your
StarbasePlayer without your approval and keep any surplus rent the account frees.
The funder pays if the account grows instead, and must stay rent-exempt.

Recover the StarbasePlayer before respawning a Claim Stake or Crafting Hab in
that System, because the program refuses those respawns until you do. Afterwards
your crew and ships wait in `respawning` until their respawn time passes. Atlas
Kit does not plan collecting them yet, or respawning Claim Stakes and Crafting
Habs.

## Gotchas

**Starbase cargo and fleet cargo are different accounts.** Loading a fleet does
not change what the starbase holds, and vice versa. If you are tracking totals,
you need both.

**A starbase you have never used has no player state.** The starbase exists in
the world; your state at it does not. That is why the read can legitimately
come back empty.

**Upgrade timing is derived.** Completion times come from the process state
plus the Game account's definitions, not from a stored timestamp you can read
directly.

## Reference

- [`starbases`](/reference/starbases/) — every export in this entry
- [`starbases/actions`](/reference/starbases/actions/) — the registration planner
- [`world`](/guides/world/) — the starbase as shared infrastructure
- [`crafting`](/guides/crafting/) — what happens at a starbase's habs
