smrng 0.2.1

A simple command line tool for printing information about RNG loops and drop chances in Super Metroid
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
pub mod analysis;

use std::{
    collections::HashMap,
    ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index, Sub, SubAssign},
    sync::LazyLock,
};

use serde::Deserialize;

use crate::Rng;

const ENEMY_DROPS_JSON: &str = include_str!("enemy_drops.json");

/// The drop table for enemies in vanilla SM.
pub static ENEMY_DROPS: LazyLock<HashMap<String, DropTable>> =
    LazyLock::new(|| serde_json::from_str(ENEMY_DROPS_JSON).unwrap());

/// A drop type.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Drop {
    Nothing,
    SmallEnergy,
    BigEnergy,
    Missile,
    SuperMissile,
    PowerBomb,
}

impl Drop {
    /// Whether this is considered a "Tier 2" drop.
    pub const fn is_major(&self) -> bool {
        use self::Drop::*;
        match self {
            Nothing | SmallEnergy | BigEnergy | Missile => false,
            SuperMissile | PowerBomb => true,
        }
    }

    const fn index(&self) -> u8 {
        use self::Drop::*;
        match self {
            SmallEnergy => 0,
            BigEnergy => 1,
            Missile => 2,
            Nothing => 3,
            SuperMissile => 4,
            PowerBomb => 5,
        }
    }

    const fn from_index(index: u8) -> Self {
        use self::Drop::*;
        match index {
            0 => SmallEnergy,
            1 => BigEnergy,
            2 => Missile,
            3 => Nothing,
            4 => SuperMissile,
            5 => PowerBomb,
            _ => panic!("invalid drop index"),
        }
    }
}

/// A set of drops.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct DropSet(u8);

impl DropSet {
    pub const EMPTY: DropSet = DropSet::new();
    pub const ALL: DropSet = DropSet::from_slice(&[
        Drop::Nothing,
        Drop::SmallEnergy,
        Drop::BigEnergy,
        Drop::Missile,
        Drop::SuperMissile,
        Drop::PowerBomb,
    ]);
    pub const MINOR: DropSet = DropSet::from_slice(&[
        Drop::Nothing,
        Drop::SmallEnergy,
        Drop::BigEnergy,
        Drop::Missile,
    ]);
    pub const MAJOR: DropSet = DropSet::from_slice(&[Drop::SuperMissile, Drop::PowerBomb]);
    pub const HEALTH_BOMB: DropSet = DropSet::from_slice(&[Drop::SmallEnergy, Drop::BigEnergy]);

    pub const fn new() -> DropSet {
        DropSet(0)
    }

    pub const fn is_empty(&self) -> bool {
        self.0 == 0
    }

    pub const fn len(&self) -> usize {
        self.0.count_ones() as usize
    }

    pub const fn contains(&self, drop: &Drop) -> bool {
        self.0 & (1 << drop.index()) != 0
    }

    pub const fn insert(&mut self, drop: Drop) -> bool {
        let inserted = !self.contains(&drop);
        self.0 |= 1 << drop.index();
        inserted
    }

    pub const fn remove(&mut self, drop: Drop) -> bool {
        let removed = self.contains(&drop);
        self.0 &= !(1 << drop.index());
        removed
    }

    pub const fn intersection(&self, other: &DropSet) -> DropSet {
        DropSet(self.0 & other.0)
    }

    pub const fn union(&self, other: &DropSet) -> DropSet {
        DropSet(self.0 | other.0)
    }

    pub const fn difference(&self, other: &DropSet) -> DropSet {
        DropSet(self.0 & !other.0)
    }

    pub const fn symmetric_difference(&self, other: &DropSet) -> DropSet {
        DropSet(self.0 ^ other.0)
    }

    pub fn iter(&self) -> impl Iterator<Item = Drop> + '_ {
        DropSetIterator(self.clone())
    }

    const fn from_slice(drops: &[Drop]) -> DropSet {
        let mut result = DropSet::new();
        let mut i = 0;
        while i < drops.len() {
            result.insert(drops[i]);
            i += 1;
        }
        result
    }
}
impl Default for DropSet {
    fn default() -> Self {
        DropSet::new()
    }
}

impl BitAnd<&DropSet> for DropSet {
    type Output = DropSet;
    fn bitand(self, rhs: &DropSet) -> Self::Output {
        self.intersection(rhs)
    }
}
impl BitAndAssign<&DropSet> for DropSet {
    fn bitand_assign(&mut self, rhs: &DropSet) {
        *self = std::mem::take(self) & rhs;
    }
}
impl BitOr<&DropSet> for DropSet {
    type Output = DropSet;
    fn bitor(self, rhs: &DropSet) -> Self::Output {
        self.union(rhs)
    }
}
impl BitOrAssign<&DropSet> for DropSet {
    fn bitor_assign(&mut self, rhs: &DropSet) {
        *self = std::mem::take(self) | rhs;
    }
}
impl BitXor<&DropSet> for DropSet {
    type Output = DropSet;
    fn bitxor(self, rhs: &DropSet) -> Self::Output {
        self.symmetric_difference(rhs)
    }
}
impl BitXorAssign<&DropSet> for DropSet {
    fn bitxor_assign(&mut self, rhs: &DropSet) {
        *self = std::mem::take(self) ^ rhs;
    }
}
impl Sub<&DropSet> for DropSet {
    type Output = DropSet;
    fn sub(self, rhs: &DropSet) -> Self::Output {
        self.difference(rhs)
    }
}
impl SubAssign<&DropSet> for DropSet {
    fn sub_assign(&mut self, rhs: &DropSet) {
        *self = std::mem::take(self) - rhs;
    }
}

impl IntoIterator for DropSet {
    type Item = Drop;
    type IntoIter = DropSetIterator;
    fn into_iter(self) -> Self::IntoIter {
        DropSetIterator(self)
    }
}

impl Extend<Drop> for DropSet {
    fn extend<T: IntoIterator<Item = Drop>>(&mut self, iter: T) {
        for drop in iter {
            self.insert(drop);
        }
    }
}

impl FromIterator<Drop> for DropSet {
    fn from_iter<T: IntoIterator<Item = Drop>>(iter: T) -> Self {
        let mut result = DropSet::new();
        result.extend(iter);
        result
    }
}

pub struct DropSetIterator(DropSet);
impl Iterator for DropSetIterator {
    type Item = Drop;
    fn next(&mut self) -> Option<Self::Item> {
        if self.0.is_empty() {
            None
        } else {
            let drop = Drop::from_index(self.0 .0.trailing_zeros() as u8);
            self.0.remove(drop);
            Some(drop)
        }
    }
}

/// A table of an enemy's drop chances.
#[derive(Deserialize)]
pub struct DropTable {
    pub nothing: u8,
    pub small_energy: u8,
    pub big_energy: u8,
    pub missile: u8,
    pub super_missile: u8,
    pub power_bomb: u8,

    /// If this enemy calls a multi-drop routine, the number of drops to generate.
    #[serde(default)]
    pub count: Option<u32>,

    /// The type of explosion animation, if this enemy's explosion generates an extra drop.
    #[serde(default)]
    pub extra: Option<ExplosionDrop>,
}

#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExplosionDrop {
    Metroid,
    Minikraid,
}

impl ExplosionDrop {
    /// The number of frames between explosions.
    pub fn explosion_interval(&self) -> u32 {
        8
    }

    /// The number of explosions before generating the final drop.
    pub fn num_explosions(&self) -> u32 {
        match self {
            ExplosionDrop::Metroid => 5,
            ExplosionDrop::Minikraid => 16,
        }
    }

    /// The number of RNG calls per explosion.
    pub fn rng_per_explosion(&self) -> u32 {
        match self {
            ExplosionDrop::Metroid => 2,
            ExplosionDrop::Minikraid => 3,
        }
    }
}

impl Index<self::Drop> for DropTable {
    type Output = u8;

    fn index(&self, index: self::Drop) -> &Self::Output {
        match index {
            Drop::Nothing => &self.nothing,
            Drop::SmallEnergy => &self.small_energy,
            Drop::BigEnergy => &self.big_energy,
            Drop::Missile => &self.missile,
            Drop::SuperMissile => &self.super_missile,
            Drop::PowerBomb => &self.power_bomb,
        }
    }
}

impl DropTable {
    /// Returns the raw drop chance for a given drop.
    pub fn get(&self, drop: Drop) -> u8 {
        self[drop]
    }

    /// Calculates ideal drops based purely on probabilities in the drop table.
    ///
    /// Returns the expected number of times `drop` will be dropped after farming this enemy
    /// `farms` times.
    pub fn ideal_drops_per_farm(&self, drop: Drop, possible_drops: &DropSet, farms: u32) -> f32 {
        if !possible_drops.contains(&drop) {
            return 0.;
        }

        let pooled_minor = possible_drops
            .intersection(&DropSet::MINOR)
            .iter()
            .map(|d| self[d])
            .sum::<u8>() as u16;

        let pooled_major_complement = 0xFF
            - possible_drops
                .intersection(&DropSet::MAJOR)
                .iter()
                .map(|d| self[d])
                .sum::<u8>() as u16;

        let chance = if !drop.is_major() {
            self[drop] as u16 * pooled_major_complement / pooled_minor
        } else {
            self[drop] as u16
        };

        chance as f32 / 255.
            * (self.count.unwrap_or(1) + self.extra.as_ref().map(|_| 1).unwrap_or(0)) as f32
            * farms as f32
    }

    /// Simulates a single drop (even if this enemy drops multiple items).
    pub fn roll_one(&self, rng: &mut Rng, possible_drops: &DropSet) -> Drop {
        let random = loop {
            match rng.roll() as u8 {
                0 => continue,
                n => break n as u16,
            }
        };

        let pooled_minor = possible_drops
            .intersection(&DropSet::MINOR)
            .iter()
            .map(|d| self[d])
            .sum::<u8>() as u16;

        let pooled_major_complement = 0xFF
            - possible_drops
                .intersection(&DropSet::MAJOR)
                .iter()
                .map(|d| self[d])
                .sum::<u8>() as u16;

        let mut acc = 0;
        for drop in possible_drops.intersection(&DropSet::MINOR) {
            if pooled_minor != 0 {
                acc += (self[drop] as u16) * pooled_major_complement / pooled_minor;
            }
            if acc >= random {
                return drop;
            }
        }

        for drop in possible_drops.intersection(&DropSet::MAJOR) {
            acc += self[drop] as u16;
            if acc >= random {
                return drop;
            }
        }

        Drop::Nothing
    }

    /// Simulates this enemy's drops.
    pub fn roll<'a>(
        &'a self,
        rng: &'a mut Rng,
        possible_drops: &'a DropSet,
    ) -> impl Iterator<Item = Drop> + 'a {
        self.roll_multiple(rng, possible_drops, 1)
    }

    /// Simulates the drops obtained by farming multiple of this enemy in a single frame.
    pub fn roll_multiple<'a>(
        &'a self,
        rng: &'a mut Rng,
        possible_drops: &'a DropSet,
        n: u32,
    ) -> impl Iterator<Item = Drop> + 'a {
        let mut main_drops = 0;
        let mut extra_drops = 0;
        std::iter::from_fn(move || {
            let count = self.count.unwrap_or(1) * n;

            if main_drops < count {
                main_drops += 1;
                rng.roll();
                Some(self.roll_one(rng, possible_drops))
            } else if let Some(extra) = &self.extra {
                if extra_drops < n {
                    if extra_drops == 0 {
                        for _ in 0..extra.num_explosions() {
                            for _ in 0..extra.rng_per_explosion() * n {
                                rng.roll();
                            }

                            for _ in 0..extra.explosion_interval() {
                                rng.frame_advance();
                            }
                        }
                    }

                    extra_drops += 1;
                    Some(self.roll_one(rng, possible_drops))
                } else {
                    None
                }
            } else {
                None
            }
        })
    }
}