73 lines
4.1 KiB
Java
73 lines
4.1 KiB
Java
package online.astradev.content.rail;
|
|
|
|
import java.util.*;
|
|
import net.minecraft.core.BlockPos;
|
|
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;
|
|
|
|
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 x:List.of(ea.getFirst(),ea.getSecond())) for(var y:List.of(eb.getFirst(),eb.getSecond()))
|
|
if(a.getX()+x.getX()==b.getX() && a.getZ()+x.getZ()==b.getZ()
|
|
&& b.getX()+y.getX()==a.getX() && b.getZ()+y.getZ()==a.getZ()
|
|
&& a.getY()+(sa.isSlope()?x.getY()+1:0)==b.getY()+(sb.isSlope()?y.getY()+1:0)) return true;
|
|
return false;
|
|
}
|
|
/** 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 .4;
|
|
RailShape shape=rail.getRailDirection(state,level,check,cart);
|
|
if(shape!=RailShape.NORTH_SOUTH && shape!=RailShape.EAST_WEST) return .4;
|
|
if (i>0 && (state.is(Blocks.DETECTOR_RAIL) || state.is(Blocks.ACTIVATOR_RAIL)
|
|
|| state.is(Blocks.POWERED_RAIL) && !state.getValue(PoweredRailBlock.POWERED))) return .4;
|
|
// Never jump over a single slower regulator.
|
|
if (i>0) limit=Math.min(limit,target(level,check,limit*20)/20);
|
|
}
|
|
return limit;
|
|
}
|
|
}
|