diff --git a/crates/horizon-client/src/brewing.rs b/crates/horizon-client/src/brewing.rs index 8c809be11..13bdb826b 100644 --- a/crates/horizon-client/src/brewing.rs +++ b/crates/horizon-client/src/brewing.rs @@ -152,7 +152,10 @@ pub fn potion_color(id: Option<&str>) -> u32 { b += w * (e.color & 255); total += w; } - if total == 0 { BASE_POTION_COLOR } else { (r / total) << 16 | (g / total) << 8 | b / total } + match (r.checked_div(total), g.checked_div(total), b.checked_div(total)) { + (Some(r), Some(g), Some(b)) => (r << 16) | (g << 8) | b, + _ => BASE_POTION_COLOR, + } } /// `MobEffectUtil.formatDuration` at 20 TPS: m:ss or h:mm:ss. pub fn format_duration(ticks: i32) -> String { diff --git a/crates/horizon-client/src/enchant.rs b/crates/horizon-client/src/enchant.rs index 3fbe56fd6..e10a5ab4d 100644 --- a/crates/horizon-client/src/enchant.rs +++ b/crates/horizon-client/src/enchant.rs @@ -523,7 +523,9 @@ pub fn apply_value_effect(effect: &Value, level: u32, random: &mut LegacyRandom, "minecraft:remove_binomial" => { let p = level_value(&effect["chance"], level); let n = value; - let removed = if !(n <= 128.0) && !(n * p < 20.0) && !(n * (1.0 - p) < 20.0) { + // Normal approximation for large counts (written as vanilla's negated tests). + let large = n > 128.0 && n * p >= 20.0 && n * (1.0 - p) >= 20.0; + let removed = if large { let miu = (f64::from(n) * f64::from(p)).floor(); let sigma = (f64::from(n) * f64::from(p) * f64::from(1.0 - p)).sqrt(); ((miu + random.next_gaussian() * sigma).round() as i32).clamp(0, n as i32) diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index cd7297be5..c19c89870 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -103,7 +103,7 @@ impl Game<'_> { let remaining = 32u32.saturating_sub(ticks); let eye = self.eye_position().unwrap_or_default(); // Consumable.emitParticlesAndSounds: the drink sound every 4 ticks near the end. - if remaining <= 25 && remaining % 4 == 0 { + if remaining <= 25 && remaining.is_multiple_of(4) { let pitch = self.world.random.next_float() * 0.1 + 0.9; self.audio.play("minecraft:entity.generic.drink", Some(eye), 0.5, pitch); } @@ -269,7 +269,7 @@ impl Game<'_> { self.apply_effect_action(a); } } else { - let d = (intensity * f64::from(*duration) * f64::from(scale) + 0.5) as i32; + let d = (intensity * f64::from(*duration) * scale + 0.5) as i32; if d > 20 && let Some(e) = brewing::effect(id) { diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index d9c1b507f..e85e4d83c 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -890,15 +890,14 @@ impl Game<'_> { let count = names.next_int_bound(2).unwrap_or(0) + 3; let words: Vec<&str> = (0..count).map(|_| WORDS[names.next_int_bound(WORDS.len() as i32).unwrap_or(0) as usize]).collect(); let message = words.join(" "); - let col: i32; let disabled = (gold < i as i32 + 1 || level < cost) && !self.creative; - if disabled { + let col: i32 = if disabled { batch.sprite_scaled(atlas, "gui/sprites/container/enchanting_table/enchantment_slot_disabled", ox + bx, oy + by, bw, bh, u32::MAX); batch.sprite(atlas, &format!("gui/sprites/container/enchanting_table/level_{}_disabled", i + 1), ox + bx + 1.0, oy + by + 1.0); let c = ((-9937334i32) & 16711422) >> 1; let msg_color = argb(c | 0xFF00_0000u32 as i32); self.galactic_deferred(batch, &message, ox + bx + 20.0, oy + by + 2.0, text_width, msg_color); - col = -12550384; + -12550384 } else { let hover = inside(local, bx, by, bw, bh); let sprite = if hover { "enchantment_slot_highlighted" } else { "enchantment_slot" }; @@ -906,8 +905,8 @@ impl Game<'_> { batch.sprite(atlas, &format!("gui/sprites/container/enchanting_table/level_{}", i + 1), ox + bx + 1.0, oy + by + 1.0); let msg_color = if hover { -128 } else { -9937334 }; self.galactic_deferred(batch, &message, ox + bx + 20.0, oy + by + 2.0, text_width, argb(msg_color)); - col = -8323296; - } + -8323296 + }; let atlas = &self.res.atlas; let cw = HudBatch::text_width(atlas, &cost_text); batch.text(atlas, &cost_text, ox + bx + 20.0 + 86.0 - cw, oy + by + 9.0, argb(col), true); @@ -942,7 +941,7 @@ impl Game<'_> { batch.sprite(atlas, "gui/sprites/container/anvil/error", ox + 99.0, oy + 45.0); } if s.inputs[0].is_some() { - let blink = if (self.ticks / 6) % 2 == 0 { "_" } else { "" }; + let blink = if (self.ticks / 6).is_multiple_of(2) { "_" } else { "" }; batch.text(atlas, &format!("{}{blink}", s.name), ox + 62.0, oy + 24.0, u32::MAX, true); } let cost = s.anvil.cost; diff --git a/crates/horizon-client/src/merchant.rs b/crates/horizon-client/src/merchant.rs index 8c52a1c42..d8fa86a07 100644 --- a/crates/horizon-client/src/merchant.rs +++ b/crates/horizon-client/src/merchant.rs @@ -286,10 +286,10 @@ pub fn offer_from(ctx: &TradeContext<'_>, t: &Value, random: &m result = stack; additional += extra; } - if let Some(Value::String(tag)) = t.get("double_trade_price_enchantments") { - if result.stored_enchantments().iter().any(|(e, _)| ctx.enchantments.in_tag(tag, e)) { - additional *= 2; - } + if let Some(Value::String(tag)) = t.get("double_trade_price_enchantments") + && result.stored_enchantments().iter().any(|(e, _)| ctx.enchantments.in_tag(tag, e)) + { + additional *= 2; } let a = cost(ctx.items, &t["wants"], additional); if a.stack.count < 1 { diff --git a/crates/horizon-client/src/stations.rs b/crates/horizon-client/src/stations.rs index f0cca1156..6a659f605 100644 --- a/crates/horizon-client/src/stations.rs +++ b/crates/horizon-client/src/stations.rs @@ -507,7 +507,7 @@ mod tests { let a = enchant_offers(&e, &items, 1234, 15, Some(&s)); let b = enchant_offers(&e, &items, 1234, 15, Some(&s)); assert_eq!(a, b); - assert!(a.costs[2] >= 30 && a.costs[2] <= 30, "{a:?}"); + assert_eq!(a.costs[2], 30, "{a:?}"); assert!(a.costs[0] >= 1 && a.costs[0] <= a.costs[2]); // The clue is the first offer's random pick from the same list. for slot in 0..3 {