use of net.minecraft.world.level.block.state.properties.RailShape in project Create by Creators-of-Create.
the class MinecartSim2020 method moveCartAlongTrack.
public static void moveCartAlongTrack(AbstractMinecart cart, Vec3 forcedMovement, BlockPos cartPos, BlockState trackState) {
if (forcedMovement.equals(Vec3.ZERO))
return;
Vec3 previousMotion = cart.getDeltaMovement();
cart.fallDistance = 0.0F;
double x = cart.getX();
double y = cart.getY();
double z = cart.getZ();
double actualX = x;
double actualY = y;
double actualZ = z;
Vec3 actualVec = cart.getPos(actualX, actualY, actualZ);
actualY = cartPos.getY() + 1;
BaseRailBlock abstractrailblock = (BaseRailBlock) trackState.getBlock();
RailShape railshape = abstractrailblock.getRailDirection(trackState, cart.level, cartPos, cart);
switch(railshape) {
case ASCENDING_EAST:
forcedMovement = forcedMovement.add(-1 * cart.getSlopeAdjustment(), 0.0D, 0.0D);
actualY++;
break;
case ASCENDING_WEST:
forcedMovement = forcedMovement.add(cart.getSlopeAdjustment(), 0.0D, 0.0D);
actualY++;
break;
case ASCENDING_NORTH:
forcedMovement = forcedMovement.add(0.0D, 0.0D, cart.getSlopeAdjustment());
actualY++;
break;
case ASCENDING_SOUTH:
forcedMovement = forcedMovement.add(0.0D, 0.0D, -1 * cart.getSlopeAdjustment());
actualY++;
default:
break;
}
Pair<Vec3i, Vec3i> pair = MATRIX.get(railshape);
Vec3i Vector3i = pair.getFirst();
Vec3i Vector3i1 = pair.getSecond();
double d4 = (double) (Vector3i1.getX() - Vector3i.getX());
double d5 = (double) (Vector3i1.getZ() - Vector3i.getZ());
// double d6 = Math.sqrt(d4 * d4 + d5 * d5);
double d7 = forcedMovement.x * d4 + forcedMovement.z * d5;
if (d7 < 0.0D) {
d4 = -d4;
d5 = -d5;
}
double d23 = (double) cartPos.getX() + 0.5D + (double) Vector3i.getX() * 0.5D;
double d10 = (double) cartPos.getZ() + 0.5D + (double) Vector3i.getZ() * 0.5D;
double d12 = (double) cartPos.getX() + 0.5D + (double) Vector3i1.getX() * 0.5D;
double d13 = (double) cartPos.getZ() + 0.5D + (double) Vector3i1.getZ() * 0.5D;
d4 = d12 - d23;
d5 = d13 - d10;
double d14;
if (d4 == 0.0D) {
d14 = actualZ - (double) cartPos.getZ();
} else if (d5 == 0.0D) {
d14 = actualX - (double) cartPos.getX();
} else {
double d15 = actualX - d23;
double d16 = actualZ - d10;
d14 = (d15 * d4 + d16 * d5) * 2.0D;
}
actualX = d23 + d4 * d14;
actualZ = d10 + d5 * d14;
cart.setPos(actualX, actualY, actualZ);
cart.setDeltaMovement(forcedMovement);
cart.moveMinecartOnRail(cartPos);
x = cart.getX();
y = cart.getY();
z = cart.getZ();
if (Vector3i.getY() != 0 && Mth.floor(x) - cartPos.getX() == Vector3i.getX() && Mth.floor(z) - cartPos.getZ() == Vector3i.getZ()) {
cart.setPos(x, y + (double) Vector3i.getY(), z);
} else if (Vector3i1.getY() != 0 && Mth.floor(x) - cartPos.getX() == Vector3i1.getX() && Mth.floor(z) - cartPos.getZ() == Vector3i1.getZ()) {
cart.setPos(x, y + (double) Vector3i1.getY(), z);
}
x = cart.getX();
y = cart.getY();
z = cart.getZ();
Vec3 Vector3d3 = cart.getPos(x, y, z);
if (Vector3d3 != null && actualVec != null) {
double d17 = (actualVec.y - Vector3d3.y) * 0.05D;
Vec3 Vector3d4 = cart.getDeltaMovement();
double d18 = Math.sqrt(Vector3d4.horizontalDistanceSqr());
if (d18 > 0.0D) {
cart.setDeltaMovement(Vector3d4.multiply((d18 + d17) / d18, 1.0D, (d18 + d17) / d18));
}
cart.setPos(x, Vector3d3.y, z);
}
x = cart.getX();
y = cart.getY();
z = cart.getZ();
int j = Mth.floor(x);
int i = Mth.floor(z);
if (j != cartPos.getX() || i != cartPos.getZ()) {
Vec3 Vector3d5 = cart.getDeltaMovement();
double d26 = Math.sqrt(Vector3d5.horizontalDistanceSqr());
cart.setDeltaMovement(d26 * (double) (j - cartPos.getX()), Vector3d5.y, d26 * (double) (i - cartPos.getZ()));
}
cart.setDeltaMovement(previousMotion);
}
use of net.minecraft.world.level.block.state.properties.RailShape in project Create by Creators-of-Create.
the class ControllerRailBlock method rotate.
@Override
public BlockState rotate(BlockState state, Rotation rotation) {
if (rotation == Rotation.NONE)
return state;
RailShape railshape = Blocks.POWERED_RAIL.defaultBlockState().setValue(SHAPE, state.getValue(SHAPE)).rotate(rotation).getValue(SHAPE);
state = state.setValue(SHAPE, railshape);
if (rotation == Rotation.CLOCKWISE_180 || (getPointingTowards(state).getAxis() == Axis.Z) == (rotation == Rotation.COUNTERCLOCKWISE_90))
return state.cycle(BACKWARDS);
return state;
}
use of net.minecraft.world.level.block.state.properties.RailShape in project Create by Creators-of-Create.
the class ControllerRailGenerator method getYRotation.
@Override
protected int getYRotation(BlockState state) {
RailShape shape = state.getValue(ControllerRailBlock.SHAPE);
boolean backwards = ControllerRailBlock.isStateBackwards(state);
int rotation = backwards ? 180 : 0;
switch(shape) {
case EAST_WEST:
case ASCENDING_WEST:
return rotation + 270;
case ASCENDING_EAST:
return rotation + 90;
case ASCENDING_SOUTH:
return rotation + 180;
default:
return rotation;
}
}
use of net.minecraft.world.level.block.state.properties.RailShape in project Create by Creators-of-Create.
the class CouplingPhysics method hardCollisionStep.
public static void hardCollisionStep(Level world, Couple<AbstractMinecart> carts, double couplingLength) {
if (!MinecartSim2020.canAddMotion(carts.get(false)) && MinecartSim2020.canAddMotion(carts.get(true)))
carts = carts.swap();
Couple<Vec3> corrections = Couple.create(null, null);
Couple<Float> maxSpeed = carts.map(AbstractMinecart::getMaxCartSpeedOnRail);
boolean firstLoop = true;
for (boolean current : new boolean[] { true, false, true }) {
AbstractMinecart cart = carts.get(current);
AbstractMinecart otherCart = carts.get(!current);
float stress = (float) (couplingLength - cart.position().distanceTo(otherCart.position()));
if (Math.abs(stress) < 1 / 8f)
continue;
RailShape shape = null;
BlockPos railPosition = cart.getCurrentRailPosition();
BlockState railState = world.getBlockState(railPosition.above());
if (railState.getBlock() instanceof BaseRailBlock) {
BaseRailBlock block = (BaseRailBlock) railState.getBlock();
shape = block.getRailDirection(railState, world, railPosition, cart);
}
Vec3 correction = Vec3.ZERO;
Vec3 pos = cart.position();
Vec3 link = otherCart.position().subtract(pos);
float correctionMagnitude = firstLoop ? -stress / 2f : -stress;
if (!MinecartSim2020.canAddMotion(cart))
correctionMagnitude /= 2;
correction = shape != null ? followLinkOnRail(link, pos, correctionMagnitude, MinecartSim2020.getRailVec(shape)).subtract(pos) : link.normalize().scale(correctionMagnitude);
float maxResolveSpeed = 1.75f;
correction = VecHelper.clamp(correction, Math.min(maxResolveSpeed, maxSpeed.get(current)));
if (corrections.get(current) == null)
corrections.set(current, correction);
if (shape != null)
MinecartSim2020.moveCartAlongTrack(cart, correction, railPosition, railState);
else {
cart.move(MoverType.SELF, correction);
cart.setDeltaMovement(cart.getDeltaMovement().scale(0.95f));
}
firstLoop = false;
}
}
use of net.minecraft.world.level.block.state.properties.RailShape in project Create by Creators-of-Create.
the class CouplingPhysics method softCollisionStep.
public static void softCollisionStep(Level world, Couple<AbstractMinecart> carts, double couplingLength) {
Couple<Float> maxSpeed = carts.map(AbstractMinecart::getMaxCartSpeedOnRail);
Couple<Boolean> canAddmotion = carts.map(MinecartSim2020::canAddMotion);
// Assuming Minecarts will never move faster than 1 block/tick
Couple<Vec3> motions = carts.map(Entity::getDeltaMovement);
motions.replaceWithParams(VecHelper::clamp, Couple.create(1f, 1f));
Couple<Vec3> nextPositions = carts.map(MinecartSim2020::predictNextPositionOf);
Couple<RailShape> shapes = carts.mapWithContext((minecart, current) -> {
Vec3 vec = nextPositions.get(current);
int x = Mth.floor(vec.x());
int y = Mth.floor(vec.y());
int z = Mth.floor(vec.z());
BlockPos pos = new BlockPos(x, y - 1, z);
if (minecart.level.getBlockState(pos).is(BlockTags.RAILS))
pos = pos.below();
BlockPos railPosition = pos;
BlockState railState = world.getBlockState(railPosition.above());
if (!(railState.getBlock() instanceof BaseRailBlock))
return null;
BaseRailBlock block = (BaseRailBlock) railState.getBlock();
return block.getRailDirection(railState, world, railPosition, minecart);
});
float futureStress = (float) (couplingLength - nextPositions.getFirst().distanceTo(nextPositions.getSecond()));
if (Mth.equal(futureStress, 0D))
return;
for (boolean current : Iterate.trueAndFalse) {
Vec3 correction = Vec3.ZERO;
Vec3 pos = nextPositions.get(current);
Vec3 link = nextPositions.get(!current).subtract(pos);
float correctionMagnitude = -futureStress / 2f;
if (canAddmotion.get(current) != canAddmotion.get(!current))
correctionMagnitude = !canAddmotion.get(current) ? 0 : correctionMagnitude * 2;
if (!canAddmotion.get(current))
continue;
RailShape shape = shapes.get(current);
if (shape != null) {
Vec3 railVec = MinecartSim2020.getRailVec(shape);
correction = followLinkOnRail(link, pos, correctionMagnitude, railVec).subtract(pos);
} else
correction = link.normalize().scale(correctionMagnitude);
correction = VecHelper.clamp(correction, maxSpeed.get(current));
motions.set(current, motions.get(current).add(correction));
}
motions.replaceWithParams(VecHelper::clamp, maxSpeed);
carts.forEachWithParams(Entity::setDeltaMovement, motions);
}
Aggregations