Going further
Bot screen (viewer)
After this page you can open a live 3D view of what your bot sees, protect it, record it, and know what it costs and which versions it draws.
What it is
ghast-viewer, the viewer feature of ghast, serves a page per bot: a WebGL view of the
bot's world in first person, third person or with a free camera, plus recordings, a dash cam
and replays on the same page.
It is read-only. The viewer wraps your behaviour and reads the bot in after_tick, after the
movement step, so the page shows the pose the server has. It never writes to the game
connection, and the page can only send clock pings, flow-control acknowledgements and playback
controls.
Versions
| session | drawn |
|---|---|
| 1.8.9 | yes |
| 1.12.2, 1.16.5 | not yet |
| 26.1, 26.2, 26.3 | live view, mapped onto the 1.8.9 assets |
The page draws with the 1.8.9 assets. A 26.x world is mapped onto them: blocks and items 1.8 has keep their state, newer ones become a look-alike, and mobs 1.8 lacks are drawn as a box of the entity's size. Text never pretends: the header says "26.1 (drawn with 1.8.9 assets)", tooltips carry the real item names, stand-in icons carry a "≈" mark, and entities drawn with a stand-in get a name tag with their real name. The page shows 256 rows of the dimension around the bot and moves the window when the bot leaves it. Recordings of 26.x sessions are untested.
In the facade, the viewer feature turns on ghast-viewer with its download feature, and
the v26_1, v26_2 and v26_3 version features switch on the viewer's 26.x support with
them.
Opening it
= { = "0.5", = ["viewer"] }
use ;
use ;
let viewer = new;
let addr = viewer.start.await?;
println!;
let client = new
.world_config;
let mut behaviour = viewer.watch;
client.run.await?;
viewer.watch(b) wraps any behaviour in a Watched<B> that passes everything through; use
your own instead of Idle, and reach it with inner() / inner_mut().
ghast::viewer::world_config() makes the client keep light, biomes and block entities; without
it signs have no text, skulls and banners are missing, and shading is flat. Keep the same
Viewer across reconnects; a new session resets the page by itself.
/?mode=fp|tp|free picks the camera, /status returns JSON for a dashboard, /replays lists
the recordings.
Access
The server listens on 127.0.0.1 and, with port 0 (the default), on any free port, which
start() returns. Each bot needs its own port. To reach the page from elsewhere, put a reverse
proxy in front or bind .host(..) to another address, and in that case always set
.token(..): every route then needs it, as /?token=<token> once (the page keeps it in a
cookie) or as Authorization: Bearer <token>.
The server stops itself after .idle(..) without a page (60 s by default). Call
viewer.start().await again when someone asks; it returns the running address.
Textures come from Mojang at run time
Nothing of the game ships with the crate. On first start the viewer downloads the vanilla 1.8.9
client jar from Mojang's servers (SHA-1 checked) into cache_dir, builds the block data and
texture pack from it (about 0.1 s) and caches them there. The default cache,
.cache/ghast-viewer, is relative to the working directory, so give all bots on a machine one
absolute path. On an offline machine, point .client_jar(path) at a local copy.
Recordings, dash cam, replays
A recording is exactly what a page would have received, written to replay_dir and listed on
the page, which plays it back with pause, 1 to 16× speed and seeking (/?replay=<name>).
let mut rec = start; // on disk at once
// ...
let meta = rec.stop; // also on drop
A crash leaves a playable file (data is flushed every 10 s), and a runaway recording stops at
400 MB of raw frames. DashCam keeps the last minutes in memory and saves them when you call
clip(note), for rare events such as a player coming near. prune_recordings keeps a folder
within a size and age limit.
What it costs
ViewerConfig::entities decides when entities are tracked for the page: Always (the
default, for the whole session), WhileWatched (nothing while no page or recording is attached)
or Off. If your behaviour asks for entities anyway, the viewer adds nothing.
While nobody watches and nothing records, no frames are built and no columns copied. Measured on x86-64 WSL against a 1.8 lab server, one bot process, 5-minute runs:
| state | CPU (one core) | RSS |
|---|---|---|
| standing, no page | 0.38 % | 10 MB |
| standing, one page | 0.46 % | 11 MB |
| moving, one page | 0.67 % | 12 MB |
| moving, recording, no page | 0.52 % | 12 MB |
A page of a moving bot receives about 1.5 KB/s, and a recording of the same run came to about 2.6 MB an hour. A dash cam costs about what a recording does while it is active.