git clone https://garage44.eu/jeroen/frits.git.. /crates/bench/scripts/firebird.frits
// The firebird: a burning thing that circles Groningen, comes when the beacon on
// the Grote Markt calls it, and goes off if you sneak up on it.
//
// Cartopolis's `content/firebird.lua`, written in Frits. What changed in the
// translation: the state machine is an enum, so `match` checks every state is
// handled; positions are records; and the script says up front what it does
// (`script / …`), which is all it can do.
script / Prim, Chat, Channel, Sensor, Timer
// Where it circles, how wide, how long a lap takes, and how high it flies.
let CENTRE = { lat: 53.2194, lng: 6.5665 }
let RADIUS_M = 180.0
let LAP_S = 48.0
let ALTITUDE_M = 45.0
// Where it hovers when it has been called in.
let BEACON = { lat: 53.21935, lng: 6.56675 }
let HOVER_M = 9.0
let HOVER_DRIFT_M = 2.5
// How near an avatar has to get to startle it, in metres.
let NOTICE_M = 45.0
// How long it shakes before it goes, and how far it rattles.
let SHAKE_S = 2.5
let SHAKE_M = 0.6
// How long the blast is left standing before the shell reforms.
let LINGER_S = 2.0
// How long after being released it is too busy climbing away to be startled.
let RELEASE_GRACE_S = 6.0
// The channel the beacon is on.
let CHANNEL = 4747
// Metres per degree here, computed once: this thing never leaves Groningen.
let M = geo.metres_per_degree(CENTRE.lat)
enum State { Patrol, Summoned, Shaking, Blown }
var state = Patrol
var marked = 0.0
// Time the patrol is measured from, so being summoned and released does not
// teleport it round its own circle.
var lap_epoch = 0.0
var calm_until = 0.0
// A flame: hot core, rising, curling. Every field left out keeps the fire preset's.
var flame = {
..Prim.fire,
count: 120,
life: 1.3,
speed: 2.2,
spread_deg: 26,
gravity: 2.6,
source_radius: 0.6,
size_start: 1.1,
size_end: 0.15,
color_start: { r: 5.0, g: 2.2, b: 0.5 },
color_end: { r: 1.2, g: 0.1, b: 0.02 },
turbulence: 1.2,
}
fn light_it() / Prim {
Prim.alpha(1.0)
Prim.glow(0.25)
Prim.particles(flame)
}
fn at_height(y: Float) / Prim {
let p = Prim.pos()
Prim.set_pos({ ..p, y: y })
}
on start() {
Prim.name("Firebird")
// A dark shell, so what you see is the fire and not a lit cylinder.
Prim.color({ r: 0.09, g: 0.07, b: 0.06 })
Prim.scale({ x: 1.6, y: 0.9, z: 1.6 })
at_height(ALTITUDE_M)
light_it()
Channel.listen(CHANNEL)
Chat.shout("The firebird is up.")
// One tick at 20 Hz.
Timer.every(0.05)
}
// Where on the circle it is at time `t`.
fn fly(t: Float) / Prim {
let a = (t - lap_epoch) / LAP_S * 2 * math.pi
Prim.set_geo({
lat: CENTRE.lat + RADIUS_M * math.cos(a) / M.lat,
lng: CENTRE.lng + RADIUS_M * math.sin(a) / M.lng,
})
// Nose along the tangent.
Prim.set_rot({ x: 0, y: -math.deg(a), z: 0 })
}
// Hovering over the beacon, turning slowly and bobbing.
fn hover(t: Float) / Prim {
let a = t * 0.7
Prim.set_geo({
lat: BEACON.lat + HOVER_DRIFT_M * math.cos(a) / M.lat,
lng: BEACON.lng + HOVER_DRIFT_M * math.sin(a) / M.lng,
})
at_height(HOVER_M + 0.6 * math.sin(t * 1.7))
Prim.set_rot({ x: 0, y: math.deg(a) * 2, z: 0 })
}
// What the beacon's label is asking about.
fn report() / Channel {
Channel.send(CHANNEL, if state == Summoned { "here" } else { "patrolling" })
}
on message(channel: Int, text: Str, sender: Int) {
if text == "where" {
report()
return
}
// Anything else on this channel is a press.
match state {
Summoned => {
state = Patrol
// Rejoin the circle where it left it.
lap_epoch = Timer.now()
calm_until = Timer.now() + RELEASE_GRACE_S
at_height(ALTITUDE_M)
Chat.shout("Back to the rounds.")
report()
}
Patrol => {
state = Summoned
Chat.shout("Coming.")
report()
}
// Mid-explosion: it reports again when it reforms.
Shaking => {}
Blown => {}
}
}
on timer() {
let now = Timer.now()
match state {
Summoned => hover(now),
Patrol => {
fly(now)
if now >= calm_until {
if let Some(who) = Sensor.nearest(NOTICE_M) {
state = Shaking
marked = now
Chat.shout("{who.name} is {who.distance:.0} m away. Stand back.")
// Wind the flame up: the fire is the warning.
flame = { ..flame, count: 200, speed: 4.0, size_start: 1.6 }
Prim.particles(flame)
}
}
}
Shaking => {
let t = now - marked
// Rattle in place, in geo: horizontal position is the anchor's.
let wobble = SHAKE_M * (t / SHAKE_S)
let g = Prim.geo()
Prim.set_geo({
lat: g.lat + wobble * math.sin(t * 47) / M.lat,
lng: g.lng + wobble * math.cos(t * 53) / M.lng,
})
Prim.set_rot({ x: 6 * math.sin(t * 61), y: 0, z: 6 * math.cos(t * 59) })
if t >= SHAKE_S {
state = Blown
marked = now
Chat.shout("...")
Prim.burst({
..Prim.fire,
count: 240,
life: 1.4,
speed: 18.0,
spread_deg: 180,
gravity: -9.0,
source_radius: 1.0,
size_start: 2.2,
size_end: 0.3,
color_start: { r: 8.0, g: 3.6, b: 1.0 },
color_end: { r: 0.9, g: 0.12, b: 0.02 },
})
// The shell goes, the blast stays.
Prim.alpha(0.0)
Prim.glow(0.0)
}
}
Blown => {
if now - marked >= LINGER_S {
// Reform out on the circle, away from whoever startled it.
state = Patrol
lap_epoch = now
calm_until = now + RELEASE_GRACE_S
flame = { ..flame, count: 120, speed: 2.2, size_start: 1.1 }
at_height(ALTITUDE_M)
fly(now)
light_it()
report()
}
}
}
}