Search in sources :

Example 1 with MinecraftChunk

use of io.xol.enklume.MinecraftChunk 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)1 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)1 MinecraftChunk (io.xol.enklume.MinecraftChunk)1 MinecraftRegion (io.xol.enklume.MinecraftRegion)1 IOException (java.io.IOException)1