Files
AstraContent/src/main/java/online/astradev/content/rail/RailSpeed.java
T

112 lines
5.1 KiB
Java
Raw Normal View History

package online.astradev.content.rail;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Vec3i;
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;
import java.util.*;
public final class RailSpeed {
private RailSpeed() {}
public static int signalSpeed(int signal) {
return Math.clamp(signal, 0, 15) * 2;
}
public static double target(ServerLevel level, BlockPos pos, double previous) {
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)));
}
/** Mirror the vanilla eight-rail power reach, preserving bends, slopes and highest input. */
public static int signal(ServerLevel level, BlockPos origin) {
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);
if (!state.is(Blocks.POWERED_RAIL)) continue;
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));
}
}
}
return best;
}
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;
}
}
return false;
}
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);
}
/** Old movement needs vanilla speed when approaching bends, slopes or the end of a track. */
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;
// Never jump over a single slower regulator.
if (i > 0) limit = Math.min(limit, target(level, check, limit * 20) / 20);
}
return limit;
}
}