Search in sources :

Example 1 with Region

use of io.xol.chunkstories.api.world.region.Region in project chunkstories by Hugobros3.

the class WorldImplementation method peek.

@Override
public /**
 * Fancy get method that throws exceptions when the world isn't loaded
 */
ChunkCell peek(int x, int y, int z) throws WorldException {
    x = sanitizeHorizontalCoordinate(x);
    y = sanitizeVerticalCoordinate(y);
    z = sanitizeHorizontalCoordinate(z);
    Region region = this.getRegionWorldCoordinates(x, y, z);
    if (region == null)
        throw new RegionNotLoadedException(this, x / 256, y / 256, z / 256);
    Chunk chunk = region.getChunk((x / 32) % 8, (y / 32) % 8, (z / 32) % 8);
    if (chunk == null)
        throw new ChunkNotLoadedException(region, (x / 32) % 8, (y / 32) % 8, (z / 32) % 8);
    return chunk.peek(x, y, z);
}
Also used : Region(io.xol.chunkstories.api.world.region.Region) ChunkNotLoadedException(io.xol.chunkstories.api.exceptions.world.ChunkNotLoadedException) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) RegionNotLoadedException(io.xol.chunkstories.api.exceptions.world.RegionNotLoadedException)

Example 2 with Region

use of io.xol.chunkstories.api.world.region.Region in project chunkstories by Hugobros3.

the class WorldEntitiesHolder method getEntitiesInBox.

public NearEntitiesIterator getEntitiesInBox(Vector3dc center, Vector3dc boxSize) {
    return new NearEntitiesIterator() {

        int centerVoxel_x = (int) (double) center.x();

        int centerVoxel_y = (int) (double) center.y();

        int centerVoxel_z = (int) (double) center.z();

        int box_ceil_x = (int) Math.ceil((double) boxSize.x());

        int box_ceil_y = (int) Math.ceil((double) boxSize.y());

        int box_ceil_z = (int) Math.ceil((double) boxSize.z());

        int box_start_x = sanitizeHorizontalCoordinate(centerVoxel_x - box_ceil_x);

        int box_start_y = sanitizeVerticalCoordinate(centerVoxel_y - box_ceil_y);

        int box_start_z = sanitizeHorizontalCoordinate(centerVoxel_z - box_ceil_z);

        int box_end_x = sanitizeHorizontalCoordinate(centerVoxel_x + box_ceil_x);

        int box_end_y = sanitizeVerticalCoordinate(centerVoxel_y + box_ceil_y);

        int box_end_z = sanitizeHorizontalCoordinate(centerVoxel_z + box_ceil_z);

        // We currently sort this out by regions, chunks would be more appropriate ?
        int region_start_x = box_start_x / 256;

        int region_start_y = box_start_y / 256;

        int region_start_z = box_start_z / 256;

        int region_end_x = box_end_x / 256;

        int region_end_y = box_end_y / 256;

        int region_end_z = box_end_z / 256;

        int region_x = region_start_x;

        int region_y = region_start_y;

        int region_z = region_start_z;

        Region currentRegion = world.getRegion(region_x, region_y, region_z);

        Iterator<Entity> currentRegionIterator = currentRegion == null ? null : currentRegion.getEntitiesWithinRegion();

        Entity next = null;

        double distance = 0D;

        private void seekNextEntity() {
            next = null;
            while (true) {
                // Break the loop if we find an entity in the region
                if (seekNextEntityWithinRegion())
                    break;
                else {
                    // Seek a suitable region if we failed to find anything above
                    if (seekNextRegion())
                        continue;
                    else
                        break;
                }
            }
        }

        private boolean seekNextEntityWithinRegion() {
            if (currentRegionIterator == null)
                return false;
            while (currentRegionIterator.hasNext()) {
                Entity entity = currentRegionIterator.next();
                // Check if it's inside the box for realz
                Location loc = entity.getLocation();
                int locx = (int) (double) loc.x();
                // Normal case, check if it's in the bounds, wrap-arround case, check if it's outside
                if ((box_start_x > box_end_x) == (locx >= box_start_x && locx <= box_end_x))
                    continue;
                int locy = (int) (double) loc.y();
                // Normal case, check if it's in the bounds, wrap-arround case, check if it's outside
                if ((box_start_y > box_end_y) == (locy >= box_start_y && locy <= box_end_y))
                    continue;
                int locz = (int) (double) loc.z();
                // Normal case, check if it's in the bounds, wrap-arround case, check if it's outside
                if ((box_start_z > box_end_z) == (locz >= box_start_z && locz <= box_end_z))
                    continue;
                // if(Math.abs(check.getX()) <= boxSize.getX() && Math.abs(check.getY()) <= boxSize.getY() && Math.abs(check.getZ()) <= boxSize.getZ())
                {
                    // Found a good one
                    this.next = entity;
                    Vector3d check = new Vector3d(loc);
                    check.sub(center);
                    this.distance = check.length();
                    return true;
                }
            }
            // We found nothing :(
            currentRegionIterator = null;
            return false;
        }

        private boolean seekNextRegion() {
            currentRegion = null;
            while (true) {
                // Found one !
                if (currentRegion != null) {
                    currentRegionIterator = currentRegion.getEntitiesWithinRegion();
                    return true;
                }
                region_x++;
                // Wrap arround in X dimension to Y
                if (region_x > region_end_x) {
                    region_x = 0;
                    region_y++;
                }
                // Then Y to Z
                if (region_y > region_end_y) {
                    region_y = 0;
                    region_z++;
                }
                // We are done here
                if (region_z > region_end_z)
                    return false;
                currentRegion = world.getRegion(region_x, region_y, region_z);
            }
        }

        @Override
        public boolean hasNext() {
            if (next == null)
                seekNextEntity();
            return next != null;
        }

        @Override
        public Entity next() {
            Entity entity = next;
            seekNextEntity();
            return entity;
        }

        @Override
        public double distance() {
            return distance;
        }
    };
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) NearEntitiesIterator(io.xol.chunkstories.api.world.World.NearEntitiesIterator) Vector3d(org.joml.Vector3d) Iterator(java.util.Iterator) NearEntitiesIterator(io.xol.chunkstories.api.world.World.NearEntitiesIterator) Region(io.xol.chunkstories.api.world.region.Region) Location(io.xol.chunkstories.api.Location)

Example 3 with Region

use of io.xol.chunkstories.api.world.region.Region in project chunkstories by Hugobros3.

the class WorldImplementation method poke.

@Override
public ChunkCell poke(int x, int y, int z, Voxel voxel, int sunlight, int blocklight, int metadata, WorldModificationCause cause) throws WorldException {
    x = sanitizeHorizontalCoordinate(x);
    y = sanitizeVerticalCoordinate(y);
    z = sanitizeHorizontalCoordinate(z);
    Region region = this.getRegionWorldCoordinates(x, y, z);
    if (region == null)
        throw new RegionNotLoadedException(this, x / 256, y / 256, z / 256);
    Chunk chunk = region.getChunk((x / 32) % 8, (y / 32) % 8, (z / 32) % 8);
    if (chunk == null)
        throw new ChunkNotLoadedException(region, (x / 32) % 8, (y / 32) % 8, (z / 32) % 8);
    return chunk.poke(x, y, z, voxel, sunlight, blocklight, metadata, cause);
}
Also used : Region(io.xol.chunkstories.api.world.region.Region) ChunkNotLoadedException(io.xol.chunkstories.api.exceptions.world.ChunkNotLoadedException) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) RegionNotLoadedException(io.xol.chunkstories.api.exceptions.world.RegionNotLoadedException)

Example 4 with Region

use of io.xol.chunkstories.api.world.region.Region in project chunkstories by Hugobros3.

the class WorldImplementation method peekSafely.

@Override
public WorldCell peekSafely(int x, int y, int z) {
    x = sanitizeHorizontalCoordinate(x);
    y = sanitizeVerticalCoordinate(y);
    z = sanitizeHorizontalCoordinate(z);
    Region region = this.getRegionWorldCoordinates(x, y, z);
    if (region == null) {
        return new UnloadedWorldCell(x, y, z, this.getGameContext().getContent().voxels().air(), 0, 0, 0);
    }
    Chunk chunk = region.getChunk((x / 32) % 8, (y / 32) % 8, (z / 32) % 8);
    if (chunk == null)
        return new UnloadedWorldCell(x, y, z, this.getGameContext().getContent().voxels().air(), 0, 0, 0);
    return chunk.peek(x, y, z);
}
Also used : Region(io.xol.chunkstories.api.world.region.Region) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk)

Example 5 with Region

use of io.xol.chunkstories.api.world.region.Region in project chunkstories by Hugobros3.

the class HashMapWorldRegionsHolder method saveAll.

public Fence saveAll() {
    CompoundFence allRegionsFences = new CompoundFence();
    Iterator<RegionImplementation> i = regions.values().iterator();
    Region region;
    while (i.hasNext()) {
        region = i.next();
        if (region != null) {
            allRegionsFences.add(region.save());
        }
    }
    return allRegionsFences;
}
Also used : CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) Region(io.xol.chunkstories.api.world.region.Region)

Aggregations

Region (io.xol.chunkstories.api.world.region.Region)5 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)3 CubicChunk (io.xol.chunkstories.world.chunk.CubicChunk)3 ChunkNotLoadedException (io.xol.chunkstories.api.exceptions.world.ChunkNotLoadedException)2 RegionNotLoadedException (io.xol.chunkstories.api.exceptions.world.RegionNotLoadedException)2 Location (io.xol.chunkstories.api.Location)1 Entity (io.xol.chunkstories.api.entity.Entity)1 NearEntitiesIterator (io.xol.chunkstories.api.world.World.NearEntitiesIterator)1 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)1 Iterator (java.util.Iterator)1 Vector3d (org.joml.Vector3d)1