Search in sources :

Example 1 with HeightMap

use of org.pepsoft.worldpainter.HeightMap 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 HeightMap

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

the class DelegatingHeightMap method replace.

public final HeightMap replace(int index, HeightMap newChild) {
    HeightMap oldChild = children[index];
    children[index] = newChild;
    if (oldChild instanceof AbstractHeightMap) {
        ((AbstractHeightMap) oldChild).parent = null;
    }
    if (newChild instanceof AbstractHeightMap) {
        ((AbstractHeightMap) newChild).parent = this;
    }
    childrenChanged();
    return oldChild;
}
Also used : HeightMap(org.pepsoft.worldpainter.HeightMap)

Example 3 with HeightMap

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

the class RepairHeightMap method repairDimension.

private static final boolean repairDimension(Dimension dimension) {
    TileFactory tileFactory = dimension.getTileFactory();
    if (tileFactory instanceof HeightMapTileFactory) {
        HeightMap heightMap = ((HeightMapTileFactory) tileFactory).getHeightMap();
        if ((heightMap instanceof SumHeightMap) && ((((SumHeightMap) heightMap).getHeightMap1() == null) || (((SumHeightMap) heightMap).getHeightMap2() == null))) {
            System.out.println("Broken height map found in dimension " + dimension.getName() + "; replacing with default height map");
            heightMap = new ConstantHeightMap(46);
            // heightMap = new SumHeightMap(new ConstantHeightMap(58), new NoiseHeightMap(20, 1.0, 1));
            ((HeightMapTileFactory) tileFactory).setHeightMap(heightMap);
            return true;
        }
    }
    return false;
}
Also used : SumHeightMap(org.pepsoft.worldpainter.heightMaps.SumHeightMap) ConstantHeightMap(org.pepsoft.worldpainter.heightMaps.ConstantHeightMap) HeightMap(org.pepsoft.worldpainter.HeightMap) SumHeightMap(org.pepsoft.worldpainter.heightMaps.SumHeightMap) HeightMapTileFactory(org.pepsoft.worldpainter.HeightMapTileFactory) ConstantHeightMap(org.pepsoft.worldpainter.heightMaps.ConstantHeightMap) TileFactory(org.pepsoft.worldpainter.TileFactory) HeightMapTileFactory(org.pepsoft.worldpainter.HeightMapTileFactory)

Example 4 with HeightMap

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

the class HeightMapTreeCellRenderer method getTreeCellRendererComponent.

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    if (value instanceof HeightMap) {
        HeightMap heightMap = (HeightMap) value;
        String role = null;
        StringBuilder name = new StringBuilder();
        if (value instanceof AbstractHeightMap) {
            DelegatingHeightMap parent = ((AbstractHeightMap) value).getParent();
            if (parent instanceof DisplacementHeightMap) {
                role = parent.getRole(parent.getIndex(heightMap));
                if (role.endsWith("HeightMap")) {
                    name.append(role.substring(0, role.length() - 9));
                } else if (role.endsWith("Map")) {
                    name.append(role.substring(0, role.length() - 3));
                } else {
                    name.append(role);
                }
            }
        }
        if (heightMap.getName() != null) {
            if (name.length() > 0) {
                name.append(": ");
            }
            name.append(heightMap.getName());
        }
        if (name.length() == 0) {
            name.append(heightMap.getClass().getSimpleName());
        }
        if (value == focusHeightMap) {
            setBorder(focusBorder);
        } else if (getBorder() != null) {
            setBorder(null);
        }
        setText(name.toString());
        setIcon(heightMap.getIcon());
        setToolTipText(getTooltipText(heightMap, role));
    }
    return this;
}
Also used : DelegatingHeightMap(org.pepsoft.worldpainter.heightMaps.DelegatingHeightMap) AbstractHeightMap(org.pepsoft.worldpainter.heightMaps.AbstractHeightMap) HeightMap(org.pepsoft.worldpainter.HeightMap) DisplacementHeightMap(org.pepsoft.worldpainter.heightMaps.DisplacementHeightMap) AbstractHeightMap(org.pepsoft.worldpainter.heightMaps.AbstractHeightMap) DisplacementHeightMap(org.pepsoft.worldpainter.heightMaps.DisplacementHeightMap) DelegatingHeightMap(org.pepsoft.worldpainter.heightMaps.DelegatingHeightMap)

Aggregations

HeightMap (org.pepsoft.worldpainter.HeightMap)4 Dimension (org.pepsoft.worldpainter.Dimension)1 HeightMapTileFactory (org.pepsoft.worldpainter.HeightMapTileFactory)1 TileFactory (org.pepsoft.worldpainter.TileFactory)1 AbstractHeightMap (org.pepsoft.worldpainter.heightMaps.AbstractHeightMap)1 ConstantHeightMap (org.pepsoft.worldpainter.heightMaps.ConstantHeightMap)1 DelegatingHeightMap (org.pepsoft.worldpainter.heightMaps.DelegatingHeightMap)1 DisplacementHeightMap (org.pepsoft.worldpainter.heightMaps.DisplacementHeightMap)1 SumHeightMap (org.pepsoft.worldpainter.heightMaps.SumHeightMap)1 TransformingHeightMap (org.pepsoft.worldpainter.heightMaps.TransformingHeightMap)1 DefaultFilter (org.pepsoft.worldpainter.panels.DefaultFilter)1