-
Notifications
You must be signed in to change notification settings - Fork 6
/
CommandLineInterface.java
130 lines (113 loc) · 4.75 KB
/
CommandLineInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package net.dries007.tfc.seedmaker;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import com.beust.jcommander.Parameter;
import net.dries007.tfc.seedmaker.util.Layers;
import net.dries007.tfc.seedmaker.util.WorldGen;
/**
* @author Dries007
*/
public class CommandLineInterface implements Runnable
{
@Parameter(names = {"-t", "--threads"}, description = "Amount of threads used, by default amount of CPU cores available")
public int threads = Runtime.getRuntime().availableProcessors();
@Parameter(names = {"-r", "--radius"}, description = "Radius in blocks")
public int radius = 1024 * 5;
@Parameter(names = {"-c", "--chunksize"}, description = "Size per 'chunk', more is faster but used (a lot) more RAM")
public int chunkSize = 128;
@Parameter(names = {"-s", "--seed", "--seeds"}, description = "The seeds to use, if none provided one random seed is chosen per thread. Comma separated list of strings")
public List<String> seeds = new ArrayList<>();
@Parameter(names = {"-n", "-count", "-target"}, description = "The amount of seeds to try and find. Only used when no seeds are given. If -1, the program will run forever")
public int targetCount = 10;
@Parameter(names = {"-m", "--map", "--maps"}, description = "Possible maps: All, Rocks, Biomes, Rock_Top, Rock_Middle, Rock_Bottom, Stability, PH, Drainage. Defaults to Biomes")
public List<String> maps = new ArrayList<>();
@Parameter(names = {"-?", "--help"}, help = true, description = "Display command line interface parameters")
public boolean help;
@Override
public void run()
{
// Don't start more threads than seeds we've asked for.
threads = Math.min(threads, targetCount);
System.out.println("Config: ");
System.out.println("- threads: " + threads);
System.out.println("- radius: " + radius);
System.out.println("- chunkSize: " + chunkSize);
System.out.println("- seeds: " + seeds);
System.out.println("- targetCount: " + targetCount);
System.out.println("- maps: " + maps);
Set<Layers> layersTmp = new HashSet<>();
// Find out what maps to draw
for (String map : maps)
{
if (map.equalsIgnoreCase("all"))
{
layersTmp = EnumSet.allOf(Layers.class);
break;
}
else if (map.equalsIgnoreCase("rocks"))
{
layersTmp.add(Layers.ROCK_TOP);
layersTmp.add(Layers.ROCK_MIDDLE);
layersTmp.add(Layers.ROCK_BOTTOM);
}
else
{
layersTmp.add(Layers.valueOf(map.toUpperCase()));
}
}
final EnumSet<Layers> layers = EnumSet.copyOf(layersTmp);
final Thread[] threadArray = new Thread[threads];
if (!seeds.isEmpty()) // We got seeds via CLI
{
// Queue up all the seeds
final ConcurrentLinkedQueue<WorldGen> queue = new ConcurrentLinkedQueue<>();
for (String seed : seeds) queue.add(new WorldGen(seed, radius, chunkSize, layers));
// Make a bunch of worker threads
for (int i = 0; i < threads; i++)
{
threadArray[i] = new Thread(() -> {
while (!queue.isEmpty())
{
WorldGen worldGen = queue.poll();
if (worldGen == null) continue; // Just in case
worldGen.run();
}
});
}
}
else // We didn't get seeds via CLI, make some at random
{
final AtomicInteger goodCount = new AtomicInteger();
// Make a bunch of worker threads
for (int i = 0; i < threads; i++)
{
threadArray[i] = new Thread(() -> {
while (targetCount < 0 || goodCount.get() < targetCount)
{
WorldGen worldGen = new WorldGen(null, radius, chunkSize, layers);
worldGen.run();
goodCount.incrementAndGet();
}
});
}
}
// Now actually start the threads
for (int i = 0; i < threads; i++) threadArray[i].start();
// Output routine, quick and dirty.
while (true)
{
boolean done = true;
for (int i = 0; i < threads; i++) if (threadArray[i].isAlive()) done = false;
if (done) break;
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}