Lightning scrapes oxidation off copper

A struck copper block goes back to unoxidized, and 3 to 5 random walks
take one step of oxidation off the weathering copper around it. Waxed
blocks stay as they are but still start the walks.
This commit is contained in:
Kacpherek
2026-10-26 15:15:30 +01:00
parent 8027cb4176
commit 85a6ce0921
2 changed files with 94 additions and 0 deletions
@@ -221,6 +221,7 @@ impl Game<'_> {
if !b.visual_only {
self.bolt_fire(b.at, 4);
self.strike_rod(b.at);
self.bolt_clear_copper(b.at);
}
}
b.life -= 1;
@@ -278,6 +279,16 @@ impl Game<'_> {
self.level_event_at(3002, p, axis);
}
}
/// `LightningBolt.clearCopperOnLightningStrike` at the block the bolt struck.
fn bolt_clear_copper(&mut self, at: [f64; 3]) {
let p = BlockPos::new(
at[0].floor() as i32,
(at[1] - 1.0e-6).floor() as i32,
at[2].floor() as i32,
);
self.with_level(|b, level, ticks| b.lightning_clear_copper(level, ticks, p));
let _ = self.process_level_events();
}
/// `thunderHit` for entities within 3 blocks (up to 9 above).
fn thunder_hit(&mut self, b: &mut Bolt) {
let at = b.at;
+83
View File
@@ -1479,6 +1479,80 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
let block = b.table.block_by_name(&format!("minecraft:{next}"))?;
Some(b.table.with_properties_of(block, state))
}
/// `WeatheringCopper.getPrevious` (and `getFirst` by repeating it) by
/// registry naming: the block one oxidation step back.
fn copper_previous_name(path: &str) -> Option<String> {
if let Some(rest) = path.strip_prefix("oxidized_") {
Some(format!("weathered_{rest}"))
} else if let Some(rest) = path.strip_prefix("weathered_") {
Some(format!("exposed_{rest}"))
} else {
path.strip_prefix("exposed_").map(|rest| {
if rest == "copper" {
"copper_block".to_owned()
} else {
rest.to_owned()
}
})
}
}
fn copper_stepped(&self, state: BlockStateId, all_the_way: bool) -> Option<BlockStateId> {
let b = self.b;
let name = &*b.table.info(b.block(state)).name;
let mut path = Self::copper_previous_name(name.strip_prefix("minecraft:")?)?;
while all_the_way && let Some(earlier) = Self::copper_previous_name(&path) {
path = earlier;
}
let block = b.table.block_by_name(&format!("minecraft:{path}"))?;
Some(b.table.with_properties_of(block, state))
}
/// `LightningBolt.clearCopperOnLightningStrike`: the struck copper goes
/// back to unoxidized, then 3 to 5 random walks of up to 8 steps each
/// scrape one step of oxidation off the weathering copper they land on.
/// A waxed block is left as it is but still starts the walks.
pub(crate) fn lightning_clear_copper(&mut self, struck: BlockPos) {
let b = self.b;
let state = self.state(struck);
let weathering = b
.data(state)
.implements(super::classes::Interface::WeatheringCopper);
let waxed = b.table.name(state).starts_with("minecraft:waxed_");
if !(weathering || waxed) {
return;
}
if weathering && let Some(first) = self.copper_stepped(state, true) {
self.set_block_and_update(struck, first);
}
for _ in 0..self.rand_int(3) + 3 {
let mut at = struck;
for _ in 0..self.rand_int(8) + 1 {
// randomStepCleaningCopper: up to 10 random blocks in the
// 3x3x3 cube around the walk's position.
let mut next = None;
for _ in 0..10 {
let x = at.x - 1 + self.rand_int(3);
let y = at.y - 1 + self.rand_int(3);
let z = at.z - 1 + self.rand_int(3);
let candidate = BlockPos::new(x, y, z);
let state = self.state(candidate);
if b.data(state)
.implements(super::classes::Interface::WeatheringCopper)
{
if let Some(previous) = self.copper_stepped(state, false) {
self.set_block_and_update(candidate, previous);
}
self.level_event(3002, candidate, -1);
next = Some(candidate);
break;
}
}
match next {
Some(p) => at = p,
None => break,
}
}
}
}
}
/// `Biome.Precipitation` at a position.
@@ -1662,6 +1736,15 @@ impl Behaviours {
) {
self.sim(level, ticks).random_tick_at(pos);
}
/// `LightningBolt.clearCopperOnLightningStrike` for the block at `struck`.
pub fn lightning_clear_copper<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
struck: BlockPos,
) {
self.sim(level, ticks).lightning_clear_copper(struck);
}
/// Run one scheduled block tick at `pos` immediately (no queue check).
pub fn scheduled_block_tick<L: LevelAccess + ?Sized>(
&self,