Search in sources :

Example 1 with Bo2ObjectProvider

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

the class PlantLayerExporter method apply.

@Override
public Fixup apply(Dimension dimension, Point3i location, int intensity, Rectangle exportedArea, MinecraftWorld minecraftWorld) {
    final Random random = incidentalRandomRef.get();
    final long seed = dimension.getSeed() ^ ((long) location.x << 40) ^ ((long) location.y << 20) ^ (location.z);
    random.setSeed(seed);
    if ((intensity >= 100) || ((intensity > 0) && (random.nextInt(100) < intensity))) {
        // Place plant
        final Bo2ObjectProvider objectProvider = layer.getObjectProvider();
        objectProvider.setSeed(seed);
        final Plant plant = (Plant) objectProvider.getObject();
        final int existingBlockType = minecraftWorld.getBlockTypeAt(location.x, location.y, location.z);
        if ((location.z < (minecraftWorld.getMaxHeight() - 1)) && ((plant.getCategory() == Plant.Category.WATER_PLANTS) ? ((existingBlockType == Constants.BLK_WATER) || (existingBlockType == Constants.BLK_STATIONARY_WATER)) : (existingBlockType == Constants.BLK_AIR))) {
            if (plant.isValidFoundation(minecraftWorld, location.x, location.y, location.z - 1)) {
                if (plant.getCategory() == Plant.Category.WATER_PLANTS) {
                    possiblyRenderWaterPlant(minecraftWorld, dimension, plant, location.x, location.y, location.z + 1);
                } else {
                    renderObject(minecraftWorld, dimension, plant, location.x, location.y, location.z, false);
                }
            } else if (layer.isGenerateTilledDirt() && (plant.getCategory() == Plant.Category.CROPS) && ((minecraftWorld.getBlockTypeAt(location.x, location.y, location.z - 1) == Constants.BLK_GRASS) || (minecraftWorld.getBlockTypeAt(location.x, location.y, location.z - 1) == Constants.BLK_DIRT))) {
                minecraftWorld.setMaterialAt(location.x, location.y, location.z - 1, TILLED_DIRT);
                renderObject(minecraftWorld, dimension, plant, location.x, location.y, location.z, false);
            }
        }
    }
    return null;
}
Also used : Random(java.util.Random) Bo2ObjectProvider(org.pepsoft.worldpainter.layers.bo2.Bo2ObjectProvider)

Example 2 with Bo2ObjectProvider

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

the class PlantLayer method getObjectProvider.

public Bo2ObjectProvider getObjectProvider() {
    int total = 0;
    for (PlantSettings setting : settings) {
        if (setting != null) {
            total += setting.occurrence;
        }
    }
    final byte[] pool = new byte[total], growthOffset = new byte[total], growthRange = new byte[total];
    int index = 0;
    for (byte i = 0; i < settings.length; i++) {
        if (settings[i] != null) {
            for (short j = 0; j < settings[i].occurrence; j++) {
                growthOffset[index] = settings[i].dataValueFrom;
                growthRange[index] = (byte) (settings[i].dataValueTo - settings[i].dataValueFrom);
                pool[index++] = i;
            }
        }
    }
    return new Bo2ObjectProvider() {

        @Override
        public String getName() {
            return PlantLayer.this.getName();
        }

        @Override
        public WPObject getObject() {
            final int index = random.nextInt(pool.length);
            final Plant plant = Plant.ALL_PLANTS[pool[index]];
            if (growthRange[index] == 0) {
                return plant.withGrowth(growthOffset[index]);
            } else {
                return plant.withGrowth(growthOffset[index] + random.nextInt(growthRange[index] + 1));
            }
        }

        @Override
        public List<WPObject> getAllObjects() {
            throw new UnsupportedOperationException("Not supported");
        }

        @Override
        public void setSeed(long seed) {
            random.setSeed(seed);
        }

        private final Random random = new Random();
    };
}
Also used : Random(java.util.Random) WPObject(org.pepsoft.worldpainter.objects.WPObject) Bo2ObjectProvider(org.pepsoft.worldpainter.layers.bo2.Bo2ObjectProvider)

Example 3 with Bo2ObjectProvider

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

the class PlantLayerExporter method render.

@Override
public List<Fixup> render(Dimension dimension, Rectangle area, Rectangle exportedArea, MinecraftWorld minecraftWorld) {
    final long seed = dimension.getSeed();
    final int tileX1 = exportedArea.x >> TILE_SIZE_BITS, tileX2 = (exportedArea.x + exportedArea.width - 1) >> TILE_SIZE_BITS;
    final int tileY1 = exportedArea.y >> TILE_SIZE_BITS, tileY2 = (exportedArea.y + exportedArea.height - 1) >> TILE_SIZE_BITS;
    final int maxY = minecraftWorld.getMaxHeight() - 1;
    final boolean generateTilledDirt = layer.isGenerateTilledDirt();
    final boolean blockRulesEnforced = !"false".equalsIgnoreCase(System.getProperty("org.pepsoft.worldpainter.enforceBlockRules"));
    final Bo2ObjectProvider objectProvider = layer.getObjectProvider();
    for (int tileX = tileX1; tileX <= tileX2; tileX++) {
        for (int tileY = tileY1; tileY <= tileY2; tileY++) {
            final Tile tile = dimension.getTile(tileX, tileY);
            if ((tile == null) || (!tile.hasLayer(layer))) {
                // Tile doesn't exist, or it doesn't have the layer
                continue;
            }
            final long tileSeed = (long) tileX << 32 ^ tileY ^ seed;
            objectProvider.setSeed(tileSeed);
            for (int x = 0; x < TILE_SIZE; x++) {
                for (int y = 0; y < TILE_SIZE; y++) {
                    if (tile.getBitLayerValue(layer, x, y)) {
                        // Possibly place a plant
                        final int height = tile.getIntHeight(x, y);
                        if (height < maxY) {
                            final int worldX = (tileX << TILE_SIZE_BITS) | x, worldY = (tileY << TILE_SIZE_BITS) | y;
                            final Plant plant = (Plant) objectProvider.getObject();
                            if (plant.getCategory() == Plant.Category.WATER_PLANTS) {
                                if ((!blockRulesEnforced) || plant.isValidFoundation(minecraftWorld, worldX, worldY, height)) {
                                    possiblyRenderWaterPlant(minecraftWorld, dimension, plant, worldX, worldY, height + 1);
                                }
                            } else {
                                if (tile.getIntHeight(x, y) >= tile.getWaterLevel(x, y)) {
                                    if (!blockRulesEnforced) {
                                        renderObject(minecraftWorld, dimension, plant, worldX, worldY, height + 1, false);
                                        if (generateTilledDirt && (plant.getCategory() == Plant.Category.CROPS) && ((minecraftWorld.getBlockTypeAt(worldX, worldY, height) == Constants.BLK_GRASS) || (minecraftWorld.getBlockTypeAt(worldX, worldY, height) == Constants.BLK_DIRT))) {
                                            minecraftWorld.setMaterialAt(worldX, worldY, height, TILLED_DIRT);
                                        }
                                    } else {
                                        if (plant.isValidFoundation(minecraftWorld, worldX, worldY, height)) {
                                            renderObject(minecraftWorld, dimension, plant, worldX, worldY, height + 1, false);
                                        } else if (generateTilledDirt && (plant.getCategory() == Plant.Category.CROPS) && ((minecraftWorld.getBlockTypeAt(worldX, worldY, height) == Constants.BLK_GRASS) || (minecraftWorld.getBlockTypeAt(worldX, worldY, height) == Constants.BLK_DIRT))) {
                                            minecraftWorld.setMaterialAt(worldX, worldY, height, TILLED_DIRT);
                                            renderObject(minecraftWorld, dimension, plant, worldX, worldY, height + 1, false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : Tile(org.pepsoft.worldpainter.Tile) Bo2ObjectProvider(org.pepsoft.worldpainter.layers.bo2.Bo2ObjectProvider)

Aggregations

Bo2ObjectProvider (org.pepsoft.worldpainter.layers.bo2.Bo2ObjectProvider)3 Random (java.util.Random)2 Tile (org.pepsoft.worldpainter.Tile)1 WPObject (org.pepsoft.worldpainter.objects.WPObject)1