Search in sources :

Example 1 with BaseRailBlock

use of net.minecraft.world.level.block.BaseRailBlock 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 BaseRailBlock

use of net.minecraft.world.level.block.BaseRailBlock 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 3 with BaseRailBlock

use of net.minecraft.world.level.block.BaseRailBlock 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)

Example 4 with BaseRailBlock

use of net.minecraft.world.level.block.BaseRailBlock in project Create by Creators-of-Create.

the class CartAssemblerBlockItem method tryPlaceAssembler.

public boolean tryPlaceAssembler(UseOnContext context) {
    BlockPos pos = context.getClickedPos();
    Level world = context.getLevel();
    BlockState state = world.getBlockState(pos);
    Block block = state.getBlock();
    Player player = context.getPlayer();
    if (player == null)
        return false;
    if (!(block instanceof BaseRailBlock)) {
        Lang.sendStatus(player, "block.cart_assembler.invalid");
        return false;
    }
    RailShape shape = ((BaseRailBlock) block).getRailDirection(state, world, pos, null);
    if (shape != RailShape.EAST_WEST && shape != RailShape.NORTH_SOUTH)
        return false;
    BlockState newState = AllBlocks.CART_ASSEMBLER.getDefaultState().setValue(CartAssemblerBlock.RAIL_SHAPE, shape);
    CartAssembleRailType newType = null;
    for (CartAssembleRailType type : CartAssembleRailType.values()) if (type.matches(state))
        newType = type;
    if (newType == null)
        return false;
    if (world.isClientSide)
        return true;
    newState = newState.setValue(CartAssemblerBlock.RAIL_TYPE, newType);
    if (state.hasProperty(ControllerRailBlock.BACKWARDS))
        newState = newState.setValue(CartAssemblerBlock.BACKWARDS, state.getValue(ControllerRailBlock.BACKWARDS));
    else {
        Direction direction = player.getMotionDirection();
        newState = newState.setValue(CartAssemblerBlock.BACKWARDS, direction.getAxisDirection() == AxisDirection.POSITIVE);
    }
    world.setBlockAndUpdate(pos, newState);
    if (!player.isCreative())
        context.getItemInHand().shrink(1);
    return true;
}
Also used : Player(net.minecraft.world.entity.player.Player) BlockState(net.minecraft.world.level.block.state.BlockState) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) RailShape(net.minecraft.world.level.block.state.properties.RailShape) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) ControllerRailBlock(com.simibubi.create.content.contraptions.components.tracks.ControllerRailBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) Direction(net.minecraft.core.Direction) AxisDirection(net.minecraft.core.Direction.AxisDirection)

Example 5 with BaseRailBlock

use of net.minecraft.world.level.block.BaseRailBlock in project Create by Creators-of-Create.

the class MinecartContraptionItem method useOn.

// Taken and adjusted from MinecartItem
@Override
public InteractionResult useOn(UseOnContext context) {
    Level world = context.getLevel();
    BlockPos blockpos = context.getClickedPos();
    BlockState blockstate = world.getBlockState(blockpos);
    if (!blockstate.is(BlockTags.RAILS)) {
        return InteractionResult.FAIL;
    } else {
        ItemStack itemstack = context.getItemInHand();
        if (!world.isClientSide) {
            RailShape railshape = blockstate.getBlock() instanceof BaseRailBlock ? ((BaseRailBlock) blockstate.getBlock()).getRailDirection(blockstate, world, blockpos, null) : RailShape.NORTH_SOUTH;
            double d0 = 0.0D;
            if (railshape.isAscending()) {
                d0 = 0.5D;
            }
            AbstractMinecart abstractminecartentity = AbstractMinecart.createMinecart(world, (double) blockpos.getX() + 0.5D, (double) blockpos.getY() + 0.0625D + d0, (double) blockpos.getZ() + 0.5D, this.minecartType);
            if (itemstack.hasCustomHoverName())
                abstractminecartentity.setCustomName(itemstack.getHoverName());
            Player player = context.getPlayer();
            world.addFreshEntity(abstractminecartentity);
            addContraptionToMinecart(world, itemstack, abstractminecartentity, player == null ? null : player.getDirection());
        }
        itemstack.shrink(1);
        return InteractionResult.SUCCESS;
    }
}
Also used : Player(net.minecraft.world.entity.player.Player) 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) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)11 BlockState (net.minecraft.world.level.block.state.BlockState)9 BlockPos (net.minecraft.core.BlockPos)8 RailShape (net.minecraft.world.level.block.state.properties.RailShape)8 AbstractMinecart (net.minecraft.world.entity.vehicle.AbstractMinecart)6 Vec3 (net.minecraft.world.phys.Vec3)5 Direction (net.minecraft.core.Direction)4 Level (net.minecraft.world.level.Level)4 Nonnull (javax.annotation.Nonnull)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 KineticTileEntity (com.simibubi.create.content.contraptions.base.KineticTileEntity)1 StabilizedContraption (com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption)1 CapabilityMinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController)1 MinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController)1 ControllerRailBlock (com.simibubi.create.content.contraptions.components.tracks.ControllerRailBlock)1 VecHelper (com.simibubi.create.foundation.utility.VecHelper)1 AxisDirection (net.minecraft.core.Direction.AxisDirection)1