garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /crates/simulator/src/state.rs

raw

use cartopolis_core::{
    snap_yaw_continuation, AvatarStateDto, Emote, EmoteState, Expression, ExpressionState,
    GeoAnchor, HandPose, PrimBody, PrimDto, PrimSound, ToneSpec, EMOTE_REPLICATE_SECS,
    EXPRESSION_REPLICATE_SECS, MAX_SEAT_OFFSET_M,
};
use glam::Vec3;
use std::collections::{HashMap, HashSet};

use crate::physics::{AvatarProbe, Carry, Kick, Physics, Release, MAX_PHYSICAL_PRIMS};
use crate::reach::{ground_m, Place};
use crate::scripting::{PrimView, ScriptCommand};
use cartopolis_core::{carry, motion};

mod greet;
pub use greet::{GreetRefusal, GreetingStart};

struct AvatarSim {
    /// Where this avatar stands on Earth, once its client has said (protocol
    /// 47): the first `ClientIntent` carrying a `geo`, or a `Teleport`. **This
    /// is the authoritative position**, and [`SimWorld::step`] moves it by the
    /// intent's velocity in metres turned into degrees at this latitude
    /// (`cartopolis_core::degree_scale_m`); every client converts it into the
    /// frame its own map is anchored in, as it does for a prim's anchor.
    ///
    /// Until protocol 46 the position was metres in the avatar's *own* client's
    /// map frame, and `geo` was a second, client-reported place the sim kept
    /// beside it for sensors and physics — so Here chat compared frames that
    /// differed, and no two clients could be shown one avatar at one spot unless
    /// they happened to share an anchor.
    ///
    /// `None` is an avatar that is nowhere: every connection gets one, and not
    /// every connection is somebody standing in the world. The web UI signs in to
    /// chat and never says where it is; such an avatar is in nobody's snapshot
    /// and in earshot of nobody ([`SimWorld::placed_avatars`],
    /// [`SimWorld::embodied_place`]). The app sends its first intent on its first
    /// online frame, so for an app this lasts one frame.
    place: Option<GeoAnchor>,
    /// Height of the feet above the flat ground (`motion::GROUND_Y`), metres:
    /// what a jump and a fall change.
    alt: f32,
    yaw: f32,
    velocity: Vec3,
    /// Vertical velocity (m/s, up positive) for the jump/fall arc, integrated
    /// under [`motion::GRAVITY`]. It replaced a constant descent rate, which meant a
    /// fall ran at full speed from its first frame and a jump was impossible to
    /// express at all.
    vertical_velocity: f32,
    /// Set for one tick by a `ClientIntent` whose `jump` bit is set. Consumed by
    /// [`SimWorld::step`], which only honours it when the avatar is on the ground —
    /// the authority decides whether a jump happens, so a client cannot climb by
    /// spamming the bit.
    jump_requested: bool,
    /// The emote this avatar is currently broadcasting, and how long it has been
    /// doing so. Held for [`EMOTE_REPLICATE_SECS`] rather than forwarded once, so
    /// a client whose first snapshot of this avatar lands mid-wave still sees it.
    emote: Option<EmoteState>,
    emote_age: f32,
    /// Monotonic per-avatar counter. The same emote twice running is two events,
    /// and a receiver that diffed on the emote alone would play only the first.
    emote_sequence: u32,
    /// The expression the face is showing, held and aged as `emote` is
    /// ([`SimWorld::start_expression`]).
    expression: Option<ExpressionState>,
    expression_age: f32,
    expression_sequence: u32,
    /// What [`SimWorld::step`] actually moved this avatar by, per second, on its
    /// last tick — replicated as `AvatarStateDto::velocity`. Measured from the
    /// position change rather than assembled from `velocity` and
    /// `vertical_velocity`, so the ground clamp is already in it: an avatar landing from a jump reports zero, not the fall rate it
    /// had before the floor stopped it.
    moved: Vec3,
    /// The account this avatar is signed in as, for anything that has to *name*
    /// it — today that is `sensor`, which hands a script the name so a prim can
    /// greet whoever walked up. Set once at the handshake; "Someone" until then,
    /// which is what a connection that has not authenticated yet is.
    name: String,
    /// How this avatar looks (protocol 42), once its owner's client or the
    /// account has said; handed to clients as `AvatarLookOf`.
    look: Option<cartopolis_core::AvatarLook>,
    /// Where this avatar's body is drawn relative to [`Self::place`], while it
    /// is sitting on something (`AvatarStateDto::seated`).
    ///
    /// The sim holds it without understanding it: it has no map, so it cannot
    /// check that there is a bench there. What it *can* do is the two things a
    /// client must not be trusted with — bound it (`MAX_SEAT_OFFSET_M`) and drop
    /// it the moment the same avatar asks to move, so "sitting" cannot become a
    /// way to walk around displaced from your own position.
    seated: Option<[f32; 2]>,
    /// The physical prim this avatar is carrying (protocol 48,
    /// [`SimWorld::grab_prim`]). The sim holds it in front of the avatar every
    /// tick; sitting, a teleport, leaving, or the prim ceasing to be a
    /// carryable physical prim lets go of it.
    holding: Option<i64>,
    /// The car this avatar is at the wheel of (protocol 52,
    /// [`SimWorld::set_driving`]). The sim draws nothing from it but the
    /// speed cap: the handling, and the walls that stop a car, are its
    /// driver's client's.
    driving: Option<cartopolis_core::protocol::Driving>,
    /// Seconds a greeting still holds this avatar's facing (protocol 49,
    /// [`cartopolis_core::greet::GREET_HOLD_SECS`]): the sim turned it to its
    /// partner, and an intent sent before its client heard must not turn it
    /// back. Moving lets go at once.
    greet_hold: f32,
}

impl AvatarSim {
    /// Where the avatar stands, if anywhere.
    fn placed(&self) -> Option<Place> {
        self.place.map(|geo| Place {
            geo,
            alt_m: self.alt,
        })
    }
}

pub struct SimWorld {
    /// Every prim, in id order.
    prims: Vec<PrimDto>,
    /// Prim id -> position in `prims`.
    ///
    /// Every edit, permission check, script write and touch looks a prim up by
    /// id, and a linear scan of a 1,000-prim world is a thousand comparisons per
    /// gizmo frame per dragging client, under the write lock. Kept in step by the
    /// three methods that change the list's shape — [`Self::commit_prim`],
    /// [`Self::forget_prim`] and [`Self::new`] — and by nothing else, because
    /// nothing else inserts or removes.
    index: HashMap<i64, usize>,
    avatars: HashMap<u64, AvatarSim>,
    next_avatar_id: u64,
    /// Creates that have claimed room against the budget and are inserting
    /// their row with this lock released ([`Self::reserve_prim`]).
    pending_prims: u32,
    /// What this world may hold at once, from `cartopolis.toml`.
    ///
    /// One budget for the server. It used to be a row per region, on the
    /// reasoning that a region is the unit an operator hands out — but a
    /// simulator only ever ran one region, so the ceiling was already the
    /// server's, and it now sits with the rest of an operator's decisions
    /// instead of in a table.
    caps: WorldCaps,
    /// Prim id -> the account that built it (schema V15), for the edit and delete
    /// permission. Absent means nobody owns it, which is how everything built
    /// before V15 and everything a content pack installed reads.
    ///
    /// In memory rather than queried, because `UpdatePrim` arrives at the gizmo's
    /// drag rate: a permission check that read SQLite twenty times a second per
    /// dragging client would put the database on the movement path. Loaded once
    /// at startup and maintained here, exactly as `scripted` is.
    owners: HashMap<i64, String>,
    /// Prims carrying a script, so the script budget can be counted without
    /// touching the database on every create.
    scripted: HashSet<i64>,
    /// Prims mutated by a script since the last flush.
    ///
    /// Script writes land in memory and replicate through the ordinary
    /// `WorldSnapshot` — prims are already in it — and are written back to SQLite
    /// on a timer instead. A moving prim would otherwise be an UPDATE at the tick
    /// rate, which is 20 disk writes a second per script for state that is
    /// re-derivable and changes again immediately.
    dirty: HashSet<i64>,
    /// Prims a script deleted (`die`) whose rows are still to be removed —
    /// the deletion half of `dirty` ([`Self::retire_prim`]).
    retired: HashSet<i64>,
    /// Who has been near whom, by lowercase username pair (sorted), for the
    /// "people you have been near" request setting (`contacts`). Pruned to
    /// [`MET_WINDOW_MS`] as it is written. In memory on purpose: it is an hour of
    /// who-stood-where, which is exactly the kind of thing not to keep on disk.
    met: HashMap<(String, String), cartopolis_core::MetDto>,
    /// Seconds since the last pass over [`Self::met`].
    meet_clock: f32,
    /// The rigid-body step for physical prims (`physics.rs`).
    physics: Physics,
    /// What the last [`Self::step`] moved, for the tick loop to send as one
    /// `PrimMotion` ([`Self::take_motion`]).
    motion: Vec<PrimBody>,
    /// Greetings offered and not yet taken (protocol 49, `greet.rs`).
    greetings: Vec<greet::Offer>,
    /// Offers withdrawn because their offerer moved, `(to, from)`, for the
    /// connection that moved to tell the one offered ([`Self::take_withdrawn`]).
    withdrawn: Vec<(u64, u64)>,
}

/// How far past a prim's `hand_range` a reported hand is still accepted: the
/// client tests the range against its own copy of the prim, which can lag the
/// script's by a tick, and a hand on the boundary should not flicker.
const HAND_RANGE_SLACK_M: f32 = 0.25;

/// Two avatars this close count as having met (`contacts`). Twice the Here
/// chat's say radius: close enough to have been talking, loose enough that
/// walking past each other on a square counts.
pub const MEET_RADIUS_M: f32 = 2.0 * crate::chat::SAY_RADIUS_M;

/// How long having been near somebody lets them send you a request.
pub const MET_WINDOW_MS: u64 = 60 * 60 * 1000;

/// How often the meetings are measured. Every avatar against every other is
/// quadratic, and nobody meets anybody in under a couple of seconds.
const MEET_SCAN_SECS: f32 = 2.0;

fn wall_ms() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

fn pair(a: &str, b: &str) -> (String, String) {
    let (a, b) = (a.to_lowercase(), b.to_lowercase());
    if a <= b {
        (a, b)
    } else {
        (b, a)
    }
}

/// Furthest a `sensor` may see, whatever a script asks for.
///
/// A cap on the *gathering*, not on the question: [`SimWorld::script_views`]
/// measures every located avatar against every scripted prim once a tick, and
/// without a ceiling a script could ask about a continent and get one. It is
/// generous next to any plausible in-world use and stingy next to a planet.
const SENSOR_MAX_RANGE_M: f32 = 4_096.0;

/// Most neighbours carried to one script per tick. `sensor` returns the nearest
/// one, so this only bounds what a future "list everyone" call could ask for —
/// and bounds the per-tick allocation now, which is the reason it exists.
const SENSOR_MAX_RESULTS: usize = 16;

/// Ground distance between two geo anchors, in metres, as `f32` for the script
/// API. Non-finite inputs come back as infinity, which every caller filters out —
/// there is no distance that means "unknown", so the answer has to be one no
/// range test accepts.
fn ground_distance_m(a: GeoAnchor, b: GeoAnchor) -> f32 {
    ground_m(a, b)
}

/// Why a [`SimWorld::grab_prim`] was refused. Each is a `ServerError` to the
/// client that asked, with [`Self::code`] and [`Self::message`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GrabRefusal {
    /// The avatar is not standing anywhere yet.
    Nowhere,
    Seated,
    /// Already carrying something else.
    HandsFull,
    NoSuchPrim,
    NotPhysical,
    TooBig,
    OutOfReach,
    HeldByOther,
}

impl GrabRefusal {
    /// The HTTP-style code the refusal travels with.
    #[must_use]
    pub fn code(self) -> u32 {
        match self {
            Self::NoSuchPrim => 404,
            Self::HandsFull | Self::HeldByOther => 409,
            Self::Nowhere | Self::Seated | Self::NotPhysical | Self::TooBig | Self::OutOfReach => {
                422
            }
        }
    }

    #[must_use]
    pub fn message(self) -> &'static str {
        match self {
            Self::Nowhere => "your avatar is not in the world yet",
            Self::Seated => "stand up first",
            Self::HandsFull => "your hands are full",
            Self::NoSuchPrim => "there is no such thing",
            Self::NotPhysical => "that is fixed in place",
            Self::TooBig => "that is too big or heavy to carry",
            Self::OutOfReach => "that is out of reach",
            Self::HeldByOther => "somebody else is holding that",
        }
    }
}

/// What the world may hold at once ([`SimWorld::caps`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WorldCaps {
    pub max_prims: u32,
    pub max_scripts: u32,
}

/// The world's budget and what is currently drawn against it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CapacityUsage {
    pub prims_used: u32,
    pub prims_max: u32,
    pub scripts_used: u32,
    pub scripts_max: u32,
}

impl SimWorld {
    /// A world holding `prims`, as loaded from the database.
    ///
    /// Takes no connection, and that is deliberate: nothing in here does I/O, so
    /// nothing done under the world lock can wait on the disk. The database is
    /// `crate::prims::PrimService`'s.
    pub fn new(
        mut prims: Vec<PrimDto>,
        caps: WorldCaps,
        scripted: HashSet<i64>,
        owners: HashMap<i64, String>,
    ) -> Self {
        // `db::load_world` already orders by id; sorting again is what keeps the
        // `partition_point` in `commit_prim` right for any caller.
        prims.sort_by_key(|p| p.id);
        let index = prims.iter().enumerate().map(|(i, p)| (p.id, i)).collect();
        Self {
            prims,
            index,
            avatars: HashMap::new(),
            next_avatar_id: 1,
            pending_prims: 0,
            caps,
            scripted,
            owners,
            dirty: HashSet::new(),
            retired: HashSet::new(),
            met: HashMap::new(),
            meet_clock: 0.0,
            physics: Physics::new(),
            motion: Vec::new(),
            greetings: Vec::new(),
            withdrawn: Vec::new(),
        }
    }

    pub fn spawn_avatar(&mut self) -> u64 {
        let id = self.next_avatar_id;
        self.next_avatar_id += 1;
        self.avatars.insert(
            id,
            AvatarSim {
                // Nowhere until its client says where (`AvatarSim::place`): the
                // sim holds no map and has nothing to spawn relative to.
                place: None,
                // Feet on the ground: the client avatar models are authored with
                // their origin at the feet, so the standing height is 0 (the sim
                // clamps it there below). The old +1.0 floated the humanoids a
                // metre up.
                alt: motion::GROUND_Y,
                // Match client tank convention: yaw π ↔ tank 0 ↔ travel −Z when pressing W.
                yaw: std::f32::consts::PI,
                velocity: Vec3::ZERO,
                vertical_velocity: 0.0,
                jump_requested: false,
                emote: None,
                emote_age: 0.0,
                emote_sequence: 0,
                expression: None,
                expression_age: 0.0,
                expression_sequence: 0,
                moved: Vec3::ZERO,
                name: "Someone".to_string(),
                look: None,
                seated: None,
                holding: None,
                driving: None,
                greet_hold: 0.0,
            },
        );
        id
    }

    /// Retire an avatar, handing back where on Earth it last stood, for the
    /// account to come back to (`db::save_last_place`).
    pub fn remove_avatar(&mut self, id: u64) -> Option<GeoAnchor> {
        self.avatars.remove(&id).and_then(|a| a.place)
    }

    /// Record who an avatar is, once the handshake knows.
    /// Wear `look` (protocol 42).
    pub fn set_avatar_look(&mut self, id: u64, look: cartopolis_core::AvatarLook) {
        if let Some(a) = self.avatars.get_mut(&id) {
            a.look = Some(look);
        }
    }

    /// Every online avatar that has a look, for a client that has just arrived.
    #[must_use]
    pub fn avatar_looks(&self) -> Vec<(u64, cartopolis_core::AvatarLook)> {
        self.avatars
            .iter()
            .filter_map(|(id, a)| a.look.clone().map(|l| (*id, l)))
            .collect()
    }

    pub fn set_avatar_name(&mut self, id: u64, name: &str) {
        if let Some(a) = self.avatars.get_mut(&id) {
            a.name = name.to_string();
        }
    }

    /// Who an avatar is signed in as, once it has said.
    #[must_use]
    pub fn avatar_name(&self, id: u64) -> Option<String> {
        self.avatars
            .get(&id)
            .filter(|a| a.name != "Someone")
            .map(|a| a.name.clone())
    }

    /// Where and when `a` and `b` were last near each other, within the last
    /// [`MET_WINDOW_MS`].
    #[must_use]
    pub fn met(&self, a: &str, b: &str) -> Option<cartopolis_core::MetDto> {
        let m = self.met.get(&pair(a, b))?;
        (wall_ms().saturating_sub(m.at_ms) <= MET_WINDOW_MS).then_some(*m)
    }

    /// Metres between two signed-in people's avatars now, if both are in the
    /// world and located.
    #[must_use]
    pub fn distance_between(&self, a: &str, b: &str) -> Option<f32> {
        let geo_of = |n: &str| {
            self.avatars
                .values()
                .find(|av| av.name.eq_ignore_ascii_case(n))
                .and_then(|av| av.place)
        };
        let d = ground_distance_m(geo_of(a)?, geo_of(b)?);
        d.is_finite().then_some(d)
    }

    /// Where each of `names` has an avatar in the world with a location: the
    /// avatar's id and where it is on Earth. A name with no located avatar is
    /// left out; one signed in on two devices answers with the first found.
    #[must_use]
    pub fn whereabouts(&self, names: &[String]) -> Vec<(String, u64, GeoAnchor)> {
        names
            .iter()
            .filter_map(|n| {
                self.avatars
                    .iter()
                    .filter(|(_, av)| av.name.eq_ignore_ascii_case(n))
                    .find_map(|(id, av)| Some((n.clone(), *id, av.place?)))
            })
            .collect()
    }

    /// Every avatar standing in the world with a location: its name and where it
    /// is on Earth, for `crate::stats` to bin into squares once a minute. Lower
    /// case, so one person on two devices is one person.
    #[must_use]
    pub fn stats_positions(&self) -> Vec<(String, f64, f64)> {
        self.avatars
            .values()
            .filter_map(|av| {
                let g = av.place?;
                Some((av.name.to_lowercase(), g.latitude, g.longitude))
            })
            .collect()
    }

    /// Everybody within [`MEET_RADIUS_M`] of `name`'s avatar, nearest first.
    #[must_use]
    pub fn near(&self, name: &str) -> Vec<(String, f32)> {
        let Some(me) = self
            .avatars
            .values()
            .find(|av| av.name.eq_ignore_ascii_case(name))
            .and_then(|av| av.place)
        else {
            return Vec::new();
        };
        let mut out: Vec<(String, f32)> = self
            .avatars
            .values()
            .filter(|av| av.name != "Someone" && !av.name.eq_ignore_ascii_case(name))
            .filter_map(|av| Some((av.name.clone(), ground_distance_m(me, av.place?))))
            .filter(|(_, d)| *d <= MEET_RADIUS_M)
            .collect();
        out.sort_by(|a, b| a.1.total_cmp(&b.1));
        out.dedup_by(|a, b| a.0.eq_ignore_ascii_case(&b.0));
        out
    }

    /// Record every pair of located avatars within [`MEET_RADIUS_M`] of each
    /// other, and forget meetings older than the window.
    fn note_meetings(&mut self) {
        let now = wall_ms();
        let located: Vec<(&str, GeoAnchor)> = self
            .avatars
            .values()
            .filter(|a| a.name != "Someone")
            .filter_map(|a| Some((a.name.as_str(), a.place?)))
            .collect();
        for (i, (a, ga)) in located.iter().enumerate() {
            for (b, gb) in &located[i + 1..] {
                if a.eq_ignore_ascii_case(b) || ground_distance_m(*ga, *gb) > MEET_RADIUS_M {
                    continue;
                }
                self.met.insert(
                    pair(a, b),
                    cartopolis_core::MetDto {
                        at_ms: now,
                        latitude: (ga.latitude + gb.latitude) / 2.0,
                        longitude: (ga.longitude + gb.longitude) / 2.0,
                    },
                );
            }
        }
        self.met
            .retain(|_, m| now.saturating_sub(m.at_ms) <= MET_WINDOW_MS);
    }

    /// Place an avatar (protocol 17, `NetMessage::Teleport`): the counterpart of
    /// [`Self::apply_intent`], which can only ask to *move*. Every jump in the
    /// client — a searched place, a bookmark, a shared link, a distant GPS fix —
    /// funnels here when a session is online; without it the client moved only
    /// its local entity and the next snapshot pulled the avatar straight back.
    ///
    /// Motion state is cleared along with the position, for the same reason the
    /// client's own teleport lands the camera at rest: velocity carried across a
    /// jump slides the avatar off the place it asked for, and a jump arc from
    /// the origin means landing mid-fall at the destination. The altitude is
    /// clamped like [`Self::step`] clamps it, so a malformed teleport cannot put
    /// an avatar underground.
    pub fn teleport_avatar(&mut self, id: u64, geo: GeoAnchor, alt_m: f32) {
        // Same rule as `apply_intent`, and the same reason: postcard decodes a
        // float from raw bytes, a NaN place poisons every later snapshot and is
        // at no distance from anyone, and there is no honest partial application
        // of a malformed placement.
        if !(Place { geo, alt_m }).is_valid() {
            tracing::debug!(id, "discarded a teleport to a place that is not one");
            return;
        }
        let Some(av) = self.avatars.get_mut(&id) else {
            return;
        };
        av.place = Some(geo);
        av.alt = alt_m.max(motion::GROUND_Y);
        // What was in the hands stays where the avatar left: carried along, it
        // would be a way to move a ball across the world in one message. The
        // next step drops it where it was held.
        av.holding = None;
        av.velocity = Vec3::ZERO;
        av.vertical_velocity = 0.0;
        av.jump_requested = false;
        // A placement is not travel: a receiver extrapolating the pre-jump walk
        // would draw the avatar strolling on from where it landed.
        av.moved = Vec3::ZERO;
        // `debug`, not `info`: this stopped being one message per deliberate jump
        // when device tracking started sending a placement per GNSS fix, i.e. at
        // ~1 Hz for as long as somebody is driving or has the locate button lit.
        // A line a second per tracked client is a log nobody can read the rest of.
        tracing::debug!(
            id,
            lat = geo.latitude,
            lng = geo.longitude,
            "avatar teleported"
        );
    }

    /// Where an avatar stands, if its client has placed it ([`AvatarSim::place`]).
    /// What anything that delivers by distance — nearby chat, voice, a prim's
    /// speech — must ask, so a connection that only chats is never in earshot
    /// of anyone.
    pub fn embodied_place(&self, id: u64) -> Option<Place> {
        self.avatars.get(&id).and_then(AvatarSim::placed)
    }

    /// Whether an avatar exists at all, placed or not.
    #[must_use]
    pub fn has_avatar(&self, id: u64) -> bool {
        self.avatars.contains_key(&id)
    }

    /// How many avatars are in the world — one per connected client, so this is
    /// the population reported by `/api/health`.
    pub fn avatar_count(&self) -> usize {
        self.avatars.len()
    }

    /// Every prim in the world, in id order.
    ///
    /// The AoI filter in [`SimWorld::snapshot`] is what a *client* sees; this is
    /// the whole set, which is what the authoring API lists and what the tests
    /// assert against. An operator listing the world's contents is not standing
    /// anywhere in it.
    pub fn prims(&self) -> &[PrimDto] {
        &self.prims
    }

    #[allow(clippy::too_many_arguments)]
    pub fn apply_intent(
        &mut self,
        avatar_id: u64,
        move_x: f32,
        move_z: f32,
        display_yaw: f32,
        jump: bool,
        emote: Option<Emote>,
        seated: Option<[f32; 2]>,
        geo: Option<GeoAnchor>,
    ) {
        // Every float in an intent must be a real number, or the whole intent is
        // discarded.
        //
        // postcard decodes an `f32` from four raw little-endian bytes, so a client
        // can put any bit pattern it likes on the wire, including infinity — and
        // infinity is the one that gets through. The magnitude test below rejects
        // NaN by accident (`NaN > 1e-4` is false) but *accepts* `inf`, and
        // `v / inf` is NaN, which lands in `velocity`, then in `position`, then in
        // every `WorldSnapshot` to every client, permanently: no later intent can
        // recover a position that is already NaN, because NaN + anything is NaN.
        //
        // Discarding the whole intent rather than sanitising the bad component is
        // deliberate. A frame like this is not a client having a rounding problem,
        // it is a client sending something no encoder here produces; there is no
        // part of it worth honouring, and picking one would mean deciding what a
        // malformed jump bit means. `display_yaw` is guarded downstream by
        // `snap_yaw_continuation`, but it is checked here too so the rule is one
        // rule rather than a note about which fields happen to be safe.
        if !move_x.is_finite() || !move_z.is_finite() || !display_yaw.is_finite() {
            tracing::debug!(avatar_id, "discarded an intent with a non-finite field");
            return;
        }
        let Some(av) = self.avatars.get_mut(&avatar_id) else {
            return;
        };
        // The client's word on where it stands is taken once, to put an avatar
        // that is nowhere yet into the world; from then on the sim's own
        // integration is the authority and the report is not needed
        // (`NetMessage::ClientIntent::geo`). Filtered rather than trusted: a
        // non-finite or off-globe coordinate is no place, and an avatar put
        // there would be at a NaN distance from everyone — seen by nobody and
        // heard by nobody, for as long as that client stayed connected.
        if av.place.is_none() {
            if let Some(g) = geo.filter(|g| {
                Place {
                    geo: *g,
                    alt_m: 0.0,
                }
                .is_valid()
            }) {
                av.place = Some(g);
            }
        }
        // Horizontal speed is the intent *magnitude* scaled, capped
        // (`motion::sim_speed_for_wish` — the client predicts with the same
        // function). The magnitude is the throttle: a stroll, a walk and a run
        // are three magnitudes, which is why none of them needed a protocol
        // field of its own. The cap (`motion::MAX_SPEED`) is a run's worth: a
        // client asking for more — the flight speed of a pre-2026-11-14 client —
        // is clamped to it.
        let mut v = Vec3::new(move_x, 0.0, move_z);
        let mag = v.length();
        // At the wheel the cap is a car's (`motion::MAX_DRIVE_SPEED`), the one
        // thing the sim takes from `driving`.
        let driving = av.driving.is_some();
        if mag > 1e-4 {
            v = v / mag * motion::sim_speed_for_wish_capped(mag, driving);
        } else {
            v = Vec3::ZERO;
        }
        av.velocity.x = v.x;
        av.velocity.z = v.z;
        let moves = mag > 1e-4 || jump;
        // Always apply facing so remotes see orbit-camera rotation while idle
        // (not only when moving) — except while a greeting holds it turned to a
        // partner, until the avatar walks off.
        if moves {
            av.greet_hold = 0.0;
        }
        if av.greet_hold <= 0.0 {
            av.yaw = snap_yaw_continuation(av.yaw, display_yaw);
        }
        // Latch rather than assign: intents arrive faster than ticks and are rate
        // limited on top (`net::MIN_INTENT_INTERVAL`), so a jump that shared a tick
        // with a later "not jumping" intent would be silently dropped. `step`
        // clears it.
        // Nobody jumps out of a moving car.
        av.jump_requested |= jump && !driving;
        // The seat. A *level*, unlike the jump and the emote beside it: it holds
        // until the client says otherwise, so an avatar that comes into range
        // mid-sit is drawn sitting.
        //
        // Three ways to lose it, and each is the sim refusing to take the client's
        // word for something. A non-finite or over-long offset is rejected outright
        // — the client is describing a body somewhere its position is not, and
        // there is a limit to how far that may be. Asking to move ends the sit, so
        // sitting cannot become a way to walk around displaced. And a jump ends it
        // for the same reason.
        av.seated = seated.filter(|o| {
            o[0].is_finite()
                && o[1].is_finite()
                && (o[0] * o[0] + o[1] * o[1]) <= MAX_SEAT_OFFSET_M * MAX_SEAT_OFFSET_M
        });
        if moves || driving {
            av.seated = None;
        }
        // Sitting down puts down whatever was being carried: the seated pose
        // has the hands on the thighs.
        if av.seated.is_some() {
            av.holding = None;
        }
        // An emote is a one-shot: stamp it with a fresh sequence so receivers can
        // tell "waved again" from "still waving". A greeting is not the client's
        // to start: the sim starts it on both avatars when one is accepted
        // (`greet.rs`), and one asked for here would shake an empty hand.
        if let Some(e) = emote.filter(|e| !e.is_paired()) {
            av.emote_sequence = av.emote_sequence.wrapping_add(1);
            av.emote = Some(EmoteState {
                emote: e,
                sequence: av.emote_sequence,
            });
            av.emote_age = 0.0;
            // The kick emote is also a kick: a physical prim at the avatar's feet
            // is sent the way it faces (`physics::Physics::kick`).
            if e == Emote::Kick {
                if let Some(g) = av.place {
                    self.physics.kick(Kick {
                        geo: g,
                        feet_y: av.alt,
                        yaw: av.yaw,
                    });
                }
            }
        }
        // Walking off (or sitting down) withdraws any greeting this avatar
        // offered or was offered: the two are no longer where it was offered.
        if moves || seated.is_some() {
            self.withdraw_greetings_of(avatar_id);
        }
    }

    /// Put an avatar at the wheel of a car, or take it out (protocol 52).
    ///
    /// Checked as an intent's floats are: a car whose wheel angle is past any
    /// lock is not one a client of ours describes, and is refused. Getting in
    /// puts down whatever was being carried — both hands are on the wheel.
    pub fn set_driving(
        &mut self,
        avatar_id: u64,
        driving: Option<cartopolis_core::protocol::Driving>,
    ) {
        let Some(av) = self.avatars.get_mut(&avatar_id) else {
            return;
        };
        let driving = driving.filter(|d| d.steer_rad().abs() <= 0.8);
        if driving.is_some() {
            av.holding = None;
        }
        av.driving = driving;
    }

    pub fn step(&mut self, dt: f32) {
        self.meet_clock += dt;
        if self.meet_clock >= MEET_SCAN_SECS {
            self.meet_clock = 0.0;
            self.note_meetings();
        }
        self.age_greetings(dt);
        for av in self.avatars.values_mut() {
            // An avatar that is nowhere has nothing to integrate; its intent's
            // velocity waits for a place.
            let Some(geo) = av.place else {
                continue;
            };
            let alt_before = av.alt;
            // Metres east and south this tick, turned into degrees at this
            // latitude. Clients integrate in metres of their anchored Mercator
            // frame, which agrees with the ellipsoid's own scale to a part in
            // ten thousand over a reach, so prediction and authority differ by
            // less than a millimetre a tick.
            let dx = av.velocity.x * dt;
            let dz = av.velocity.z * dt;
            if dx != 0.0 || dz != 0.0 {
                let (per_lat, per_lng) = cartopolis_core::degree_scale_m(geo.latitude);
                let latitude = (geo.latitude - f64::from(dz) / per_lat).clamp(-90.0, 90.0);
                let mut longitude = geo.longitude + f64::from(dx) / per_lng;
                if longitude > 180.0 {
                    longitude -= 360.0;
                } else if longitude < -180.0 {
                    longitude += 360.0;
                }
                av.place = Some(GeoAnchor {
                    latitude,
                    longitude,
                });
            }
            // The vertical arc is `motion::step_fall` — the same function the
            // client predicts with and the `--offline` path integrates, so the
            // three cannot disagree. Every avatar is on it: there is no flight
            // (removed 2026-11-14), and a client that still asks for it is fallen
            // like anyone else.
            //
            // A jump is only granted from the ground: the authority decides, so
            // holding the key cannot climb.
            if av.jump_requested && av.alt <= motion::GROUND_Y + 1e-3 {
                av.vertical_velocity = motion::JUMP_SPEED;
            }
            av.jump_requested = false;
            let s = motion::step_fall(av.alt, av.vertical_velocity, motion::GROUND_Y, dt);
            av.alt = s.y;
            av.vertical_velocity = s.vertical_velocity;
            if dt > 0.0 {
                av.moved = Vec3::new(dx, av.alt - alt_before, dz) / dt;
            }
            // Age out the replicated emote so it stops being re-broadcast (and
            // re-played by anyone who arrives) long after it finished.
            if av.emote.is_some() {
                av.emote_age += dt;
                if av.emote_age > EMOTE_REPLICATE_SECS {
                    av.emote = None;
                }
            }
            if av.expression.is_some() {
                av.expression_age += dt;
                if av.expression_age > EXPRESSION_REPLICATE_SECS {
                    av.expression = None;
                }
            }
            if av.greet_hold > 0.0 {
                av.greet_hold -= dt;
            }
            // Horizontal yaw comes from [`SimWorld::apply_intent`] (`display_yaw`); do not derive from
            // `atan2(velocity)` (branch cuts + integration drift caused client flip-flops).
        }

        // Physical prims, after the avatars have moved so a push uses where they
        // are now. Avatars take part by their geographic fix, the only frame they
        // share with a prim; one that has not sent one cannot push anything.
        let probes: Vec<AvatarProbe> = self
            .avatars
            .iter()
            .filter_map(|(&id, a)| {
                a.place.map(|geo| AvatarProbe {
                    id,
                    geo,
                    feet_y: a.alt,
                })
            })
            .collect();
        // What each avatar carries, after checking it still may: a prim deleted,
        // switched off or rescaled past carrying since the grab is let go of.
        for av in self.avatars.values_mut() {
            if let Some(id) = av.holding {
                let still = self.index.get(&id).is_some_and(|&i| {
                    let p = &self.prims[i];
                    p.physical && crate::physics::can_carry(p)
                });
                if !still || av.place.is_none() {
                    av.holding = None;
                }
            }
        }
        let carries: Vec<Carry> = self
            .avatars
            .values()
            .filter_map(|a| {
                Some(Carry {
                    prim_id: a.holding?,
                    geo: a.place?,
                    feet_y: a.alt,
                    yaw: a.yaw,
                })
            })
            .collect();
        let moved = self.physics.step(dt, &mut self.prims, &probes, &carries);
        // Persisted on the same timer as a script's writes, so a ball left in the
        // goal is still in the goal after a restart.
        self.dirty.extend(moved.iter().map(|b| b.id));
        self.motion.extend(moved);
    }

    /// The physical prims the steps since the last call moved, one entry per
    /// prim (its latest pose), for one `PrimMotion`.
    pub fn take_motion(&mut self) -> Vec<PrimBody> {
        let mut out = std::mem::take(&mut self.motion);
        // Two steps between takes leave two entries for one prim; keep the last.
        out.reverse();
        let mut seen = HashSet::new();
        out.retain(|b| seen.insert(b.id));
        out.sort_unstable_by_key(|b| b.id);
        out
    }

    /// How many prims are physical now.
    #[must_use]
    pub fn physical_count(&self) -> usize {
        self.prims.iter().filter(|p| p.physical).count()
    }

    /// Switch a prim's physics on or off in memory. Refused (`Err`) when turning
    /// it on would put the world over [`MAX_PHYSICAL_PRIMS`].
    pub fn set_physical(&mut self, prim_id: i64, on: bool) -> Result<Option<PrimDto>, String> {
        let Some(&pos) = self.index.get(&prim_id) else {
            return Ok(None);
        };
        if on && !self.prims[pos].physical && self.physical_count() >= MAX_PHYSICAL_PRIMS {
            return Err(format!(
                "this world already has {MAX_PHYSICAL_PRIMS} physical prims, the most it may hold"
            ));
        }
        self.prims[pos].physical = on;
        Ok(Some(self.prims[pos].clone()))
    }

    /// How fast a physical prim is moving (`get_vel`).
    #[must_use]
    pub fn prim_velocity(&self, prim_id: i64) -> Vec3 {
        self.physics.velocity(prim_id)
    }

    /// Pick up a physical prim (protocol 48, `NetMessage::GrabPrim`).
    ///
    /// Anyone may, as anyone may kick the ball — it is not an edit, so there is
    /// no owner to ask. What the sim does check is everything a client must not
    /// be trusted with: that the prim is physical and small and light enough
    /// (`carry::can_carry`), that it is within [`carry::GRAB_REACH_M`] of where
    /// *the sim* holds the avatar, and that nobody else has it. One thing per
    /// avatar; asking again for the same one is granted, so a repeated key
    /// press is harmless.
    pub fn grab_prim(&mut self, avatar_id: u64, prim_id: i64) -> Result<(), GrabRefusal> {
        let av = self.avatars.get(&avatar_id).ok_or(GrabRefusal::Nowhere)?;
        if av.holding == Some(prim_id) {
            return Ok(());
        }
        if av.holding.is_some() {
            return Err(GrabRefusal::HandsFull);
        }
        let place = av.place.ok_or(GrabRefusal::Nowhere)?;
        if av.seated.is_some() {
            return Err(GrabRefusal::Seated);
        }
        // Nobody carries a draft: it is in its maker's design room.
        let p = self
            .prim(prim_id)
            .filter(|p| !p.draft)
            .ok_or(GrabRefusal::NoSuchPrim)?;
        if !p.physical {
            return Err(GrabRefusal::NotPhysical);
        }
        if !crate::physics::can_carry(p) {
            return Err(GrabRefusal::TooBig);
        }
        let flat = ground_distance_m(place, p.geo);
        let up = p.position.y - av.alt;
        if !(flat <= carry::GRAB_REACH_M
            && (-carry::GRAB_BELOW_M..=carry::GRAB_ABOVE_M).contains(&up))
        {
            return Err(GrabRefusal::OutOfReach);
        }
        if self
            .avatars
            .iter()
            .any(|(&id, a)| id != avatar_id && a.holding == Some(prim_id))
        {
            return Err(GrabRefusal::HeldByOther);
        }
        if let Some(av) = self.avatars.get_mut(&avatar_id) {
            av.holding = Some(prim_id);
        }
        Ok(())
    }

    /// Let go of whatever this avatar carries (`NetMessage::ReleasePrim`):
    /// thrown along its facing when `throw`, set down at its feet otherwise.
    /// Returns the prim let go of, `None` when the hands were empty.
    pub fn release_prim(&mut self, avatar_id: u64, throw: bool) -> Option<i64> {
        let av = self.avatars.get_mut(&avatar_id)?;
        let prim_id = av.holding.take()?;
        self.physics.release(Release {
            prim_id,
            throw,
            yaw: av.yaw,
            feet_y: av.alt,
            carrier_velocity: av.moved,
        });
        Some(prim_id)
    }

    /// What an avatar is carrying, if anything.
    #[must_use]
    pub fn holding(&self, avatar_id: u64) -> Option<i64> {
        self.avatars.get(&avatar_id).and_then(|a| a.holding)
    }

    /// Every placed avatar: its id, where it stands, and its state as a client
    /// receives it. What the tick loop hands `reach::TickTable`, which encodes
    /// each state once and lets every connection take the ones within its reach
    /// (`docs/notes/interest-by-reach.md`).
    ///
    /// **There is no area-of-interest filter here, and none belongs here.** The
    /// one ADR-012 had excluded whole *regions* by the distance from a single
    /// `observer` field every connection overwrote, while one snapshot was
    /// broadcast to all — so what it did was blank every prim for everybody as
    /// soon as one client walked off. A filter has to be computed per connection,
    /// which is what the connection does with this list; the world only says
    /// where everyone is.
    #[must_use]
    pub fn placed_avatars(&self) -> Vec<(u64, Place, AvatarStateDto)> {
        self.avatars
            .iter()
            .filter_map(|(&id, a)| {
                let place = a.placed()?;
                Some((
                    id,
                    place,
                    AvatarStateDto {
                        id,
                        geo: place.geo,
                        alt_m: place.alt_m,
                        yaw: a.yaw,
                        emote: a.emote,
                        expression: a.expression,
                        seated: a.seated,
                        velocity: a.moved,
                        holding: a.holding,
                        driving: a.driving,
                    },
                ))
            })
            .collect()
    }

    /// Every thing's id and place, for the same table: a thing is in a
    /// connection's world while it is within reach, and a `PrimUpsert` or a
    /// `PrimOutOfReach` marks each crossing.
    #[must_use]
    pub fn thing_places(&self) -> Vec<(i64, Place)> {
        self.prims
            .iter()
            .map(|p| {
                (
                    p.id,
                    Place {
                        geo: p.geo,
                        alt_m: p.position.y,
                    },
                )
            })
            .collect()
    }

    /// The face of `avatar_id` starts `expression`: a one-shot like an emote,
    /// stamped with a fresh sequence so receivers tell "smiled again" from
    /// "still smiling", and replicated for [`EXPRESSION_REPLICATE_SECS`].
    pub fn start_expression(&mut self, avatar_id: u64, expression: Expression) {
        if let Some(av) = self.avatars.get_mut(&avatar_id) {
            av.expression_sequence = av.expression_sequence.wrapping_add(1);
            av.expression = Some(ExpressionState {
                expression,
                sequence: av.expression_sequence,
            });
            av.expression_age = 0.0;
        }
    }

    // -----------------------------------------------------------------------
    // Prim storage — in memory only
    //
    // Nothing below touches SQLite, and that is the point of the shape: the world
    // is behind one write lock that the tick loop takes twenty times a second, so
    // a write that waited on the disk while holding it stalled every connected
    // client at once. The database half of each operation lives in
    // `crate::prims::PrimService`, which does it with this lock released and then
    // comes back here to make the in-memory copy agree.
    // -----------------------------------------------------------------------

    /// Claim room for one prim that is about to be inserted into the database.
    ///
    /// Capacity is checked *before* the insert, so the refusal costs no write and
    /// the id sequence does not advance on a rejected create. It is a
    /// reservation rather than a bare check because the insert now happens with
    /// the world lock released: two creates that both checked `len()` and then
    /// both inserted would put the world one prim over its budget. Every
    /// reservation ends in exactly one [`Self::commit_prim`] or
    /// [`Self::release_prim_reservation`].
    pub fn reserve_prim(&mut self) -> Result<(), String> {
        let used = self.prims.len() as u32 + self.pending_prims;
        if used >= self.caps.max_prims {
            return Err(format!(
                "the world is full: {used} of {} prims. Delete something or ask the operator to raise the budget.",
                self.caps.max_prims
            ));
        }
        self.pending_prims += 1;
        Ok(())
    }

    /// Give back a reservation whose insert did not happen.
    pub fn release_prim_reservation(&mut self) {
        self.pending_prims = self.pending_prims.saturating_sub(1);
    }

    /// Add a prim the database has just stored, spending its reservation.
    ///
    /// Inserted in id order rather than pushed: [`Self::prims`] promises id
    /// order, and two creates that finished their inserts in one order may reach
    /// here in the other.
    pub fn commit_prim(&mut self, mut prim: PrimDto, owner: Option<&str>) {
        self.release_prim_reservation();
        prim.scripted = self.scripted.contains(&prim.id);
        if let Some(owner) = owner {
            self.owners.insert(prim.id, owner.to_string());
        }
        if let Some(&pos) = self.index.get(&prim.id) {
            // Cannot happen — the id is the database's and was never here — but
            // replacing is the answer that leaves the index honest if it does.
            self.prims[pos] = prim;
            return;
        }
        let pos = self.prims.partition_point(|p| p.id < prim.id);
        self.prims.insert(pos, prim);
        self.reindex_from(pos);
    }

    /// Replace a prim's in-memory copy with what the database now holds for it.
    ///
    /// `false` when the prim is no longer in the world — a script can retire one
    /// (`die`) between an edit's database write and this call, and a caller
    /// that broadcast the edit anyway would hand every client a resurrected copy.
    pub fn replace_prim(&mut self, mut prim: PrimDto) -> bool {
        let Some(&pos) = self.index.get(&prim.id) else {
            return false;
        };
        // The in-memory flag is the one maintained beside the script store, so it
        // wins over whatever the row read back said.
        prim.scripted = self.scripted.contains(&prim.id);
        // A looping sound is never in the row at all (it is not persisted), so an
        // edit carries the one playing across rather than silencing it.
        prim.sound_loop = self.prims[pos].sound_loop;
        // Nor is a hand range or a tone: an edit to a prim's colour must not
        // stop it sensing hands or silence it.
        prim.hand_range = self.prims[pos].hand_range;
        prim.tone = self.prims[pos].tone;
        // Physics is switched in memory first and written after (a script's
        // `set_physical` reaches the row at the next flush), so memory wins here
        // as it does for `scripted`.
        prim.physical = self.prims[pos].physical;
        self.prims[pos] = prim;
        true
    }

    /// Drop a prim the database has already deleted.
    ///
    /// Returns whether it was here. Releases everything held against it — its
    /// script budget (`db::delete_prim` removed the script row too), its owner
    /// and any unflushed script write.
    pub fn forget_prim(&mut self, prim_id: i64) -> bool {
        let Some(pos) = self.index.remove(&prim_id) else {
            return false;
        };
        self.prims.remove(pos);
        self.reindex_from(pos);
        self.scripted.remove(&prim_id);
        self.owners.remove(&prim_id);
        self.dirty.remove(&prim_id);
        true
    }

    /// Whether a reported hand is one the prim it names wants: the prim senses
    /// hands, and this one is within its range.
    #[must_use]
    pub fn hand_in_range(&self, pose: &HandPose) -> bool {
        self.prim(pose.prim_id).is_some_and(|p| {
            !p.draft && p.hand_range > 0.0 && pose.distance() <= p.hand_range + HAND_RANGE_SLACK_M
        })
    }

    /// Remove a prim now and delete its row at the next flush.
    ///
    /// For `die`, which is applied inside the tick while this lock is held and
    /// so cannot wait on the database the way a person's delete does. The world
    /// is the authority — the prim is gone for every client at once — and the row
    /// follows on the same cadence script writes do; a failed delete stays queued
    /// and is retried, exactly as a failed script write stays dirty.
    pub fn retire_prim(&mut self, prim_id: i64) -> bool {
        if !self.forget_prim(prim_id) {
            return false;
        }
        self.retired.insert(prim_id);
        true
    }

    /// Re-point the id index at every prim from `from` onwards, after an insert
    /// or removal shifted them.
    fn reindex_from(&mut self, from: usize) {
        for (i, p) in self.prims.iter().enumerate().skip(from) {
            self.index.insert(p.id, i);
        }
    }

    /// A prim by id, for the lookups a script command needs (its name and where
    /// it is standing when it speaks).
    pub fn prim(&self, prim_id: i64) -> Option<&PrimDto> {
        self.index.get(&prim_id).map(|&i| &self.prims[i])
    }

    /// Whether a prim carries a script. Cheap enough to ask per touch — the set is
    /// in memory precisely so this doesn't become a query.
    pub fn is_scripted(&self, prim_id: i64) -> bool {
        self.scripted.contains(&prim_id)
    }

    /// Does a prim by this id exist?
    #[must_use]
    pub fn has_prim(&self, prim_id: i64) -> bool {
        self.index.contains_key(&prim_id)
    }

    /// Budget and usage for the world, for [`NetMessage::WorldCapacity`].
    #[must_use]
    pub fn capacity(&self) -> CapacityUsage {
        let scripts_used = self
            .prims
            .iter()
            // A draft senses nobody: it is in the design room, not the street.
            .filter(|p| self.scripted.contains(&p.id) && !p.draft)
            .count() as u32;
        CapacityUsage {
            prims_used: self.prims.len() as u32,
            prims_max: self.caps.max_prims,
            scripts_used,
            scripts_max: self.caps.max_scripts,
        }
    }

    /// Who owns a prim, if anybody does.
    #[must_use]
    pub fn prim_owner(&self, prim_id: i64) -> Option<&str> {
        self.owners.get(&prim_id).map(String::as_str)
    }

    /// Record a prim's owner, after it has been written to the database.
    pub fn set_owner(&mut self, prim_id: i64, owner: &str) {
        self.owners.insert(prim_id, owner.to_string());
    }

    /// Is this prim `principal`'s?
    ///
    /// An **owned prim is its owner's**, and nobody else's. Before owners,
    /// `UpdatePrim` and `DeletePrim` were open to any connected client, so
    /// anything anybody built could be moved, recoloured, rescripted or deleted
    /// by anybody else.
    ///
    /// An **unowned prim belongs to the server**: nobody owns it, so only an
    /// account allowed to change anybody's prims (`prim_edit_any`; an admin)
    /// may touch it. That is everything built before schema V15 and everything a
    /// content pack installs, and it is the deliberate side of a real trade: the
    /// alternative — treating "no owner" as "anybody's" — would leave the
    /// firebird and the beacon deletable by any passer-by. An operator re-homes a
    /// genuinely orphaned build with `POST /api/content/transfer`.
    ///
    /// What an actor may *do* with a prim is its permissions' business
    /// (`prims::Actor::may_edit`, `docs/notes/permissions.md`); this answers only
    /// whose the prim is.
    #[must_use]
    pub fn owns(&self, prim_id: i64, principal: &str) -> bool {
        !principal.is_empty() && self.owners.get(&prim_id).is_some_and(|o| o == principal)
    }

    /// May `principal` know this prim exists? Everybody, unless it is a draft
    /// from the design room (`PrimDto::draft`), which is its owner's alone until
    /// it is placed. A prim that is not there is seen by nobody.
    #[must_use]
    pub fn sees(&self, prim_id: i64, principal: &str) -> bool {
        self.prim(prim_id)
            .is_some_and(|p| !p.draft || self.owns(prim_id, principal))
    }

    /// Record that a prim gained or lost a script, after the script budget has
    /// been checked with [`SimWorld::can_add_script`].
    pub fn set_scripted(&mut self, prim_id: i64, scripted: bool) {
        if scripted {
            self.scripted.insert(prim_id);
        } else {
            self.scripted.remove(&prim_id);
        }
        // And on the prim itself, which is what clients are sent (protocol 21).
        // `db::row_to_prim` reads the same fact out of `prim_scripts` on every
        // load, so this is the in-memory copy catching up rather than a second
        // source of truth — but without it a freshly scripted prim would not be
        // pressable until the server restarted.
        if let Some(&pos) = self.index.get(&prim_id) {
            self.prims[pos].scripted = scripted;
        }
    }

    /// Whether `prim_id` may take a script. Replacing an existing one is always
    /// allowed — it consumes no additional budget.
    pub fn can_add_script(&self, prim_id: i64) -> Result<(), String> {
        if self.scripted.contains(&prim_id) {
            return Ok(());
        }
        if !self.has_prim(prim_id) {
            return Err(format!("prim {prim_id} not found"));
        }
        let usage = self.capacity();
        if usage.scripts_used >= usage.scripts_max {
            return Err(format!(
                "the world is at its script limit: {} of {}",
                usage.scripts_used, usage.scripts_max
            ));
        }
        Ok(())
    }

    /// Read-only views of every scripted prim, handed to the host each tick.
    ///
    /// The neighbours are computed here rather than answered on demand from the
    /// script thread, and that is the whole shape of `sensor`: the host thread
    /// holds no reference to `SimWorld` (that is what keeps a script off the tick's
    /// write lock — see `scripting`'s module note), so anything a script can ask
    /// about has to have been pushed to it at the barrier. A sensor therefore
    /// reads a snapshot that is at most one tick old, which is the same freshness
    /// `get_pos` has always had.
    pub fn script_views(&self) -> Vec<(i64, PrimView)> {
        // Every avatar that has told us where on Earth it is. Gathered once for
        // the whole pass: a world with 200 scripted prims and 20 avatars would
        // otherwise rebuild this list 200 times a tick.
        let located: Vec<(u64, GeoAnchor, String)> = self
            .avatars
            .iter()
            .filter_map(|(id, a)| a.place.map(|g| (*id, g, a.name.clone())))
            .collect();
        self.prims
            .iter()
            .filter(|p| self.scripted.contains(&p.id))
            .map(|p| {
                let mut nearby: Vec<(u64, String, f32)> = located
                    .iter()
                    .map(|(id, g, name)| (*id, name.clone(), ground_distance_m(p.geo, *g)))
                    .filter(|(_, _, d)| d.is_finite() && *d <= SENSOR_MAX_RANGE_M)
                    .collect();
                // Nearest first, so `sensor(range)` is a prefix scan and the
                // truncation below drops the ones a script is least likely to
                // have been asking about.
                nearby.sort_by(|a, b| a.2.total_cmp(&b.2));
                nearby.truncate(SENSOR_MAX_RESULTS);
                (
                    p.id,
                    PrimView {
                        pos: p.position,
                        rot: p.rotation,
                        color: p.color,
                        scale: p.scale,
                        alpha: p.surface.alpha,
                        glow: p.surface.glow,
                        full_bright: p.surface.full_bright,
                        name: p.name.clone(),
                        particles: p.particles,
                        geo: p.geo,
                        nearby,
                        physical: p.physical,
                        velocity: self.physics.velocity(p.id),
                        mass: crate::physics::mass_of(p),
                    },
                )
            })
            .collect()
    }

    /// Apply a script's transform/appearance write in memory.
    ///
    /// Returns the mutated prim so the caller can broadcast a `PrimUpsert`.
    /// Nothing is persisted here — see [`SimWorld::take_dirty`] and the note on
    /// the `dirty` field.
    pub fn apply_script_command(&mut self, cmd: &ScriptCommand) -> Option<PrimDto> {
        let (prim_id, mutate): (i64, Box<dyn FnOnce(&mut PrimDto)>) = match cmd {
            ScriptCommand::SetPos { prim_id, pos } => {
                let pos = *pos;
                (*prim_id, Box::new(move |p: &mut PrimDto| p.position = pos))
            }
            ScriptCommand::SetColor { prim_id, color } => {
                let color = *color;
                (*prim_id, Box::new(move |p: &mut PrimDto| p.color = color))
            }
            ScriptCommand::SetScale { prim_id, scale } => {
                let scale = *scale;
                (*prim_id, Box::new(move |p: &mut PrimDto| p.scale = scale))
            }
            ScriptCommand::SetRot { prim_id, rot } => {
                let rot = *rot;
                (*prim_id, Box::new(move |p: &mut PrimDto| p.rotation = rot))
            }
            ScriptCommand::SetAlpha { prim_id, alpha } => {
                let alpha = *alpha;
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.surface.alpha = alpha),
                )
            }
            ScriptCommand::SetGlow { prim_id, glow } => {
                let glow = *glow;
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.surface.glow = glow),
                )
            }
            ScriptCommand::SetFullBright { prim_id, on } => {
                let on = *on;
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.surface.full_bright = on),
                )
            }
            ScriptCommand::SetName { prim_id, name } => {
                let name = name.clone();
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.name = name.clone()),
                )
            }
            ScriptCommand::SetGeo {
                prim_id,
                latitude,
                longitude,
            } => {
                let geo = GeoAnchor {
                    latitude: *latitude,
                    longitude: *longitude,
                };
                (*prim_id, Box::new(move |p: &mut PrimDto| p.geo = geo))
            }
            ScriptCommand::SetParticles { prim_id, effect } => {
                let effect = effect.map(|e| e.sanitized());
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.particles = effect),
                )
            }
            ScriptCommand::SetSoundLoop { prim_id, sound } => {
                let sound = sound.map(PrimSound::sanitized);
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.sound_loop = sound),
                )
            }
            ScriptCommand::SenseHands { prim_id, range } => {
                let range = range.clamp(0.0, cartopolis_core::tone::MAX_HAND_RANGE_M);
                (
                    *prim_id,
                    Box::new(move |p: &mut PrimDto| p.hand_range = range),
                )
            }
            // Written here but broadcast by the tick loop as a `PrimTone`, not
            // an upsert: a script following a hand sets it twenty times a second.
            ScriptCommand::SetTone { prim_id, tone } => {
                let pos = *self.index.get(prim_id)?;
                self.prims[pos].tone = tone.map(ToneSpec::sanitized);
                return None;
            }
            ScriptCommand::SetPhysical { prim_id, on } => {
                return match self.set_physical(*prim_id, *on) {
                    Ok(Some(p)) => {
                        self.dirty.insert(*prim_id);
                        Some(p)
                    }
                    Ok(None) => None,
                    Err(why) => {
                        tracing::debug!(prim_id, "script set_physical refused: {why}");
                        None
                    }
                };
            }
            // Motion, not a field write: the physics step applies it, and the
            // pose it produces goes out in `PrimMotion`.
            ScriptCommand::SetVel { prim_id, velocity } => {
                self.physics.set_velocity(*prim_id, *velocity);
                return None;
            }
            ScriptCommand::ApplyImpulse { prim_id, impulse } => {
                self.physics.apply_impulse(*prim_id, *impulse);
                return None;
            }
            // Not a field write: the caller routes these to chat, to every
            // connection, to the editor, and to `remove_prim` respectively.
            ScriptCommand::Say { .. }
            | ScriptCommand::PlaySound { .. }
            | ScriptCommand::Output { .. }
            | ScriptCommand::Die { .. } => return None,
        };
        let pos = *self.index.get(&prim_id)?;
        let prim = &mut self.prims[pos];
        mutate(prim);
        let out = prim.clone();
        self.dirty.insert(prim_id);
        Some(out)
    }

    /// Hand over everything scripts have changed since the last call, for
    /// [`crate::prims::PrimService::flush`] to write with this lock released.
    ///
    /// A *copy* of each dirty prim as it stands now, not a reference to it: the
    /// write happens after the lock is gone, while scripts carry on moving the
    /// same prims, and a later change simply marks the prim dirty again for the
    /// next flush. What the batch carries is therefore always a state the world
    /// really was in, which is all a crash-recovery copy has to be.
    ///
    /// The ids are drained. A batch that does not land must be handed back with
    /// [`Self::restore_dirty`], or those writes are lost until the prim next
    /// changes — the promise `flush` makes is that a failed write is retried.
    #[must_use]
    pub fn take_dirty(&mut self) -> DirtyBatch {
        let mut rows = Vec::with_capacity(self.dirty.len());
        for id in self.dirty.drain() {
            if let Some(&pos) = self.index.get(&id) {
                rows.push(self.prims[pos].clone());
            }
        }
        DirtyBatch {
            rows,
            retired: self.retired.drain().collect(),
        }
    }

    /// Mark a batch that did not reach the database as unwritten again.
    ///
    /// An updated prim that has since left the world is not re-marked — its row
    /// is being deleted, and writing it back would be the resurrection the
    /// delete exists to prevent. A retired id always is: its row still exists.
    pub fn restore_dirty(&mut self, batch: &DirtyBatch) {
        for p in &batch.rows {
            if self.index.contains_key(&p.id) {
                self.dirty.insert(p.id);
            }
        }
        self.retired.extend(batch.retired.iter().copied());
    }
}

/// What one flush writes: prims scripts have changed, and prims scripts have
/// deleted (`die`). See [`SimWorld::take_dirty`].
#[derive(Debug, Default)]
pub struct DirtyBatch {
    pub rows: Vec<PrimDto>,
    pub retired: Vec<i64>,
}

impl DirtyBatch {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty() && self.retired.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A world loaded from a throwaway database, so the prims it holds are ones
    /// the real schema produced rather than hand-built stand-ins.
    fn test_world() -> (SimWorld, tempfile::TempDir) {
        let dir = tempfile::TempDir::new().expect("temp dir");
        let path = dir.path().join("test.db");
        let conn = crate::db::open_and_migrate(path.to_str().unwrap()).expect("open db");
        let prims = crate::db::load_world(&conn).expect("load world");
        let world = SimWorld::new(
            prims,
            crate::config::SimConfig::default().world_caps(),
            HashSet::new(),
            HashMap::new(),
        );
        (world, dir)
    }

    /// Store a prim and add it to the world, the two halves
    /// `prims::PrimService::create` performs with the lock released between them.
    fn stored_prim(world: &mut SimWorld, dir: &tempfile::TempDir) -> PrimDto {
        let path = dir.path().join("test.db");
        let conn = crate::db::open_and_migrate(path.to_str().unwrap()).expect("open db");
        let prim =
            crate::db::insert_prim(&conn, Vec3::ZERO, "box", GeoAnchor::default(), None, None)
                .expect("insert prim");
        world.reserve_prim().expect("room for a prim");
        world.commit_prim(prim.clone(), None);
        prim
    }

    /// The id index has to follow every change to the list's shape, including a
    /// removal from the middle, which shifts every prim after it. A stale index
    /// would answer `prim(id)` with a *different* prim — the edit lands on the
    /// wrong object with nothing to say so.
    #[test]
    fn the_prim_index_follows_inserts_and_removals() {
        let (mut world, dir) = test_world();
        let a = stored_prim(&mut world, &dir);
        let b = stored_prim(&mut world, &dir);
        let c = stored_prim(&mut world, &dir);
        assert!(world.forget_prim(b.id));
        assert!(!world.has_prim(b.id));
        assert_eq!(world.prim(a.id).map(|p| p.id), Some(a.id));
        assert_eq!(world.prim(c.id).map(|p| p.id), Some(c.id));
        assert!(!world.forget_prim(b.id), "a second forget finds nothing");

        // A retirement is gone from the world at once and queued for the row.
        assert!(world.retire_prim(c.id));
        assert!(world.prim(c.id).is_none());
        let batch = world.take_dirty();
        assert_eq!(batch.retired, vec![c.id]);
        // And a failed flush hands the retirement back for the next one.
        world.restore_dirty(&batch);
        assert_eq!(world.take_dirty().retired, vec![c.id]);
        assert_eq!(world.prims().len(), 1);
    }

    /// A looping sound is never in the database, so an edit — which replaces the
    /// prim with the row read back — must carry it across rather than silence it.
    #[test]
    fn an_edit_keeps_the_loop_a_script_started() {
        use crate::scripting::ScriptCommand;
        use cartopolis_core::SoundName;
        let (mut world, dir) = test_world();
        let prim = stored_prim(&mut world, &dir);
        let sound = PrimSound {
            sound: SoundName::Bell,
            volume: 0.5,
            range_m: 40.0,
        };
        world
            .apply_script_command(&ScriptCommand::SetSoundLoop {
                prim_id: prim.id,
                sound: Some(sound),
            })
            .expect("prim is there");
        assert!(world.replace_prim(prim.clone()));
        assert_eq!(world.prim(prim.id).unwrap().sound_loop, Some(sound));
    }

    /// A hand is accepted only for a prim that senses hands and only within its
    /// range; an edit keeps the range and the tone; a tone is written to the
    /// prim but is not an upsert.
    #[test]
    fn hands_are_taken_within_range_and_tones_survive_an_edit() {
        use crate::scripting::ScriptCommand;
        let (mut world, dir) = test_world();
        let prim = stored_prim(&mut world, &dir);
        let hand = |offset| HandPose {
            prim_id: prim.id,
            offset,
            precise: true,
        };
        assert!(
            !world.hand_in_range(&hand([0.3, 0.0, 0.0])),
            "not sensing yet"
        );
        world
            .apply_script_command(&ScriptCommand::SenseHands {
                prim_id: prim.id,
                range: 2.0,
            })
            .expect("prim is there");
        assert!(world.hand_in_range(&hand([0.3, 0.0, 0.0])));
        assert!(!world.hand_in_range(&hand([3.0, 0.0, 0.0])));
        assert!(!world.hand_in_range(&hand([f32::NAN, 0.0, 0.0])));

        let tone = ToneSpec::default();
        assert!(world
            .apply_script_command(&ScriptCommand::SetTone {
                prim_id: prim.id,
                tone: Some(tone),
            })
            .is_none());
        assert_eq!(world.prim(prim.id).unwrap().tone, Some(tone));
        assert!(world.replace_prim(prim.clone()));
        let after = world.prim(prim.id).unwrap();
        assert_eq!(after.tone, Some(tone), "an edit keeps the tone");
        assert_eq!(after.hand_range, 2.0, "and the range");
    }

    /// A reservation counts against the budget before its row exists, so two
    /// creates racing through the database cannot both fit into the last slot.
    #[test]
    fn a_reservation_holds_its_place_in_the_budget() {
        let dir = tempfile::TempDir::new().expect("temp dir");
        let mut world = SimWorld::new(
            Vec::new(),
            WorldCaps {
                max_prims: 1,
                max_scripts: 1,
            },
            HashSet::new(),
            HashMap::new(),
        );
        world.reserve_prim().expect("the first create fits");
        assert!(world.reserve_prim().is_err(), "the second must not");
        world.release_prim_reservation();
        let _ = stored_prim(&mut world, &dir);
        assert!(world.reserve_prim().is_err(), "the world is full");
    }

    // Groningen's Grote Markt, where the avatar tests stand.
    const LAT: f64 = 53.2194;
    const LNG: f64 = 6.5665;

    fn here() -> GeoAnchor {
        GeoAnchor {
            latitude: LAT,
            longitude: LNG,
        }
    }

    /// The state a client is sent for `id`, if the avatar is placed.
    fn dto(world: &SimWorld, id: u64) -> Option<AvatarStateDto> {
        world
            .placed_avatars()
            .into_iter()
            .find(|(i, _, _)| *i == id)
            .map(|(_, _, d)| d)
    }

    /// Where `id` stands, in metres east and north of [`here`], and its height.
    fn offset_m(world: &SimWorld, id: u64) -> Vec3 {
        let p = world.embodied_place(id).expect("placed");
        let (per_lat, per_lng) = cartopolis_core::degree_scale_m(LAT);
        Vec3::new(
            ((p.geo.longitude - LNG) * per_lng) as f32,
            p.alt_m,
            ((p.geo.latitude - LAT) * per_lat) as f32,
        )
    }

    /// A connection that only chats (the web UI) never says where it stands, and
    /// must be neither drawn nor in earshot: no snapshot shows it and nothing
    /// delivered by distance finds it. Its first intent with a place gives it a
    /// body; one without a place does not.
    #[test]
    fn an_avatar_is_nowhere_until_its_client_places_it() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        assert!(dto(&world, id).is_none(), "nobody has placed it yet");
        assert_eq!(world.embodied_place(id), None);
        assert!(world.has_avatar(id), "it still exists");

        world.apply_intent(id, 0.0, 1.0, 0.0, false, None, None, None);
        world.step(0.05);
        assert!(
            dto(&world, id).is_none(),
            "an intent without a place puts nothing into the world"
        );

        world.apply_intent(id, 0.0, 0.0, 0.0, false, None, None, Some(here()));
        let d = dto(&world, id).expect("placed now");
        assert_eq!(d.geo, here());
        assert_eq!(d.alt_m, motion::GROUND_Y);
        assert_eq!(world.embodied_place(id), Some(Place::new(LAT, LNG, 0.0)));
    }

    /// Once placed, the avatar is moved by the sim and not by the client's
    /// reports: a later intent claiming to stand elsewhere moves nothing, and a
    /// tick of walking moves it the walk's distance over the ground — east for
    /// +x and south for +z, as the client's frame has it.
    #[test]
    fn the_sim_moves_a_placed_avatar_and_ignores_later_reports() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);

        let elsewhere = GeoAnchor {
            latitude: LAT + 0.01,
            longitude: LNG,
        };
        world.apply_intent(id, 1.0, 0.0, 0.0, false, None, None, Some(elsewhere));
        world.step(0.5);
        let o = offset_m(&world, id);
        let expect = motion::sim_speed_for_wish(1.0) * 0.5;
        assert!(
            (o.x - expect).abs() < 1e-2 && o.z.abs() < 1e-3,
            "walking east for half a second put the avatar at {o:?}, not {expect} m east"
        );

        world.apply_intent(id, 0.0, 1.0, 0.0, false, None, None, None);
        world.step(0.5);
        let o2 = offset_m(&world, id);
        assert!(
            (o2.x - o.x).abs() < 1e-3 && (-o2.z - expect).abs() < 1e-2,
            "walking +z for half a second should go {expect} m south: {o2:?}"
        );
    }

    /// A car may go at a car's speed, and only a car: the cap lifts for an
    /// avatar at the wheel and comes straight back down when it gets out.
    #[test]
    fn a_driver_may_go_at_a_cars_speed_and_a_walker_may_not() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);
        let car = cartopolis_core::protocol::Driving {
            kind: cartopolis_core::protocol::CarKind::Hatchback,
            paint: [200, 30, 30],
            steer: 0,
            braking: false,
        };
        // 30 m/s, as a wish.
        let wish = motion::wish_for_speed_capped(30.0, true);
        world.set_driving(id, Some(car));
        world.apply_intent(id, wish, 0.0, 0.0, false, None, None, None);
        world.step(1.0);
        let driven = offset_m(&world, id).x;
        assert!((driven - 30.0).abs() < 0.1, "drove {driven} m in a second");
        assert_eq!(dto(&world, id).expect("in view").driving, Some(car));

        // No jump from the driver's seat, and no seat beside it.
        world.apply_intent(id, 0.0, 0.0, 0.0, true, None, Some([0.3, 0.0]), None);
        world.step(0.1);
        let d = dto(&world, id).expect("in view");
        assert!(d.alt_m <= motion::GROUND_Y + 1e-3 && d.seated.is_none());

        // Out of the car the same wish is a run.
        world.set_driving(id, None);
        world.apply_intent(id, wish, 0.0, 0.0, false, None, None, None);
        world.step(1.0);
        let walked = offset_m(&world, id).x - driven;
        assert!((walked - motion::MAX_SPEED).abs() < 0.1, "ran {walked} m");
        assert_eq!(dto(&world, id).expect("in view").driving, None);

        // A wheel turned past any lock is no car of ours.
        world.set_driving(
            id,
            Some(cartopolis_core::protocol::Driving { steer: 120, ..car }),
        );
        assert_eq!(dto(&world, id).expect("in view").driving, None);
    }

    /// A hostile client must not be able to write a non-number into the
    /// authoritative avatar state.
    ///
    /// Infinity is the case that got through: the wire is raw `f32` bytes, the
    /// magnitude test only rejects NaN, and `v / inf` is NaN. Once the place is
    /// NaN it stays NaN — every subsequent intent adds to it — and it is at no
    /// distance from anyone for the life of the process.
    #[test]
    fn a_non_finite_intent_cannot_poison_the_avatar() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);
        let start = world.embodied_place(id).expect("placed");

        for (x, z, yaw) in [
            (f32::INFINITY, 0.0, 0.0),
            (f32::NEG_INFINITY, 0.0, 0.0),
            (0.0, f32::INFINITY, 0.0),
            (f32::NAN, f32::NAN, 0.0),
            (1.0, 0.0, f32::NAN),
        ] {
            world.apply_intent(id, x, z, yaw, false, None, None, None);
            world.step(0.05);
            let p = world.embodied_place(id).expect("still there");
            assert!(
                p.is_valid(),
                "intent ({x}, {z}, yaw {yaw}) put {p:?} into the authoritative place"
            );
        }
        assert_eq!(
            world.embodied_place(id),
            Some(start),
            "a rejected intent should move nothing at all"
        );

        // A place that is not one is refused too, and leaves the avatar where it
        // is rather than nowhere.
        let unplaced = world.spawn_avatar();
        for bad in [
            GeoAnchor {
                latitude: f64::NAN,
                longitude: LNG,
            },
            GeoAnchor {
                latitude: LAT,
                longitude: 181.0,
            },
            GeoAnchor {
                latitude: -91.0,
                longitude: LNG,
            },
        ] {
            world.apply_intent(unplaced, 0.0, 0.0, 0.0, false, None, None, Some(bad));
            assert_eq!(world.embodied_place(unplaced), None, "{bad:?} is no place");
            world.teleport_avatar(id, bad, 0.0);
            assert_eq!(
                world.embodied_place(id),
                Some(start),
                "{bad:?} moved the avatar"
            );
        }

        // And an ordinary intent still moves, so the guard is not simply refusing
        // everything.
        world.apply_intent(id, 0.0, 1.0, 0.0, false, None, None, None);
        world.step(0.05);
        assert!(
            offset_m(&world, id).z.abs() > 1e-4,
            "a valid intent should have moved the avatar"
        );
    }

    /// The seat is the one thing a client may use to draw its body away from the
    /// place this sim holds for it, so the sim polices it: it must be a real,
    /// short offset, and it does not survive the same client asking to move.
    ///
    /// Without the second rule "sitting" is a way to walk around a metre or two
    /// displaced from your own position — invisible to anything that reasons
    /// about where an avatar is, because that is still the honest number.
    #[test]
    fn a_seat_is_bounded_and_does_not_survive_moving() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);
        let seated = |w: &SimWorld| dto(w, id).expect("in view").seated;
        let sit = |w: &mut SimWorld, x: f32, z: f32, jump: bool, seat: [f32; 2]| {
            w.apply_intent(id, x, z, 0.0, jump, None, Some(seat), None);
        };

        // An ordinary seat: still, and within reach of a bench.
        sit(&mut world, 0.0, 0.0, false, [0.4, -0.9]);
        assert_eq!(seated(&world), Some([0.4, -0.9]));

        // Asking to move ends it, in the same intent that asks.
        sit(&mut world, 0.0, 1.0, false, [0.4, -0.9]);
        assert_eq!(seated(&world), None, "walking away should end the sit");

        // So does a jump.
        sit(&mut world, 0.0, 0.0, false, [0.4, -0.9]);
        sit(&mut world, 0.0, 0.0, true, [0.4, -0.9]);
        assert_eq!(seated(&world), None, "jumping should end the sit");

        // Nothing a client can put on the wire draws its body across the street,
        // and nothing it can put there is not a number.
        for bad in [
            [50.0, 0.0],
            [0.0, -50.0],
            [f32::INFINITY, 0.0],
            [f32::NAN, f32::NAN],
        ] {
            sit(&mut world, 0.0, 0.0, false, bad);
            assert_eq!(seated(&world), None, "{bad:?} should have been refused");
        }

        // And the place itself never moved through any of it.
        assert!(world.embodied_place(id).expect("placed").is_valid());
    }

    /// Sitting down puts down what was carried, and a sitter picks nothing up:
    /// the seated pose has its hands on its thighs.
    #[test]
    fn sitting_lets_go_and_a_sitter_picks_nothing_up() {
        let (mut world, dir) = test_world();
        let mut ball = stored_prim(&mut world, &dir);
        ball.shape = "sphere".into();
        ball.scale = Vec3::splat(0.22);
        ball.position.y = 0.11;
        ball.geo = here();
        assert!(world.replace_prim(ball.clone()));
        world.set_physical(ball.id, true).expect("room for one");
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);

        world.grab_prim(id, ball.id).expect("a ball at the feet");
        world.step(0.05);
        assert_eq!(dto(&world, id).expect("in view").holding, Some(ball.id));

        world.apply_intent(id, 0.0, 0.0, 0.0, false, None, Some([0.4, -0.9]), None);
        world.step(0.05);
        assert_eq!(world.holding(id), None, "sitting should put it down");
        assert_eq!(world.grab_prim(id, ball.id), Err(GrabRefusal::Seated));
    }

    /// A teleport (protocol 17) *places* the avatar: place set, motion cleared,
    /// and the placement survives a tick — the whole point is that the next
    /// snapshot confirms the jump instead of pulling the avatar back.
    #[test]
    fn a_teleport_places_the_avatar_and_the_placement_survives_a_tick() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);
        // Walking when the jump lands: the carried velocity must not slide the
        // avatar off the destination.
        world.apply_intent(id, 0.0, 1.0, 0.0, false, None, None, None);
        world.step(0.05);

        let dest = GeoAnchor {
            latitude: 52.3731,
            longitude: 4.8926,
        };
        world.teleport_avatar(id, dest, 0.05);
        assert_eq!(
            world.embodied_place(id),
            Some(Place::new(52.3731, 4.8926, 0.05))
        );
        world.step(0.05);
        let p = world.embodied_place(id).expect("still there");
        assert!(
            ground_m(p.geo, dest) < 1e-3,
            "carried velocity slid the avatar {} m after a teleport",
            ground_m(p.geo, dest)
        );

        // A below-ground placement is clamped to the floor.
        world.teleport_avatar(id, dest, -50.0);
        let sunk = world.embodied_place(id).expect("still there");
        assert!(sunk.alt_m >= 0.0, "teleported underground: {sunk:?}");
    }

    /// The replicated velocity is what the tick actually moved the avatar by —
    /// so it matches the walk, is zero once a teleport has placed it, and never
    /// reports travel into the floor.
    #[test]
    fn the_snapshot_carries_the_velocity_the_tick_moved_the_avatar_by() {
        let (mut world, _dir) = test_world();
        let id = world.spawn_avatar();
        world.teleport_avatar(id, here(), 0.0);
        let velocity = |world: &SimWorld| dto(world, id).expect("in the snapshot").velocity;
        assert_eq!(velocity(&world), Vec3::ZERO, "a fresh avatar is at rest");

        world.apply_intent(id, 0.0, 1.0, 0.0, false, None, None, None);
        let before = offset_m(&world, id);
        world.step(0.05);
        let after = offset_m(&world, id);
        let v = velocity(&world);
        assert!(v.length() > 0.5, "walking but reported {v:?}");
        // `offset_m` measures north; the wire's z runs south.
        let moved = Vec3::new(after.x - before.x, after.y - before.y, before.z - after.z) / 0.05;
        assert!(
            (moved - v).length() < 1e-2,
            "reported {v:?} for a move of {moved:?} in 50 ms"
        );
        assert!(
            v.y.abs() < 1e-6,
            "standing on the floor, yet vertical {v:?}"
        );

        world.teleport_avatar(id, here(), 0.0);
        assert_eq!(velocity(&world), Vec3::ZERO, "a placement is not travel");
    }
}