garage44.eu/jeroen/cartopolis

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

.. /crates/simulator/src/net/avatar.rs

raw

//! The avatar: movement intents, teleports and presence.

use super::{ConnCtx, FrameTransport, MIN_INTENT_INTERVAL};
use cartopolis_core::{Emote, Expression, GeoAnchor};
use std::time::Instant;

/// A `ClientIntent`, as the sim reads it.
pub(super) struct Intent {
    pub move_x: f32,
    pub move_z: f32,
    pub display_yaw: f32,
    pub jump: bool,
    pub emote: Option<Emote>,
    pub expression: Option<Expression>,
    pub seated: Option<[f32; 2]>,
    pub geo: Option<GeoAnchor>,
    pub driving: Option<cartopolis_core::protocol::Driving>,
}

pub(super) async fn on_intent<T: FrameTransport>(
    ctx: &mut ConnCtx<T>,
    intent: Intent,
) -> anyhow::Result<()> {
    // The rate limit drops surplus intents, but a jump and an emote are *edges* โ€”
    // dropping one loses the action outright rather than delaying it by a frame,
    // so they are never rate limited away.
    if ctx.gates.last_intent.elapsed() < MIN_INTENT_INTERVAL
        && !intent.jump
        && intent.emote.is_none()
        && intent.expression.is_none()
    {
        return Ok(());
    }
    ctx.gates.last_intent = Instant::now();
    let mut w = ctx.world.write().await;
    // Before the intent, so the speed it asks for is capped for a car.
    w.set_driving(ctx.avatar_id, intent.driving);
    w.apply_intent(
        ctx.avatar_id,
        intent.move_x,
        intent.move_z,
        intent.display_yaw,
        intent.jump,
        intent.emote,
        intent.seated,
        intent.geo,
    );
    if let Some(e) = intent.expression {
        w.start_expression(ctx.avatar_id, e);
    }
    // Walking off withdrew any greeting this avatar had offered; the one it
    // was offered to is told, so the prompt goes.
    let withdrawn = w.take_withdrawn();
    drop(w);
    super::greet::tell_withdrawn(ctx, withdrawn)
}

/// Deliberately not rate-limited like intents: a teleport is a deliberate act,
/// not a stream, and dropping one is not corrected by the next frame.
pub(super) async fn on_teleport<T: FrameTransport>(
    ctx: &mut ConnCtx<T>,
    geo: GeoAnchor,
    alt_m: f32,
) -> anyhow::Result<()> {
    ctx.world
        .write()
        .await
        .teleport_avatar(ctx.avatar_id, geo, alt_m);
    Ok(())
}

/// Deliberately not rate-limited, unlike intents and chat. It is an *edge* โ€” one
/// message when the app leaves the screen and one when it comes back โ€” and a
/// dropped one is not corrected by the next: refusing the "I'm back" would leave a
/// foregrounded client with no world state and nothing to make it ask again. A
/// client that flapped the flag would only be choosing whether to receive its own
/// snapshots.
pub(super) fn on_presence<T: FrameTransport>(ctx: &mut ConnCtx<T>, active: bool) {
    if ctx.presence_active != active {
        ctx.presence_active = active;
        tracing::debug!(avatar_id = ctx.avatar_id, active, "presence");
    }
}