ogym 0.1.0

Composable, type-safe reinforcement learning environments for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
use super::terrain::generate_moon;
use super::utils::{Helipad, Leg, LANDER_POLY, LANDER_POLY_WIDTH};
use crate::{
    env::{
        environment::{self, Environment, Error, Experience, Terminal},
        rapier::{lunar_lander::config::LunarLanderConfig, world::PhysicsWorld},
    },
    spaces::{Boxed, EnvSpace, Mixed, MixedItem},
};
use nalgebra::{point, Isometry2, SVector, Vector2};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use rapier2d::prelude::{
    ActiveEvents, ColliderBuilder, ColliderHandle, CollisionEvent, InteractionGroups,
    RevoluteJointBuilder, RigidBodyBuilder, RigidBodyHandle, RigidBodyType,
};
use std::f32::consts::PI;
use std::f64::consts::TAU;

const ACTION_SIZE: usize = 2;
const STATE_SIZE: usize = 8;

type State = SVector<f32, STATE_SIZE>;
type StateSpace = Boxed<STATE_SIZE>;

type Action = MixedItem<ACTION_SIZE>;
type ActionSpace = Mixed<ACTION_SIZE>;

pub struct LunarLander {
    config: LunarLanderConfig,
    t: u32,
    state: Option<State>,
    pub space: EnvSpace<StateSpace, ActionSpace>,
    helipad: Helipad,
    world: PhysicsWorld,
    lander: RigidBodyHandle,
    legs: Vec<Leg>,
    moon: RigidBodyHandle,
    crash: bool,
    prev_shaping: Option<f32>,
    wind_idx: f32,
    torque_idx: f32,
    rng: StdRng,
}

impl LunarLander {
    pub fn new(config: LunarLanderConfig) -> Result<Self, Error> {
        let ha = SVector::from_vec(vec![1.0, 1.0]);
        let hs = SVector::from_vec(vec![2.5, 2.5, 10.0, 10.0, TAU, 10.0, 1.0, 1.0]);
        let mut low = -hs;
        low[6] = 0.0;
        low[7] = 0.0;
        let space = EnvSpace {
            state: Boxed::new(low, hs)?,
            action: match config.continuous {
                true => Mixed::continuous(-ha, ha)?,
                false => Mixed::discrete(4)?,
            },
        };
        let mut lunar_lander = Self {
            config,
            t: 0,
            state: None,
            space,
            helipad: Helipad::default(),
            world: PhysicsWorld::new(config.gravity),
            lander: Default::default(),
            legs: Vec::new(),
            moon: Default::default(),
            prev_shaping: None,
            crash: false,
            wind_idx: 0.0,
            torque_idx: 0.0,
            rng: StdRng::seed_from_u64(10),
        };

        lunar_lander.reset(None)?;

        Ok(lunar_lander)
    }

    fn get_init_xy(&self) -> (f32, f32) {
        (
            self.config.get_scaled_width() / 2.0,
            self.config.get_scaled_height(),
        )
    }

    fn create_lander(&mut self) {
        let init_y = self.config.get_scaled_height();
        let init_x = self.config.get_scaled_width() / 2.0;
        let lander_poly = LANDER_POLY
            .iter()
            .map(|&(x, y)| point![x as f32 / self.config.scale, y as f32 / self.config.scale])
            .collect();
        let lander_pos = nalgebra::Isometry2::new(Vector2::new(init_x, init_y), 0.0);
        let lander_body = RigidBodyBuilder::new(RigidBodyType::Dynamic)
            .position(lander_pos)
            .build();
        let lander_handle = self.world.rigid_body_set.insert(lander_body);

        let collider = ColliderBuilder::convex_polyline(lander_poly)
            .unwrap()
            .density(5.0)
            .friction(0.1)
            .restitution(0.0)
            .collision_groups(InteractionGroups::new(0x0010.into(), 0x0001.into()))
            .active_events(ActiveEvents::COLLISION_EVENTS)
            .build();

        self.world.collider_set.insert_with_parent(
            collider,
            lander_handle,
            &mut self.world.rigid_body_set,
        );

        let force = self.config.initial_random;
        let force_x = self.rng.random_range(-force..force) as f32;
        let force_y = self.rng.random_range(-force..force) as f32;
        // Gymnasium applies this as a *force* (ApplyForceToCenter), whose effect
        // on velocity is scaled by the integration dt; an impulse of the same
        // raw magnitude would produce a far larger velocity kick.
        let dt = 1.0 / self.config.fps as f32;

        if let Some(body) = self.world.rigid_body_set.get_mut(lander_handle) {
            body.apply_impulse(Vector2::new(force_x * dt, force_y * dt), true);
        }

        self.lander = lander_handle;
    }

    fn create_legs(&mut self) {
        self.legs.clear();
        let (init_x, init_y) = self.get_init_xy();

        for i in [-1., 1.] {
            let pos = Isometry2::new(
                Vector2::new(
                    init_x - i * self.config.leg_offset_x / self.config.scale,
                    init_y,
                ),
                i * 0.05,
            );

            let body = RigidBodyBuilder::new(RigidBodyType::Dynamic)
                .position(pos)
                .build();
            let handle = self.world.rigid_body_set.insert(body);

            let hx = self.config.leg_width / self.config.scale;
            let hy = self.config.leg_length / self.config.scale;
            let coll = ColliderBuilder::cuboid(hx, hy)
                .density(1.0)
                .restitution(0.0)
                .collision_groups(InteractionGroups::new(0x0020.into(), 0x001.into()))
                .active_events(ActiveEvents::COLLISION_EVENTS)
                .build();

            self.world.collider_set.insert_with_parent(
                coll,
                handle,
                &mut self.world.rigid_body_set,
            );

            let joint = RevoluteJointBuilder::new()
                .local_anchor1(point![0.0, 0.0])
                .local_anchor2(point![
                    i * self.config.leg_offset_x / self.config.scale,
                    self.config.leg_offset_y / self.config.scale,
                ])
                .motor_velocity(0.3 * i, self.config.leg_spring_torque)
                .limits(if i == -1. { [0.4, 0.9] } else { [-0.9, -0.4] })
                .build();

            let joint_handle =
                self.world
                    .impulse_joint_set
                    .insert(self.lander, handle, joint, true);

            self.legs.push(Leg {
                body: handle,
                joint: joint_handle,
                ground_contact: false,
            });
        }
    }

    fn apply_engine_forces(&mut self, action: &Action) -> (f32, f32) {
        let mut main_force = 0.0;
        let mut side_force = 0.0;

        let (angle, translation) =
            if let Some(lander_body) = self.world.rigid_body_set.get(self.lander) {
                (lander_body.rotation().angle(), *lander_body.translation())
            } else {
                return (0.0, 0.0);
            };

        let tip = (angle.sin(), angle.cos());
        let side = (-tip.1, tip.0);
        let dispersion = [
            self.rng.random_range(-1.0..1.0) / self.config.scale,
            self.rng.random_range(-1.0..1.0) / self.config.scale,
        ];

        let main_engine_active = match action {
            MixedItem::Discrete(2) => true,
            MixedItem::Continuous(matrix) => matrix[0] > 0.0,
            _ => false,
        };

        if main_engine_active {
            main_force = match action {
                MixedItem::Discrete(2) => 1.0,
                MixedItem::Continuous(matrix) => 0.5 * matrix[0].clamp(0.0, 1.0) + 0.5,
                _ => 0.0,
            } as f32;

            let ox = tip.0
                * (self.config.main_engine_y_position / self.config.scale + 2.0 * dispersion[0])
                + side.0 * dispersion[1];
            let oy = -tip.1
                * (self.config.main_engine_y_position / self.config.scale + 2.0 * dispersion[0])
                - side.1 * dispersion[1];
            let impulse_pos = point![translation.x + ox, translation.y + oy];
            let force = Vector2::new(
                -ox * self.config.main_engine_force * main_force,
                -oy * self.config.main_engine_force * main_force,
            );

            if let Some(body) = self.world.rigid_body_set.get_mut(self.lander) {
                body.apply_impulse_at_point(force, impulse_pos, true);
            }
        }

        let (side_engine_active, direction) = match action {
            MixedItem::Discrete(1) => (true, -1.0),
            MixedItem::Discrete(3) => (true, 1.0),
            MixedItem::Continuous(actions) => (actions[1].abs() > 0.5, actions[1].signum()),
            _ => (false, 0.0),
        };

        if side_engine_active {
            side_force = match action {
                MixedItem::Continuous(actions) => actions[1].abs().clamp(0.5, 1.0),
                _ => 1.0,
            } as f32;

            let ox = tip.0 * dispersion[0]
                + side.0
                    * (3.0 * dispersion[1]
                        + (direction as f32 * self.config.side_engine_offset_x
                            / self.config.scale));
            let oy = -tip.1 * dispersion[0]
                - side.1
                    * (3.0 * dispersion[1]
                        + (direction as f32 * self.config.side_engine_offset_x
                            / self.config.scale));
            let impulse_pos = point![
                translation.x + ox - tip.0 * LANDER_POLY_WIDTH / 2.0 / self.config.scale,
                translation.y + oy + tip.1 * self.config.side_engine_offset_y / self.config.scale
            ];
            let force = Vector2::new(
                -ox * side_force * self.config.side_engine_force,
                -oy * side_force * self.config.side_engine_force,
            );

            if let Some(body) = self.world.rigid_body_set.get_mut(self.lander) {
                body.apply_impulse_at_point(force, impulse_pos, true);
            }
        }

        (main_force, side_force)
    }

    fn apply_wind_effects(&mut self) {
        if self.legs[0].ground_contact
            || self.legs[1].ground_contact
            || self.config.wind_strength.is_none()
        {
            return;
        }
        self.wind_idx += 1.0;
        self.torque_idx += 1.0;
        let c = ((0.02 * self.wind_idx).sin() + (PI * 0.01 * self.wind_idx).sin()).tanh();
        let wind_power = self.config.wind_strength.unwrap_or(0.0);
        let wind_mag = c * wind_power;
        let torque_mag = ((0.02 * self.torque_idx).sin() + (PI * 0.01 * self.torque_idx).sin())
            .tanh()
            * self.config.turbulence_strength;
        if let Some(lander_body) = self.world.rigid_body_set.get_mut(self.lander) {
            lander_body.apply_impulse(Vector2::new(wind_mag, 0.0), true);
            lander_body.apply_torque_impulse(torque_mag, true);
        }
    }

    fn get_state(&self) -> State {
        if let Some(lander_body) = self.world.rigid_body_set.get(self.lander) {
            let pos = lander_body.translation();
            let vel = lander_body.linvel();
            let theta = lander_body.rotation().angle();
            let omega = lander_body.angvel();
            let fps = self.config.fps as f32;
            SVector::from_vec(vec![
                ((pos.x - self.config.get_scaled_width() / 2.0)
                    / (self.config.get_scaled_width() / 2.0)),
                ((pos.y - (self.helipad.y + self.config.leg_offset_y / self.config.scale))
                    / (self.config.get_scaled_height() / 2.0)),
                vel.x * (self.config.get_scaled_width() / 2.0) / fps,
                vel.y * (self.config.get_scaled_height() / 2.0) / fps,
                theta,
                20.0 * omega / fps,
                if self.legs[0].ground_contact {
                    1.0
                } else {
                    0.0
                },
                if self.legs[1].ground_contact {
                    1.0
                } else {
                    0.0
                },
            ])
        } else {
            SVector::zeros()
        }
    }

    fn calc_reward(&self, state: &State, main_force: f32, side_force: f32) -> (f32, f32) {
        let shaping = -100.0 * (state[0] * state[0] + state[1] * state[1]).sqrt()
            - 100.0 * (state[2] * state[2] + state[3] * state[3]).sqrt()
            - 100.0 * state[4].abs()
            + 10.0 * state[6]
            + 10.0 * state[7];

        let mut reward = 0.0;
        if let Some(prev) = self.prev_shaping {
            reward = shaping - prev;
        }

        reward -= main_force * 0.30;
        reward -= side_force * 0.03;

        (reward, shaping)
    }

    fn is_out_of_screen(&self) -> bool {
        if let Some(state) = &self.state {
            state[0].abs() >= 1.0
        } else {
            false
        }
    }

    fn has_collided<T: PartialEq + Copy>(&self, parents: (T, T), a: T, b: T) -> bool {
        parents == (a, b) || parents == (b, a)
    }

    fn has_crashed(&self, h1: ColliderHandle, h2: ColliderHandle) -> Option<bool> {
        let parents = (
            self.world.collider_set.get(h1)?.parent()?,
            self.world.collider_set.get(h2)?.parent()?,
        );
        Some(self.lander == parents.0 || self.lander == parents.1)
    }

    fn is_leg_collided(&self, h1: ColliderHandle, h2: ColliderHandle) -> Option<(bool, bool)> {
        let parents = (
            self.world.collider_set.get(h1)?.parent()?,
            self.world.collider_set.get(h2)?.parent()?,
        );

        let left_leg = self.has_collided(parents, self.moon, self.legs[0].body);
        let right_leg = self.has_collided(parents, self.moon, self.legs[1].body);
        Some((left_leg, right_leg))
    }

    fn handle_collisions(&mut self) {
        while let Ok(collision_event) = self.world.collision_recv.try_recv() {
            match collision_event {
                CollisionEvent::Started(h1, h2, _) => {
                    self.crash = self.has_crashed(h1, h2).is_some();
                    let on_ground = self.is_leg_collided(h1, h2).unwrap_or((false, false));
                    self.legs[0].ground_contact = on_ground.0;
                    self.legs[1].ground_contact = on_ground.1;
                }
                CollisionEvent::Stopped(h1, h2, _) => {
                    if let Some((left_leg, right_leg)) = self.is_leg_collided(h1, h2) {
                        self.legs[0].ground_contact &= !left_leg;
                        self.legs[1].ground_contact &= !right_leg;
                    }
                }
            }
        }
    }

    fn is_game_over(&self) -> bool {
        self.crash || self.is_out_of_screen()
    }

    fn is_landed(&self) -> bool {
        if let Some(lander_body) = self.world.rigid_body_set.get(self.lander) {
            let linear_velocity = lander_body.linvel();
            let angular_velocity = lander_body.angvel();
            let linear_threshold = 1e-3;
            let angular_threshold = 1e-3;

            linear_velocity.magnitude() < linear_threshold
                && angular_velocity.abs() < angular_threshold
                && self.legs.iter().all(|leg| leg.ground_contact)
        } else {
            false
        }
    }
}

impl Environment for LunarLander {
    type Action = Action;
    type State = State;
    type Info = ();

    fn reset(&mut self, seed: Option<u64>) -> Result<(Self::State, Self::Info), Error> {
        if let Some(s) = seed {
            self.rng = StdRng::seed_from_u64(s);
        }
        self.world = PhysicsWorld::new(self.config.gravity);
        self.t = 0;
        self.helipad = Helipad::default();
        self.prev_shaping = None;
        self.crash = false;

        if self.config.wind_strength.is_some() {
            self.wind_idx = 0.0;
            self.torque_idx = 0.0;
        }

        let (helipad, moon) = generate_moon(&self.config, &mut self.world, &mut self.rng);
        self.helipad = helipad;
        self.moon = moon;

        self.create_lander();
        self.create_legs();
        let state = self.get_state();
        self.state = Some(state);
        Ok((state, ()))
    }

    fn step(
        &mut self,
        action: Self::Action,
    ) -> Result<crate::env::environment::Experience<Self::State, Self::Info, Self::Action>, Error>
    {
        let curr_state = match self.state {
            Some(state) => state,
            None => return Err(environment::Error::NotInitialized),
        };

        self.apply_wind_effects();
        let (m_power, s_power) = self.apply_engine_forces(&action);

        self.world.step_with_dt(1.0 / self.config.fps as f32);
        self.handle_collisions();

        let state = self.get_state();
        let (reward, shaping) = self.calc_reward(&state, m_power, s_power);
        self.prev_shaping = Some(shaping);

        let terminated = self.is_game_over() || self.is_landed();
        let reward = match (self.is_game_over(), self.is_landed()) {
            (false, true) => 100.0,
            (true, false) => -100.0,
            _ => reward,
        };

        self.state = Some(state);
        self.t += 1;

        let terminal = Terminal::from_flags(terminated, self.is_truncated());
        Ok(Experience {
            curr_state,
            action,
            reward: reward as f64,
            next_state: state,
            terminal,
            info: (),
            step: self.t,
        })
    }

    fn is_terminal(&self) -> Result<bool, Error> {
        if self.state.is_some() {
            // Matches step()'s terminated computation: crash/out-of-screen or a
            // successful landing, not just out-of-screen.
            Ok(self.is_game_over() || self.is_landed())
        } else {
            Err(environment::Error::NotInitialized)
        }
    }

    fn is_truncated(&self) -> bool {
        self.t >= self.config.max_steps
    }

    fn state(&self) -> Result<Self::State, Error> {
        match self.state {
            Some(state) => Ok(state),
            None => Err(environment::Error::NotInitialized),
        }
    }
}

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

    // Helper to get a standard config
    fn get_test_config() -> LunarLanderConfig {
        LunarLanderConfig::default()
    }

    #[test]
    fn test_initialization_and_reset() {
        let config = get_test_config();
        let env = LunarLander::new(config).expect("Failed to create environment");

        // Check initial internal state
        assert_eq!(env.t, 0);
        assert!(env.state.is_some());
        assert!(!env.crash);
        assert!(env.prev_shaping.is_none());

        // Verify state dimension
        let state = env.state().expect("State should exist");
        assert_eq!(state.len(), STATE_SIZE);

        // Verify observation bounds (rough check based on initialization)
        // X should be around 0.0 (middle of screen centered)
        // Y should be around 1.0 (top of screen)
        assert!(state[0].abs() < 0.1);
        assert!(state[1] > 0.8);
    }

    #[test]
    fn test_action_space_modes() {
        // Discrete Mode
        let config_discrete = get_test_config().with_discrete_action();
        let env_discrete = LunarLander::new(config_discrete).unwrap();

        if let crate::spaces::Mixed::Discrete(disccrete) = env_discrete.space.action {
            assert_eq!(disccrete.size(), 4);
        } else {
            panic!("Expected Discrete action space");
        }

        // Continuous Mode
        let config_continuous = get_test_config().with_continuous_action();
        let env_continuous = LunarLander::new(config_continuous).unwrap();

        if let crate::spaces::Mixed::Continuous(_) = env_continuous.space.action {
            // Success
        } else {
            panic!("Expected Continuous action space");
        }
    }

    #[test]
    fn test_step_gravity() {
        let config = LunarLanderConfig::default().with_initial_random(f32::EPSILON);
        let mut env = LunarLander::new(config).unwrap();
        eprintln!("Config: {:?}", config);

        // Take several steps doing nothing to ensure gravity effect is clear
        let initial_state = env.state().unwrap();
        let initial_y = initial_state[1];

        let action = MixedItem::Discrete(0);

        // Step multiple times to accumulate noticeable gravity effect
        for _ in 0..10 {
            env.step(action).unwrap();
        }

        let final_state = env.state().unwrap();
        let final_y = final_state[1];

        // Y position should decrease (falling down)
        assert!(
            final_y < initial_y,
            "Lander should fall due to gravity. Initial Y: {}, Final Y: {}",
            initial_y,
            final_y
        );
        assert_eq!(env.t, 10);
    }

    #[test]
    fn test_step_main_engine_discrete() {
        let config = get_test_config();
        let mut env = LunarLander::new(config).unwrap();

        // Let it fall for a frame to establish downward momentum
        env.step(MixedItem::Discrete(0)).unwrap();
        let state_before = env.state().unwrap();
        let vy_before = state_before[3];

        // Action 2 is main engine in discrete
        let action = MixedItem::Discrete(2);
        env.step(action).unwrap();

        let state_after = env.state().unwrap();
        let vy_after = state_after[3];

        // The engine is strong (force 13.0 vs gravity -10.0), so velocity should increase (become less negative or positive)
        // relative to just falling.
        assert!(
            vy_after > vy_before,
            "Main engine should push lander upwards"
        );
    }

    #[test]
    fn test_step_main_engine_continuous() {
        let config = get_test_config().with_continuous_action();
        let mut env = LunarLander::new(config).unwrap();

        // Let it fall for several frames to establish downward momentum
        let no_action = MixedItem::Continuous(SVector::from_vec(vec![0.0, 0.0]));
        for _ in 0..5 {
            env.step(no_action).unwrap();
        }

        let state_before = env.state().unwrap();
        let vy_before = state_before[3];

        // Apply full main engine for multiple steps
        let full_thrust = MixedItem::Continuous(SVector::from_vec(vec![1.0, 0.0]));
        for _ in 0..5 {
            env.step(full_thrust).unwrap();
        }

        let state_after = env.state().unwrap();
        let vy_after = state_after[3];

        assert!(
            vy_after > vy_before,
            "Continuous main engine should push lander upwards. VY before: {}, VY after: {}",
            vy_before,
            vy_after
        );
    }

    #[test]
    fn test_truncation() {
        let max_steps = 10;
        let config = get_test_config().with_fps(50).with_viewport_size(600, 400);
        // Create a config that limits steps
        let mut limited_env = LunarLander::new(LunarLanderConfig {
            max_steps,
            ..config
        })
        .unwrap();

        for _ in 0..max_steps {
            assert!(!limited_env.is_truncated());
            limited_env.step(MixedItem::Discrete(0)).unwrap();
        }

        assert!(
            limited_env.is_truncated(),
            "Environment should be truncated after max_steps"
        );

        // Verify Terminal flag in experience
        let experience = limited_env.step(MixedItem::Discrete(0)).unwrap();
        assert!(experience.terminal.is_truncated());
    }

    #[test]
    fn test_out_of_bounds_detection() {
        let config = get_test_config();
        let mut env = LunarLander::new(config).unwrap();

        // Manually force a state that is out of bounds
        // State[0] is x position. |x| >= 1.0 is out of bounds.
        let mut bad_state = SVector::<f32, STATE_SIZE>::zeros();
        bad_state[0] = 1.5;
        env.state = Some(bad_state);

        assert!(env.is_out_of_screen());
        assert!(env.is_game_over());
        assert!(env.is_terminal().unwrap());
    }

    #[test]
    fn test_reward_shaping() {
        let config = get_test_config();
        let env = LunarLander::new(config).unwrap();

        let mut state = SVector::<f32, STATE_SIZE>::zeros();

        // 1. Good state: centered, close to target, no velocity, upright, on ground
        state[0] = 0.0; // X centered
        state[1] = 0.0; // Y at target (not high up!)
        state[2] = 0.0; // VX
        state[3] = 0.0; // VY
        state[4] = 0.0; // Angle upright
        state[5] = 0.0; // Angular velocity
        state[6] = 1.0; // Left leg on ground (bonus!)
        state[7] = 1.0; // Right leg on ground (bonus!)

        // Compare shaping potentials (2nd return value) because the step reward (1st value)
        // is 0 when prev_shaping is None.
        let (_, shaping_good) = env.calc_reward(&state, 0.0, 0.0);

        // 2. Bad state: off-center, high up, moving fast, tilted, not on ground
        state[0] = 0.8; // Far from center
        state[1] = 0.8; // High up from target
        state[2] = 2.0; // Moving horizontally
        state[3] = -2.0; // Falling fast
        state[4] = 0.5; // Tilted
        state[5] = 1.0; // Spinning
        state[6] = 0.0; // Left leg not touching
        state[7] = 0.0; // Right leg not touching

        let (reward_bad, shaping_bad) = env.calc_reward(&state, 0.0, 0.0);

        assert!(
            shaping_good > shaping_bad,
            "Better state should yield higher shaping reward. Good: {}, Bad: {}",
            shaping_good,
            shaping_bad
        );

        // 3. Engine penalty
        // reward_bad is 0.0 (no engine, prev_shaping is None).
        // Engine use should result in negative reward here.
        let (reward_with_main_engine, _) = env.calc_reward(&state, 1.0, 0.0);
        let (reward_with_side_engine, _) = env.calc_reward(&state, 0.0, 1.0);

        assert!(
            reward_with_main_engine < reward_bad,
            "Using main engine should penalize reward"
        );
        assert!(
            reward_with_side_engine < reward_bad,
            "Using side engine should penalize reward"
        );
    }

    #[test]
    fn test_wind_application() {
        // Ensure enabling wind doesn't crash and affects simulation implicitly
        let config = get_test_config().with_wind_strength(10.0);
        let mut env = LunarLander::new(config).unwrap();

        // We can't easily inspect the internal force applied without mocking Rapier,
        // but we can ensure the step runs without error and internal indices update.

        assert_eq!(env.wind_idx, 0.0);
        env.step(MixedItem::Discrete(0)).unwrap();
        assert_eq!(env.wind_idx, 1.0);
    }

    #[test]
    fn test_seeded_reset_is_reproducible() {
        // Both the initial random impulse (create_lander) and the per-step
        // engine dispersion (apply_engine_forces) draw from self.rng, so a
        // seeded reset should make the whole rollout deterministic.
        let config = get_test_config();
        let mut env1 = LunarLander::new(config).unwrap();
        let mut env2 = LunarLander::new(config).unwrap();

        let (state1, _) = env1.reset(Some(42)).unwrap();
        let (state2, _) = env2.reset(Some(42)).unwrap();
        assert_eq!(state1, state2);

        for _ in 0..10 {
            let exp1 = env1.step(MixedItem::Discrete(0)).unwrap();
            let exp2 = env2.step(MixedItem::Discrete(0)).unwrap();
            assert_eq!(exp1.next_state, exp2.next_state);
        }

        let (state3, _) = env1.reset(Some(7)).unwrap();
        assert_ne!(
            state3, state1,
            "a different seed should produce a different initial impulse"
        );
    }

    #[test]
    fn test_is_terminal_matches_step_terminated_on_crash() {
        let config = get_test_config();
        let mut env = LunarLander::new(config).unwrap();
        env.reset(Some(1)).unwrap();

        // Simulate a crash the way handle_collisions() would set it, without
        // needing to force an actual physics collision.
        env.crash = true;

        // is_terminal() must reflect the crash, not just out-of-screen.
        assert!(env.is_terminal().unwrap());
    }

    #[test]
    fn test_is_terminal_matches_step_terminated_on_landing() {
        let config = get_test_config();
        let mut env = LunarLander::new(config).unwrap();
        env.reset(Some(1)).unwrap();

        // Force a "landed" condition: both legs on the ground, lander at rest.
        if let Some(body) = env.world.rigid_body_set.get_mut(env.lander) {
            body.set_linvel(Vector2::new(0.0, 0.0), true);
            body.set_angvel(0.0, true);
        }
        env.legs[0].ground_contact = true;
        env.legs[1].ground_contact = true;

        assert!(env.is_landed());
        assert!(env.is_terminal().unwrap());
    }
}