Rocket Foundry dedicated server
===============================

Hosts a multiplayer world without playing in it. Players join it from the
game with the room code the server prints. No game install, graphics card
or display is needed.

Start a new world and open a room:

    ./RocketFoundry.Server --save "My World" --new --room

The server prints "Room code: XXXXXX". Players open MULTIPLAYER, JOIN,
and enter that code. Run the same command later without --new to host the
saved world again: --new always starts over, keeping a copy of the old
world in saves-backup/ in the data root. Use --create instead to host the
saved world and only create it the first time.

If a server update can no longer resume the exact multiplayer session a
world was saved with, the server hosts the saved world without it and says
so. A world it cannot read at all is replaced by a new one. Either way the
original save is kept in saves-backup/.

Options:

    --save NAME        world to host (required)
    --create           create the world if it does not exist yet
    --new              start a new world (an existing one goes to saves-backup/)
    --room             open a room players can join with a code
    --port N           also accept direct connections on this port (default 8801)
    --name NAME        host name shown to players (default Server)
    --data-root PATH   where saves live (default ~/rocketfoundry-server)
    --allow-teleport   players may teleport
    --launch-from WHERE  where players may launch: anywhere (the default),
                       ground (never straight into orbit) or home (only the
                       home world's sites and colony pads, so other worlds are
                       reached by flying). Co-hosts launch anywhere.
                       --no-orbit-launch is the same as --launch-from ground
    --allow-drills     players may drill into terrain
    --allow-bombs      players may detonate bombs
    --no-takeover      players may not take over craft others left behind
    --comm-network     uncrewed craft take commands only with a dish and a link
                       to crew or a ground station (--no-comm-network turns it
                       off; left out, the world keeps what it was saved with,
                       and a new world starts with it off)
    --allow-cheat NAME let every player turn this cheat on for their own craft
                       (repeatable; --cheat is the same); NAME is fuel, electricity, oxygen,
                       indestructible, nogravity, noheat, noscorch, evaparts,
                       deposits, resources or probes. Co-hosts may always use
                       every cheat.
    --everyone-cohost  every player who joins becomes a co-host
    --no-auto-owner    nobody becomes the owner by joining first (a public
                       server: name co-hosts with the cohost command)
    --world-systems LIST  a world the server creates plays with research,
                       contracts, funding and resources (comma separated, or
                       all); see "Research, contracts and funding" below

An option the server does not know, or a value it cannot read, stops it at
once with a line naming it (exit code 2), so a typo never goes unnoticed.

For a server that runs for weeks (the official one runs with all of these):

    --log-dir PATH     also write rotating log files there: server/ (this
                       output), timeline/ (the full multiplayer diagnostics
                       timeline), metrics/ (a JSON line every 10 s) and
                       profile/ (where the server's time went, each minute)
    --log-budget-gb N  disk the log files may use in total (default 6)
    --lag-snapshots PATH  write a report (and a copy of the world) there when
                       the server stalls or lags
    --lag-spike-ms N   a loop pass this slow counts as a stall (default 250)
    --stuck-seconds N  a loop stopped this long is reported as stuck
                       (default 15, 0 turns the watchdog off); the watchdog
                       watches only the running loop, not startup or the
                       final save on shutdown
    --profile-detail   the minute profile (and --bench) also times the inner
                       loops (per craft, substep and packet); costs CPU
    --autosave-seconds N  save the world every N seconds of world time
                       (default 30, at least 5)
    --dump-dir PATH    write a memory dump of a stuck server there
    --command-pipe PATH  also read console commands from this named pipe
                       (create it with mkfifo), for servers run by systemd

WebRTC routes use any free UDP port. Set RF_WEBRTC_PORT_RANGE=40000-40999 to
keep them in one range your firewall can open (one port per player).

Check a machine before hosting on it:

    ./RocketFoundry.Server --connection-test

It measures the network, the relay, direct connections and the hosting speed,
prints a report and exits (exit code 1 when a step fails). Add --json for a
machine-readable report and --show-addresses to keep IP addresses in it.

Rules you leave out keep whatever the world was saved with. The
matching --no-teleport, --launch-from anywhere, --no-drills, --no-bombs,
--allow-takeover and --no-everyone-cohost turn a saved rule back, and --no-cheat NAME withdraws a
saved allowed cheat.

Players need the same game version as the server. Download a new server
with every game update.

Mods
----

A server can host a modded world, with mods from the Community Hangar or
your own files. Players do not need to install anything: when they join,
the server sends them the world's exact mods.

Find a Hangar mod, then start a world with it:

    ./RocketFoundry.Server --hangar-search "trappist"
    ./RocketFoundry.Server --save "Trappist" --create --room --mod hangar:exoplanetfan.trappist

--mod takes, and may be given several times:

    hangar:ID          a Community Hangar mod, by its mod id (author.mod-name), its Hangar
                       id, or a pasted Hangar link
    PATH.spacemod      a mod file
    PATH               a mod folder (one with mod.json in it)
    ID                 a mod already in the data root's mods/ library

A new world starts with the mods you name. An existing world adds the ones it
does not have yet and keeps the exact versions it already uses. Also:

    --remove-mod ID    drop a mod (refused while the world still uses something from it,
                       such as a craft made of its parts or a base on its planet)
    --update-mods      move the world's Hangar mods to their newest published versions

The server prints the world's mods when it starts and says when the Hangar has
a newer version. A saved world keeps a copy of every mod it uses in
world_mods/ in the data root; copy that folder along with saves/ when you move
a world to another machine. A world whose mods the server cannot find is never
replaced: the server says which mods are missing and stops.

Console: "mods" lists the world's mods; "hangar search TEXT" and
"hangar info ID" look mods up in the Community Hangar.

Time warp
---------
The server always runs at real time, so one player's warp never slows or
speeds up anyone else. A player who warps goes out of sync: their craft run
on their own machine and the others see them as see-through ghosts. They
press SYNC to bring their craft back to the server's time, keeping their
orbit around the body they are near. A player who disconnects while out of
sync is synced automatically. A co-host's SYNC EVERYONE (pause menu) or the
console's sync-all brings every player back to the server's time at once.

Co-hosts
--------

Nobody plays on the server itself, so the first player ever to join it
becomes its owner: a co-host who can kick, freeze, set the rules, clear
debris and name other co-hosts from the pause menu. The owner stays a
co-host every time they come back, recognised by their community identity
(or by name if they joined without one). Co-hosts are kept in
multiplayer_cohosts.json in the data root; delete it to pick a new owner.
With --everyone-cohost (saved with the world) every player who joins is
a co-host for that visit; a co-host can switch it from the pause menu.

While the server runs, type (or "help" for the short list):

    players            who is connected, and who keeps colony resources private where
    status / vessels   a summary now / the largest craft with their ids
    absent [TEXT]      players who played here and left, newest first
    cohost NAME        make a connected player a co-host
    uncohost NAME      take it back (the owner stays)
    cohosts            list co-hosts
    everyone-cohost on|off  make every joining player a co-host
    say TEXT           send a chat line to everyone as the server
    chat on|off        let players chat
    images on|off      let players use their own pictures and text on craft and flags
                       (off removes the ones already there, for good)
    takeover on|off    let players take over craft others left behind
    teleport on|off    let players teleport
    launch-from anywhere|ground|home  where players may launch from
    drills on|off      let players use drills
    bombs on|off       let players use bombs
    comm-network on|off  switch the communication network for everyone
    cheats             show which cheats players may use, and who has which on
    cheat NAME on|off  allow or withdraw a cheat for every player's own craft
    debris SECONDS|off remove debris this long after it appears (off keeps it)
    cleardebris        clear everyone's wreckage and spent stages now, in every star
                       system (craft someone flies or rides in stay)
    sync-all           bring every player who is out of sync back to the server's time
    locks              every player's craft, locked or unlocked, and its owner
    lock CRAFT_ID on|off  lock or unlock a craft (ids from vessels or locks)
    delete CRAFT_ID ...   delete craft as the host, as the in-game DELETE does (ids from vessels)
    delete-all FILTER... [confirm=COUNT]
                       list every craft that matches all the filters, in every star system:
                       body=BODY (the body it orbits or stands on),
                       kind=player|debris|asteroid|astronaut, owner=NAME|KEY (who built most
                       of it). Run it again with confirm= and the count it listed to delete
                       them. Craft a connected player flies, rides, watches or has crew aboard
                       stay; asteroids go only with kind=asteroid.
                       Example: delete-all body=mercury kind=debris confirm=12
    journeys / journey-discard OWNER_KEY  players traveling between stars
    mods / hangar search TEXT / hangar info ID  the world's mods, and the Hangar's
    kick NAME / ban NAME|KEY / unban NAME|KEY / bans
    snapshot NOTE      write a lag snapshot now (needs --lag-snapshots)
    save / stop

Research, contracts and funding
-------------------------------

Career, with its story and lessons, is single player, but a Sandbox world can
play with research, contracts and launch funding in multiplayer. The world has
one shared program: every player's launch is paid from its funds, every
player's flights complete its contracts and milestones, and its research
unlocks parts for everyone. The owner and co-hosts buy research and accept
contracts. Progress is saved with the world.

    ./RocketFoundry.Server --save "Space Program" --create --room --world-systems research,contracts,funding

--world-systems takes research, contracts, funding and resources (colony
resources are used up), comma separated, or all. It applies only when the
server creates the world (--new, or --create the first time); a world keeps
the systems it was created with. Or make the world in the game (NEW SAVE,
Sandbox, WORLD SYSTEMS), copy its file from the game's saves/ folder into the
server's saves/ folder and start with --save and its name, without --new.

Room code
---------

The server keeps the room code of each world it hosts in relay_room.json in
the data root and takes the same code back when it starts again, so players
keep it. It tries a few times on a slow connection before it gives up and
opens a new room ("Room ... could not be taken back"); the log then prints the
new code. Keep the data root (and relay_room.json in it) between restarts and
updates. Running two servers at once? Give each its own --data-root: co-hosts,
bans and the server's identity live there too.

Players can also join by address: the server's IP and --port (default 8801),
which never changes and does not use the relay at all. The port takes IPv4 and
IPv6; write an IPv6 address in brackets: [2001:db8::7]:8801. Open the port in
the firewall for both. That is the fastest
route when the players are far from the relay or the relay is slow to reach.

Keeping it running
------------------

In a terminal session that survives logout:

    tmux new -s rocketfoundry './RocketFoundry.Server --save "My World" --create --room'

Or as a systemd service (edit the paths and user):

    [Unit]
    Description=Rocket Foundry dedicated server
    After=network-online.target

    [Service]
    User=YOUR_USER
    WorkingDirectory=/home/YOUR_USER/RocketFoundry-Server-linux-x64
    ExecStart=/home/YOUR_USER/RocketFoundry-Server-linux-x64/RocketFoundry.Server --save "My World" --create --room
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target

Stop the server with Ctrl-C (or systemctl stop). It saves the world before
it exits.

If the server runs into a bug it keeps the world running, prints the details
here and shows players a "Server error, please report it" message. Please send
those details with your report.

If the world's simulation stops for good (it could not recover from an error),
the server prints "WORLD STOPPED", tells the players, and about 10 seconds
later exits with code 3 without overwriting the last good save. Run it under
systemd (Restart=on-failure) or a restart loop so it comes back from that save
on its own.

Each start keeps a copy of the world in saves-backup/ named
"<world>.start-<time>.json" (not when the world is unchanged since the last
one). The newest 10 are kept, plus the last one of each day for a week. Copies
of a world that was reset or replaced by --new are never deleted.
