2026-09-13 09:16:42 +03:00
|
|
|
package online.astradev.content.rail;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.core.BlockPos;
|
2026-09-13 21:55:44 +03:00
|
|
|
import net.minecraft.core.Vec3i;
|
2026-09-13 09:16:42 +03:00
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
|
import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
|
|
|
|
import net.minecraft.world.level.block.*;
|
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
|
import net.minecraft.world.level.block.state.properties.RailShape;
|
|
|
|
|
|
2026-09-13 21:55:44 +03:00
|
|
|
import java.util.*;
|
|
|
|
|
|
2026-09-13 09:16:42 +03:00
|
|
|
public final class RailSpeed {
|
|
|
|
|
private RailSpeed() {}
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
public static int signalSpeed(int signal) {
|
|
|
|
|
return Math.clamp(signal, 0, 15) * 2;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 09:16:42 +03:00
|
|
|
public static double target(ServerLevel level, BlockPos pos, double previous) {
|
2026-09-13 21:55:44 +03:00
|
|
|
var state = level.getBlockState(pos);
|
|
|
|
|
if (!state.is(Blocks.POWERED_RAIL) || !state.getValue(PoweredRailBlock.POWERED))
|
|
|
|
|
return previous;
|
|
|
|
|
var kind = Rails.find(level, pos);
|
|
|
|
|
return kind != null ? kind.speed() : Math.max(2, signalSpeed(signal(level, pos)));
|
2026-09-13 09:16:42 +03:00
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
|
2026-09-13 09:16:42 +03:00
|
|
|
/** Mirror the vanilla eight-rail power reach, preserving bends, slopes and highest input. */
|
|
|
|
|
public static int signal(ServerLevel level, BlockPos origin) {
|
2026-09-13 21:55:44 +03:00
|
|
|
record Step(BlockPos pos, int distance) {}
|
|
|
|
|
var queue = new ArrayDeque<Step>();
|
|
|
|
|
var seen = new HashSet<BlockPos>();
|
|
|
|
|
queue.add(new Step(origin, 0));
|
|
|
|
|
int best = 0;
|
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
|
var step = queue.removeFirst();
|
|
|
|
|
if (!seen.add(step.pos) || !level.hasChunkAt(step.pos)) continue;
|
|
|
|
|
var state = level.getBlockState(step.pos);
|
2026-09-13 09:16:42 +03:00
|
|
|
if (!state.is(Blocks.POWERED_RAIL)) continue;
|
2026-09-13 21:55:44 +03:00
|
|
|
best = Math.max(best, level.getBestNeighborSignal(step.pos));
|
|
|
|
|
if (best == 15) return best;
|
|
|
|
|
if (step.distance == 8) continue;
|
|
|
|
|
var shape = state.getValue(PoweredRailBlock.SHAPE);
|
|
|
|
|
var exits = AbstractMinecart.exits(shape);
|
|
|
|
|
for (var exit : List.of(exits.getFirst(), exits.getSecond())) {
|
|
|
|
|
var adjacent =
|
|
|
|
|
step.pos.offset(
|
|
|
|
|
exit.getX(), shape.isSlope() ? exit.getY() + 1 : 0, exit.getZ());
|
|
|
|
|
for (var candidate : List.of(adjacent, adjacent.below())) {
|
|
|
|
|
if (!level.hasChunkAt(candidate)) continue;
|
|
|
|
|
var other = level.getBlockState(candidate);
|
|
|
|
|
if (other.is(Blocks.POWERED_RAIL)
|
|
|
|
|
&& connected(
|
|
|
|
|
step.pos,
|
|
|
|
|
shape,
|
|
|
|
|
candidate,
|
|
|
|
|
other.getValue(PoweredRailBlock.SHAPE)))
|
|
|
|
|
queue.add(new Step(candidate, step.distance + 1));
|
2026-09-13 09:16:42 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return best;
|
|
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
private static boolean connected(BlockPos a, RailShape sa, BlockPos b, RailShape sb) {
|
|
|
|
|
var ea = AbstractMinecart.exits(sa);
|
|
|
|
|
var eb = AbstractMinecart.exits(sb);
|
|
|
|
|
for (var exitA : List.of(ea.getFirst(), ea.getSecond())) {
|
|
|
|
|
if (a.getX() + exitA.getX() != b.getX()) continue;
|
|
|
|
|
if (a.getZ() + exitA.getZ() != b.getZ()) continue;
|
|
|
|
|
int heightA = exitHeight(a, sa, exitA);
|
|
|
|
|
for (var exitB : List.of(eb.getFirst(), eb.getSecond())) {
|
|
|
|
|
if (b.getX() + exitB.getX() != a.getX()) continue;
|
|
|
|
|
if (b.getZ() + exitB.getZ() != a.getZ()) continue;
|
|
|
|
|
if (heightA == exitHeight(b, sb, exitB)) return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 09:16:42 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
private static int exitHeight(BlockPos pos, RailShape shape, Vec3i exit) {
|
|
|
|
|
if (shape.isSlope()) return pos.getY() + exit.getY() + 1;
|
|
|
|
|
return pos.getY();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean stopsFastTravel(BlockState state) {
|
|
|
|
|
if (state.is(Blocks.DETECTOR_RAIL) || state.is(Blocks.ACTIVATOR_RAIL)) return true;
|
|
|
|
|
return state.is(Blocks.POWERED_RAIL) && !state.getValue(PoweredRailBlock.POWERED);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 09:16:42 +03:00
|
|
|
/** Old movement needs vanilla speed when approaching bends, slopes or the end of a track. */
|
2026-09-13 21:55:44 +03:00
|
|
|
public static double safeLimit(AbstractMinecart cart, ServerLevel level, double limit) {
|
|
|
|
|
if (AbstractMinecart.useExperimentalMovement(level) || limit <= .4) return limit;
|
|
|
|
|
var pos = cart.getCurrentBlockPosOrRailBelow();
|
|
|
|
|
var movement = cart.getDeltaMovement();
|
|
|
|
|
int dx = (int) Math.signum(movement.x), dz = (int) Math.signum(movement.z);
|
|
|
|
|
for (int i = 0; i <= 2; i++) {
|
|
|
|
|
var check = pos.offset(dx * i, 0, dz * i);
|
|
|
|
|
if (!level.hasChunkAt(check)) return .4;
|
|
|
|
|
BlockState state = level.getBlockState(check);
|
|
|
|
|
if (!(state.getBlock() instanceof BaseRailBlock rail))
|
|
|
|
|
return state.is(Blocks.SLIME_BLOCK) ? limit : .4;
|
|
|
|
|
RailShape shape = rail.getRailDirection(state, level, check, cart);
|
|
|
|
|
if (shape != RailShape.NORTH_SOUTH && shape != RailShape.EAST_WEST) return .4;
|
|
|
|
|
if (i > 0 && stopsFastTravel(state)) return .4;
|
2026-09-13 09:16:42 +03:00
|
|
|
// Never jump over a single slower regulator.
|
2026-09-13 21:55:44 +03:00
|
|
|
if (i > 0) limit = Math.min(limit, target(level, check, limit * 20) / 20);
|
2026-09-13 09:16:42 +03:00
|
|
|
}
|
|
|
|
|
return limit;
|
|
|
|
|
}
|
|
|
|
|
}
|