cn.nukkit.math.MathHelper Java Examples

The following examples show how to use cn.nukkit.math.MathHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void moveFlying(float strafe, float forward, float friction) {
    float speed = strafe * strafe + forward * forward;
    if (speed >= 1.0E-4F) {
        speed = MathHelper.sqrt(speed);
        if (speed < 1.0F) {
            speed = 1.0F;
        }
        speed = friction / speed;
        strafe *= speed;
        forward *= speed;
        float nest = MathHelper.sin((float) (this.yaw * 3.1415927F / 180.0F));
        float place = MathHelper.cos((float) (this.yaw * 3.1415927F / 180.0F));
        this.motionX += strafe * place - forward * nest;
        this.motionZ += forward * place + strafe * nest;
    }
}
 
Example #2
Source File: BlockGlowstone.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    Random random = new Random();
    int count = 2 + random.nextInt(3);

    Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
    if (fortune != null && fortune.getLevel() >= 1) {
        count += random.nextInt(fortune.getLevel() + 1);
    }

    return new Item[]{
            new ItemGlowstoneDust(0, MathHelper.clamp(count, 1, 4))
    };
}
 
Example #3
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void applyEntityCollision(Entity entity) {
    if (entity.riding != this && entity.linkedEntity != this) {
        double dx = entity.x - this.x;
        double dy = entity.z - this.z;
        double dz = NukkitMath.getDirection(dx, dy);

        if (dz >= 0.009999999776482582D) {
            dz = MathHelper.sqrt((float) dz);
            dx /= dz;
            dy /= dz;
            double d3 = 1.0D / dz;

            if (d3 > 1.0D) {
                d3 = 1.0D;
            }

            dx *= d3;
            dy *= d3;
            dx *= 0.05000000074505806D;
            dy *= 0.05000000074505806D;
            dx *= 1.0F + entityCollisionReduction;
            dz *= 1.0F + entityCollisionReduction;
            if (this.riding == null) {
                motionX -= dx;
                motionZ -= dy;
            }
        }
    }
}
 
Example #4
Source File: Level.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public int calculateSkylightSubtracted(float tickDiff) {
    float angle = this.calculateCelestialAngle(getTime(), tickDiff);
    float light = 1 - (MathHelper.cos(angle * ((float) Math.PI * 2F)) * 2 + 0.5f);
    light = light < 0 ? 0 : light > 1 ? 1 : light;
    light = 1 - light;
    light = (float) ((double) light * ((isRaining() ? 1 : 0) - (double) 5f / 16d));
    light = (float) ((double) light * ((isThundering() ? 1 : 0) - (double) 5f / 16d));
    light = 1 - light;
    return (int) (light * 11f);
}
 
Example #5
Source File: NoiseGeneratorOctavesD.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public double[] generateNoiseOctaves(double[] noiseArray, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, double xScale, double yScale, double zScale) {
    if (noiseArray == null) {
        noiseArray = new double[xSize * ySize * zSize];
    } else {
        for (int i = 0; i < noiseArray.length; ++i) {
            noiseArray[i] = 0.0D;
        }
    }

    double d3 = 1.0D;

    for (int j = 0; j < this.octaves; ++j) {
        double d0 = (double) xOffset * d3 * xScale;
        double d1 = (double) yOffset * d3 * yScale;
        double d2 = (double) zOffset * d3 * zScale;
        long k = MathHelper.floor_double_long(d0);
        long l = MathHelper.floor_double_long(d2);
        d0 = d0 - (double) k;
        d2 = d2 - (double) l;
        k = k % 16777216L;
        l = l % 16777216L;
        d0 = d0 + (double) k;
        d2 = d2 + (double) l;
        this.generatorCollection[j].populateNoiseArray(noiseArray, d0, d1, d2, xSize, ySize, zSize, xScale * d3, yScale * d3, zScale * d3, d3);
        d3 /= 2.0D;
    }

    return noiseArray;
}
 
Example #6
Source File: NoiseGeneratorOctavesF.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public float[] generateNoiseOctaves(float[] noiseArray, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, float xScale, float yScale, float zScale) {
    if (noiseArray == null) {
        noiseArray = new float[xSize * ySize * zSize];
    } else {
        for (int i = 0; i < noiseArray.length; ++i) {
            noiseArray[i] = 0.0f;
        }
    }

    float d3 = 1.0f;

    for (int j = 0; j < this.octaves; ++j) {
        float d0 = (float) xOffset * d3 * xScale;
        float d1 = (float) yOffset * d3 * yScale;
        float d2 = (float) zOffset * d3 * zScale;
        int k = MathHelper.floor_float_int(d0);
        int l = MathHelper.floor_float_int(d2);
        d0 = d0 - (float) k;
        d2 = d2 - (float) l;
        k = k % 16777216;
        l = l % 16777216;
        d0 = d0 + (float) k;
        d2 = d2 + (float) l;
        this.generatorCollection[j].populateNoiseArray(noiseArray, d0, d1, d2, xSize, ySize, zSize, xScale * d3, yScale * d3, zScale * d3, d3);
        d3 /= 2.0D;
    }

    return noiseArray;
}
 
Example #7
Source File: BlockGlowstone.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    Random random = new Random();
    int count = 2 + random.nextInt(3);

    Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
    if (fortune != null && fortune.getLevel() >= 1) {
        count += random.nextInt(fortune.getLevel() + 1);
    }

    return new Item[]{
            new ItemGlowstoneDust(0, MathHelper.clamp(count, 1, 4))
    };
}
 
Example #8
Source File: Pow2BitArray.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
Pow2BitArray(BitArrayVersion version, int size, int[] words) {
    this.size = size;
    this.version = version;
    this.words = words;
    int expectedWordsLength = MathHelper.ceil((float) size / version.entriesPerWord);
    if (words.length != expectedWordsLength) {
        throw new IllegalArgumentException("Invalid length given for storage, got: " + words.length +
                " but expected: " + expectedWordsLength);
    }
}
 
Example #9
Source File: PaddedBitArray.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
PaddedBitArray(BitArrayVersion version, int size, int[] words) {
    this.size = size;
    this.version = version;
    this.words = words;
    int expectedWordsLength = MathHelper.ceil((float) size / version.entriesPerWord);
    if (words.length != expectedWordsLength) {
        throw new IllegalArgumentException("Invalid length given for storage, got: " + words.length +
                " but expected: " + expectedWordsLength);
    }
}
 
Example #10
Source File: OreType.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean spawn(ChunkManager level, NukkitRandom rand, int replaceId, int x, int y, int z) {
    float piScaled = rand.nextFloat() * (float) Math.PI;
    double scaleMaxX = (double) ((float) (x + 8) + MathHelper.sin(piScaled) * (float) clusterSize / 8.0F);
    double scaleMinX = (double) ((float) (x + 8) - MathHelper.sin(piScaled) * (float) clusterSize / 8.0F);
    double scaleMaxZ = (double) ((float) (z + 8) + MathHelper.cos(piScaled) * (float) clusterSize / 8.0F);
    double scaleMinZ = (double) ((float) (z + 8) - MathHelper.cos(piScaled) * (float) clusterSize / 8.0F);
    double scaleMaxY = (double) (y + rand.nextBoundedInt(3) - 2);
    double scaleMinY = (double) (y + rand.nextBoundedInt(3) - 2);

    for (int i = 0; i < clusterSize; ++i) {
        float sizeIncr = (float) i / (float) clusterSize;
        double scaleX = scaleMaxX + (scaleMinX - scaleMaxX) * (double) sizeIncr;
        double scaleY = scaleMaxY + (scaleMinY - scaleMaxY) * (double) sizeIncr;
        double scaleZ = scaleMaxZ + (scaleMinZ - scaleMaxZ) * (double) sizeIncr;
        double randSizeOffset = rand.nextDouble() * (double) clusterSize / 16.0D;
        double randVec1 = (double) (MathHelper.sin((float) Math.PI * sizeIncr) + 1.0F) * randSizeOffset + 1.0D;
        double randVec2 = (double) (MathHelper.sin((float) Math.PI * sizeIncr) + 1.0F) * randSizeOffset + 1.0D;
        int minX = MathHelper.floor(scaleX - randVec1 / 2.0D);
        int minY = MathHelper.floor(scaleY - randVec2 / 2.0D);
        int minZ = MathHelper.floor(scaleZ - randVec1 / 2.0D);
        int maxX = MathHelper.floor(scaleX + randVec1 / 2.0D);
        int maxY = MathHelper.floor(scaleY + randVec2 / 2.0D);
        int maxZ = MathHelper.floor(scaleZ + randVec1 / 2.0D);

        for (int xSeg = minX; xSeg <= maxX; ++xSeg) {
            double xVal = ((double) xSeg + 0.5D - scaleX) / (randVec1 / 2.0D);

            if (xVal * xVal < 1.0D) {
                for (int ySeg = minY; ySeg <= maxY; ++ySeg) {
                    double yVal = ((double) ySeg + 0.5D - scaleY) / (randVec2 / 2.0D);

                    if (xVal * xVal + yVal * yVal < 1.0D) {
                        for (int zSeg = minZ; zSeg <= maxZ; ++zSeg) {
                            double zVal = ((double) zSeg + 0.5D - scaleZ) / (randVec1 / 2.0D);

                            if (xVal * xVal + yVal * yVal + zVal * zVal < 1.0D) {
                                if (level.getBlockIdAt(xSeg, ySeg, zSeg) == replaceBlockId) {
                                    level.setBlockFullIdAt(xSeg, ySeg, zSeg, fullId);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    return true;
}
 
Example #11
Source File: DataPalette.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean compress() {
    char[] raw = rawData;
    if (raw != null) {
        synchronized (this) {
            char unique = 0;

            boolean[] countTable = ThreadCache.boolCache4096.get();
            char[] mapFullTable = ThreadCache.charCache4096.get();
            char[] mapBitTable = ThreadCache.charCache4096v2.get();
            Arrays.fill(countTable, false);
            for (char c : raw) {
                if (!countTable[c]) {
                    mapBitTable[unique] = c;
                    countTable[c] = true;
                    unique++;
                }
            }

            char[] keys = Arrays.copyOfRange(mapBitTable, 0, unique);
            if (keys.length > 1) {
                Arrays.sort(keys);
                for (char c = 0; c < keys.length; c++) {
                    mapFullTable[keys[c]] = c;
                }
            } else {
                mapFullTable[keys[0]] = 0;
            }

            CharPalette palette = new CharPalette();
            palette.set(keys);

            int bits = MathHelper.log2nlz(unique) + 1;
            BitArray encodedData = new BitArray(bits);

            for (int i = 0; i < raw.length; i++) {
                raw[i] = mapFullTable[raw[i]];
            }

            encodedData.fromRaw(raw);

            this.palette = palette;
            this.encodedData = encodedData;
            rawData = null;
            return true;
        }
    }
    return false;
}
 
Example #12
Source File: ChunkPosition.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public ChunkPosition(Vector3 vec3d) {
    this(MathHelper.floor(vec3d.x), MathHelper.floor(vec3d.y), MathHelper.floor(vec3d.z));
}
 
Example #13
Source File: ObjectJungleBigTree.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean generate(ChunkManager level, NukkitRandom rand, Vector3 position) {
    int height = this.getHeight(rand);

    if (!this.ensureGrowable(level, rand, position, height)) {
        return false;
    } else {
        this.createCrown(level, position.up(height), 2);

        for (int j = (int) position.getY() + height - 2 - rand.nextBoundedInt(4); j > position.getY() + height / 2; j -= 2 + rand.nextBoundedInt(4)) {
            float f = rand.nextFloat() * ((float) Math.PI * 2F);
            int k = (int) (position.getX() + (0.5F + MathHelper.cos(f) * 4.0F));
            int l = (int) (position.getZ() + (0.5F + MathHelper.sin(f) * 4.0F));

            for (int i1 = 0; i1 < 5; ++i1) {
                k = (int) (position.getX() + (1.5F + MathHelper.cos(f) * (float) i1));
                l = (int) (position.getZ() + (1.5F + MathHelper.sin(f) * (float) i1));
                this.setBlockAndNotifyAdequately(level, new Vector3(k, j - 3 + i1 / 2, l), this.woodMetadata);
            }

            int j2 = 1 + rand.nextBoundedInt(2);
            int j1 = j;

            for (int k1 = j - j2; k1 <= j1; ++k1) {
                int l1 = k1 - j1;
                this.growLeavesLayer(level, new Vector3(k, k1, l), 1 - l1);
            }
        }

        for (int i2 = 0; i2 < height; ++i2) {
            Vector3 blockpos = position.up(i2);

            if (this.canGrowInto(level.getBlockIdAt((int) blockpos.x, (int) blockpos.y, (int) blockpos.z))) {
                this.setBlockAndNotifyAdequately(level, blockpos, this.woodMetadata);

                if (i2 > 0) {
                    this.placeVine(level, rand, blockpos.west(), 8);
                    this.placeVine(level, rand, blockpos.north(), 1);
                }
            }

            if (i2 < height - 1) {
                Vector3 blockpos1 = blockpos.east();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos1.x, (int) blockpos1.y, (int) blockpos1.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos1, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos1.east(), 2);
                        this.placeVine(level, rand, blockpos1.north(), 1);
                    }
                }

                Vector3 blockpos2 = blockpos.south().east();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos2.x, (int) blockpos2.y, (int) blockpos2.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos2, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos2.east(), 2);
                        this.placeVine(level, rand, blockpos2.south(), 4);
                    }
                }

                Vector3 blockpos3 = blockpos.south();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos3.x, (int) blockpos3.y, (int) blockpos3.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos3, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos3.west(), 8);
                        this.placeVine(level, rand, blockpos3.south(), 4);
                    }
                }
            }
        }

        return true;
    }
}
 
Example #14
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
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;
    }
}
 
Example #15
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void applyEntityCollision(Entity entity) {
    if (entity != riding) {
        if (entity instanceof EntityLiving
                && !(entity instanceof EntityHuman)
                && motionX * motionX + motionZ * motionZ > 0.01D
                && linkedEntity == null
                && entity.riding == null
                && blockInside == null) {
            if (riding == null && devs) {
                mountEntity(entity);// TODO: rewrite (weird riding)
            }
        }

        double motiveX = entity.x - x;
        double motiveZ = entity.z - z;
        double square = motiveX * motiveX + motiveZ * motiveZ;

        if (square >= 9.999999747378752E-5D) {
            square = Math.sqrt(square);
            motiveX /= square;
            motiveZ /= square;
            double next = 1 / square;

            if (next > 1) {
                next = 1;
            }

            motiveX *= next;
            motiveZ *= next;
            motiveX *= 0.10000000149011612D;
            motiveZ *= 0.10000000149011612D;
            motiveX *= 1 + entityCollisionReduction;
            motiveZ *= 1 + entityCollisionReduction;
            motiveX *= 0.5D;
            motiveZ *= 0.5D;
            if (entity instanceof EntityMinecartAbstract) {
                EntityMinecartAbstract mine = (EntityMinecartAbstract) entity;
                double desinityX = mine.x - x;
                double desinityZ = mine.z - z;
                Vector3 vector = new Vector3(desinityX, 0, desinityZ).normalize();
                Vector3 vec = new Vector3((double) MathHelper.cos((float) yaw * 0.017453292F), 0, (double) MathHelper.sin((float) yaw * 0.017453292F)).normalize();
                double desinityXZ = Math.abs(vector.dot(vec));

                if (desinityXZ < 0.800000011920929D) {
                    return;
                }

                double motX = mine.motionX + motionX;
                double motZ = mine.motionZ + motionZ;

                if (mine.getType().getId() == 2 && getType().getId() != 2) {
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX - motiveX;
                    motionZ += mine.motionZ - motiveZ;
                    mine.motionX *= 0.949999988079071D;
                    mine.motionZ *= 0.949999988079071D;
                } else if (mine.getType().getId() != 2 && getType().getId() == 2) {
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX + motiveX;
                    motionZ += mine.motionZ + motiveZ;
                    motionX *= 0.949999988079071D;
                    motionZ *= 0.949999988079071D;
                } else {
                    motX /= 2;
                    motZ /= 2;
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += motX - motiveX;
                    motionZ += motZ - motiveZ;
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    mine.motionX += motX + motiveX;
                    mine.motionZ += motZ + motiveZ;
                }
            } else {
                motionX -= motiveX;
                motionZ -= motiveZ;
            }                
        }
    }
}
 
Example #16
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }

    if (!this.isAlive()) {
        ++this.deadTicks;
        if (this.deadTicks >= 10) {
            this.despawnFromAll();
            this.close();
        }
        return this.deadTicks < 10;
    }

    int tickDiff = currentTick - this.lastUpdate;

    if (tickDiff <= 0) {
        return false;
    }

    this.lastUpdate = currentTick;

    if (isAlive()) {
        super.onUpdate(currentTick);

        // Entity variables
        lastX = x;
        lastY = y;
        lastZ = z;
        motionY -= 0.03999999910593033D;
        int dx = MathHelper.floor(x);
        int dy = MathHelper.floor(y);
        int dz = MathHelper.floor(z);

        // Some hack to check rails
        if (Rail.isRailBlock(level.getBlockIdAt(dx, dy - 1, dz))) {
            --dy;
        }

        Block block = level.getBlock(new Vector3(dx, dy, dz));

        // Ensure that the block is a rail
        if (Rail.isRailBlock(block)) {
            processMovement(dx, dy, dz, (BlockRail) block);
            if (block instanceof BlockRailActivator) {
                // Activate the minecart/TNT
                activate(dx, dy, dz, (block.getDamage() & 0x8) != 0);
            }
        } else {
            setFalling();
        }
        checkBlockCollision();

        // Minecart head
        pitch = 0;
        double diffX = this.lastX - this.x;
        double diffZ = this.lastZ - this.z;
        double yawToChange = yaw;
        if (diffX * diffX + diffZ * diffZ > 0.001D) {
            yawToChange = (Math.atan2(diffZ, diffX) * 180 / 3.141592653589793D);
        }

        // Reverse yaw if yaw is below 0
        if (yawToChange < 0) {
            // -90-(-90)-(-90) = 90
            yawToChange -= yawToChange - yawToChange;
        }

        setRotation(yawToChange, pitch);

        Location from = new Location(lastX, lastY, lastZ, lastYaw, lastPitch, level);
        Location to = new Location(this.x, this.y, this.z, this.yaw, this.pitch, level);

        this.getServer().getPluginManager().callEvent(new VehicleUpdateEvent(this));

        if (!from.equals(to)) {
            this.getServer().getPluginManager().callEvent(new VehicleMoveEvent(this, from, to));
        }

        // Collisions
        for (Entity entity : level.getNearbyEntities(boundingBox.grow(0.2D, 0, 0.2D), this)) {
            if (entity != linkedEntity && entity instanceof EntityMinecartAbstract) {
                entity.applyEntityCollision(this);
            }
        }

        // Easier
        if ((linkedEntity != null) && (!linkedEntity.isAlive())) {
            if (linkedEntity.riding == this) {
                linkedEntity.riding = null;
            }
            linkedEntity = null;
        }
        // No need to onGround or Motion diff! This always have an update
        return true;
    }
    return false;
}
 
Example #17
Source File: BlockDataPalette.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public synchronized boolean compress() {
    char[] raw = rawData;
    if (raw != null) {
        char unique = 0;

        boolean[] countTable = ThreadCache.boolCache4096.get();
        char[] mapFullTable = ThreadCache.charCache4096.get();
        char[] mapBitTable = ThreadCache.charCache4096v2.get();
        Arrays.fill(countTable, false);
        for (char c : raw) {
            if (!countTable[c]) {
                mapBitTable[unique] = c;
                countTable[c] = true;
                unique++;
            }
        }

        char[] keys = Arrays.copyOfRange(mapBitTable, 0, unique);
        if (keys.length > 1) {
            Arrays.sort(keys);
            for (char c = 0; c < keys.length; c++) {
                mapFullTable[keys[c]] = c;
            }
        } else {
            mapFullTable[keys[0]] = 0;
        }

        CharPalette palette = new CharPalette();
        palette.set(keys);

        int bits = MathHelper.log2(unique - 1);
        BitArray4096 encodedData = new BitArray4096(bits);

        for (int i = 0; i < raw.length; i++) {
            mapBitTable[i] = mapFullTable[raw[i]];
        }

        encodedData.fromRaw(mapBitTable);

        this.palette = palette;
        this.encodedData = encodedData;
        rawData = null;
        return true;
    }
    return false;
}
 
Example #18
Source File: BiomePalette.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public synchronized void set(int index, int value) {
    if (encodedData == null) {
        if (value == biome) return;
        if (biome == Integer.MIN_VALUE) {
            biome = value;
            return;
        }
        synchronized (this) {
            palette = new IntPalette();
            palette.add(biome);
            palette.add(value);
            encodedData = new BitArray256(1);
            if (value < biome) {
                Arrays.fill(encodedData.data, -1);
                encodedData.setAt(index, 0);
            } else {
                encodedData.setAt(index, 1);
            }
            return;
        }
    }

    int encodedValue = palette.getValue(value);
    if (encodedValue != Integer.MIN_VALUE) {
        encodedData.setAt(index, encodedValue);
    } else {
        synchronized (this) {
            int[] raw = encodedData.toRaw(ThreadCache.intCache256.get());

            // TODO skip remapping of raw data and use grow instead if `remap`
            // boolean remap = value < palette.getValue(palette.length() - 1);

            for (int i = 0; i < 256; i++) {
                raw[i] = palette.getKey(raw[i]);
            }

            int oldRaw = raw[4];

            raw[index] = value;

            palette.add(value);

            int oldBits = MathHelper.log2(palette.length() - 2);
            int newBits = MathHelper.log2(palette.length() - 1);
            if (oldBits != newBits) {
                encodedData = new BitArray256(newBits);
            }

            for (int i = 0; i < raw.length; i++) {
                raw[i] = palette.getValue(raw[i]);
            }

            encodedData.fromRaw(raw);
        }
    }

}
 
Example #19
Source File: ChunkPosition.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public ChunkPosition(Vector3 vec3d) {
    this(MathHelper.floor(vec3d.x), MathHelper.floor(vec3d.y), MathHelper.floor(vec3d.z));
}
 
Example #20
Source File: EntityMinecartAbstract.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }

    if (!this.isAlive()) {
        ++this.deadTicks;
        if (this.deadTicks >= 10) {
            this.despawnFromAll();
            this.close();
        }
        return this.deadTicks < 10;
    }

    int tickDiff = currentTick - this.lastUpdate;

    if (tickDiff <= 0) {
        return false;
    }

    this.lastUpdate = currentTick;

    if (isAlive()) {
        super.onUpdate(currentTick);

        // Entity variables
        lastX = x;
        lastY = y;
        lastZ = z;
        motionY -= 0.03999999910593033D;
        int dx = MathHelper.floor(x);
        int dy = MathHelper.floor(y);
        int dz = MathHelper.floor(z);

        // Some hack to check rails
        if (Rail.isRailBlock(level.getBlockIdAt(dx, dy - 1, dz))) {
            --dy;
        }

        Block block = level.getBlock(new Vector3(dx, dy, dz));

        // Ensure that the block is a rail
        if (Rail.isRailBlock(block)) {
            processMovement(dx, dy, dz, (BlockRail) block);
            if (block instanceof BlockRailActivator) {
                // Activate the minecart/TNT
                activate(dx, dy, dz, (block.getDamage() & 0x8) != 0);
            }
        } else {
            setFalling();
        }
        checkBlockCollision();

        // Minecart head
        pitch = 0;
        double diffX = this.lastX - this.x;
        double diffZ = this.lastZ - this.z;
        double yawToChange = yaw;
        if (diffX * diffX + diffZ * diffZ > 0.001D) {
            yawToChange = (Math.atan2(diffZ, diffX) * 180 / 3.141592653589793D);
        }
        
        // Reverse yaw if yaw is below 0
        if (yawToChange < 0) {
            // -90-(-90)-(-90) = 90
            yawToChange -= yawToChange - yawToChange;
        }
        
        setRotation(yawToChange, pitch);

        Location from = new Location(lastX, lastY, lastZ, lastYaw, lastPitch, level);
        Location to = new Location(this.x, this.y, this.z, this.yaw, this.pitch, level);

        this.getServer().getPluginManager().callEvent(new VehicleUpdateEvent(this));

        if (!from.equals(to)) {
            this.getServer().getPluginManager().callEvent(new VehicleMoveEvent(this, from, to));
        }

        // Collisions
        for (Entity entity : level.getNearbyEntities(boundingBox.grow(0.2D, 0, 0.2D), this)) {
            if (entity != linkedEntity && entity instanceof EntityMinecartAbstract) {
                entity.applyEntityCollision(this);
            }
        }

        // Easier
        if ((linkedEntity != null) && (!linkedEntity.isAlive())) {
            if (linkedEntity.riding == this) {
                linkedEntity.riding = null;
            }
            linkedEntity = null;
        }
        // No need to onGround or Motion diff! This always have an update
        return true;
    }
    return false;
}
 
Example #21
Source File: ObjectJungleBigTree.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean generate(ChunkManager level, NukkitRandom rand, Vector3 position) {
    int height = this.getHeight(rand);

    if (!this.ensureGrowable(level, rand, position, height)) {
        return false;
    } else {
        this.createCrown(level, position.up(height), 2);

        for (int j = (int) position.getY() + height - 2 - rand.nextBoundedInt(4); j > position.getY() + height / 2; j -= 2 + rand.nextBoundedInt(4)) {
            float f = rand.nextFloat() * ((float) Math.PI * 2F);
            int k = (int) (position.getX() + (0.5F + MathHelper.cos(f) * 4.0F));
            int l = (int) (position.getZ() + (0.5F + MathHelper.sin(f) * 4.0F));

            for (int i1 = 0; i1 < 5; ++i1) {
                k = (int) (position.getX() + (1.5F + MathHelper.cos(f) * (float) i1));
                l = (int) (position.getZ() + (1.5F + MathHelper.sin(f) * (float) i1));
                this.setBlockAndNotifyAdequately(level, new BlockVector3(k, j - 3 + i1 / 2, l), this.woodMetadata);
            }

            int j2 = 1 + rand.nextBoundedInt(2);

            for (int k1 = j - j2; k1 <= j; ++k1) {
                int l1 = k1 - j;
                this.growLeavesLayer(level, new Vector3(k, k1, l), 1 - l1);
            }
        }

        for (int i2 = 0; i2 < height; ++i2) {
            Vector3 blockpos = position.up(i2);

            if (this.canGrowInto(level.getBlockIdAt((int) blockpos.x, (int) blockpos.y, (int) blockpos.z))) {
                this.setBlockAndNotifyAdequately(level, blockpos, this.woodMetadata);

                if (i2 > 0) {
                    this.placeVine(level, rand, blockpos.west(), 8);
                    this.placeVine(level, rand, blockpos.north(), 1);
                }
            }

            if (i2 < height - 1) {
                Vector3 blockpos1 = blockpos.east();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos1.x, (int) blockpos1.y, (int) blockpos1.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos1, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos1.east(), 2);
                        this.placeVine(level, rand, blockpos1.north(), 1);
                    }
                }

                Vector3 blockpos2 = blockpos.south().east();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos2.x, (int) blockpos2.y, (int) blockpos2.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos2, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos2.east(), 2);
                        this.placeVine(level, rand, blockpos2.south(), 4);
                    }
                }

                Vector3 blockpos3 = blockpos.south();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos3.x, (int) blockpos3.y, (int) blockpos3.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos3, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos3.west(), 8);
                        this.placeVine(level, rand, blockpos3.south(), 4);
                    }
                }
            }
        }

        return true;
    }
}
 
Example #22
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
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;
    }
}
 
Example #23
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void applyEntityCollision(cn.nukkit.entity.Entity entity) {
    if (entity != riding && !(entity instanceof Player && ((Player) entity).getGamemode() == Player.SPECTATOR)) {
        if (entity instanceof EntityLiving
                && !(entity instanceof EntityHuman)
                && motionX * motionX + motionZ * motionZ > 0.01D
                && passengers.isEmpty()
                && entity.riding == null
                && blockInside == null) {
            if (riding == null && devs) {
                mountEntity(entity);// TODO: rewrite (weird riding)
            }
        }

        double motiveX = entity.x - x;
        double motiveZ = entity.z - z;
        double square = motiveX * motiveX + motiveZ * motiveZ;

        if (square >= 9.999999747378752E-5D) {
            square = Math.sqrt(square);
            motiveX /= square;
            motiveZ /= square;
            double next = 1 / square;

            if (next > 1) {
                next = 1;
            }

            motiveX *= next;
            motiveZ *= next;
            motiveX *= 0.10000000149011612D;
            motiveZ *= 0.10000000149011612D;
            motiveX *= 1 + entityCollisionReduction;
            motiveZ *= 1 + entityCollisionReduction;
            motiveX *= 0.5D;
            motiveZ *= 0.5D;
            if (entity instanceof EntityMinecartAbstract) {
                EntityMinecartAbstract mine = (EntityMinecartAbstract) entity;
                double desinityX = mine.x - x;
                double desinityZ = mine.z - z;
                Vector3 vector = new Vector3(desinityX, 0, desinityZ).normalize();
                Vector3 vec = new Vector3((double) MathHelper.cos((float) yaw * 0.017453292F), 0, (double) MathHelper.sin((float) yaw * 0.017453292F)).normalize();
                double desinityXZ = Math.abs(vector.dot(vec));

                if (desinityXZ < 0.800000011920929D) {
                    return;
                }

                double motX = mine.motionX + motionX;
                double motZ = mine.motionZ + motionZ;

                if (mine.getType().getId() == 2 && getType().getId() != 2) {
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX - motiveX;
                    motionZ += mine.motionZ - motiveZ;
                    mine.motionX *= 0.949999988079071D;
                    mine.motionZ *= 0.949999988079071D;
                } else if (mine.getType().getId() != 2 && getType().getId() == 2) {
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX + motiveX;
                    motionZ += mine.motionZ + motiveZ;
                    motionX *= 0.949999988079071D;
                    motionZ *= 0.949999988079071D;
                } else {
                    motX /= 2;
                    motZ /= 2;
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += motX - motiveX;
                    motionZ += motZ - motiveZ;
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    mine.motionX += motX + motiveX;
                    mine.motionZ += motZ + motiveZ;
                }
            } else {
                motionX -= motiveX;
                motionZ -= motiveZ;
            }
        }
    }
}
 
Example #24
Source File: ChunkPosition.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public ChunkPosition(Vector3 vec3d) {
    this(MathHelper.floor(vec3d.x), MathHelper.floor(vec3d.y), MathHelper.floor(vec3d.z));
}
 
Example #25
Source File: ObjectJungleBigTree.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public boolean generate(ChunkManager level, NukkitRandom rand, Vector3 position) {
    int height = this.getHeight(rand);

    if (!this.ensureGrowable(level, rand, position, height)) {
        return false;
    } else {
        this.createCrown(level, position.up(height), 2);

        for (int j = (int) position.getY() + height - 2 - rand.nextBoundedInt(4); j > position.getY() + height / 2; j -= 2 + rand.nextBoundedInt(4)) {
            float f = rand.nextFloat() * ((float) Math.PI * 2F);
            int k = (int) (position.getX() + (0.5F + MathHelper.cos(f) * 4.0F));
            int l = (int) (position.getZ() + (0.5F + MathHelper.sin(f) * 4.0F));

            for (int i1 = 0; i1 < 5; ++i1) {
                k = (int) (position.getX() + (1.5F + MathHelper.cos(f) * (float) i1));
                l = (int) (position.getZ() + (1.5F + MathHelper.sin(f) * (float) i1));
                this.setBlockAndNotifyAdequately(level, new Vector3(k, j - 3 + i1 / 2, l), this.woodMetadata);
            }

            int j2 = 1 + rand.nextBoundedInt(2);
            int j1 = j;

            for (int k1 = j - j2; k1 <= j1; ++k1) {
                int l1 = k1 - j1;
                this.growLeavesLayer(level, new Vector3(k, k1, l), 1 - l1);
            }
        }

        for (int i2 = 0; i2 < height; ++i2) {
            Vector3 blockpos = position.up(i2);

            if (this.canGrowInto(level.getBlockIdAt((int) blockpos.x, (int) blockpos.y, (int) blockpos.z))) {
                this.setBlockAndNotifyAdequately(level, blockpos, this.woodMetadata);

                if (i2 > 0) {
                    this.placeVine(level, rand, blockpos.west(), 8);
                    this.placeVine(level, rand, blockpos.north(), 1);
                }
            }

            if (i2 < height - 1) {
                Vector3 blockpos1 = blockpos.east();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos1.x, (int) blockpos1.y, (int) blockpos1.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos1, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos1.east(), 2);
                        this.placeVine(level, rand, blockpos1.north(), 1);
                    }
                }

                Vector3 blockpos2 = blockpos.south().east();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos2.x, (int) blockpos2.y, (int) blockpos2.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos2, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos2.east(), 2);
                        this.placeVine(level, rand, blockpos2.south(), 4);
                    }
                }

                Vector3 blockpos3 = blockpos.south();

                if (this.canGrowInto(level.getBlockIdAt((int) blockpos3.x, (int) blockpos3.y, (int) blockpos3.z))) {
                    this.setBlockAndNotifyAdequately(level, blockpos3, this.woodMetadata);

                    if (i2 > 0) {
                        this.placeVine(level, rand, blockpos3.west(), 8);
                        this.placeVine(level, rand, blockpos3.south(), 4);
                    }
                }
            }
        }

        return true;
    }
}
 
Example #26
Source File: EntityMinecartAbstract.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
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;
    }
}
 
Example #27
Source File: EntityMinecartAbstract.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void applyEntityCollision(Entity entity) {
    if (entity != riding) {
        if (entity instanceof EntityLiving
                && !(entity instanceof EntityHuman)
                && motionX * motionX + motionZ * motionZ > 0.01D
                && linkedEntity == null
                && entity.riding == null
                && blockInside == null) {
            if (riding == null && devs) {
                mountEntity(entity);// TODO: rewrite (weird riding)
            }
        }

        double motiveX = entity.x - x;
        double motiveZ = entity.z - z;
        double square = motiveX * motiveX + motiveZ * motiveZ;

        if (square >= 9.999999747378752E-5D) {
            square = Math.sqrt(square);
            motiveX /= square;
            motiveZ /= square;
            double next = 1 / square;

            if (next > 1) {
                next = 1;
            }

            motiveX *= next;
            motiveZ *= next;
            motiveX *= 0.10000000149011612D;
            motiveZ *= 0.10000000149011612D;
            motiveX *= 1 + entityCollisionReduction;
            motiveZ *= 1 + entityCollisionReduction;
            motiveX *= 0.5D;
            motiveZ *= 0.5D;
            if (entity instanceof EntityMinecartAbstract) {
                EntityMinecartAbstract mine = (EntityMinecartAbstract) entity;
                double desinityX = mine.x - x;
                double desinityZ = mine.z - z;
                Vector3 vector = new Vector3(desinityX, 0, desinityZ).normalize();
                Vector3 vec = new Vector3((double) MathHelper.cos((float) yaw * 0.017453292F), 0, (double) MathHelper.sin((float) yaw * 0.017453292F)).normalize();
                double desinityXZ = Math.abs(vector.dot(vec));

                if (desinityXZ < 0.800000011920929D) {
                    return;
                }

                double motX = mine.motionX + motionX;
                double motZ = mine.motionZ + motionZ;

                if (mine.getType().getId() == 2 && getType().getId() != 2) {
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX - motiveX;
                    motionZ += mine.motionZ - motiveZ;
                    mine.motionX *= 0.949999988079071D;
                    mine.motionZ *= 0.949999988079071D;
                } else if (mine.getType().getId() != 2 && getType().getId() == 2) {
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX - motiveX;
                    motionZ += mine.motionZ - motiveZ;
                    motionX *= 0.949999988079071D;
                    motionZ *= 0.949999988079071D;
                } else {
                    motX /= 2;
                    motZ /= 2;
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += motX - motiveX;
                    motionZ += motZ - motiveZ;
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    mine.motionX += motX - motiveX;
                    mine.motionZ += motZ - motiveZ;
                }
            } else {
                motionX -= motiveX;
                motionZ -= motiveZ;
            }
        }
    }
}