Search in sources :

Example 1 with RailShape

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);
}
Also used : Vec3i(net.minecraft.core.Vec3i) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) RailShape(net.minecraft.world.level.block.state.properties.RailShape) Vec3(net.minecraft.world.phys.Vec3)

Example 2 with RailShape

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;
}
Also used : RailShape(net.minecraft.world.level.block.state.properties.RailShape)

Example 3 with RailShape

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;
    }
}
Also used : RailShape(net.minecraft.world.level.block.state.properties.RailShape)

Example 4 with RailShape

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;
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) RailShape(net.minecraft.world.level.block.state.properties.RailShape) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos)

Example 5 with RailShape

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);
}
Also used : Entity(net.minecraft.world.entity.Entity) AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) BlockState(net.minecraft.world.level.block.state.BlockState) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) RailShape(net.minecraft.world.level.block.state.properties.RailShape) Vec3(net.minecraft.world.phys.Vec3) VecHelper(com.simibubi.create.foundation.utility.VecHelper) BlockPos(net.minecraft.core.BlockPos)

Aggregations

RailShape (net.minecraft.world.level.block.state.properties.RailShape)14 BlockState (net.minecraft.world.level.block.state.BlockState)9 BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)8 BlockPos (net.minecraft.core.BlockPos)7 AbstractMinecart (net.minecraft.world.entity.vehicle.AbstractMinecart)6 Direction (net.minecraft.core.Direction)5 Vec3 (net.minecraft.world.phys.Vec3)5 Level (net.minecraft.world.level.Level)4 Nonnull (javax.annotation.Nonnull)2 AxisDirection (net.minecraft.core.Direction.AxisDirection)2 Vec3i (net.minecraft.core.Vec3i)2 Player (net.minecraft.world.entity.player.Player)2 ItemStack (net.minecraft.world.item.ItemStack)2 Block (net.minecraft.world.level.block.Block)2 EntityPoolMinecart (vazkii.botania.common.entity.EntityPoolMinecart)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DirectionalAxisKineticBlock (com.simibubi.create.content.contraptions.base.DirectionalAxisKineticBlock)1 StabilizedContraption (com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption)1 LinearChassisBlock (com.simibubi.create.content.contraptions.components.structureMovement.chassis.LinearChassisBlock)1