Search in sources :

Example 6 with PerlinNoise

use of org.pepsoft.util.PerlinNoise in project WorldPainter by Captain-Chaos.

the class TreesExporter method renderMushrooms.

private void renderMushrooms(int blockInWorldX, int blockInWorldY, int height, int strength, MinecraftWorld minecraftWorld, Random random, long seed) {
    if (height > (minecraftWorld.getMaxHeight() - 2)) {
        return;
    }
    TreeLayerSettings<T> settings = (TreeLayerSettings<T>) getSettings();
    int mushroomIncidence = settings.getMushroomIncidence();
    float mushroomChance = settings.getMushroomChance();
    PerlinNoise perlinNoise = perlinNoiseRef.get();
    if (perlinNoise == null) {
        perlinNoise = new PerlinNoise(seed);
        perlinNoiseRef.set(perlinNoise);
    }
    if (perlinNoise.getSeed() != (seed + SEED_OFFSET)) {
        perlinNoise.setSeed(seed + SEED_OFFSET);
    }
    int size = Math.min(2 + strength / 3, 5) + random.nextInt(3);
    int r = Math.min(size / 2, 3);
    if (r > 0) {
        for (int dx = -r; dx <= r; dx++) {
            for (int dy = -r; dy <= r; dy++) {
                if ((dx != 0) || (dy != 0)) {
                    int rnd = random.nextInt(mushroomIncidence);
                    int x = blockInWorldX + dx, y = blockInWorldY + dy;
                    if ((rnd == 0) && (minecraftWorld.getBlockTypeAt(x, y, height) != BLK_AIR) && (minecraftWorld.getBlockTypeAt(x, y, height + 1) == BLK_AIR)) {
                        float chance = perlinNoise.getPerlinNoise(x / SMALL_BLOBS, y / SMALL_BLOBS, height / SMALL_BLOBS);
                        if (chance > mushroomChance) {
                            minecraftWorld.setBlockTypeAt(x, y, height + 1, BLK_BROWN_MUSHROOM);
                        } else if (chance < -mushroomChance) {
                            minecraftWorld.setBlockTypeAt(x, y, height + 1, BLK_RED_MUSHROOM);
                        }
                    }
                }
            }
        }
    }
}
Also used : PerlinNoise(org.pepsoft.util.PerlinNoise)

Example 7 with PerlinNoise

use of org.pepsoft.util.PerlinNoise in project WorldPainter by Captain-Chaos.

the class TunnelLayerExporter method generatePreview.

// private void excavateDisc(final MinecraftWorld world, final int x, final int y, final int z, int r, final Material materialAbove, final Material materialBesides, final Material materialBelow) {
// GeometryUtil.visitFilledCircle(r, new PlaneVisitor() {
// @Override
// public boolean visit(int dx, int dy, float d) {
// world.setMaterialAt(x + dx, y + dy, z, Material.AIR);
// if (materialAbove != null) {
// setIfSolid(world, x + dx, y + dy, z - 1, 0, Integer.MAX_VALUE, materialAbove);
// }
// if (materialBesides != null) {
// setIfSolid(world, x + dx - 1, y + dy, z, 0, Integer.MAX_VALUE, materialBesides);
// setIfSolid(world, x + dx, y + dy - 1, z, 0, Integer.MAX_VALUE, materialBesides);
// setIfSolid(world, x + dx + 1, y + dy, z, 0, Integer.MAX_VALUE, materialBesides);
// setIfSolid(world, x + dx, y + dy + 1, z, 0, Integer.MAX_VALUE, materialBesides);
// }
// if (materialBelow != null) {
// setIfSolid(world, x + dx, y + dy, z + 1, 0, Integer.MAX_VALUE, materialBelow);
// }
// return true;
// }
// });
// }
public BufferedImage generatePreview(int width, int height, int waterLevel, int baseHeight, int heightDifference) {
    final TunnelLayer.Mode floorMode = layer.getFloorMode(), roofMode = layer.getRoofMode();
    final int floorWallDepth = layer.getFloorWallDepth(), roofWallDepth = layer.getRoofWallDepth(), floorLevel = layer.getFloorLevel(), roofLevel = layer.getRoofLevel(), tunnelExtent = width - 24, floorMin = layer.getFloorMin(), floorMax = layer.getFloorMax(), roofMin = layer.getRoofMin(), roofMax = layer.getRoofMax(), floodLevel = layer.getFloodLevel();
    final boolean removeWater = layer.isRemoveWater(), floodWithLava = layer.isFloodWithLava();
    final PerlinNoise noise = new PerlinNoise(0);
    final BufferedImage preview = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        // Clear the sky
        final int terrainHeight = MathUtils.clamp(0, (int) (Math.sin((x / (double) width * 1.5 + 1.25) * Math.PI) * heightDifference / 2 + heightDifference / 2 + baseHeight + noise.getPerlinNoise(x / 20.0) * 32 + noise.getPerlinNoise(x / 10.0) * 16 + noise.getPerlinNoise(x / 5.0) * 8), baseHeight + heightDifference - 1);
        for (int z = height - 1; z > terrainHeight; z--) {
            preview.setRGB(x, height - 1 - z, (z <= waterLevel) ? 0x0000ff : 0xffffff);
        }
        if (x <= tunnelExtent) {
            // Draw the tunnel
            int actualFloorLevel = calculateLevel(floorMode, floorLevel, terrainHeight, floorMin, floorMax, 1, height - 1, (floorNoise != null) ? ((int) floorNoise.getHeight(x, 0) - floorNoiseOffset) : 0);
            int actualRoofLevel = calculateLevel(roofMode, roofLevel, terrainHeight, roofMin, roofMax, 1, height - 1, (roofNoise != null) ? ((int) roofNoise.getHeight(x, 0) - roofNoiseOffset) : 0);
            if (actualRoofLevel > actualFloorLevel) {
                final float distanceToWall = tunnelExtent - x;
                final int floorLedgeHeight = calculateLedgeHeight(floorWallDepth, distanceToWall);
                final int roofLedgeHeight = calculateLedgeHeight(roofWallDepth, distanceToWall);
                actualFloorLevel += floorLedgeHeight;
                actualRoofLevel = Math.min(actualRoofLevel - roofLedgeHeight, Math.max(terrainHeight, 62));
                for (int z = actualFloorLevel + 1; z <= actualRoofLevel; z++) {
                    if (z <= floodLevel) {
                        if (floodWithLava) {
                            preview.setRGB(x, height - 1 - z, 0xff8000);
                        } else {
                            preview.setRGB(x, height - 1 - z, 0x0000ff);
                        }
                    } else {
                        if (z > terrainHeight) {
                            if (removeWater) {
                                preview.setRGB(x, height - 1 - z, 0x7f7fff);
                            }
                        } else {
                            preview.setRGB(x, height - 1 - z, 0x7f7f7f);
                        }
                    }
                }
            }
        }
    }
    return preview;
}
Also used : PerlinNoise(org.pepsoft.util.PerlinNoise) BufferedImage(java.awt.image.BufferedImage)

Example 8 with PerlinNoise

use of org.pepsoft.util.PerlinNoise in project WorldPainter by Captain-Chaos.

the class SimpleTheme method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    fixTerrainRangesTable();
    perlinNoise = new PerlinNoise(seed);
    initCaches();
}
Also used : PerlinNoise(org.pepsoft.util.PerlinNoise)

Example 9 with PerlinNoise

use of org.pepsoft.util.PerlinNoise in project WorldPainter by Captain-Chaos.

the class BorderChunkFactory method create.

public static ChunkFactory.ChunkCreationResult create(int chunkX, int chunkZ, Dimension dimension, Map<Layer, LayerExporter> exporters) {
    final int maxHeight = dimension.getMaxHeight();
    final Platform platform = dimension.getWorld().getPlatform();
    final Border border = dimension.getBorder();
    final int borderLevel = dimension.getBorderLevel();
    final boolean dark = dimension.isDarkLevel();
    final boolean bottomless = dimension.isBottomless();
    final Terrain subsurfaceMaterial = dimension.getSubsurfaceMaterial();
    final PerlinNoise noiseGenerator;
    if (noiseGenerators.get() == null) {
        noiseGenerator = new PerlinNoise(0);
        noiseGenerators.set(noiseGenerator);
    } else {
        noiseGenerator = noiseGenerators.get();
    }
    final long seed = dimension.getSeed();
    if (noiseGenerator.getSeed() != seed) {
        noiseGenerator.setSeed(seed);
    }
    final int floor = Math.max(borderLevel - 20, 0);
    final int variation = Math.min(15, (borderLevel - floor) / 2);
    final ChunkFactory.ChunkCreationResult result = new ChunkFactory.ChunkCreationResult();
    result.chunk = PlatformManager.getInstance().createChunk(platform, chunkX, chunkZ, maxHeight);
    final int maxY = maxHeight - 1;
    if (platform.capabilities.contains(BIOMES)) {
        switch(border) {
            case VOID:
            case LAVA:
                for (int x = 0; x < 16; x++) {
                    for (int z = 0; z < 16; z++) {
                        result.chunk.setBiome(x, z, BIOME_PLAINS);
                    }
                }
                break;
            case WATER:
                for (int x = 0; x < 16; x++) {
                    for (int z = 0; z < 16; z++) {
                        result.chunk.setBiome(x, z, BIOME_OCEAN);
                    }
                }
                break;
            default:
                throw new InternalError();
        }
    }
    for (int x = 0; x < 16; x++) {
        for (int z = 0; z < 16; z++) {
            if (border != Border.VOID) {
                final int worldX = (chunkX << 4) | x, worldZ = (chunkZ << 4) | z;
                final int floorLevel = (int) (floor + (noiseGenerator.getPerlinNoise(worldX / MEDIUM_BLOBS, worldZ / MEDIUM_BLOBS) + 0.5f) * variation + 0.5f);
                final int surfaceLayerLevel = floorLevel - dimension.getTopLayerDepth(worldX, worldZ, floorLevel);
                for (int y = 0; y <= maxY; y++) {
                    if ((y == 0) && (!bottomless)) {
                        result.chunk.setBlockType(x, y, z, BLK_BEDROCK);
                    } else if (y <= surfaceLayerLevel) {
                        result.chunk.setMaterial(x, y, z, subsurfaceMaterial.getMaterial(seed, worldX, worldZ, y, floorLevel));
                    } else if (y <= floorLevel) {
                        result.chunk.setMaterial(x, y, z, BEACHES.getMaterial(seed, worldX, worldZ, y, floorLevel));
                    } else if (y <= borderLevel) {
                        switch(border) {
                            case WATER:
                                result.chunk.setBlockType(x, y, z, BLK_STATIONARY_WATER);
                                break;
                            case LAVA:
                                result.chunk.setBlockType(x, y, z, BLK_STATIONARY_LAVA);
                                break;
                            default:
                        }
                    }
                }
            }
            if (dark) {
                result.chunk.setBlockType(x, maxY, z, BLK_BEDROCK);
                result.chunk.setHeight(x, z, maxY);
            } else if (border == Border.VOID) {
                result.chunk.setHeight(x, z, 0);
            } else {
                result.chunk.setHeight(x, z, (borderLevel < maxY) ? (borderLevel + 1) : maxY);
            }
        }
    }
    if (border != Border.VOID) {
        // Apply layers set to be applied everywhere, if any
        final Set<Layer> minimumLayers = dimension.getMinimumLayers();
        if (!minimumLayers.isEmpty()) {
            Tile virtualTile = new Tile(chunkX >> 3, chunkZ >> 3, dimension.getMaxHeight()) {

                @Override
                public synchronized float getHeight(int x, int y) {
                    return floor + (noiseGenerator.getPerlinNoise(((getX() << TILE_SIZE_BITS) | x) / MEDIUM_BLOBS, ((getY() << TILE_SIZE_BITS) | y) / MEDIUM_BLOBS) + 0.5f) * variation;
                }

                @Override
                public synchronized int getWaterLevel(int x, int y) {
                    return borderLevel;
                }

                private static final long serialVersionUID = 1L;
            };
            for (Layer layer : minimumLayers) {
                LayerExporter layerExporter = exporters.get(layer);
                if (layerExporter instanceof FirstPassLayerExporter) {
                    ((FirstPassLayerExporter) layerExporter).render(dimension, virtualTile, result.chunk);
                }
            }
        }
    }
    result.chunk.setTerrainPopulated(true);
    result.stats.surfaceArea = 256;
    if ((border == Border.WATER) || (border == Border.LAVA)) {
        result.stats.waterArea = 256;
    }
    return result;
}
Also used : Platform(org.pepsoft.worldpainter.Platform) Tile(org.pepsoft.worldpainter.Tile) PerlinNoise(org.pepsoft.util.PerlinNoise) Layer(org.pepsoft.worldpainter.layers.Layer) Terrain(org.pepsoft.worldpainter.Terrain) Border(org.pepsoft.worldpainter.Dimension.Border) ChunkFactory(org.pepsoft.minecraft.ChunkFactory)

Aggregations

PerlinNoise (org.pepsoft.util.PerlinNoise)9 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 PropertyChangeSupport (java.beans.PropertyChangeSupport)1 File (java.io.File)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 Serializable (java.io.Serializable)1 java.util (java.util)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ImageIO (javax.imageio.ImageIO)1 ImageReader (javax.imageio.ImageReader)1 FileImageInputStream (javax.imageio.stream.FileImageInputStream)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 javax.swing (javax.swing)1 Point3i (javax.vecmath.Point3i)1 ChunkFactory (org.pepsoft.minecraft.ChunkFactory)1 Constants (org.pepsoft.minecraft.Constants)1