ogym 0.1.0

Composable, type-safe reinforcement learning environments for Rust
Documentation
use super::utils::{Helipad, CHUNKS, MIDDLE};
use crate::env::rapier::{lunar_lander::config::LunarLanderConfig, world::PhysicsWorld};
use nalgebra::point;
use rand::rngs::StdRng;
use rand::Rng;
use rapier2d::prelude::{
    ActiveEvents, ColliderBuilder, RigidBodyBuilder, RigidBodyHandle, RigidBodyType,
};

pub fn generate_moon(
    config: &LunarLanderConfig,
    world: &mut PhysicsWorld,
    rng: &mut StdRng,
) -> (Helipad, RigidBodyHandle) {
    let w = config.get_scaled_width();
    let h = config.get_scaled_height();

    let mut helipad = Helipad {
        y: h / 4.0,
        ..Helipad::default()
    };

    let height: Vec<f32> = (0..=CHUNKS)
        .map(|i| match i {
            x if ((MIDDLE - 2)..=(MIDDLE + 2)).contains(&x) => helipad.y,
            _ => rng.random_range(0.0..h / 2.0),
        })
        .collect();

    let chunk_x: Vec<f32> = (0..CHUNKS)
        .map(|i| w / (CHUNKS - 1) as f32 * i as f32)
        .collect();

    helipad.x1 = chunk_x[MIDDLE - 1];
    helipad.x2 = chunk_x[MIDDLE + 1];

    let smooth_y: Vec<f32> = (0..CHUNKS)
        .map(|i| {
            0.33 * (height[i]
                + height[i + 1]
                + match i {
                    0 => height[CHUNKS],
                    _ => height[i - 1],
                })
        })
        .collect();

    let moon_body = RigidBodyBuilder::new(RigidBodyType::Fixed).build();
    let moon_handle = world.rigid_body_set.insert(moon_body);

    (0..(CHUNKS - 1)).for_each(|i| {
        let p1 = point![chunk_x[i], smooth_y[i]];
        let p2 = point![chunk_x[i + 1], smooth_y[i + 1]];
        let coll = ColliderBuilder::segment(p1, p2)
            .friction(0.1)
            .active_events(ActiveEvents::COLLISION_EVENTS)
            .build();
        world
            .collider_set
            .insert_with_parent(coll, moon_handle, &mut world.rigid_body_set);
    });

    (helipad, moon_handle)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::env::rapier::lunar_lander::config::LunarLanderConfig;
    use crate::env::rapier::world::PhysicsWorld;
    use rand::SeedableRng;
    use rapier2d::prelude::{ColliderSet, RigidBodySet};

    // Helper to create a basic PhysicsWorld for testing
    fn create_test_world() -> PhysicsWorld {
        PhysicsWorld {
            rigid_body_set: RigidBodySet::new(),
            collider_set: ColliderSet::new(),
            // Add other fields if PhysicsWorld has them, strictly strictly strictly strictly strictly strictly strictly strictly default or empty
            ..Default::default()
        }
    }

    #[test]
    fn test_moon_generation_basics() {
        let config = LunarLanderConfig::new()
            .with_viewport_size(600, 400)
            .with_scale(30.0);
        let mut world = create_test_world();
        let mut rng = StdRng::seed_from_u64(0);

        let (helipad, moon_handle) = generate_moon(&config, &mut world, &mut rng);

        // 1. Verify RigidBody creation
        assert!(
            world.rigid_body_set.contains(moon_handle),
            "Moon body should be inserted into the set"
        );
        let body = &world.rigid_body_set[moon_handle];
        assert!(body.is_fixed(), "Moon should be a fixed rigid body");

        // 2. Verify Helipad coordinates
        // Height is scaled: 400 / 30 = 13.333...
        // Helipad y is h / 4.0 = 3.333...
        let h = config.get_scaled_height();
        let w = config.get_scaled_width();

        assert_eq!(helipad.y, h / 4.0);

        // Calculate expected X coordinates based on CHUNKS logic
        // CHUNKS = 11, MIDDLE = 5
        // Width step = w / (11 - 1) = w / 10
        let step = w / 10.0;
        let expected_x1 = step * (5 - 1) as f32; // Index 4
        let expected_x2 = step * (5 + 1) as f32; // Index 6

        // Allow small float margin
        assert!((helipad.x1 - expected_x1).abs() < 1e-5);
        assert!((helipad.x2 - expected_x2).abs() < 1e-5);
    }

    #[test]
    fn test_moon_colliders_count() {
        let config = LunarLanderConfig::default();
        let mut world = create_test_world();
        let mut rng = StdRng::seed_from_u64(0);

        let (_, moon_handle) = generate_moon(&config, &mut world, &mut rng);

        // CHUNKS is 11. The loop runs 0..(CHUNKS-1), so 10 segments.
        // We need to count how many colliders are attached to the moon body.
        let collider_count = world
            .collider_set
            .iter()
            .filter(|(_, collider)| collider.parent() == Some(moon_handle))
            .count();

        assert_eq!(
            collider_count, 10,
            "Should generate CHUNKS - 1 ground segments"
        );
    }

    #[test]
    fn test_moon_colliders_properties() {
        let config = LunarLanderConfig::default();
        let mut world = create_test_world();
        let mut rng = StdRng::seed_from_u64(0);

        let (_, moon_handle) = generate_moon(&config, &mut world, &mut rng);

        // Check friction of the generated segments
        for (_, collider) in world.collider_set.iter() {
            if collider.parent() == Some(moon_handle) {
                assert_eq!(
                    collider.friction(),
                    0.1,
                    "Ground segments should have 0.1 friction"
                );
                assert!(
                    collider.shape().as_segment().is_some(),
                    "Ground should be made of segments"
                );
            }
        }
    }

    #[test]
    fn test_helipad_flatness() {
        // This test is slightly complex because of the randomness and smoothing.
        // However, we know the logic explicitly sets height[i] to helipad.y
        // for indices (MIDDLE - 2)..=(MIDDLE + 2).
        // The smoothing logic averages [i], [i+1], and [i-1].
        // If the input array has a sequence of identical values, the smoothed output
        // for the internal points of that sequence should be relatively flat or close to that value.

        let config = LunarLanderConfig::default();
        let mut world = create_test_world();
        let mut rng = StdRng::seed_from_u64(0);
        let (_, moon_handle) = generate_moon(&config, &mut world, &mut rng);

        // We want to inspect the Y coordinates of the segments specifically around the middle.
        // This verifies that the logic trying to flatten the helipad area is actually producing segments.

        let mut segments_found = false;
        for (_, collider) in world.collider_set.iter() {
            if collider.parent() == Some(moon_handle) {
                if let Some(segment) = collider.shape().as_segment() {
                    // Just verifying we can access the geometry
                    let _a = segment.a;
                    let _b = segment.b;
                    segments_found = true;
                }
            }
        }
        assert!(segments_found, "Should find segments attached to moon");
    }
}