Route world generation's and mob AI's Math calls through jmath

Beardifier, density pow/log, Marsaglia's Gaussian, dripstone, ores, fancy
trunks, icebergs, end spikes, strongholds, note pitches, sculk charge
particles, the warden's and brains' weighted shuffles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-13 03:06:48 +02:00
parent 558d140fca
commit ab9109c787
17 changed files with 40 additions and 33 deletions
@@ -742,7 +742,7 @@ impl Gate {
// current order, then a stable sort.
for k in &mut self.kids {
let r = self.random.next_float();
k.2 = -f64::from(r).powf(f64::from(1.0_f32 / k.1 as f32));
k.2 = -horizon_core::jmath::pow(f64::from(r), f64::from(1.0_f32 / k.1 as f32));
}
self.kids.sort_by(|a, b| a.2.total_cmp(&b.2));
for k in &mut self.kids {
@@ -550,7 +550,7 @@ impl RunOne {
// order, then a stable sort.
for k in &mut self.kids {
let r = self.random.next_float();
k.2 = -f64::from(r).powf(f64::from(1.0_f32 / k.1 as f32));
k.2 = -horizon_core::jmath::pow(f64::from(r), f64::from(1.0_f32 / k.1 as f32));
}
self.kids.sort_by(|a, b| a.2.total_cmp(&b.2));
for k in &mut self.kids {
@@ -1101,7 +1101,7 @@ impl Gate {
// current order, then a stable sort.
for k in &mut self.kids {
let r = self.random.next_float();
k.2 = -f64::from(r).powf(f64::from(1.0_f32 / k.1 as f32));
k.2 = -horizon_core::jmath::pow(f64::from(r), f64::from(1.0_f32 / k.1 as f32));
}
self.kids.sort_by(|a, b| a.2.total_cmp(&b.2));
}
@@ -2392,7 +2392,7 @@ impl B {
} => {
for k in kids.iter_mut() {
let r = random.next_float();
k.2 = -f64::from(r).powf(f64::from(1.0_f32 / k.1 as f32));
k.2 = -horizon_core::jmath::pow(f64::from(r), f64::from(1.0_f32 / k.1 as f32));
}
kids.sort_by(|a, b| a.2.total_cmp(&b.2));
for k in kids.iter_mut() {
+2 -2
View File
@@ -1061,8 +1061,8 @@ fn attack(m: &mut Mob, target: Who, cx: &mut Cx) {
fn run_one(m: &mut Mob, st: &mut Warden, cx: &mut Cx) {
let now = i64::from(m.age);
// ShufflingList.shuffle: each entry sorts by -ln(random) / weight.
let stroll_key = -f64::from(m.random.next_float()).ln() / 2.0;
let pause_key = -f64::from(m.random.next_float()).ln();
let stroll_key = -horizon_core::jmath::log(f64::from(m.random.next_float())) / 2.0;
let pause_key = -horizon_core::jmath::log(f64::from(m.random.next_float()));
let pause_first = pause_key < stroll_key;
let order = if pause_first {
[false, true]
@@ -8,7 +8,7 @@ pub(super) fn java_power(base: f64, exponent: f64) -> f64 {
} else if exponent.is_nan() || (base.abs() == 1.0 && exponent.is_infinite()) {
f64::NAN
} else {
base.powf(exponent)
crate::jmath::pow(base, exponent)
}
}
@@ -83,7 +83,7 @@ impl UnaryOp {
Self::Square => value * value,
Self::Cube => value * value * value,
Self::Sqrt => (value as f64).sqrt() as f32,
Self::Log => (value as f64).ln() as f32,
Self::Log => crate::jmath::log(value as f64) as f32,
Self::PowerBase(base) => java_power(base, value as f64) as f32,
Self::PowerExponent(exponent) => java_power(value as f64, exponent) as f32,
Self::Reciprocal => 1.0 / value,
@@ -208,7 +208,7 @@ fn kernel() -> &'static [f32; 13824] {
let x = ((i / 24) % 24) as f64 - 12.;
let y = (i % 24) as f64 - 12. + 0.5;
let squared = (x * x + y * y) + z * z;
std::f64::consts::E.powf(-squared / 16.) as f32
crate::jmath::pow(std::f64::consts::E, -squared / 16.) as f32
})
})
}
+1 -1
View File
@@ -189,7 +189,7 @@ impl Interval {
Ok(Self::NAI)
} else {
self.maximum(Self::exact(0.0)?)
.map_monotonic(|v| (v as f64).ln() as f32)
.map_monotonic(|v| crate::jmath::log(v as f64) as f32)
}
}
pub fn power(self, exponent: Self) -> Self {
+1 -1
View File
@@ -300,7 +300,7 @@ pub fn marsaglia_polar(cache: &mut Option<f64>, mut next_double: impl FnMut() ->
if radius >= 1.0 || radius == 0.0 {
continue;
}
let multiplier = (-2.0 * radius.ln() / radius).sqrt();
let multiplier = (-2.0 * crate::jmath::log(radius) / radius).sqrt();
*cache = Some(y * multiplier);
return x * multiplier;
}
+1 -1
View File
@@ -1092,7 +1092,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
let pitch = if mob_head {
1.0
} else {
2f64.powf(f64::from(note - 12) / 12.0) as f32
horizon_core::jmath::pow(2.0, f64::from(note - 12) / 12.0) as f32
};
let sound = match instrument.as_str() {
"custom_head" => "minecraft:ui.button.click".to_owned(),
+2 -1
View File
@@ -315,7 +315,8 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
if charge > 0
&& let Some(faces) = faces
{
let particles = ((f64::from(charge) + 1.0).ln() as f32 / 2.3) as i32 + 1;
let particles =
(horizon_core::jmath::log(f64::from(charge) + 1.0) as f32 / 2.3) as i32 + 1;
let data = (particles << 6) + pack_faces(&faces);
self.level_event(3006, pos, data);
}
@@ -545,9 +545,9 @@ fn speleothem_height(distance: f64, radius: f64, scale: f64, bluntness: f64) ->
distance
};
let r = distance / radius * 0.384;
let part1 = 0.75 * r.powf(1.3333333333333333);
let part2 = r.powf(0.6666666666666666);
let part3 = 0.3333333333333333 * r.ln();
let part1 = 0.75 * horizon_core::jmath::pow(r, 1.3333333333333333);
let part2 = horizon_core::jmath::pow(r, 0.6666666666666666);
let part3 = 0.3333333333333333 * horizon_core::jmath::log(r);
let height = (scale * (part1 - part2 - part3)).max(0.0);
height / 0.384 * radius
}
@@ -38,8 +38,8 @@ pub fn spikes_for_seed(seed: i64) -> Vec<EndSpike> {
2.0 * (-std::f64::consts::PI + (std::f64::consts::PI / 10.0) * f64::from(i));
let size = sizes[i as usize];
EndSpike {
center_x: (42.0 * angle.cos()).floor() as i32,
center_z: (42.0 * angle.sin()).floor() as i32,
center_x: (42.0 * horizon_core::jmath::cos(angle)).floor() as i32,
center_z: (42.0 * horizon_core::jmath::sin(angle)).floor() as i32,
radius: 2 + size / 3,
height: 76 + size * 3,
guarded: size == 1 || size == 2,
+7 -3
View File
@@ -29,7 +29,7 @@ fn ceil(v: f32) -> i32 {
f64::from(v).ceil() as i32
}
fn pow2(v: i32) -> f64 {
f64::from(v).powf(2.0)
horizon_core::jmath::pow(f64::from(v), 2.0)
}
impl IcebergFeature {
pub(crate) fn decode(
@@ -380,7 +380,11 @@ impl IcebergFeature {
fn signed_ellipse(xo: i32, zo: i32, origin: [i32; 3], a: i32, c: i32, angle: f64) -> f64 {
let dx = f64::from(xo - origin[0]);
let dz = f64::from(zo - origin[2]);
((dx * angle.cos() - dz * angle.sin()) / f64::from(a)).powf(2.0)
+ ((dx * angle.sin() + dz * angle.cos()) / f64::from(c)).powf(2.0)
let (sin, cos) = (
horizon_core::jmath::sin(angle),
horizon_core::jmath::cos(angle),
);
horizon_core::jmath::pow((dx * cos - dz * sin) / f64::from(a), 2.0)
+ horizon_core::jmath::pow((dx * sin + dz * cos) / f64::from(c), 2.0)
- 1.0
}
@@ -155,7 +155,10 @@ impl OreFeature {
let spread_xy = self.size as f32 / 8.0;
let max_radius = ((self.size as f32 / 16.0 * 2.0 + 1.0) / 2.0).ceil() as i32;
let (ox, oz) = (f64::from(origin[0]), f64::from(origin[2]));
let (sin, cos) = (f64::from(dir).sin(), f64::from(dir).cos());
let (sin, cos) = (
horizon_core::jmath::sin(f64::from(dir)),
horizon_core::jmath::cos(f64::from(dir)),
);
let spread = f64::from(spread_xy);
let x0 = ox + sin * spread;
let x1 = ox - sin * spread;
@@ -30,7 +30,10 @@ pub fn ring_positions(
let dist = f64::from(4 * distance + distance * circle * 6)
+ (random.next_double() - 0.5) * (f64::from(distance) * 2.5);
let round = |v: f64| (v + 0.5).floor() as i64 as i32;
let (x, z) = (round(angle.cos() * dist), round(angle.sin() * dist));
let (x, z) = (
round(horizon_core::jmath::cos(angle) * dist),
round(horizon_core::jmath::sin(angle) * dist),
);
let mut search = random.fork();
let (cx, cz) = ((x * 16 + 8) >> 2, (z * 16 + 8) >> 2);
let mut found = 0;
@@ -690,8 +690,9 @@ impl TrunkPlacer {
let height = tree_height + 2;
let trunk_height = (f64::from(height) * 0.618).floor() as i32;
Self::place_below_trunk(run, tree, tb::offset(origin, 0, -1, 0))?;
let clusters_per_y =
1.min((1.382 + (1.0 * f64::from(height) / 13.0).powf(2.0)).floor() as i32);
let clusters_per_y = 1.min(
(1.382 + horizon_core::jmath::pow(1.0 * f64::from(height) / 13.0, 2.0)).floor() as i32,
);
let trunk_top = origin[1].wrapping_add(trunk_height);
let mut relative_y = height - 5;
// (attachment position, branch base y)
@@ -704,8 +705,8 @@ impl TrunkPlacer {
let radius =
1.0 * f64::from(shape) * (f64::from(run.random.next_float()) + 0.328);
let angle = f64::from(run.random.next_float() * 2.0f32) * std::f64::consts::PI;
let x = radius * angle.sin() + 0.5;
let z = radius * angle.cos() + 0.5;
let x = radius * horizon_core::jmath::sin(angle) + 0.5;
let z = radius * horizon_core::jmath::cos(angle) + 0.5;
let check_start =
tb::offset(origin, x.floor() as i32, relative_y - 1, z.floor() as i32);
let check_end = tb::offset(check_start, 0, 5, 0);
+1 -6
View File
@@ -9,12 +9,7 @@
"python": "/opt/homebrew/bin/python3.13",
"rsync": "/opt/homebrew/bin/rsync",
"jobs": 14,
"exclude": {
"options": "Rust's f64::powf uses the platform libm, which rounds differently on macOS",
"chunk_generation": "one ocean ruined portal differs on macOS (platform float math)",
"world_border_render": "the wall's alpha differs by one ulp on macOS (platform float math)",
"mob_ai": "mob movement differs on macOS (platform float math in random positions)"
}
"exclude": {}
},
"thinkpad": {
"ssh": "thinkpad",