36 lines
1.6 KiB
Java
36 lines
1.6 KiB
Java
package online.astradev.content.rail;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.level.block.WeatheringCopper;
|
|
import net.minecraft.world.level.gamerules.GameRules;
|
|
|
|
/** Match copper's random-tick selection, age neighborhood and oxidation probability. */
|
|
final class RailOxidation {
|
|
private RailOxidation() {}
|
|
|
|
static boolean advance(ServerLevel level, BlockPos pos, RailKind kind) {
|
|
if (kind.waxed() || kind.age() == 3 || !level.isPositionEntityTicking(pos)) return false;
|
|
int rate = level.getGameRules().get(GameRules.RANDOM_TICK_SPEED);
|
|
double chance = 1 - Math.pow(1 - .05688889 / 4096, rate);
|
|
if (level.getRandom().nextDouble() >= chance) return false;
|
|
int older = 0, same = 0;
|
|
for (var other : BlockPos.withinManhattan(pos, 4, 4, 4)) {
|
|
int distance = other.distManhattan(pos);
|
|
if (distance > 4) break;
|
|
if (distance == 0 || !level.hasChunkAt(other)) continue;
|
|
int age = -1;
|
|
var rail = Rails.find(level, other);
|
|
if (rail != null && !rail.waxed()) age = rail.age();
|
|
else if (level.getBlockState(other).getBlock() instanceof WeatheringCopper copper)
|
|
age = copper.getAge().ordinal();
|
|
if (age < 0) continue;
|
|
if (age < kind.age()) return false;
|
|
if (age > kind.age()) older++;
|
|
else same++;
|
|
}
|
|
double ratio = (older + 1.0) / (older + same + 1.0);
|
|
return level.getRandom().nextDouble() < ratio * ratio * (kind.age() == 0 ? .75 : 1);
|
|
}
|
|
}
|