fix(game): use the original Borland random stream

Replace the Xorshift state with the reconstructed Borland Win16 generator,
including its wrapping linear update and high-word Random(n) mapping. Route
launcher clamp variation, effect selection, and claw terminal choices through
one shared stream so seeded event order follows the executable.

Use the same stream for the recovered magnetic-field response. Fields now scale
horizontal speed by 0.9 and choose the upward impulse from
-3800 * (1 - Random * 0.3) instead of assigning a fixed -3000 velocity. Adjust
deterministic scenario expectations where the original stream permits a second
natural claw capture or does not force one during a particular autoplay seed.

Test Plan:
- `cargo test --all-targets` -- passed, 56 tests
- `cargo clippy --all-targets -- -D warnings` -- passed
- `rumdl check CHANGELOG.md RECONSTRUCTION.md` -- passed
- `git diff --cached --check` -- passed
This commit is contained in:
2026-08-23 16:52:17 +02:00
parent 9de3dcc755
commit c25287186a
6 changed files with 92 additions and 29 deletions
+69
View File
@@ -0,0 +1,69 @@
//! Borland Win16 random-number stream used by the original executable.
const MULTIPLIER: u32 = 0x0808_8405;
const TWO_TO_32: f64 = 4_294_967_296.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct BorlandRandom {
seed: u32,
}
impl BorlandRandom {
pub const fn new(seed: u32) -> Self {
Self { seed }
}
pub fn next_u32(&mut self) -> u32 {
self.seed = self.seed.wrapping_mul(MULTIPLIER).wrapping_add(1);
self.seed
}
#[allow(clippy::cast_possible_truncation)]
pub fn below(&mut self, upper_bound: u16) -> u16 {
let product = u64::from(self.next_u32()) * u64::from(upper_bound);
(product >> 32) as u16
}
/// Exact host representation of the x87 `Random` result in `[0, 1)`.
pub fn unit_interval(&mut self) -> f64 {
f64::from(self.next_u32()) / TWO_TO_32
}
#[cfg(test)]
pub const fn seed(self) -> u32 {
self.seed
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stream_matches_the_reconstructed_borland_runtime() {
let mut random = BorlandRandom::new(0x1234_5678);
let first = 0x1234_5678_u32.wrapping_mul(MULTIPLIER).wrapping_add(1);
assert_eq!(random.next_u32(), first);
assert_eq!(random.below(3_800), 1_810);
}
#[test]
fn zero_bounds_still_advance_the_seed() {
let mut random = BorlandRandom::new(7);
assert_eq!(random.below(0), 0);
assert_eq!(random.seed(), 7_u32.wrapping_mul(MULTIPLIER).wrapping_add(1));
}
#[test]
fn unit_interval_is_the_exact_unsigned_seed_fraction() {
let mut random = BorlandRandom::new(0xfedc_ba98);
let expected_seed = 0xfedc_ba98_u32
.wrapping_mul(MULTIPLIER)
.wrapping_add(1);
assert_eq!(
random.unit_interval().to_bits(),
(f64::from(expected_seed) / TWO_TO_32).to_bits()
);
}
}