Search in sources :

Example 1 with IRailReception

use of club.nsdn.nyasamarailway.block.rail.IRailReception in project NyaSamaRailway by NSDN.

the class TileEntityRailReception method tick.

public static void tick(boolean isAnti, List bBox, TileEntityRailReception rail, boolean playerDetectable) {
    if (rail == null)
        return;
    World world = rail.worldObj;
    int x = rail.xCoord, y = rail.yCoord, z = rail.zCoord;
    boolean hasCart = !bBox.isEmpty();
    if (!playerDetectable) {
        if (rail.getSender() != null && rail.getBlockType() instanceof IRailReception) {
            IRailReception blockRail = (IRailReception) rail.getBlockType();
            if (!isRailPowered(world, x, y, z) && rail.senderIsPowered()) {
                blockRail.setRailState(world, x, y, z, true);
            } else if (isRailPowered(world, x, y, z) && !rail.senderIsPowered()) {
                blockRail.setRailState(world, x, y, z, false);
            }
        }
    } else {
        if (rail.cartType.equals("loco")) {
            if (!hasCart) {
                rail.reset();
            } else {
                if (rail.getTarget() != null) {
                    rail.controlTarget(rail.doorCtrl);
                }
                for (Object obj : bBox) {
                    if (obj instanceof LocoBase) {
                        loco(isAnti, (LocoBase) obj, rail);
                        break;
                    }
                }
            }
        } else {
            if (hasCart) {
                if (rail.getTarget() != null) {
                    rail.controlTarget(rail.doorCtrl);
                }
                for (Object obj : bBox) {
                    if (obj instanceof EntityMinecart) {
                        if (((EntityMinecart) obj).riddenByEntity == null) {
                            EntityMinecart cart = (EntityMinecart) obj;
                            cart.motionX = 0.0D;
                            cart.motionZ = 0.0D;
                            cart.setPosition(x + 0.5, y + 0.5, z + 0.5);
                            rail.reset();
                            rail.prev = true;
                        } else if (rail.prev) {
                            rail.prev = false;
                        // rail.delay = DELAY_TIME * 15 - 1;
                        }
                        break;
                    }
                }
            } else {
                rail.count += 1;
                if (rail.count >= TileEntityRailReception.SPAWN_DELAY * 20) {
                    rail.reset();
                    rail.spawn(rail.worldObj, x, y, z);
                }
            }
        }
    }
}
Also used : IRailReception(club.nsdn.nyasamarailway.block.rail.IRailReception) LocoBase(club.nsdn.nyasamarailway.entity.LocoBase) World(net.minecraft.world.World) EntityMinecart(net.minecraft.entity.item.EntityMinecart)

Example 2 with IRailReception

use of club.nsdn.nyasamarailway.block.rail.IRailReception in project NyaSamaRailway by NSDN.

the class LocoBase method func_145821_a.

@Override
protected void func_145821_a(int x, int y, int z, double maxVel, double slopeAdj, Block block, int meta) {
    // applyPush
    if (worldObj.getTileEntity(x, y, z) instanceof TileEntityTrackSideReception) {
        TileEntityTrackSideReception reception = (TileEntityTrackSideReception) worldObj.getTileEntity(x, y, z);
        if (reception.delay < reception.setDelay * 20) {
            applyDrag();
            return;
        }
    }
    if (block instanceof IRailReception) {
        if (!((IRailReception) block).checkNearbySameRail(worldObj, x, y, z))
            if (!((IRailReception) block).timeExceed(worldObj, x, y, z)) {
                applyDrag();
                return;
            }
    }
    /* ******************************** MAIN FUNC ******************************** */
    this.fallDistance = 0.0F;
    Vec3 vec3 = this.func_70489_a(this.posX, this.posY, this.posZ);
    this.posY = (double) y;
    boolean isRailPowered = false;
    boolean slowDown = false;
    if (block == Blocks.golden_rail) {
        // Stock Powered Rail, Push-Brake
        isRailPowered = (this.worldObj.getBlockMetadata(x, y, z) & 8) != 0;
        slowDown = !isRailPowered;
    }
    if (((BlockRailBase) block).isPowered()) {
        meta &= 7;
    }
    if (meta >= 2 && meta <= 5) {
        this.posY = (double) (y + 1);
    }
    if (meta == 2) {
        this.motionX -= slopeAdj;
    }
    if (meta == 3) {
        this.motionX += slopeAdj;
    }
    if (meta == 4) {
        this.motionZ += slopeAdj;
    }
    if (meta == 5) {
        this.motionZ -= slopeAdj;
    }
    int[][] aint = matrix[meta];
    double d2 = (double) (aint[1][0] - aint[0][0]);
    double d3 = (double) (aint[1][2] - aint[0][2]);
    double d4 = Math.sqrt(d2 * d2 + d3 * d3);
    double d5 = this.motionX * d2 + this.motionZ * d3;
    if (d5 < 0.0D) {
        d2 = -d2;
        d3 = -d3;
    }
    double vel = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    if (vel > getMaxCartSpeedOnRail()) {
        vel = getMaxCartSpeedOnRail();
    }
    this.motionX = vel * d2 / d4;
    this.motionZ = vel * d3 / d4;
    double d7;
    double d8;
    double d9;
    double d10;
    if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityLivingBase) {
        d7 = (double) ((EntityLivingBase) this.riddenByEntity).moveForward;
        if (d7 > 0.0D) {
            d8 = -Math.sin((double) (this.riddenByEntity.rotationYaw * 3.1415927F / 180.0F));
            d9 = Math.cos((double) (this.riddenByEntity.rotationYaw * 3.1415927F / 180.0F));
            d10 = this.motionX * this.motionX + this.motionZ * this.motionZ;
            if (d10 < 0.01D) {
                this.motionX += d8 * 0.1D;
                this.motionZ += d9 * 0.1D;
                slowDown = false;
            }
        }
    }
    if (slowDown && this.shouldDoRailFunctions()) {
        d7 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
        if (d7 < 0.03D) {
            this.motionX *= 0.0D;
            this.motionY *= 0.0D;
            this.motionZ *= 0.0D;
        } else {
            this.motionX *= 0.5D;
            this.motionY *= 0.0D;
            this.motionZ *= 0.5D;
        }
    }
    d7 = 0.0D;
    d8 = (double) x + 0.5D + (double) aint[0][0] * 0.5D;
    d9 = (double) z + 0.5D + (double) aint[0][2] * 0.5D;
    d10 = (double) x + 0.5D + (double) aint[1][0] * 0.5D;
    double d11 = (double) z + 0.5D + (double) aint[1][2] * 0.5D;
    d2 = d10 - d8;
    d3 = d11 - d9;
    if (d2 == 0.0D) {
        this.posX = (double) x + 0.5D;
        d7 = this.posZ - (double) z;
    } else if (d3 == 0.0D) {
        this.posZ = (double) z + 0.5D;
        d7 = this.posX - (double) x;
    } else {
        double d12 = this.posX - d8;
        double d13 = this.posZ - d9;
        d7 = (d12 * d2 + d13 * d3) * 2.0D;
    }
    this.posX = d8 + d2 * d7;
    this.posZ = d9 + d3 * d7;
    this.setPosition(this.posX, this.posY + (double) this.yOffset, this.posZ);
    this.moveMinecartOnRail(x, y, z, maxVel);
    if (aint[0][1] != 0 && MathHelper.floor_double(this.posX) - x == aint[0][0] && MathHelper.floor_double(this.posZ) - z == aint[0][2]) {
        this.setPosition(this.posX, this.posY + (double) aint[0][1], this.posZ);
    } else if (aint[1][1] != 0 && MathHelper.floor_double(this.posX) - x == aint[1][0] && MathHelper.floor_double(this.posZ) - z == aint[1][2]) {
        this.setPosition(this.posX, this.posY + (double) aint[1][1], this.posZ);
    }
    this.applyDrag();
    Vec3 vec31 = this.func_70489_a(this.posX, this.posY, this.posZ);
    if (vec31 != null && vec3 != null) {
        double d14 = (vec3.yCoord - vec31.yCoord) * 0.05D;
        vel = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
        if (vel > 0.0D) {
            this.motionX = this.motionX / vel * (vel + d14);
            this.motionZ = this.motionZ / vel * (vel + d14);
        }
        this.setPosition(this.posX, vec31.yCoord, this.posZ);
    }
    /**
     * HOLY SHIT! THE CODE CAUSES BUG!
     *        int pX = MathHelper.floor_double(this.posX);
     *        int pZ = MathHelper.floor_double(this.posZ);
     *        if (pX != x || pZ != z) {
     *            vel = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
     *            this.motionX = vel * (double)(pX - x);
     *            this.motionZ = vel * (double)(pZ - z);
     *        }
     */
    if (this.shouldDoRailFunctions()) {
        ((BlockRailBase) block).onMinecartPass(this.worldObj, this, x, y, z);
    }
    if (isRailPowered && this.shouldDoRailFunctions()) {
        double d15 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
        if (d15 > 0.01D) {
            double d16 = 0.06D;
            this.motionX += this.motionX / d15 * d16;
            this.motionZ += this.motionZ / d15 * d16;
        } else if (meta == 1) {
            if (this.worldObj.getBlock(x - 1, y, z).isNormalCube()) {
                this.motionX = 0.02D;
            } else if (this.worldObj.getBlock(x + 1, y, z).isNormalCube()) {
                this.motionX = -0.02D;
            }
        } else if (meta == 0) {
            if (this.worldObj.getBlock(x, y, z - 1).isNormalCube()) {
                this.motionZ = 0.02D;
            } else if (this.worldObj.getBlock(x, y, z + 1).isNormalCube()) {
                this.motionZ = -0.02D;
            }
        }
    }
}
Also used : IRailReception(club.nsdn.nyasamarailway.block.rail.IRailReception) EntityLivingBase(net.minecraft.entity.EntityLivingBase) TileEntityTrackSideReception(club.nsdn.nyasamarailway.tileblock.signal.TileEntityTrackSideReception) BlockRailBase(net.minecraft.block.BlockRailBase)

Aggregations

IRailReception (club.nsdn.nyasamarailway.block.rail.IRailReception)2 LocoBase (club.nsdn.nyasamarailway.entity.LocoBase)1 TileEntityTrackSideReception (club.nsdn.nyasamarailway.tileblock.signal.TileEntityTrackSideReception)1 BlockRailBase (net.minecraft.block.BlockRailBase)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityMinecart (net.minecraft.entity.item.EntityMinecart)1 World (net.minecraft.world.World)1