Search in sources :

Example 1 with DataSize

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

the class Tile method getActiveLayers.

public List<Layer> getActiveLayers(int x, int y) {
    ensureReadable(BIT_LAYER_DATA);
    ensureReadable(LAYER_DATA);
    List<Layer> activeLayers = new ArrayList<>(bitLayerData.size() + layerData.size());
    for (Map.Entry<Layer, BitSet> entry : bitLayerData.entrySet()) {
        final Layer layer = entry.getKey();
        final DataSize dataSize = layer.getDataSize();
        if (((dataSize == DataSize.BIT) && getBitPerBlockLayerValue(entry.getValue(), x, y)) || ((dataSize == DataSize.BIT_PER_CHUNK) && getBitPerChunkLayerValue(entry.getValue(), x, y))) {
            activeLayers.add(layer);
        }
    }
    for (Map.Entry<Layer, byte[]> entry : layerData.entrySet()) {
        final Layer layer = entry.getKey();
        final DataSize dataSize = layer.getDataSize();
        final int defaultValue = layer.getDefaultValue();
        if (dataSize == DataSize.NIBBLE) {
            final int byteOffset = x | (y << TILE_SIZE_BITS);
            final byte _byte = entry.getValue()[byteOffset / 2];
            if ((byteOffset % 2 == 0) ? ((_byte & 0x0F) != defaultValue) : (((_byte & 0xF0) >> 4) != defaultValue)) {
                activeLayers.add(layer);
            }
        } else if ((entry.getValue()[x | (y << TILE_SIZE_BITS)] & 0xFF) != defaultValue) {
            activeLayers.add(layer);
        }
    }
    return activeLayers;
}
Also used : DataSize(org.pepsoft.worldpainter.layers.Layer.DataSize) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) Layer(org.pepsoft.worldpainter.layers.Layer) HashMap(java.util.HashMap) Map(java.util.Map) Point(java.awt.Point)

Example 2 with DataSize

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

the class Tile method releaseEvents.

/**
 * Release an inhibition on firing events. Will fire all appropriate events at this time, if the tile was modified
 * since the first invocation of {@link #inhibitEvents()}, but only if this is the last invocation of
 * <code>releaseEvents()</code> in a nested set.
 */
public synchronized void releaseEvents() {
    if (eventInhibitionCounter > 0) {
        eventInhibitionCounter--;
        if (eventInhibitionCounter == 0) {
            if (heightMapDirty) {
                heightMapChanged();
                heightMapDirty = false;
            }
            if (terrainDirty) {
                terrainChanged();
                terrainDirty = false;
            }
            if (waterLevelDirty) {
                waterLevelChanged();
                waterLevelDirty = false;
            }
            if (bitLayersDirty) {
                allBitLayerDataChanged();
                bitLayersDirty = false;
                for (Iterator<Layer> i = dirtyLayers.iterator(); i.hasNext(); ) {
                    DataSize dataSize = i.next().getDataSize();
                    if ((dataSize == DataSize.BIT) || (dataSize == DataSize.BIT_PER_CHUNK)) {
                        i.remove();
                    }
                }
            }
            if (nonBitLayersDirty) {
                allNonBitLayerDataChanged();
                nonBitLayersDirty = false;
                for (Iterator<Layer> i = dirtyLayers.iterator(); i.hasNext(); ) {
                    DataSize dataSize = i.next().getDataSize();
                    if ((dataSize != DataSize.BIT) && (dataSize != DataSize.BIT_PER_CHUNK)) {
                        i.remove();
                    }
                }
            }
            if (!dirtyLayers.isEmpty()) {
                Set<Layer> changedLayers = Collections.unmodifiableSet(dirtyLayers);
                for (Listener listener : listeners) {
                    listener.layerDataChanged(this, changedLayers);
                }
                dirtyLayers.clear();
            }
            if (seedsDirty) {
                seedsChanged();
                seedsDirty = false;
            }
        }
    } else {
        throw new IllegalStateException("Events not inhibited");
    }
}
Also used : UndoListener(org.pepsoft.util.undo.UndoListener) DataSize(org.pepsoft.worldpainter.layers.Layer.DataSize) Layer(org.pepsoft.worldpainter.layers.Layer)

Example 3 with DataSize

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

the class CombinedLayer method apply.

public Set<Layer> apply(Tile tile) {
    Set<Layer> addedLayers = new HashSet<>();
    if (!tile.hasLayer(this)) {
        return Collections.emptySet();
    }
    tile.inhibitEvents();
    try {
        for (Layer layer : layers) {
            boolean layerAdded = false;
            final float factor = factors.get(layer);
            DataSize dataSize = layer.getDataSize();
            if ((dataSize == BIT) || (dataSize == BIT_PER_CHUNK)) {
                for (int x = 0; x < TILE_SIZE; x++) {
                    for (int y = 0; y < TILE_SIZE; y++) {
                        float strength = Math.min(tile.getLayerValue(this, x, y) / 15.0f * factor, 1.0f);
                        if ((strength > 0.95f) || (Math.random() < strength)) {
                            tile.setBitLayerValue(layer, x, y, true);
                            layerAdded = true;
                        }
                    }
                }
            } else {
                int maxValue = (dataSize == NIBBLE) ? 15 : 255;
                for (int x = 0; x < TILE_SIZE; x++) {
                    for (int y = 0; y < TILE_SIZE; y++) {
                        int value = Math.min((int) (tile.getLayerValue(this, x, y) * factor + 0.5f), maxValue);
                        if (value > 0) {
                            tile.setLayerValue(layer, x, y, value);
                            layerAdded = true;
                        }
                    }
                }
            }
            if (layerAdded) {
                addedLayers.add(layer);
            }
        }
        tile.clearLayerData(this);
    } finally {
        tile.releaseEvents();
    }
    return addedLayers;
}
Also used : DataSize(org.pepsoft.worldpainter.layers.Layer.DataSize)

Aggregations

DataSize (org.pepsoft.worldpainter.layers.Layer.DataSize)3 Layer (org.pepsoft.worldpainter.layers.Layer)2 Point (java.awt.Point)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UndoListener (org.pepsoft.util.undo.UndoListener)1