mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-14 03:36:03 +00:00
implementation compiles now
This commit is contained in:
@@ -1,206 +0,0 @@
|
||||
package noise;
|
||||
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.util.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.util.collections.ProbabilityCollectionImpl;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import com.dfsek.terra.config.GenericLoaders;
|
||||
import com.dfsek.terra.config.fileloaders.FolderLoader;
|
||||
import com.dfsek.terra.config.loaders.ProbabilityCollectionLoader;
|
||||
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
||||
import com.dfsek.terra.config.loaders.config.sampler.NoiseSamplerBuilderLoader;
|
||||
import com.dfsek.terra.registry.config.NoiseRegistry;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class NoiseTool {
|
||||
public static void main(String... args) throws ConfigException, IOException {
|
||||
JFrame frame = new JFrame("Noise Viewer");
|
||||
|
||||
AtomicInteger seed = new AtomicInteger(2403);
|
||||
JLabel label = new JLabel(new ImageIcon(load(seed.get(), false, false)));
|
||||
frame.add(label);
|
||||
frame.pack();
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
|
||||
frame.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
if(e.getKeyChar() == 'r') {
|
||||
try {
|
||||
label.setIcon(new ImageIcon(load(seed.get(), false, false)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
} else if(e.getKeyChar() == 's') {
|
||||
try {
|
||||
seed.set(ThreadLocalRandom.current().nextInt());
|
||||
label.setIcon(new ImageIcon(load(seed.get(), false, false)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
} else if(e.getKeyChar() == 'd') {
|
||||
try {
|
||||
label.setIcon(new ImageIcon(load(seed.get(), true, false)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
} else if(e.getKeyChar() == 'c') {
|
||||
try {
|
||||
label.setIcon(new ImageIcon(load(seed.get(), false, true)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
});
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private static int normal(double in, double out, double min, double max) {
|
||||
double range = max - min;
|
||||
return (int) (((in - min) * out) / range);
|
||||
}
|
||||
|
||||
private static int buildRGBA(int in) {
|
||||
return (255 << 24)
|
||||
+ (in << 16)
|
||||
+ (in << 8)
|
||||
+ in;
|
||||
}
|
||||
|
||||
private static BufferedImage load(int seed, boolean distribution, boolean chunk) throws ConfigException, IOException {
|
||||
long s = System.nanoTime();
|
||||
|
||||
FolderLoader folderLoader = new FolderLoader(Paths.get("./"));
|
||||
|
||||
ConfigLoader loader = new ConfigLoader();
|
||||
loader.registerLoader(NoiseSeeded.class, new NoiseSamplerBuilderLoader(new NoiseRegistry()))
|
||||
.registerLoader(BufferedImage.class, new BufferedImageLoader(folderLoader))
|
||||
.registerLoader(ProbabilityCollection.class, new ProbabilityCollectionLoader());
|
||||
|
||||
new GenericLoaders(null).register(loader);
|
||||
NoiseConfigTemplate template = new NoiseConfigTemplate();
|
||||
|
||||
File file = new File("./config.yml");
|
||||
|
||||
System.out.println(file.getAbsolutePath());
|
||||
|
||||
File colorFile = new File("./color.yml");
|
||||
|
||||
|
||||
System.out.println(file.getAbsolutePath());
|
||||
if(!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
FileUtils.copyInputStreamToFile(NoiseTool.class.getResourceAsStream("/config.yml"), file);
|
||||
}
|
||||
|
||||
|
||||
boolean colors = false;
|
||||
ColorConfigTemplate color = new ColorConfigTemplate();
|
||||
if(colorFile.exists()) {
|
||||
loader.load(color, new FileInputStream(colorFile));
|
||||
colors = color.enable();
|
||||
}
|
||||
ProbabilityCollectionImpl<Integer> colorCollection = color.getColors();
|
||||
|
||||
loader.load(template, new FileInputStream(file));
|
||||
System.out.println(template.getBuilder().getDimensions());
|
||||
NoiseSampler noise = template.getBuilder().apply((long) seed);
|
||||
|
||||
|
||||
int size = 1024;
|
||||
|
||||
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
double[][] noiseVals = new double[size][size];
|
||||
int[][] rgbVals = new int[size][size];
|
||||
double max = Double.MIN_VALUE;
|
||||
double min = Double.MAX_VALUE;
|
||||
|
||||
int[] buckets = new int[1024];
|
||||
|
||||
for(int x = 0; x < noiseVals.length; x++) {
|
||||
for(int z = 0; z < noiseVals[x].length; z++) {
|
||||
double n = noise.getNoise(x, z);
|
||||
noiseVals[x][z] = n;
|
||||
max = Math.max(n, max);
|
||||
min = Math.min(n, min);
|
||||
if(colors) rgbVals[x][z] = colorCollection.get(noise, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
for(int x = 0; x < noiseVals.length; x++) {
|
||||
for(int z = 0; z < noiseVals[x].length; z++) {
|
||||
if(colors) image.setRGB(x, z, rgbVals[x][z] + (255 << 24));
|
||||
else image.setRGB(x, z, buildRGBA(normal(noiseVals[x][z], 255, min, max)));
|
||||
buckets[normal(noiseVals[x][z], size - 1, min, max)]++;
|
||||
}
|
||||
}
|
||||
|
||||
long time = System.nanoTime() - s;
|
||||
|
||||
double ms = time / 1000000d;
|
||||
|
||||
|
||||
if(chunk) {
|
||||
for(int x = 0; x < image.getWidth(); x += 16) {
|
||||
for(int y = 0; y < image.getHeight(); y++) image.setRGB(x, y, buildRGBA(0));
|
||||
}
|
||||
for(int y = 0; y < image.getWidth(); y += 16) {
|
||||
for(int x = 0; x < image.getHeight(); x++) image.setRGB(x, y, buildRGBA(0));
|
||||
}
|
||||
}
|
||||
|
||||
Graphics graphics = image.getGraphics();
|
||||
graphics.setColor(Color.WHITE);
|
||||
graphics.fillRect(0, 0, 325, 90);
|
||||
graphics.setColor(Color.BLACK);
|
||||
graphics.setFont(new Font("Monospace", Font.BOLD, 20));
|
||||
graphics.drawString("min: " + min, 0, 20);
|
||||
graphics.drawString("max: " + max, 0, 40);
|
||||
graphics.drawString("seed: " + seed, 0, 60);
|
||||
graphics.drawString("time: " + ms + "ms", 0, 80);
|
||||
|
||||
if(distribution) {
|
||||
graphics.setColor(Color.WHITE);
|
||||
graphics.fillRect(0, size - (size / 4) - 1, size, (size / 4) - 1);
|
||||
int highestBucket = Integer.MIN_VALUE;
|
||||
for(int i : buckets) highestBucket = Math.max(highestBucket, i);
|
||||
graphics.setColor(Color.BLACK);
|
||||
graphics.drawString("" + highestBucket, 0, size - (size / 4) - 1 + 20);
|
||||
|
||||
for(int x = 0; x < size; x++) {
|
||||
for(int y = 0; y < ((double) buckets[x] / highestBucket) * ((double) size / 4); y++) {
|
||||
image.setRGB(x, size - y - 1, buildRGBA(0));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package structure;
|
||||
|
||||
import com.dfsek.terra.addons.structure.structures.tokenizer.Lookahead;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
public class LookaheadTest {
|
||||
@Test
|
||||
public void lookahead() {
|
||||
Lookahead lookahead = new Lookahead(new StringReader("Test string..."));
|
||||
|
||||
for(int i = 0; lookahead.next(i) != null; i++) {
|
||||
System.out.print(lookahead.next(i).getCharacter());
|
||||
}
|
||||
while(lookahead.next(0) != null) {
|
||||
System.out.print(lookahead.consume().getCharacter());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package structure;
|
||||
|
||||
import com.dfsek.terra.addons.structure.structures.parser.Parser;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.structure.structures.parser.lang.variables.Variable;
|
||||
import com.dfsek.terra.addons.structure.structures.tokenizer.Position;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParserTest {
|
||||
@Test
|
||||
public void parse() throws IOException, ParseException {
|
||||
Parser parser = new Parser(IOUtils.toString(getClass().getResourceAsStream("/test.tesf"), Charset.defaultCharset()));
|
||||
|
||||
parser.registerFunction("test", new FunctionBuilder<Test1>() {
|
||||
@Override
|
||||
public Test1 build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new Test1(argumentList.get(0), argumentList.get(1), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int argNumber() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
switch(position) {
|
||||
case 0:
|
||||
return Returnable.ReturnType.STRING;
|
||||
case 1:
|
||||
return Returnable.ReturnType.NUMBER;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
long l = System.nanoTime();
|
||||
Block block = parser.parse();
|
||||
long t = System.nanoTime() - l;
|
||||
System.out.println("Took " + (double) t / 1000000);
|
||||
|
||||
block.apply(null, new HashMap<>());
|
||||
|
||||
block.apply(null, new HashMap<>());
|
||||
}
|
||||
|
||||
private static class Test1 implements Function<Void> {
|
||||
private final Returnable<?> a;
|
||||
private final Returnable<?> b;
|
||||
private final Position position;
|
||||
|
||||
public Test1(Returnable<?> a, Returnable<?> b, Position position) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||
System.out.println("string: " + a.apply(implementationArguments, variableMap) + ", double: " + b.apply(implementationArguments, variableMap));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.VOID;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user