Search in sources :

Example 31 with Layer

use of org.pepsoft.worldpainter.layers.Layer in project WorldPainter by Captain-Chaos.

the class FillDialog method invertLayer.

private void invertLayer(ProgressReceiver progressReceiver) throws UnsupportedOperationException, OperationCancelled {
    Layer layer = (Layer) comboBoxInvertLayer.getSelectedItem();
    if (layer.getDataSize() == Layer.DataSize.NIBBLE) {
        dimension.visitTiles().forFilter(filter).andDo(tile -> {
            final int worldTileX = tile.getX() << TILE_SIZE_BITS;
            final int worldTileY = tile.getY() << TILE_SIZE_BITS;
            for (int x = 0; x < TILE_SIZE; x++) {
                for (int y = 0; y < TILE_SIZE; y++) {
                    boolean set;
                    if (filter == null) {
                        set = true;
                    } else {
                        float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
                        set = (strength > 0.95f) || (Math.random() < strength);
                    }
                    if (set) {
                        tile.setLayerValue(layer, x, y, 15 - tile.getLayerValue(layer, x, y));
                    }
                }
            }
        }, progressReceiver);
    } else if (layer.getDataSize() == Layer.DataSize.BIT) {
        dimension.visitTiles().forFilter(filter).andDo(tile -> {
            final int worldTileX = tile.getX() << TILE_SIZE_BITS;
            final int worldTileY = tile.getY() << TILE_SIZE_BITS;
            for (int x = 0; x < TILE_SIZE; x++) {
                for (int y = 0; y < TILE_SIZE; y++) {
                    boolean set;
                    if (filter == null) {
                        set = true;
                    } else {
                        float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
                        set = (strength > 0.95f) || (Math.random() < strength);
                    }
                    if (set) {
                        tile.setBitLayerValue(layer, x, y, !tile.getBitLayerValue(layer, x, y));
                    }
                }
            }
        }, progressReceiver);
    } else if (layer.getDataSize() == Layer.DataSize.BIT_PER_CHUNK) {
        dimension.visitTiles().forFilter(filter).andDo(tile -> {
            final int worldTileX = tile.getX() << TILE_SIZE_BITS;
            final int worldTileY = tile.getY() << TILE_SIZE_BITS;
            for (int x = 0; x < TILE_SIZE; x += 16) {
                for (int y = 0; y < TILE_SIZE; y += 16) {
                    boolean set;
                    if (filter == null) {
                        set = true;
                    } else {
                        float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
                        set = (strength > 0.95f) || (Math.random() < strength);
                    }
                    if (set) {
                        tile.setBitLayerValue(layer, x, y, !tile.getBitLayerValue(layer, x, y));
                    }
                }
            }
        }, progressReceiver);
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : CustomBiomeManager(org.pepsoft.worldpainter.biomeschemes.CustomBiomeManager) BiomeHelper(org.pepsoft.worldpainter.biomeschemes.BiomeHelper) Arrays(java.util.Arrays) AutoBiomeScheme(org.pepsoft.worldpainter.biomeschemes.AutoBiomeScheme) Collection(java.util.Collection) Biome(org.pepsoft.worldpainter.layers.Biome) ProgressDialog(org.pepsoft.util.swing.ProgressDialog) Listener(org.pepsoft.worldpainter.panels.BrushOptions.Listener) Set(java.util.Set) DefaultFilter(org.pepsoft.worldpainter.panels.DefaultFilter) TILE_SIZE(org.pepsoft.worldpainter.Constants.TILE_SIZE) Layer(org.pepsoft.worldpainter.layers.Layer) TerrainListCellRenderer(org.pepsoft.worldpainter.themes.TerrainListCellRenderer) ProgressTask(org.pepsoft.util.swing.ProgressTask) FloodWithLava(org.pepsoft.worldpainter.layers.FloodWithLava) ObservableBoolean(org.pepsoft.util.ObservableBoolean) ProgressReceiver(org.pepsoft.util.ProgressReceiver) SelectionBlock(org.pepsoft.worldpainter.selection.SelectionBlock) OperationCancelled(org.pepsoft.util.ProgressReceiver.OperationCancelled) SelectionChunk(org.pepsoft.worldpainter.selection.SelectionChunk) SelectionHelper(org.pepsoft.worldpainter.selection.SelectionHelper) TILE_SIZE_BITS(org.pepsoft.worldpainter.Constants.TILE_SIZE_BITS) Filter(org.pepsoft.worldpainter.operations.Filter) javax.swing(javax.swing) Layer(org.pepsoft.worldpainter.layers.Layer)

Example 32 with Layer

use of org.pepsoft.worldpainter.layers.Layer in project WorldPainter by Captain-Chaos.

the class ImportMaskDialog method setControlStates.

private void setControlStates() {
    File file = new File(fieldFilename.getText());
    if (file.isFile() && ((selectedFile == null) || (!file.equals(selectedFile)))) {
        selectedFile = file;
        loadImage();
    }
    boolean fileSelected = (selectedFile != null) && selectedFile.isFile();
    radioButtonLayer.setEnabled(fileSelected);
    if (!fileSelected) {
        // loadImage() will enable it when necessary
        radioButtonTerrain.setEnabled(false);
    }
    boolean targetSelected = (radioButtonLayer.isSelected() && (comboBoxLayer.getSelectedItem() != null)) || radioButtonTerrain.isSelected();
    if (radioButtonTerrain.isEnabled() && radioButtonTerrain.isSelected()) {
        maskImporter.setApplyToTerrain(true);
    } else if (radioButtonLayer.isEnabled() && radioButtonLayer.isSelected() && comboBoxLayer.getSelectedItem() != null) {
        maskImporter.setApplyToLayer((Layer) comboBoxLayer.getSelectedItem());
    }
    radioButtonOneToOne.setEnabled(fileSelected && targetSelected && maskImporter.getPossibleMappings().contains(ONE_TO_ONE));
    radioButtonThreshold.setEnabled(fileSelected && targetSelected && maskImporter.getPossibleMappings().contains(THRESHOLD));
    radioButtonDither.setEnabled(fileSelected && targetSelected && maskImporter.getPossibleMappings().contains(DITHERING));
    radioButtonFullRange.setEnabled(fileSelected && targetSelected && maskImporter.getPossibleMappings().contains(FULL_RANGE));
    spinnerThreshold.setEnabled(fileSelected && radioButtonThreshold.isSelected());
    comboBoxLayer.setEnabled(fileSelected && radioButtonLayer.isSelected());
    checkBoxRemoveExisting.setEnabled(fileSelected && radioButtonLayer.isSelected());
    boolean mappingSelected = (radioButtonOneToOne.isEnabled() && radioButtonOneToOne.isSelected()) || (radioButtonThreshold.isEnabled() && radioButtonThreshold.isSelected()) || (radioButtonDither.isEnabled() && radioButtonDither.isSelected()) || (radioButtonFullRange.isEnabled() && radioButtonFullRange.isSelected());
    buttonOk.setEnabled(fileSelected && targetSelected && mappingSelected);
}
Also used : File(java.io.File) Layer(org.pepsoft.worldpainter.layers.Layer)

Example 33 with Layer

use of org.pepsoft.worldpainter.layers.Layer in project WorldPainter by Captain-Chaos.

the class NewWorldDialog method getSelectedDimension.

public Dimension getSelectedDimension(final ProgressReceiver progressReceiver) throws ProgressReceiver.OperationCancelled {
    long minecraftSeed;
    try {
        minecraftSeed = Long.parseLong(fieldSeed.getText());
    } catch (NumberFormatException e) {
        minecraftSeed = fieldSeed.getText().hashCode();
    }
    if (radioButtonCustomSeed.isSelected()) {
        worldpainterSeed = minecraftSeed;
    }
    int waterHeight = (Integer) spinnerWaterLevel.getValue();
    final TileFactory tileFactory = createTileFactory(worldpainterSeed);
    int maxHeight = (Integer) comboBoxMaxHeight.getSelectedItem();
    final Dimension dimension = new Dimension(minecraftSeed, tileFactory, dim, maxHeight);
    dimension.setEventsInhibited(true);
    try {
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        final AtomicBoolean cancelled = new AtomicBoolean();
        if (tiles != null) {
            logger.info("Creating new dimension with " + tiles.size() + " preselected tiles");
            final int[] tileCount = new int[1];
            final int totalTiles = tiles.size();
            for (final Point tileCoords : tiles) {
                executorService.execute(() -> {
                    if (cancelled.get()) {
                        // Operation cancelled by user
                        return;
                    }
                    Tile tile = tileFactory.createTile(tileCoords.x, tileCoords.y);
                    dimension.addTile(tile);
                    if (progressReceiver != null) {
                        synchronized (tileCount) {
                            tileCount[0]++;
                            try {
                                progressReceiver.setProgress((float) tileCount[0] / totalTiles);
                            } catch (ProgressReceiver.OperationCancelled e) {
                                cancelled.set(true);
                            }
                        }
                    }
                });
            }
        } else if (checkBoxCircular.isSelected()) {
            final int radius = (Integer) spinnerWidth.getValue() / 2;
            int diameter = radius * 2;
            logger.info("Creating new circular dimension with diameter " + diameter + " blocks");
            int tileRadius = (radius + 127) / 128;
            final int[] tileCount = new int[1];
            final int approximateTotalTiles = (int) Math.ceil(Math.PI * tileRadius * tileRadius);
            for (int x = -tileRadius; x < tileRadius; x++) {
                for (int y = -tileRadius; y < tileRadius; y++) {
                    if (org.pepsoft.worldpainter.util.MathUtils.getSmallestDistanceFromOrigin(x, y) < radius) {
                        // At least one corner is inside the circle; include
                        // the tile. Note that this is always correct in
                        // this case only because the centre of the circle
                        // is always at a tile intersection so the circle
                        // can never "bulge" into a tile without any of the
                        // the tile's corners being inside the circle
                        final int tileX = x, tileY = y;
                        executorService.execute(() -> {
                            if (cancelled.get()) {
                                // Operation cancelled by user
                                return;
                            }
                            Tile tile = tileFactory.createTile(tileX, tileY);
                            dimension.addTile(tile);
                            if (org.pepsoft.worldpainter.util.MathUtils.getLargestDistanceFromOrigin(tileX, tileY) >= radius) {
                                // the edge
                                for (int xx = 0; xx < TILE_SIZE; xx++) {
                                    for (int yy = 0; yy < TILE_SIZE; yy++) {
                                        float distance = MathUtils.getDistance(tileX * TILE_SIZE + xx + 0.5f, tileY * TILE_SIZE + yy + 0.5f);
                                        if (distance > radius) {
                                            tile.setBitLayerValue(org.pepsoft.worldpainter.layers.Void.INSTANCE, xx, yy, true);
                                        }
                                    }
                                }
                            }
                            if (progressReceiver != null) {
                                synchronized (tileCount) {
                                    tileCount[0]++;
                                    try {
                                        // System.out.println("Progress: " + tileCount[0] + " of " + approximateTotalTiles + " (" + ((float) tileCount[0] / approximateTotalTiles) + ")");
                                        progressReceiver.setProgress(Math.min((float) tileCount[0] / approximateTotalTiles, 1.0f));
                                    } catch (ProgressReceiver.OperationCancelled e) {
                                        cancelled.set(true);
                                    }
                                }
                            }
                        });
                    }
                }
            }
            // Assume the user will want an endless void border by default;
            // override the preferences
            dimension.setBorder(Border.ENDLESS_VOID);
        } else {
            int width = ((Integer) spinnerWidth.getValue()) / 128;
            int height = ((Integer) spinnerLength.getValue()) / 128;
            logger.info("Creating new dimension of size " + width + "x" + height + " for a total of " + width * height + " tiles");
            final int[] tileCount = new int[1];
            final int totalTiles = width * height;
            int startX = -width / 2;
            int startY = -height / 2;
            for (int x = startX; x < startX + width; x++) {
                for (int y = startY; y < startY + height; y++) {
                    final int tileX = x, tileY = y;
                    executorService.execute(() -> {
                        if (cancelled.get()) {
                            // Operation cancelled by user
                            return;
                        }
                        Tile tile = tileFactory.createTile(tileX, tileY);
                        dimension.addTile(tile);
                        if (progressReceiver != null) {
                            synchronized (tileCount) {
                                tileCount[0]++;
                                try {
                                    progressReceiver.setProgress((float) tileCount[0] / totalTiles);
                                } catch (ProgressReceiver.OperationCancelled e) {
                                    cancelled.set(true);
                                }
                            }
                        }
                    });
                }
            }
        }
        // Wait for all tiles to be created
        executorService.shutdown();
        try {
            executorService.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            throw new RuntimeException("Thread interrupted", e);
        }
        if (cancelled.get()) {
            // The operation was cancelled by the user
            return null;
        }
        if (dim == DIM_NORMAL_CEILING) {
            ResourcesExporter.ResourcesExporterSettings resourcesSettings = new ResourcesExporter.ResourcesExporterSettings(maxHeight);
            // Invert min and max levels:
            int maxZ = maxHeight - 1;
            for (int blockType : resourcesSettings.getBlockTypes()) {
                int low = resourcesSettings.getMinLevel(blockType);
                int high = resourcesSettings.getMaxLevel(blockType);
                resourcesSettings.setMinLevel(blockType, maxZ - high);
                resourcesSettings.setMaxLevel(blockType, maxZ - low);
            }
            // Remove lava and water:
            resourcesSettings.setChance(BLK_WATER, 0);
            resourcesSettings.setChance(BLK_LAVA, 0);
            dimension.setLayerSettings(Resources.INSTANCE, resourcesSettings);
        } else if (dim == DIM_NETHER) {
            dimension.setSubsurfaceMaterial(NETHERLIKE);
            CavernsSettings cavernsSettings = new CavernsSettings();
            cavernsSettings.setCavernsEverywhereLevel(16);
            cavernsSettings.setSurfaceBreaking(true);
            cavernsSettings.setFloodWithLava(true);
            cavernsSettings.setWaterLevel(16);
            dimension.setLayerSettings(Caverns.INSTANCE, cavernsSettings);
        } else if (dim == DIM_NETHER_CEILING) {
            dimension.setSubsurfaceMaterial(NETHERLIKE);
        } else if ((dim == DIM_END) || (dim == DIM_END_CEILING)) {
            dimension.setSubsurfaceMaterial(END_STONE);
        }
        Configuration config = Configuration.getInstance();
        Dimension defaults = config.getDefaultTerrainAndLayerSettings();
        if (dim == DIM_NORMAL) {
            if (!checkBoxCircular.isSelected()) {
                dimension.setBorder(defaults.getBorder());
                dimension.setBorderSize(defaults.getBorderSize());
            }
            dimension.setBedrockWall(defaults.isBedrockWall());
            dimension.setSubsurfaceMaterial(defaults.getSubsurfaceMaterial());
            dimension.setPopulate(defaults.isPopulate());
            dimension.setTopLayerMinDepth(defaults.getTopLayerMinDepth());
            dimension.setTopLayerVariation(defaults.getTopLayerVariation());
            dimension.setBottomless(defaults.isBottomless());
            for (Map.Entry<Layer, ExporterSettings> entry : defaults.getAllLayerSettings().entrySet()) {
                dimension.setLayerSettings(entry.getKey(), entry.getValue().clone());
            }
        }
        dimension.setBorderLevel(waterHeight);
        dimension.setCoverSteepTerrain(defaults.isCoverSteepTerrain());
        dimension.setGridEnabled(config.isDefaultGridEnabled());
        dimension.setGridSize(config.getDefaultGridSize());
        dimension.setContoursEnabled(config.isDefaultContoursEnabled());
        dimension.setContourSeparation(config.getDefaultContourSeparation());
    } finally {
        dimension.setEventsInhibited(false);
    }
    dimension.setDirty(false);
    return dimension;
}
Also used : ExporterSettings(org.pepsoft.worldpainter.layers.exporters.ExporterSettings) CavernsSettings(org.pepsoft.worldpainter.layers.exporters.CavernsExporter.CavernsSettings) ResourcesExporter(org.pepsoft.worldpainter.layers.exporters.ResourcesExporter) Layer(org.pepsoft.worldpainter.layers.Layer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) ProgressReceiver(org.pepsoft.util.ProgressReceiver)

Example 34 with Layer

use of org.pepsoft.worldpainter.layers.Layer in project WorldPainter by Captain-Chaos.

the class WorldFactory method createFancyWorld.

public static World2 createFancyWorld(final Configuration config, final long seed) {
    final HeightMapTileFactory tileFactory = TileFactoryFactory.createFancyTileFactory(seed, Terrain.GRASS, DEFAULT_MAX_HEIGHT_2, 58, 62, false, 20f, 1.0);
    final Dimension defaults = config.getDefaultTerrainAndLayerSettings();
    final World2 world = new World2(JAVA_ANVIL, World2.DEFAULT_OCEAN_SEED, tileFactory, tileFactory.getMaxHeight());
    world.addHistoryEntry(HistoryEntry.WORLD_CREATED);
    if (config.getDefaultMaxHeight() == DEFAULT_MAX_HEIGHT_2) {
        world.setGenerator(Generator.LARGE_BIOMES);
    }
    world.setMixedMaterial(0, new MixedMaterial("Dirt/Gravel", new Row[] { new Row(Material.DIRT, 750, 1.0f), new Row(Material.GRAVEL, 250, 1.0f) }, Minecraft1_2BiomeScheme.BIOME_PLAINS, null, 1.0f));
    world.setMixedMaterial(1, new MixedMaterial("Stone/Gravel", new Row[] { new Row(Material.STONE, 750, 1.0f), new Row(Material.GRAVEL, 250, 1.0f) }, Minecraft1_2BiomeScheme.BIOME_PLAINS, null, 1.0f));
    final ResourceBundle strings = ResourceBundle.getBundle("org.pepsoft.worldpainter.resources.strings");
    world.setName(strings.getString("generated.world"));
    final Dimension dim0 = world.getDimension(0);
    final boolean circularWorld = true;
    final int radius = 750;
    dim0.setEventsInhibited(true);
    try {
        if (circularWorld) {
            final int tileRadius = (radius + 127) / 128;
            for (int x = -tileRadius; x < tileRadius; x++) {
                for (int y = -tileRadius; y < tileRadius; y++) {
                    if (getSmallestDistanceFromOrigin(x, y) < radius) {
                        // At least one corner is inside the circle; include
                        // the tile. Note that this is always correct in
                        // this case only because the centre of the circle
                        // is always at a tile intersection so the circle
                        // can never "bulge" into a tile without any of the
                        // the tile's corners being inside the circle
                        final Tile tile = tileFactory.createTile(x, y);
                        dim0.addTile(tile);
                    // if (org.pepsoft.worldpainter.util.MathUtils.getLargestDistanceFromOrigin(x, y) >= radius) {
                    // // The tile is not completely inside the circle,
                    // // so use the Void layer to create the shape of
                    // // the edge
                    // for (int xx = 0; xx < TILE_SIZE; xx++) {
                    // for (int yy = 0; yy < TILE_SIZE; yy++) {
                    // float distance = MathUtils.getDistance(x * TILE_SIZE + xx + 0.5f, y * TILE_SIZE + yy + 0.5f);
                    // if (distance > radius) {
                    // tile.setBitLayerValue(org.pepsoft.worldpainter.layers.Void.INSTANCE, xx, yy, true);
                    // }
                    // }
                    // }
                    // }
                    }
                }
            }
        // Assume the user will want a void border by default; override
        // the preferences
        // dim0.setBorder(Dimension.Border.VOID);
        // dim0.setBorderSize(2);
        } else {
            final int width = config.getDefaultWidth(), height = config.getDefaultHeight();
            final int startX = -width / 2;
            final int startY = -height / 2;
            for (int x = startX; x < startX + width; x++) {
                for (int y = startY; y < startY + height; y++) {
                    final Tile tile = tileFactory.createTile(x, y);
                    dim0.addTile(tile);
                }
            }
            dim0.setBorder(defaults.getBorder());
            dim0.setBorderSize(defaults.getBorderSize());
            dim0.setBedrockWall(defaults.isBedrockWall());
        }
        dim0.setBorderLevel(defaults.getBorderLevel());
        dim0.setSubsurfaceMaterial(defaults.getSubsurfaceMaterial());
        dim0.setPopulate(defaults.isPopulate());
        for (Map.Entry<Layer, ExporterSettings> entry : defaults.getAllLayerSettings().entrySet()) {
            dim0.setLayerSettings(entry.getKey(), entry.getValue().clone());
        }
        FrostExporter.FrostSettings frostSettings = (FrostExporter.FrostSettings) dim0.getLayerSettings(Frost.INSTANCE);
        if (frostSettings == null) {
            frostSettings = new FrostExporter.FrostSettings();
        }
        frostSettings.setMode(frostSettings.MODE_SMOOTH_AT_ALL_ELEVATIONS);
        dim0.setLayerSettings(Frost.INSTANCE, frostSettings);
        dim0.setGridEnabled(config.isDefaultGridEnabled());
        dim0.setGridSize(config.getDefaultGridSize());
        dim0.setContoursEnabled(config.isDefaultContoursEnabled());
        dim0.setContourSeparation(config.getDefaultContourSeparation());
        dim0.setTopLayerMinDepth(defaults.getTopLayerMinDepth());
        dim0.setTopLayerVariation(defaults.getTopLayerVariation());
        dim0.setBottomless(defaults.isBottomless());
    } finally {
        dim0.setEventsInhibited(false);
    }
    // final RiverGenerator riverGenerator = new RiverGenerator(dim0);
    // riverGenerator.generateRivers();
    world.setDirty(false);
    return world;
}
Also used : FrostExporter(org.pepsoft.worldpainter.layers.exporters.FrostExporter) ExporterSettings(org.pepsoft.worldpainter.layers.exporters.ExporterSettings) Layer(org.pepsoft.worldpainter.layers.Layer) ResourceBundle(java.util.ResourceBundle) Row(org.pepsoft.worldpainter.MixedMaterial.Row) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

Layer (org.pepsoft.worldpainter.layers.Layer)34 CustomLayer (org.pepsoft.worldpainter.layers.CustomLayer)9 Map (java.util.Map)8 ProgressReceiver (org.pepsoft.util.ProgressReceiver)7 HashMap (java.util.HashMap)6 OperationCancelled (org.pepsoft.util.ProgressReceiver.OperationCancelled)5 ExporterSettings (org.pepsoft.worldpainter.layers.exporters.ExporterSettings)5 Point (java.awt.Point)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SubProgressReceiver (org.pepsoft.util.SubProgressReceiver)4 TILE_SIZE (org.pepsoft.worldpainter.Constants.TILE_SIZE)4 Biome (org.pepsoft.worldpainter.layers.Biome)4 Bo2Layer (org.pepsoft.worldpainter.layers.Bo2Layer)4 CombinedLayer (org.pepsoft.worldpainter.layers.CombinedLayer)4 GroundCoverLayer (org.pepsoft.worldpainter.layers.groundcover.GroundCoverLayer)4 PlantLayer (org.pepsoft.worldpainter.layers.plants.PlantLayer)4 File (java.io.File)3 Arrays (java.util.Arrays)3 BitSet (java.util.BitSet)3