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
use c_str_macro::c_str;
use std::ffi::CString;
use std::path::Path;
use std::path::PathBuf;

#[derive(Copy, Clone, Debug, num_derive::FromPrimitive)]
#[repr(i32)]
pub enum AleAction {
    Noop = 0,
    Fire = 1,
    Up = 2,
    Right = 3,
    Left = 4,
    Down = 5,
    UpRight = 6,
    UpLeft = 7,
    DownRight = 8,
    DownLeft = 9,
    UpFire = 10,
    RightFire = 11,
    LeftFire = 12,
    DownFire = 13,
    UpRightFire = 14,
    UpLeftFire = 15,
    DownRightFire = 16,
    DownLeftFire = 17,
    // admin actions
    // Reset = 40,
    // SaveState = 43,
    // LoadState = 44,
    // SystemReset = 45,
}

pub struct AleConfig {
    pub random_seed: i32, // if 0, set to time
    pub display_screen: bool,
    pub sound: bool,
    pub color_averaging: bool, // average the last 2 frames
    ///
    pub frame_skip: i32, // 1 is no skip
    pub repeat_action_probability: f32,
    pub record_screen_dir: Option<PathBuf>,
    ///
    pub difficulty_setting: i32,
}

impl Default for AleConfig {
    fn default() -> Self {
        Self {
            random_seed: 0,
            display_screen: false,
            sound: false,
            color_averaging: false, // true is recommended
            frame_skip: 1,
            repeat_action_probability: 0.25,
            record_screen_dir: None,
            difficulty_setting: 0,
        }
    }
}

pub struct Ale {
    #[cfg(not(doc))]
    inner: *mut atari_env_sys::ALEInterface,
}

// TODO: it seems to work, but needs to be verified
unsafe impl Send for Ale {}

impl Drop for Ale {
    fn drop(&mut self) {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::ALE_del(self.inner);
        }

        #[cfg(doc)]
        unimplemented!();
    }
}

impl Ale {
    pub fn new(rom_path: &Path, config: AleConfig) -> Self {
        #[cfg(not(doc))]
        {
            let ale = unsafe { atari_env_sys::ALE_new() };
            unsafe {
                atari_env_sys::setInt(ale, c_str!("random_seed").as_ptr(), config.random_seed);
                atari_env_sys::setBool(
                    ale,
                    c_str!("display_screen").as_ptr(),
                    config.display_screen,
                );
                atari_env_sys::setBool(ale, c_str!("sound").as_ptr(), config.sound);
                atari_env_sys::setBool(
                    ale,
                    c_str!("color_averaging").as_ptr(),
                    config.color_averaging,
                );
                atari_env_sys::setInt(ale, c_str!("frame_skip").as_ptr(), config.frame_skip);
                atari_env_sys::setFloat(
                    ale,
                    c_str!("repeat_action_probability").as_ptr(),
                    config.repeat_action_probability,
                );

                if let Some(path) = config.record_screen_dir {
                    let path = CString::new(path.to_str().unwrap()).unwrap();
                    atari_env_sys::setString(
                        ale,
                        c_str!("record_screen_dir").as_ptr(),
                        path.as_ptr(),
                    );
                }
                let rom_path = CString::new(rom_path.to_str().unwrap()).unwrap();
                atari_env_sys::loadROM(ale, rom_path.as_ptr());
            }
            unsafe {
                atari_env_sys::setDifficulty(ale, config.difficulty_setting);
                atari_env_sys::reset_game(ale);
            }

            Self { inner: ale }
        }

        #[cfg(doc)]
        unimplemented!();
    }
    pub fn available_actions(&self) -> Vec<AleAction> {
        #[cfg(not(doc))]
        {
            let n = unsafe { atari_env_sys::getLegalActionSize(self.inner) } as usize;
            let mut buf = vec![AleAction::Noop; n];
            unsafe {
                atari_env_sys::getLegalActionSet(self.inner, buf.as_mut_ptr() as *mut i32);
            }
            buf
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn minimal_actions(&self) -> Vec<AleAction> {
        #[cfg(not(doc))]
        {
            let n = unsafe { atari_env_sys::getMinimalActionSize(self.inner) } as usize;
            let mut buf = vec![AleAction::Noop; n];
            unsafe {
                atari_env_sys::getMinimalActionSet(self.inner, buf.as_mut_ptr() as *mut i32);
            }
            buf
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn is_game_over(&self) -> bool {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::game_over(self.inner)
        }

        #[cfg(doc)]
        unimplemented!();
    }

    /// frame number since rom loading (Ale::new)
    pub fn rom_frame_number(&self) -> i32 {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getFrameNumber(self.inner)
        }

        #[cfg(doc)]
        unimplemented!();
    }

    /// frame number of the current episode
    pub fn episode_frame_number(&self) -> i32 {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getEpisodeFrameNumber(self.inner)
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn reset(&mut self) {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::reset_game(self.inner);
        }

        #[cfg(doc)]
        unimplemented!();
    }

    /// returns reward
    pub fn take_action(&mut self, action: AleAction) -> i32 {
        #[cfg(not(doc))]
        {
            let ret: ::std::os::raw::c_int =
                unsafe { atari_env_sys::act(self.inner, action as i32) };
            ret.into()
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn lives(&self) -> u32 {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::lives(self.inner) as u32
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn available_difficulty_settings(&self) -> Vec<i32> {
        #[cfg(not(doc))]
        {
            let n = unsafe { atari_env_sys::getAvailableDifficultiesSize(self.inner) } as usize;
            let mut buf = vec![0i32; n];
            unsafe {
                atari_env_sys::getAvailableDifficulties(self.inner, buf.as_mut_ptr() as *mut i32);
            }
            buf
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn width(&self) -> u32 {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getScreenWidth(self.inner) as u32
        }

        #[cfg(doc)]
        unimplemented!();
    }
    pub fn height(&self) -> u32 {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getScreenHeight(self.inner) as u32
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn rgb24_size(&self) -> usize {
        #[cfg(not(doc))]
        return (self.width() as usize) * (self.height() as usize) * 3;

        #[cfg(doc)]
        unimplemented!();
    }
    /// bgr on little-endian, rgb on big-endian
    pub fn rgb24_native_endian(&self, buf: &mut [u8]) {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getScreenRGB(self.inner, buf.as_mut_ptr());
        }

        #[cfg(doc)]
        unimplemented!();
    }
    /// always rgb in regardless of endianness
    pub fn rgb24(&self, buf: &mut [u8]) {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getScreenRGB2(self.inner, buf.as_mut_ptr());
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn rgb32_size(&self) -> usize {
        #[cfg(not(doc))]
        return (self.width() as usize) * (self.height() as usize) * 4;

        #[cfg(doc)]
        unimplemented!();
    }
    /// always rgb in regardless of endianness
    pub fn rgb32(&self, buf: &mut [u8]) {
        #[cfg(not(doc))]
        {
            let n = buf.len() / 4;
            self.rgb24(&mut buf[n..]);
            for i in 0..n {
                buf[i * 4 + 0] = buf[n + (i * 3) + 0];
                buf[i * 4 + 1] = buf[n + (i * 3) + 1];
                buf[i * 4 + 2] = buf[n + (i * 3) + 2];
                buf[i * 4 + 3] = 0;
            }
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn ram_size(&self) -> usize {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getRAMSize(self.inner) as usize
        }

        #[cfg(doc)]
        unimplemented!();
    }
    pub fn ram(&self, buf: &mut [u8]) {
        #[cfg(not(doc))]
        unsafe {
            atari_env_sys::getRAM(self.inner, buf.as_mut_ptr());
        }

        #[cfg(doc)]
        unimplemented!();
    }

    pub fn save_png<P: AsRef<Path>>(&self, path: P) {
        #[cfg(not(doc))]
        {
            use std::os::unix::ffi::OsStrExt;
            let path = path.as_ref();
            let path = CString::new(path.as_os_str().as_bytes()).unwrap();
            unsafe {
                atari_env_sys::saveScreenPNG(self.inner, path.as_ptr());
            }
        }

        #[cfg(doc)]
        unimplemented!();
    }
}