Search in sources :

Example 1 with Heightmap

use of io.xol.chunkstories.api.world.heightmap.Heightmap in project chunkstories by Hugobros3.

the class ConverterWorkers method dropAll.

public void dropAll() {
    CompoundFence readyAll = new CompoundFence();
    CompoundFence doneAll = new CompoundFence();
    SimpleFence atSignal = new SimpleFence();
    for (int i = 0; i < workers.length; i++) {
        SimpleFence ready = new SimpleFence();
        readyAll.add(ready);
        SimpleFence done = new SimpleFence();
        doneAll.add(done);
        scheduleTask(new Task() {

            @Override
            protected boolean task(TaskExecutor taskExecutor) {
                ready.signal();
                atSignal.traverse();
                ConverterWorkerThread cwt = (ConverterWorkerThread) taskExecutor;
                for (ChunkHolder holder : cwt.registeredCS_Holders) {
                    holder.unregisterUser(cwt);
                    cwt.chunksAquired--;
                }
                for (Heightmap summary : cwt.registeredCS_Summaries) summary.unregisterUser(cwt);
                cwt.registeredCS_Summaries.clear();
                cwt.registeredCS_Holders.clear();
                done.signal();
                return true;
            }
        });
    }
    readyAll.traverse();
    atSignal.signal();
    doneAll.traverse();
}
Also used : CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) Task(io.xol.chunkstories.api.workers.Task) TaskExecutor(io.xol.chunkstories.api.workers.TaskExecutor) Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder) SimpleFence(io.xol.chunkstories.util.concurrency.SimpleFence)

Example 2 with Heightmap

use of io.xol.chunkstories.api.world.heightmap.Heightmap in project chunkstories by Hugobros3.

the class MultithreadedOfflineWorldConverter method stepThreeSpreadLightning.

protected void stepThreeSpreadLightning(WorldTool csWorld) {
    verbose("Entering step three: spreading light");
    csWorld.setLightning(true);
    WorldSize size = csWorld.getWorldInfo().getSize();
    int maxHeightPossible = 256;
    int done = 0;
    int todo = (size.sizeInChunks) * (size.sizeInChunks);
    double completion = 0.0;
    long lastPercentageShow = System.currentTimeMillis();
    Set<ChunkHolder> registeredCS_Holders = new HashSet<ChunkHolder>();
    Set<Heightmap> registeredCS_Summaries = new HashSet<Heightmap>();
    int chunksAquired = 0;
    WorldUser worldUser = this;
    int waveSize = this.threadsCount * 32;
    int wave = 0;
    CompoundFence waveFence = new CompoundFence();
    for (int chunkX = 0; chunkX < size.sizeInChunks; chunkX++) {
        for (int chunkZ = 0; chunkZ < size.sizeInChunks; chunkZ++) {
            wave++;
            CompoundFence loadRelevantData = new CompoundFence();
            Heightmap sum = csWorld.getRegionsSummariesHolder().aquireHeightmapChunkCoordinates(worldUser, chunkX, chunkZ);
            registeredCS_Summaries.add(sum);
            loadRelevantData.add(sum.waitForLoading());
            // Loads 3x3 arround relevant chunks
            for (int i = -1; i < 2; i++) {
                for (int j = -1; j < 2; j++) {
                    for (int chunkY = 0; chunkY <= maxHeightPossible / 32; chunkY++) {
                        ChunkHolder chunkHolder = csWorld.aquireChunkHolder(worldUser, chunkX + i, chunkY, chunkZ + j);
                        if (chunkHolder != null) {
                            loadRelevantData.add(chunkHolder.waitForLoading());
                            if (registeredCS_Holders.add(chunkHolder))
                                chunksAquired++;
                        }
                    }
                }
            }
            assert chunksAquired == registeredCS_Holders.size();
            // Wait for everything to actually load
            loadRelevantData.traverse();
            // Spreads lightning, from top to botton
            for (int chunkY = maxHeightPossible / 32; chunkY >= 0; chunkY--) {
                CubicChunk chunk = csWorld.getChunk(chunkX, chunkY, chunkZ);
                Fence fence = chunk.lightBaker.requestLightningUpdate();
                // TaskLightChunk task = new TaskLightChunk(chunk, true);
                // workers.scheduleTask(task);
                waveFence.add(fence);
            }
            if (wave >= waveSize) {
                waveFence.traverse();
                waveFence.clear();
                while (true) {
                    if (workers.size() > 0) {
                        // endless tasks
                        try {
                            Thread.sleep(50L);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else
                        break;
                }
                wave = 0;
                // Show progress
                done += waveSize;
                if (Math.floor(((double) done / (double) todo) * 100) > completion) {
                    completion = Math.floor(((double) done / (double) todo) * 100);
                    if (completion >= 100.0 || (System.currentTimeMillis() - lastPercentageShow > 5000)) {
                        verbose(completion + "% ... using " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + "/" + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "Mb ");
                        lastPercentageShow = System.currentTimeMillis();
                    }
                }
                if (registeredCS_Holders.size() > targetChunksToKeepInRam) {
                    for (ChunkHolder holder : registeredCS_Holders) {
                        holder.unregisterUser(worldUser);
                        chunksAquired--;
                    }
                    for (Heightmap summary : registeredCS_Summaries) summary.unregisterUser(worldUser);
                    registeredCS_Summaries.clear();
                    registeredCS_Holders.clear();
                    csWorld.unloadUselessData().traverse();
                // verbose("Done.");
                }
            }
        }
    }
    waveFence.traverse();
    wave = 0;
    // Terminate
    for (ChunkHolder holder : registeredCS_Holders) {
        holder.unregisterUser(worldUser);
        chunksAquired--;
    }
    for (Heightmap summary : registeredCS_Summaries) summary.unregisterUser(worldUser);
    registeredCS_Summaries.clear();
    registeredCS_Holders.clear();
    csWorld.unloadUselessData().traverse();
/*csWorld.saveEverything();
		for (ChunkHolder holder : registeredCS_Holders)
			holder.unregisterUser(worldUser);

		csWorld.unloadUselessData().traverse();*/
}
Also used : Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) WorldUser(io.xol.chunkstories.api.world.WorldUser) CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder) WorldSize(io.xol.chunkstories.api.world.WorldInfo.WorldSize) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) Fence(io.xol.chunkstories.api.util.concurrency.Fence) HashSet(java.util.HashSet)

Example 3 with Heightmap

use of io.xol.chunkstories.api.world.heightmap.Heightmap 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 4 with Heightmap

use of io.xol.chunkstories.api.world.heightmap.Heightmap 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 5 with Heightmap

use of io.xol.chunkstories.api.world.heightmap.Heightmap in project chunkstories by Hugobros3.

the class DebugWorldDataCommands method handleCommand.

@Override
public boolean handleCommand(CommandEmitter emitter, Command command, String[] arguments) {
    if (command.getName().equals("chunk") && emitter.hasPermission("server.debug")) {
        Player player = (Player) emitter;
        emitter.sendMessage("#00FFD0" + player.getControlledEntity().getChunk());
        return true;
    } else if (command.getName().equals("region") && emitter.hasPermission("server.debug")) {
        Player player = (Player) emitter;
        Chunk chunk = player.getControlledEntity().getChunk();
        if (chunk != null)
            emitter.sendMessage("#00FFD0" + chunk.getRegion());
        else
            emitter.sendMessage("#00FFD0" + "not within a loaded chunk, so no parent region could be found.");
        return true;
    } else if (command.getName().equals("heightmap") && emitter.hasPermission("server.debug")) {
        Heightmap sum;
        if (arguments.length == 2) {
            int x = Integer.parseInt(arguments[0]);
            int z = Integer.parseInt(arguments[1]);
            sum = server.getWorld().getRegionsSummariesHolder().getHeightmap(x, z);
        } else {
            Player player = (Player) emitter;
            sum = player.getWorld().getRegionsSummariesHolder().getHeightmapLocation(player.getLocation());
        }
        emitter.sendMessage("#00FFD0" + sum);
        return true;
    } else if (command.getName().equals("heightmaps") && emitter.hasPermission("server.debug")) {
        dumpLoadedHeightmap((WorldImplementation) server.getWorld(), emitter);
    }
    return false;
}
Also used : Player(io.xol.chunkstories.api.player.Player) Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) WorldImplementation(io.xol.chunkstories.world.WorldImplementation)

Aggregations

Heightmap (io.xol.chunkstories.api.world.heightmap.Heightmap)8 ChunkHolder (io.xol.chunkstories.api.world.chunk.ChunkHolder)6 Player (io.xol.chunkstories.api.player.Player)2 WorldInfo (io.xol.chunkstories.api.world.WorldInfo)2 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)2 Location (io.xol.chunkstories.api.Location)1 Entity (io.xol.chunkstories.api.entity.Entity)1 IllegalPacketException (io.xol.chunkstories.api.exceptions.net.IllegalPacketException)1 Fence (io.xol.chunkstories.api.util.concurrency.Fence)1 Task (io.xol.chunkstories.api.workers.Task)1 TaskExecutor (io.xol.chunkstories.api.workers.TaskExecutor)1 World (io.xol.chunkstories.api.world.World)1 WorldSize (io.xol.chunkstories.api.world.WorldInfo.WorldSize)1 WorldUser (io.xol.chunkstories.api.world.WorldUser)1 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)1 SimpleFence (io.xol.chunkstories.util.concurrency.SimpleFence)1 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)1 WorldImplementation (io.xol.chunkstories.world.WorldImplementation)1 CubicChunk (io.xol.chunkstories.world.chunk.CubicChunk)1 RegionImplementation (io.xol.chunkstories.world.region.RegionImplementation)1