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
use bindings::ffi;
use bindings::AsNative;

#[repr(C)]
#[derive(Copy, Clone)]
pub enum Distribution {
    Linear = ffi::TCOD_DISTRIBUTION_LINEAR as isize,
    Gaussian = ffi::TCOD_DISTRIBUTION_GAUSSIAN as isize,
    GaussianRange = ffi::TCOD_DISTRIBUTION_GAUSSIAN_RANGE as isize,
    GaussianInverse = ffi::TCOD_DISTRIBUTION_GAUSSIAN_INVERSE as isize,
    GaussianRangeInverse = ffi::TCOD_DISTRIBUTION_GAUSSIAN_RANGE_INVERSE as isize,

}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum Algo {
    MT = ffi::TCOD_RNG_MT as isize,
    CMWC = ffi::TCOD_RNG_CMWC as isize
}

pub struct Rng {
    tcod_random: ffi::TCOD_random_t,
    default: bool
}

impl Rng {
    pub fn get_instance() -> Rng {
        unsafe {
            Rng { tcod_random: ffi::TCOD_random_get_instance(), default: true }
        }
    }

    pub fn new(algo: Algo) -> Rng {
        unsafe {
            Rng { tcod_random: ffi::TCOD_random_new(algo as u32), default: false }
        }
    }

    pub fn new_with_seed(algo: Algo, seed: u32) -> Rng {
        unsafe {
            Rng { tcod_random: ffi::TCOD_random_new_from_seed(algo as u32, seed), default: false }
        }
    }

    pub fn save(&self) -> Rng {
        unsafe {
            Rng { tcod_random: ffi::TCOD_random_save(self.tcod_random), default: false }
        }
    }

    pub fn restore(&mut self, backup: &Rng) {
        unsafe {
            ffi::TCOD_random_restore(self.tcod_random, backup.tcod_random);
        }
    }

    pub fn set_distribution(&self, distribution: Distribution) {
        unsafe {
            ffi::TCOD_random_set_distribution(self.tcod_random, distribution as u32);
        }
    }

    pub fn get_int(&self, min: i32, max: i32) -> i32 {
        unsafe {
            ffi::TCOD_random_get_int(self.tcod_random, min, max)
        }
    }

    pub fn get_int_mean(&self, min: i32, max: i32, mean: i32) -> i32 {
        unsafe {
            ffi::TCOD_random_get_int_mean(self.tcod_random,  min, max, mean)
        }
    }

    pub fn get_float(&self, min: f32, max: f32) -> f32 {
        unsafe {
            ffi::TCOD_random_get_float(self.tcod_random, min, max)
        }
    }

    pub fn get_float_mean(&self, min: f32, max: f32, mean: f32) -> f32 {
        unsafe {
            ffi::TCOD_random_get_float_mean(self.tcod_random, min, max, mean)
        }
    }

    pub fn get_double(&self, min: f64, max: f64) -> f64 {
        unsafe {
            ffi::TCOD_random_get_double(self.tcod_random, min, max)
        }
    }

    pub fn get_double_mean(&self, min: f64, max: f64, mean: f64) -> f64 {
        unsafe {
            ffi::TCOD_random_get_double_mean(self.tcod_random, min, max, mean)
        }
    }
}

impl AsNative<ffi::TCOD_random_t> for Rng {
    unsafe fn as_native(&self) -> &ffi::TCOD_random_t {
        &self.tcod_random
    }
}

impl Drop for Rng {
    fn drop(&mut self) {
        if !self.default {
            unsafe {
                ffi::TCOD_random_delete(self.tcod_random);
            }
        }
    }
}