Skip to content

Commit f50a98a

Browse files
committed
Added code files
Current version tested against WorldEngine 0.19.0
1 parent bbd5359 commit f50a98a

File tree

5 files changed

+11127
-0
lines changed

5 files changed

+11127
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ release.properties
77
dependency-reduced-pom.xml
88
buildNumber.properties
99
.mvn/timing.properties
10+
/.idea/
11+
*.iml
12+
/lib/

pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>1.4.0.RELEASE</version>
11+
</parent>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-web</artifactId>
17+
</dependency>
18+
</dependencies>
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<artifactId>maven-compiler-plugin</artifactId>
23+
<configuration>
24+
<source>1.8</source>
25+
<target>1.8</target>
26+
</configuration>
27+
</plugin>
28+
<plugin>
29+
<groupId>org.codehaus.mojo</groupId>
30+
<artifactId>exec-maven-plugin</artifactId>
31+
<version>1.2.1</version>
32+
<configuration>
33+
<mainClass>com.seiferware.java.worldreader.Main</mainClass>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
39+
<groupId>com.seiferware.java</groupId>
40+
<artifactId>world-reader</artifactId>
41+
<version>1.0</version>
42+
</project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.seiferware.java.worldreader;
2+
3+
import com.google.protobuf.CodedInputStream;
4+
import org.omg.CORBA.DoubleHolder;
5+
import org.omg.CORBA.IntHolder;
6+
7+
import javax.imageio.ImageIO;
8+
import java.awt.image.BufferedImage;
9+
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.stream.Stream;
15+
16+
public class Main {
17+
private static final String PATH_ROOT = "C:\\software\\apps\\worldengine\\worlds\\";
18+
private static void createImage(String path, WorldOuterClass.World.DoubleMatrix data) throws IOException {
19+
createImage(path, data, ColorCalculator.DEFAULT);
20+
}
21+
private static void createImage(String path, WorldOuterClass.World.DoubleMatrix data, ColorCalculator calc) throws IOException {
22+
DoubleHolder min = new DoubleHolder(Double.MAX_VALUE);
23+
DoubleHolder max = new DoubleHolder(Double.MIN_VALUE);
24+
data.getRowsList().stream().flatMap(d -> d.getCellsList().stream()).forEach(d -> {
25+
if(min.value > d) {
26+
min.value = d;
27+
}
28+
if(max.value < d) {
29+
max.value = d;
30+
}
31+
});
32+
BufferedImage bim = new BufferedImage(data.getRows(0).getCellsCount(), data.getRowsCount(), BufferedImage.TYPE_INT_RGB);
33+
for(int y = 0; y < data.getRowsCount(); y++) {
34+
WorldOuterClass.World.DoubleRow row = data.getRows(y);
35+
for(int x = 0; x < row.getCellsCount(); x++) {
36+
int color = calc.getColorForValue(row.getCells(x), min.value, max.value);
37+
bim.setRGB(x, y, color);
38+
}
39+
}
40+
ImageIO.write(bim, "png", new File(path));
41+
}
42+
private static void getDistribution(WorldOuterClass.World.DoubleMatrix data, int precision) {
43+
DoubleHolder min = new DoubleHolder(Double.MAX_VALUE);
44+
DoubleHolder max = new DoubleHolder(Double.MIN_VALUE);
45+
IntHolder minInt = new IntHolder();
46+
IntHolder maxInt = new IntHolder();
47+
Map<Integer, IntHolder> values = new HashMap<>();
48+
data.getRowsList().stream().flatMap(d -> d.getCellsList().stream()).forEach(d -> {
49+
int intVal = Math.toIntExact(Math.round(d * precision));
50+
if(min.value > d) {
51+
min.value = d;
52+
minInt.value = intVal;
53+
}
54+
if(max.value < d) {
55+
max.value = d;
56+
maxInt.value = intVal;
57+
}
58+
if(!values.containsKey(intVal)) {
59+
values.put(intVal, new IntHolder(0));
60+
}
61+
values.get(intVal).value++;
62+
});
63+
IntHolder zero = new IntHolder(0);
64+
for(int i = minInt.value; i <= maxInt.value; i++) {
65+
System.out.println(i / (float) precision + "\t" + values.getOrDefault(i, zero).value);
66+
}
67+
}
68+
public static void main(String[] args) {
69+
if(args.length == 1) {
70+
File f = new File(args[0]);
71+
if(f.exists()) {
72+
if(f.isDirectory()) {
73+
processDirectory(f);
74+
} else {
75+
processFile(f.getAbsolutePath());
76+
}
77+
} else {
78+
System.out.println("Could not locate specified file or directory.");
79+
}
80+
} else if(args.length == 0) {
81+
processDirectory(new File("."));
82+
} else {
83+
System.out.println("The only valid command line argument is either a world file, or a directory with world files. If no argument is specified, the current working directory will be used.");
84+
}
85+
}
86+
private static void processDirectory(File dir) {
87+
File[] files = dir.listFiles((dir1, name) -> name.endsWith(".world"));
88+
if(files != null && files.length > 0) {
89+
Stream.of(files).forEach(file -> {
90+
System.out.println("Processing " + file.getName());
91+
processFile(file.getAbsolutePath());
92+
});
93+
} else {
94+
System.out.println("Did not find any *.world files in specified directory.");
95+
}
96+
}
97+
private static void processFile(String fileName) {
98+
processFile(fileName, fileName.endsWith(".world") ? fileName.substring(0, fileName.length() - 6) : fileName);
99+
}
100+
private static void processFile(String fileName, String baseFileName) {
101+
WorldOuterClass.World world;
102+
try (FileInputStream fis = new FileInputStream(new File(fileName))) {
103+
CodedInputStream cis = CodedInputStream.newInstance(fis);
104+
world = WorldOuterClass.World.parseFrom(cis);
105+
} catch (IOException e) {
106+
System.err.println("An error occurred: " + e.getMessage());
107+
return;
108+
}
109+
//getDistribution(world.getPrecipitationData(), 10);
110+
try {
111+
createImage(baseFileName + "-height.png", world.getHeightMapData());
112+
createImage(baseFileName + "-rain.png", world.getPrecipitationData());
113+
createImage(baseFileName + "-temp.png", world.getTemperatureData());
114+
createImage(baseFileName + "-rivers.png", world.getRivermap(), (value, minValue, maxValue) -> {
115+
if(value == 0) {
116+
return 0;
117+
}
118+
return ColorCalculator.DEFAULT.getColorForValue(value, minValue, maxValue) | 255;
119+
});
120+
} catch (IOException e) {
121+
System.err.println("An error occurred: " + e.getMessage());
122+
}
123+
}
124+
private interface ColorCalculator {
125+
ColorCalculator DEFAULT = (value, minValue, maxValue) -> {
126+
int result = (int) Math.max(0, Math.min(255, Math.round((value - minValue) * 255 / (maxValue - minValue))));
127+
return (result << 16) | (result << 8) | (result);
128+
};
129+
int getColorForValue(double value, double minValue, double maxValue);
130+
}
131+
}

0 commit comments

Comments
 (0)