Search in sources :

Example 46 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class BlockFarmland method onUpdate.

@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_RANDOM) {
        Vector3 v = new Vector3();
        if (this.level.getBlock(v.setComponents(x, this.y + 1, z)) instanceof BlockCrops) {
            return 0;
        }
        if (this.level.getBlock(v.setComponents(x, this.y + 1, z)).isSolid()) {
            this.level.setBlock(this, new BlockDirt(), false, true);
            return Level.BLOCK_UPDATE_RANDOM;
        }
        boolean found = false;
        if (this.level.isRaining()) {
            found = true;
        } else {
            for (int x = (int) this.x - 4; x <= this.x + 4; x++) {
                for (int z = (int) this.z - 4; z <= this.z + 4; z++) {
                    for (int y = (int) this.y; y <= this.y + 1; y++) {
                        if (z == this.z && x == this.x && y == this.y) {
                            continue;
                        }
                        v.setComponents(x, y, z);
                        int block = this.level.getBlockIdAt(v.getFloorX(), v.getFloorY(), v.getFloorZ());
                        if (block == WATER || block == STILL_WATER) {
                            found = true;
                            break;
                        }
                    }
                }
            }
        }
        Block block = this.level.getBlock(v.setComponents(x, y - 1, z));
        if (found || block instanceof BlockWater) {
            if (this.getDamage() < 7) {
                this.setDamage(7);
                this.level.setBlock(this, this, false, false);
            }
            return Level.BLOCK_UPDATE_RANDOM;
        }
        if (this.getDamage() > 0) {
            this.setDamage(this.getDamage() - 1);
            this.level.setBlock(this, this, false, false);
        } else {
            this.level.setBlock(this, Block.get(Block.DIRT), false, true);
        }
        return Level.BLOCK_UPDATE_RANDOM;
    }
    return 0;
}
Also used : ItemBlock(cn.nukkit.item.ItemBlock) Vector3(cn.nukkit.math.Vector3)

Example 47 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class BlockGrass method onUpdate.

@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_RANDOM) {
        Block block = this.getLevel().getBlock(new Vector3(this.x, this.y, this.z));
        if (block.up().getLightLevel() < 4) {
            BlockSpreadEvent ev = new BlockSpreadEvent(block, this, new BlockDirt());
            Server.getInstance().getPluginManager().callEvent(ev);
        } else if (block.up().getLightLevel() >= 9) {
            for (int l = 0; l < 4; ++l) {
                NukkitRandom random = new NukkitRandom();
                int x = random.nextRange((int) this.x - 1, (int) this.x + 1);
                int y = random.nextRange((int) this.y - 2, (int) this.y + 2);
                int z = random.nextRange((int) this.z - 1, (int) this.z + 1);
                Block blocks = this.getLevel().getBlock(new Vector3(x, y, z));
                if (blocks.getId() == Block.DIRT && blocks.getDamage() == 0x0F && blocks.up().getLightLevel() >= 4 && blocks.z <= 2) {
                    BlockSpreadEvent ev = new BlockSpreadEvent(blocks, this, new BlockGrass());
                    Server.getInstance().getPluginManager().callEvent(ev);
                    if (!ev.isCancelled()) {
                        this.getLevel().setBlock(blocks, ev.getNewState());
                    }
                }
            }
        }
    }
    return 0;
}
Also used : BlockSpreadEvent(cn.nukkit.event.block.BlockSpreadEvent) Vector3(cn.nukkit.math.Vector3) NukkitRandom(cn.nukkit.math.NukkitRandom)

Example 48 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class EntityMinecartAbstract method processMovement.

private void processMovement(int dx, int dy, int dz, BlockRail block) {
    fallDistance = 0.0F;
    Vector3 vector = getNextRail(x, y, z);
    y = (double) dy;
    boolean isPowered = false;
    boolean isSlowed = false;
    if (block instanceof BlockRailPowered) {
        isPowered = block.isActive();
        isSlowed = !block.isActive();
    }
    switch(Orientation.byMetadata(block.getRealMeta())) {
        case ASCENDING_NORTH:
            motionX -= 0.0078125D;
            y += 1;
            break;
        case ASCENDING_SOUTH:
            motionX += 0.0078125D;
            y += 1;
            break;
        case ASCENDING_EAST:
            motionZ += 0.0078125D;
            y += 1;
            break;
        case ASCENDING_WEST:
            motionZ -= 0.0078125D;
            y += 1;
            break;
    }
    int[][] facing = matrix[block.getRealMeta()];
    double facing1 = (double) (facing[1][0] - facing[0][0]);
    double facing2 = (double) (facing[1][2] - facing[0][2]);
    double speedOnTurns = Math.sqrt(facing1 * facing1 + facing2 * facing2);
    double realFacing = motionX * facing1 + motionZ * facing2;
    if (realFacing < 0) {
        facing1 = -facing1;
        facing2 = -facing2;
    }
    double squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
    if (squareOfFame > 2) {
        squareOfFame = 2;
    }
    motionX = squareOfFame * facing1 / speedOnTurns;
    motionZ = squareOfFame * facing2 / speedOnTurns;
    double expectedSpeed;
    // PlayerYawNegative
    double playerYawNeg;
    // PlayerYawPositive
    double playerYawPos;
    double motion;
    if (linkedEntity != null && linkedEntity instanceof EntityLiving) {
        expectedSpeed = currentSpeed;
        if (expectedSpeed > 0) {
            // This is a trajectory (Angle of elevation)
            playerYawNeg = -Math.sin(linkedEntity.yaw * 3.1415927F / 180.0F);
            playerYawPos = Math.cos(linkedEntity.yaw * 3.1415927F / 180.0F);
            motion = motionX * motionX + motionZ * motionZ;
            if (motion < 0.01D) {
                motionX += playerYawNeg * 0.1D;
                motionZ += playerYawPos * 0.1D;
                isSlowed = false;
            }
        }
    }
    // http://minecraft.gamepedia.com/Powered_Rail#Rail
    if (isSlowed) {
        expectedSpeed = Math.sqrt(motionX * motionX + motionZ * motionZ);
        if (expectedSpeed < 0.03D) {
            motionX *= 0;
            motionY *= 0;
            motionZ *= 0;
        } else {
            motionX *= 0.5D;
            motionY *= 0;
            motionZ *= 0.5D;
        }
    }
    playerYawNeg = (double) dx + 0.5D + (double) facing[0][0] * 0.5D;
    playerYawPos = (double) dz + 0.5D + (double) facing[0][2] * 0.5D;
    motion = (double) dx + 0.5D + (double) facing[1][0] * 0.5D;
    double wallOfFame = (double) dz + 0.5D + (double) facing[1][2] * 0.5D;
    facing1 = motion - playerYawNeg;
    facing2 = wallOfFame - playerYawPos;
    double motX;
    double motZ;
    if (facing1 == 0) {
        x = (double) dx + 0.5D;
        expectedSpeed = z - (double) dz;
    } else if (facing2 == 0) {
        z = (double) dz + 0.5D;
        expectedSpeed = x - (double) dx;
    } else {
        motX = x - playerYawNeg;
        motZ = z - playerYawPos;
        expectedSpeed = (motX * facing1 + motZ * facing2) * 2;
    }
    x = playerYawNeg + facing1 * expectedSpeed;
    z = playerYawPos + facing2 * expectedSpeed;
    // Hehe, my minstake :3
    setPosition(new Vector3(x, y, z));
    motX = motionX;
    motZ = motionZ;
    if (linkedEntity != null) {
        motX *= 0.75D;
        motZ *= 0.75D;
    }
    motX = NukkitMath.clamp(motX, -getMaxSpeed(), getMaxSpeed());
    motZ = NukkitMath.clamp(motZ, -getMaxSpeed(), getMaxSpeed());
    move(motX, 0, motZ);
    if (facing[0][1] != 0 && MathHelper.floor(x) - dx == facing[0][0] && MathHelper.floor(z) - dz == facing[0][2]) {
        setPosition(new Vector3(x, y + (double) facing[0][1], z));
    } else if (facing[1][1] != 0 && MathHelper.floor(x) - dx == facing[1][0] && MathHelper.floor(z) - dz == facing[1][2]) {
        setPosition(new Vector3(x, y + (double) facing[1][1], z));
    }
    applyDrag();
    Vector3 vector1 = getNextRail(x, y, z);
    if (vector1 != null && vector != null) {
        double d14 = (vector.y - vector1.y) * 0.05D;
        squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
        if (squareOfFame > 0) {
            motionX = motionX / squareOfFame * (squareOfFame + d14);
            motionZ = motionZ / squareOfFame * (squareOfFame + d14);
        }
        setPosition(new Vector3(x, vector1.y, z));
    }
    int floorX = MathHelper.floor(x);
    int floorZ = MathHelper.floor(z);
    if (floorX != dx || floorZ != dz) {
        squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
        motionX = squareOfFame * (double) (floorX - dx);
        motionZ = squareOfFame * (double) (floorZ - dz);
    }
    if (isPowered) {
        double newMovie = Math.sqrt(motionX * motionX + motionZ * motionZ);
        if (newMovie > 0.01D) {
            double nextMovie = 0.06D;
            motionX += motionX / newMovie * nextMovie;
            motionZ += motionZ / newMovie * nextMovie;
        } else if (block.getOrientation() == Orientation.STRAIGHT_NORTH_SOUTH) {
            if (level.getBlock(new Vector3(dx - 1, dy, dz)).isNormalBlock()) {
                motionX = 0.02D;
            } else if (level.getBlock(new Vector3(dx + 1, dy, dz)).isNormalBlock()) {
                motionX = -0.02D;
            }
        } else if (block.getOrientation() == Orientation.STRAIGHT_EAST_WEST) {
            if (level.getBlock(new Vector3(dx, dy, dz - 1)).isNormalBlock()) {
                motionZ = 0.02D;
            } else if (level.getBlock(new Vector3(dx, dy, dz + 1)).isNormalBlock()) {
                motionZ = -0.02D;
            }
        }
    }
}
Also used : EntityLiving(cn.nukkit.entity.EntityLiving) BlockRailPowered(cn.nukkit.block.BlockRailPowered) Vector3(cn.nukkit.math.Vector3)

Example 49 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class EntityMinecartAbstract method getNextRail.

private Vector3 getNextRail(double dx, double dy, double dz) {
    int checkX = MathHelper.floor(dx);
    int checkY = MathHelper.floor(dy);
    int checkZ = MathHelper.floor(dz);
    if (Rail.isRailBlock(level.getBlockIdAt(checkX, checkY - 1, checkZ))) {
        --checkY;
    }
    Block block = level.getBlock(new Vector3(checkX, checkY, checkZ));
    if (Rail.isRailBlock(block)) {
        int[][] facing = matrix[((BlockRail) block).getRealMeta()];
        double rail;
        // Genisys mistake (Doesn't check surrounding more exactly)
        double nextOne = (double) checkX + 0.5D + (double) facing[0][0] * 0.5D;
        double nextTwo = (double) checkY + 0.5D + (double) facing[0][1] * 0.5D;
        double nextThree = (double) checkZ + 0.5D + (double) facing[0][2] * 0.5D;
        double nextFour = (double) checkX + 0.5D + (double) facing[1][0] * 0.5D;
        double nextFive = (double) checkY + 0.5D + (double) facing[1][1] * 0.5D;
        double nextSix = (double) checkZ + 0.5D + (double) facing[1][2] * 0.5D;
        double nextSeven = nextFour - nextOne;
        double nextEight = (nextFive - nextTwo) * 2;
        double nextMax = nextSix - nextThree;
        if (nextSeven == 0) {
            rail = dz - (double) checkZ;
        } else if (nextMax == 0) {
            rail = dx - (double) checkX;
        } else {
            double whatOne = dx - nextOne;
            double whatTwo = dz - nextThree;
            rail = (whatOne * nextSeven + whatTwo * nextMax) * 2;
        }
        dx = nextOne + nextSeven * rail;
        dy = nextTwo + nextEight * rail;
        dz = nextThree + nextMax * rail;
        if (nextEight < 0) {
            ++dy;
        }
        if (nextEight > 0) {
            dy += 0.5D;
        }
        return new Vector3(dx, dy, dz);
    } else {
        return null;
    }
}
Also used : Block(cn.nukkit.block.Block) Vector3(cn.nukkit.math.Vector3)

Example 50 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class EntityProjectile method onUpdate.

@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }
    int tickDiff = currentTick - this.lastUpdate;
    if (tickDiff <= 0 && !this.justCreated) {
        return true;
    }
    this.lastUpdate = currentTick;
    boolean hasUpdate = this.entityBaseTick(tickDiff);
    if (this.isAlive()) {
        MovingObjectPosition movingObjectPosition = null;
        if (!this.isCollided) {
            this.motionY -= this.getGravity();
        }
        Vector3 moveVector = new Vector3(this.x + this.motionX, this.y + this.motionY, this.z + this.motionZ);
        Entity[] list = this.getLevel().getCollidingEntities(this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1, 1, 1), this);
        double nearDistance = Integer.MAX_VALUE;
        Entity nearEntity = null;
        for (Entity entity : list) {
            if (/*!entity.canCollideWith(this) or */
            (entity == this.shootingEntity && this.ticksLived < 5)) {
                continue;
            }
            AxisAlignedBB axisalignedbb = entity.boundingBox.grow(0.3, 0.3, 0.3);
            MovingObjectPosition ob = axisalignedbb.calculateIntercept(this, moveVector);
            if (ob == null) {
                continue;
            }
            double distance = this.distanceSquared(ob.hitVector);
            if (distance < nearDistance) {
                nearDistance = distance;
                nearEntity = entity;
            }
        }
        if (nearEntity != null) {
            movingObjectPosition = MovingObjectPosition.fromEntity(nearEntity);
        }
        if (movingObjectPosition != null) {
            if (movingObjectPosition.entityHit != null) {
                onCollideWithEntity(movingObjectPosition.entityHit);
                return true;
            }
        }
        this.move(this.motionX, this.motionY, this.motionZ);
        if (this.isCollided && !this.hadCollision) {
            // collide with block
            this.hadCollision = true;
            this.motionX = 0;
            this.motionY = 0;
            this.motionZ = 0;
            this.server.getPluginManager().callEvent(new ProjectileHitEvent(this, MovingObjectPosition.fromBlock(this.getFloorX(), this.getFloorY(), this.getFloorZ(), -1, this)));
            return false;
        } else if (!this.isCollided && this.hadCollision) {
            this.hadCollision = false;
        }
        if (!this.hadCollision || Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionY) > 0.00001 || Math.abs(this.motionZ) > 0.00001) {
            double f = Math.sqrt((this.motionX * this.motionX) + (this.motionZ * this.motionZ));
            this.yaw = Math.atan2(this.motionX, this.motionZ) * 180 / Math.PI;
            this.pitch = Math.atan2(this.motionY, f) * 180 / Math.PI;
            hasUpdate = true;
        }
        this.updateMovement();
    }
    return hasUpdate;
}
Also used : AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) Entity(cn.nukkit.entity.Entity) MovingObjectPosition(cn.nukkit.level.MovingObjectPosition) Vector3(cn.nukkit.math.Vector3)

Aggregations

Vector3 (cn.nukkit.math.Vector3)62 BlockFace (cn.nukkit.math.BlockFace)16 Block (cn.nukkit.block.Block)5 Player (cn.nukkit.Player)3 Entity (cn.nukkit.entity.Entity)3 BlockIgniteEvent (cn.nukkit.event.block.BlockIgniteEvent)3 Random (java.util.Random)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 EntityLiving (cn.nukkit.entity.EntityLiving)2 BlockRedstoneEvent (cn.nukkit.event.block.BlockRedstoneEvent)2 BlockSpreadEvent (cn.nukkit.event.block.BlockSpreadEvent)2 VehicleMoveEvent (cn.nukkit.event.vehicle.VehicleMoveEvent)2 VehicleUpdateEvent (cn.nukkit.event.vehicle.VehicleUpdateEvent)2 ItemBlock (cn.nukkit.item.ItemBlock)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Level (cn.nukkit.level.Level)2 Location (cn.nukkit.level.Location)2 AxisAlignedBB (cn.nukkit.math.AxisAlignedBB)2 NukkitRandom (cn.nukkit.math.NukkitRandom)2 Rail (cn.nukkit.utils.Rail)2