Search in sources :

Example 6 with Location

use of io.xol.chunkstories.api.Location in project chunkstories-core by Hugobros3.

the class VoxelDoor method handleInteraction.

// Meta
// 0x0 -> open/close
// 0x1 -> left/right hinge || left = 0 right = 1 (left is default)
// 0x2-0x4 -> side ( VoxelSide << 2 )
@Override
public boolean handleInteraction(Entity entity, ChunkCell voxelContext, Input input) {
    if (!input.getName().equals("mouse.right"))
        return false;
    if (!(entity.getWorld() instanceof WorldMaster))
        return true;
    boolean isOpen = ((voxelContext.getMetaData() >> 0) & 0x1) == 1;
    boolean hingeSide = ((voxelContext.getMetaData() >> 1) & 0x1) == 1;
    int facingPassed = (voxelContext.getMetaData() >> 2) & 0x3;
    boolean newState = !isOpen;
    int newData = computeMeta(newState, hingeSide, facingPassed);
    Location otherPartLocation = voxelContext.getLocation();
    if (top)
        otherPartLocation.add(0.0, -1.0, 0.0);
    else
        otherPartLocation.add(0.0, 1.0, 0.0);
    EditableCell otherLocationPeek = voxelContext.getWorld().peekSafely(otherPartLocation);
    if (otherLocationPeek.getVoxel() instanceof VoxelDoor) {
        System.out.println("new door status : " + newState);
        voxelContext.getWorld().getSoundManager().playSoundEffect("sounds/voxels/door.ogg", Mode.NORMAL, voxelContext.getLocation(), 1.0f, 1.0f);
        voxelContext.setMetaData(newData);
        otherLocationPeek.setMetaData(newData);
    // otherPartLocation.setVoxelDataAtLocation(VoxelFormat.changeMeta(otherPartLocation.getVoxelDataAtLocation(), newData));
    } else {
        store.parent().logger().error("Incomplete door @ " + otherPartLocation);
    }
    return true;
}
Also used : WorldMaster(io.xol.chunkstories.api.world.WorldMaster) EditableCell(io.xol.chunkstories.api.world.cell.EditableCell) Location(io.xol.chunkstories.api.Location)

Example 7 with Location

use of io.xol.chunkstories.api.Location in project chunkstories-core by Hugobros3.

the class VoxelStairs method onPlace.

@Override
public void onPlace(FutureCell cell, WorldModificationCause cause) {
    // id+dir of slope
    // 0LEFT x-
    // 1RIGHT x+
    // 2BACK z-
    // 3FRONT z+
    int stairsSide = 0;
    if (cause != null && cause instanceof Entity) {
        Entity entity = (Entity) cause;
        Location loc = entity.getLocation();
        double dx = loc.x() - (cell.getX() + 0.5);
        double dz = loc.z() - (cell.getZ() + 0.5);
        if (Math.abs(dx) > Math.abs(dz)) {
            if (dx > 0)
                stairsSide = 1;
            else
                stairsSide = 0;
        } else {
            if (dz > 0)
                stairsSide = 3;
            else
                stairsSide = 2;
        }
        if (entity instanceof EntityPlayer) {
            if (((EntityPlayer) entity).getEntityRotationComponent().getVerticalRotation() < 0)
                stairsSide += 4;
        }
        cell.setMetaData(stairsSide);
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityPlayer(io.xol.chunkstories.core.entity.EntityPlayer) Location(io.xol.chunkstories.api.Location)

Example 8 with Location

use of io.xol.chunkstories.api.Location 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 9 with Location

use of io.xol.chunkstories.api.Location in project chunkstories by Hugobros3.

the class HeightmapArrayTexture method update.

public void update() {
    Player player = client.getPlayer();
    Location playerPosition = player.getLocation();
    if (playerPosition == null)
        // We won't do shit with that going on
        return;
    World world = playerPosition.getWorld();
    int chunkX = (int) Math.floor(playerPosition.x / 32.0);
    int chunkZ = (int) Math.floor(playerPosition.z / 32.0);
    int regionX = chunkX / 8;
    int regionZ = chunkZ / 8;
    // Remap the array
    if (lastRegionX != regionX || lastRegionZ != regionZ || redo.compareAndSet(true, false)) {
        WriteLock writeLock = lock.writeLock();
        writeLock.lock();
        // We may need this
        ByteBuffer bb = MemoryUtil.memAlloc(4 * 256 * 256);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        // Clear unused slots
        for (int i = 0; i < 81; i++) {
            ArrayTextureSlot slot = arrayTextureContents[i];
            if (slot == null)
                continue;
            // Frees slots immediately once out of the area we care about
            if (Math.abs(slot.regionX - regionX) >= 5 || Math.abs(slot.regionZ - regionZ) >= 5)
                arrayTextureContents[i] = null;
        }
        for (int i = -4; i <= 4; i++) for (int j = -4; j <= 4; j++) {
            int regionI = regionX + i;
            int regionJ = regionZ + j;
            // Wrap arround the world!
            if (regionI < 0)
                regionI += world.getSizeInChunks() / 256;
            if (regionJ < 0)
                regionJ += world.getSizeInChunks() / 256;
            // Look for a slot already containing our wanted textures
            int free = -1;
            int good = -1;
            for (int k = 0; k < 81; k++) {
                ArrayTextureSlot slot = arrayTextureContents[k];
                if (slot == null) {
                    if (free == -1)
                        free = k;
                } else {
                    if (slot.regionX == regionI && slot.regionZ == regionJ) {
                        good = k;
                        break;
                    }
                }
            }
            int slot;
            // If no good slot was found :(
            if (good == -1) {
                arrayTextureContents[free] = new ArrayTextureSlot();
                arrayTextureContents[free].regionX = regionI;
                arrayTextureContents[free].regionZ = regionJ;
                slot = free;
            } else {
                slot = good;
            }
            // If data is not yet in the slot, check if the world has data for it
            if (!arrayTextureContents[slot].hasData) {
                Heightmap sum = world.getRegionsSummariesHolder().getHeightmap(arrayTextureContents[slot].regionX, arrayTextureContents[slot].regionZ);
                if (sum != null && sum.isLoaded()) {
                    loadHeights((HeightmapImplementation) sum, bb, 0);
                    heights.uploadTextureData(slot, 0, bb);
                    for (int lod = 1; lod <= 8; lod++) {
                        loadHeights((HeightmapImplementation) sum, bb, lod);
                        heights.uploadTextureData(slot, lod, bb);
                    }
                    heights.setMipMapping(true);
                    heights.setMipmapLevelsRange(0, 8);
                    loadTopVoxels((HeightmapImplementation) sum, bb, 0);
                    topVoxels.uploadTextureData(slot, 0, bb);
                    for (int lod = 1; lod <= 8; lod++) {
                        loadTopVoxels((HeightmapImplementation) sum, bb, lod);
                        topVoxels.uploadTextureData(slot, lod, bb);
                    }
                    topVoxels.setMipMapping(true);
                    topVoxels.setMipmapLevelsRange(0, 8);
                    arrayTextureContents[slot].hasData = true;
                }
            }
            arrayTextureReference[i + 4][j + 4] = slot;
        }
        MemoryUtil.memFree(bb);
        lastRegionX = regionX;
        lastRegionZ = regionZ;
        writeLock.unlock();
    }
}
Also used : WriteLock(java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Player(io.xol.chunkstories.api.player.Player) Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) World(io.xol.chunkstories.api.world.World) ByteBuffer(java.nio.ByteBuffer) Location(io.xol.chunkstories.api.Location)

Example 10 with Location

use of io.xol.chunkstories.api.Location in project chunkstories by Hugobros3.

the class DebugInfoRenderer method drawF3debugMenu.

public void drawF3debugMenu(RenderingInterface renderingInterface) {
    CameraInterface camera = renderingInterface.getCamera();
    Entity playerEntity = client.getPlayer().getControlledEntity();
    /*int timeTook = Client.profiler.timeTook();
		String debugInfo = Client.profiler.reset("gui").toString();
		if (timeTook > 400)
			System.out.println("Lengty frame, printing debug information : \n" + debugInfo);*/
    // Memory usage
    long total = Runtime.getRuntime().totalMemory();
    long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    // By default use the camera position
    int bx = ((int) camera.getCameraPosition().x());
    int by = ((int) camera.getCameraPosition().y());
    int bz = ((int) camera.getCameraPosition().z());
    int lx = bx, ly = by, lz = bz;
    // If the player can look use that
    if (playerEntity != null && playerEntity instanceof EntityControllable) {
        Location loc = ((EntityControllable) playerEntity).getBlockLookingAt(true);
        if (loc != null) {
            lx = (int) loc.x();
            ly = (int) loc.y();
            lz = (int) loc.z();
        }
    }
    int raw_data = world.peekRaw(lx, ly, lz);
    CellData cell = world.peekSafely(lx, ly, lz);
    // System.out.println(VoxelFormat.id(raw_data));
    int cx = bx / 32;
    int cy = by / 32;
    int cz = bz / 32;
    int csh = world.getRegionsSummariesHolder().getHeightAtWorldCoordinates(bx, bz);
    // Obtain the angle the player is facing
    VoxelSide side = VoxelSide.TOP;
    float angleX = -1;
    if (playerEntity != null && playerEntity instanceof EntityLiving)
        angleX = Math.round(((EntityLiving) playerEntity).getEntityRotationComponent().getHorizontalRotation());
    double dx = Math.sin(angleX / 360 * 2.0 * Math.PI);
    double dz = Math.cos(angleX / 360 * 2.0 * Math.PI);
    if (Math.abs(dx) > Math.abs(dz)) {
        if (dx > 0)
            side = VoxelSide.RIGHT;
        else
            side = VoxelSide.LEFT;
    } else {
        if (dz > 0)
            side = VoxelSide.FRONT;
        else
            side = VoxelSide.BACK;
    }
    // Count all the entities
    int ec = 0;
    IterableIterator<Entity> i = world.getAllLoadedEntities();
    while (i.hasNext()) {
        i.next();
        ec++;
    }
    Chunk current = world.getChunk(cx, cy, cz);
    int x_top = renderingInterface.getWindow().getHeight() - 16;
    Font font = null;
    font = renderingInterface.getFontRenderer().getFont("LiberationSans-Regular", 12);
    if (font == null)
        font = renderingInterface.getFontRenderer().getFont("LiberationSans-Regular", 12);
    int lineHeight = font.getLineHeight();
    int posx, posy;
    String text;
    posx = 8;
    posy = x_top - posx;
    text = GLCalls.getStatistics() + " Chunks in view : " + world.getWorldRenderer().getChunksRenderer().getChunksVisible() + " Entities " + ec + " Particles :" + ((ClientParticlesRenderer) world.getParticlesManager()).count() + " #FF0000Render FPS: " + Client.getInstance().getGameWindow().getFPS() + " avg: " + Math.floor(10000.0 / Client.getInstance().getGameWindow().getFPS()) / 10.0 + " #00FFFFSimulation FPS: " + world.getWorldRenderer().getWorld().getGameLogic().getSimulationFps();
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "RAM usage : " + used / 1024 / 1024 + " / " + total / 1024 / 1024 + " MB used, chunks loaded in ram: " + world.getRegionsHolder().countChunksWithData() + "/" + world.getRegionsHolder().countChunks() + " " + Math.floor(world.getRegionsHolder().countChunksWithData() * 4 * 32 * 32 * 32 / (1024L * 1024 / 100f)) / 100f + "MB used by chunks";
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    long totalVram = (renderingInterface.getTotalVramUsage()) / 1024 / 1024;
    posy -= lineHeight;
    text = "VRAM usage : " + totalVram + "MB as " + Texture2DGL.getTotalNumberOfTextureObjects() + " textures using " + Texture2DGL.getTotalVramUsage() / 1024 / 1024 + "MB + " + VertexBufferGL.getTotalNumberOfVerticesObjects() + " vbos using " + renderingInterface.getVertexDataVramUsage() / 1024 / 1024 + " MB";
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Worker threads: " + world.getGameContext().tasks() + " - " + world.ioHandler.toString();
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Position : x:" + bx + " y:" + by + " z:" + bz + " dir: " + angleX + " side: " + side + " #FF0000Block looking at#FFFFFF : pos: " + lx + ": " + ly + ": " + lz + " data: " + raw_data + " voxel_type: " + cell.getVoxel().getName() + " sl:" + cell.getSunlight() + " bl: " + cell.getBlocklight() + " meta:" + cell.getMetaData() + " csh:" + csh;
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Current Summary : " + world.getRegionsSummariesHolder().getHeightmapChunkCoordinates(cx, cz);
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Current Region : " + world.getRegionChunkCoordinates(cx, cy, cz);
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    if (current == null) {
        posy -= lineHeight;
        text = "Current chunk null";
        renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    } else if (current instanceof ChunkRenderable) {
        ChunkRenderDataHolder chunkRenderData = ((ClientChunk) current).getChunkRenderData();
        if (chunkRenderData != null) {
            posy -= lineHeight;
            text = "Current Chunk : " + current + " - " + chunkRenderData.toString();
            renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
        } else {
            posy -= lineHeight;
            text = "Current Chunk : " + current + " - No rendering data";
            renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
        }
    }
    if (playerEntity != null && playerEntity instanceof Entity) {
        posy -= lineHeight;
        text = "Controlled Entity : " + playerEntity;
        renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) VoxelSide(io.xol.chunkstories.api.voxel.VoxelSide) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) ChunkRenderable(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) ClientChunk(io.xol.chunkstories.world.chunk.ClientChunk) CellData(io.xol.chunkstories.api.world.cell.CellData) Font(io.xol.chunkstories.api.rendering.text.FontRenderer.Font) ChunkRenderDataHolder(io.xol.chunkstories.renderer.chunks.ChunkRenderDataHolder) Vector4f(org.joml.Vector4f) CameraInterface(io.xol.chunkstories.api.rendering.CameraInterface) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) Location(io.xol.chunkstories.api.Location) ClientParticlesRenderer(io.xol.chunkstories.renderer.particles.ClientParticlesRenderer)

Aggregations

Location (io.xol.chunkstories.api.Location)35 Entity (io.xol.chunkstories.api.entity.Entity)17 Player (io.xol.chunkstories.api.player.Player)12 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)12 Vector3d (org.joml.Vector3d)12 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)9 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)6 Vector3dc (org.joml.Vector3dc)6 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)5 World (io.xol.chunkstories.api.world.World)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)4 WorldClient (io.xol.chunkstories.api.world.WorldClient)4 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)3 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)3 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)3 FutureCell (io.xol.chunkstories.api.world.cell.FutureCell)3 EntityPlayer (io.xol.chunkstories.core.entity.EntityPlayer)3 Matrix4f (org.joml.Matrix4f)3 Vector2d (org.joml.Vector2d)3