Search in sources :

Example 1 with WorldSize

use of io.xol.chunkstories.api.world.WorldInfo.WorldSize 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 2 with WorldSize

use of io.xol.chunkstories.api.world.WorldInfo.WorldSize in project chunkstories by Hugobros3.

the class MultithreadedOfflineWorldConverter method stepTwoCreateSummaryData.

protected void stepTwoCreateSummaryData(WorldTool csWorld) {
    verbose("Entering step two: making summary data");
    WorldSize size = csWorld.getWorldInfo().getSize();
    int done = 0;
    int todo = (size.sizeInChunks / 8) * (size.sizeInChunks / 8);
    double completion = 0.0;
    long lastPercentageShow = System.currentTimeMillis();
    int wavesSize = this.threadsCount * 4;
    int wave = 0;
    CompoundFence compoundFence = new CompoundFence();
    for (int regionX = 0; regionX < size.sizeInChunks / 8; regionX++) {
        for (int regionZ = 0; regionZ < size.sizeInChunks / 8; regionZ++) {
            TaskBuildHeightmap task = new TaskBuildHeightmap(regionX, regionZ, csWorld);
            workers.scheduleTask(task);
            compoundFence.traverse();
            if (wave < wavesSize) {
                wave++;
            } else {
                compoundFence.traverse();
                compoundFence.clear();
                wave = 0;
                done += wavesSize;
                // Display progress...
                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();
                    }
                }
                // Drop all unsued chunk data
                workers.dropAll();
                csWorld.unloadUselessData().traverse();
            }
        }
    }
    compoundFence.traverse();
    // Drop all unsued chunk data
    workers.dropAll();
    verbose("Saving unused chunk data...");
    csWorld.unloadUselessData().traverse();
    verbose("Done.");
}
Also used : CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) WorldSize(io.xol.chunkstories.api.world.WorldInfo.WorldSize)

Example 3 with WorldSize

use of io.xol.chunkstories.api.world.WorldInfo.WorldSize in project chunkstories by Hugobros3.

the class MultithreadedOfflineWorldConverter method stepOneCopyWorldData.

protected void stepOneCopyWorldData(MinecraftWorld mcWorld, WorldImplementation csWorld, int minecraftOffsetX, int minecraftOffsetZ) {
    verbose("Entering step one: converting raw block data");
    // Prepares the loops
    WorldSize size = csWorld.getWorldInfo().getSize();
    int mcRegionStartX = MinecraftWorld.blockToRegionCoordinates(minecraftOffsetX);
    int mcRegionStartZ = MinecraftWorld.blockToRegionCoordinates(minecraftOffsetZ);
    int mcRegionEndX = MinecraftWorld.blockToRegionCoordinates(minecraftOffsetX + size.sizeInChunks * 32);
    int mcRegionEndZ = MinecraftWorld.blockToRegionCoordinates(minecraftOffsetZ + size.sizeInChunks * 32);
    int minecraftChunksImported = 0;
    long minecraftChunksToImport = ((long) (size.sizeInChunks * 32) * (long) (size.sizeInChunks * 32)) / (16 * 16);
    // System.out.println(size + " " + size.sizeInChunks + " " + minecraftChunksToImport);
    double completion = 0.0;
    long lastPercentageShow = System.currentTimeMillis();
    try {
        // We do this minecraft region per minecraft region.
        for (int minecraftRegionX = mcRegionStartX; minecraftRegionX < mcRegionEndX; minecraftRegionX++) {
            for (int minecraftRegionZ = mcRegionStartZ; minecraftRegionZ < mcRegionEndZ; minecraftRegionZ++) {
                // Load the culprit (There isn't any fancy world management, the getRegion()
                // actually loads the entire region file)
                MinecraftRegion minecraftRegion = mcWorld.getRegion(minecraftRegionX, minecraftRegionZ);
                CompoundFence waitForTheBoys = new CompoundFence();
                // TODO Good candidate for task-ifying
                for (int minecraftCurrentChunkXinsideRegion = 0; minecraftCurrentChunkXinsideRegion < 32; minecraftCurrentChunkXinsideRegion++) {
                    for (int minecraftCuurrentChunkZinsideRegion = 0; minecraftCuurrentChunkZinsideRegion < 32; minecraftCuurrentChunkZinsideRegion++) {
                        // Map minecraft chunk-space to chunk stories's
                        int chunkStoriesCurrentChunkX = (minecraftCurrentChunkXinsideRegion + minecraftRegionX * 32) * 16 - minecraftOffsetX;
                        int chunkStoriesCurrentChunkZ = (minecraftCuurrentChunkZinsideRegion + minecraftRegionZ * 32) * 16 - minecraftOffsetZ;
                        // Is it within our borders ?
                        if (chunkStoriesCurrentChunkX >= 0 && chunkStoriesCurrentChunkX < csWorld.getWorldInfo().getSize().sizeInChunks * 32 && chunkStoriesCurrentChunkZ >= 0 && chunkStoriesCurrentChunkZ < csWorld.getWorldInfo().getSize().sizeInChunks * 32) {
                            if (minecraftRegion != null) {
                                MinecraftChunk minecraftChunk = minecraftRegion.getChunk(minecraftCurrentChunkXinsideRegion, minecraftCuurrentChunkZinsideRegion);
                                TaskConvertMcChunk task = new TaskConvertMcChunk(minecraftRegion, minecraftChunk, chunkStoriesCurrentChunkX, chunkStoriesCurrentChunkZ, minecraftCurrentChunkXinsideRegion, minecraftCuurrentChunkZinsideRegion, minecraftRegionX, minecraftRegionZ, mappers);
                                workers.scheduleTask(task);
                                waitForTheBoys.add(task);
                            }
                        }
                    }
                }
                // System.out.println("Waiting on "+waitForTheBoys.size() + " async tasks...");
                // This code is mysoginistic, hang it
                waitForTheBoys.traverse();
                workers.dropAll();
                csWorld.unloadUselessData().traverse();
                // Close region
                if (minecraftRegion != null)
                    minecraftRegion.close();
                System.gc();
                // Display progress
                minecraftChunksImported += 32 * 32;
                if (Math.floor(((double) minecraftChunksImported / (double) minecraftChunksToImport) * 100) > completion) {
                    completion = Math.floor(((double) minecraftChunksImported / (double) minecraftChunksToImport) * 100);
                    if (completion >= 100.0 || (System.currentTimeMillis() - lastPercentageShow > 5000)) {
                        verbose(completion + "% ... (" + csWorld.getRegionsHolder().countChunks() + " chunks loaded ) using " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + "/" + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "Mb ");
                        lastPercentageShow = System.currentTimeMillis();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    csWorld.unloadUselessData();
}
Also used : CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) MinecraftRegion(io.xol.enklume.MinecraftRegion) MinecraftChunk(io.xol.enklume.MinecraftChunk) WorldSize(io.xol.chunkstories.api.world.WorldInfo.WorldSize) IOException(java.io.IOException)

Aggregations

WorldSize (io.xol.chunkstories.api.world.WorldInfo.WorldSize)3 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)3 Fence (io.xol.chunkstories.api.util.concurrency.Fence)1 WorldUser (io.xol.chunkstories.api.world.WorldUser)1 ChunkHolder (io.xol.chunkstories.api.world.chunk.ChunkHolder)1 Heightmap (io.xol.chunkstories.api.world.heightmap.Heightmap)1 CubicChunk (io.xol.chunkstories.world.chunk.CubicChunk)1 MinecraftChunk (io.xol.enklume.MinecraftChunk)1 MinecraftRegion (io.xol.enklume.MinecraftRegion)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1