Search in sources :

Example 1 with Fence

use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.

the class PacketInitializeRemoteWorld method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    String initializationString = in.readUTF();
    ByteArrayInputStream bais = new ByteArrayInputStream(initializationString.getBytes("UTF-8"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(bais, "UTF-8"));
    info = new WorldInfoImplementation(reader);
    if (processor instanceof ClientPacketsContext) {
        processor.logger().info("Received World initialization packet");
        ClientPacketsContext cpp = (ClientPacketsContext) processor;
        OnlineContentTranslator contentTranslator = cpp.getContentTranslator();
        if (contentTranslator == null) {
            processor.logger().error("Can't initialize a world without a ContentTranslator initialized first!");
            return;
        }
        // TODO should we expose this to the interface ?
        Client client = (Client) cpp.getContext();
        Fence fence = client.getGameWindow().queueSynchronousTask(new Runnable() {

            @Override
            public void run() {
                WorldClientRemote world;
                try {
                    world = new WorldClientRemote(client, info, contentTranslator, cpp.getConnection());
                    client.changeWorld(world);
                    cpp.getConnection().handleSystemRequest("world/ok");
                } catch (WorldLoadingException e) {
                    client.exitToMainMenu(e.getMessage());
                }
            }
        });
        fence.traverse();
    }
}
Also used : WorldLoadingException(io.xol.chunkstories.world.WorldLoadingException) OnlineContentTranslator(io.xol.chunkstories.api.content.OnlineContentTranslator) InputStreamReader(java.io.InputStreamReader) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) ClientPacketsContext(io.xol.chunkstories.client.net.ClientPacketsContext) WorldInfoImplementation(io.xol.chunkstories.world.WorldInfoImplementation) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) Fence(io.xol.chunkstories.api.util.concurrency.Fence) Client(io.xol.chunkstories.client.Client)

Example 2 with Fence

use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.

the class ChunkRenderDataHolder method setData.

public void setData(ChunkMeshDataSections newData) {
    if (newData == null)
        throw new NullPointerException("setData() requires non-null ata");
    oneUploadAtATime.acquireUninterruptibly();
    noDrawDeleteConflicts.acquireUninterruptibly();
    // Meh that's a waste of time then
    if (isDestroyed) {
        noDrawDeleteConflicts.release();
        oneUploadAtATime.release();
        // <-- Free the data
        newData.notNeeded();
        return;
    }
    // No verticesObject already created; create one, fill it and then change the bails
    if (verticesObject == null) {
        VertexBuffer wip = worldRenderer.getRenderingInterface().newVertexBuffer();
        Fence fence = wip.uploadData(newData.dataToUpload);
        // We unlock while waiting for the upload
        noDrawDeleteConflicts.release();
        fence.traverse();
        // Then we lock again
        noDrawDeleteConflicts.acquireUninterruptibly();
        verticesObject = wip;
        currentData = newData;
    // And we're good !
    } else // Already a VerticesObject present hum, we create another one then delete the old one
    {
        VertexBuffer wip = worldRenderer.getRenderingInterface().newVertexBuffer();
        Fence fence = wip.uploadData(newData.dataToUpload);
        // We unlock while waiting for the upload
        noDrawDeleteConflicts.release();
        fence.traverse();
        // Then we lock again
        noDrawDeleteConflicts.acquireUninterruptibly();
        // We delete the OLD one
        verticesObject.destroy();
        // We swap the new one in
        verticesObject = wip;
        currentData = newData;
    }
    newData.consumed();
    noDrawDeleteConflicts.release();
    oneUploadAtATime.release();
}
Also used : VertexBuffer(io.xol.chunkstories.api.rendering.vertex.VertexBuffer) Fence(io.xol.chunkstories.api.util.concurrency.Fence)

Example 3 with Fence

use of io.xol.chunkstories.api.util.concurrency.Fence 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 4 with Fence

use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.

the class TaskBuildHeightmap method task.

@Override
protected boolean task(TaskExecutor taskExecutor) {
    ConverterWorkerThread cwt = (ConverterWorkerThread) taskExecutor;
    // We wait on a bunch of stuff to load everytime
    CompoundFence loadRelevantData = new CompoundFence();
    HeightmapImplementation summary = csWorld.getRegionsSummariesHolder().aquireHeightmap(cwt, regionX, regionZ);
    loadRelevantData.add(summary.waitForLoading());
    // Aquires the chunks we want to make the summaries of.
    for (int innerCX = 0; innerCX < 8; innerCX++) for (int innerCZ = 0; innerCZ < 8; innerCZ++) for (int chunkY = 0; chunkY < OfflineWorldConverter.mcWorldHeight / 32; chunkY++) {
        ChunkHolder holder = csWorld.aquireChunkHolder(cwt, regionX * 8 + innerCX, chunkY, regionZ * 8 + innerCZ);
        if (holder != null) {
            loadRelevantData.add(holder.waitForLoading());
            if (cwt.registeredCS_Holders.add(holder))
                cwt.chunksAquired++;
        }
    }
    // Wait until all of that crap loads
    loadRelevantData.traverse();
    // Descend from top
    for (int i = 0; i < 256; i++) for (int j = 0; j < 256; j++) {
        for (int h = OfflineWorldConverter.mcWorldHeight; h > 0; h--) {
            CellData data = csWorld.peekSafely(regionX * 256 + i, h, regionZ * 256 + j);
            if (!data.getVoxel().isAir()) {
                Voxel vox = data.getVoxel();
                if (vox.getDefinition().isSolid() || vox.getDefinition().isLiquid()) {
                    summary.setTopCell(data);
                    break;
                }
            }
        }
    }
    Fence waitForSummarySave = summary.saveSummary();
    // cwt.converter().verbose("Waiting for summary saving...");
    waitForSummarySave.traverse();
    // cwt.converter().verbose("Done.");
    // We don't need the summary anymore
    summary.unregisterUser(cwt);
    return true;
}
Also used : CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder) Voxel(io.xol.chunkstories.api.voxel.Voxel) ConverterWorkerThread(io.xol.chunkstories.converter.ConverterWorkers.ConverterWorkerThread) Fence(io.xol.chunkstories.api.util.concurrency.Fence) CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) CellData(io.xol.chunkstories.api.world.cell.CellData)

Example 5 with Fence

use of io.xol.chunkstories.api.util.concurrency.Fence in project chunkstories by Hugobros3.

the class Ingame method destroy.

@Override
public void destroy() {
    // Logout sequence: Save the player entity
    if (world instanceof WorldMaster) {
        Player player = getPlayer();
        PlayerLogoutEvent playerDisconnectionEvent = new PlayerLogoutEvent(player);
        world.getPluginManager().fireEvent(playerDisconnectionEvent);
        if (this.playerEntity != null) {
            SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + getPlayer().getName().toLowerCase() + ".csf");
            playerEntityFile.write(this.playerEntity);
        }
    }
    // Stop the game logic and save
    if (world instanceof WorldMaster) {
        // TODO: Stop simulation
        Fence fence = ((WorldMaster) world).stopLogic();
        // exitButton.text = "#{world.saving}";
        fence.traverse();
        fence = world.saveEverything();
        // exitButton.text = "#{world.saving}";
        fence.traverse();
    }
    // Disables plugins
    world.getPluginManager().disablePlugins();
    this.world.getWorldRenderer().destroy();
}
Also used : Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) PlayerLogoutEvent(io.xol.chunkstories.api.events.player.PlayerLogoutEvent) SerializedEntityFile(io.xol.chunkstories.entity.SerializedEntityFile) Fence(io.xol.chunkstories.api.util.concurrency.Fence) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Aggregations

Fence (io.xol.chunkstories.api.util.concurrency.Fence)6 ChunkHolder (io.xol.chunkstories.api.world.chunk.ChunkHolder)2 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)2 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 OnlineContentTranslator (io.xol.chunkstories.api.content.OnlineContentTranslator)1 PlayerLogoutEvent (io.xol.chunkstories.api.events.player.PlayerLogoutEvent)1 Player (io.xol.chunkstories.api.player.Player)1 VertexBuffer (io.xol.chunkstories.api.rendering.vertex.VertexBuffer)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 WorldSize (io.xol.chunkstories.api.world.WorldInfo.WorldSize)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 WorldUser (io.xol.chunkstories.api.world.WorldUser)1 CellData (io.xol.chunkstories.api.world.cell.CellData)1 Heightmap (io.xol.chunkstories.api.world.heightmap.Heightmap)1 Client (io.xol.chunkstories.client.Client)1 ClientPacketsContext (io.xol.chunkstories.client.net.ClientPacketsContext)1 ConverterWorkerThread (io.xol.chunkstories.converter.ConverterWorkers.ConverterWorkerThread)1 SerializedEntityFile (io.xol.chunkstories.entity.SerializedEntityFile)1 ConnectionOverlay (io.xol.chunkstories.gui.layer.ingame.ConnectionOverlay)1 Ingame (io.xol.chunkstories.gui.layer.ingame.Ingame)1