
    hU                        S /r SSKrSSKJr  SSKrSSKrSSKJrJ	r	  SSK
Jr  SSKJr  SSKJr   SSKrSSKJrJrJrJrJrJr  \(       a  SSKrS
rSrSrSrSr/ SQr Sr!Sr"Su  r#r$Sr%Sr&Sr'Sr(Sr)Sr* " S S\5      r+ " S S\RX                  \5      r-S r.S'S jr/ " S S 5      r0\1S!:X  a  \Rd                  " S"S#S$9r3\/" \3S%S&9  gg! \ a  r\" S	5      \eSrCff = f)(u   Andrea PIERRÉ    N)TYPE_CHECKING)errorspaces)DependencyNotInstalled)EzPickle)step_api_compatibility)circleShapecontactListener	edgeShape
fixtureDefpolygonShaperevoluteJointDefzqBox2D is not installed, you can install it by run `pip install swig` followed by `pip install "gymnasium[box2d]"`2   g      >@g      *@g333333?g     @@))i   )r   )r   )r   r   )r   r   )   r         )      (   r         iX  i  c                   &    \ rS rSrS rS rS rSrg)ContactDetector;   c                 <    [         R                  " U 5        Xl        g )N)r
   __init__env)selfr    s     [/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/box2d/lunar_lander.pyr   ContactDetector.__init__<   s      &    c                    U R                   R                  UR                  R                  :X  d.  U R                   R                  UR                  R                  :X  a  SU R                   l        [        S5       Hj  nU R                   R                  U   UR                  R                  UR                  R                  4;   d  ML  SU R                   R                  U   l        Ml     g )NTr   )	r    landerfixtureAbodyfixtureB	game_overrangelegsground_contactr!   contactis      r"   BeginContactContactDetector.BeginContact@   s    HHOOw//444xx'"2"2"7"77!%DHHqAxx}}QG$4$4$9$97;K;K;P;P#QQ26a / r$   c                     [        S5       Hj  nU R                  R                  U   UR                  R                  UR
                  R                  4;   d  ML  SU R                  R                  U   l        Ml     g )Nr   F)r+   r    r,   r'   r(   r)   r-   r.   s      r"   
EndContactContactDetector.EndContactJ   sW    qAxx}}QG$4$4$9$97;K;K;P;P#QQ27a / r$   )r    N)__name__
__module____qualname____firstlineno__r   r1   r4   __static_attributes__ r$   r"   r   r   ;   s    78r$   r   c                      ^  \ rS rSrSrSS/\S.r      SS\S-  S\S	\	S
\S\	S\	4S jjr
S rSSS.S\S-  S\S-  4U 4S jjjrS rS rS rS rS rSrU =r$ )LunarLanderP   a  
## Description
This environment is a classic rocket trajectory optimization problem.
According to Pontryagin's maximum principle, it is optimal to fire the
engine at full throttle or turn it off. This is the reason why this
environment has discrete actions: engine on or off.

There are two environment versions: discrete or continuous.
The landing pad is always at coordinates (0,0). The coordinates are the
first two numbers in the state vector.
Landing outside of the landing pad is possible. Fuel is infinite, so an agent
can learn to fly and then land on its first attempt.

To see a heuristic landing, run:
```shell
python gymnasium/envs/box2d/lunar_lander.py
```

## Action Space
There are four discrete actions available:
- 0: do nothing
- 1: fire left orientation engine
- 2: fire main engine
- 3: fire right orientation engine

## Observation Space
The state is an 8-dimensional vector: the coordinates of the lander in `x` & `y`, its linear
velocities in `x` & `y`, its angle, its angular velocity, and two booleans
that represent whether each leg is in contact with the ground or not.

## Rewards
After every step a reward is granted. The total reward of an episode is the
sum of the rewards for all the steps within that episode.

For each step, the reward:
- is increased/decreased the closer/further the lander is to the landing pad.
- is increased/decreased the slower/faster the lander is moving.
- is decreased the more the lander is tilted (angle not horizontal).
- is increased by 10 points for each leg that is in contact with the ground.
- is decreased by 0.03 points each frame a side engine is firing.
- is decreased by 0.3 points each frame the main engine is firing.

The episode receive an additional reward of -100 or +100 points for crashing or landing safely respectively.

An episode is considered a solution if it scores at least 200 points.

## Starting State
The lander starts at the top center of the viewport with a random initial
force applied to its center of mass.

## Episode Termination
The episode finishes if:
1) the lander crashes (the lander body gets in contact with the moon);
2) the lander gets outside of the viewport (`x` coordinate is greater than 1);
3) the lander is not awake. From the [Box2D docs](https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_dynamics.html#autotoc_md61),
    a body which is not awake is a body which doesn't move and doesn't
    collide with any other body:
> When Box2D determines that a body (or group of bodies) has come to rest,
> the body enters a sleep state which has very little CPU overhead. If a
> body is awake and collides with a sleeping body, then the sleeping body
> wakes up. Bodies will also wake up if a joint or contact attached to
> them is destroyed.

## Arguments

Lunar Lander has a large number of arguments

```python
>>> import gymnasium as gym
>>> env = gym.make("LunarLander-v3", continuous=False, gravity=-10.0,
...                enable_wind=False, wind_power=15.0, turbulence_power=1.5)
>>> env
<TimeLimit<OrderEnforcing<PassiveEnvChecker<LunarLander<LunarLander-v3>>>>>

```

 * `continuous` determines if discrete or continuous actions (corresponding to the throttle of the engines) will be used with the
 action space being `Discrete(4)` or `Box(-1, +1, (2,), dtype=np.float32)` respectively.
 For continuous actions, the first coordinate of an action determines the throttle of the main engine, while the second
 coordinate specifies the throttle of the lateral boosters. Given an action `np.array([main, lateral])`, the main
 engine will be turned off completely if `main < 0` and the throttle scales affinely from 50% to 100% for
 `0 <= main <= 1` (in particular, the main engine doesn't work  with less than 50% power).
 Similarly, if `-0.5 < lateral < 0.5`, the lateral boosters will not fire at all. If `lateral < -0.5`, the left
 booster will fire, and if `lateral > 0.5`, the right booster will fire. Again, the throttle scales affinely
 from 50% to 100% between -1 and -0.5 (and 0.5 and 1, respectively).

* `gravity` dictates the gravitational constant, this is bounded to be within 0 and -12. Default is -10.0

* `enable_wind` determines if there will be wind effects applied to the lander. The wind is generated using
 the function `tanh(sin(2 k (t+C)) + sin(pi k (t+C)))` where `k` is set to 0.01 and `C` is sampled randomly between -9999 and 9999.

* `wind_power` dictates the maximum magnitude of linear wind applied to the craft. The recommended value for
 `wind_power` is between 0.0 and 20.0.

* `turbulence_power` dictates the maximum magnitude of rotational wind applied to the craft.
 The recommended value for `turbulence_power` is between 0.0 and 2.0.

## Version History
- v3:
    - Reset wind and turbulence offset (`C`) whenever the environment is reset to ensure statistical independence between consecutive episodes (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium/issues/954)).
    - Fix non-deterministic behaviour due to not fully destroying the world (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium/issues/728)).
    - Changed observation space for `x`, `y`  coordinates from $\pm 1.5$ to $\pm 2.5$, velocities from $\pm 5$ to $\pm 10$ and angles from $\pm \pi$ to $\pm 2\pi$ (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium/issues/752)).
- v2: Count energy spent and in v0.24, added turbulence with wind power and turbulence_power parameters
- v1: Legs contact with ground added in state vector; contact with ground give +10 reward points, and -10 if then lose contact; reward renormalized to 200; harder initial random push.
- v0: Initial version

## Notes

There are several unexpected bugs with the implementation of the environment.

1. The position of the side thrusters on the body of the lander changes, depending on the orientation of the lander.
This in turn results in an orientation dependent torque being applied to the lander.

2. The units of the state are not consistent. I.e.
* The angular velocity is in units of 0.4 radians per second. In order to convert to radians per second, the value needs to be multiplied by a factor of 2.5.

For the default values of VIEWPORT_W, VIEWPORT_H, SCALE, and FPS, the scale factors equal:
'x': 10, 'y': 6.666, 'vx': 5, 'vy': 7.5, 'angle': 1, 'angular velocity': 2.5

After the correction has been made, the units of the state are as follows:
'x': (units), 'y': (units), 'vx': (units/second), 'vy': (units/second), 'angle': (radians), 'angular velocity': (radians/second)

<!-- ## References -->

## Credits
Created by Oleg Klimov
human	rgb_array)render_modes
render_fpsNrender_mode
continuousgravityenable_wind
wind_powerturbulence_powerc           
         [         R                  " U UUUUUU5        SU:  a  US:  d   SU S35       eX0l        SU:  d  US:  a#  [        R                  R                  SU S35        XPl        SU:  d  US:  a#  [        R                  R                  S	U S35        X`l        X@l        S U l	        S U l
        S
U l        [        R                  " SU4S9U l        S U l        S U l        / U l        S U l        X l        [(        R*                  " SSSSS[,        R.                  -  SSS/5      R1                  [(        R2                  5      n[(        R*                  " SSSSS[,        R.                  -  SSS/5      R1                  [(        R2                  5      n[4        R6                  " Xx5      U l        U R&                  (       a+  [4        R6                  " SSS[(        R2                  S9U l        O[4        R<                  " S5      U l        Xl        g )Ng      (        zgravity (current value: z) must be between -12 and 0      4@zLwind_power value is recommended to be between 0.0 and 20.0, (current value: )g       @zQturbulence_power value is recommended to be between 0.0 and 2.0, (current value: Tr   rE   g            $g       g      @g      $@r         ?   )r   dtyper   ) r   r   rE   gymloggerwarnrG   rH   rF   screenclockisopenBox2Db2Worldworldmoonr&   	particlesprev_rewardrD   nparraymathpiastypefloat32r   Boxobservation_spaceaction_spaceDiscreterC   )	r!   rC   rD   rE   rF   rG   rH   lowhighs	            r"   r   LunarLander.__init__   s    		
 GO#	K%gY.IJ	K-zD0JJOO^_i^jjkl %!!%5%;JJOOcdtcuuvw !1&&*
]]Aw<8
	+/$hh
 TWW
 &
 	  xx
 DGG
 &
 	$ "(C!6?? !'

2r4rzz JD !' 2D&r$   c                    U R                   (       d  g S U R                  l        U R                  S5        U R                  R	                  U R                   5        S U l         U R                  R	                  U R
                  5        S U l        U R                  R	                  U R                  S   5        U R                  R	                  U R                  S   5        g )NTr   rR   )r^   r]   r
   _clean_particlesDestroyBodyr&   r,   r!   s    r"   _destroyLunarLander._destroy7  s    yy%)

"d#

tyy)	

t{{+

tyy|,

tyy|,r$   )seedoptionsrt   ru   c                
  > [         TU ]  US9  U R                  5         [        R                  " SU R
                  4S9U l        [        U 5      U R                  l        U R                  R                  U R                  l	        SU l
        S U l        [        [        -  n[        [        -  nSnU R                  R!                  SUS-  US-   4S9n[#        U5       Vs/ s H  osUS-
  -  U-  PM     nnXS-  S-
     U l        XS-  S-      U l        US	-  U l        U R(                  XeS-  S-
  '   U R(                  XeS-  S-
  '   U R(                  XeS-  S-   '   U R(                  XeS-  S-   '   U R(                  XeS-  S-   '   [#        U5       Vs/ s H  nS
XgS-
     XgS-      -   XgS-      -   -  PM     n	nU R                  R+                  [-        SUS4/S9S9U l        / U l        [#        US-
  5       H]  nX   X   4n
XS-      XS-      4nU R.                  R3                  X/SSS9  U R0                  R5                  XUS   U4U
S   U4/5        M_     SU R.                  l        SU R.                  l        [        [        -  n[        [        -  S-  nU R                  R;                  X4S[=        [?        [@         VVs/ s H  u  pU[        -  U[        -  4PM     snnS9SSSSSS9S9U l!        SU RB                  l        SU RB                  l        U RB                  RE                  U R                  R!                  [F        * [F        5      U R                  R!                  [F        * [F        5      4S5        U RH                  (       aB  U R                  RK                  SS5      U l&        U R                  RK                  SS5      U l'        / U l(        S GH  nU R                  R;                  X[R        -  [        -  -
  U4US-  [=        [?        [T        [        -  [V        [        -  4S9SSSSS 9S9nSUl,        SUl        SUl        [[        U RB                  USU[R        -  [        -  [\        [        -  4SS[^        S!U-  S"9nUS#:X  a  S$Ul0        S%Ul1        OS&Ul0        S'Ul1        U R                  Re                  U5      Ul3        U RP                  R5                  U5        GM     U RB                  /U RP                  -   U l4        U Rj                  S(:X  a  U Rm                  5         U Ro                  U Rp                  (       a  [r        Rt                  " SS/5      OS5      S   0 4$ s  snf s  snf s  snnf ))Nrt   r   rM   F   r   rR   )sizer   gQ?r   r   )vertices)shapes皙?)r{   densityfriction)rJ   rJ   rJ   rJ   g      @   shaper~   r   categoryBitsmaskBitsrestitutionpositionanglefixtures)   f      )M   r   r   Tii'  )rQ   rR   皙?)boxrP       )r   r~   r   r   r   333333?)bodyAbodyBlocalAnchorAlocalAnchorBenableMotorenableLimitmaxMotorTorque
motorSpeedrQ   皙?g?g皙ٿr?   );superresetrr   r[   r\   rE   r]   r   contactListener_keeprefr
   r*   prev_shaping
VIEWPORT_WSCALE
VIEWPORT_H	np_randomuniformr+   
helipad_x1
helipad_x2	helipad_yCreateStaticBodyr   r^   	sky_polysCreateEdgeFixtureappendcolor1color2CreateDynamicBodyr   r   LANDER_POLYr&   ApplyForceToCenterINITIAL_RANDOMrF   integerswind_idx
torque_idxr,   LEG_AWAYLEG_WLEG_Hr-   r   LEG_DOWNLEG_SPRING_TORQUE
lowerAngle
upperAngleCreateJointjointdrawlistrC   rendersteprD   ra   rb   )r!   rt   ru   WHCHUNKSheightr0   chunk_xsmooth_yp1p2	initial_y	initial_xxylegrjd	__class__s                     r"   r   LunarLander.resetC  s    	4 
 ]]At||+<=
-<T-B

*%)ZZ%G%G

"  ''1q5
}'E16v?A
#a'?!A+/2!A+/2Q"&..{Q"&..{Q"&..{Q"&..{Q"&..{Q 6]
" Fq5MFq5M1Fq5MAB" 	 

 JJ//v1v&67 0 
	 vz"A*hk*Ba%.(q5/2BII''"1s'SNN!!2BqE1:1qz"BC	 # +		*		 &	&*	jj22+"ALMq5y!e)4M #	 3 
 -* 	&&&&G&&G 	
  NN33E4@DM"nn55eTBDO 	A**..#(lU&::IF4x#&EEM55=+IJ #!'" / 
C "'C(CJ&CJ"kk#(lU2Hu4DE  0!8	C Bw  "&!%!+

..s3CIIIS!E H 		1w&KKMyyT__1a&)!DQGKKM @
4 Ns   U$U#:U(c                     U R                   R                  X#4S[        [        S[        -  SS9USSSSS	9S
9nXEl        U R                  R                  U5        U R                  S5        U$ )NrJ   r   rz   )radiusposr}      rR   r   r   r   F)	r]   r   r   r	   r   ttlr_   r   ro   )r!   massr   r   r   ps         r"   _create_particleLunarLander._create_particle  st    JJ((V!U?# ) 
 a e$r$   c                 N   U R                   (       a  U(       d  U R                   S   R                  S:  ao  U R                  R                  U R                   R	                  S5      5        U R                   (       a)  U(       a  MN  U R                   S   R                  S:  a  Mm  g g g g )Nr   )r_   r   r]   rp   pop)r!   all_particles     r"   ro   LunarLander._clean_particles  sl    nn,$..2C2G2G!2KJJ""4>>#5#5a#89 nn,,$..2C2G2G!2Kn2Knr$   c           	      L   U R                   c   eU R                   c   S5       eU R                  (       Ga  U R                  S   R                  (       Gdu  U R                  S   R                  (       GdV  [        R
                  " [        R                  " SU R                  -  5      [        R                  " [        R                  S-  U R                  -  5      -   5      U R                  -  nU =R                  S-  sl        U R                   R                  US4S5        [        R
                  " [        R                  " SU R                  -  5      [        R                  " [        R                  S-  U R                  -  5      -   5      U R                  -  nU =R                  S-  sl        U R                   R                  US5        U R                  (       a6  [        R                   " USS5      R#                  [        R$                  5      nO7U R&                  R)                  U5      (       d   U< S	[+        U5       S
35       e[        R                  " U R                   R,                  5      [        R.                  " U R                   R,                  5      4nUS   * US   4n[1        S5       Vs/ s H%  o`R2                  R5                  SS5      [6        -  PM'     nnSnU R                  (       a	  US   S:  d  U R                  (       GdX  US:X  GaQ  U R                  (       a0  [        R                   " US   SS5      S-   S-  nUS:  a  US::  d   eOSnUS   [8        [6        -  SUS   -  -   -  US   US   -  -   n	US   * [8        [6        -  SUS   -  -   -  US   US   -  -
  n
U R                   R:                  S   U	-   U R                   R:                  S   U
-   4nU R<                  bC  U R?                  SUS   US   U5      nURA                  U	[B        -  U-  U
[B        -  U-  4US5        U R                   RA                  U	* [B        -  U-  U
* [B        -  U-  4US5        SnU R                  (       a  [        RD                  " US   5      S:  d  U R                  (       Gd  US;   Ga  U R                  (       aW  [        RF                  " US   5      n[        R                   " [        RD                  " US   5      SS5      nUS:  a  US::  d   eOUS-
  nSnUS   US   -  US   SUS   -  U[H        -  [6        -  -   -  -   n	US   * US   -  US   SUS   -  U[H        -  [6        -  -   -  -
  n
U R                   R:                  S   U	-   US   S-  [6        -  -
  U R                   R:                  S   U
-   US   [J        -  [6        -  -   4nU R<                  bC  U R?                  SUS   US   U5      nURA                  U	[L        -  U-  U
[L        -  U-  4US5        U R                   RA                  U	* [L        -  U-  U
* [L        -  U-  4US5        U RN                  RQ                  S[R        -  SS5        U R                   R:                  nU R                   RT                  nURV                  [X        [6        -  S-  -
  [X        [6        -  S-  -  URZ                  U R\                  [^        [6        -  -   -
  [`        [6        -  S-  -  URV                  [X        [6        -  S-  -  [R        -  URZ                  [`        [6        -  S-  -  [R        -  U R                   R,                  SU R                   Rb                  -  [R        -  U R                  S   R                  (       a  SOSU R                  S   R                  (       a  SOS/n[e        U5      S:X  d   eSnS[        Rf                  " US   US   -  US   US   -  -   5      -  S[        Rf                  " US   US   -  US   US   -  -   5      -  -
  S[E        US   5      -  -
  SUS   -  -   SUS   -  -   nU Rh                  b  UU Rh                  -
  nUU l4        UUS-  -  nUUS-  -  nS nU Rj                  (       d  [E        US   5      S:  a  SnSnU R                   Rl                  (       d  SnSnU R<                  S!:X  a  U Ro                  5         [        Rp                  " U[        Rr                  S"9UUS 0 4$ s  snf )#NzYou forgot to call reset()r   rR   g{Gz?g{Gz?rJ   TrQ   z (z
) invalid r   g      rP         ?g      @)rR      r   r   gffffff?   <   rK   r   id   r   
         r   gQ?Fr?   rS   ):r&   rF   r,   r-   rc   tanhsinr   rd   rG   r   r   rH   ApplyTorquerD   ra   clipre   float64ri   containstyper   cosr+   r   r   r   MAIN_ENGINE_Y_LOCATIONr   rC   r   ApplyLinearImpulseMAIN_ENGINE_POWERabssignSIDE_ENGINE_AWAYSIDE_ENGINE_HEIGHTSIDE_ENGINE_POWERr]   StepFPSlinearVelocityr   r   r   r   r   r   angularVelocitylensqrtr   r*   awaker   rb   rf   )r!   actionwind_mag
torque_magtipside_
dispersionm_poweroxoyimpulse_posr   s_power	directionr   velstaterewardshaping
terminateds                        r"   r   LunarLander.step  s   {{&&& {{&D(DD&IIaL'''499Q<+F+F+F
 		HHTDMM12xx$ >?A //	"  MMQMKK**3 		HHTDOO34xx$ @AC ''	(  OOq OKK##
 ??WWVR,33BJJ?F$$--  72d6l^:67  xx))*DHHT[[5F5F,GH QQ  KPPQ(S(Qnn,,T485@(
SOOq	CFaK 776!9c37#=D#~'S.88.
 A0581z!};LLMq'JqM)* 
 Q1E9A
1<MMNq'JqM)* 
  ;;//2R79M9Ma9PSU9UVK+))NN	 $$..8..8   KK**((72RC:K4Kg4UV OOvay 1C 7Ff$4 GGF1I.	''"&&"3S#>#~'S.88. #QJ	 Q*Q-'$q'JqM!I0@$@5$HH+ B a&:a=(47JqM!I0@$@5$HH, B $$Q'",s1v{U/BB$$Q'",s1v8J/JU/RRK +))#{1~{1~wW$$..8..8   KK**((72RC:K4Kg4UV 	

c	662kk""kk(( UUZ%'!++
U0BQ0FGUUdnnx%'778Z%=ORS=STEEZ%'!+,s2EEZ%'!+,s2KK4;;...499Q<..CC99Q<..CC	
 5zQ27758eAh.qE!H1DDEEBGGE!HuQx/%(U1X2EEFFGCaM!" 58m 58m	 	 (t000F#dN	
 	'D. 
>>Sq]c1JF{{  JFw&KKMxxRZZ0&*eROOY Ts   4,d!c                 |   U R                   cG  U R                  c   e[        R                  R	                  SU R                  R
                   S35        g  SS KnSSKJn  U R                  ce  U R                   S:X  aU  UR                  " 5         UR                  R                  5         UR                  R                  [        [        45      U l
        U R                   c  UR"                  R%                  5       U l        UR&                  " [        [        45      U l        UR*                  R-                  U R(                  [.        [.        45        UR0                  R3                  U R(                  SU R(                  R5                  5       5        U R6                   H  nU=R8                  S-  sl        [;        [=        S	SUR8                  -   5      S
-  5      [;        [=        S	SUR8                  -  5      S
-  5      [;        [=        S	SUR8                  -  5      S
-  5      4Ul        [;        [=        S	SUR8                  -   5      S
-  5      [;        [=        S	SUR8                  -  5      S
-  5      [;        [=        S	SUR8                  -  5      S
-  5      4Ul         GM     U RC                  S5        U RD                   Hy  n/ nU H*  nURG                  US   [.        -  US   [.        -  45        M,     UR0                  RI                  U R(                  SU5        URK                  U R(                  US5        M{     U R6                  U RL                  -    GH  nURN                   GH  nURP                  R*                  n	[S        URT                  5      [V        L a  UR0                  RY                  U R(                  UR>                  XRT                  RZ                  -  [.        -  URT                  R\                  [.        -  S9  UR0                  RY                  U R(                  UR@                  XRT                  RZ                  -  [.        -  URT                  R\                  [.        -  S9  OURT                  R^                   V
s/ s H  oU
-  [.        -  PM     nn
UR0                  RI                  U R(                  UR>                  US9  URK                  U R(                  XR>                  5        UR0                  Ra                  U R(                  UR@                  USS9  U Rb                  U Rd                  4 H  nU[.        -  nU Rf                  [.        -  nUS-   nUR0                  Ri                  U R(                  SX4X4SS9  UR0                  RI                  U R(                  SX4XS-
  4US-   US-
  4/S9  URK                  U R(                  X4XS-
  4US-   US-
  4/S5        M     GM     GM     UR*                  Rk                  U R(                  SS5      U l        U R                   S:X  a  U R                  c   eU R                  Rm                  U R(                  S5        URn                  Rq                  5         U R                   Rs                  U Rt                  S   5        UR                  Rk                  5         g U R                   S:X  aL  [v        Rx                  " [v        Rz                  " UR|                  R                  U R(                  5      5      SS9$ g ! [         a  n[        S5      UeS nAff = fs  sn
f )NzYou are calling render method without specifying any render mode. You can specify the render_mode at initialization, e.g. gym.make("z", render_mode="rgb_array")r   )gfxdrawz=pygame is not installed, run `pip install "gymnasium[box2d]"`r?   )   r  r  g333333?g?r  r   FrR   )r   r   r   )colorcenterr   )r  pointsT)r  r  closedr   )r  	start_posend_poswidth)   r  r   r         rz   rB   r@   )rR   r   r   )axes)@rC   specrU   rV   rW   idpygamer  ImportErrorr   rX   initdisplayset_moder   r   rY   timeClockSurfacesurf	transformscaler   drawrectget_rectr_   r   intmaxr   r   ro   r   r   polygon	aapolygonr   r   r(   r   r   r	   circler   r   r{   aalinesr   r   r   lineflipbliteventpumptickmetadatara   	transposerb   	surfarraypixels3d)r!   r!  r  eobjr   scaled_polycoordftransvpathr   flagy1flagy2s                  r"   r   LunarLander.render  s   #99(((JJOO""&)),,/JL
 	& ;;4#3#3w#>KKMNN! ..11:z2JKDK::**,DJNNJ
#;<	tyy5%.9OTYY5G5G5IJ>>CGGtOGCTCGG^,s23CS377]+c12CS377]+c12CJ CTCGG^,s23CS377]+c12CS377]+c12CJ " 	e$AK""E!Hu$4eAh6F#GH KK		9kBdiii@   >>DMM1C\\((=K/KK&&		!jj$ww{{2U: ww~~5	 '  KK&&		!jj$ww{{2U: ww~~5	 '  89ww7G7GH7G!AI-7GDHKK''		D'Q%%diizzBKK''		D (  //4??;AE	A!^^e3F#b[FKK$$		-#$+!" %  KK''		+K,VVaZ0  (  %%		q2+&6R!8LM%) <1 " 2f $$))$))UDA	w&;;***KKTYY/LLJJOODMM,78NN!,<<))22499=>Y  -E  	(O	r Is   
\ ?\9
\6%\11\6c                     U R                   b7  SS KnUR                  R                  5         UR                  " 5         SU l        g g )Nr   F)rX   r!  r$  quitrZ   )r!   r!  s     r"   closeLunarLander.close  s4    ;;"NN!KKMDK #r$   )ri   rY   rD   r   rF   r*   rE   r   r   r   rZ   r&   r,   r^   rh   r_   r`   r   rC   rX   r   r)  r   rH   r   rG   r]   )NFrN   Fg      .@g      ?)r6   r7   r8   r9   __doc__r   r;  strboolfloatr   rr   r/  dictr   r   ro   r   r   rL  r:   __classcell__)r   s   @r"   r=   r=   P   s    ~B !+.H #' ! "%_'4Z_' _' 	_'
 _' _'  _'B
-  #	~L Dj~L 	~L ~L@$:BPHrh   r$   r=   c                 (   US   S-  US   S-  -   nUS:  a  SnUS:  a  SnS[         R                  " US   5      -  nX!S   -
  S-  US	   S-  -
  nX1S
   -
  S-  US   S-  -
  nUS   (       d
  US   (       a  SnUS   * S-  nU R                  R                  (       a<  [         R                  " US-  S
-
  U* S-  /5      n[         R
                  " USS
5      nU$ SnU[         R                  " U5      :  a
  US:  a  SnU$ US:  a  SnU$ US:  a  S
nU$ )a*  
The heuristic for
1. Testing
2. Demonstration rollout.

Args:
    env: The environment
    s (list): The state. Attributes:
        s[0] is the horizontal coordinate
        s[1] is the vertical coordinate
        s[2] is the horizontal speed
        s[3] is the vertical speed
        s[4] is the angle
        s[5] is the angular speed
        s[6] 1 if first leg has contact, else 0
        s[7] 1 if second leg has contact, else 0

Returns:
     a: The heuristic to be fed into the step function defined above to determine the next step and reward.
r   r   r   rP   r   r   g?r   r  rR   r   r   r   r   rQ   r   g)ra   r   	unwrappedrD   rb   r   )r    s
angle_targ
hover_targ
angle_todo
hover_todoas          r"   	heuristicr\    sV   , 1adSj(JC
D
	! J t#s*adc\9Jt#s*adc\9Jtqt
dGcM 	 }}HHj2o)J;+;<=GGAr2 H z**zD/@A
 H	 %A H %AHr$   c           
         SnSnU R                  US9u  pV [        X5      n[        U R                  U5      S5      u  pXpnX8-  nU(       a  U R	                  5       nUSL a  OlUS-  S:X  d  U	(       d  U
(       a?  [        SSR                  U Vs/ s H  oS PM     sn5      5        [        S	U S
US 35        US-  nU	(       d  U
(       a  OM  U(       a  U R                  5         U$ s  snf )Nr   rw   TFr   zobservations: z+0.2fzstep z total_reward rR   )r   r\  r   r   r   printjoinrL  )r    rt   r   total_rewardstepsrV  infor[  rr  	truncated
still_openr   s                r"   demo_heuristic_landerrg  P  s    LEiiTi"GA
c,B388A;PT,U)jTJU"2:?jI/3881,E1a%y\1,E#FGE%|E.BCD
   		 -Fs   C'c                       \ rS rSrS rSrg)LunarLanderContinuousii  c                 .    [         R                  " S5      e)Na%  Error initializing LunarLanderContinuous Environment.
Currently, we do not support initializing this mode of environment by calling the class directly.
To use this environment, instead create it by specifying the continuous keyword in gym.make, i.e.
gym.make("LunarLander-v3", continuous=True))r   Errorrq   s    r"   r   LunarLanderContinuous.__init__j  s    kk:
 	
r$   r;   N)r6   r7   r8   r9   r   r:   r;   r$   r"   ri  ri  i  s    
r$   ri  __main__zLunarLander-v3r@   )rC   T)r   )NF)4__credits__rc   typingr   numpyra   	gymnasiumrU   r   r   gymnasium.errorr   gymnasium.utilsr   &gymnasium.utils.step_api_compatibilityr   r[   Box2D.b2r	   r
   r   r   r   r   r"  r?  r!  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Envr=   r\  rg  ri  r6   maker    r;   r$   r"   <module>rx     s          # 2 $ I   	  Ru     

8o 8*G #''8 G T3l2
 
 z
((#
=C#d+ u  
 {s   C C	CC