Module minimap

Source
Expand description

Checked, allocation-free projection of caller-authorized planar points.

World coordinates are (x, y) in caller units (adapt an XZ plane explicitly with vec2(position.x, position.z)). WorldYAxis declares whether positive world Y points up or down. Map coordinates are logical UI units, X right and Y down. Positive rotation is clockwise on screen, in radians, applied after the Y convention. The world center stays at the map center. One uniform scale contains the entire rotated world rectangle, with centered letterboxing; there is no crop/stretch mode. Minimap::footprint is the actual rotated world boundary, which can occupy less than its axis-aligned bounding box.

Both rectangles reuse Rect. Negative extents are rejected, never normalized. Edges are min and the f32 sum min + size, matching UI geometry; world edges are closed for projection. Pointer picking first uses the map’s half-open bounds and the caller’s half-open safe/clip rectangle, then rejects letterbox/rotated-corner space outside the world footprint. No DPR conversion occurs here. Feed already-logical coordinates, or call super::Viewport::unproject exactly once before picking in viewport space.

Arithmetic widens before subtraction, scaling and rotation. Returned Vec2 coordinates round to f32; round trips are approximate, especially near large origins or thin rectangles. Picking allows half the larger adjacent f32 spacing per map coordinate around the world footprint (plus an f64 arithmetic allowance), then clamps that numerical fringe to its closed world edge. This never widens the half-open map/safe rectangles. Rectangles with subnormal/zero extents or collapsed/overflowing f32 edges are rejected, as is a fitted world boundary with collapsed/collinear corners. There is no exact-predicate or cross-platform bitwise-determinism promise.

No journal, discovery, renderer, input, simulation or global state is owned. Unknown/unauthorized points must be omitted by the caller; absence never creates a marker. Reconfiguration replaces all geometry transactionally; no separate reset is needed. Queries and batches retain no history and allocate no heap storage. All batches have a fixed MAX_MINIMAP_POINTS work bound.

§Runnable Crystal-style revealed-room and waypoint recipe

This uses supplied sample data, not Crystal’s real journal. Parent adoption must select only rooms already revealed by accepted game events, retain IDs, and authorize any picked waypoint. Geometry alone grants no gameplay access.

use ferravex_math::{Vec2, vec2};
use ferravex_play::ui::{Rect, Viewport};
use ferravex_play::ui::minimap::{
    MapConfig, Minimap, MinimapError, OffMapPolicy, ProjectionStatus, WorldYAxis,
};

fn main() -> Result<(), MinimapError> {
    let viewport = Viewport::responsive([400., 300.], [12., 20., 8., 10.]).unwrap();
    let config = MapConfig {
        world: Rect { min: [-20., -10.], size: [80., 40.] },
        map: Rect { min: [16., 24.], size: [240., 160.] },
        y_axis: WorldYAxis::Up,
        rotation_radians: 0.,
    };
    let mut map = Minimap::new(config)?;
    // None represents unknown/omitted location, never an inferred objective.
    let supplied_rooms = [(7, Some(vec2(0., 0.))), (9, None),
                          (12, Some(vec2(40., 20.)))];
    let mut ids = [0; 3];
    let mut known = [Vec2::ZERO; 3];
    let mut count = 0;
    for (id, point) in supplied_rooms {
        if let Some(point) = point {
            ids[count] = id;
            known[count] = point;
            count += 1;
        }
    }
    let seed = map.project(known[0], OffMapPolicy::ClampToEdge)?;
    let mut markers = [seed; 3];
    assert_eq!(map.project_into(&known[..count], OffMapPolicy::ClampToEdge,
                               &mut markers)?, 2);
    assert_eq!(&ids[..count], &[7, 12]); // No room 9 marker.
    assert_eq!(markers[0].status, ProjectionStatus::Inside);
    let waypoint = map.project(vec2(100., 10.), OffMapPolicy::ClampToEdge)?;
    assert_eq!(waypoint.status, ProjectionStatus::Clamped);
    assert!(waypoint.off_map.unwrap().direction.x > 0.);
    let outside = map.project(vec2(100., 10.), OffMapPolicy::Preserve)?;
    assert_eq!(outside.status, ProjectionStatus::Outside);
    assert_eq!(outside.off_map, waypoint.off_map);
    let boundary = map.footprint(); // Use the same vertices for painting.
    assert!(boundary.into_iter().all(Vec2::is_finite));
    // Host pointer is already in logical screen units, with no DPR rescaling.
    let host_pointer = [markers[0].position.x + 12., markers[0].position.y + 20.];
    let logical = viewport.unproject(host_pointer).unwrap();
    let picked = map.pick(Vec2::from_array(logical), viewport.bounds())?.unwrap();
    assert!((picked - known[0]).length() < 0.0001);
    let old = map;
    let mut invalid = map.config();
    invalid.world.size[0] = 0.;
    assert!(map.reconfigure(invalid).is_err());
    assert_eq!(map, old); // Last-good transform survives failure.
    map.reconfigure(config)?; // Also the scene/resize reset operation.
    Ok(())
}

Structs§

MapConfig
Explicit world and map geometry. See the module recipe for construction.
MapProjection
One projection, with no inferred ID, discovery or gameplay permission.
Minimap
Immutable checked geometry with transactional replacement, no heap storage. See the module recipe for all queries, batch projection and reconfiguration.
OffMapIndicator
Boundary marker for an outside point, regardless of projection policy.

Enums§

MinimapError
Checked failure. Rejected updates/batches leave their destinations unchanged.
OffMapPolicy
Treatment of finite points outside the closed world rectangle.
ProjectionStatus
Separates inside, outside/unclamped, and outside/clamped markers.
WorldYAxis
Positive planar world Y convention before clockwise screen rotation.

Constants§

MAX_MINIMAP_POINTS
Maximum input count for one atomic Minimap::project_into call.