git clone https://garage44.eu/jeroen/cartopolis.git.. /crates/simulator/content/beacon.js
// The beacon on the Grote Markt: a panel you press to call the firebird in, and
// press again to send it back out over the city.
//
// It is the worked example for **script-to-script messaging**, which is the one
// thing a button needs and the script API had no answer for: every call a script
// can make acts on its own prim (`prim_id` is captured by the host, never passed
// in), so a button cannot reach the thing it controls without a channel between
// them. `send` and `on_message` are that channel; `firebird.js` beside this is
// on the other end of it.
//
// The panel holds no state of its own about where the firebird is. It asks, and
// the firebird answers — because the firebird is the only thing that knows, and a
// button that remembered would be wrong the moment anything else moved it (or the
// firebird was reinstalled, or it went off and reformed).
// The channel the pair talk on. Any number; it is private to scripts, so nobody
// in-world hears it.
const CHANNEL = 4747
// What the panel is showing. Set from the firebird's replies, never guessed.
let status = '?'
function label() {
if (status === 'here') return 'Beacon — press to release the firebird'
if (status === 'patrolling') return 'Beacon — press to summon the firebird'
return 'Beacon — press to call the firebird'
}
// The flame on top of it. A beacon has to be findable from across a square, and
// the first version was not: a 1.1 m panel painted (0.10, 0.13, 0.16) is a dark
// slab on grey stone, and the person it was built for could not find it. Size and
// colour fixed that; the fire is what makes it visible from the far side.
const MARKER = {
count: 70,
life: 1.6,
speed: 1.1,
spread_deg: 16,
gravity: 1.6,
source_radius: 0.35,
size_start: 0.55,
size_end: 0.08,
color_start: [4.0, 1.6, 0.3],
color_end: [0.9, 0.15, 0.03],
alpha: 0.8,
turbulence: 0.7,
}
function on_start() {
set_name(label())
// Warm orange rather than near-black, and full bright so it keeps that colour
// at every time of day.
set_color(1.0, 0.42, 0.08)
set_glow(0.35)
set_full_bright(true)
// A panel: wide, tall, thin, and tall enough to read as a thing somebody put
// there. It stands on the ground, so half its height up.
set_scale(1.6, 2.6, 0.28)
const [x, , z] = get_pos()
set_pos(x, 1.3, z)
particles(MARKER)
listen(CHANNEL)
// Ask who is out there, so the label is right before anybody presses
// anything. A reply may never come — the firebird may not be installed — and
// the label above says the neutral thing until one does.
send(CHANNEL, 'where')
}
function on_touch(avatar_id, avatar_name) {
// The press is a *toggle*, and the panel deliberately does not decide which
// way. It says "somebody pressed me" and the firebird, which knows what it is
// doing, does the other thing. Two objects each holding half a state machine
// is how they come apart.
send(CHANNEL, 'toggle ' + avatar_name)
say(avatar_name + ' presses the beacon.')
// A press should look like a press even before anything answers.
set_glow(1.0)
sleep(0.25)
set_glow(0.35)
}
function on_message(channel, text, sender) {
if (text === 'here' || text === 'patrolling') {
status = text
set_name(label())
// Hotter while the firebird is with you, cooler while it is away — the
// panel is the readout as well as the switch. Both stay bright enough to
// find; the state is in the hue, not in whether you can see it.
if (status === 'here') {
set_color(1.0, 0.72, 0.3)
set_glow(0.7)
} else {
set_color(1.0, 0.42, 0.08)
set_glow(0.35)
}
}
}