Search in sources :

Example 1 with MutableBlockVector2

use of com.fastasyncworldedit.core.math.MutableBlockVector2 in project FastAsyncWorldEdit by IntellectualSites.

the class EditSession method regenerate.

public boolean regenerate(Region region, BiomeType biome, Long seed) {
    // TODO Optimize - avoid Vector2D creation (make mutable)
    final AbstractChangeSet fcs = (AbstractChangeSet) this.getChangeSet();
    this.setChangeSet(null);
    final FaweRegionExtent fe = this.getRegionExtent();
    final boolean cuboid = region instanceof CuboidRegion;
    if (fe != null && cuboid) {
        BlockVector3 max = region.getMaximumPoint();
        BlockVector3 min = region.getMinimumPoint();
        if (!fe.contains(max.getBlockX(), max.getBlockY(), max.getBlockZ()) && !fe.contains(min.getBlockX(), min.getBlockY(), min.getBlockZ())) {
            throw FaweCache.OUTSIDE_REGION;
        }
    }
    final Set<BlockVector2> chunks = region.getChunks();
    MutableBlockVector3 mutable = new MutableBlockVector3();
    MutableBlockVector3 mutable2 = new MutableBlockVector3();
    MutableBlockVector2 mutable2D = new MutableBlockVector2();
    for (BlockVector2 chunk : chunks) {
        final int cx = chunk.getBlockX();
        final int cz = chunk.getBlockZ();
        final int bx = cx << 4;
        final int bz = cz << 4;
        final BlockVector3 cmin = BlockVector3.at(bx, 0, bz);
        final BlockVector3 cmax = cmin.add(15, maxY, 15);
        final boolean containsBot1 = fe == null || fe.contains(cmin.getBlockX(), cmin.getBlockY(), cmin.getBlockZ());
        final boolean containsBot2 = region.contains(cmin);
        final boolean containsTop1 = fe == null || fe.contains(cmax.getBlockX(), cmax.getBlockY(), cmax.getBlockZ());
        final boolean containsTop2 = region.contains(cmax);
        if (containsBot2 && containsTop2 && !containsBot1 && !containsTop1) {
            continue;
        }
        boolean conNextX = chunks.contains(mutable2D.setComponents(cx + 1, cz));
        boolean conNextZ = chunks.contains(mutable2D.setComponents(cx, cz + 1));
        boolean containsAny = false;
        if (cuboid && containsBot1 && containsBot2 && containsTop1 && containsTop2 && conNextX && conNextZ) {
            containsAny = true;
            if (fcs != null) {
                for (int x = 0; x < 16; x++) {
                    int xx = x + bx;
                    for (int z = 0; z < 16; z++) {
                        int zz = z + bz;
                        for (int y = minY; y < maxY + 1; y++) {
                            BaseBlock block = getFullBlock(mutable.setComponents(xx, y, zz));
                            fcs.add(mutable, block, BlockTypes.AIR.getDefaultState().toBaseBlock());
                        }
                    }
                }
            }
        } else {
            if (!conNextX) {
                setExistingBlocks(mutable.setComponents(bx + 16, 0, bz), mutable2.setComponents(bx + 31, maxY, bz + 15));
            }
            if (!conNextZ) {
                setExistingBlocks(mutable.setComponents(bx, 0, bz + 16), mutable2.setComponents(bx + 15, maxY, bz + 31));
            }
            if (!chunks.contains(mutable2D.setComponents(cx + 1, cz + 1)) && !conNextX && !conNextZ) {
                setExistingBlocks(mutable.setComponents(bx + 16, 0, bz + 16), mutable2.setComponents(bx + 31, maxY, bz + 31));
            }
            for (int x = 0; x < 16; x++) {
                int xx = x + bx;
                mutable.mutX(xx);
                for (int z = 0; z < 16; z++) {
                    int zz = z + bz;
                    mutable.mutZ(zz);
                    for (int y = minY; y < maxY + 1; y++) {
                        mutable.mutY(y);
                        boolean contains = (fe == null || fe.contains(xx, y, zz)) && region.contains(mutable);
                        if (contains) {
                            containsAny = true;
                            if (fcs != null) {
                                BaseBlock block = getFullBlock(mutable);
                                fcs.add(mutable, block, BlockTypes.AIR.getDefaultState().toBaseBlock());
                            }
                        } else {
                            BaseBlock block = getFullBlock(mutable);
                            try {
                                setBlock(mutable, block);
                            } catch (MaxChangedBlocksException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
        if (containsAny) {
            changes++;
            TaskManager.taskManager().sync(new RunnableVal<Object>() {

                @Override
                public void run(Object value) {
                    regenerateChunk(cx, cz, biome, seed);
                }
            });
        }
    }
    if (changes != 0) {
        flushQueue();
        return true;
    }
    return false;
}
Also used : AbstractChangeSet(com.fastasyncworldedit.core.history.changeset.AbstractChangeSet) FaweRegionExtent(com.fastasyncworldedit.core.extent.FaweRegionExtent) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) BlockVector2(com.sk89q.worldedit.math.BlockVector2) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3)

Example 2 with MutableBlockVector2

use of com.fastasyncworldedit.core.math.MutableBlockVector2 in project FastAsyncWorldEdit by IntellectualSites.

the class CuboidRegion method getChunks.

@Override
public Set<BlockVector2> getChunks() {
    BlockVector3 min = getMinimumPoint();
    BlockVector3 max = getMaximumPoint();
    final int maxX = max.getBlockX() >> ChunkStore.CHUNK_SHIFTS;
    final int minX = min.getBlockX() >> ChunkStore.CHUNK_SHIFTS;
    final int maxZ = max.getBlockZ() >> ChunkStore.CHUNK_SHIFTS;
    final int minZ = min.getBlockZ() >> ChunkStore.CHUNK_SHIFTS;
    final int size = (maxX - minX + 1) * (maxZ - minZ + 1);
    // FAWE start
    return new AbstractSet<BlockVector2>() {

        @Nonnull
        @Override
        public Iterator<BlockVector2> iterator() {
            return new Iterator<BlockVector2>() {

                final MutableBlockVector2 mutable = new MutableBlockVector2(0, 0);

                final int bx = minX;

                final int bz = minZ;

                final int tx = maxX;

                final int tz = maxZ;

                private int x = minX;

                private int z = minZ;

                int regionX = x >> 5;

                int regionZ = z >> 5;

                int rbx = Math.max(bx, regionX << 5);

                int rbz = Math.max(bz, regionZ << 5);

                int rtx = Math.min(tx, 31 + (regionX << 5));

                int rtz = Math.min(tz, 31 + (regionZ << 5));

                boolean hasNext = true;

                @Override
                public boolean hasNext() {
                    return hasNext;
                }

                @Override
                public BlockVector2 next() {
                    mutable.mutX(x);
                    mutable.mutZ(z);
                    if (++x > rtx) {
                        if (++z > rtz) {
                            if (x > tx) {
                                x = bx;
                                if (z > tz) {
                                    if (!hasNext) {
                                        throw new NoSuchElementException("End of iterator") {

                                            @Override
                                            public Throwable fillInStackTrace() {
                                                return this;
                                            }
                                        };
                                    }
                                    x = tx;
                                    hasNext = false;
                                    return mutable;
                                }
                            } else {
                                z = rbz;
                            }
                            regionX = x >> 5;
                            regionZ = z >> 5;
                            rbx = Math.max(bx, regionX << 5);
                            rbz = Math.max(bz, regionZ << 5);
                            rtx = Math.min(tx, 31 + (regionX << 5));
                            rtz = Math.min(tz, 31 + (regionZ << 5));
                        } else {
                            x = rbx;
                        }
                    }
                    return mutable;
                }
            };
        }

        @Override
        public int size() {
            return size;
        }

        @Override
        public boolean contains(Object o) {
            if (o instanceof BlockVector2) {
                BlockVector2 cv = (BlockVector2) o;
                return cv.getX() >= minX && cv.getX() <= maxX && cv.getZ() >= minZ && cv.getZ() <= maxZ;
            }
            return false;
        }
    };
}
Also used : MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) Iterator(java.util.Iterator) AbstractSet(java.util.AbstractSet) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) MutableBlockVector2(com.fastasyncworldedit.core.math.MutableBlockVector2) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

MutableBlockVector2 (com.fastasyncworldedit.core.math.MutableBlockVector2)2 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)2 FaweRegionExtent (com.fastasyncworldedit.core.extent.FaweRegionExtent)1 AbstractChangeSet (com.fastasyncworldedit.core.history.changeset.AbstractChangeSet)1 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)1 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)1 AbstractSet (java.util.AbstractSet)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1