Search in sources :

Example 1 with Dimension

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

the class MappingOp method go.

@Override
public Void go() throws ScriptException {
    goCalled();
    // Check preconditions
    if ((heightMap == null) && (layer == null) && (terrainIndex == -1)) {
        throw new ScriptException("No data source (heightMap, layer or terrain) specified");
    }
    if ((mode != Mode.SET_TERRAIN) && (layer == null)) {
        throw new ScriptException("layer not specified");
    }
    if (world == null) {
        throw new ScriptException("world not specified");
    }
    boolean greyScaleMapPresent = false;
    for (int mappedValue : mapping) {
        if (mappedValue != -1) {
            greyScaleMapPresent = true;
            break;
        }
    }
    final boolean colourMapPresent = !colourMapping.isEmpty();
    if ((greyScaleMapPresent || colourMapPresent) && (heightMap == null)) {
        throw new ScriptException("Mapping specified but no height map specified");
    } else if (heightMap != null) {
        if ((!greyScaleMapPresent) && (!colourMapPresent)) {
            throw new ScriptException("mapping not specified");
        }
        if (greyScaleMapPresent && colourMapPresent) {
            throw new ScriptException("Cannot mix grey scale and colour mapping");
        }
        if (layer != null) {
            if (layer.dataSize == Layer.DataSize.NONE) {
                throw new ScriptException("Layer of unsupported type specified: " + layer);
            }
            int bits;
            switch(layer.dataSize) {
                case BIT:
                case BIT_PER_CHUNK:
                    bits = 1;
                    break;
                case NIBBLE:
                    bits = 4;
                    break;
                case BYTE:
                    bits = 8;
                    break;
                default:
                    throw new InternalError();
            }
            int maxValue = (1 << bits) - 1;
            if (greyScaleMapPresent) {
                for (int mappedValue : mapping) {
                    if ((mappedValue < -1) || (mappedValue > maxValue)) {
                        throw new ScriptException("Invalid destination level " + mappedValue + " specified for " + bits + "-bit layer " + layer);
                    }
                }
            } else {
                for (Map.Entry<Integer, Integer> entry : colourMapping.entrySet()) {
                    int mappedValue = entry.getValue();
                    if ((mappedValue < 0) || (mappedValue > maxValue)) {
                        throw new ScriptException("Invalid destination level " + mappedValue + " specified for " + bits + "-bit layer " + layer);
                    }
                }
            }
        } else {
            if (greyScaleMapPresent) {
                for (int mappedValue : mapping) {
                    if ((mappedValue < -1) || (mappedValue >= Terrain.VALUES.length)) {
                        throw new ScriptException("Invalid terrain index " + mappedValue + " specified");
                    }
                }
            } else {
                for (Map.Entry<Integer, Integer> entry : colourMapping.entrySet()) {
                    int mappedValue = entry.getValue();
                    if ((mappedValue < 0) || (mappedValue >= Terrain.VALUES.length)) {
                        throw new ScriptException("Invalid terrain index " + mappedValue + " specified");
                    }
                }
            }
        }
    }
    final Dimension dimension = world.getDimension(dimIndex);
    if (dimension == null) {
        throw new ScriptException("Non existent dimension specified");
    }
    final HeightMap scaledHeightMap;
    Rectangle extent = new Rectangle(dimension.getLowestX() << TILE_SIZE_BITS, dimension.getLowestY() << TILE_SIZE_BITS, dimension.getWidth() << TILE_SIZE_BITS, dimension.getHeight() << TILE_SIZE_BITS);
    final boolean smoothScalingAllowed = greyScaleMapPresent && (mode != Mode.SET_TERRAIN) && (!Biome.INSTANCE.equals(layer)) && (!Annotations.INSTANCE.equals(layer));
    if (heightMap != null) {
        if ((scale != 100) || (offsetX != 0) || (offsetY != 0)) {
            // TODO ?
            boolean smoothScaling = (scale != 100) && smoothScalingAllowed;
            scaledHeightMap = TransformingHeightMap.build().withHeightMap(heightMap).withScale(scale).withOffset(offsetX, offsetY).now();
        } else {
            scaledHeightMap = heightMap;
        }
        if (scaledHeightMap.getExtent() != null) {
            extent = extent.intersection(scaledHeightMap.getExtent());
        }
    } else {
        scaledHeightMap = null;
    }
    final int x1 = extent.x, y1 = extent.y;
    final int x2 = extent.x + extent.width, y2 = extent.y + extent.height;
    final boolean bitLayer = (layer != null) && ((layer.getDataSize() == Layer.DataSize.BIT) || (layer.getDataSize() == Layer.DataSize.BIT_PER_CHUNK));
    if (filter instanceof DefaultFilter) {
        ((DefaultFilter) filter).setDimension(dimension);
    }
    for (int x = x1; x < x2; x++) {
        for (int y = y1; y < y2; y++) {
            int valueOut;
            if (scaledHeightMap != null) {
                if (colourMapPresent) {
                    int colour = scaledHeightMap.getColour(x, y);
                    if (colourMapping.containsKey(colour)) {
                        valueOut = colourMapping.get(colour);
                    } else {
                        continue;
                    }
                } else {
                    int valueIn = (int) (scaledHeightMap.getHeight(x, y) + 0.5f);
                    if ((valueIn < 0) || (valueIn > 65535)) {
                        continue;
                    }
                    valueOut = mapping[valueIn];
                    if (valueOut == -1) {
                        continue;
                    }
                }
            } else if (layer != null) {
                valueOut = layerValue;
            } else {
                valueOut = terrainIndex;
            }
            if (filter != null) {
                float filterValue = filter.modifyStrength(x, y, 1.0f);
                if (filterValue == 0.0f) {
                    continue;
                } else if (smoothScalingAllowed && (filterValue != 1.0f)) {
                    valueOut = (int) (filterValue * valueOut + 0.5f);
                }
            }
            switch(mode) {
                case SET_TERRAIN:
                    dimension.setTerrainAt(x, y, Terrain.VALUES[valueOut]);
                    break;
                case SET:
                    if (bitLayer) {
                        dimension.setBitLayerValueAt(layer, x, y, (valueOut != 0));
                    } else {
                        dimension.setLayerValueAt(layer, x, y, valueOut);
                    }
                    break;
                case SET_WHEN_HIGHER:
                    if (bitLayer) {
                        if (valueOut != 0) {
                            dimension.setBitLayerValueAt(layer, x, y, true);
                        }
                    } else {
                        if (dimension.getLayerValueAt(layer, x, y) < valueOut) {
                            dimension.setLayerValueAt(layer, x, y, valueOut);
                        }
                    }
                    break;
                case SET_WHEN_LOWER:
                    if (bitLayer) {
                        if (valueOut == 0) {
                            dimension.setBitLayerValueAt(layer, x, y, false);
                        }
                    } else {
                        if (dimension.getLayerValueAt(layer, x, y) > valueOut) {
                            dimension.setLayerValueAt(layer, x, y, valueOut);
                        }
                    }
                    break;
            }
        }
    }
    return null;
}
Also used : TransformingHeightMap(org.pepsoft.worldpainter.heightMaps.TransformingHeightMap) HeightMap(org.pepsoft.worldpainter.HeightMap) DefaultFilter(org.pepsoft.worldpainter.panels.DefaultFilter) Dimension(org.pepsoft.worldpainter.Dimension)

Example 2 with Dimension

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

the class DynMapPreviewer method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    // Install java.util.logging -> slf4j bridge:
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();
    // Load or initialise configuration
    // This will migrate the configuration directory if necessary
    Configuration config = Configuration.load();
    if (config == null) {
        if (!logger.isDebugEnabled()) {
            // If debug logging is on, the Configuration constructor will
            // already log this
            logger.info("Creating new configuration");
        }
        config = new Configuration();
    }
    Configuration.setInstance(config);
    logger.info("Installation ID: " + config.getUuid());
    // Load and install trusted WorldPainter root certificate
    X509Certificate trustedCert = null;
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        trustedCert = (X509Certificate) certificateFactory.generateCertificate(DynMapPreviewer.class.getResourceAsStream("/wproot.pem"));
    } catch (CertificateException e) {
        logger.error("Certificate exception while loading trusted root certificate", e);
    }
    // Load the plugins
    if (trustedCert != null) {
        PluginManager.loadPlugins(new File(Configuration.getConfigDir(), "plugins"), trustedCert.getPublicKey());
    } else {
        logger.error("Trusted root certificate not available; not loading plugins");
    }
    WPPluginManager.initialise(config.getUuid());
    JFrame frame = new JFrame("DynMapPreviewerTest");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    DynMapPreviewer viewer = new DynMapPreviewer();
    WPObject object = CustomObjectManager.getInstance().loadObject(new File(args[0]));
    TileFactory tileFactory = TileFactoryFactory.createNoiseTileFactory(0L, Terrain.GRASS, DEFAULT_MAX_HEIGHT_2, 58, 62, false, true, 20.0f, 1.0);
    Dimension dimension = new Dimension(0L, tileFactory, DIM_NORMAL, DEFAULT_MAX_HEIGHT_2);
    viewer.setObject(object, dimension);
    frame.getContentPane().add(viewer, BorderLayout.CENTER);
    frame.setSize(800, 600);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
Also used : WPObject(org.pepsoft.worldpainter.objects.WPObject) CertificateException(java.security.cert.CertificateException) Dimension(org.pepsoft.worldpainter.Dimension) CertificateFactory(java.security.cert.CertificateFactory) File(java.io.File) X509Certificate(java.security.cert.X509Certificate)

Example 3 with Dimension

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

the class IntegrityChecker method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Loading " + args[0]);
    World2 world;
    try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(args[0])))) {
        world = (World2) in.readObject();
    }
    for (Dimension dimension : world.getDimensions()) {
        float maxHeight = (dimension.getMaxHeight() - 1) + 0.5f;
        System.out.println("Checking integrity of " + dimension.getName() + " dimension");
        for (Tile tile : dimension.getTiles()) {
            boolean tileReported = false;
            for (int x = 0; x < Constants.TILE_SIZE; x++) {
                for (int y = 0; y < Constants.TILE_SIZE; y++) {
                    float height = tile.getHeight(x, y);
                    if (height < -0.5f) {
                        if (!tileReported) {
                            System.out.println("Tile " + tile.getX() + "," + tile.getY());
                            tileReported = true;
                        }
                        System.out.println("Height " + height + " < -0.5 @ " + x + "," + y);
                    } else if (height > maxHeight) {
                        if (!tileReported) {
                            System.out.println("Tile " + tile.getX() + "," + tile.getY());
                            tileReported = true;
                        }
                        System.out.println("Height " + height + " > " + maxHeight + " @ " + x + "," + y);
                    }
                }
            }
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) World2(org.pepsoft.worldpainter.World2) Tile(org.pepsoft.worldpainter.Tile) Dimension(org.pepsoft.worldpainter.Dimension) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with Dimension

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

the class ResetLava method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Loading world " + args[0]);
    File worldFile = new File(args[0]);
    int waterLevel = Integer.parseInt(args[1]);
    World2 world;
    try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(worldFile)))) {
        world = (World2) in.readObject();
    }
    System.out.println("World loaded");
    Dimension dim0 = world.getDimension(0);
    for (Tile tile : dim0.getTiles()) {
        for (int x = 0; x < TILE_SIZE; x++) {
            for (int y = 0; y < TILE_SIZE; y++) {
                // if ((tile.getWaterLevel(x, y) > (tile.getIntHeight(x, y))) && tile.getBitLayerValue(FloodWithLava.INSTANCE, x, y)) {
                tile.setBitLayerValue(FloodWithLava.INSTANCE, x, y, false);
                tile.setWaterLevel(x, y, waterLevel);
            // }
            }
        }
        System.out.print('.');
    }
    System.out.println();
    System.out.println("Saving world " + args[0]);
    try (ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(worldFile)))) {
        out.writeObject(world);
    }
    System.out.println("World saved");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) World2(org.pepsoft.worldpainter.World2) FileOutputStream(java.io.FileOutputStream) Tile(org.pepsoft.worldpainter.Tile) Dimension(org.pepsoft.worldpainter.Dimension) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with Dimension

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

the class RepairHeightMap method main.

public static final void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Scanning world " + args[0]);
    World2 world;
    try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(args[0])))) {
        world = (World2) in.readObject();
    }
    boolean repairsPerformed = false;
    for (Dimension dimension : world.getDimensions()) {
        repairsPerformed |= repairDimension(dimension);
    }
    if (repairsPerformed) {
        System.out.println("Repairs performed. Writing world out to " + args[0] + ".repaired");
        try (ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(args[0] + ".repaired")))) {
            out.writeObject(world);
        }
    } else {
        System.out.println("No repairs performed.");
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) World2(org.pepsoft.worldpainter.World2) FileOutputStream(java.io.FileOutputStream) Dimension(org.pepsoft.worldpainter.Dimension) ObjectOutputStream(java.io.ObjectOutputStream) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Dimension (org.pepsoft.worldpainter.Dimension)39 File (java.io.File)9 Tile (org.pepsoft.worldpainter.Tile)7 World2 (org.pepsoft.worldpainter.World2)6 EventVO (org.pepsoft.worldpainter.vo.EventVO)6 FileOutputStream (java.io.FileOutputStream)5 ProgressReceiver (org.pepsoft.util.ProgressReceiver)5 java.awt (java.awt)4 FileInputStream (java.io.FileInputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 SubProgressReceiver (org.pepsoft.util.SubProgressReceiver)4 ObjectOutputStream (java.io.ObjectOutputStream)3 java.util (java.util)3 List (java.util.List)3 Map (java.util.Map)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 FileInUseException (org.pepsoft.worldpainter.util.FileInUseException)3 java.awt.event (java.awt.event)2 PropertyVetoException (java.beans.PropertyVetoException)2