Malen von Gras erweitert
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package de.blight.common;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
|
||||
/**
|
||||
* Liest und schreibt Terrain-Farb-Gras-Halme als komprimierte Binärdatei
|
||||
* ({@code blight_grass_terrain.blgv}) neben der Kartendatei.
|
||||
*
|
||||
* Format v1: int MAGIC, int VERSION, int count, N × (float x, float y, float z, float height, byte seedIdx)
|
||||
* Kein Trockenheitswert – Farbe kommt ausschließlich vom Terrain.
|
||||
*/
|
||||
public final class TerrainColorGrassIO {
|
||||
|
||||
private static final int MAGIC = 0x54434758; // "TCGX"
|
||||
private static final int VERSION = 1;
|
||||
|
||||
private TerrainColorGrassIO() {}
|
||||
|
||||
public static Path getPath() {
|
||||
return MapIO.getMapPath().resolveSibling("blight_grass_terrain.blgv");
|
||||
}
|
||||
|
||||
public static void save(List<GrassVertexBlade> blades) throws IOException {
|
||||
Path p = getPath();
|
||||
Files.createDirectories(p.getParent());
|
||||
try (DataOutputStream out = new DataOutputStream(
|
||||
new BufferedOutputStream(new GZIPOutputStream(Files.newOutputStream(p))))) {
|
||||
out.writeInt(MAGIC);
|
||||
out.writeInt(VERSION);
|
||||
out.writeInt(blades.size());
|
||||
for (GrassVertexBlade b : blades) {
|
||||
out.writeFloat(b.x());
|
||||
out.writeFloat(b.y());
|
||||
out.writeFloat(b.z());
|
||||
out.writeFloat(b.height());
|
||||
out.writeByte(Math.max(-1, Math.min(126, b.seedIdx())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<GrassVertexBlade> load() throws IOException {
|
||||
Path p = getPath();
|
||||
if (!Files.exists(p)) return List.of();
|
||||
try (DataInputStream in = new DataInputStream(
|
||||
new BufferedInputStream(new GZIPInputStream(Files.newInputStream(p))))) {
|
||||
int magic = in.readInt();
|
||||
if (magic != MAGIC) throw new IOException("Ungültiges Dateiformat (MAGIC)");
|
||||
int version = in.readInt();
|
||||
if (version < 1 || version > VERSION) throw new IOException("Unbekannte Version: " + version);
|
||||
int count = in.readInt();
|
||||
List<GrassVertexBlade> list = new ArrayList<>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
float x = in.readFloat();
|
||||
float y = in.readFloat();
|
||||
float z = in.readFloat();
|
||||
float h = in.readFloat();
|
||||
int si = in.readByte();
|
||||
list.add(new GrassVertexBlade(x, y, z, h, 0f, si));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user