Search in sources :

Example 6 with ChunkHolder

use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.

the class RemotePlayerLoadingAgent method handleClientRequest.

public void handleClientRequest(PacketWorldUser packet) {
    try {
        lock.lock();
        if (packet.getType() == Type.REGISTER_CHUNK) {
            int handle = chunkHandle(packet.getX(), packet.getY(), packet.getZ());
            if (usedChunksHandles.add(handle)) {
                ChunkHolder holder = player.getWorld().aquireChunkHolder(player, packet.getX(), packet.getY(), packet.getZ());
                // assume it not being null because it's the supposed behaviour
                assert holder != null;
            } else {
                System.out.println("Received twin request for chunk handle " + handle);
            }
        } else if (packet.getType() == Type.UNREGISTER_CHUNK) {
            int handle = chunkHandle(packet.getX(), packet.getY(), packet.getZ());
            // If we actually owned this handle
            if (usedChunksHandles.remove(handle)) {
                ChunkHolder holder = player.getWorld().getRegionChunkCoordinates(packet.getX(), packet.getY(), packet.getZ()).getChunkHolder(packet.getX(), packet.getY(), packet.getZ());
                // We can assert the chunk holder exists because at this point it MUST be held by this very loading agent !
                assert holder != null;
                holder.unregisterUser(player);
            } else {
                System.out.println("Client requested to unregister something he never registered in the first place.");
            }
        } else if (packet.getType() == Type.REGISTER_SUMMARY) {
            int handle = summaryHandle(packet.getX(), packet.getZ());
            int[] check = summary(handle);
            if (check[0] != packet.getX() || check[1] != packet.getZ()) {
                System.out.println("major fuck up with handle " + handle);
                System.out.println("should have been (" + packet.getX() + ", " + packet.getZ() + ")");
                System.out.println("kys");
                System.exit(-1);
            }
            if (usedRegionHandles.add(handle)) {
                Heightmap regionSummary = player.getWorld().getRegionsSummariesHolder().aquireHeightmap(player, packet.getX(), packet.getZ());
                // assume it not being null because it's the supposed behaviour
                assert regionSummary != null;
            } else {
                System.out.println("Received twin request for region summary (" + packet.getX() + ", " + packet.getZ() + ")");
            }
        } else if (packet.getType() == Type.UNREGISTER_SUMMARY) {
            int handle = summaryHandle(packet.getX(), packet.getZ());
            // If we actually owned this handle
            if (usedRegionHandles.remove(handle)) {
                Heightmap regionSummary = player.getWorld().getRegionsSummariesHolder().getHeightmap(packet.getX(), packet.getZ());
                // We can assert the region summary exists because at this point it MUST be held by this very loading agent !
                assert regionSummary != null;
                regionSummary.unregisterUser(player);
            } else {
                System.out.println("Client requested to unregister summary (" + packet.getX() + ", " + packet.getZ() + ") that he never registered in the first place.");
            }
        } else {
        // TODO hurt him like he hurted you
        // throw new IllegalPacketException(packet);
        }
    } finally {
        lock.unlock();
    }
}
Also used : Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder)

Example 7 with ChunkHolder

use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.

the class RemotePlayerLoadingAgent method destroy.

public void destroy() {
    try {
        lock.lock();
        for (int handle : this.usedChunksHandles) {
            int[] pos = chunk(handle);
            RegionImplementation region = player.getWorld().getRegionChunkCoordinates(pos[0], pos[1], pos[2]);
            // assert region != null;
            if (region != null) {
                ChunkHolder holder = region.getChunkHolder(pos[0], pos[1], pos[2]);
                // assert holder != null; // We can assert the chunk holder exists because at this point it MUST be held by this very loading agent !
                if (holder != null) {
                    holder.unregisterUser(player);
                    continue;
                }
            }
            player.getContext().logger().error("Error while disconnecting player: " + player + ", chunkholder at [" + pos[0] + ":" + pos[1] + ":" + pos[2] + "] wasn't loaded even thought it was part of that player's subscriptions list");
        }
        this.usedChunksHandles.clear();
        for (int handle : this.usedRegionHandles) {
            int[] pos = summary(handle);
            Heightmap regionSummary = player.getWorld().getRegionsSummariesHolder().getHeightmap(pos[0], pos[1]);
            // assert regionSummary != null; // We can assert the region summary exists because at this point it MUST be held by this very loading agent !
            if (regionSummary != null)
                regionSummary.unregisterUser(player);
            else {
                player.getContext().logger().error("Error while disconnecting player: " + player + ", region at [" + pos[0] + " :" + pos[1] + "] wasn't loaded even thought it was part of that player's subscriptions list");
            }
        }
        this.usedRegionHandles.clear();
        destroyed = true;
    } catch (Exception e) {
        player.getContext().logger().error("Error while disconnecting player: " + player + ", exception thrown while freeing his held world data.");
        player.getContext().logger().error(e.getMessage());
    } finally {
        lock.unlock();
    }
}
Also used : Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) RegionImplementation(io.xol.chunkstories.world.region.RegionImplementation) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder)

Example 8 with ChunkHolder

use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.

the class LocalClientLoadingAgent method handleServerResponse.

public void handleServerResponse(PacketWorldUser packet) throws IllegalPacketException {
    try {
        lock.lock();
        // The server refused to register us to this chunk. We gracefully accept.
        if (packet.getType() == Type.UNREGISTER_CHUNK) {
            ChunkHolder holder = world.getRegionChunkCoordinates(packet.getX(), packet.getY(), packet.getZ()).getChunkHolder(packet.getX(), packet.getY(), packet.getZ());
            // Apparently we already figured we didn't need this anyway
            if (holder == null)
                return;
            WorldInfo worldInfo = world.getWorldInfo();
            WorldInfo.WorldSize size = worldInfo.getSize();
            int filteredChunkX = holder.getChunkCoordinateX() & (size.maskForChunksCoordinates);
            int filteredChunkY = Math2.clampi(holder.getChunkCoordinateY(), 0, 31);
            int filteredChunkZ = holder.getChunkCoordinateZ() & (size.maskForChunksCoordinates);
            int summed = ((filteredChunkX << size.bitlengthOfVerticalChunksCoordinates) | filteredChunkY) << size.bitlengthOfHorizontalChunksCoordinates | filteredChunkZ;
            // We remove it from our list
            fastChunksMask.remove(summed);
            usedChunks.remove(holder);
            // And we unsub.
            holder.unregisterUser(player);
        // This is the same but for region summaries
        } else if (packet.getType() == Type.UNREGISTER_SUMMARY) {
            Heightmap regionSummary = world.getRegionsSummariesHolder().getHeightmap(packet.getX(), packet.getZ());
            if (regionSummary == null)
                return;
            usedRegionSummaries.remove(regionSummary);
            regionSummary.unregisterUser(player);
        } else
            // We only expect UNREGISTER packets from the server !
            throw new IllegalPacketException(packet);
    } finally {
        lock.unlock();
    }
}
Also used : Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder) WorldInfo(io.xol.chunkstories.api.world.WorldInfo) IllegalPacketException(io.xol.chunkstories.api.exceptions.net.IllegalPacketException)

Example 9 with ChunkHolder

use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.

the class HashMapWorldRegionsHolder method aquireChunkHolder.

/**
 * Atomically grabs or create a region and registers the asked holder
 */
public ChunkHolder aquireChunkHolder(WorldUser user, int chunkX, int chunkY, int chunkZ) {
    if (chunkY < 0 || chunkY > world.getMaxHeight() / 32)
        return null;
    noConcurrentRegionCreationDestruction.acquireUninterruptibly();
    ChunkHolder holder = this.getOrCreateRegion(chunkX / 8, chunkY / 8, chunkZ / 8).getChunkHolder(chunkX, chunkY, chunkZ);
    boolean userAdded = holder.registerUser(user);
    noConcurrentRegionCreationDestruction.release();
    return userAdded ? holder : holder;
}
Also used : ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder)

Aggregations

ChunkHolder (io.xol.chunkstories.api.world.chunk.ChunkHolder)9 Heightmap (io.xol.chunkstories.api.world.heightmap.Heightmap)6 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)4 Fence (io.xol.chunkstories.api.util.concurrency.Fence)2 WorldInfo (io.xol.chunkstories.api.world.WorldInfo)2 ConverterWorkerThread (io.xol.chunkstories.converter.ConverterWorkers.ConverterWorkerThread)2 Mapper (io.xol.chunkstories.api.converter.mappings.Mapper)1 NonTrivialMapper (io.xol.chunkstories.api.converter.mappings.NonTrivialMapper)1 Entity (io.xol.chunkstories.api.entity.Entity)1 IllegalPacketException (io.xol.chunkstories.api.exceptions.net.IllegalPacketException)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 Task (io.xol.chunkstories.api.workers.Task)1 TaskExecutor (io.xol.chunkstories.api.workers.TaskExecutor)1 WorldSize (io.xol.chunkstories.api.world.WorldInfo.WorldSize)1 WorldUser (io.xol.chunkstories.api.world.WorldUser)1 CellData (io.xol.chunkstories.api.world.cell.CellData)1 FutureCell (io.xol.chunkstories.api.world.cell.FutureCell)1 SimpleFence (io.xol.chunkstories.util.concurrency.SimpleFence)1 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)1 WorldTool (io.xol.chunkstories.world.WorldTool)1